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)
61 dprintk("nfsd: GETATTR(3) %s\n",
62 SVCFH_fmt(&argp->fh));
64 fh_copy(&resp->fh, &argp->fh);
65 nfserr = fh_verify(rqstp, &resp->fh, 0, MAY_NOP);
66 RETURN_STATUS(nfserr);
70 * Set a file's attributes
73 nfsd3_proc_setattr(struct svc_rqst *rqstp, struct nfsd3_sattrargs *argp,
74 struct nfsd3_attrstat *resp)
78 dprintk("nfsd: SETATTR(3) %s\n",
79 SVCFH_fmt(&argp->fh));
81 fh_copy(&resp->fh, &argp->fh);
82 nfserr = nfsd_setattr(rqstp, &resp->fh, &argp->attrs,
83 argp->check_guard, argp->guardtime);
84 RETURN_STATUS(nfserr);
88 * Look up a path name component
91 nfsd3_proc_lookup(struct svc_rqst *rqstp, struct nfsd3_diropargs *argp,
92 struct nfsd3_diropres *resp)
96 dprintk("nfsd: LOOKUP(3) %s %.*s\n",
101 fh_copy(&resp->dirfh, &argp->fh);
102 fh_init(&resp->fh, NFS3_FHSIZE);
104 nfserr = nfsd_lookup(rqstp, &resp->dirfh,
108 RETURN_STATUS(nfserr);
115 nfsd3_proc_access(struct svc_rqst *rqstp, struct nfsd3_accessargs *argp,
116 struct nfsd3_accessres *resp)
120 dprintk("nfsd: ACCESS(3) %s 0x%x\n",
121 SVCFH_fmt(&argp->fh),
124 fh_copy(&resp->fh, &argp->fh);
125 resp->access = argp->access;
126 nfserr = nfsd_access(rqstp, &resp->fh, &resp->access, NULL);
127 RETURN_STATUS(nfserr);
134 nfsd3_proc_readlink(struct svc_rqst *rqstp, struct nfsd3_readlinkargs *argp,
135 struct nfsd3_readlinkres *resp)
139 dprintk("nfsd: READLINK(3) %s\n", SVCFH_fmt(&argp->fh));
141 /* Read the symlink. */
142 fh_copy(&resp->fh, &argp->fh);
143 resp->len = NFS3_MAXPATHLEN;
144 nfserr = nfsd_readlink(rqstp, &resp->fh, argp->buffer, &resp->len);
145 RETURN_STATUS(nfserr);
149 * Read a portion of a file.
152 nfsd3_proc_read(struct svc_rqst *rqstp, struct nfsd3_readargs *argp,
153 struct nfsd3_readres *resp)
157 dprintk("nfsd: READ(3) %s %lu bytes at %lu\n",
158 SVCFH_fmt(&argp->fh),
159 (unsigned long) argp->count,
160 (unsigned long) argp->offset);
162 /* Obtain buffer pointer for payload.
163 * 1 (status) + 22 (post_op_attr) + 1 (count) + 1 (eof)
164 * + 1 (xdr opaque byte count) = 26
167 resp->count = argp->count;
168 if (NFSSVC_MAXBLKSIZE < resp->count)
169 resp->count = NFSSVC_MAXBLKSIZE;
171 svc_reserve(rqstp, ((1 + NFS3_POST_OP_ATTR_WORDS + 3)<<2) + resp->count +4);
173 fh_copy(&resp->fh, &argp->fh);
174 nfserr = nfsd_read(rqstp, &resp->fh, NULL,
176 argp->vec, argp->vlen,
179 struct inode *inode = resp->fh.fh_dentry->d_inode;
181 resp->eof = (argp->offset + resp->count) >= inode->i_size;
184 RETURN_STATUS(nfserr);
188 * Write data to a file
191 nfsd3_proc_write(struct svc_rqst *rqstp, struct nfsd3_writeargs *argp,
192 struct nfsd3_writeres *resp)
196 dprintk("nfsd: WRITE(3) %s %d bytes at %ld%s\n",
197 SVCFH_fmt(&argp->fh),
199 (unsigned long) argp->offset,
200 argp->stable? " stable" : "");
202 fh_copy(&resp->fh, &argp->fh);
203 resp->committed = argp->stable;
204 nfserr = nfsd_write(rqstp, &resp->fh, NULL,
206 argp->vec, argp->vlen,
209 resp->count = argp->count;
210 RETURN_STATUS(nfserr);
214 * With NFSv3, CREATE processing is a lot easier than with NFSv2.
215 * At least in theory; we'll see how it fares in practice when the
216 * first reports about SunOS compatibility problems start to pour in...
219 nfsd3_proc_create(struct svc_rqst *rqstp, struct nfsd3_createargs *argp,
220 struct nfsd3_diropres *resp)
222 svc_fh *dirfhp, *newfhp = NULL;
226 dprintk("nfsd: CREATE(3) %s %.*s\n",
227 SVCFH_fmt(&argp->fh),
231 dirfhp = fh_copy(&resp->dirfh, &argp->fh);
232 newfhp = fh_init(&resp->fh, NFS3_FHSIZE);
235 /* Get the directory inode */
236 nfserr = fh_verify(rqstp, dirfhp, S_IFDIR, MAY_CREATE);
238 RETURN_STATUS(nfserr);
240 /* Unfudge the mode bits */
241 attr->ia_mode &= ~S_IFMT;
242 if (!(attr->ia_valid & ATTR_MODE)) {
243 attr->ia_valid |= ATTR_MODE;
244 attr->ia_mode = S_IFREG;
246 attr->ia_mode = (attr->ia_mode & ~S_IFMT) | S_IFREG;
249 /* Now create the file and set attributes */
250 nfserr = nfsd_create_v3(rqstp, dirfhp, argp->name, argp->len,
252 argp->createmode, argp->verf, NULL);
254 RETURN_STATUS(nfserr);
258 * Make directory. This operation is not idempotent.
261 nfsd3_proc_mkdir(struct svc_rqst *rqstp, struct nfsd3_createargs *argp,
262 struct nfsd3_diropres *resp)
266 dprintk("nfsd: MKDIR(3) %s %.*s\n",
267 SVCFH_fmt(&argp->fh),
271 argp->attrs.ia_valid &= ~ATTR_SIZE;
272 fh_copy(&resp->dirfh, &argp->fh);
273 fh_init(&resp->fh, NFS3_FHSIZE);
274 nfserr = nfsd_create(rqstp, &resp->dirfh, argp->name, argp->len,
275 &argp->attrs, S_IFDIR, 0, &resp->fh);
277 RETURN_STATUS(nfserr);
281 nfsd3_proc_symlink(struct svc_rqst *rqstp, struct nfsd3_symlinkargs *argp,
282 struct nfsd3_diropres *resp)
286 dprintk("nfsd: SYMLINK(3) %s %.*s -> %.*s\n",
287 SVCFH_fmt(&argp->ffh),
288 argp->flen, argp->fname,
289 argp->tlen, argp->tname);
291 fh_copy(&resp->dirfh, &argp->ffh);
292 fh_init(&resp->fh, NFS3_FHSIZE);
293 nfserr = nfsd_symlink(rqstp, &resp->dirfh, argp->fname, argp->flen,
294 argp->tname, argp->tlen,
295 &resp->fh, &argp->attrs);
296 RETURN_STATUS(nfserr);
300 * Make socket/fifo/device.
303 nfsd3_proc_mknod(struct svc_rqst *rqstp, struct nfsd3_mknodargs *argp,
304 struct nfsd3_diropres *resp)
309 dprintk("nfsd: MKNOD(3) %s %.*s\n",
310 SVCFH_fmt(&argp->fh),
314 fh_copy(&resp->dirfh, &argp->fh);
315 fh_init(&resp->fh, NFS3_FHSIZE);
317 if (argp->ftype == 0 || argp->ftype >= NF3BAD)
318 RETURN_STATUS(nfserr_inval);
319 if (argp->ftype == NF3CHR || argp->ftype == NF3BLK) {
320 rdev = MKDEV(argp->major, argp->minor);
321 if (MAJOR(rdev) != argp->major ||
322 MINOR(rdev) != argp->minor)
323 RETURN_STATUS(nfserr_inval);
325 if (argp->ftype != NF3SOCK && argp->ftype != NF3FIFO)
326 RETURN_STATUS(nfserr_inval);
328 type = nfs3_ftypes[argp->ftype];
329 nfserr = nfsd_create(rqstp, &resp->dirfh, argp->name, argp->len,
330 &argp->attrs, type, rdev, &resp->fh);
332 RETURN_STATUS(nfserr);
336 * Remove file/fifo/socket etc.
339 nfsd3_proc_remove(struct svc_rqst *rqstp, struct nfsd3_diropargs *argp,
340 struct nfsd3_attrstat *resp)
344 dprintk("nfsd: REMOVE(3) %s %.*s\n",
345 SVCFH_fmt(&argp->fh),
349 /* Unlink. -S_IFDIR means file must not be a directory */
350 fh_copy(&resp->fh, &argp->fh);
351 nfserr = nfsd_unlink(rqstp, &resp->fh, -S_IFDIR, argp->name, argp->len);
352 RETURN_STATUS(nfserr);
359 nfsd3_proc_rmdir(struct svc_rqst *rqstp, struct nfsd3_diropargs *argp,
360 struct nfsd3_attrstat *resp)
364 dprintk("nfsd: RMDIR(3) %s %.*s\n",
365 SVCFH_fmt(&argp->fh),
369 fh_copy(&resp->fh, &argp->fh);
370 nfserr = nfsd_unlink(rqstp, &resp->fh, S_IFDIR, argp->name, argp->len);
371 RETURN_STATUS(nfserr);
375 nfsd3_proc_rename(struct svc_rqst *rqstp, struct nfsd3_renameargs *argp,
376 struct nfsd3_renameres *resp)
380 dprintk("nfsd: RENAME(3) %s %.*s ->\n",
381 SVCFH_fmt(&argp->ffh),
384 dprintk("nfsd: -> %s %.*s\n",
385 SVCFH_fmt(&argp->tfh),
389 fh_copy(&resp->ffh, &argp->ffh);
390 fh_copy(&resp->tfh, &argp->tfh);
391 nfserr = nfsd_rename(rqstp, &resp->ffh, argp->fname, argp->flen,
392 &resp->tfh, argp->tname, argp->tlen);
393 RETURN_STATUS(nfserr);
397 nfsd3_proc_link(struct svc_rqst *rqstp, struct nfsd3_linkargs *argp,
398 struct nfsd3_linkres *resp)
402 dprintk("nfsd: LINK(3) %s ->\n",
403 SVCFH_fmt(&argp->ffh));
404 dprintk("nfsd: -> %s %.*s\n",
405 SVCFH_fmt(&argp->tfh),
409 fh_copy(&resp->fh, &argp->ffh);
410 fh_copy(&resp->tfh, &argp->tfh);
411 nfserr = nfsd_link(rqstp, &resp->tfh, argp->tname, argp->tlen,
413 RETURN_STATUS(nfserr);
417 * Read a portion of a directory.
420 nfsd3_proc_readdir(struct svc_rqst *rqstp, struct nfsd3_readdirargs *argp,
421 struct nfsd3_readdirres *resp)
425 dprintk("nfsd: READDIR(3) %s %d bytes at %d\n",
426 SVCFH_fmt(&argp->fh),
427 argp->count, (u32) argp->cookie);
429 /* Make sure we've room for the NULL ptr & eof flag, and shrink to
430 * client read size */
431 count = (argp->count >> 2) - 2;
433 /* Read directory and encode entries on the fly */
434 fh_copy(&resp->fh, &argp->fh);
436 resp->buflen = count;
437 resp->common.err = nfs_ok;
438 resp->buffer = argp->buffer;
440 nfserr = nfsd_readdir(rqstp, &resp->fh, (loff_t*) &argp->cookie,
441 &resp->common, nfs3svc_encode_entry);
442 memcpy(resp->verf, argp->verf, 8);
443 resp->count = resp->buffer - argp->buffer;
445 xdr_encode_hyper(resp->offset, argp->cookie);
447 RETURN_STATUS(nfserr);
451 * Read a portion of a directory, including file handles and attrs.
452 * For now, we choose to ignore the dircount parameter.
455 nfsd3_proc_readdirplus(struct svc_rqst *rqstp, struct nfsd3_readdirargs *argp,
456 struct nfsd3_readdirres *resp)
458 int nfserr, count = 0;
461 caddr_t page_addr = NULL;
463 dprintk("nfsd: READDIR+(3) %s %d bytes at %d\n",
464 SVCFH_fmt(&argp->fh),
465 argp->count, (u32) argp->cookie);
467 /* Convert byte count to number of words (i.e. >> 2),
468 * and reserve room for the NULL ptr & eof flag (-2 words) */
469 resp->count = (argp->count >> 2) - 2;
471 /* Read directory and encode entries on the fly */
472 fh_copy(&resp->fh, &argp->fh);
474 resp->common.err = nfs_ok;
475 resp->buffer = argp->buffer;
476 resp->buflen = resp->count;
478 offset = argp->cookie;
479 nfserr = nfsd_readdir(rqstp, &resp->fh,
482 nfs3svc_encode_entry_plus);
483 memcpy(resp->verf, argp->verf, 8);
484 for (i=1; i<rqstp->rq_resused ; i++) {
485 page_addr = page_address(rqstp->rq_respages[i]);
487 if (((caddr_t)resp->buffer >= page_addr) &&
488 ((caddr_t)resp->buffer < page_addr + PAGE_SIZE)) {
489 count += (caddr_t)resp->buffer - page_addr;
494 resp->count = count >> 2;
496 if (unlikely(resp->offset1)) {
497 /* we ended up with offset on a page boundary */
498 *resp->offset = htonl(offset >> 32);
499 *resp->offset1 = htonl(offset & 0xffffffff);
500 resp->offset1 = NULL;
502 xdr_encode_hyper(resp->offset, offset);
506 RETURN_STATUS(nfserr);
510 * Get file system stats
513 nfsd3_proc_fsstat(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
514 struct nfsd3_fsstatres *resp)
518 dprintk("nfsd: FSSTAT(3) %s\n",
519 SVCFH_fmt(&argp->fh));
521 nfserr = nfsd_statfs(rqstp, &argp->fh, &resp->stats);
523 RETURN_STATUS(nfserr);
527 * Get file system info
530 nfsd3_proc_fsinfo(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
531 struct nfsd3_fsinfores *resp)
535 dprintk("nfsd: FSINFO(3) %s\n",
536 SVCFH_fmt(&argp->fh));
538 resp->f_rtmax = NFSSVC_MAXBLKSIZE;
539 resp->f_rtpref = NFSSVC_MAXBLKSIZE;
540 resp->f_rtmult = PAGE_SIZE;
541 resp->f_wtmax = NFSSVC_MAXBLKSIZE;
542 resp->f_wtpref = NFSSVC_MAXBLKSIZE;
543 resp->f_wtmult = PAGE_SIZE;
544 resp->f_dtpref = PAGE_SIZE;
545 resp->f_maxfilesize = ~(u32) 0;
546 resp->f_properties = NFS3_FSF_DEFAULT;
548 nfserr = fh_verify(rqstp, &argp->fh, 0, MAY_NOP);
550 /* Check special features of the file system. May request
551 * different read/write sizes for file systems known to have
552 * problems with large blocks */
554 struct super_block *sb = argp->fh.fh_dentry->d_inode->i_sb;
556 /* Note that we don't care for remote fs's here */
557 if (sb->s_magic == 0x4d44 /* MSDOS_SUPER_MAGIC */) {
558 resp->f_properties = NFS3_FSF_BILLYBOY;
560 resp->f_maxfilesize = sb->s_maxbytes;
564 RETURN_STATUS(nfserr);
568 * Get pathconf info for the specified file
571 nfsd3_proc_pathconf(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
572 struct nfsd3_pathconfres *resp)
576 dprintk("nfsd: PATHCONF(3) %s\n",
577 SVCFH_fmt(&argp->fh));
579 /* Set default pathconf */
580 resp->p_link_max = 255; /* at least */
581 resp->p_name_max = 255; /* at least */
582 resp->p_no_trunc = 0;
583 resp->p_chown_restricted = 1;
584 resp->p_case_insensitive = 0;
585 resp->p_case_preserving = 1;
587 nfserr = fh_verify(rqstp, &argp->fh, 0, MAY_NOP);
590 struct super_block *sb = argp->fh.fh_dentry->d_inode->i_sb;
592 /* Note that we don't care for remote fs's here */
593 switch (sb->s_magic) {
594 case EXT2_SUPER_MAGIC:
595 resp->p_link_max = EXT2_LINK_MAX;
596 resp->p_name_max = EXT2_NAME_LEN;
598 case 0x4d44: /* MSDOS_SUPER_MAGIC */
599 resp->p_case_insensitive = 1;
600 resp->p_case_preserving = 0;
606 RETURN_STATUS(nfserr);
611 * Commit a file (range) to stable storage.
614 nfsd3_proc_commit(struct svc_rqst * rqstp, struct nfsd3_commitargs *argp,
615 struct nfsd3_commitres *resp)
619 dprintk("nfsd: COMMIT(3) %s %u@%Lu\n",
620 SVCFH_fmt(&argp->fh),
622 (unsigned long long) argp->offset);
624 if (argp->offset > NFS_OFFSET_MAX)
625 RETURN_STATUS(nfserr_inval);
627 fh_copy(&resp->fh, &argp->fh);
628 nfserr = nfsd_commit(rqstp, &resp->fh, argp->offset, argp->count);
630 RETURN_STATUS(nfserr);
635 * NFSv3 Server procedures.
636 * Only the results of non-idempotent operations are cached.
638 #define nfs3svc_decode_voidargs NULL
639 #define nfs3svc_release_void NULL
640 #define nfs3svc_decode_fhandleargs nfs3svc_decode_fhandle
641 #define nfs3svc_encode_attrstatres nfs3svc_encode_attrstat
642 #define nfs3svc_encode_wccstatres nfs3svc_encode_wccstat
643 #define nfsd3_mkdirargs nfsd3_createargs
644 #define nfsd3_readdirplusargs nfsd3_readdirargs
645 #define nfsd3_fhandleargs nfsd_fhandle
646 #define nfsd3_fhandleres nfsd3_attrstat
647 #define nfsd3_attrstatres nfsd3_attrstat
648 #define nfsd3_wccstatres nfsd3_attrstat
649 #define nfsd3_createres nfsd3_diropres
650 #define nfsd3_voidres nfsd3_voidargs
651 struct nfsd3_voidargs { int dummy; };
653 #define PROC(name, argt, rest, relt, cache, respsize) \
654 { (svc_procfunc) nfsd3_proc_##name, \
655 (kxdrproc_t) nfs3svc_decode_##argt##args, \
656 (kxdrproc_t) nfs3svc_encode_##rest##res, \
657 (kxdrproc_t) nfs3svc_release_##relt, \
658 sizeof(struct nfsd3_##argt##args), \
659 sizeof(struct nfsd3_##rest##res), \
665 #define ST 1 /* status*/
666 #define FH 17 /* filehandle with length */
667 #define AT 21 /* attributes */
668 #define pAT (1+AT) /* post attributes - conditional */
669 #define WC (7+pAT) /* WCC attributes */
671 static struct svc_procedure nfsd_procedures3[22] = {
672 PROC(null, void, void, void, RC_NOCACHE, ST),
673 PROC(getattr, fhandle, attrstat, fhandle, RC_NOCACHE, ST+AT),
674 PROC(setattr, sattr, wccstat, fhandle, RC_REPLBUFF, ST+WC),
675 PROC(lookup, dirop, dirop, fhandle2, RC_NOCACHE, ST+FH+pAT+pAT),
676 PROC(access, access, access, fhandle, RC_NOCACHE, ST+pAT+1),
677 PROC(readlink, readlink, readlink, fhandle, RC_NOCACHE, ST+pAT+1+NFS3_MAXPATHLEN/4),
678 PROC(read, read, read, fhandle, RC_NOCACHE, ST+pAT+4+NFSSVC_MAXBLKSIZE),
679 PROC(write, write, write, fhandle, RC_REPLBUFF, ST+WC+4),
680 PROC(create, create, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
681 PROC(mkdir, mkdir, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
682 PROC(symlink, symlink, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
683 PROC(mknod, mknod, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
684 PROC(remove, dirop, wccstat, fhandle, RC_REPLBUFF, ST+WC),
685 PROC(rmdir, dirop, wccstat, fhandle, RC_REPLBUFF, ST+WC),
686 PROC(rename, rename, rename, fhandle2, RC_REPLBUFF, ST+WC+WC),
687 PROC(link, link, link, fhandle2, RC_REPLBUFF, ST+pAT+WC),
688 PROC(readdir, readdir, readdir, fhandle, RC_NOCACHE, 0),
689 PROC(readdirplus,readdirplus, readdir, fhandle, RC_NOCACHE, 0),
690 PROC(fsstat, fhandle, fsstat, void, RC_NOCACHE, ST+pAT+2*6+1),
691 PROC(fsinfo, fhandle, fsinfo, void, RC_NOCACHE, ST+pAT+12),
692 PROC(pathconf, fhandle, pathconf, void, RC_NOCACHE, ST+pAT+6),
693 PROC(commit, commit, commit, fhandle, RC_NOCACHE, ST+WC+2),
696 struct svc_version nfsd_version3 = {
699 .vs_proc = nfsd_procedures3,
700 .vs_dispatch = nfsd_dispatch,
701 .vs_xdrsize = NFS3_SVC_XDRSIZE,