Merge tag 'char-misc-6.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregk...
[linux-2.6-block.git] / fs / nfsd / nfs3proc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Process version 3 NFS requests.
4  *
5  * Copyright (C) 1996, 1997, 1998 Olaf Kirch <okir@monad.swb.de>
6  */
7
8 #include <linux/fs.h>
9 #include <linux/ext2_fs.h>
10 #include <linux/magic.h>
11 #include <linux/namei.h>
12
13 #include "cache.h"
14 #include "xdr3.h"
15 #include "vfs.h"
16 #include "filecache.h"
17
18 #define NFSDDBG_FACILITY                NFSDDBG_PROC
19
20 static int      nfs3_ftypes[] = {
21         0,                      /* NF3NON */
22         S_IFREG,                /* NF3REG */
23         S_IFDIR,                /* NF3DIR */
24         S_IFBLK,                /* NF3BLK */
25         S_IFCHR,                /* NF3CHR */
26         S_IFLNK,                /* NF3LNK */
27         S_IFSOCK,               /* NF3SOCK */
28         S_IFIFO,                /* NF3FIFO */
29 };
30
31 static __be32 nfsd3_map_status(__be32 status)
32 {
33         switch (status) {
34         case nfs_ok:
35                 break;
36         case nfserr_nofilehandle:
37                 status = nfserr_badhandle;
38                 break;
39         case nfserr_wrongsec:
40         case nfserr_file_open:
41                 status = nfserr_acces;
42                 break;
43         case nfserr_symlink_not_dir:
44                 status = nfserr_notdir;
45                 break;
46         case nfserr_symlink:
47         case nfserr_wrong_type:
48                 status = nfserr_inval;
49                 break;
50         }
51         return status;
52 }
53
54 /*
55  * NULL call.
56  */
57 static __be32
58 nfsd3_proc_null(struct svc_rqst *rqstp)
59 {
60         return rpc_success;
61 }
62
63 /*
64  * Get a file's attributes
65  */
66 static __be32
67 nfsd3_proc_getattr(struct svc_rqst *rqstp)
68 {
69         struct nfsd_fhandle *argp = rqstp->rq_argp;
70         struct nfsd3_attrstat *resp = rqstp->rq_resp;
71
72         dprintk("nfsd: GETATTR(3)  %s\n",
73                 SVCFH_fmt(&argp->fh));
74
75         fh_copy(&resp->fh, &argp->fh);
76         resp->status = fh_verify(rqstp, &resp->fh, 0,
77                                  NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
78         if (resp->status != nfs_ok)
79                 goto out;
80
81         resp->status = fh_getattr(&resp->fh, &resp->stat);
82 out:
83         resp->status = nfsd3_map_status(resp->status);
84         return rpc_success;
85 }
86
87 /*
88  * Set a file's attributes
89  */
90 static __be32
91 nfsd3_proc_setattr(struct svc_rqst *rqstp)
92 {
93         struct nfsd3_sattrargs *argp = rqstp->rq_argp;
94         struct nfsd3_attrstat *resp = rqstp->rq_resp;
95         struct nfsd_attrs attrs = {
96                 .na_iattr       = &argp->attrs,
97         };
98         const struct timespec64 *guardtime = NULL;
99
100         dprintk("nfsd: SETATTR(3)  %s\n",
101                                 SVCFH_fmt(&argp->fh));
102
103         fh_copy(&resp->fh, &argp->fh);
104         if (argp->check_guard)
105                 guardtime = &argp->guardtime;
106         resp->status = nfsd_setattr(rqstp, &resp->fh, &attrs, guardtime);
107         resp->status = nfsd3_map_status(resp->status);
108         return rpc_success;
109 }
110
111 /*
112  * Look up a path name component
113  */
114 static __be32
115 nfsd3_proc_lookup(struct svc_rqst *rqstp)
116 {
117         struct nfsd3_diropargs *argp = rqstp->rq_argp;
118         struct nfsd3_diropres  *resp = rqstp->rq_resp;
119
120         dprintk("nfsd: LOOKUP(3)   %s %.*s\n",
121                                 SVCFH_fmt(&argp->fh),
122                                 argp->len,
123                                 argp->name);
124
125         fh_copy(&resp->dirfh, &argp->fh);
126         fh_init(&resp->fh, NFS3_FHSIZE);
127
128         resp->status = nfsd_lookup(rqstp, &resp->dirfh,
129                                    argp->name, argp->len,
130                                    &resp->fh);
131         resp->status = nfsd3_map_status(resp->status);
132         return rpc_success;
133 }
134
135 /*
136  * Check file access
137  */
138 static __be32
139 nfsd3_proc_access(struct svc_rqst *rqstp)
140 {
141         struct nfsd3_accessargs *argp = rqstp->rq_argp;
142         struct nfsd3_accessres *resp = rqstp->rq_resp;
143
144         dprintk("nfsd: ACCESS(3)   %s 0x%x\n",
145                                 SVCFH_fmt(&argp->fh),
146                                 argp->access);
147
148         fh_copy(&resp->fh, &argp->fh);
149         resp->access = argp->access;
150         resp->status = nfsd_access(rqstp, &resp->fh, &resp->access, NULL);
151         resp->status = nfsd3_map_status(resp->status);
152         return rpc_success;
153 }
154
155 /*
156  * Read a symlink.
157  */
158 static __be32
159 nfsd3_proc_readlink(struct svc_rqst *rqstp)
160 {
161         struct nfsd_fhandle *argp = rqstp->rq_argp;
162         struct nfsd3_readlinkres *resp = rqstp->rq_resp;
163
164         dprintk("nfsd: READLINK(3) %s\n", SVCFH_fmt(&argp->fh));
165
166         /* Read the symlink. */
167         fh_copy(&resp->fh, &argp->fh);
168         resp->len = NFS3_MAXPATHLEN;
169         resp->pages = rqstp->rq_next_page++;
170         resp->status = nfsd_readlink(rqstp, &resp->fh,
171                                      page_address(*resp->pages), &resp->len);
172         resp->status = nfsd3_map_status(resp->status);
173         return rpc_success;
174 }
175
176 /*
177  * Read a portion of a file.
178  */
179 static __be32
180 nfsd3_proc_read(struct svc_rqst *rqstp)
181 {
182         struct nfsd3_readargs *argp = rqstp->rq_argp;
183         struct nfsd3_readres *resp = rqstp->rq_resp;
184
185         dprintk("nfsd: READ(3) %s %lu bytes at %Lu\n",
186                                 SVCFH_fmt(&argp->fh),
187                                 (unsigned long) argp->count,
188                                 (unsigned long long) argp->offset);
189
190         argp->count = min_t(u32, argp->count, svc_max_payload(rqstp));
191         argp->count = min_t(u32, argp->count, rqstp->rq_res.buflen);
192         if (argp->offset > (u64)OFFSET_MAX)
193                 argp->offset = (u64)OFFSET_MAX;
194         if (argp->offset + argp->count > (u64)OFFSET_MAX)
195                 argp->count = (u64)OFFSET_MAX - argp->offset;
196
197         resp->pages = rqstp->rq_next_page;
198
199         /* Obtain buffer pointer for payload.
200          * 1 (status) + 22 (post_op_attr) + 1 (count) + 1 (eof)
201          * + 1 (xdr opaque byte count) = 26
202          */
203         resp->count = argp->count;
204         svc_reserve_auth(rqstp, ((1 + NFS3_POST_OP_ATTR_WORDS + 3) << 2) +
205                          resp->count + 4);
206
207         fh_copy(&resp->fh, &argp->fh);
208         resp->status = nfsd_read(rqstp, &resp->fh, argp->offset,
209                                  &resp->count, &resp->eof);
210         resp->status = nfsd3_map_status(resp->status);
211         return rpc_success;
212 }
213
214 /*
215  * Write data to a file
216  */
217 static __be32
218 nfsd3_proc_write(struct svc_rqst *rqstp)
219 {
220         struct nfsd3_writeargs *argp = rqstp->rq_argp;
221         struct nfsd3_writeres *resp = rqstp->rq_resp;
222         unsigned long cnt = argp->len;
223         unsigned int nvecs;
224
225         dprintk("nfsd: WRITE(3)    %s %d bytes at %Lu%s\n",
226                                 SVCFH_fmt(&argp->fh),
227                                 argp->len,
228                                 (unsigned long long) argp->offset,
229                                 argp->stable ? " stable" : "");
230
231         resp->status = nfserr_fbig;
232         if (argp->offset > (u64)OFFSET_MAX ||
233             argp->offset + argp->len > (u64)OFFSET_MAX)
234                 return rpc_success;
235
236         fh_copy(&resp->fh, &argp->fh);
237         resp->committed = argp->stable;
238         nvecs = svc_fill_write_vector(rqstp, &argp->payload);
239
240         resp->status = nfsd_write(rqstp, &resp->fh, argp->offset,
241                                   rqstp->rq_vec, nvecs, &cnt,
242                                   resp->committed, resp->verf);
243         resp->count = cnt;
244         resp->status = nfsd3_map_status(resp->status);
245         return rpc_success;
246 }
247
248 /*
249  * Implement NFSv3's unchecked, guarded, and exclusive CREATE
250  * semantics for regular files. Except for the created file,
251  * this operation is stateless on the server.
252  *
253  * Upon return, caller must release @fhp and @resfhp.
254  */
255 static __be32
256 nfsd3_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
257                   struct svc_fh *resfhp, struct nfsd3_createargs *argp)
258 {
259         struct iattr *iap = &argp->attrs;
260         struct dentry *parent, *child;
261         struct nfsd_attrs attrs = {
262                 .na_iattr       = iap,
263         };
264         __u32 v_mtime, v_atime;
265         struct inode *inode;
266         __be32 status;
267         int host_err;
268
269         if (isdotent(argp->name, argp->len))
270                 return nfserr_exist;
271         if (!(iap->ia_valid & ATTR_MODE))
272                 iap->ia_mode = 0;
273
274         status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
275         if (status != nfs_ok)
276                 return status;
277
278         parent = fhp->fh_dentry;
279         inode = d_inode(parent);
280
281         host_err = fh_want_write(fhp);
282         if (host_err)
283                 return nfserrno(host_err);
284
285         inode_lock_nested(inode, I_MUTEX_PARENT);
286
287         child = lookup_one_len(argp->name, parent, argp->len);
288         if (IS_ERR(child)) {
289                 status = nfserrno(PTR_ERR(child));
290                 goto out;
291         }
292
293         if (d_really_is_negative(child)) {
294                 status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
295                 if (status != nfs_ok)
296                         goto out;
297         }
298
299         status = fh_compose(resfhp, fhp->fh_export, child, fhp);
300         if (status != nfs_ok)
301                 goto out;
302
303         v_mtime = 0;
304         v_atime = 0;
305         if (argp->createmode == NFS3_CREATE_EXCLUSIVE) {
306                 u32 *verifier = (u32 *)argp->verf;
307
308                 /*
309                  * Solaris 7 gets confused (bugid 4218508) if these have
310                  * the high bit set, as do xfs filesystems without the
311                  * "bigtime" feature. So just clear the high bits.
312                  */
313                 v_mtime = verifier[0] & 0x7fffffff;
314                 v_atime = verifier[1] & 0x7fffffff;
315         }
316
317         if (d_really_is_positive(child)) {
318                 status = nfs_ok;
319
320                 switch (argp->createmode) {
321                 case NFS3_CREATE_UNCHECKED:
322                         if (!d_is_reg(child))
323                                 break;
324                         iap->ia_valid &= ATTR_SIZE;
325                         goto set_attr;
326                 case NFS3_CREATE_GUARDED:
327                         status = nfserr_exist;
328                         break;
329                 case NFS3_CREATE_EXCLUSIVE:
330                         if (inode_get_mtime_sec(d_inode(child)) == v_mtime &&
331                             inode_get_atime_sec(d_inode(child)) == v_atime &&
332                             d_inode(child)->i_size == 0) {
333                                 break;
334                         }
335                         status = nfserr_exist;
336                 }
337                 goto out;
338         }
339
340         if (!IS_POSIXACL(inode))
341                 iap->ia_mode &= ~current_umask();
342
343         status = fh_fill_pre_attrs(fhp);
344         if (status != nfs_ok)
345                 goto out;
346         host_err = vfs_create(&nop_mnt_idmap, inode, child, iap->ia_mode, true);
347         if (host_err < 0) {
348                 status = nfserrno(host_err);
349                 goto out;
350         }
351         fh_fill_post_attrs(fhp);
352
353         /* A newly created file already has a file size of zero. */
354         if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
355                 iap->ia_valid &= ~ATTR_SIZE;
356         if (argp->createmode == NFS3_CREATE_EXCLUSIVE) {
357                 iap->ia_valid = ATTR_MTIME | ATTR_ATIME |
358                                 ATTR_MTIME_SET | ATTR_ATIME_SET;
359                 iap->ia_mtime.tv_sec = v_mtime;
360                 iap->ia_atime.tv_sec = v_atime;
361                 iap->ia_mtime.tv_nsec = 0;
362                 iap->ia_atime.tv_nsec = 0;
363         }
364
365 set_attr:
366         status = nfsd_create_setattr(rqstp, fhp, resfhp, &attrs);
367
368 out:
369         inode_unlock(inode);
370         if (child && !IS_ERR(child))
371                 dput(child);
372         fh_drop_write(fhp);
373         return status;
374 }
375
376 static __be32
377 nfsd3_proc_create(struct svc_rqst *rqstp)
378 {
379         struct nfsd3_createargs *argp = rqstp->rq_argp;
380         struct nfsd3_diropres *resp = rqstp->rq_resp;
381         svc_fh *dirfhp, *newfhp;
382
383         dprintk("nfsd: CREATE(3)   %s %.*s\n",
384                                 SVCFH_fmt(&argp->fh),
385                                 argp->len,
386                                 argp->name);
387
388         dirfhp = fh_copy(&resp->dirfh, &argp->fh);
389         newfhp = fh_init(&resp->fh, NFS3_FHSIZE);
390
391         resp->status = nfsd3_create_file(rqstp, dirfhp, newfhp, argp);
392         resp->status = nfsd3_map_status(resp->status);
393         return rpc_success;
394 }
395
396 /*
397  * Make directory. This operation is not idempotent.
398  */
399 static __be32
400 nfsd3_proc_mkdir(struct svc_rqst *rqstp)
401 {
402         struct nfsd3_createargs *argp = rqstp->rq_argp;
403         struct nfsd3_diropres *resp = rqstp->rq_resp;
404         struct nfsd_attrs attrs = {
405                 .na_iattr       = &argp->attrs,
406         };
407
408         dprintk("nfsd: MKDIR(3)    %s %.*s\n",
409                                 SVCFH_fmt(&argp->fh),
410                                 argp->len,
411                                 argp->name);
412
413         argp->attrs.ia_valid &= ~ATTR_SIZE;
414         fh_copy(&resp->dirfh, &argp->fh);
415         fh_init(&resp->fh, NFS3_FHSIZE);
416         resp->status = nfsd_create(rqstp, &resp->dirfh, argp->name, argp->len,
417                                    &attrs, S_IFDIR, 0, &resp->fh);
418         resp->status = nfsd3_map_status(resp->status);
419         return rpc_success;
420 }
421
422 static __be32
423 nfsd3_proc_symlink(struct svc_rqst *rqstp)
424 {
425         struct nfsd3_symlinkargs *argp = rqstp->rq_argp;
426         struct nfsd3_diropres *resp = rqstp->rq_resp;
427         struct nfsd_attrs attrs = {
428                 .na_iattr       = &argp->attrs,
429         };
430
431         if (argp->tlen == 0) {
432                 resp->status = nfserr_inval;
433                 goto out;
434         }
435         if (argp->tlen > NFS3_MAXPATHLEN) {
436                 resp->status = nfserr_nametoolong;
437                 goto out;
438         }
439
440         argp->tname = svc_fill_symlink_pathname(rqstp, &argp->first,
441                                                 page_address(rqstp->rq_arg.pages[0]),
442                                                 argp->tlen);
443         if (IS_ERR(argp->tname)) {
444                 resp->status = nfserrno(PTR_ERR(argp->tname));
445                 goto out;
446         }
447
448         dprintk("nfsd: SYMLINK(3)  %s %.*s -> %.*s\n",
449                                 SVCFH_fmt(&argp->ffh),
450                                 argp->flen, argp->fname,
451                                 argp->tlen, argp->tname);
452
453         fh_copy(&resp->dirfh, &argp->ffh);
454         fh_init(&resp->fh, NFS3_FHSIZE);
455         resp->status = nfsd_symlink(rqstp, &resp->dirfh, argp->fname,
456                                     argp->flen, argp->tname, &attrs, &resp->fh);
457         kfree(argp->tname);
458 out:
459         resp->status = nfsd3_map_status(resp->status);
460         return rpc_success;
461 }
462
463 /*
464  * Make socket/fifo/device.
465  */
466 static __be32
467 nfsd3_proc_mknod(struct svc_rqst *rqstp)
468 {
469         struct nfsd3_mknodargs *argp = rqstp->rq_argp;
470         struct nfsd3_diropres  *resp = rqstp->rq_resp;
471         struct nfsd_attrs attrs = {
472                 .na_iattr       = &argp->attrs,
473         };
474         int type;
475         dev_t   rdev = 0;
476
477         dprintk("nfsd: MKNOD(3)    %s %.*s\n",
478                                 SVCFH_fmt(&argp->fh),
479                                 argp->len,
480                                 argp->name);
481
482         fh_copy(&resp->dirfh, &argp->fh);
483         fh_init(&resp->fh, NFS3_FHSIZE);
484
485         if (argp->ftype == NF3CHR || argp->ftype == NF3BLK) {
486                 rdev = MKDEV(argp->major, argp->minor);
487                 if (MAJOR(rdev) != argp->major ||
488                     MINOR(rdev) != argp->minor) {
489                         resp->status = nfserr_inval;
490                         goto out;
491                 }
492         } else if (argp->ftype != NF3SOCK && argp->ftype != NF3FIFO) {
493                 resp->status = nfserr_badtype;
494                 goto out;
495         }
496
497         type = nfs3_ftypes[argp->ftype];
498         resp->status = nfsd_create(rqstp, &resp->dirfh, argp->name, argp->len,
499                                    &attrs, type, rdev, &resp->fh);
500 out:
501         resp->status = nfsd3_map_status(resp->status);
502         return rpc_success;
503 }
504
505 /*
506  * Remove file/fifo/socket etc.
507  */
508 static __be32
509 nfsd3_proc_remove(struct svc_rqst *rqstp)
510 {
511         struct nfsd3_diropargs *argp = rqstp->rq_argp;
512         struct nfsd3_attrstat *resp = rqstp->rq_resp;
513
514         dprintk("nfsd: REMOVE(3)   %s %.*s\n",
515                                 SVCFH_fmt(&argp->fh),
516                                 argp->len,
517                                 argp->name);
518
519         /* Unlink. -S_IFDIR means file must not be a directory */
520         fh_copy(&resp->fh, &argp->fh);
521         resp->status = nfsd_unlink(rqstp, &resp->fh, -S_IFDIR,
522                                    argp->name, argp->len);
523         resp->status = nfsd3_map_status(resp->status);
524         return rpc_success;
525 }
526
527 /*
528  * Remove a directory
529  */
530 static __be32
531 nfsd3_proc_rmdir(struct svc_rqst *rqstp)
532 {
533         struct nfsd3_diropargs *argp = rqstp->rq_argp;
534         struct nfsd3_attrstat *resp = rqstp->rq_resp;
535
536         dprintk("nfsd: RMDIR(3)    %s %.*s\n",
537                                 SVCFH_fmt(&argp->fh),
538                                 argp->len,
539                                 argp->name);
540
541         fh_copy(&resp->fh, &argp->fh);
542         resp->status = nfsd_unlink(rqstp, &resp->fh, S_IFDIR,
543                                    argp->name, argp->len);
544         resp->status = nfsd3_map_status(resp->status);
545         return rpc_success;
546 }
547
548 static __be32
549 nfsd3_proc_rename(struct svc_rqst *rqstp)
550 {
551         struct nfsd3_renameargs *argp = rqstp->rq_argp;
552         struct nfsd3_renameres *resp = rqstp->rq_resp;
553
554         dprintk("nfsd: RENAME(3)   %s %.*s ->\n",
555                                 SVCFH_fmt(&argp->ffh),
556                                 argp->flen,
557                                 argp->fname);
558         dprintk("nfsd: -> %s %.*s\n",
559                                 SVCFH_fmt(&argp->tfh),
560                                 argp->tlen,
561                                 argp->tname);
562
563         fh_copy(&resp->ffh, &argp->ffh);
564         fh_copy(&resp->tfh, &argp->tfh);
565         resp->status = nfsd_rename(rqstp, &resp->ffh, argp->fname, argp->flen,
566                                    &resp->tfh, argp->tname, argp->tlen);
567         resp->status = nfsd3_map_status(resp->status);
568         return rpc_success;
569 }
570
571 static __be32
572 nfsd3_proc_link(struct svc_rqst *rqstp)
573 {
574         struct nfsd3_linkargs *argp = rqstp->rq_argp;
575         struct nfsd3_linkres  *resp = rqstp->rq_resp;
576
577         dprintk("nfsd: LINK(3)     %s ->\n",
578                                 SVCFH_fmt(&argp->ffh));
579         dprintk("nfsd:   -> %s %.*s\n",
580                                 SVCFH_fmt(&argp->tfh),
581                                 argp->tlen,
582                                 argp->tname);
583
584         fh_copy(&resp->fh,  &argp->ffh);
585         fh_copy(&resp->tfh, &argp->tfh);
586         resp->status = nfsd_link(rqstp, &resp->tfh, argp->tname, argp->tlen,
587                                  &resp->fh);
588         resp->status = nfsd3_map_status(resp->status);
589         return rpc_success;
590 }
591
592 static void nfsd3_init_dirlist_pages(struct svc_rqst *rqstp,
593                                      struct nfsd3_readdirres *resp,
594                                      u32 count)
595 {
596         struct xdr_buf *buf = &resp->dirlist;
597         struct xdr_stream *xdr = &resp->xdr;
598         unsigned int sendbuf = min_t(unsigned int, rqstp->rq_res.buflen,
599                                      svc_max_payload(rqstp));
600
601         memset(buf, 0, sizeof(*buf));
602
603         /* Reserve room for the NULL ptr & eof flag (-2 words) */
604         buf->buflen = clamp(count, (u32)(XDR_UNIT * 2), sendbuf);
605         buf->buflen -= XDR_UNIT * 2;
606         buf->pages = rqstp->rq_next_page;
607         rqstp->rq_next_page += (buf->buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
608
609         xdr_init_encode_pages(xdr, buf, buf->pages,  NULL);
610 }
611
612 /*
613  * Read a portion of a directory.
614  */
615 static __be32
616 nfsd3_proc_readdir(struct svc_rqst *rqstp)
617 {
618         struct nfsd3_readdirargs *argp = rqstp->rq_argp;
619         struct nfsd3_readdirres  *resp = rqstp->rq_resp;
620         loff_t          offset;
621
622         dprintk("nfsd: READDIR(3)  %s %d bytes at %d\n",
623                                 SVCFH_fmt(&argp->fh),
624                                 argp->count, (u32) argp->cookie);
625
626         nfsd3_init_dirlist_pages(rqstp, resp, argp->count);
627
628         fh_copy(&resp->fh, &argp->fh);
629         resp->common.err = nfs_ok;
630         resp->cookie_offset = 0;
631         resp->rqstp = rqstp;
632         offset = argp->cookie;
633         resp->status = nfsd_readdir(rqstp, &resp->fh, &offset,
634                                     &resp->common, nfs3svc_encode_entry3);
635         memcpy(resp->verf, argp->verf, 8);
636         nfs3svc_encode_cookie3(resp, offset);
637
638         /* Recycle only pages that were part of the reply */
639         rqstp->rq_next_page = resp->xdr.page_ptr + 1;
640
641         resp->status = nfsd3_map_status(resp->status);
642         return rpc_success;
643 }
644
645 /*
646  * Read a portion of a directory, including file handles and attrs.
647  * For now, we choose to ignore the dircount parameter.
648  */
649 static __be32
650 nfsd3_proc_readdirplus(struct svc_rqst *rqstp)
651 {
652         struct nfsd3_readdirargs *argp = rqstp->rq_argp;
653         struct nfsd3_readdirres  *resp = rqstp->rq_resp;
654         loff_t  offset;
655
656         dprintk("nfsd: READDIR+(3) %s %d bytes at %d\n",
657                                 SVCFH_fmt(&argp->fh),
658                                 argp->count, (u32) argp->cookie);
659
660         nfsd3_init_dirlist_pages(rqstp, resp, argp->count);
661
662         fh_copy(&resp->fh, &argp->fh);
663         resp->common.err = nfs_ok;
664         resp->cookie_offset = 0;
665         resp->rqstp = rqstp;
666         offset = argp->cookie;
667
668         resp->status = fh_verify(rqstp, &resp->fh, S_IFDIR, NFSD_MAY_NOP);
669         if (resp->status != nfs_ok)
670                 goto out;
671
672         if (resp->fh.fh_export->ex_flags & NFSEXP_NOREADDIRPLUS) {
673                 resp->status = nfserr_notsupp;
674                 goto out;
675         }
676
677         resp->status = nfsd_readdir(rqstp, &resp->fh, &offset,
678                                     &resp->common, nfs3svc_encode_entryplus3);
679         memcpy(resp->verf, argp->verf, 8);
680         nfs3svc_encode_cookie3(resp, offset);
681
682         /* Recycle only pages that were part of the reply */
683         rqstp->rq_next_page = resp->xdr.page_ptr + 1;
684
685 out:
686         resp->status = nfsd3_map_status(resp->status);
687         return rpc_success;
688 }
689
690 /*
691  * Get file system stats
692  */
693 static __be32
694 nfsd3_proc_fsstat(struct svc_rqst *rqstp)
695 {
696         struct nfsd_fhandle *argp = rqstp->rq_argp;
697         struct nfsd3_fsstatres *resp = rqstp->rq_resp;
698
699         dprintk("nfsd: FSSTAT(3)   %s\n",
700                                 SVCFH_fmt(&argp->fh));
701
702         resp->status = nfsd_statfs(rqstp, &argp->fh, &resp->stats, 0);
703         fh_put(&argp->fh);
704         resp->status = nfsd3_map_status(resp->status);
705         return rpc_success;
706 }
707
708 /*
709  * Get file system info
710  */
711 static __be32
712 nfsd3_proc_fsinfo(struct svc_rqst *rqstp)
713 {
714         struct nfsd_fhandle *argp = rqstp->rq_argp;
715         struct nfsd3_fsinfores *resp = rqstp->rq_resp;
716         u32     max_blocksize = svc_max_payload(rqstp);
717
718         dprintk("nfsd: FSINFO(3)   %s\n",
719                                 SVCFH_fmt(&argp->fh));
720
721         resp->f_rtmax  = max_blocksize;
722         resp->f_rtpref = max_blocksize;
723         resp->f_rtmult = PAGE_SIZE;
724         resp->f_wtmax  = max_blocksize;
725         resp->f_wtpref = max_blocksize;
726         resp->f_wtmult = PAGE_SIZE;
727         resp->f_dtpref = max_blocksize;
728         resp->f_maxfilesize = ~(u32) 0;
729         resp->f_properties = NFS3_FSF_DEFAULT;
730
731         resp->status = fh_verify(rqstp, &argp->fh, 0,
732                                  NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
733
734         /* Check special features of the file system. May request
735          * different read/write sizes for file systems known to have
736          * problems with large blocks */
737         if (resp->status == nfs_ok) {
738                 struct super_block *sb = argp->fh.fh_dentry->d_sb;
739
740                 /* Note that we don't care for remote fs's here */
741                 if (sb->s_magic == MSDOS_SUPER_MAGIC) {
742                         resp->f_properties = NFS3_FSF_BILLYBOY;
743                 }
744                 resp->f_maxfilesize = sb->s_maxbytes;
745         }
746
747         fh_put(&argp->fh);
748         resp->status = nfsd3_map_status(resp->status);
749         return rpc_success;
750 }
751
752 /*
753  * Get pathconf info for the specified file
754  */
755 static __be32
756 nfsd3_proc_pathconf(struct svc_rqst *rqstp)
757 {
758         struct nfsd_fhandle *argp = rqstp->rq_argp;
759         struct nfsd3_pathconfres *resp = rqstp->rq_resp;
760
761         dprintk("nfsd: PATHCONF(3) %s\n",
762                                 SVCFH_fmt(&argp->fh));
763
764         /* Set default pathconf */
765         resp->p_link_max = 255;         /* at least */
766         resp->p_name_max = 255;         /* at least */
767         resp->p_no_trunc = 0;
768         resp->p_chown_restricted = 1;
769         resp->p_case_insensitive = 0;
770         resp->p_case_preserving = 1;
771
772         resp->status = fh_verify(rqstp, &argp->fh, 0, NFSD_MAY_NOP);
773
774         if (resp->status == nfs_ok) {
775                 struct super_block *sb = argp->fh.fh_dentry->d_sb;
776
777                 /* Note that we don't care for remote fs's here */
778                 switch (sb->s_magic) {
779                 case EXT2_SUPER_MAGIC:
780                         resp->p_link_max = EXT2_LINK_MAX;
781                         resp->p_name_max = EXT2_NAME_LEN;
782                         break;
783                 case MSDOS_SUPER_MAGIC:
784                         resp->p_case_insensitive = 1;
785                         resp->p_case_preserving  = 0;
786                         break;
787                 }
788         }
789
790         fh_put(&argp->fh);
791         resp->status = nfsd3_map_status(resp->status);
792         return rpc_success;
793 }
794
795 /*
796  * Commit a file (range) to stable storage.
797  */
798 static __be32
799 nfsd3_proc_commit(struct svc_rqst *rqstp)
800 {
801         struct nfsd3_commitargs *argp = rqstp->rq_argp;
802         struct nfsd3_commitres *resp = rqstp->rq_resp;
803         struct nfsd_file *nf;
804
805         dprintk("nfsd: COMMIT(3)   %s %u@%Lu\n",
806                                 SVCFH_fmt(&argp->fh),
807                                 argp->count,
808                                 (unsigned long long) argp->offset);
809
810         fh_copy(&resp->fh, &argp->fh);
811         resp->status = nfsd_file_acquire_gc(rqstp, &resp->fh, NFSD_MAY_WRITE |
812                                             NFSD_MAY_NOT_BREAK_LEASE, &nf);
813         if (resp->status)
814                 goto out;
815         resp->status = nfsd_commit(rqstp, &resp->fh, nf, argp->offset,
816                                    argp->count, resp->verf);
817         nfsd_file_put(nf);
818 out:
819         resp->status = nfsd3_map_status(resp->status);
820         return rpc_success;
821 }
822
823
824 /*
825  * NFSv3 Server procedures.
826  * Only the results of non-idempotent operations are cached.
827  */
828 #define nfs3svc_encode_attrstatres      nfs3svc_encode_attrstat
829 #define nfs3svc_encode_wccstatres       nfs3svc_encode_wccstat
830 #define nfsd3_mkdirargs                 nfsd3_createargs
831 #define nfsd3_readdirplusargs           nfsd3_readdirargs
832 #define nfsd3_fhandleargs               nfsd_fhandle
833 #define nfsd3_attrstatres               nfsd3_attrstat
834 #define nfsd3_wccstatres                nfsd3_attrstat
835 #define nfsd3_createres                 nfsd3_diropres
836
837 #define ST 1            /* status*/
838 #define FH 17           /* filehandle with length */
839 #define AT 21           /* attributes */
840 #define pAT (1+AT)      /* post attributes - conditional */
841 #define WC (7+pAT)      /* WCC attributes */
842
843 static const struct svc_procedure nfsd_procedures3[22] = {
844         [NFS3PROC_NULL] = {
845                 .pc_func = nfsd3_proc_null,
846                 .pc_decode = nfssvc_decode_voidarg,
847                 .pc_encode = nfssvc_encode_voidres,
848                 .pc_argsize = sizeof(struct nfsd_voidargs),
849                 .pc_argzero = sizeof(struct nfsd_voidargs),
850                 .pc_ressize = sizeof(struct nfsd_voidres),
851                 .pc_cachetype = RC_NOCACHE,
852                 .pc_xdrressize = ST,
853                 .pc_name = "NULL",
854         },
855         [NFS3PROC_GETATTR] = {
856                 .pc_func = nfsd3_proc_getattr,
857                 .pc_decode = nfs3svc_decode_fhandleargs,
858                 .pc_encode = nfs3svc_encode_getattrres,
859                 .pc_release = nfs3svc_release_fhandle,
860                 .pc_argsize = sizeof(struct nfsd_fhandle),
861                 .pc_argzero = sizeof(struct nfsd_fhandle),
862                 .pc_ressize = sizeof(struct nfsd3_attrstatres),
863                 .pc_cachetype = RC_NOCACHE,
864                 .pc_xdrressize = ST+AT,
865                 .pc_name = "GETATTR",
866         },
867         [NFS3PROC_SETATTR] = {
868                 .pc_func = nfsd3_proc_setattr,
869                 .pc_decode = nfs3svc_decode_sattrargs,
870                 .pc_encode = nfs3svc_encode_wccstatres,
871                 .pc_release = nfs3svc_release_fhandle,
872                 .pc_argsize = sizeof(struct nfsd3_sattrargs),
873                 .pc_argzero = sizeof(struct nfsd3_sattrargs),
874                 .pc_ressize = sizeof(struct nfsd3_wccstatres),
875                 .pc_cachetype = RC_REPLBUFF,
876                 .pc_xdrressize = ST+WC,
877                 .pc_name = "SETATTR",
878         },
879         [NFS3PROC_LOOKUP] = {
880                 .pc_func = nfsd3_proc_lookup,
881                 .pc_decode = nfs3svc_decode_diropargs,
882                 .pc_encode = nfs3svc_encode_lookupres,
883                 .pc_release = nfs3svc_release_fhandle2,
884                 .pc_argsize = sizeof(struct nfsd3_diropargs),
885                 .pc_argzero = sizeof(struct nfsd3_diropargs),
886                 .pc_ressize = sizeof(struct nfsd3_diropres),
887                 .pc_cachetype = RC_NOCACHE,
888                 .pc_xdrressize = ST+FH+pAT+pAT,
889                 .pc_name = "LOOKUP",
890         },
891         [NFS3PROC_ACCESS] = {
892                 .pc_func = nfsd3_proc_access,
893                 .pc_decode = nfs3svc_decode_accessargs,
894                 .pc_encode = nfs3svc_encode_accessres,
895                 .pc_release = nfs3svc_release_fhandle,
896                 .pc_argsize = sizeof(struct nfsd3_accessargs),
897                 .pc_argzero = sizeof(struct nfsd3_accessargs),
898                 .pc_ressize = sizeof(struct nfsd3_accessres),
899                 .pc_cachetype = RC_NOCACHE,
900                 .pc_xdrressize = ST+pAT+1,
901                 .pc_name = "ACCESS",
902         },
903         [NFS3PROC_READLINK] = {
904                 .pc_func = nfsd3_proc_readlink,
905                 .pc_decode = nfs3svc_decode_fhandleargs,
906                 .pc_encode = nfs3svc_encode_readlinkres,
907                 .pc_release = nfs3svc_release_fhandle,
908                 .pc_argsize = sizeof(struct nfsd_fhandle),
909                 .pc_argzero = sizeof(struct nfsd_fhandle),
910                 .pc_ressize = sizeof(struct nfsd3_readlinkres),
911                 .pc_cachetype = RC_NOCACHE,
912                 .pc_xdrressize = ST+pAT+1+NFS3_MAXPATHLEN/4,
913                 .pc_name = "READLINK",
914         },
915         [NFS3PROC_READ] = {
916                 .pc_func = nfsd3_proc_read,
917                 .pc_decode = nfs3svc_decode_readargs,
918                 .pc_encode = nfs3svc_encode_readres,
919                 .pc_release = nfs3svc_release_fhandle,
920                 .pc_argsize = sizeof(struct nfsd3_readargs),
921                 .pc_argzero = sizeof(struct nfsd3_readargs),
922                 .pc_ressize = sizeof(struct nfsd3_readres),
923                 .pc_cachetype = RC_NOCACHE,
924                 .pc_xdrressize = ST+pAT+4+NFSSVC_MAXBLKSIZE/4,
925                 .pc_name = "READ",
926         },
927         [NFS3PROC_WRITE] = {
928                 .pc_func = nfsd3_proc_write,
929                 .pc_decode = nfs3svc_decode_writeargs,
930                 .pc_encode = nfs3svc_encode_writeres,
931                 .pc_release = nfs3svc_release_fhandle,
932                 .pc_argsize = sizeof(struct nfsd3_writeargs),
933                 .pc_argzero = sizeof(struct nfsd3_writeargs),
934                 .pc_ressize = sizeof(struct nfsd3_writeres),
935                 .pc_cachetype = RC_REPLBUFF,
936                 .pc_xdrressize = ST+WC+4,
937                 .pc_name = "WRITE",
938         },
939         [NFS3PROC_CREATE] = {
940                 .pc_func = nfsd3_proc_create,
941                 .pc_decode = nfs3svc_decode_createargs,
942                 .pc_encode = nfs3svc_encode_createres,
943                 .pc_release = nfs3svc_release_fhandle2,
944                 .pc_argsize = sizeof(struct nfsd3_createargs),
945                 .pc_argzero = sizeof(struct nfsd3_createargs),
946                 .pc_ressize = sizeof(struct nfsd3_createres),
947                 .pc_cachetype = RC_REPLBUFF,
948                 .pc_xdrressize = ST+(1+FH+pAT)+WC,
949                 .pc_name = "CREATE",
950         },
951         [NFS3PROC_MKDIR] = {
952                 .pc_func = nfsd3_proc_mkdir,
953                 .pc_decode = nfs3svc_decode_mkdirargs,
954                 .pc_encode = nfs3svc_encode_createres,
955                 .pc_release = nfs3svc_release_fhandle2,
956                 .pc_argsize = sizeof(struct nfsd3_mkdirargs),
957                 .pc_argzero = sizeof(struct nfsd3_mkdirargs),
958                 .pc_ressize = sizeof(struct nfsd3_createres),
959                 .pc_cachetype = RC_REPLBUFF,
960                 .pc_xdrressize = ST+(1+FH+pAT)+WC,
961                 .pc_name = "MKDIR",
962         },
963         [NFS3PROC_SYMLINK] = {
964                 .pc_func = nfsd3_proc_symlink,
965                 .pc_decode = nfs3svc_decode_symlinkargs,
966                 .pc_encode = nfs3svc_encode_createres,
967                 .pc_release = nfs3svc_release_fhandle2,
968                 .pc_argsize = sizeof(struct nfsd3_symlinkargs),
969                 .pc_argzero = sizeof(struct nfsd3_symlinkargs),
970                 .pc_ressize = sizeof(struct nfsd3_createres),
971                 .pc_cachetype = RC_REPLBUFF,
972                 .pc_xdrressize = ST+(1+FH+pAT)+WC,
973                 .pc_name = "SYMLINK",
974         },
975         [NFS3PROC_MKNOD] = {
976                 .pc_func = nfsd3_proc_mknod,
977                 .pc_decode = nfs3svc_decode_mknodargs,
978                 .pc_encode = nfs3svc_encode_createres,
979                 .pc_release = nfs3svc_release_fhandle2,
980                 .pc_argsize = sizeof(struct nfsd3_mknodargs),
981                 .pc_argzero = sizeof(struct nfsd3_mknodargs),
982                 .pc_ressize = sizeof(struct nfsd3_createres),
983                 .pc_cachetype = RC_REPLBUFF,
984                 .pc_xdrressize = ST+(1+FH+pAT)+WC,
985                 .pc_name = "MKNOD",
986         },
987         [NFS3PROC_REMOVE] = {
988                 .pc_func = nfsd3_proc_remove,
989                 .pc_decode = nfs3svc_decode_diropargs,
990                 .pc_encode = nfs3svc_encode_wccstatres,
991                 .pc_release = nfs3svc_release_fhandle,
992                 .pc_argsize = sizeof(struct nfsd3_diropargs),
993                 .pc_argzero = sizeof(struct nfsd3_diropargs),
994                 .pc_ressize = sizeof(struct nfsd3_wccstatres),
995                 .pc_cachetype = RC_REPLBUFF,
996                 .pc_xdrressize = ST+WC,
997                 .pc_name = "REMOVE",
998         },
999         [NFS3PROC_RMDIR] = {
1000                 .pc_func = nfsd3_proc_rmdir,
1001                 .pc_decode = nfs3svc_decode_diropargs,
1002                 .pc_encode = nfs3svc_encode_wccstatres,
1003                 .pc_release = nfs3svc_release_fhandle,
1004                 .pc_argsize = sizeof(struct nfsd3_diropargs),
1005                 .pc_argzero = sizeof(struct nfsd3_diropargs),
1006                 .pc_ressize = sizeof(struct nfsd3_wccstatres),
1007                 .pc_cachetype = RC_REPLBUFF,
1008                 .pc_xdrressize = ST+WC,
1009                 .pc_name = "RMDIR",
1010         },
1011         [NFS3PROC_RENAME] = {
1012                 .pc_func = nfsd3_proc_rename,
1013                 .pc_decode = nfs3svc_decode_renameargs,
1014                 .pc_encode = nfs3svc_encode_renameres,
1015                 .pc_release = nfs3svc_release_fhandle2,
1016                 .pc_argsize = sizeof(struct nfsd3_renameargs),
1017                 .pc_argzero = sizeof(struct nfsd3_renameargs),
1018                 .pc_ressize = sizeof(struct nfsd3_renameres),
1019                 .pc_cachetype = RC_REPLBUFF,
1020                 .pc_xdrressize = ST+WC+WC,
1021                 .pc_name = "RENAME",
1022         },
1023         [NFS3PROC_LINK] = {
1024                 .pc_func = nfsd3_proc_link,
1025                 .pc_decode = nfs3svc_decode_linkargs,
1026                 .pc_encode = nfs3svc_encode_linkres,
1027                 .pc_release = nfs3svc_release_fhandle2,
1028                 .pc_argsize = sizeof(struct nfsd3_linkargs),
1029                 .pc_argzero = sizeof(struct nfsd3_linkargs),
1030                 .pc_ressize = sizeof(struct nfsd3_linkres),
1031                 .pc_cachetype = RC_REPLBUFF,
1032                 .pc_xdrressize = ST+pAT+WC,
1033                 .pc_name = "LINK",
1034         },
1035         [NFS3PROC_READDIR] = {
1036                 .pc_func = nfsd3_proc_readdir,
1037                 .pc_decode = nfs3svc_decode_readdirargs,
1038                 .pc_encode = nfs3svc_encode_readdirres,
1039                 .pc_release = nfs3svc_release_fhandle,
1040                 .pc_argsize = sizeof(struct nfsd3_readdirargs),
1041                 .pc_argzero = sizeof(struct nfsd3_readdirargs),
1042                 .pc_ressize = sizeof(struct nfsd3_readdirres),
1043                 .pc_cachetype = RC_NOCACHE,
1044                 .pc_name = "READDIR",
1045         },
1046         [NFS3PROC_READDIRPLUS] = {
1047                 .pc_func = nfsd3_proc_readdirplus,
1048                 .pc_decode = nfs3svc_decode_readdirplusargs,
1049                 .pc_encode = nfs3svc_encode_readdirres,
1050                 .pc_release = nfs3svc_release_fhandle,
1051                 .pc_argsize = sizeof(struct nfsd3_readdirplusargs),
1052                 .pc_argzero = sizeof(struct nfsd3_readdirplusargs),
1053                 .pc_ressize = sizeof(struct nfsd3_readdirres),
1054                 .pc_cachetype = RC_NOCACHE,
1055                 .pc_name = "READDIRPLUS",
1056         },
1057         [NFS3PROC_FSSTAT] = {
1058                 .pc_func = nfsd3_proc_fsstat,
1059                 .pc_decode = nfs3svc_decode_fhandleargs,
1060                 .pc_encode = nfs3svc_encode_fsstatres,
1061                 .pc_argsize = sizeof(struct nfsd3_fhandleargs),
1062                 .pc_argzero = sizeof(struct nfsd3_fhandleargs),
1063                 .pc_ressize = sizeof(struct nfsd3_fsstatres),
1064                 .pc_cachetype = RC_NOCACHE,
1065                 .pc_xdrressize = ST+pAT+2*6+1,
1066                 .pc_name = "FSSTAT",
1067         },
1068         [NFS3PROC_FSINFO] = {
1069                 .pc_func = nfsd3_proc_fsinfo,
1070                 .pc_decode = nfs3svc_decode_fhandleargs,
1071                 .pc_encode = nfs3svc_encode_fsinfores,
1072                 .pc_argsize = sizeof(struct nfsd3_fhandleargs),
1073                 .pc_argzero = sizeof(struct nfsd3_fhandleargs),
1074                 .pc_ressize = sizeof(struct nfsd3_fsinfores),
1075                 .pc_cachetype = RC_NOCACHE,
1076                 .pc_xdrressize = ST+pAT+12,
1077                 .pc_name = "FSINFO",
1078         },
1079         [NFS3PROC_PATHCONF] = {
1080                 .pc_func = nfsd3_proc_pathconf,
1081                 .pc_decode = nfs3svc_decode_fhandleargs,
1082                 .pc_encode = nfs3svc_encode_pathconfres,
1083                 .pc_argsize = sizeof(struct nfsd3_fhandleargs),
1084                 .pc_argzero = sizeof(struct nfsd3_fhandleargs),
1085                 .pc_ressize = sizeof(struct nfsd3_pathconfres),
1086                 .pc_cachetype = RC_NOCACHE,
1087                 .pc_xdrressize = ST+pAT+6,
1088                 .pc_name = "PATHCONF",
1089         },
1090         [NFS3PROC_COMMIT] = {
1091                 .pc_func = nfsd3_proc_commit,
1092                 .pc_decode = nfs3svc_decode_commitargs,
1093                 .pc_encode = nfs3svc_encode_commitres,
1094                 .pc_release = nfs3svc_release_fhandle,
1095                 .pc_argsize = sizeof(struct nfsd3_commitargs),
1096                 .pc_argzero = sizeof(struct nfsd3_commitargs),
1097                 .pc_ressize = sizeof(struct nfsd3_commitres),
1098                 .pc_cachetype = RC_NOCACHE,
1099                 .pc_xdrressize = ST+WC+2,
1100                 .pc_name = "COMMIT",
1101         },
1102 };
1103
1104 static DEFINE_PER_CPU_ALIGNED(unsigned long,
1105                               nfsd_count3[ARRAY_SIZE(nfsd_procedures3)]);
1106 const struct svc_version nfsd_version3 = {
1107         .vs_vers        = 3,
1108         .vs_nproc       = ARRAY_SIZE(nfsd_procedures3),
1109         .vs_proc        = nfsd_procedures3,
1110         .vs_dispatch    = nfsd_dispatch,
1111         .vs_count       = nfsd_count3,
1112         .vs_xdrsize     = NFS3_SVC_XDRSIZE,
1113 };