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>
21 #include <linux/magic.h>
23 #include <linux/sunrpc/svc.h>
24 #include <linux/nfsd/nfsd.h>
25 #include <linux/nfsd/cache.h>
26 #include <linux/nfsd/xdr3.h>
27 #include <linux/nfs3.h>
29 #define NFSDDBG_FACILITY NFSDDBG_PROC
31 #define RETURN_STATUS(st) { resp->status = (st); return (st); }
33 static int nfs3_ftypes[] = {
40 S_IFSOCK, /* NF3SOCK */
41 S_IFIFO, /* NF3FIFO */
48 nfsd3_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
54 * Get a file's attributes
57 nfsd3_proc_getattr(struct svc_rqst *rqstp, struct nfsd_fhandle *argp,
58 struct nfsd3_attrstat *resp)
63 dprintk("nfsd: GETATTR(3) %s\n",
64 SVCFH_fmt(&argp->fh));
66 fh_copy(&resp->fh, &argp->fh);
67 nfserr = fh_verify(rqstp, &resp->fh, 0,
68 NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
70 RETURN_STATUS(nfserr);
72 err = vfs_getattr(resp->fh.fh_export->ex_path.mnt,
73 resp->fh.fh_dentry, &resp->stat);
74 nfserr = nfserrno(err);
76 RETURN_STATUS(nfserr);
80 * Set a file's attributes
83 nfsd3_proc_setattr(struct svc_rqst *rqstp, struct nfsd3_sattrargs *argp,
84 struct nfsd3_attrstat *resp)
88 dprintk("nfsd: SETATTR(3) %s\n",
89 SVCFH_fmt(&argp->fh));
91 fh_copy(&resp->fh, &argp->fh);
92 nfserr = nfsd_setattr(rqstp, &resp->fh, &argp->attrs,
93 argp->check_guard, argp->guardtime);
94 RETURN_STATUS(nfserr);
98 * Look up a path name component
101 nfsd3_proc_lookup(struct svc_rqst *rqstp, struct nfsd3_diropargs *argp,
102 struct nfsd3_diropres *resp)
106 dprintk("nfsd: LOOKUP(3) %s %.*s\n",
107 SVCFH_fmt(&argp->fh),
111 fh_copy(&resp->dirfh, &argp->fh);
112 fh_init(&resp->fh, NFS3_FHSIZE);
114 nfserr = nfsd_lookup(rqstp, &resp->dirfh,
118 RETURN_STATUS(nfserr);
125 nfsd3_proc_access(struct svc_rqst *rqstp, struct nfsd3_accessargs *argp,
126 struct nfsd3_accessres *resp)
130 dprintk("nfsd: ACCESS(3) %s 0x%x\n",
131 SVCFH_fmt(&argp->fh),
134 fh_copy(&resp->fh, &argp->fh);
135 resp->access = argp->access;
136 nfserr = nfsd_access(rqstp, &resp->fh, &resp->access, NULL);
137 RETURN_STATUS(nfserr);
144 nfsd3_proc_readlink(struct svc_rqst *rqstp, struct nfsd3_readlinkargs *argp,
145 struct nfsd3_readlinkres *resp)
149 dprintk("nfsd: READLINK(3) %s\n", SVCFH_fmt(&argp->fh));
151 /* Read the symlink. */
152 fh_copy(&resp->fh, &argp->fh);
153 resp->len = NFS3_MAXPATHLEN;
154 nfserr = nfsd_readlink(rqstp, &resp->fh, argp->buffer, &resp->len);
155 RETURN_STATUS(nfserr);
159 * Read a portion of a file.
162 nfsd3_proc_read(struct svc_rqst *rqstp, struct nfsd3_readargs *argp,
163 struct nfsd3_readres *resp)
166 u32 max_blocksize = svc_max_payload(rqstp);
168 dprintk("nfsd: READ(3) %s %lu bytes at %lu\n",
169 SVCFH_fmt(&argp->fh),
170 (unsigned long) argp->count,
171 (unsigned long) argp->offset);
173 /* Obtain buffer pointer for payload.
174 * 1 (status) + 22 (post_op_attr) + 1 (count) + 1 (eof)
175 * + 1 (xdr opaque byte count) = 26
178 resp->count = argp->count;
179 if (max_blocksize < resp->count)
180 resp->count = max_blocksize;
182 svc_reserve_auth(rqstp, ((1 + NFS3_POST_OP_ATTR_WORDS + 3)<<2) + resp->count +4);
184 fh_copy(&resp->fh, &argp->fh);
185 nfserr = nfsd_read(rqstp, &resp->fh, NULL,
187 rqstp->rq_vec, argp->vlen,
190 struct inode *inode = resp->fh.fh_dentry->d_inode;
192 resp->eof = (argp->offset + resp->count) >= inode->i_size;
195 RETURN_STATUS(nfserr);
199 * Write data to a file
202 nfsd3_proc_write(struct svc_rqst *rqstp, struct nfsd3_writeargs *argp,
203 struct nfsd3_writeres *resp)
206 unsigned long cnt = argp->len;
208 dprintk("nfsd: WRITE(3) %s %d bytes at %ld%s\n",
209 SVCFH_fmt(&argp->fh),
211 (unsigned long) argp->offset,
212 argp->stable? " stable" : "");
214 fh_copy(&resp->fh, &argp->fh);
215 resp->committed = argp->stable;
216 nfserr = nfsd_write(rqstp, &resp->fh, NULL,
218 rqstp->rq_vec, argp->vlen,
222 RETURN_STATUS(nfserr);
226 * With NFSv3, CREATE processing is a lot easier than with NFSv2.
227 * At least in theory; we'll see how it fares in practice when the
228 * first reports about SunOS compatibility problems start to pour in...
231 nfsd3_proc_create(struct svc_rqst *rqstp, struct nfsd3_createargs *argp,
232 struct nfsd3_diropres *resp)
234 svc_fh *dirfhp, *newfhp = NULL;
238 dprintk("nfsd: CREATE(3) %s %.*s\n",
239 SVCFH_fmt(&argp->fh),
243 dirfhp = fh_copy(&resp->dirfh, &argp->fh);
244 newfhp = fh_init(&resp->fh, NFS3_FHSIZE);
247 /* Get the directory inode */
248 nfserr = fh_verify(rqstp, dirfhp, S_IFDIR, NFSD_MAY_CREATE);
250 RETURN_STATUS(nfserr);
252 /* Unfudge the mode bits */
253 attr->ia_mode &= ~S_IFMT;
254 if (!(attr->ia_valid & ATTR_MODE)) {
255 attr->ia_valid |= ATTR_MODE;
256 attr->ia_mode = S_IFREG;
258 attr->ia_mode = (attr->ia_mode & ~S_IFMT) | S_IFREG;
261 /* Now create the file and set attributes */
262 nfserr = nfsd_create_v3(rqstp, dirfhp, argp->name, argp->len,
264 argp->createmode, argp->verf, NULL, NULL);
266 RETURN_STATUS(nfserr);
270 * Make directory. This operation is not idempotent.
273 nfsd3_proc_mkdir(struct svc_rqst *rqstp, struct nfsd3_createargs *argp,
274 struct nfsd3_diropres *resp)
278 dprintk("nfsd: MKDIR(3) %s %.*s\n",
279 SVCFH_fmt(&argp->fh),
283 argp->attrs.ia_valid &= ~ATTR_SIZE;
284 fh_copy(&resp->dirfh, &argp->fh);
285 fh_init(&resp->fh, NFS3_FHSIZE);
286 nfserr = nfsd_create(rqstp, &resp->dirfh, argp->name, argp->len,
287 &argp->attrs, S_IFDIR, 0, &resp->fh);
289 RETURN_STATUS(nfserr);
293 nfsd3_proc_symlink(struct svc_rqst *rqstp, struct nfsd3_symlinkargs *argp,
294 struct nfsd3_diropres *resp)
298 dprintk("nfsd: SYMLINK(3) %s %.*s -> %.*s\n",
299 SVCFH_fmt(&argp->ffh),
300 argp->flen, argp->fname,
301 argp->tlen, argp->tname);
303 fh_copy(&resp->dirfh, &argp->ffh);
304 fh_init(&resp->fh, NFS3_FHSIZE);
305 nfserr = nfsd_symlink(rqstp, &resp->dirfh, argp->fname, argp->flen,
306 argp->tname, argp->tlen,
307 &resp->fh, &argp->attrs);
308 RETURN_STATUS(nfserr);
312 * Make socket/fifo/device.
315 nfsd3_proc_mknod(struct svc_rqst *rqstp, struct nfsd3_mknodargs *argp,
316 struct nfsd3_diropres *resp)
322 dprintk("nfsd: MKNOD(3) %s %.*s\n",
323 SVCFH_fmt(&argp->fh),
327 fh_copy(&resp->dirfh, &argp->fh);
328 fh_init(&resp->fh, NFS3_FHSIZE);
330 if (argp->ftype == 0 || argp->ftype >= NF3BAD)
331 RETURN_STATUS(nfserr_inval);
332 if (argp->ftype == NF3CHR || argp->ftype == NF3BLK) {
333 rdev = MKDEV(argp->major, argp->minor);
334 if (MAJOR(rdev) != argp->major ||
335 MINOR(rdev) != argp->minor)
336 RETURN_STATUS(nfserr_inval);
338 if (argp->ftype != NF3SOCK && argp->ftype != NF3FIFO)
339 RETURN_STATUS(nfserr_inval);
341 type = nfs3_ftypes[argp->ftype];
342 nfserr = nfsd_create(rqstp, &resp->dirfh, argp->name, argp->len,
343 &argp->attrs, type, rdev, &resp->fh);
345 RETURN_STATUS(nfserr);
349 * Remove file/fifo/socket etc.
352 nfsd3_proc_remove(struct svc_rqst *rqstp, struct nfsd3_diropargs *argp,
353 struct nfsd3_attrstat *resp)
357 dprintk("nfsd: REMOVE(3) %s %.*s\n",
358 SVCFH_fmt(&argp->fh),
362 /* Unlink. -S_IFDIR means file must not be a directory */
363 fh_copy(&resp->fh, &argp->fh);
364 nfserr = nfsd_unlink(rqstp, &resp->fh, -S_IFDIR, argp->name, argp->len);
365 RETURN_STATUS(nfserr);
372 nfsd3_proc_rmdir(struct svc_rqst *rqstp, struct nfsd3_diropargs *argp,
373 struct nfsd3_attrstat *resp)
377 dprintk("nfsd: RMDIR(3) %s %.*s\n",
378 SVCFH_fmt(&argp->fh),
382 fh_copy(&resp->fh, &argp->fh);
383 nfserr = nfsd_unlink(rqstp, &resp->fh, S_IFDIR, argp->name, argp->len);
384 RETURN_STATUS(nfserr);
388 nfsd3_proc_rename(struct svc_rqst *rqstp, struct nfsd3_renameargs *argp,
389 struct nfsd3_renameres *resp)
393 dprintk("nfsd: RENAME(3) %s %.*s ->\n",
394 SVCFH_fmt(&argp->ffh),
397 dprintk("nfsd: -> %s %.*s\n",
398 SVCFH_fmt(&argp->tfh),
402 fh_copy(&resp->ffh, &argp->ffh);
403 fh_copy(&resp->tfh, &argp->tfh);
404 nfserr = nfsd_rename(rqstp, &resp->ffh, argp->fname, argp->flen,
405 &resp->tfh, argp->tname, argp->tlen);
406 RETURN_STATUS(nfserr);
410 nfsd3_proc_link(struct svc_rqst *rqstp, struct nfsd3_linkargs *argp,
411 struct nfsd3_linkres *resp)
415 dprintk("nfsd: LINK(3) %s ->\n",
416 SVCFH_fmt(&argp->ffh));
417 dprintk("nfsd: -> %s %.*s\n",
418 SVCFH_fmt(&argp->tfh),
422 fh_copy(&resp->fh, &argp->ffh);
423 fh_copy(&resp->tfh, &argp->tfh);
424 nfserr = nfsd_link(rqstp, &resp->tfh, argp->tname, argp->tlen,
426 RETURN_STATUS(nfserr);
430 * Read a portion of a directory.
433 nfsd3_proc_readdir(struct svc_rqst *rqstp, struct nfsd3_readdirargs *argp,
434 struct nfsd3_readdirres *resp)
439 dprintk("nfsd: READDIR(3) %s %d bytes at %d\n",
440 SVCFH_fmt(&argp->fh),
441 argp->count, (u32) argp->cookie);
443 /* Make sure we've room for the NULL ptr & eof flag, and shrink to
444 * client read size */
445 count = (argp->count >> 2) - 2;
447 /* Read directory and encode entries on the fly */
448 fh_copy(&resp->fh, &argp->fh);
450 resp->buflen = count;
451 resp->common.err = nfs_ok;
452 resp->buffer = argp->buffer;
454 nfserr = nfsd_readdir(rqstp, &resp->fh, (loff_t*) &argp->cookie,
455 &resp->common, nfs3svc_encode_entry);
456 memcpy(resp->verf, argp->verf, 8);
457 resp->count = resp->buffer - argp->buffer;
459 xdr_encode_hyper(resp->offset, argp->cookie);
461 RETURN_STATUS(nfserr);
465 * Read a portion of a directory, including file handles and attrs.
466 * For now, we choose to ignore the dircount parameter.
469 nfsd3_proc_readdirplus(struct svc_rqst *rqstp, struct nfsd3_readdirargs *argp,
470 struct nfsd3_readdirres *resp)
476 caddr_t page_addr = NULL;
478 dprintk("nfsd: READDIR+(3) %s %d bytes at %d\n",
479 SVCFH_fmt(&argp->fh),
480 argp->count, (u32) argp->cookie);
482 /* Convert byte count to number of words (i.e. >> 2),
483 * and reserve room for the NULL ptr & eof flag (-2 words) */
484 resp->count = (argp->count >> 2) - 2;
486 /* Read directory and encode entries on the fly */
487 fh_copy(&resp->fh, &argp->fh);
489 resp->common.err = nfs_ok;
490 resp->buffer = argp->buffer;
491 resp->buflen = resp->count;
493 offset = argp->cookie;
494 nfserr = nfsd_readdir(rqstp, &resp->fh,
497 nfs3svc_encode_entry_plus);
498 memcpy(resp->verf, argp->verf, 8);
499 for (i=1; i<rqstp->rq_resused ; i++) {
500 page_addr = page_address(rqstp->rq_respages[i]);
502 if (((caddr_t)resp->buffer >= page_addr) &&
503 ((caddr_t)resp->buffer < page_addr + PAGE_SIZE)) {
504 count += (caddr_t)resp->buffer - page_addr;
509 resp->count = count >> 2;
511 if (unlikely(resp->offset1)) {
512 /* we ended up with offset on a page boundary */
513 *resp->offset = htonl(offset >> 32);
514 *resp->offset1 = htonl(offset & 0xffffffff);
515 resp->offset1 = NULL;
517 xdr_encode_hyper(resp->offset, offset);
521 RETURN_STATUS(nfserr);
525 * Get file system stats
528 nfsd3_proc_fsstat(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
529 struct nfsd3_fsstatres *resp)
533 dprintk("nfsd: FSSTAT(3) %s\n",
534 SVCFH_fmt(&argp->fh));
536 nfserr = nfsd_statfs(rqstp, &argp->fh, &resp->stats, 0);
538 RETURN_STATUS(nfserr);
542 * Get file system info
545 nfsd3_proc_fsinfo(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
546 struct nfsd3_fsinfores *resp)
549 u32 max_blocksize = svc_max_payload(rqstp);
551 dprintk("nfsd: FSINFO(3) %s\n",
552 SVCFH_fmt(&argp->fh));
554 resp->f_rtmax = max_blocksize;
555 resp->f_rtpref = max_blocksize;
556 resp->f_rtmult = PAGE_SIZE;
557 resp->f_wtmax = max_blocksize;
558 resp->f_wtpref = max_blocksize;
559 resp->f_wtmult = PAGE_SIZE;
560 resp->f_dtpref = PAGE_SIZE;
561 resp->f_maxfilesize = ~(u32) 0;
562 resp->f_properties = NFS3_FSF_DEFAULT;
564 nfserr = fh_verify(rqstp, &argp->fh, 0,
565 NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
567 /* Check special features of the file system. May request
568 * different read/write sizes for file systems known to have
569 * problems with large blocks */
571 struct super_block *sb = argp->fh.fh_dentry->d_inode->i_sb;
573 /* Note that we don't care for remote fs's here */
574 if (sb->s_magic == MSDOS_SUPER_MAGIC) {
575 resp->f_properties = NFS3_FSF_BILLYBOY;
577 resp->f_maxfilesize = sb->s_maxbytes;
581 RETURN_STATUS(nfserr);
585 * Get pathconf info for the specified file
588 nfsd3_proc_pathconf(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
589 struct nfsd3_pathconfres *resp)
593 dprintk("nfsd: PATHCONF(3) %s\n",
594 SVCFH_fmt(&argp->fh));
596 /* Set default pathconf */
597 resp->p_link_max = 255; /* at least */
598 resp->p_name_max = 255; /* at least */
599 resp->p_no_trunc = 0;
600 resp->p_chown_restricted = 1;
601 resp->p_case_insensitive = 0;
602 resp->p_case_preserving = 1;
604 nfserr = fh_verify(rqstp, &argp->fh, 0, NFSD_MAY_NOP);
607 struct super_block *sb = argp->fh.fh_dentry->d_inode->i_sb;
609 /* Note that we don't care for remote fs's here */
610 switch (sb->s_magic) {
611 case EXT2_SUPER_MAGIC:
612 resp->p_link_max = EXT2_LINK_MAX;
613 resp->p_name_max = EXT2_NAME_LEN;
615 case MSDOS_SUPER_MAGIC:
616 resp->p_case_insensitive = 1;
617 resp->p_case_preserving = 0;
623 RETURN_STATUS(nfserr);
628 * Commit a file (range) to stable storage.
631 nfsd3_proc_commit(struct svc_rqst * rqstp, struct nfsd3_commitargs *argp,
632 struct nfsd3_commitres *resp)
636 dprintk("nfsd: COMMIT(3) %s %u@%Lu\n",
637 SVCFH_fmt(&argp->fh),
639 (unsigned long long) argp->offset);
641 if (argp->offset > NFS_OFFSET_MAX)
642 RETURN_STATUS(nfserr_inval);
644 fh_copy(&resp->fh, &argp->fh);
645 nfserr = nfsd_commit(rqstp, &resp->fh, argp->offset, argp->count);
647 RETURN_STATUS(nfserr);
652 * NFSv3 Server procedures.
653 * Only the results of non-idempotent operations are cached.
655 #define nfs3svc_decode_voidargs NULL
656 #define nfs3svc_release_void NULL
657 #define nfs3svc_decode_fhandleargs nfs3svc_decode_fhandle
658 #define nfs3svc_encode_attrstatres nfs3svc_encode_attrstat
659 #define nfs3svc_encode_wccstatres nfs3svc_encode_wccstat
660 #define nfsd3_mkdirargs nfsd3_createargs
661 #define nfsd3_readdirplusargs nfsd3_readdirargs
662 #define nfsd3_fhandleargs nfsd_fhandle
663 #define nfsd3_fhandleres nfsd3_attrstat
664 #define nfsd3_attrstatres nfsd3_attrstat
665 #define nfsd3_wccstatres nfsd3_attrstat
666 #define nfsd3_createres nfsd3_diropres
667 #define nfsd3_voidres nfsd3_voidargs
668 struct nfsd3_voidargs { int dummy; };
670 #define PROC(name, argt, rest, relt, cache, respsize) \
671 { (svc_procfunc) nfsd3_proc_##name, \
672 (kxdrproc_t) nfs3svc_decode_##argt##args, \
673 (kxdrproc_t) nfs3svc_encode_##rest##res, \
674 (kxdrproc_t) nfs3svc_release_##relt, \
675 sizeof(struct nfsd3_##argt##args), \
676 sizeof(struct nfsd3_##rest##res), \
682 #define ST 1 /* status*/
683 #define FH 17 /* filehandle with length */
684 #define AT 21 /* attributes */
685 #define pAT (1+AT) /* post attributes - conditional */
686 #define WC (7+pAT) /* WCC attributes */
688 static struct svc_procedure nfsd_procedures3[22] = {
689 PROC(null, void, void, void, RC_NOCACHE, ST),
690 PROC(getattr, fhandle, attrstat, fhandle, RC_NOCACHE, ST+AT),
691 PROC(setattr, sattr, wccstat, fhandle, RC_REPLBUFF, ST+WC),
692 PROC(lookup, dirop, dirop, fhandle2, RC_NOCACHE, ST+FH+pAT+pAT),
693 PROC(access, access, access, fhandle, RC_NOCACHE, ST+pAT+1),
694 PROC(readlink, readlink, readlink, fhandle, RC_NOCACHE, ST+pAT+1+NFS3_MAXPATHLEN/4),
695 PROC(read, read, read, fhandle, RC_NOCACHE, ST+pAT+4+NFSSVC_MAXBLKSIZE/4),
696 PROC(write, write, write, fhandle, RC_REPLBUFF, ST+WC+4),
697 PROC(create, create, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
698 PROC(mkdir, mkdir, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
699 PROC(symlink, symlink, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
700 PROC(mknod, mknod, create, fhandle2, RC_REPLBUFF, ST+(1+FH+pAT)+WC),
701 PROC(remove, dirop, wccstat, fhandle, RC_REPLBUFF, ST+WC),
702 PROC(rmdir, dirop, wccstat, fhandle, RC_REPLBUFF, ST+WC),
703 PROC(rename, rename, rename, fhandle2, RC_REPLBUFF, ST+WC+WC),
704 PROC(link, link, link, fhandle2, RC_REPLBUFF, ST+pAT+WC),
705 PROC(readdir, readdir, readdir, fhandle, RC_NOCACHE, 0),
706 PROC(readdirplus,readdirplus, readdir, fhandle, RC_NOCACHE, 0),
707 PROC(fsstat, fhandle, fsstat, void, RC_NOCACHE, ST+pAT+2*6+1),
708 PROC(fsinfo, fhandle, fsinfo, void, RC_NOCACHE, ST+pAT+12),
709 PROC(pathconf, fhandle, pathconf, void, RC_NOCACHE, ST+pAT+6),
710 PROC(commit, commit, commit, fhandle, RC_NOCACHE, ST+WC+2),
713 struct svc_version nfsd_version3 = {
716 .vs_proc = nfsd_procedures3,
717 .vs_dispatch = nfsd_dispatch,
718 .vs_xdrsize = NFS3_SVC_XDRSIZE,