2 * nfsproc2.c Process version 2 NFS requests.
3 * linux/fs/nfsd/nfs2proc.c
5 * Process version 2 NFS requests.
7 * Copyright (C) 1995-1997 Olaf Kirch <okir@monad.swb.de>
10 #include <linux/linkage.h>
11 #include <linux/time.h>
12 #include <linux/errno.h>
14 #include <linux/stat.h>
15 #include <linux/fcntl.h>
16 #include <linux/net.h>
18 #include <linux/namei.h>
19 #include <linux/unistd.h>
20 #include <linux/slab.h>
22 #include <linux/sunrpc/svc.h>
23 #include <linux/nfsd/nfsd.h>
24 #include <linux/nfsd/cache.h>
25 #include <linux/nfsd/xdr.h>
27 typedef struct svc_rqst svc_rqst;
28 typedef struct svc_buf svc_buf;
30 #define NFSDDBG_FACILITY NFSDDBG_PROC
34 nfsd_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
40 * Get a file's attributes
41 * N.B. After this call resp->fh needs an fh_put
44 nfsd_proc_getattr(struct svc_rqst *rqstp, struct nfsd_fhandle *argp,
45 struct nfsd_attrstat *resp)
47 dprintk("nfsd: GETATTR %s\n", SVCFH_fmt(&argp->fh));
49 fh_copy(&resp->fh, &argp->fh);
50 return fh_verify(rqstp, &resp->fh, 0, MAY_NOP);
54 * Set a file's attributes
55 * N.B. After this call resp->fh needs an fh_put
58 nfsd_proc_setattr(struct svc_rqst *rqstp, struct nfsd_sattrargs *argp,
59 struct nfsd_attrstat *resp)
61 dprintk("nfsd: SETATTR %s, valid=%x, size=%ld\n",
63 argp->attrs.ia_valid, (long) argp->attrs.ia_size);
65 fh_copy(&resp->fh, &argp->fh);
66 return nfsd_setattr(rqstp, &resp->fh, &argp->attrs,0, (time_t)0);
70 * Look up a path name component
71 * Note: the dentry in the resp->fh may be negative if the file
73 * N.B. After this call resp->fh needs an fh_put
76 nfsd_proc_lookup(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
77 struct nfsd_diropres *resp)
81 dprintk("nfsd: LOOKUP %s %.*s\n",
82 SVCFH_fmt(&argp->fh), argp->len, argp->name);
84 fh_init(&resp->fh, NFS_FHSIZE);
85 nfserr = nfsd_lookup(rqstp, &argp->fh, argp->name, argp->len,
96 nfsd_proc_readlink(struct svc_rqst *rqstp, struct nfsd_readlinkargs *argp,
97 struct nfsd_readlinkres *resp)
101 dprintk("nfsd: READLINK %s\n", SVCFH_fmt(&argp->fh));
103 /* Read the symlink. */
104 resp->len = NFS_MAXPATHLEN;
105 nfserr = nfsd_readlink(rqstp, &argp->fh, argp->buffer, &resp->len);
112 * Read a portion of a file.
113 * N.B. After this call resp->fh needs an fh_put
116 nfsd_proc_read(struct svc_rqst *rqstp, struct nfsd_readargs *argp,
117 struct nfsd_readres *resp)
121 dprintk("nfsd: READ %s %d bytes at %d\n",
122 SVCFH_fmt(&argp->fh),
123 argp->count, argp->offset);
125 /* Obtain buffer pointer for payload. 19 is 1 word for
126 * status, 17 words for fattr, and 1 word for the byte count.
129 if (NFSSVC_MAXBLKSIZE < argp->count) {
131 "oversized read request from %08x:%d (%d bytes)\n",
132 ntohl(rqstp->rq_addr.sin_addr.s_addr),
133 ntohs(rqstp->rq_addr.sin_port),
135 argp->count = NFSSVC_MAXBLKSIZE;
137 svc_reserve(rqstp, (19<<2) + argp->count + 4);
139 resp->count = argp->count;
140 nfserr = nfsd_read(rqstp, fh_copy(&resp->fh, &argp->fh),
142 argp->vec, argp->vlen,
149 * Write data to a file
150 * N.B. After this call resp->fh needs an fh_put
153 nfsd_proc_write(struct svc_rqst *rqstp, struct nfsd_writeargs *argp,
154 struct nfsd_attrstat *resp)
159 dprintk("nfsd: WRITE %s %d bytes at %d\n",
160 SVCFH_fmt(&argp->fh),
161 argp->len, argp->offset);
163 nfserr = nfsd_write(rqstp, fh_copy(&resp->fh, &argp->fh),
165 argp->vec, argp->vlen,
172 * CREATE processing is complicated. The keyword here is `overloaded.'
173 * The parent directory is kept locked between the check for existence
174 * and the actual create() call in compliance with VFS protocols.
175 * N.B. After this call _both_ argp->fh and resp->fh need an fh_put
178 nfsd_proc_create(struct svc_rqst *rqstp, struct nfsd_createargs *argp,
179 struct nfsd_diropres *resp)
181 svc_fh *dirfhp = &argp->fh;
182 svc_fh *newfhp = &resp->fh;
183 struct iattr *attr = &argp->attrs;
185 struct dentry *dchild;
186 int nfserr, type, mode;
187 dev_t rdev = 0, wanted = new_decode_dev(attr->ia_size);
189 dprintk("nfsd: CREATE %s %.*s\n",
190 SVCFH_fmt(dirfhp), argp->len, argp->name);
192 /* First verify the parent file handle */
193 nfserr = fh_verify(rqstp, dirfhp, S_IFDIR, MAY_EXEC);
195 goto done; /* must fh_put dirfhp even on error */
197 /* Check for MAY_WRITE in nfsd_create if necessary */
199 nfserr = nfserr_acces;
202 nfserr = nfserr_exist;
203 if (isdotent(argp->name, argp->len))
206 dchild = lookup_one_len(argp->name, dirfhp->fh_dentry, argp->len);
207 if (IS_ERR(dchild)) {
208 nfserr = nfserrno(PTR_ERR(dchild));
211 fh_init(newfhp, NFS_FHSIZE);
212 nfserr = fh_compose(newfhp, dirfhp->fh_export, dchild, dirfhp);
213 if (!nfserr && !dchild->d_inode)
214 nfserr = nfserr_noent;
216 if (nfserr != nfserr_noent)
219 * If the new file handle wasn't verified, we can't tell
220 * whether the file exists or not. Time to bail ...
222 nfserr = nfserr_acces;
223 if (!newfhp->fh_dentry) {
225 "nfsd_proc_create: file handle not verified\n");
230 inode = newfhp->fh_dentry->d_inode;
232 /* Unfudge the mode bits */
233 if (attr->ia_valid & ATTR_MODE) {
234 type = attr->ia_mode & S_IFMT;
235 mode = attr->ia_mode & ~S_IFMT;
237 /* no type, so if target exists, assume same as that,
238 * else assume a file */
240 type = inode->i_mode & S_IFMT;
244 /* reserve rdev for later checking */
245 rdev = inode->i_rdev;
246 attr->ia_valid |= ATTR_SIZE;
250 /* this is probably a permission check..
251 * at least IRIX implements perm checking on
252 * echo thing > device-special-file-or-pipe
253 * by doing a CREATE with type==0
255 nfserr = nfsd_permission(newfhp->fh_export,
257 MAY_WRITE|MAY_LOCAL_ACCESS);
258 if (nfserr && nfserr != nfserr_rofs)
265 type = inode->i_mode & S_IFMT;
266 mode = inode->i_mode & ~S_IFMT;
272 attr->ia_valid |= ATTR_MODE;
273 attr->ia_mode = mode;
275 /* Special treatment for non-regular files according to the
276 * gospel of sun micro
278 if (type != S_IFREG) {
280 if (type != S_IFBLK && type != S_IFCHR) {
282 } else if (type == S_IFCHR && !(attr->ia_valid & ATTR_SIZE)) {
283 /* If you think you've seen the worst, grok this. */
286 /* Okay, char or block special */
292 /* we've used the SIZE information, so discard it */
293 attr->ia_valid &= ~ATTR_SIZE;
295 /* Make sure the type and device matches */
296 nfserr = nfserr_exist;
297 if (inode && type != (inode->i_mode & S_IFMT))
303 /* File doesn't exist. Create it and set attrs */
304 nfserr = nfsd_create(rqstp, dirfhp, argp->name, argp->len,
305 attr, type, rdev, newfhp);
306 } else if (type == S_IFREG) {
307 dprintk("nfsd: existing %s, valid=%x, size=%ld\n",
308 argp->name, attr->ia_valid, (long) attr->ia_size);
309 /* File already exists. We ignore all attributes except
310 * size, so that creat() behaves exactly like
311 * open(..., O_CREAT|O_TRUNC|O_WRONLY).
313 attr->ia_valid &= ATTR_SIZE;
315 nfserr = nfsd_setattr(rqstp, newfhp, attr, 0, (time_t)0);
319 /* We don't really need to unlock, as fh_put does it. */
328 nfsd_proc_remove(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
333 dprintk("nfsd: REMOVE %s %.*s\n", SVCFH_fmt(&argp->fh),
334 argp->len, argp->name);
336 /* Unlink. -SIFDIR means file must not be a directory */
337 nfserr = nfsd_unlink(rqstp, &argp->fh, -S_IFDIR, argp->name, argp->len);
343 nfsd_proc_rename(struct svc_rqst *rqstp, struct nfsd_renameargs *argp,
348 dprintk("nfsd: RENAME %s %.*s -> \n",
349 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname);
350 dprintk("nfsd: -> %s %.*s\n",
351 SVCFH_fmt(&argp->tfh), argp->tlen, argp->tname);
353 nfserr = nfsd_rename(rqstp, &argp->ffh, argp->fname, argp->flen,
354 &argp->tfh, argp->tname, argp->tlen);
361 nfsd_proc_link(struct svc_rqst *rqstp, struct nfsd_linkargs *argp,
366 dprintk("nfsd: LINK %s ->\n",
367 SVCFH_fmt(&argp->ffh));
368 dprintk("nfsd: %s %.*s\n",
369 SVCFH_fmt(&argp->tfh),
373 nfserr = nfsd_link(rqstp, &argp->tfh, argp->tname, argp->tlen,
381 nfsd_proc_symlink(struct svc_rqst *rqstp, struct nfsd_symlinkargs *argp,
387 dprintk("nfsd: SYMLINK %s %.*s -> %.*s\n",
388 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname,
389 argp->tlen, argp->tname);
391 fh_init(&newfh, NFS_FHSIZE);
393 * Create the link, look up new file and set attrs.
395 nfserr = nfsd_symlink(rqstp, &argp->ffh, argp->fname, argp->flen,
396 argp->tname, argp->tlen,
397 &newfh, &argp->attrs);
406 * Make directory. This operation is not idempotent.
407 * N.B. After this call resp->fh needs an fh_put
410 nfsd_proc_mkdir(struct svc_rqst *rqstp, struct nfsd_createargs *argp,
411 struct nfsd_diropres *resp)
415 dprintk("nfsd: MKDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
417 if (resp->fh.fh_dentry) {
419 "nfsd_proc_mkdir: response already verified??\n");
422 argp->attrs.ia_valid &= ~ATTR_SIZE;
423 fh_init(&resp->fh, NFS_FHSIZE);
424 nfserr = nfsd_create(rqstp, &argp->fh, argp->name, argp->len,
425 &argp->attrs, S_IFDIR, 0, &resp->fh);
434 nfsd_proc_rmdir(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
439 dprintk("nfsd: RMDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
441 nfserr = nfsd_unlink(rqstp, &argp->fh, S_IFDIR, argp->name, argp->len);
447 * Read a portion of a directory.
450 nfsd_proc_readdir(struct svc_rqst *rqstp, struct nfsd_readdirargs *argp,
451 struct nfsd_readdirres *resp)
456 dprintk("nfsd: READDIR %s %d bytes at %d\n",
457 SVCFH_fmt(&argp->fh),
458 argp->count, argp->cookie);
460 /* Shrink to the client read size */
461 count = (argp->count >> 2) - 2;
463 /* Make sure we've room for the NULL ptr & eof flag */
468 resp->buffer = argp->buffer;
470 resp->buflen = count;
471 resp->common.err = nfs_ok;
472 /* Read directory and encode entries on the fly */
473 offset = argp->cookie;
474 nfserr = nfsd_readdir(rqstp, &argp->fh, &offset,
475 &resp->common, nfssvc_encode_entry);
477 resp->count = resp->buffer - argp->buffer;
479 *resp->offset = htonl(offset);
486 * Get file system info
489 nfsd_proc_statfs(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
490 struct nfsd_statfsres *resp)
494 dprintk("nfsd: STATFS %s\n", SVCFH_fmt(&argp->fh));
496 nfserr = nfsd_statfs(rqstp, &argp->fh, &resp->stats);
502 * NFSv2 Server procedures.
503 * Only the results of non-idempotent operations are cached.
505 #define nfsd_proc_none NULL
506 #define nfssvc_release_none NULL
507 struct nfsd_void { int dummy; };
509 #define PROC(name, argt, rest, relt, cache, respsize) \
510 { (svc_procfunc) nfsd_proc_##name, \
511 (kxdrproc_t) nfssvc_decode_##argt, \
512 (kxdrproc_t) nfssvc_encode_##rest, \
513 (kxdrproc_t) nfssvc_release_##relt, \
514 sizeof(struct nfsd_##argt), \
515 sizeof(struct nfsd_##rest), \
521 #define ST 1 /* status */
522 #define FH 8 /* filehandle */
523 #define AT 18 /* attributes */
525 static struct svc_procedure nfsd_procedures2[18] = {
526 PROC(null, void, void, none, RC_NOCACHE, ST),
527 PROC(getattr, fhandle, attrstat, fhandle, RC_NOCACHE, ST+AT),
528 PROC(setattr, sattrargs, attrstat, fhandle, RC_REPLBUFF, ST+AT),
529 PROC(none, void, void, none, RC_NOCACHE, ST),
530 PROC(lookup, diropargs, diropres, fhandle, RC_NOCACHE, ST+FH+AT),
531 PROC(readlink, readlinkargs, readlinkres, none, RC_NOCACHE, ST+1+NFS_MAXPATHLEN/4),
532 PROC(read, readargs, readres, fhandle, RC_NOCACHE, ST+AT+1+NFSSVC_MAXBLKSIZE),
533 PROC(none, void, void, none, RC_NOCACHE, ST),
534 PROC(write, writeargs, attrstat, fhandle, RC_REPLBUFF, ST+AT),
535 PROC(create, createargs, diropres, fhandle, RC_REPLBUFF, ST+FH+AT),
536 PROC(remove, diropargs, void, none, RC_REPLSTAT, ST),
537 PROC(rename, renameargs, void, none, RC_REPLSTAT, ST),
538 PROC(link, linkargs, void, none, RC_REPLSTAT, ST),
539 PROC(symlink, symlinkargs, void, none, RC_REPLSTAT, ST),
540 PROC(mkdir, createargs, diropres, fhandle, RC_REPLBUFF, ST+FH+AT),
541 PROC(rmdir, diropargs, void, none, RC_REPLSTAT, ST),
542 PROC(readdir, readdirargs, readdirres, none, RC_REPLBUFF, 0),
543 PROC(statfs, fhandle, statfsres, none, RC_NOCACHE, ST+5),
547 struct svc_version nfsd_version2 = {
550 .vs_proc = nfsd_procedures2,
551 .vs_dispatch = nfsd_dispatch,
552 .vs_xdrsize = NFS2_SVC_XDRSIZE,
556 * Map errnos to NFS errnos.
566 { nfserr_perm, -EPERM },
567 { nfserr_noent, -ENOENT },
569 { nfserr_nxio, -ENXIO },
570 { nfserr_acces, -EACCES },
571 { nfserr_exist, -EEXIST },
572 { nfserr_xdev, -EXDEV },
573 { nfserr_mlink, -EMLINK },
574 { nfserr_nodev, -ENODEV },
575 { nfserr_notdir, -ENOTDIR },
576 { nfserr_isdir, -EISDIR },
577 { nfserr_inval, -EINVAL },
578 { nfserr_fbig, -EFBIG },
579 { nfserr_nospc, -ENOSPC },
580 { nfserr_rofs, -EROFS },
581 { nfserr_mlink, -EMLINK },
582 { nfserr_nametoolong, -ENAMETOOLONG },
583 { nfserr_notempty, -ENOTEMPTY },
585 { nfserr_dquot, -EDQUOT },
587 { nfserr_stale, -ESTALE },
588 { nfserr_jukebox, -ETIMEDOUT },
589 { nfserr_dropit, -EAGAIN },
590 { nfserr_dropit, -ENOMEM },
595 for (i = 0; nfs_errtbl[i].nfserr != -1; i++) {
596 if (nfs_errtbl[i].syserr == errno)
597 return nfs_errtbl[i].nfserr;
599 printk (KERN_INFO "nfsd: non-standard errno: %d\n", errno);