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/time.h>
11 #include <linux/errno.h>
13 #include <linux/ext2_fs.h>
14 #include <linux/stat.h>
15 #include <linux/fcntl.h>
16 #include <linux/net.h>
18 #include <linux/unistd.h>
19 #include <linux/slab.h>
20 #include <linux/major.h>
22 #include <linux/sunrpc/svc.h>
23 #include <linux/nfsd/nfsd.h>
24 #include <linux/nfsd/cache.h>
25 #include <linux/nfsd/xdr3.h>
26 #include <linux/nfs3.h>
28 #define NFSDDBG_FACILITY NFSDDBG_PROC
30 #define RETURN_STATUS(st) { resp->status = (st); return (st); }
32 static int nfs3_ftypes[] = {
39 S_IFSOCK, /* NF3SOCK */
40 S_IFIFO, /* NF3FIFO */
47 nfsd3_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
53 * Get a file's attributes
56 nfsd3_proc_getattr(struct svc_rqst *rqstp, struct nfsd_fhandle *argp,
57 struct nfsd3_attrstat *resp)
62 dprintk("nfsd: GETATTR(3) %s\n",
63 SVCFH_fmt(&argp->fh));
65 fh_copy(&resp->fh, &argp->fh);
66 nfserr = fh_verify(rqstp, &resp->fh, 0, MAY_NOP);
68 RETURN_STATUS(nfserr);
70 err = vfs_getattr(resp->fh.fh_export->ex_mnt,
71 resp->fh.fh_dentry, &resp->stat);
72 nfserr = nfserrno(err);
74 RETURN_STATUS(nfserr);
78 * Set a file's attributes
81 nfsd3_proc_setattr(struct svc_rqst *rqstp, struct nfsd3_sattrargs *argp,
82 struct nfsd3_attrstat *resp)
86 dprintk("nfsd: SETATTR(3) %s\n",
87 SVCFH_fmt(&argp->fh));
89 fh_copy(&resp->fh, &argp->fh);
90 nfserr = nfsd_setattr(rqstp, &resp->fh, &argp->attrs,
91 argp->check_guard, argp->guardtime);
92 RETURN_STATUS(nfserr);
96 * Look up a path name component
99 nfsd3_proc_lookup(struct svc_rqst *rqstp, struct nfsd3_diropargs *argp,
100 struct nfsd3_diropres *resp)
104 dprintk("nfsd: LOOKUP(3) %s %.*s\n",
105 SVCFH_fmt(&argp->fh),
109 fh_copy(&resp->dirfh, &argp->fh);
110 fh_init(&resp->fh, NFS3_FHSIZE);
112 nfserr = nfsd_lookup(rqstp, &resp->dirfh,
116 RETURN_STATUS(nfserr);
123 nfsd3_proc_access(struct svc_rqst *rqstp, struct nfsd3_accessargs *argp,
124 struct nfsd3_accessres *resp)
128 dprintk("nfsd: ACCESS(3) %s 0x%x\n",
129 SVCFH_fmt(&argp->fh),
132 fh_copy(&resp->fh, &argp->fh);
133 resp->access = argp->access;
134 nfserr = nfsd_access(rqstp, &resp->fh, &resp->access, NULL);
135 RETURN_STATUS(nfserr);
142 nfsd3_proc_readlink(struct svc_rqst *rqstp, struct nfsd3_readlinkargs *argp,
143 struct nfsd3_readlinkres *resp)
147 dprintk("nfsd: READLINK(3) %s\n", SVCFH_fmt(&argp->fh));
149 /* Read the symlink. */
150 fh_copy(&resp->fh, &argp->fh);
151 resp->len = NFS3_MAXPATHLEN;
152 nfserr = nfsd_readlink(rqstp, &resp->fh, argp->buffer, &resp->len);
153 RETURN_STATUS(nfserr);
157 * Read a portion of a file.
160 nfsd3_proc_read(struct svc_rqst *rqstp, struct nfsd3_readargs *argp,
161 struct nfsd3_readres *resp)
164 u32 max_blocksize = svc_max_payload(rqstp);
166 dprintk("nfsd: READ(3) %s %lu bytes at %lu\n",
167 SVCFH_fmt(&argp->fh),
168 (unsigned long) argp->count,
169 (unsigned long) argp->offset);
171 /* Obtain buffer pointer for payload.
172 * 1 (status) + 22 (post_op_attr) + 1 (count) + 1 (eof)
173 * + 1 (xdr opaque byte count) = 26
176 resp->count = argp->count;
177 if (max_blocksize < resp->count)
178 resp->count = max_blocksize;
180 svc_reserve(rqstp, ((1 + NFS3_POST_OP_ATTR_WORDS + 3)<<2) + resp->count +4);
182 fh_copy(&resp->fh, &argp->fh);
183 nfserr = nfsd_read(rqstp, &resp->fh, NULL,
185 rqstp->rq_vec, argp->vlen,
188 struct inode *inode = resp->fh.fh_dentry->d_inode;
190 resp->eof = (argp->offset + resp->count) >= inode->i_size;
193 RETURN_STATUS(nfserr);
197 * Write data to a file
200 nfsd3_proc_write(struct svc_rqst *rqstp, struct nfsd3_writeargs *argp,
201 struct nfsd3_writeres *resp)
205 dprintk("nfsd: WRITE(3) %s %d bytes at %ld%s\n",
206 SVCFH_fmt(&argp->fh),
208 (unsigned long) argp->offset,
209 argp->stable? " stable" : "");
211 fh_copy(&resp->fh, &argp->fh);
212 resp->committed = argp->stable;
213 nfserr = nfsd_write(rqstp, &resp->fh, NULL,
215 rqstp->rq_vec, argp->vlen,
218 resp->count = argp->count;
219 RETURN_STATUS(nfserr);
223 * With NFSv3, CREATE processing is a lot easier than with NFSv2.
224 * At least in theory; we'll see how it fares in practice when the
225 * first reports about SunOS compatibility problems start to pour in...
228 nfsd3_proc_create(struct svc_rqst *rqstp, struct nfsd3_createargs *argp,
229 struct nfsd3_diropres *resp)
231 svc_fh *dirfhp, *newfhp = NULL;
235 dprintk("nfsd: CREATE(3) %s %.*s\n",
236 SVCFH_fmt(&argp->fh),
240 dirfhp = fh_copy(&resp->dirfh, &argp->fh);
241 newfhp = fh_init(&resp->fh, NFS3_FHSIZE);
244 /* Get the directory inode */
245 nfserr = fh_verify(rqstp, dirfhp, S_IFDIR, MAY_CREATE);
247 RETURN_STATUS(nfserr);
249 /* Unfudge the mode bits */
250 attr->ia_mode &= ~S_IFMT;
251 if (!(attr->ia_valid & ATTR_MODE)) {
252 attr->ia_valid |= ATTR_MODE;
253 attr->ia_mode = S_IFREG;
255 attr->ia_mode = (attr->ia_mode & ~S_IFMT) | S_IFREG;
258 /* Now create the file and set attributes */
259 nfserr = nfsd_create_v3(rqstp, dirfhp, argp->name, argp->len,
261 argp->createmode, argp->verf, NULL, NULL);
263 RETURN_STATUS(nfserr);
267 * Make directory. This operation is not idempotent.
270 nfsd3_proc_mkdir(struct svc_rqst *rqstp, struct nfsd3_createargs *argp,
271 struct nfsd3_diropres *resp)
275 dprintk("nfsd: MKDIR(3) %s %.*s\n",
276 SVCFH_fmt(&argp->fh),
280 argp->attrs.ia_valid &= ~ATTR_SIZE;
281 fh_copy(&resp->dirfh, &argp->fh);
282 fh_init(&resp->fh, NFS3_FHSIZE);
283 nfserr = nfsd_create(rqstp, &resp->dirfh, argp->name, argp->len,
284 &argp->attrs, S_IFDIR, 0, &resp->fh);
286 RETURN_STATUS(nfserr);
290 nfsd3_proc_symlink(struct svc_rqst *rqstp, struct nfsd3_symlinkargs *argp,
291 struct nfsd3_diropres *resp)
295 dprintk("nfsd: SYMLINK(3) %s %.*s -> %.*s\n",
296 SVCFH_fmt(&argp->ffh),
297 argp->flen, argp->fname,
298 argp->tlen, argp->tname);
300 fh_copy(&resp->dirfh, &argp->ffh);
301 fh_init(&resp->fh, NFS3_FHSIZE);
302 nfserr = nfsd_symlink(rqstp, &resp->dirfh, argp->fname, argp->flen,
303 argp->tname, argp->tlen,
304 &resp->fh, &argp->attrs);
305 RETURN_STATUS(nfserr);
309 * Make socket/fifo/device.
312 nfsd3_proc_mknod(struct svc_rqst *rqstp, struct nfsd3_mknodargs *argp,
313 struct nfsd3_diropres *resp)
319 dprintk("nfsd: MKNOD(3) %s %.*s\n",
320 SVCFH_fmt(&argp->fh),
324 fh_copy(&resp->dirfh, &argp->fh);
325 fh_init(&resp->fh, NFS3_FHSIZE);
327 if (argp->ftype == 0 || argp->ftype >= NF3BAD)
328 RETURN_STATUS(nfserr_inval);
329 if (argp->ftype == NF3CHR || argp->ftype == NF3BLK) {
330 rdev = MKDEV(argp->major, argp->minor);
331 if (MAJOR(rdev) != argp->major ||
332 MINOR(rdev) != argp->minor)
333 RETURN_STATUS(nfserr_inval);
335 if (argp->ftype != NF3SOCK && argp->ftype != NF3FIFO)
336 RETURN_STATUS(nfserr_inval);
338 type = nfs3_ftypes[argp->ftype];
339 nfserr = nfsd_create(rqstp, &resp->dirfh, argp->name, argp->len,
340 &argp->attrs, type, rdev, &resp->fh);
342 RETURN_STATUS(nfserr);
346 * Remove file/fifo/socket etc.
349 nfsd3_proc_remove(struct svc_rqst *rqstp, struct nfsd3_diropargs *argp,
350 struct nfsd3_attrstat *resp)
354 dprintk("nfsd: REMOVE(3) %s %.*s\n",
355 SVCFH_fmt(&argp->fh),
359 /* Unlink. -S_IFDIR means file must not be a directory */
360 fh_copy(&resp->fh, &argp->fh);
361 nfserr = nfsd_unlink(rqstp, &resp->fh, -S_IFDIR, argp->name, argp->len);
362 RETURN_STATUS(nfserr);
369 nfsd3_proc_rmdir(struct svc_rqst *rqstp, struct nfsd3_diropargs *argp,
370 struct nfsd3_attrstat *resp)
374 dprintk("nfsd: RMDIR(3) %s %.*s\n",
375 SVCFH_fmt(&argp->fh),
379 fh_copy(&resp->fh, &argp->fh);
380 nfserr = nfsd_unlink(rqstp, &resp->fh, S_IFDIR, argp->name, argp->len);
381 RETURN_STATUS(nfserr);
385 nfsd3_proc_rename(struct svc_rqst *rqstp, struct nfsd3_renameargs *argp,
386 struct nfsd3_renameres *resp)
390 dprintk("nfsd: RENAME(3) %s %.*s ->\n",
391 SVCFH_fmt(&argp->ffh),
394 dprintk("nfsd: -> %s %.*s\n",
395 SVCFH_fmt(&argp->tfh),
399 fh_copy(&resp->ffh, &argp->ffh);
400 fh_copy(&resp->tfh, &argp->tfh);
401 nfserr = nfsd_rename(rqstp, &resp->ffh, argp->fname, argp->flen,
402 &resp->tfh, argp->tname, argp->tlen);
403 RETURN_STATUS(nfserr);
407 nfsd3_proc_link(struct svc_rqst *rqstp, struct nfsd3_linkargs *argp,
408 struct nfsd3_linkres *resp)
412 dprintk("nfsd: LINK(3) %s ->\n",
413 SVCFH_fmt(&argp->ffh));
414 dprintk("nfsd: -> %s %.*s\n",
415 SVCFH_fmt(&argp->tfh),
419 fh_copy(&resp->fh, &argp->ffh);
420 fh_copy(&resp->tfh, &argp->tfh);
421 nfserr = nfsd_link(rqstp, &resp->tfh, argp->tname, argp->tlen,
423 RETURN_STATUS(nfserr);
427 * Read a portion of a directory.
430 nfsd3_proc_readdir(struct svc_rqst *rqstp, struct nfsd3_readdirargs *argp,
431 struct nfsd3_readdirres *resp)
436 dprintk("nfsd: READDIR(3) %s %d bytes at %d\n",
437 SVCFH_fmt(&argp->fh),
438 argp->count, (u32) argp->cookie);
440 /* Make sure we've room for the NULL ptr & eof flag, and shrink to
441 * client read size */
442 count = (argp->count >> 2) - 2;
444 /* Read directory and encode entries on the fly */
445 fh_copy(&resp->fh, &argp->fh);
447 resp->buflen = count;
448 resp->common.err = nfs_ok;
449 resp->buffer = argp->buffer;
451 nfserr = nfsd_readdir(rqstp, &resp->fh, (loff_t*) &argp->cookie,
452 &resp->common, nfs3svc_encode_entry);
453 memcpy(resp->verf, argp->verf, 8);
454 resp->count = resp->buffer - argp->buffer;
456 xdr_encode_hyper(resp->offset, argp->cookie);
458 RETURN_STATUS(nfserr);
462 * Read a portion of a directory, including file handles and attrs.
463 * For now, we choose to ignore the dircount parameter.
466 nfsd3_proc_readdirplus(struct svc_rqst *rqstp, struct nfsd3_readdirargs *argp,
467 struct nfsd3_readdirres *resp)
473 caddr_t page_addr = NULL;
475 dprintk("nfsd: READDIR+(3) %s %d bytes at %d\n",
476 SVCFH_fmt(&argp->fh),
477 argp->count, (u32) argp->cookie);
479 /* Convert byte count to number of words (i.e. >> 2),
480 * and reserve room for the NULL ptr & eof flag (-2 words) */
481 resp->count = (argp->count >> 2) - 2;
483 /* Read directory and encode entries on the fly */
484 fh_copy(&resp->fh, &argp->fh);
486 resp->common.err = nfs_ok;
487 resp->buffer = argp->buffer;
488 resp->buflen = resp->count;
490 offset = argp->cookie;
491 nfserr = nfsd_readdir(rqstp, &resp->fh,
494 nfs3svc_encode_entry_plus);
495 memcpy(resp->verf, argp->verf, 8);
496 for (i=1; i<rqstp->rq_resused ; i++) {
497 page_addr = page_address(rqstp->rq_respages[i]);
499 if (((caddr_t)resp->buffer >= page_addr) &&
500 ((caddr_t)resp->buffer < page_addr + PAGE_SIZE)) {
501 count += (caddr_t)resp->buffer - page_addr;
506 resp->count = count >> 2;
508 if (unlikely(resp->offset1)) {
509 /* we ended up with offset on a page boundary */
510 *resp->offset = htonl(offset >> 32);
511 *resp->offset1 = htonl(offset & 0xffffffff);
512 resp->offset1 = NULL;
514 xdr_encode_hyper(resp->offset, offset);
518 RETURN_STATUS(nfserr);
522 * Get file system stats
525 nfsd3_proc_fsstat(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
526 struct nfsd3_fsstatres *resp)
530 dprintk("nfsd: FSSTAT(3) %s\n",
531 SVCFH_fmt(&argp->fh));
533 nfserr = nfsd_statfs(rqstp, &argp->fh, &resp->stats);
535 RETURN_STATUS(nfserr);
539 * Get file system info
542 nfsd3_proc_fsinfo(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
543 struct nfsd3_fsinfores *resp)
546 u32 max_blocksize = svc_max_payload(rqstp);
548 dprintk("nfsd: FSINFO(3) %s\n",
549 SVCFH_fmt(&argp->fh));
551 resp->f_rtmax = max_blocksize;
552 resp->f_rtpref = max_blocksize;
553 resp->f_rtmult = PAGE_SIZE;
554 resp->f_wtmax = max_blocksize;
555 resp->f_wtpref = max_blocksize;
556 resp->f_wtmult = PAGE_SIZE;
557 resp->f_dtpref = PAGE_SIZE;
558 resp->f_maxfilesize = ~(u32) 0;
559 resp->f_properties = NFS3_FSF_DEFAULT;
561 nfserr = fh_verify(rqstp, &argp->fh, 0, MAY_NOP);
563 /* Check special features of the file system. May request
564 * different read/write sizes for file systems known to have
565 * problems with large blocks */
567 struct super_block *sb = argp->fh.fh_dentry->d_inode->i_sb;
569 /* Note that we don't care for remote fs's here */
570 if (sb->s_magic == 0x4d44 /* MSDOS_SUPER_MAGIC */) {
571 resp->f_properties = NFS3_FSF_BILLYBOY;
573 resp->f_maxfilesize = sb->s_maxbytes;
577 RETURN_STATUS(nfserr);
581 * Get pathconf info for the specified file
584 nfsd3_proc_pathconf(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
585 struct nfsd3_pathconfres *resp)
589 dprintk("nfsd: PATHCONF(3) %s\n",
590 SVCFH_fmt(&argp->fh));
592 /* Set default pathconf */
593 resp->p_link_max = 255; /* at least */
594 resp->p_name_max = 255; /* at least */
595 resp->p_no_trunc = 0;
596 resp->p_chown_restricted = 1;
597 resp->p_case_insensitive = 0;
598 resp->p_case_preserving = 1;
600 nfserr = fh_verify(rqstp, &argp->fh, 0, MAY_NOP);
603 struct super_block *sb = argp->fh.fh_dentry->d_inode->i_sb;
605 /* Note that we don't care for remote fs's here */
606 switch (sb->s_magic) {
607 case EXT2_SUPER_MAGIC:
608 resp->p_link_max = EXT2_LINK_MAX;
609 resp->p_name_max = EXT2_NAME_LEN;
611 case 0x4d44: /* MSDOS_SUPER_MAGIC */
612 resp->p_case_insensitive = 1;
613 resp->p_case_preserving = 0;
619 RETURN_STATUS(nfserr);
624 * Commit a file (range) to stable storage.
627 nfsd3_proc_commit(struct svc_rqst * rqstp, struct nfsd3_commitargs *argp,
628 struct nfsd3_commitres *resp)
632 dprintk("nfsd: COMMIT(3) %s %u@%Lu\n",
633 SVCFH_fmt(&argp->fh),
635 (unsigned long long) argp->offset);
637 if (argp->offset > NFS_OFFSET_MAX)
638 RETURN_STATUS(nfserr_inval);
640 fh_copy(&resp->fh, &argp->fh);
641 nfserr = nfsd_commit(rqstp, &resp->fh, argp->offset, argp->count);
643 RETURN_STATUS(nfserr);
648 * NFSv3 Server procedures.
649 * Only the results of non-idempotent operations are cached.
651 #define nfs3svc_decode_voidargs NULL
652 #define nfs3svc_release_void NULL
653 #define nfs3svc_decode_fhandleargs nfs3svc_decode_fhandle
654 #define nfs3svc_encode_attrstatres nfs3svc_encode_attrstat
655 #define nfs3svc_encode_wccstatres nfs3svc_encode_wccstat
656 #define nfsd3_mkdirargs nfsd3_createargs
657 #define nfsd3_readdirplusargs nfsd3_readdirargs
658 #define nfsd3_fhandleargs nfsd_fhandle
659 #define nfsd3_fhandleres nfsd3_attrstat
660 #define nfsd3_attrstatres nfsd3_attrstat
661 #define nfsd3_wccstatres nfsd3_attrstat
662 #define nfsd3_createres nfsd3_diropres
663 #define nfsd3_voidres nfsd3_voidargs
664 struct nfsd3_voidargs { int dummy; };
666 #define PROC(name, argt, rest, relt, cache, respsize) \
667 { (svc_procfunc) nfsd3_proc_##name, \
668 (kxdrproc_t) nfs3svc_decode_##argt##args, \
669 (kxdrproc_t) nfs3svc_encode_##rest##res, \
670 (kxdrproc_t) nfs3svc_release_##relt, \
671 sizeof(struct nfsd3_##argt##args), \
672 sizeof(struct nfsd3_##rest##res), \
678 #define ST 1 /* status*/
679 #define FH 17 /* filehandle with length */
680 #define AT 21 /* attributes */
681 #define pAT (1+AT) /* post attributes - conditional */
682 #define WC (7+pAT) /* WCC attributes */
684 static struct svc_procedure nfsd_procedures3[22] = {
685 PROC(null, void, void, void, RC_NOCACHE, ST),
686 PROC(getattr, fhandle, attrstat, fhandle, RC_NOCACHE, ST+AT),
687 PROC(setattr, sattr, wccstat, fhandle, RC_REPLBUFF, ST+WC),
688 PROC(lookup, dirop, dirop, fhandle2, RC_NOCACHE, ST+FH+pAT+pAT),
689 PROC(access, access, access, fhandle, RC_NOCACHE, ST+pAT+1),
690 PROC(readlink, readlink, readlink, fhandle, RC_NOCACHE, ST+pAT+1+NFS3_MAXPATHLEN/4),
691 PROC(read, read, read, fhandle, RC_NOCACHE, ST+pAT+4+NFSSVC_MAXBLKSIZE/4),
692 PROC(write, write, write, fhandle, RC_REPLBUFF, ST+WC+4),
693 PROC(create, create, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
694 PROC(mkdir, mkdir, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
695 PROC(symlink, symlink, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
696 PROC(mknod, mknod, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
697 PROC(remove, dirop, wccstat, fhandle, RC_REPLBUFF, ST+WC),
698 PROC(rmdir, dirop, wccstat, fhandle, RC_REPLBUFF, ST+WC),
699 PROC(rename, rename, rename, fhandle2, RC_REPLBUFF, ST+WC+WC),
700 PROC(link, link, link, fhandle2, RC_REPLBUFF, ST+pAT+WC),
701 PROC(readdir, readdir, readdir, fhandle, RC_NOCACHE, 0),
702 PROC(readdirplus,readdirplus, readdir, fhandle, RC_NOCACHE, 0),
703 PROC(fsstat, fhandle, fsstat, void, RC_NOCACHE, ST+pAT+2*6+1),
704 PROC(fsinfo, fhandle, fsinfo, void, RC_NOCACHE, ST+pAT+12),
705 PROC(pathconf, fhandle, pathconf, void, RC_NOCACHE, ST+pAT+6),
706 PROC(commit, commit, commit, fhandle, RC_NOCACHE, ST+WC+2),
709 struct svc_version nfsd_version3 = {
712 .vs_proc = nfsd_procedures3,
713 .vs_dispatch = nfsd_dispatch,
714 .vs_xdrsize = NFS3_SVC_XDRSIZE,