2 * linux/fs/nfsd/nfs3proc.c
4 * Process version 3 NFS requests.
6 * Copyright (C) 1996, 1997, 1998 Olaf Kirch <okir@monad.swb.de>
9 #include <linux/linkage.h>
10 #include <linux/sched.h>
11 #include <linux/errno.h>
12 #include <linux/locks.h>
14 #include <linux/ext2_fs.h>
15 #include <linux/stat.h>
16 #include <linux/fcntl.h>
17 #include <linux/net.h>
19 #include <linux/version.h>
20 #include <linux/unistd.h>
21 #include <linux/slab.h>
22 #include <linux/major.h>
24 #include <linux/sunrpc/svc.h>
25 #include <linux/nfsd/nfsd.h>
26 #include <linux/nfsd/cache.h>
27 #include <linux/nfsd/xdr3.h>
28 #include <linux/nfs3.h>
30 #define NFSDDBG_FACILITY NFSDDBG_PROC
32 #define RETURN_STATUS(st) { resp->status = (st); return (st); }
34 static int nfs3_ftypes[] = {
41 S_IFSOCK, /* NF3SOCK */
42 S_IFIFO, /* NF3FIFO */
46 * Reserve room in the send buffer
49 svcbuf_reserve(struct svc_buf *buf, u32 **ptr, int *len, int nr)
52 *len = buf->buflen - buf->len - nr;
59 nfsd3_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
65 * Get a file's attributes
68 nfsd3_proc_getattr(struct svc_rqst *rqstp, struct nfsd_fhandle *argp,
69 struct nfsd3_attrstat *resp)
73 dprintk("nfsd: GETATTR(3) %s\n",
74 SVCFH_fmt(&argp->fh));
76 fh_copy(&resp->fh, &argp->fh);
77 nfserr = fh_verify(rqstp, &resp->fh, 0, MAY_NOP);
78 RETURN_STATUS(nfserr);
82 * Set a file's attributes
85 nfsd3_proc_setattr(struct svc_rqst *rqstp, struct nfsd3_sattrargs *argp,
86 struct nfsd3_attrstat *resp)
90 dprintk("nfsd: SETATTR(3) %s\n",
91 SVCFH_fmt(&argp->fh));
93 fh_copy(&resp->fh, &argp->fh);
94 nfserr = nfsd_setattr(rqstp, &resp->fh, &argp->attrs,
95 argp->check_guard, argp->guardtime);
96 RETURN_STATUS(nfserr);
100 * Look up a path name component
103 nfsd3_proc_lookup(struct svc_rqst *rqstp, struct nfsd3_diropargs *argp,
104 struct nfsd3_diropres *resp)
108 dprintk("nfsd: LOOKUP(3) %s %.*s\n",
109 SVCFH_fmt(&argp->fh),
113 fh_copy(&resp->dirfh, &argp->fh);
114 fh_init(&resp->fh, NFS3_FHSIZE);
116 nfserr = nfsd_lookup(rqstp, &resp->dirfh,
120 RETURN_STATUS(nfserr);
127 nfsd3_proc_access(struct svc_rqst *rqstp, struct nfsd3_accessargs *argp,
128 struct nfsd3_accessres *resp)
132 dprintk("nfsd: ACCESS(3) %s 0x%x\n",
133 SVCFH_fmt(&argp->fh),
136 fh_copy(&resp->fh, &argp->fh);
137 resp->access = argp->access;
138 nfserr = nfsd_access(rqstp, &resp->fh, &resp->access);
139 RETURN_STATUS(nfserr);
146 nfsd3_proc_readlink(struct svc_rqst *rqstp, struct nfsd_fhandle *argp,
147 struct nfsd3_readlinkres *resp)
152 dprintk("nfsd: READLINK(3) %s\n", SVCFH_fmt(&argp->fh));
154 /* Reserve room for status, post_op_attr, and path length */
155 svcbuf_reserve(&rqstp->rq_resbuf, &path, &dummy,
156 1 + NFS3_POST_OP_ATTR_WORDS + 1);
158 /* Read the symlink. */
159 fh_copy(&resp->fh, &argp->fh);
160 resp->len = NFS3_MAXPATHLEN;
161 nfserr = nfsd_readlink(rqstp, &resp->fh, (char *) path, &resp->len);
162 RETURN_STATUS(nfserr);
166 * Read a portion of a file.
169 nfsd3_proc_read(struct svc_rqst *rqstp, struct nfsd3_readargs *argp,
170 struct nfsd3_readres *resp)
175 dprintk("nfsd: READ(3) %s %lu bytes at %lu\n",
176 SVCFH_fmt(&argp->fh),
177 (unsigned long) argp->count,
178 (unsigned long) argp->offset);
180 /* Obtain buffer pointer for payload.
181 * 1 (status) + 22 (post_op_attr) + 1 (count) + 1 (eof)
182 * + 1 (xdr opaque byte count) = 26
184 svcbuf_reserve(&rqstp->rq_resbuf, &buffer, &avail,
185 1 + NFS3_POST_OP_ATTR_WORDS + 3);
187 resp->count = argp->count;
188 if ((avail << 2) < resp->count)
189 resp->count = avail << 2;
191 svc_reserve(rqstp, ((1 + NFS3_POST_OP_ATTR_WORDS + 3)<<2) + argp->count +4);
193 fh_copy(&resp->fh, &argp->fh);
194 nfserr = nfsd_read(rqstp, &resp->fh,
199 struct inode *inode = resp->fh.fh_dentry->d_inode;
201 resp->eof = (argp->offset + resp->count) >= inode->i_size;
204 RETURN_STATUS(nfserr);
208 * Write data to a file
211 nfsd3_proc_write(struct svc_rqst *rqstp, struct nfsd3_writeargs *argp,
212 struct nfsd3_writeres *resp)
216 dprintk("nfsd: WRITE(3) %s %d bytes at %ld%s\n",
217 SVCFH_fmt(&argp->fh),
219 (unsigned long) argp->offset,
220 argp->stable? " stable" : "");
222 fh_copy(&resp->fh, &argp->fh);
223 resp->committed = argp->stable;
224 nfserr = nfsd_write(rqstp, &resp->fh,
229 resp->count = argp->count;
230 RETURN_STATUS(nfserr);
234 * With NFSv3, CREATE processing is a lot easier than with NFSv2.
235 * At least in theory; we'll see how it fares in practice when the
236 * first reports about SunOS compatibility problems start to pour in...
239 nfsd3_proc_create(struct svc_rqst *rqstp, struct nfsd3_createargs *argp,
240 struct nfsd3_diropres *resp)
242 svc_fh *dirfhp, *newfhp = NULL;
246 dprintk("nfsd: CREATE(3) %s %.*s\n",
247 SVCFH_fmt(&argp->fh),
251 dirfhp = fh_copy(&resp->dirfh, &argp->fh);
252 newfhp = fh_init(&resp->fh, NFS3_FHSIZE);
255 /* Get the directory inode */
256 nfserr = fh_verify(rqstp, dirfhp, S_IFDIR, MAY_CREATE);
258 RETURN_STATUS(nfserr);
260 /* Unfudge the mode bits */
261 attr->ia_mode &= ~S_IFMT;
262 if (!(attr->ia_valid & ATTR_MODE)) {
263 attr->ia_valid |= ATTR_MODE;
264 attr->ia_mode = S_IFREG;
266 attr->ia_mode = (attr->ia_mode & ~S_IFMT) | S_IFREG;
269 /* Now create the file and set attributes */
270 nfserr = nfsd_create_v3(rqstp, dirfhp, argp->name, argp->len,
272 argp->createmode, argp->verf);
274 RETURN_STATUS(nfserr);
278 * Make directory. This operation is not idempotent.
281 nfsd3_proc_mkdir(struct svc_rqst *rqstp, struct nfsd3_createargs *argp,
282 struct nfsd3_diropres *resp)
286 dprintk("nfsd: MKDIR(3) %s %.*s\n",
287 SVCFH_fmt(&argp->fh),
291 argp->attrs.ia_valid &= ~ATTR_SIZE;
292 fh_copy(&resp->dirfh, &argp->fh);
293 fh_init(&resp->fh, NFS3_FHSIZE);
294 nfserr = nfsd_create(rqstp, &resp->dirfh, argp->name, argp->len,
295 &argp->attrs, S_IFDIR, 0, &resp->fh);
297 RETURN_STATUS(nfserr);
301 nfsd3_proc_symlink(struct svc_rqst *rqstp, struct nfsd3_symlinkargs *argp,
302 struct nfsd3_diropres *resp)
306 dprintk("nfsd: SYMLINK(3) %s %.*s -> %.*s\n",
307 SVCFH_fmt(&argp->ffh),
308 argp->flen, argp->fname,
309 argp->tlen, argp->tname);
311 fh_copy(&resp->dirfh, &argp->ffh);
312 fh_init(&resp->fh, NFS3_FHSIZE);
313 nfserr = nfsd_symlink(rqstp, &resp->dirfh, argp->fname, argp->flen,
314 argp->tname, argp->tlen,
315 &resp->fh, &argp->attrs);
316 RETURN_STATUS(nfserr);
320 * Make socket/fifo/device.
323 nfsd3_proc_mknod(struct svc_rqst *rqstp, struct nfsd3_mknodargs *argp,
324 struct nfsd3_diropres *resp)
329 dprintk("nfsd: MKNOD(3) %s %.*s\n",
330 SVCFH_fmt(&argp->fh),
334 fh_copy(&resp->dirfh, &argp->fh);
335 fh_init(&resp->fh, NFS3_FHSIZE);
337 if (argp->ftype == 0 || argp->ftype >= NF3BAD)
338 RETURN_STATUS(nfserr_inval);
339 if (argp->ftype == NF3CHR || argp->ftype == NF3BLK) {
340 if ((argp->ftype == NF3CHR && argp->major >= MAX_CHRDEV)
341 || (argp->ftype == NF3BLK && argp->major >= MAX_BLKDEV)
342 || argp->minor > 0xFF)
343 RETURN_STATUS(nfserr_inval);
344 rdev = MKDEV(argp->major, argp->minor);
346 if (argp->ftype != NF3SOCK && argp->ftype != NF3FIFO)
347 RETURN_STATUS(nfserr_inval);
349 type = nfs3_ftypes[argp->ftype];
350 nfserr = nfsd_create(rqstp, &resp->dirfh, argp->name, argp->len,
351 &argp->attrs, type, rdev, &resp->fh);
353 RETURN_STATUS(nfserr);
357 * Remove file/fifo/socket etc.
360 nfsd3_proc_remove(struct svc_rqst *rqstp, struct nfsd3_diropargs *argp,
361 struct nfsd3_attrstat *resp)
365 dprintk("nfsd: REMOVE(3) %s %.*s\n",
366 SVCFH_fmt(&argp->fh),
370 /* Unlink. -S_IFDIR means file must not be a directory */
371 fh_copy(&resp->fh, &argp->fh);
372 nfserr = nfsd_unlink(rqstp, &resp->fh, -S_IFDIR, argp->name, argp->len);
373 RETURN_STATUS(nfserr);
380 nfsd3_proc_rmdir(struct svc_rqst *rqstp, struct nfsd3_diropargs *argp,
381 struct nfsd3_attrstat *resp)
385 dprintk("nfsd: RMDIR(3) %s %.*s\n",
386 SVCFH_fmt(&argp->fh),
390 fh_copy(&resp->fh, &argp->fh);
391 nfserr = nfsd_unlink(rqstp, &resp->fh, S_IFDIR, argp->name, argp->len);
392 RETURN_STATUS(nfserr);
396 nfsd3_proc_rename(struct svc_rqst *rqstp, struct nfsd3_renameargs *argp,
397 struct nfsd3_renameres *resp)
401 dprintk("nfsd: RENAME(3) %s %.*s ->\n",
402 SVCFH_fmt(&argp->ffh),
405 dprintk("nfsd: -> %s %.*s\n",
406 SVCFH_fmt(&argp->tfh),
410 fh_copy(&resp->ffh, &argp->ffh);
411 fh_copy(&resp->tfh, &argp->tfh);
412 nfserr = nfsd_rename(rqstp, &resp->ffh, argp->fname, argp->flen,
413 &resp->tfh, argp->tname, argp->tlen);
414 RETURN_STATUS(nfserr);
418 nfsd3_proc_link(struct svc_rqst *rqstp, struct nfsd3_linkargs *argp,
419 struct nfsd3_linkres *resp)
423 dprintk("nfsd: LINK(3) %s ->\n",
424 SVCFH_fmt(&argp->ffh));
425 dprintk("nfsd: -> %s %.*s\n",
426 SVCFH_fmt(&argp->tfh),
430 fh_copy(&resp->fh, &argp->ffh);
431 fh_copy(&resp->tfh, &argp->tfh);
432 nfserr = nfsd_link(rqstp, &resp->tfh, argp->tname, argp->tlen,
434 RETURN_STATUS(nfserr);
438 * Read a portion of a directory.
441 nfsd3_proc_readdir(struct svc_rqst *rqstp, struct nfsd3_readdirargs *argp,
442 struct nfsd3_readdirres *resp)
448 dprintk("nfsd: READDIR(3) %s %d bytes at %d\n",
449 SVCFH_fmt(&argp->fh),
450 argp->count, (u32) argp->cookie);
452 /* Reserve buffer space for status, attributes and verifier */
453 svcbuf_reserve(&rqstp->rq_resbuf, &buffer, &count,
454 1 + NFS3_POST_OP_ATTR_WORDS + 2);
456 /* Make sure we've room for the NULL ptr & eof flag, and shrink to
457 * client read size */
458 if ((count -= 2) > (want = (argp->count >> 2) - 2))
461 /* Read directory and encode entries on the fly */
462 fh_copy(&resp->fh, &argp->fh);
463 nfserr = nfsd_readdir(rqstp, &resp->fh, (loff_t) argp->cookie,
464 nfs3svc_encode_entry,
465 buffer, &count, argp->verf);
466 memcpy(resp->verf, argp->verf, 8);
469 RETURN_STATUS(nfserr);
473 * Read a portion of a directory, including file handles and attrs.
474 * For now, we choose to ignore the dircount parameter.
477 nfsd3_proc_readdirplus(struct svc_rqst *rqstp, struct nfsd3_readdirargs *argp,
478 struct nfsd3_readdirres *resp)
481 int nfserr, count, want;
483 dprintk("nfsd: READDIR+(3) %s %d bytes at %d\n",
484 SVCFH_fmt(&argp->fh),
485 argp->count, (u32) argp->cookie);
487 /* Reserve buffer space for status, attributes and verifier */
488 svcbuf_reserve(&rqstp->rq_resbuf, &buffer, &count,
489 1 + NFS3_POST_OP_ATTR_WORDS + 2);
491 /* Make sure we've room for the NULL ptr & eof flag, and shrink to
492 * client read size */
493 if ((count -= 2) > (want = argp->count >> 2))
496 /* Read directory and encode entries on the fly */
497 fh_copy(&resp->fh, &argp->fh);
498 nfserr = nfsd_readdir(rqstp, &resp->fh, (loff_t) argp->cookie,
499 nfs3svc_encode_entry_plus,
500 buffer, &count, argp->verf);
501 memcpy(resp->verf, argp->verf, 8);
504 RETURN_STATUS(nfserr);
508 * Get file system stats
511 nfsd3_proc_fsstat(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
512 struct nfsd3_fsstatres *resp)
516 dprintk("nfsd: FSSTAT(3) %s\n",
517 SVCFH_fmt(&argp->fh));
519 nfserr = nfsd_statfs(rqstp, &argp->fh, &resp->stats);
521 RETURN_STATUS(nfserr);
525 * Get file system info
528 nfsd3_proc_fsinfo(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
529 struct nfsd3_fsinfores *resp)
533 dprintk("nfsd: FSINFO(3) %s\n",
534 SVCFH_fmt(&argp->fh));
536 resp->f_rtmax = NFSSVC_MAXBLKSIZE;
537 resp->f_rtpref = NFSSVC_MAXBLKSIZE;
538 resp->f_rtmult = PAGE_SIZE;
539 resp->f_wtmax = NFSSVC_MAXBLKSIZE;
540 resp->f_wtpref = NFSSVC_MAXBLKSIZE;
541 resp->f_wtmult = PAGE_SIZE;
542 resp->f_dtpref = PAGE_SIZE;
543 resp->f_maxfilesize = ~(u32) 0;
544 resp->f_properties = NFS3_FSF_DEFAULT;
546 nfserr = fh_verify(rqstp, &argp->fh, 0, MAY_NOP);
548 /* Check special features of the file system. May request
549 * different read/write sizes for file systems known to have
550 * problems with large blocks */
552 struct super_block *sb = argp->fh.fh_dentry->d_inode->i_sb;
554 /* Note that we don't care for remote fs's here */
555 if (sb->s_magic == 0x4d44 /* MSDOS_SUPER_MAGIC */) {
556 resp->f_properties = NFS3_FSF_BILLYBOY;
558 resp->f_maxfilesize = sb->s_maxbytes;
562 RETURN_STATUS(nfserr);
566 * Get pathconf info for the specified file
569 nfsd3_proc_pathconf(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
570 struct nfsd3_pathconfres *resp)
574 dprintk("nfsd: PATHCONF(3) %s\n",
575 SVCFH_fmt(&argp->fh));
577 /* Set default pathconf */
578 resp->p_link_max = 255; /* at least */
579 resp->p_name_max = 255; /* at least */
580 resp->p_no_trunc = 0;
581 resp->p_chown_restricted = 1;
582 resp->p_case_insensitive = 0;
583 resp->p_case_preserving = 1;
585 nfserr = fh_verify(rqstp, &argp->fh, 0, MAY_NOP);
588 struct super_block *sb = argp->fh.fh_dentry->d_inode->i_sb;
590 /* Note that we don't care for remote fs's here */
591 switch (sb->s_magic) {
592 case EXT2_SUPER_MAGIC:
593 resp->p_link_max = EXT2_LINK_MAX;
594 resp->p_name_max = EXT2_NAME_LEN;
596 case 0x4d44: /* MSDOS_SUPER_MAGIC */
597 resp->p_case_insensitive = 1;
598 resp->p_case_preserving = 0;
604 RETURN_STATUS(nfserr);
609 * Commit a file (range) to stable storage.
612 nfsd3_proc_commit(struct svc_rqst * rqstp, struct nfsd3_commitargs *argp,
613 struct nfsd3_commitres *resp)
617 dprintk("nfsd: COMMIT(3) %s %d@%ld\n",
618 SVCFH_fmt(&argp->fh),
620 (unsigned long) argp->offset);
622 if (argp->offset > NFS_OFFSET_MAX)
623 RETURN_STATUS(nfserr_inval);
625 fh_copy(&resp->fh, &argp->fh);
626 nfserr = nfsd_commit(rqstp, &resp->fh, argp->offset, argp->count);
628 RETURN_STATUS(nfserr);
633 * NFSv3 Server procedures.
634 * Only the results of non-idempotent operations are cached.
636 #define nfs3svc_decode_voidargs NULL
637 #define nfs3svc_release_void NULL
638 #define nfs3svc_decode_fhandleargs nfs3svc_decode_fhandle
639 #define nfs3svc_encode_attrstatres nfs3svc_encode_attrstat
640 #define nfs3svc_encode_wccstatres nfs3svc_encode_wccstat
641 #define nfsd3_mkdirargs nfsd3_createargs
642 #define nfsd3_readdirplusargs nfsd3_readdirargs
643 #define nfsd3_fhandleargs nfsd_fhandle
644 #define nfsd3_fhandleres nfsd3_attrstat
645 #define nfsd3_attrstatres nfsd3_attrstat
646 #define nfsd3_wccstatres nfsd3_attrstat
647 #define nfsd3_createres nfsd3_diropres
648 #define nfsd3_voidres nfsd3_voidargs
649 struct nfsd3_voidargs { int dummy; };
651 #define PROC(name, argt, rest, relt, cache, respsize) \
652 { (svc_procfunc) nfsd3_proc_##name, \
653 (kxdrproc_t) nfs3svc_decode_##argt##args, \
654 (kxdrproc_t) nfs3svc_encode_##rest##res, \
655 (kxdrproc_t) nfs3svc_release_##relt, \
656 sizeof(struct nfsd3_##argt##args), \
657 sizeof(struct nfsd3_##rest##res), \
663 #define ST 1 /* status*/
664 #define FH 17 /* filehandle with length */
665 #define AT 21 /* attributes */
666 #define pAT (1+AT) /* post attributes - conditional */
667 #define WC (7+pAT) /* WCC attributes */
669 struct svc_procedure nfsd_procedures3[22] = {
670 PROC(null, void, void, void, RC_NOCACHE, ST),
671 PROC(getattr, fhandle, attrstat, fhandle, RC_NOCACHE, ST+AT),
672 PROC(setattr, sattr, wccstat, fhandle, RC_REPLBUFF, ST+WC),
673 PROC(lookup, dirop, dirop, fhandle2, RC_NOCACHE, ST+FH+pAT+pAT),
674 PROC(access, access, access, fhandle, RC_NOCACHE, ST+pAT+1),
675 PROC(readlink, fhandle, readlink, fhandle, RC_NOCACHE, ST+pAT+1+NFS3_MAXPATHLEN/4),
676 PROC(read, read, read, fhandle, RC_NOCACHE, ST+pAT+4+NFSSVC_MAXBLKSIZE),
677 PROC(write, write, write, fhandle, RC_REPLBUFF, ST+WC+4),
678 PROC(create, create, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
679 PROC(mkdir, mkdir, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
680 PROC(symlink, symlink, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
681 PROC(mknod, mknod, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
682 PROC(remove, dirop, wccstat, fhandle, RC_REPLBUFF, ST+WC),
683 PROC(rmdir, dirop, wccstat, fhandle, RC_REPLBUFF, ST+WC),
684 PROC(rename, rename, rename, fhandle2, RC_REPLBUFF, ST+WC+WC),
685 PROC(link, link, link, fhandle2, RC_REPLBUFF, ST+pAT+WC),
686 PROC(readdir, readdir, readdir, fhandle, RC_NOCACHE, 0),
687 PROC(readdirplus,readdirplus, readdir, fhandle, RC_NOCACHE, 0),
688 PROC(fsstat, fhandle, fsstat, void, RC_NOCACHE, ST+pAT+2*6+1),
689 PROC(fsinfo, fhandle, fsinfo, void, RC_NOCACHE, ST+pAT+12),
690 PROC(pathconf, fhandle, pathconf, void, RC_NOCACHE, ST+pAT+6),
691 PROC(commit, commit, commit, fhandle, RC_NOCACHE, ST+WC+2),