blob: 86d438fbd576d26a3996a50ad128995cff8cd484 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Process version 2 NFS requests.
4 *
5 * Copyright (C) 1995-1997 Olaf Kirch <okir@monad.swb.de>
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Boaz Harrosh9a74af22009-12-03 20:30:56 +020010#include "cache.h"
11#include "xdr.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050012#include "vfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#define NFSDDBG_FACILITY NFSDDBG_PROC
15
Al Viro7111c662006-10-19 23:28:45 -070016static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +020017nfsd_proc_null(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018{
Chuck Levercc028a12020-10-02 15:52:44 -040019 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020}
21
22/*
23 * Get a file's attributes
24 * N.B. After this call resp->fh needs an fh_put
25 */
Al Viro7111c662006-10-19 23:28:45 -070026static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +020027nfsd_proc_getattr(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Christoph Hellwiga6beb732017-05-08 17:35:49 +020029 struct nfsd_fhandle *argp = rqstp->rq_argp;
30 struct nfsd_attrstat *resp = rqstp->rq_resp;
Chuck Leverf0af2212020-10-01 18:59:49 -040031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 dprintk("nfsd: GETATTR %s\n", SVCFH_fmt(&argp->fh));
33
34 fh_copy(&resp->fh, &argp->fh);
Chuck Leverf0af2212020-10-01 18:59:49 -040035 resp->status = fh_verify(rqstp, &resp->fh, 0,
36 NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
37 if (resp->status != nfs_ok)
38 goto out;
39 resp->status = fh_getattr(&resp->fh, &resp->stat);
40out:
Chuck Levercc028a12020-10-02 15:52:44 -040041 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
44/*
45 * Set a file's attributes
46 * N.B. After this call resp->fh needs an fh_put
47 */
Al Viro7111c662006-10-19 23:28:45 -070048static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +020049nfsd_proc_setattr(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Christoph Hellwiga6beb732017-05-08 17:35:49 +020051 struct nfsd_sattrargs *argp = rqstp->rq_argp;
52 struct nfsd_attrstat *resp = rqstp->rq_resp;
Andreas Gruenbachercc265082015-05-09 00:37:57 +020053 struct iattr *iap = &argp->attrs;
54 struct svc_fh *fhp;
Andreas Gruenbachercc265082015-05-09 00:37:57 +020055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 dprintk("nfsd: SETATTR %s, valid=%x, size=%ld\n",
57 SVCFH_fmt(&argp->fh),
58 argp->attrs.ia_valid, (long) argp->attrs.ia_size);
59
Andreas Gruenbachercc265082015-05-09 00:37:57 +020060 fhp = fh_copy(&resp->fh, &argp->fh);
61
62 /*
63 * NFSv2 does not differentiate between "set-[ac]time-to-now"
64 * which only requires access, and "set-[ac]time-to-X" which
65 * requires ownership.
66 * So if it looks like it might be "set both to the same time which
Jan Kara31051c82016-05-26 16:55:18 +020067 * is close to now", and if setattr_prepare fails, then we
Andreas Gruenbachercc265082015-05-09 00:37:57 +020068 * convert to "set to now" instead of "set to explicit time"
69 *
Jan Kara31051c82016-05-26 16:55:18 +020070 * We only call setattr_prepare as the last test as technically
Andreas Gruenbachercc265082015-05-09 00:37:57 +020071 * it is not an interface that we should be using.
72 */
73#define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
74#define MAX_TOUCH_TIME_ERROR (30*60)
75 if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
76 iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
77 /*
78 * Looks probable.
79 *
80 * Now just make sure time is in the right ballpark.
81 * Solaris, at least, doesn't seem to care what the time
82 * request is. We require it be within 30 minutes of now.
83 */
Arnd Bergmannb6356d42019-11-03 18:06:52 +010084 time64_t delta = iap->ia_atime.tv_sec - ktime_get_real_seconds();
Andreas Gruenbachercc265082015-05-09 00:37:57 +020085
Chuck Leverf0af2212020-10-01 18:59:49 -040086 resp->status = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
87 if (resp->status != nfs_ok)
Chuck Levercc028a12020-10-02 15:52:44 -040088 goto out;
Andreas Gruenbachercc265082015-05-09 00:37:57 +020089
90 if (delta < 0)
91 delta = -delta;
92 if (delta < MAX_TOUCH_TIME_ERROR &&
Christian Brauner2f221d62021-01-21 14:19:26 +010093 setattr_prepare(&init_user_ns, fhp->fh_dentry, iap) != 0) {
Andreas Gruenbachercc265082015-05-09 00:37:57 +020094 /*
95 * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
96 * This will cause notify_change to set these times
97 * to "now"
98 */
99 iap->ia_valid &= ~BOTH_TIME_SET;
100 }
101 }
102
Chuck Leverf0af2212020-10-01 18:59:49 -0400103 resp->status = nfsd_setattr(rqstp, fhp, iap, 0, (time64_t)0);
104 if (resp->status != nfs_ok)
105 goto out;
106
107 resp->status = fh_getattr(&resp->fh, &resp->stat);
108out:
Chuck Levercc028a12020-10-02 15:52:44 -0400109 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Chuck Lever6b3dccd2020-10-01 18:58:56 -0400112/* Obsolete, replaced by MNTPROC_MNT. */
113static __be32
114nfsd_proc_root(struct svc_rqst *rqstp)
115{
Chuck Levercc028a12020-10-02 15:52:44 -0400116 return rpc_success;
Chuck Lever6b3dccd2020-10-01 18:58:56 -0400117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/*
120 * Look up a path name component
121 * Note: the dentry in the resp->fh may be negative if the file
122 * doesn't exist yet.
123 * N.B. After this call resp->fh needs an fh_put
124 */
Al Viro7111c662006-10-19 23:28:45 -0700125static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200126nfsd_proc_lookup(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200128 struct nfsd_diropargs *argp = rqstp->rq_argp;
129 struct nfsd_diropres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 dprintk("nfsd: LOOKUP %s %.*s\n",
132 SVCFH_fmt(&argp->fh), argp->len, argp->name);
133
134 fh_init(&resp->fh, NFS_FHSIZE);
Chuck Leverf0af2212020-10-01 18:59:49 -0400135 resp->status = nfsd_lookup(rqstp, &argp->fh, argp->name, argp->len,
136 &resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 fh_put(&argp->fh);
Chuck Leverf0af2212020-10-01 18:59:49 -0400138 if (resp->status != nfs_ok)
139 goto out;
140
141 resp->status = fh_getattr(&resp->fh, &resp->stat);
142out:
Chuck Levercc028a12020-10-02 15:52:44 -0400143 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146/*
147 * Read a symlink.
148 */
Al Viro7111c662006-10-19 23:28:45 -0700149static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200150nfsd_proc_readlink(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Chuck Lever1fcbd1c2020-10-21 12:21:25 -0400152 struct nfsd_fhandle *argp = rqstp->rq_argp;
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200153 struct nfsd_readlinkres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 dprintk("nfsd: READLINK %s\n", SVCFH_fmt(&argp->fh));
156
157 /* Read the symlink. */
158 resp->len = NFS_MAXPATHLEN;
Chuck Leverd9014b02020-10-23 15:41:09 -0400159 resp->page = *(rqstp->rq_next_page++);
160 resp->status = nfsd_readlink(rqstp, &argp->fh,
161 page_address(resp->page), &resp->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 fh_put(&argp->fh);
Chuck Levercc028a12020-10-02 15:52:44 -0400164 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167/*
168 * Read a portion of a file.
169 * N.B. After this call resp->fh needs an fh_put
170 */
Al Viro7111c662006-10-19 23:28:45 -0700171static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200172nfsd_proc_read(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200174 struct nfsd_readargs *argp = rqstp->rq_argp;
175 struct nfsd_readres *resp = rqstp->rq_resp;
Chuck Lever8c293ef2020-10-21 12:15:51 -0400176 unsigned int len;
Trond Myklebust83a63072019-08-26 13:03:11 -0400177 u32 eof;
Chuck Lever8c293ef2020-10-21 12:15:51 -0400178 int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 dprintk("nfsd: READ %s %d bytes at %d\n",
181 SVCFH_fmt(&argp->fh),
182 argp->count, argp->offset);
183
Chuck Lever8c293ef2020-10-21 12:15:51 -0400184 argp->count = min_t(u32, argp->count, NFSSVC_MAXBLKSIZE_V2);
185
186 v = 0;
187 len = argp->count;
Chuck Levera6f8d9d2020-10-23 16:40:11 -0400188 resp->pages = rqstp->rq_next_page;
Chuck Lever8c293ef2020-10-21 12:15:51 -0400189 while (len > 0) {
190 struct page *page = *(rqstp->rq_next_page++);
191
192 rqstp->rq_vec[v].iov_base = page_address(page);
193 rqstp->rq_vec[v].iov_len = min_t(unsigned int, len, PAGE_SIZE);
194 len -= rqstp->rq_vec[v].iov_len;
195 v++;
196 }
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* Obtain buffer pointer for payload. 19 is 1 word for
199 * status, 17 words for fattr, and 1 word for the byte count.
200 */
Jeff Laytoncd1230122007-05-09 02:34:50 -0700201 svc_reserve_auth(rqstp, (19<<2) + argp->count + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 resp->count = argp->count;
Chuck Lever8c293ef2020-10-21 12:15:51 -0400204 fh_copy(&resp->fh, &argp->fh);
205 resp->status = nfsd_read(rqstp, &resp->fh, argp->offset,
206 rqstp->rq_vec, v, &resp->count, &eof);
Chuck Levercc028a12020-10-02 15:52:44 -0400207 if (resp->status == nfs_ok)
208 resp->status = fh_getattr(&resp->fh, &resp->stat);
209 else if (resp->status == nfserr_jukebox)
210 return rpc_drop_reply;
211 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Chuck Lever6b3dccd2020-10-01 18:58:56 -0400214/* Reserved */
215static __be32
216nfsd_proc_writecache(struct svc_rqst *rqstp)
217{
Chuck Levercc028a12020-10-02 15:52:44 -0400218 return rpc_success;
Chuck Lever6b3dccd2020-10-01 18:58:56 -0400219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/*
222 * Write data to a file
223 * N.B. After this call resp->fh needs an fh_put
224 */
Al Viro7111c662006-10-19 23:28:45 -0700225static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200226nfsd_proc_write(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200228 struct nfsd_writeargs *argp = rqstp->rq_argp;
229 struct nfsd_attrstat *resp = rqstp->rq_resp;
David Shaw31dec252009-03-05 20:16:14 -0500230 unsigned long cnt = argp->len;
Chuck Lever8154ef22018-03-27 10:54:07 -0400231 unsigned int nvecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 dprintk("nfsd: WRITE %s %d bytes at %d\n",
234 SVCFH_fmt(&argp->fh),
235 argp->len, argp->offset);
236
Chuck Lever3fd95572018-07-27 11:19:05 -0400237 nvecs = svc_fill_write_vector(rqstp, rqstp->rq_arg.pages,
238 &argp->first, cnt);
Chuck Leverf0af2212020-10-01 18:59:49 -0400239 if (!nvecs) {
240 resp->status = nfserr_io;
241 goto out;
242 }
243
244 resp->status = nfsd_write(rqstp, fh_copy(&resp->fh, &argp->fh),
245 argp->offset, rqstp->rq_vec, nvecs,
246 &cnt, NFS_DATA_SYNC, NULL);
Chuck Levercc028a12020-10-02 15:52:44 -0400247 if (resp->status == nfs_ok)
248 resp->status = fh_getattr(&resp->fh, &resp->stat);
249 else if (resp->status == nfserr_jukebox)
250 return rpc_drop_reply;
Chuck Leverf0af2212020-10-01 18:59:49 -0400251out:
Chuck Levercc028a12020-10-02 15:52:44 -0400252 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255/*
256 * CREATE processing is complicated. The keyword here is `overloaded.'
257 * The parent directory is kept locked between the check for existence
258 * and the actual create() call in compliance with VFS protocols.
259 * N.B. After this call _both_ argp->fh and resp->fh need an fh_put
260 */
Al Viro7111c662006-10-19 23:28:45 -0700261static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200262nfsd_proc_create(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200264 struct nfsd_createargs *argp = rqstp->rq_argp;
265 struct nfsd_diropres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 svc_fh *dirfhp = &argp->fh;
267 svc_fh *newfhp = &resp->fh;
268 struct iattr *attr = &argp->attrs;
269 struct inode *inode;
270 struct dentry *dchild;
Al Viroc4d987b2006-10-19 23:29:00 -0700271 int type, mode;
Jan Kara4a55c102012-06-12 16:20:33 +0200272 int hosterr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 dev_t rdev = 0, wanted = new_decode_dev(attr->ia_size);
274
275 dprintk("nfsd: CREATE %s %.*s\n",
276 SVCFH_fmt(dirfhp), argp->len, argp->name);
277
278 /* First verify the parent file handle */
Chuck Leverf0af2212020-10-01 18:59:49 -0400279 resp->status = fh_verify(rqstp, dirfhp, S_IFDIR, NFSD_MAY_EXEC);
280 if (resp->status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 goto done; /* must fh_put dirfhp even on error */
282
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200283 /* Check for NFSD_MAY_WRITE in nfsd_create if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Chuck Leverf0af2212020-10-01 18:59:49 -0400285 resp->status = nfserr_exist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (isdotent(argp->name, argp->len))
287 goto done;
Jan Kara4a55c102012-06-12 16:20:33 +0200288 hosterr = fh_want_write(dirfhp);
289 if (hosterr) {
Chuck Leverf0af2212020-10-01 18:59:49 -0400290 resp->status = nfserrno(hosterr);
Jan Kara4a55c102012-06-12 16:20:33 +0200291 goto done;
292 }
293
NeilBrown7ed94292006-10-04 02:15:43 -0700294 fh_lock_nested(dirfhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 dchild = lookup_one_len(argp->name, dirfhp->fh_dentry, argp->len);
296 if (IS_ERR(dchild)) {
Chuck Leverf0af2212020-10-01 18:59:49 -0400297 resp->status = nfserrno(PTR_ERR(dchild));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 goto out_unlock;
299 }
300 fh_init(newfhp, NFS_FHSIZE);
Chuck Leverf0af2212020-10-01 18:59:49 -0400301 resp->status = fh_compose(newfhp, dirfhp->fh_export, dchild, dirfhp);
302 if (!resp->status && d_really_is_negative(dchild))
303 resp->status = nfserr_noent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 dput(dchild);
Chuck Leverf0af2212020-10-01 18:59:49 -0400305 if (resp->status) {
306 if (resp->status != nfserr_noent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 goto out_unlock;
308 /*
309 * If the new file handle wasn't verified, we can't tell
310 * whether the file exists or not. Time to bail ...
311 */
Chuck Leverf0af2212020-10-01 18:59:49 -0400312 resp->status = nfserr_acces;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (!newfhp->fh_dentry) {
314 printk(KERN_WARNING
315 "nfsd_proc_create: file handle not verified\n");
316 goto out_unlock;
317 }
318 }
319
David Howells2b0143b2015-03-17 22:25:59 +0000320 inode = d_inode(newfhp->fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 /* Unfudge the mode bits */
323 if (attr->ia_valid & ATTR_MODE) {
324 type = attr->ia_mode & S_IFMT;
325 mode = attr->ia_mode & ~S_IFMT;
326 if (!type) {
327 /* no type, so if target exists, assume same as that,
328 * else assume a file */
329 if (inode) {
330 type = inode->i_mode & S_IFMT;
331 switch(type) {
332 case S_IFCHR:
333 case S_IFBLK:
334 /* reserve rdev for later checking */
335 rdev = inode->i_rdev;
336 attr->ia_valid |= ATTR_SIZE;
337
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500338 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 case S_IFIFO:
340 /* this is probably a permission check..
341 * at least IRIX implements perm checking on
342 * echo thing > device-special-file-or-pipe
343 * by doing a CREATE with type==0
344 */
Chuck Leverf0af2212020-10-01 18:59:49 -0400345 resp->status = nfsd_permission(rqstp,
J. Bruce Fields0ec757d2007-07-17 04:04:48 -0700346 newfhp->fh_export,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 newfhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200348 NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS);
Chuck Leverf0af2212020-10-01 18:59:49 -0400349 if (resp->status && resp->status != nfserr_rofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 goto out_unlock;
351 }
352 } else
353 type = S_IFREG;
354 }
355 } else if (inode) {
356 type = inode->i_mode & S_IFMT;
357 mode = inode->i_mode & ~S_IFMT;
358 } else {
359 type = S_IFREG;
360 mode = 0; /* ??? */
361 }
362
363 attr->ia_valid |= ATTR_MODE;
364 attr->ia_mode = mode;
365
366 /* Special treatment for non-regular files according to the
367 * gospel of sun micro
368 */
369 if (type != S_IFREG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (type != S_IFBLK && type != S_IFCHR) {
371 rdev = 0;
372 } else if (type == S_IFCHR && !(attr->ia_valid & ATTR_SIZE)) {
373 /* If you think you've seen the worst, grok this. */
374 type = S_IFIFO;
375 } else {
376 /* Okay, char or block special */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (!rdev)
378 rdev = wanted;
379 }
380
381 /* we've used the SIZE information, so discard it */
382 attr->ia_valid &= ~ATTR_SIZE;
383
384 /* Make sure the type and device matches */
Chuck Leverf0af2212020-10-01 18:59:49 -0400385 resp->status = nfserr_exist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (inode && type != (inode->i_mode & S_IFMT))
387 goto out_unlock;
388 }
389
Chuck Leverf0af2212020-10-01 18:59:49 -0400390 resp->status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (!inode) {
392 /* File doesn't exist. Create it and set attrs */
Chuck Leverf0af2212020-10-01 18:59:49 -0400393 resp->status = nfsd_create_locked(rqstp, dirfhp, argp->name,
394 argp->len, attr, type, rdev,
395 newfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 } else if (type == S_IFREG) {
397 dprintk("nfsd: existing %s, valid=%x, size=%ld\n",
398 argp->name, attr->ia_valid, (long) attr->ia_size);
399 /* File already exists. We ignore all attributes except
400 * size, so that creat() behaves exactly like
401 * open(..., O_CREAT|O_TRUNC|O_WRONLY).
402 */
403 attr->ia_valid &= ATTR_SIZE;
404 if (attr->ia_valid)
Chuck Leverf0af2212020-10-01 18:59:49 -0400405 resp->status = nfsd_setattr(rqstp, newfhp, attr, 0,
406 (time64_t)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408
409out_unlock:
410 /* We don't really need to unlock, as fh_put does it. */
411 fh_unlock(dirfhp);
Jan Kara4a55c102012-06-12 16:20:33 +0200412 fh_drop_write(dirfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413done:
414 fh_put(dirfhp);
Chuck Leverf0af2212020-10-01 18:59:49 -0400415 if (resp->status != nfs_ok)
416 goto out;
417 resp->status = fh_getattr(&resp->fh, &resp->stat);
418out:
Chuck Levercc028a12020-10-02 15:52:44 -0400419 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Al Viro7111c662006-10-19 23:28:45 -0700422static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200423nfsd_proc_remove(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200425 struct nfsd_diropargs *argp = rqstp->rq_argp;
Chuck Levercc028a12020-10-02 15:52:44 -0400426 struct nfsd_stat *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 dprintk("nfsd: REMOVE %s %.*s\n", SVCFH_fmt(&argp->fh),
429 argp->len, argp->name);
430
431 /* Unlink. -SIFDIR means file must not be a directory */
Chuck Levercc028a12020-10-02 15:52:44 -0400432 resp->status = nfsd_unlink(rqstp, &argp->fh, -S_IFDIR,
433 argp->name, argp->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 fh_put(&argp->fh);
Chuck Levercc028a12020-10-02 15:52:44 -0400435 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
Al Viro7111c662006-10-19 23:28:45 -0700438static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200439nfsd_proc_rename(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200441 struct nfsd_renameargs *argp = rqstp->rq_argp;
Chuck Levercc028a12020-10-02 15:52:44 -0400442 struct nfsd_stat *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 dprintk("nfsd: RENAME %s %.*s -> \n",
445 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname);
446 dprintk("nfsd: -> %s %.*s\n",
447 SVCFH_fmt(&argp->tfh), argp->tlen, argp->tname);
448
Chuck Levercc028a12020-10-02 15:52:44 -0400449 resp->status = nfsd_rename(rqstp, &argp->ffh, argp->fname, argp->flen,
450 &argp->tfh, argp->tname, argp->tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 fh_put(&argp->ffh);
452 fh_put(&argp->tfh);
Chuck Levercc028a12020-10-02 15:52:44 -0400453 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Al Viro7111c662006-10-19 23:28:45 -0700456static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200457nfsd_proc_link(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200459 struct nfsd_linkargs *argp = rqstp->rq_argp;
Chuck Levercc028a12020-10-02 15:52:44 -0400460 struct nfsd_stat *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 dprintk("nfsd: LINK %s ->\n",
463 SVCFH_fmt(&argp->ffh));
464 dprintk("nfsd: %s %.*s\n",
465 SVCFH_fmt(&argp->tfh),
466 argp->tlen,
467 argp->tname);
468
Chuck Levercc028a12020-10-02 15:52:44 -0400469 resp->status = nfsd_link(rqstp, &argp->tfh, argp->tname, argp->tlen,
470 &argp->ffh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 fh_put(&argp->ffh);
472 fh_put(&argp->tfh);
Chuck Levercc028a12020-10-02 15:52:44 -0400473 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Al Viro7111c662006-10-19 23:28:45 -0700476static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200477nfsd_proc_symlink(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200479 struct nfsd_symlinkargs *argp = rqstp->rq_argp;
Chuck Levercc028a12020-10-02 15:52:44 -0400480 struct nfsd_stat *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 struct svc_fh newfh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Chuck Leverf0af2212020-10-01 18:59:49 -0400483 if (argp->tlen > NFS_MAXPATHLEN) {
Chuck Levercc028a12020-10-02 15:52:44 -0400484 resp->status = nfserr_nametoolong;
Chuck Leverf0af2212020-10-01 18:59:49 -0400485 goto out;
486 }
Chuck Lever38a70312018-03-27 10:54:21 -0400487
488 argp->tname = svc_fill_symlink_pathname(rqstp, &argp->first,
Chuck Lever11b4d662018-07-27 11:19:10 -0400489 page_address(rqstp->rq_arg.pages[0]),
Chuck Lever38a70312018-03-27 10:54:21 -0400490 argp->tlen);
Chuck Leverf0af2212020-10-01 18:59:49 -0400491 if (IS_ERR(argp->tname)) {
Chuck Levercc028a12020-10-02 15:52:44 -0400492 resp->status = nfserrno(PTR_ERR(argp->tname));
Chuck Leverf0af2212020-10-01 18:59:49 -0400493 goto out;
494 }
Chuck Lever38a70312018-03-27 10:54:21 -0400495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 dprintk("nfsd: SYMLINK %s %.*s -> %.*s\n",
497 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname,
498 argp->tlen, argp->tname);
499
500 fh_init(&newfh, NFS_FHSIZE);
Chuck Levercc028a12020-10-02 15:52:44 -0400501 resp->status = nfsd_symlink(rqstp, &argp->ffh, argp->fname, argp->flen,
502 argp->tname, &newfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Chuck Lever11b4d662018-07-27 11:19:10 -0400504 kfree(argp->tname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 fh_put(&argp->ffh);
506 fh_put(&newfh);
Chuck Leverf0af2212020-10-01 18:59:49 -0400507out:
Chuck Levercc028a12020-10-02 15:52:44 -0400508 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511/*
512 * Make directory. This operation is not idempotent.
513 * N.B. After this call resp->fh needs an fh_put
514 */
Al Viro7111c662006-10-19 23:28:45 -0700515static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200516nfsd_proc_mkdir(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200518 struct nfsd_createargs *argp = rqstp->rq_argp;
519 struct nfsd_diropres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 dprintk("nfsd: MKDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
522
523 if (resp->fh.fh_dentry) {
524 printk(KERN_WARNING
525 "nfsd_proc_mkdir: response already verified??\n");
526 }
527
528 argp->attrs.ia_valid &= ~ATTR_SIZE;
529 fh_init(&resp->fh, NFS_FHSIZE);
Chuck Leverf0af2212020-10-01 18:59:49 -0400530 resp->status = nfsd_create(rqstp, &argp->fh, argp->name, argp->len,
531 &argp->attrs, S_IFDIR, 0, &resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 fh_put(&argp->fh);
Chuck Leverf0af2212020-10-01 18:59:49 -0400533 if (resp->status != nfs_ok)
534 goto out;
535
536 resp->status = fh_getattr(&resp->fh, &resp->stat);
537out:
Chuck Levercc028a12020-10-02 15:52:44 -0400538 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
541/*
542 * Remove a directory
543 */
Al Viro7111c662006-10-19 23:28:45 -0700544static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200545nfsd_proc_rmdir(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200547 struct nfsd_diropargs *argp = rqstp->rq_argp;
Chuck Levercc028a12020-10-02 15:52:44 -0400548 struct nfsd_stat *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 dprintk("nfsd: RMDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
551
Chuck Levercc028a12020-10-02 15:52:44 -0400552 resp->status = nfsd_unlink(rqstp, &argp->fh, S_IFDIR,
553 argp->name, argp->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 fh_put(&argp->fh);
Chuck Levercc028a12020-10-02 15:52:44 -0400555 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Chuck Lever788cd462020-11-13 17:03:49 -0500558static void nfsd_init_dirlist_pages(struct svc_rqst *rqstp,
559 struct nfsd_readdirres *resp,
560 int count)
561{
Chuck Leverf5dcccd2020-11-14 13:45:35 -0500562 struct xdr_buf *buf = &resp->dirlist;
563 struct xdr_stream *xdr = &resp->xdr;
564
Chuck Lever788cd462020-11-13 17:03:49 -0500565 count = min_t(u32, count, PAGE_SIZE);
566
Chuck Leverf5dcccd2020-11-14 13:45:35 -0500567 memset(buf, 0, sizeof(*buf));
Chuck Lever788cd462020-11-13 17:03:49 -0500568
Chuck Leverf5dcccd2020-11-14 13:45:35 -0500569 /* Reserve room for the NULL ptr & eof flag (-2 words) */
570 buf->buflen = count - sizeof(__be32) * 2;
571 buf->pages = rqstp->rq_next_page;
Chuck Lever788cd462020-11-13 17:03:49 -0500572 rqstp->rq_next_page++;
Chuck Leverf5dcccd2020-11-14 13:45:35 -0500573
574 /* This is xdr_init_encode(), but it assumes that
575 * the head kvec has already been consumed. */
576 xdr_set_scratch_buffer(xdr, NULL, 0);
577 xdr->buf = buf;
578 xdr->page_ptr = buf->pages;
579 xdr->iov = NULL;
580 xdr->p = page_address(*buf->pages);
581 xdr->end = xdr->p + (PAGE_SIZE >> 2);
582 xdr->rqst = NULL;
Chuck Lever788cd462020-11-13 17:03:49 -0500583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/*
586 * Read a portion of a directory.
587 */
Al Viro7111c662006-10-19 23:28:45 -0700588static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200589nfsd_proc_readdir(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200591 struct nfsd_readdirargs *argp = rqstp->rq_argp;
592 struct nfsd_readdirres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 loff_t offset;
594
595 dprintk("nfsd: READDIR %s %d bytes at %d\n",
596 SVCFH_fmt(&argp->fh),
597 argp->count, argp->cookie);
598
Chuck Lever788cd462020-11-13 17:03:49 -0500599 nfsd_init_dirlist_pages(rqstp, resp, argp->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 resp->common.err = nfs_ok;
Chuck Leverf5dcccd2020-11-14 13:45:35 -0500602 resp->cookie_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 offset = argp->cookie;
Chuck Leverf0af2212020-10-01 18:59:49 -0400604 resp->status = nfsd_readdir(rqstp, &argp->fh, &offset,
Chuck Leverf5dcccd2020-11-14 13:45:35 -0500605 &resp->common, nfs2svc_encode_entry);
Chuck Leverd52532002020-11-13 16:53:17 -0500606 nfssvc_encode_nfscookie(resp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 fh_put(&argp->fh);
Chuck Levercc028a12020-10-02 15:52:44 -0400609 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612/*
613 * Get file system info
614 */
Al Viro7111c662006-10-19 23:28:45 -0700615static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200616nfsd_proc_statfs(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200618 struct nfsd_fhandle *argp = rqstp->rq_argp;
619 struct nfsd_statfsres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 dprintk("nfsd: STATFS %s\n", SVCFH_fmt(&argp->fh));
622
Chuck Leverf0af2212020-10-01 18:59:49 -0400623 resp->status = nfsd_statfs(rqstp, &argp->fh, &resp->stats,
624 NFSD_MAY_BYPASS_GSS_ON_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 fh_put(&argp->fh);
Chuck Levercc028a12020-10-02 15:52:44 -0400626 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
629/*
630 * NFSv2 Server procedures.
631 * Only the results of non-idempotent operations are cached.
632 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634#define ST 1 /* status */
635#define FH 8 /* filehandle */
636#define AT 18 /* attributes */
637
Christoph Hellwig860bda22017-05-12 16:11:49 +0200638static const struct svc_procedure nfsd_procedures2[18] = {
Yu Zhiguob9081d92009-06-09 17:33:34 +0800639 [NFSPROC_NULL] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200640 .pc_func = nfsd_proc_null,
Chuck Lever788f7182020-11-05 14:48:29 -0500641 .pc_decode = nfssvc_decode_voidarg,
642 .pc_encode = nfssvc_encode_voidres,
643 .pc_argsize = sizeof(struct nfsd_voidargs),
644 .pc_ressize = sizeof(struct nfsd_voidres),
Yu Zhiguob9081d92009-06-09 17:33:34 +0800645 .pc_cachetype = RC_NOCACHE,
Chuck Levercc028a12020-10-02 15:52:44 -0400646 .pc_xdrressize = 0,
Chuck Lever2289e872020-09-17 17:22:49 -0400647 .pc_name = "NULL",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800648 },
649 [NFSPROC_GETATTR] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200650 .pc_func = nfsd_proc_getattr,
Chuck Leverebcd8e82020-10-21 12:14:23 -0400651 .pc_decode = nfssvc_decode_fhandleargs,
Chuck Lever92b54a42020-10-23 15:28:59 -0400652 .pc_encode = nfssvc_encode_attrstatres,
Chuck Lever1841b9b2020-10-01 18:59:44 -0400653 .pc_release = nfssvc_release_attrstat,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800654 .pc_argsize = sizeof(struct nfsd_fhandle),
655 .pc_ressize = sizeof(struct nfsd_attrstat),
656 .pc_cachetype = RC_NOCACHE,
657 .pc_xdrressize = ST+AT,
Chuck Lever2289e872020-09-17 17:22:49 -0400658 .pc_name = "GETATTR",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800659 },
660 [NFSPROC_SETATTR] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200661 .pc_func = nfsd_proc_setattr,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200662 .pc_decode = nfssvc_decode_sattrargs,
Chuck Lever92b54a42020-10-23 15:28:59 -0400663 .pc_encode = nfssvc_encode_attrstatres,
Chuck Lever1841b9b2020-10-01 18:59:44 -0400664 .pc_release = nfssvc_release_attrstat,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800665 .pc_argsize = sizeof(struct nfsd_sattrargs),
666 .pc_ressize = sizeof(struct nfsd_attrstat),
667 .pc_cachetype = RC_REPLBUFF,
668 .pc_xdrressize = ST+AT,
Chuck Lever2289e872020-09-17 17:22:49 -0400669 .pc_name = "SETATTR",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800670 },
671 [NFSPROC_ROOT] = {
Chuck Lever6b3dccd2020-10-01 18:58:56 -0400672 .pc_func = nfsd_proc_root,
Chuck Lever788f7182020-11-05 14:48:29 -0500673 .pc_decode = nfssvc_decode_voidarg,
674 .pc_encode = nfssvc_encode_voidres,
675 .pc_argsize = sizeof(struct nfsd_voidargs),
676 .pc_ressize = sizeof(struct nfsd_voidres),
Yu Zhiguob9081d92009-06-09 17:33:34 +0800677 .pc_cachetype = RC_NOCACHE,
Chuck Levercc028a12020-10-02 15:52:44 -0400678 .pc_xdrressize = 0,
Chuck Lever2289e872020-09-17 17:22:49 -0400679 .pc_name = "ROOT",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800680 },
681 [NFSPROC_LOOKUP] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200682 .pc_func = nfsd_proc_lookup,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200683 .pc_decode = nfssvc_decode_diropargs,
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200684 .pc_encode = nfssvc_encode_diropres,
Chuck Lever1841b9b2020-10-01 18:59:44 -0400685 .pc_release = nfssvc_release_diropres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800686 .pc_argsize = sizeof(struct nfsd_diropargs),
687 .pc_ressize = sizeof(struct nfsd_diropres),
688 .pc_cachetype = RC_NOCACHE,
689 .pc_xdrressize = ST+FH+AT,
Chuck Lever2289e872020-09-17 17:22:49 -0400690 .pc_name = "LOOKUP",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800691 },
692 [NFSPROC_READLINK] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200693 .pc_func = nfsd_proc_readlink,
Chuck Lever1fcbd1c2020-10-21 12:21:25 -0400694 .pc_decode = nfssvc_decode_fhandleargs,
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200695 .pc_encode = nfssvc_encode_readlinkres,
Chuck Lever1fcbd1c2020-10-21 12:21:25 -0400696 .pc_argsize = sizeof(struct nfsd_fhandle),
Yu Zhiguob9081d92009-06-09 17:33:34 +0800697 .pc_ressize = sizeof(struct nfsd_readlinkres),
698 .pc_cachetype = RC_NOCACHE,
699 .pc_xdrressize = ST+1+NFS_MAXPATHLEN/4,
Chuck Lever2289e872020-09-17 17:22:49 -0400700 .pc_name = "READLINK",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800701 },
702 [NFSPROC_READ] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200703 .pc_func = nfsd_proc_read,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200704 .pc_decode = nfssvc_decode_readargs,
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200705 .pc_encode = nfssvc_encode_readres,
Chuck Lever1841b9b2020-10-01 18:59:44 -0400706 .pc_release = nfssvc_release_readres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800707 .pc_argsize = sizeof(struct nfsd_readargs),
708 .pc_ressize = sizeof(struct nfsd_readres),
709 .pc_cachetype = RC_NOCACHE,
710 .pc_xdrressize = ST+AT+1+NFSSVC_MAXBLKSIZE_V2/4,
Chuck Lever2289e872020-09-17 17:22:49 -0400711 .pc_name = "READ",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800712 },
713 [NFSPROC_WRITECACHE] = {
Chuck Lever6b3dccd2020-10-01 18:58:56 -0400714 .pc_func = nfsd_proc_writecache,
Chuck Lever788f7182020-11-05 14:48:29 -0500715 .pc_decode = nfssvc_decode_voidarg,
716 .pc_encode = nfssvc_encode_voidres,
717 .pc_argsize = sizeof(struct nfsd_voidargs),
718 .pc_ressize = sizeof(struct nfsd_voidres),
Yu Zhiguob9081d92009-06-09 17:33:34 +0800719 .pc_cachetype = RC_NOCACHE,
Chuck Levercc028a12020-10-02 15:52:44 -0400720 .pc_xdrressize = 0,
Chuck Lever2289e872020-09-17 17:22:49 -0400721 .pc_name = "WRITECACHE",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800722 },
723 [NFSPROC_WRITE] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200724 .pc_func = nfsd_proc_write,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200725 .pc_decode = nfssvc_decode_writeargs,
Chuck Lever92b54a42020-10-23 15:28:59 -0400726 .pc_encode = nfssvc_encode_attrstatres,
Chuck Lever1841b9b2020-10-01 18:59:44 -0400727 .pc_release = nfssvc_release_attrstat,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800728 .pc_argsize = sizeof(struct nfsd_writeargs),
729 .pc_ressize = sizeof(struct nfsd_attrstat),
730 .pc_cachetype = RC_REPLBUFF,
731 .pc_xdrressize = ST+AT,
Chuck Lever2289e872020-09-17 17:22:49 -0400732 .pc_name = "WRITE",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800733 },
734 [NFSPROC_CREATE] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200735 .pc_func = nfsd_proc_create,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200736 .pc_decode = nfssvc_decode_createargs,
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200737 .pc_encode = nfssvc_encode_diropres,
Chuck Lever1841b9b2020-10-01 18:59:44 -0400738 .pc_release = nfssvc_release_diropres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800739 .pc_argsize = sizeof(struct nfsd_createargs),
740 .pc_ressize = sizeof(struct nfsd_diropres),
741 .pc_cachetype = RC_REPLBUFF,
742 .pc_xdrressize = ST+FH+AT,
Chuck Lever2289e872020-09-17 17:22:49 -0400743 .pc_name = "CREATE",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800744 },
745 [NFSPROC_REMOVE] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200746 .pc_func = nfsd_proc_remove,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200747 .pc_decode = nfssvc_decode_diropargs,
Chuck Levera887eae2020-10-23 11:08:02 -0400748 .pc_encode = nfssvc_encode_statres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800749 .pc_argsize = sizeof(struct nfsd_diropargs),
Chuck Levercc028a12020-10-02 15:52:44 -0400750 .pc_ressize = sizeof(struct nfsd_stat),
Yu Zhiguob9081d92009-06-09 17:33:34 +0800751 .pc_cachetype = RC_REPLSTAT,
752 .pc_xdrressize = ST,
Chuck Lever2289e872020-09-17 17:22:49 -0400753 .pc_name = "REMOVE",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800754 },
755 [NFSPROC_RENAME] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200756 .pc_func = nfsd_proc_rename,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200757 .pc_decode = nfssvc_decode_renameargs,
Chuck Levera887eae2020-10-23 11:08:02 -0400758 .pc_encode = nfssvc_encode_statres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800759 .pc_argsize = sizeof(struct nfsd_renameargs),
Chuck Levercc028a12020-10-02 15:52:44 -0400760 .pc_ressize = sizeof(struct nfsd_stat),
Yu Zhiguob9081d92009-06-09 17:33:34 +0800761 .pc_cachetype = RC_REPLSTAT,
762 .pc_xdrressize = ST,
Chuck Lever2289e872020-09-17 17:22:49 -0400763 .pc_name = "RENAME",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800764 },
765 [NFSPROC_LINK] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200766 .pc_func = nfsd_proc_link,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200767 .pc_decode = nfssvc_decode_linkargs,
Chuck Levera887eae2020-10-23 11:08:02 -0400768 .pc_encode = nfssvc_encode_statres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800769 .pc_argsize = sizeof(struct nfsd_linkargs),
Chuck Levercc028a12020-10-02 15:52:44 -0400770 .pc_ressize = sizeof(struct nfsd_stat),
Yu Zhiguob9081d92009-06-09 17:33:34 +0800771 .pc_cachetype = RC_REPLSTAT,
772 .pc_xdrressize = ST,
Chuck Lever2289e872020-09-17 17:22:49 -0400773 .pc_name = "LINK",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800774 },
775 [NFSPROC_SYMLINK] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200776 .pc_func = nfsd_proc_symlink,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200777 .pc_decode = nfssvc_decode_symlinkargs,
Chuck Levera887eae2020-10-23 11:08:02 -0400778 .pc_encode = nfssvc_encode_statres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800779 .pc_argsize = sizeof(struct nfsd_symlinkargs),
Chuck Levercc028a12020-10-02 15:52:44 -0400780 .pc_ressize = sizeof(struct nfsd_stat),
Yu Zhiguob9081d92009-06-09 17:33:34 +0800781 .pc_cachetype = RC_REPLSTAT,
782 .pc_xdrressize = ST,
Chuck Lever2289e872020-09-17 17:22:49 -0400783 .pc_name = "SYMLINK",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800784 },
785 [NFSPROC_MKDIR] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200786 .pc_func = nfsd_proc_mkdir,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200787 .pc_decode = nfssvc_decode_createargs,
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200788 .pc_encode = nfssvc_encode_diropres,
Chuck Lever1841b9b2020-10-01 18:59:44 -0400789 .pc_release = nfssvc_release_diropres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800790 .pc_argsize = sizeof(struct nfsd_createargs),
791 .pc_ressize = sizeof(struct nfsd_diropres),
792 .pc_cachetype = RC_REPLBUFF,
793 .pc_xdrressize = ST+FH+AT,
Chuck Lever2289e872020-09-17 17:22:49 -0400794 .pc_name = "MKDIR",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800795 },
796 [NFSPROC_RMDIR] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200797 .pc_func = nfsd_proc_rmdir,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200798 .pc_decode = nfssvc_decode_diropargs,
Chuck Levera887eae2020-10-23 11:08:02 -0400799 .pc_encode = nfssvc_encode_statres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800800 .pc_argsize = sizeof(struct nfsd_diropargs),
Chuck Levercc028a12020-10-02 15:52:44 -0400801 .pc_ressize = sizeof(struct nfsd_stat),
Yu Zhiguob9081d92009-06-09 17:33:34 +0800802 .pc_cachetype = RC_REPLSTAT,
803 .pc_xdrressize = ST,
Chuck Lever2289e872020-09-17 17:22:49 -0400804 .pc_name = "RMDIR",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800805 },
806 [NFSPROC_READDIR] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200807 .pc_func = nfsd_proc_readdir,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200808 .pc_decode = nfssvc_decode_readdirargs,
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200809 .pc_encode = nfssvc_encode_readdirres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800810 .pc_argsize = sizeof(struct nfsd_readdirargs),
811 .pc_ressize = sizeof(struct nfsd_readdirres),
812 .pc_cachetype = RC_NOCACHE,
Chuck Lever2289e872020-09-17 17:22:49 -0400813 .pc_name = "READDIR",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800814 },
815 [NFSPROC_STATFS] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200816 .pc_func = nfsd_proc_statfs,
Chuck Leverebcd8e82020-10-21 12:14:23 -0400817 .pc_decode = nfssvc_decode_fhandleargs,
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200818 .pc_encode = nfssvc_encode_statfsres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800819 .pc_argsize = sizeof(struct nfsd_fhandle),
820 .pc_ressize = sizeof(struct nfsd_statfsres),
821 .pc_cachetype = RC_NOCACHE,
822 .pc_xdrressize = ST+5,
Chuck Lever2289e872020-09-17 17:22:49 -0400823 .pc_name = "STATFS",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800824 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825};
826
827
Christoph Hellwig7fd38af2017-05-08 23:40:27 +0200828static unsigned int nfsd_count2[ARRAY_SIZE(nfsd_procedures2)];
Christoph Hellwige9679182017-05-12 16:21:37 +0200829const struct svc_version nfsd_version2 = {
830 .vs_vers = 2,
831 .vs_nproc = 18,
832 .vs_proc = nfsd_procedures2,
833 .vs_count = nfsd_count2,
834 .vs_dispatch = nfsd_dispatch,
835 .vs_xdrsize = NFS2_SVC_XDRSIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836};
837
838/*
839 * Map errnos to NFS errnos.
840 */
Al Viro63f103112006-10-19 23:28:54 -0700841__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842nfserrno (int errno)
843{
844 static struct {
Al Viro63f103112006-10-19 23:28:54 -0700845 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 int syserr;
847 } nfs_errtbl[] = {
848 { nfs_ok, 0 },
849 { nfserr_perm, -EPERM },
850 { nfserr_noent, -ENOENT },
851 { nfserr_io, -EIO },
852 { nfserr_nxio, -ENXIO },
Jeff Layton62814d62014-07-03 15:15:54 -0400853 { nfserr_fbig, -E2BIG },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 { nfserr_acces, -EACCES },
855 { nfserr_exist, -EEXIST },
856 { nfserr_xdev, -EXDEV },
857 { nfserr_mlink, -EMLINK },
858 { nfserr_nodev, -ENODEV },
859 { nfserr_notdir, -ENOTDIR },
860 { nfserr_isdir, -EISDIR },
861 { nfserr_inval, -EINVAL },
862 { nfserr_fbig, -EFBIG },
863 { nfserr_nospc, -ENOSPC },
864 { nfserr_rofs, -EROFS },
865 { nfserr_mlink, -EMLINK },
866 { nfserr_nametoolong, -ENAMETOOLONG },
867 { nfserr_notempty, -ENOTEMPTY },
868#ifdef EDQUOT
869 { nfserr_dquot, -EDQUOT },
870#endif
871 { nfserr_stale, -ESTALE },
872 { nfserr_jukebox, -ETIMEDOUT },
NeilBrown599eb302008-06-19 10:11:09 +1000873 { nfserr_jukebox, -ERESTARTSYS },
J. Bruce Fields062304a2011-01-02 22:05:33 -0500874 { nfserr_jukebox, -EAGAIN },
875 { nfserr_jukebox, -EWOULDBLOCK },
J. Bruce Fields3beb6cd2011-01-01 15:43:50 -0500876 { nfserr_jukebox, -ENOMEM },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 { nfserr_io, -ETXTBSY },
Andreas Gruenbachera838cc42005-06-22 17:16:24 +0000878 { nfserr_notsupp, -EOPNOTSUPP },
Dean Hildebrandb7aeda42008-12-15 19:40:15 +0200879 { nfserr_toosmall, -ETOOSMALL },
J. Bruce Fieldsf39bde22009-09-27 14:41:43 -0400880 { nfserr_serverfault, -ESERVERFAULT },
Jeff Laytonb3fbfe02014-07-29 21:37:44 -0400881 { nfserr_serverfault, -ENFILE },
J. Bruce Fields42e61612016-10-04 13:57:43 -0400882 { nfserr_io, -EUCLEAN },
Kinglong Meec952cd42017-03-10 09:52:20 +0800883 { nfserr_perm, -ENOKEY },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 };
885 int i;
886
Al Viro63f103112006-10-19 23:28:54 -0700887 for (i = 0; i < ARRAY_SIZE(nfs_errtbl); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (nfs_errtbl[i].syserr == errno)
889 return nfs_errtbl[i].nfserr;
890 }
J. Bruce Fieldsff30f082016-10-04 12:53:49 -0400891 WARN_ONCE(1, "nfsd: non-standard errno: %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return nfserr_io;
893}
894