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,
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,
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;
441 nfserr = nfsd_readdir(rqstp, &resp->fh, (loff_t*) &argp->cookie,
442 &resp->common, nfs3svc_encode_entry);
443 memcpy(resp->verf, argp->verf, 8);
444 resp->count = resp->buffer - argp->buffer;
446 xdr_encode_hyper(resp->offset, argp->cookie);
448 RETURN_STATUS(nfserr);
452 * Read a portion of a directory, including file handles and attrs.
453 * For now, we choose to ignore the dircount parameter.
456 nfsd3_proc_readdirplus(struct svc_rqst *rqstp, struct nfsd3_readdirargs *argp,
457 struct nfsd3_readdirres *resp)
459 int nfserr, count = 0;
462 caddr_t page_addr = NULL;
464 dprintk("nfsd: READDIR+(3) %s %d bytes at %d\n",
465 SVCFH_fmt(&argp->fh),
466 argp->count, (u32) argp->cookie);
468 /* Convert byte count to number of words (i.e. >> 2),
469 * and reserve room for the NULL ptr & eof flag (-2 words) */
470 resp->count = (argp->count >> 2) - 2;
472 /* Read directory and encode entries on the fly */
473 fh_copy(&resp->fh, &argp->fh);
475 resp->common.err = nfs_ok;
476 resp->buffer = argp->buffer;
477 resp->buflen = resp->count;
479 offset = argp->cookie;
480 nfserr = nfsd_readdir(rqstp, &resp->fh,
483 nfs3svc_encode_entry_plus);
484 memcpy(resp->verf, argp->verf, 8);
485 for (i=1; i<rqstp->rq_resused ; i++) {
486 page_addr = page_address(rqstp->rq_respages[i]);
488 if (((caddr_t)resp->buffer >= page_addr) &&
489 ((caddr_t)resp->buffer < page_addr + PAGE_SIZE)) {
490 count += (caddr_t)resp->buffer - page_addr;
495 resp->count = count >> 2;
497 xdr_encode_hyper(resp->offset, offset);
499 RETURN_STATUS(nfserr);
503 * Get file system stats
506 nfsd3_proc_fsstat(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
507 struct nfsd3_fsstatres *resp)
511 dprintk("nfsd: FSSTAT(3) %s\n",
512 SVCFH_fmt(&argp->fh));
514 nfserr = nfsd_statfs(rqstp, &argp->fh, &resp->stats);
516 RETURN_STATUS(nfserr);
520 * Get file system info
523 nfsd3_proc_fsinfo(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
524 struct nfsd3_fsinfores *resp)
528 dprintk("nfsd: FSINFO(3) %s\n",
529 SVCFH_fmt(&argp->fh));
531 resp->f_rtmax = NFSSVC_MAXBLKSIZE;
532 resp->f_rtpref = NFSSVC_MAXBLKSIZE;
533 resp->f_rtmult = PAGE_SIZE;
534 resp->f_wtmax = NFSSVC_MAXBLKSIZE;
535 resp->f_wtpref = NFSSVC_MAXBLKSIZE;
536 resp->f_wtmult = PAGE_SIZE;
537 resp->f_dtpref = PAGE_SIZE;
538 resp->f_maxfilesize = ~(u32) 0;
539 resp->f_properties = NFS3_FSF_DEFAULT;
541 nfserr = fh_verify(rqstp, &argp->fh, 0, MAY_NOP);
543 /* Check special features of the file system. May request
544 * different read/write sizes for file systems known to have
545 * problems with large blocks */
547 struct super_block *sb = argp->fh.fh_dentry->d_inode->i_sb;
549 /* Note that we don't care for remote fs's here */
550 if (sb->s_magic == 0x4d44 /* MSDOS_SUPER_MAGIC */) {
551 resp->f_properties = NFS3_FSF_BILLYBOY;
553 resp->f_maxfilesize = sb->s_maxbytes;
557 RETURN_STATUS(nfserr);
561 * Get pathconf info for the specified file
564 nfsd3_proc_pathconf(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
565 struct nfsd3_pathconfres *resp)
569 dprintk("nfsd: PATHCONF(3) %s\n",
570 SVCFH_fmt(&argp->fh));
572 /* Set default pathconf */
573 resp->p_link_max = 255; /* at least */
574 resp->p_name_max = 255; /* at least */
575 resp->p_no_trunc = 0;
576 resp->p_chown_restricted = 1;
577 resp->p_case_insensitive = 0;
578 resp->p_case_preserving = 1;
580 nfserr = fh_verify(rqstp, &argp->fh, 0, MAY_NOP);
583 struct super_block *sb = argp->fh.fh_dentry->d_inode->i_sb;
585 /* Note that we don't care for remote fs's here */
586 switch (sb->s_magic) {
587 case EXT2_SUPER_MAGIC:
588 resp->p_link_max = EXT2_LINK_MAX;
589 resp->p_name_max = EXT2_NAME_LEN;
591 case 0x4d44: /* MSDOS_SUPER_MAGIC */
592 resp->p_case_insensitive = 1;
593 resp->p_case_preserving = 0;
599 RETURN_STATUS(nfserr);
604 * Commit a file (range) to stable storage.
607 nfsd3_proc_commit(struct svc_rqst * rqstp, struct nfsd3_commitargs *argp,
608 struct nfsd3_commitres *resp)
612 dprintk("nfsd: COMMIT(3) %s %u@%Lu\n",
613 SVCFH_fmt(&argp->fh),
615 (unsigned long long) argp->offset);
617 if (argp->offset > NFS_OFFSET_MAX)
618 RETURN_STATUS(nfserr_inval);
620 fh_copy(&resp->fh, &argp->fh);
621 nfserr = nfsd_commit(rqstp, &resp->fh, argp->offset, argp->count);
623 RETURN_STATUS(nfserr);
628 * NFSv3 Server procedures.
629 * Only the results of non-idempotent operations are cached.
631 #define nfs3svc_decode_voidargs NULL
632 #define nfs3svc_release_void NULL
633 #define nfs3svc_decode_fhandleargs nfs3svc_decode_fhandle
634 #define nfs3svc_encode_attrstatres nfs3svc_encode_attrstat
635 #define nfs3svc_encode_wccstatres nfs3svc_encode_wccstat
636 #define nfsd3_mkdirargs nfsd3_createargs
637 #define nfsd3_readdirplusargs nfsd3_readdirargs
638 #define nfsd3_fhandleargs nfsd_fhandle
639 #define nfsd3_fhandleres nfsd3_attrstat
640 #define nfsd3_attrstatres nfsd3_attrstat
641 #define nfsd3_wccstatres nfsd3_attrstat
642 #define nfsd3_createres nfsd3_diropres
643 #define nfsd3_voidres nfsd3_voidargs
644 struct nfsd3_voidargs { int dummy; };
646 #define PROC(name, argt, rest, relt, cache, respsize) \
647 { (svc_procfunc) nfsd3_proc_##name, \
648 (kxdrproc_t) nfs3svc_decode_##argt##args, \
649 (kxdrproc_t) nfs3svc_encode_##rest##res, \
650 (kxdrproc_t) nfs3svc_release_##relt, \
651 sizeof(struct nfsd3_##argt##args), \
652 sizeof(struct nfsd3_##rest##res), \
658 #define ST 1 /* status*/
659 #define FH 17 /* filehandle with length */
660 #define AT 21 /* attributes */
661 #define pAT (1+AT) /* post attributes - conditional */
662 #define WC (7+pAT) /* WCC attributes */
664 static struct svc_procedure nfsd_procedures3[22] = {
665 PROC(null, void, void, void, RC_NOCACHE, ST),
666 PROC(getattr, fhandle, attrstat, fhandle, RC_NOCACHE, ST+AT),
667 PROC(setattr, sattr, wccstat, fhandle, RC_REPLBUFF, ST+WC),
668 PROC(lookup, dirop, dirop, fhandle2, RC_NOCACHE, ST+FH+pAT+pAT),
669 PROC(access, access, access, fhandle, RC_NOCACHE, ST+pAT+1),
670 PROC(readlink, readlink, readlink, fhandle, RC_NOCACHE, ST+pAT+1+NFS3_MAXPATHLEN/4),
671 PROC(read, read, read, fhandle, RC_NOCACHE, ST+pAT+4+NFSSVC_MAXBLKSIZE),
672 PROC(write, write, write, fhandle, RC_REPLBUFF, ST+WC+4),
673 PROC(create, create, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
674 PROC(mkdir, mkdir, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
675 PROC(symlink, symlink, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
676 PROC(mknod, mknod, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
677 PROC(remove, dirop, wccstat, fhandle, RC_REPLBUFF, ST+WC),
678 PROC(rmdir, dirop, wccstat, fhandle, RC_REPLBUFF, ST+WC),
679 PROC(rename, rename, rename, fhandle2, RC_REPLBUFF, ST+WC+WC),
680 PROC(link, link, link, fhandle2, RC_REPLBUFF, ST+pAT+WC),
681 PROC(readdir, readdir, readdir, fhandle, RC_NOCACHE, 0),
682 PROC(readdirplus,readdirplus, readdir, fhandle, RC_NOCACHE, 0),
683 PROC(fsstat, fhandle, fsstat, void, RC_NOCACHE, ST+pAT+2*6+1),
684 PROC(fsinfo, fhandle, fsinfo, void, RC_NOCACHE, ST+pAT+12),
685 PROC(pathconf, fhandle, pathconf, void, RC_NOCACHE, ST+pAT+6),
686 PROC(commit, commit, commit, fhandle, RC_NOCACHE, ST+WC+2),
689 struct svc_version nfsd_version3 = {
692 .vs_proc = nfsd_procedures3,
693 .vs_dispatch = nfsd_dispatch,
694 .vs_xdrsize = NFS3_SVC_XDRSIZE,