blob: 48c7344151df3b54aaaa25feb427e597ddc8e687 [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 Leverdae9a6c2021-09-30 17:06:21 -0400237 nvecs = svc_fill_write_vector(rqstp, &argp->payload);
Chuck Leverf0af2212020-10-01 18:59:49 -0400238
239 resp->status = nfsd_write(rqstp, fh_copy(&resp->fh, &argp->fh),
240 argp->offset, rqstp->rq_vec, nvecs,
241 &cnt, NFS_DATA_SYNC, NULL);
Chuck Levercc028a12020-10-02 15:52:44 -0400242 if (resp->status == nfs_ok)
243 resp->status = fh_getattr(&resp->fh, &resp->stat);
244 else if (resp->status == nfserr_jukebox)
245 return rpc_drop_reply;
Chuck Levercc028a12020-10-02 15:52:44 -0400246 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249/*
250 * CREATE processing is complicated. The keyword here is `overloaded.'
251 * The parent directory is kept locked between the check for existence
252 * and the actual create() call in compliance with VFS protocols.
253 * N.B. After this call _both_ argp->fh and resp->fh need an fh_put
254 */
Al Viro7111c662006-10-19 23:28:45 -0700255static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200256nfsd_proc_create(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200258 struct nfsd_createargs *argp = rqstp->rq_argp;
259 struct nfsd_diropres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 svc_fh *dirfhp = &argp->fh;
261 svc_fh *newfhp = &resp->fh;
262 struct iattr *attr = &argp->attrs;
263 struct inode *inode;
264 struct dentry *dchild;
Al Viroc4d987b2006-10-19 23:29:00 -0700265 int type, mode;
Jan Kara4a55c102012-06-12 16:20:33 +0200266 int hosterr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 dev_t rdev = 0, wanted = new_decode_dev(attr->ia_size);
268
269 dprintk("nfsd: CREATE %s %.*s\n",
270 SVCFH_fmt(dirfhp), argp->len, argp->name);
271
272 /* First verify the parent file handle */
Chuck Leverf0af2212020-10-01 18:59:49 -0400273 resp->status = fh_verify(rqstp, dirfhp, S_IFDIR, NFSD_MAY_EXEC);
274 if (resp->status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 goto done; /* must fh_put dirfhp even on error */
276
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200277 /* Check for NFSD_MAY_WRITE in nfsd_create if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Chuck Leverf0af2212020-10-01 18:59:49 -0400279 resp->status = nfserr_exist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (isdotent(argp->name, argp->len))
281 goto done;
Jan Kara4a55c102012-06-12 16:20:33 +0200282 hosterr = fh_want_write(dirfhp);
283 if (hosterr) {
Chuck Leverf0af2212020-10-01 18:59:49 -0400284 resp->status = nfserrno(hosterr);
Jan Kara4a55c102012-06-12 16:20:33 +0200285 goto done;
286 }
287
NeilBrown7ed94292006-10-04 02:15:43 -0700288 fh_lock_nested(dirfhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 dchild = lookup_one_len(argp->name, dirfhp->fh_dentry, argp->len);
290 if (IS_ERR(dchild)) {
Chuck Leverf0af2212020-10-01 18:59:49 -0400291 resp->status = nfserrno(PTR_ERR(dchild));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 goto out_unlock;
293 }
294 fh_init(newfhp, NFS_FHSIZE);
Chuck Leverf0af2212020-10-01 18:59:49 -0400295 resp->status = fh_compose(newfhp, dirfhp->fh_export, dchild, dirfhp);
296 if (!resp->status && d_really_is_negative(dchild))
297 resp->status = nfserr_noent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 dput(dchild);
Chuck Leverf0af2212020-10-01 18:59:49 -0400299 if (resp->status) {
300 if (resp->status != nfserr_noent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 goto out_unlock;
302 /*
303 * If the new file handle wasn't verified, we can't tell
304 * whether the file exists or not. Time to bail ...
305 */
Chuck Leverf0af2212020-10-01 18:59:49 -0400306 resp->status = nfserr_acces;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (!newfhp->fh_dentry) {
308 printk(KERN_WARNING
309 "nfsd_proc_create: file handle not verified\n");
310 goto out_unlock;
311 }
312 }
313
David Howells2b0143b2015-03-17 22:25:59 +0000314 inode = d_inode(newfhp->fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 /* Unfudge the mode bits */
317 if (attr->ia_valid & ATTR_MODE) {
318 type = attr->ia_mode & S_IFMT;
319 mode = attr->ia_mode & ~S_IFMT;
320 if (!type) {
321 /* no type, so if target exists, assume same as that,
322 * else assume a file */
323 if (inode) {
324 type = inode->i_mode & S_IFMT;
325 switch(type) {
326 case S_IFCHR:
327 case S_IFBLK:
328 /* reserve rdev for later checking */
329 rdev = inode->i_rdev;
330 attr->ia_valid |= ATTR_SIZE;
331
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500332 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 case S_IFIFO:
334 /* this is probably a permission check..
335 * at least IRIX implements perm checking on
336 * echo thing > device-special-file-or-pipe
337 * by doing a CREATE with type==0
338 */
Chuck Leverf0af2212020-10-01 18:59:49 -0400339 resp->status = nfsd_permission(rqstp,
J. Bruce Fields0ec757d2007-07-17 04:04:48 -0700340 newfhp->fh_export,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 newfhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200342 NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS);
Chuck Leverf0af2212020-10-01 18:59:49 -0400343 if (resp->status && resp->status != nfserr_rofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 goto out_unlock;
345 }
346 } else
347 type = S_IFREG;
348 }
349 } else if (inode) {
350 type = inode->i_mode & S_IFMT;
351 mode = inode->i_mode & ~S_IFMT;
352 } else {
353 type = S_IFREG;
354 mode = 0; /* ??? */
355 }
356
357 attr->ia_valid |= ATTR_MODE;
358 attr->ia_mode = mode;
359
360 /* Special treatment for non-regular files according to the
361 * gospel of sun micro
362 */
363 if (type != S_IFREG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (type != S_IFBLK && type != S_IFCHR) {
365 rdev = 0;
366 } else if (type == S_IFCHR && !(attr->ia_valid & ATTR_SIZE)) {
367 /* If you think you've seen the worst, grok this. */
368 type = S_IFIFO;
369 } else {
370 /* Okay, char or block special */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (!rdev)
372 rdev = wanted;
373 }
374
375 /* we've used the SIZE information, so discard it */
376 attr->ia_valid &= ~ATTR_SIZE;
377
378 /* Make sure the type and device matches */
Chuck Leverf0af2212020-10-01 18:59:49 -0400379 resp->status = nfserr_exist;
Al Viro6e3e2c42021-03-01 20:37:10 -0500380 if (inode && inode_wrong_type(inode, type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 goto out_unlock;
382 }
383
Chuck Leverf0af2212020-10-01 18:59:49 -0400384 resp->status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (!inode) {
386 /* File doesn't exist. Create it and set attrs */
Chuck Leverf0af2212020-10-01 18:59:49 -0400387 resp->status = nfsd_create_locked(rqstp, dirfhp, argp->name,
388 argp->len, attr, type, rdev,
389 newfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 } else if (type == S_IFREG) {
391 dprintk("nfsd: existing %s, valid=%x, size=%ld\n",
392 argp->name, attr->ia_valid, (long) attr->ia_size);
393 /* File already exists. We ignore all attributes except
394 * size, so that creat() behaves exactly like
395 * open(..., O_CREAT|O_TRUNC|O_WRONLY).
396 */
397 attr->ia_valid &= ATTR_SIZE;
398 if (attr->ia_valid)
Chuck Leverf0af2212020-10-01 18:59:49 -0400399 resp->status = nfsd_setattr(rqstp, newfhp, attr, 0,
400 (time64_t)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
403out_unlock:
404 /* We don't really need to unlock, as fh_put does it. */
405 fh_unlock(dirfhp);
Jan Kara4a55c102012-06-12 16:20:33 +0200406 fh_drop_write(dirfhp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407done:
408 fh_put(dirfhp);
Chuck Leverf0af2212020-10-01 18:59:49 -0400409 if (resp->status != nfs_ok)
410 goto out;
411 resp->status = fh_getattr(&resp->fh, &resp->stat);
412out:
Chuck Levercc028a12020-10-02 15:52:44 -0400413 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Al Viro7111c662006-10-19 23:28:45 -0700416static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200417nfsd_proc_remove(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200419 struct nfsd_diropargs *argp = rqstp->rq_argp;
Chuck Levercc028a12020-10-02 15:52:44 -0400420 struct nfsd_stat *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 dprintk("nfsd: REMOVE %s %.*s\n", SVCFH_fmt(&argp->fh),
423 argp->len, argp->name);
424
425 /* Unlink. -SIFDIR means file must not be a directory */
Chuck Levercc028a12020-10-02 15:52:44 -0400426 resp->status = nfsd_unlink(rqstp, &argp->fh, -S_IFDIR,
427 argp->name, argp->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 fh_put(&argp->fh);
Chuck Levercc028a12020-10-02 15:52:44 -0400429 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Al Viro7111c662006-10-19 23:28:45 -0700432static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200433nfsd_proc_rename(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200435 struct nfsd_renameargs *argp = rqstp->rq_argp;
Chuck Levercc028a12020-10-02 15:52:44 -0400436 struct nfsd_stat *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 dprintk("nfsd: RENAME %s %.*s -> \n",
439 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname);
440 dprintk("nfsd: -> %s %.*s\n",
441 SVCFH_fmt(&argp->tfh), argp->tlen, argp->tname);
442
Chuck Levercc028a12020-10-02 15:52:44 -0400443 resp->status = nfsd_rename(rqstp, &argp->ffh, argp->fname, argp->flen,
444 &argp->tfh, argp->tname, argp->tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 fh_put(&argp->ffh);
446 fh_put(&argp->tfh);
Chuck Levercc028a12020-10-02 15:52:44 -0400447 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Al Viro7111c662006-10-19 23:28:45 -0700450static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200451nfsd_proc_link(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200453 struct nfsd_linkargs *argp = rqstp->rq_argp;
Chuck Levercc028a12020-10-02 15:52:44 -0400454 struct nfsd_stat *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 dprintk("nfsd: LINK %s ->\n",
457 SVCFH_fmt(&argp->ffh));
458 dprintk("nfsd: %s %.*s\n",
459 SVCFH_fmt(&argp->tfh),
460 argp->tlen,
461 argp->tname);
462
Chuck Levercc028a12020-10-02 15:52:44 -0400463 resp->status = nfsd_link(rqstp, &argp->tfh, argp->tname, argp->tlen,
464 &argp->ffh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 fh_put(&argp->ffh);
466 fh_put(&argp->tfh);
Chuck Levercc028a12020-10-02 15:52:44 -0400467 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Al Viro7111c662006-10-19 23:28:45 -0700470static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200471nfsd_proc_symlink(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200473 struct nfsd_symlinkargs *argp = rqstp->rq_argp;
Chuck Levercc028a12020-10-02 15:52:44 -0400474 struct nfsd_stat *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct svc_fh newfh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Chuck Leverf0af2212020-10-01 18:59:49 -0400477 if (argp->tlen > NFS_MAXPATHLEN) {
Chuck Levercc028a12020-10-02 15:52:44 -0400478 resp->status = nfserr_nametoolong;
Chuck Leverf0af2212020-10-01 18:59:49 -0400479 goto out;
480 }
Chuck Lever38a70312018-03-27 10:54:21 -0400481
482 argp->tname = svc_fill_symlink_pathname(rqstp, &argp->first,
Chuck Lever11b4d662018-07-27 11:19:10 -0400483 page_address(rqstp->rq_arg.pages[0]),
Chuck Lever38a70312018-03-27 10:54:21 -0400484 argp->tlen);
Chuck Leverf0af2212020-10-01 18:59:49 -0400485 if (IS_ERR(argp->tname)) {
Chuck Levercc028a12020-10-02 15:52:44 -0400486 resp->status = nfserrno(PTR_ERR(argp->tname));
Chuck Leverf0af2212020-10-01 18:59:49 -0400487 goto out;
488 }
Chuck Lever38a70312018-03-27 10:54:21 -0400489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 dprintk("nfsd: SYMLINK %s %.*s -> %.*s\n",
491 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname,
492 argp->tlen, argp->tname);
493
494 fh_init(&newfh, NFS_FHSIZE);
Chuck Levercc028a12020-10-02 15:52:44 -0400495 resp->status = nfsd_symlink(rqstp, &argp->ffh, argp->fname, argp->flen,
496 argp->tname, &newfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Chuck Lever11b4d662018-07-27 11:19:10 -0400498 kfree(argp->tname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 fh_put(&argp->ffh);
500 fh_put(&newfh);
Chuck Leverf0af2212020-10-01 18:59:49 -0400501out:
Chuck Levercc028a12020-10-02 15:52:44 -0400502 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505/*
506 * Make directory. This operation is not idempotent.
507 * N.B. After this call resp->fh needs an fh_put
508 */
Al Viro7111c662006-10-19 23:28:45 -0700509static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200510nfsd_proc_mkdir(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200512 struct nfsd_createargs *argp = rqstp->rq_argp;
513 struct nfsd_diropres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 dprintk("nfsd: MKDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
516
517 if (resp->fh.fh_dentry) {
518 printk(KERN_WARNING
519 "nfsd_proc_mkdir: response already verified??\n");
520 }
521
522 argp->attrs.ia_valid &= ~ATTR_SIZE;
523 fh_init(&resp->fh, NFS_FHSIZE);
Chuck Leverf0af2212020-10-01 18:59:49 -0400524 resp->status = nfsd_create(rqstp, &argp->fh, argp->name, argp->len,
525 &argp->attrs, S_IFDIR, 0, &resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 fh_put(&argp->fh);
Chuck Leverf0af2212020-10-01 18:59:49 -0400527 if (resp->status != nfs_ok)
528 goto out;
529
530 resp->status = fh_getattr(&resp->fh, &resp->stat);
531out:
Chuck Levercc028a12020-10-02 15:52:44 -0400532 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
535/*
536 * Remove a directory
537 */
Al Viro7111c662006-10-19 23:28:45 -0700538static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200539nfsd_proc_rmdir(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200541 struct nfsd_diropargs *argp = rqstp->rq_argp;
Chuck Levercc028a12020-10-02 15:52:44 -0400542 struct nfsd_stat *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 dprintk("nfsd: RMDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
545
Chuck Levercc028a12020-10-02 15:52:44 -0400546 resp->status = nfsd_unlink(rqstp, &argp->fh, S_IFDIR,
547 argp->name, argp->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 fh_put(&argp->fh);
Chuck Levercc028a12020-10-02 15:52:44 -0400549 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Chuck Lever788cd462020-11-13 17:03:49 -0500552static void nfsd_init_dirlist_pages(struct svc_rqst *rqstp,
553 struct nfsd_readdirres *resp,
554 int count)
555{
Chuck Leverf5dcccd2020-11-14 13:45:35 -0500556 struct xdr_buf *buf = &resp->dirlist;
557 struct xdr_stream *xdr = &resp->xdr;
558
Chuck Lever788cd462020-11-13 17:03:49 -0500559 count = min_t(u32, count, PAGE_SIZE);
560
Chuck Leverf5dcccd2020-11-14 13:45:35 -0500561 memset(buf, 0, sizeof(*buf));
Chuck Lever788cd462020-11-13 17:03:49 -0500562
Chuck Leverf5dcccd2020-11-14 13:45:35 -0500563 /* Reserve room for the NULL ptr & eof flag (-2 words) */
564 buf->buflen = count - sizeof(__be32) * 2;
565 buf->pages = rqstp->rq_next_page;
Chuck Lever788cd462020-11-13 17:03:49 -0500566 rqstp->rq_next_page++;
Chuck Leverf5dcccd2020-11-14 13:45:35 -0500567
568 /* This is xdr_init_encode(), but it assumes that
569 * the head kvec has already been consumed. */
570 xdr_set_scratch_buffer(xdr, NULL, 0);
571 xdr->buf = buf;
572 xdr->page_ptr = buf->pages;
573 xdr->iov = NULL;
574 xdr->p = page_address(*buf->pages);
575 xdr->end = xdr->p + (PAGE_SIZE >> 2);
576 xdr->rqst = NULL;
Chuck Lever788cd462020-11-13 17:03:49 -0500577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579/*
580 * Read a portion of a directory.
581 */
Al Viro7111c662006-10-19 23:28:45 -0700582static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200583nfsd_proc_readdir(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200585 struct nfsd_readdirargs *argp = rqstp->rq_argp;
586 struct nfsd_readdirres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 loff_t offset;
588
589 dprintk("nfsd: READDIR %s %d bytes at %d\n",
590 SVCFH_fmt(&argp->fh),
591 argp->count, argp->cookie);
592
Chuck Lever788cd462020-11-13 17:03:49 -0500593 nfsd_init_dirlist_pages(rqstp, resp, argp->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 resp->common.err = nfs_ok;
Chuck Leverf5dcccd2020-11-14 13:45:35 -0500596 resp->cookie_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 offset = argp->cookie;
Chuck Leverf0af2212020-10-01 18:59:49 -0400598 resp->status = nfsd_readdir(rqstp, &argp->fh, &offset,
Chuck Lever8a2cf9f2020-11-15 14:30:13 -0500599 &resp->common, nfssvc_encode_entry);
Chuck Leverd52532002020-11-13 16:53:17 -0500600 nfssvc_encode_nfscookie(resp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 fh_put(&argp->fh);
Chuck Levercc028a12020-10-02 15:52:44 -0400603 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606/*
607 * Get file system info
608 */
Al Viro7111c662006-10-19 23:28:45 -0700609static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200610nfsd_proc_statfs(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200612 struct nfsd_fhandle *argp = rqstp->rq_argp;
613 struct nfsd_statfsres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 dprintk("nfsd: STATFS %s\n", SVCFH_fmt(&argp->fh));
616
Chuck Leverf0af2212020-10-01 18:59:49 -0400617 resp->status = nfsd_statfs(rqstp, &argp->fh, &resp->stats,
618 NFSD_MAY_BYPASS_GSS_ON_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 fh_put(&argp->fh);
Chuck Levercc028a12020-10-02 15:52:44 -0400620 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623/*
624 * NFSv2 Server procedures.
625 * Only the results of non-idempotent operations are cached.
626 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628#define ST 1 /* status */
629#define FH 8 /* filehandle */
630#define AT 18 /* attributes */
631
Christoph Hellwig860bda22017-05-12 16:11:49 +0200632static const struct svc_procedure nfsd_procedures2[18] = {
Yu Zhiguob9081d92009-06-09 17:33:34 +0800633 [NFSPROC_NULL] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200634 .pc_func = nfsd_proc_null,
Chuck Lever788f7182020-11-05 14:48:29 -0500635 .pc_decode = nfssvc_decode_voidarg,
636 .pc_encode = nfssvc_encode_voidres,
637 .pc_argsize = sizeof(struct nfsd_voidargs),
638 .pc_ressize = sizeof(struct nfsd_voidres),
Yu Zhiguob9081d92009-06-09 17:33:34 +0800639 .pc_cachetype = RC_NOCACHE,
Chuck Levercc028a12020-10-02 15:52:44 -0400640 .pc_xdrressize = 0,
Chuck Lever2289e872020-09-17 17:22:49 -0400641 .pc_name = "NULL",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800642 },
643 [NFSPROC_GETATTR] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200644 .pc_func = nfsd_proc_getattr,
Chuck Leverebcd8e82020-10-21 12:14:23 -0400645 .pc_decode = nfssvc_decode_fhandleargs,
Chuck Lever92b54a42020-10-23 15:28:59 -0400646 .pc_encode = nfssvc_encode_attrstatres,
Chuck Lever1841b9b2020-10-01 18:59:44 -0400647 .pc_release = nfssvc_release_attrstat,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800648 .pc_argsize = sizeof(struct nfsd_fhandle),
649 .pc_ressize = sizeof(struct nfsd_attrstat),
650 .pc_cachetype = RC_NOCACHE,
651 .pc_xdrressize = ST+AT,
Chuck Lever2289e872020-09-17 17:22:49 -0400652 .pc_name = "GETATTR",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800653 },
654 [NFSPROC_SETATTR] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200655 .pc_func = nfsd_proc_setattr,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200656 .pc_decode = nfssvc_decode_sattrargs,
Chuck Lever92b54a42020-10-23 15:28:59 -0400657 .pc_encode = nfssvc_encode_attrstatres,
Chuck Lever1841b9b2020-10-01 18:59:44 -0400658 .pc_release = nfssvc_release_attrstat,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800659 .pc_argsize = sizeof(struct nfsd_sattrargs),
660 .pc_ressize = sizeof(struct nfsd_attrstat),
661 .pc_cachetype = RC_REPLBUFF,
662 .pc_xdrressize = ST+AT,
Chuck Lever2289e872020-09-17 17:22:49 -0400663 .pc_name = "SETATTR",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800664 },
665 [NFSPROC_ROOT] = {
Chuck Lever6b3dccd2020-10-01 18:58:56 -0400666 .pc_func = nfsd_proc_root,
Chuck Lever788f7182020-11-05 14:48:29 -0500667 .pc_decode = nfssvc_decode_voidarg,
668 .pc_encode = nfssvc_encode_voidres,
669 .pc_argsize = sizeof(struct nfsd_voidargs),
670 .pc_ressize = sizeof(struct nfsd_voidres),
Yu Zhiguob9081d92009-06-09 17:33:34 +0800671 .pc_cachetype = RC_NOCACHE,
Chuck Levercc028a12020-10-02 15:52:44 -0400672 .pc_xdrressize = 0,
Chuck Lever2289e872020-09-17 17:22:49 -0400673 .pc_name = "ROOT",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800674 },
675 [NFSPROC_LOOKUP] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200676 .pc_func = nfsd_proc_lookup,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200677 .pc_decode = nfssvc_decode_diropargs,
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200678 .pc_encode = nfssvc_encode_diropres,
Chuck Lever1841b9b2020-10-01 18:59:44 -0400679 .pc_release = nfssvc_release_diropres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800680 .pc_argsize = sizeof(struct nfsd_diropargs),
681 .pc_ressize = sizeof(struct nfsd_diropres),
682 .pc_cachetype = RC_NOCACHE,
683 .pc_xdrressize = ST+FH+AT,
Chuck Lever2289e872020-09-17 17:22:49 -0400684 .pc_name = "LOOKUP",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800685 },
686 [NFSPROC_READLINK] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200687 .pc_func = nfsd_proc_readlink,
Chuck Lever1fcbd1c2020-10-21 12:21:25 -0400688 .pc_decode = nfssvc_decode_fhandleargs,
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200689 .pc_encode = nfssvc_encode_readlinkres,
Chuck Lever1fcbd1c2020-10-21 12:21:25 -0400690 .pc_argsize = sizeof(struct nfsd_fhandle),
Yu Zhiguob9081d92009-06-09 17:33:34 +0800691 .pc_ressize = sizeof(struct nfsd_readlinkres),
692 .pc_cachetype = RC_NOCACHE,
693 .pc_xdrressize = ST+1+NFS_MAXPATHLEN/4,
Chuck Lever2289e872020-09-17 17:22:49 -0400694 .pc_name = "READLINK",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800695 },
696 [NFSPROC_READ] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200697 .pc_func = nfsd_proc_read,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200698 .pc_decode = nfssvc_decode_readargs,
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200699 .pc_encode = nfssvc_encode_readres,
Chuck Lever1841b9b2020-10-01 18:59:44 -0400700 .pc_release = nfssvc_release_readres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800701 .pc_argsize = sizeof(struct nfsd_readargs),
702 .pc_ressize = sizeof(struct nfsd_readres),
703 .pc_cachetype = RC_NOCACHE,
704 .pc_xdrressize = ST+AT+1+NFSSVC_MAXBLKSIZE_V2/4,
Chuck Lever2289e872020-09-17 17:22:49 -0400705 .pc_name = "READ",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800706 },
707 [NFSPROC_WRITECACHE] = {
Chuck Lever6b3dccd2020-10-01 18:58:56 -0400708 .pc_func = nfsd_proc_writecache,
Chuck Lever788f7182020-11-05 14:48:29 -0500709 .pc_decode = nfssvc_decode_voidarg,
710 .pc_encode = nfssvc_encode_voidres,
711 .pc_argsize = sizeof(struct nfsd_voidargs),
712 .pc_ressize = sizeof(struct nfsd_voidres),
Yu Zhiguob9081d92009-06-09 17:33:34 +0800713 .pc_cachetype = RC_NOCACHE,
Chuck Levercc028a12020-10-02 15:52:44 -0400714 .pc_xdrressize = 0,
Chuck Lever2289e872020-09-17 17:22:49 -0400715 .pc_name = "WRITECACHE",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800716 },
717 [NFSPROC_WRITE] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200718 .pc_func = nfsd_proc_write,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200719 .pc_decode = nfssvc_decode_writeargs,
Chuck Lever92b54a42020-10-23 15:28:59 -0400720 .pc_encode = nfssvc_encode_attrstatres,
Chuck Lever1841b9b2020-10-01 18:59:44 -0400721 .pc_release = nfssvc_release_attrstat,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800722 .pc_argsize = sizeof(struct nfsd_writeargs),
723 .pc_ressize = sizeof(struct nfsd_attrstat),
724 .pc_cachetype = RC_REPLBUFF,
725 .pc_xdrressize = ST+AT,
Chuck Lever2289e872020-09-17 17:22:49 -0400726 .pc_name = "WRITE",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800727 },
728 [NFSPROC_CREATE] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200729 .pc_func = nfsd_proc_create,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200730 .pc_decode = nfssvc_decode_createargs,
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200731 .pc_encode = nfssvc_encode_diropres,
Chuck Lever1841b9b2020-10-01 18:59:44 -0400732 .pc_release = nfssvc_release_diropres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800733 .pc_argsize = sizeof(struct nfsd_createargs),
734 .pc_ressize = sizeof(struct nfsd_diropres),
735 .pc_cachetype = RC_REPLBUFF,
736 .pc_xdrressize = ST+FH+AT,
Chuck Lever2289e872020-09-17 17:22:49 -0400737 .pc_name = "CREATE",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800738 },
739 [NFSPROC_REMOVE] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200740 .pc_func = nfsd_proc_remove,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200741 .pc_decode = nfssvc_decode_diropargs,
Chuck Levera887eae2020-10-23 11:08:02 -0400742 .pc_encode = nfssvc_encode_statres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800743 .pc_argsize = sizeof(struct nfsd_diropargs),
Chuck Levercc028a12020-10-02 15:52:44 -0400744 .pc_ressize = sizeof(struct nfsd_stat),
Yu Zhiguob9081d92009-06-09 17:33:34 +0800745 .pc_cachetype = RC_REPLSTAT,
746 .pc_xdrressize = ST,
Chuck Lever2289e872020-09-17 17:22:49 -0400747 .pc_name = "REMOVE",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800748 },
749 [NFSPROC_RENAME] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200750 .pc_func = nfsd_proc_rename,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200751 .pc_decode = nfssvc_decode_renameargs,
Chuck Levera887eae2020-10-23 11:08:02 -0400752 .pc_encode = nfssvc_encode_statres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800753 .pc_argsize = sizeof(struct nfsd_renameargs),
Chuck Levercc028a12020-10-02 15:52:44 -0400754 .pc_ressize = sizeof(struct nfsd_stat),
Yu Zhiguob9081d92009-06-09 17:33:34 +0800755 .pc_cachetype = RC_REPLSTAT,
756 .pc_xdrressize = ST,
Chuck Lever2289e872020-09-17 17:22:49 -0400757 .pc_name = "RENAME",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800758 },
759 [NFSPROC_LINK] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200760 .pc_func = nfsd_proc_link,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200761 .pc_decode = nfssvc_decode_linkargs,
Chuck Levera887eae2020-10-23 11:08:02 -0400762 .pc_encode = nfssvc_encode_statres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800763 .pc_argsize = sizeof(struct nfsd_linkargs),
Chuck Levercc028a12020-10-02 15:52:44 -0400764 .pc_ressize = sizeof(struct nfsd_stat),
Yu Zhiguob9081d92009-06-09 17:33:34 +0800765 .pc_cachetype = RC_REPLSTAT,
766 .pc_xdrressize = ST,
Chuck Lever2289e872020-09-17 17:22:49 -0400767 .pc_name = "LINK",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800768 },
769 [NFSPROC_SYMLINK] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200770 .pc_func = nfsd_proc_symlink,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200771 .pc_decode = nfssvc_decode_symlinkargs,
Chuck Levera887eae2020-10-23 11:08:02 -0400772 .pc_encode = nfssvc_encode_statres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800773 .pc_argsize = sizeof(struct nfsd_symlinkargs),
Chuck Levercc028a12020-10-02 15:52:44 -0400774 .pc_ressize = sizeof(struct nfsd_stat),
Yu Zhiguob9081d92009-06-09 17:33:34 +0800775 .pc_cachetype = RC_REPLSTAT,
776 .pc_xdrressize = ST,
Chuck Lever2289e872020-09-17 17:22:49 -0400777 .pc_name = "SYMLINK",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800778 },
779 [NFSPROC_MKDIR] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200780 .pc_func = nfsd_proc_mkdir,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200781 .pc_decode = nfssvc_decode_createargs,
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200782 .pc_encode = nfssvc_encode_diropres,
Chuck Lever1841b9b2020-10-01 18:59:44 -0400783 .pc_release = nfssvc_release_diropres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800784 .pc_argsize = sizeof(struct nfsd_createargs),
785 .pc_ressize = sizeof(struct nfsd_diropres),
786 .pc_cachetype = RC_REPLBUFF,
787 .pc_xdrressize = ST+FH+AT,
Chuck Lever2289e872020-09-17 17:22:49 -0400788 .pc_name = "MKDIR",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800789 },
790 [NFSPROC_RMDIR] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200791 .pc_func = nfsd_proc_rmdir,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200792 .pc_decode = nfssvc_decode_diropargs,
Chuck Levera887eae2020-10-23 11:08:02 -0400793 .pc_encode = nfssvc_encode_statres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800794 .pc_argsize = sizeof(struct nfsd_diropargs),
Chuck Levercc028a12020-10-02 15:52:44 -0400795 .pc_ressize = sizeof(struct nfsd_stat),
Yu Zhiguob9081d92009-06-09 17:33:34 +0800796 .pc_cachetype = RC_REPLSTAT,
797 .pc_xdrressize = ST,
Chuck Lever2289e872020-09-17 17:22:49 -0400798 .pc_name = "RMDIR",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800799 },
800 [NFSPROC_READDIR] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200801 .pc_func = nfsd_proc_readdir,
Christoph Hellwig026fec72017-05-08 19:01:48 +0200802 .pc_decode = nfssvc_decode_readdirargs,
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200803 .pc_encode = nfssvc_encode_readdirres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800804 .pc_argsize = sizeof(struct nfsd_readdirargs),
805 .pc_ressize = sizeof(struct nfsd_readdirres),
806 .pc_cachetype = RC_NOCACHE,
Chuck Lever2289e872020-09-17 17:22:49 -0400807 .pc_name = "READDIR",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800808 },
809 [NFSPROC_STATFS] = {
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200810 .pc_func = nfsd_proc_statfs,
Chuck Leverebcd8e82020-10-21 12:14:23 -0400811 .pc_decode = nfssvc_decode_fhandleargs,
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200812 .pc_encode = nfssvc_encode_statfsres,
Yu Zhiguob9081d92009-06-09 17:33:34 +0800813 .pc_argsize = sizeof(struct nfsd_fhandle),
814 .pc_ressize = sizeof(struct nfsd_statfsres),
815 .pc_cachetype = RC_NOCACHE,
816 .pc_xdrressize = ST+5,
Chuck Lever2289e872020-09-17 17:22:49 -0400817 .pc_name = "STATFS",
Yu Zhiguob9081d92009-06-09 17:33:34 +0800818 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819};
820
821
Christoph Hellwig7fd38af2017-05-08 23:40:27 +0200822static unsigned int nfsd_count2[ARRAY_SIZE(nfsd_procedures2)];
Christoph Hellwige9679182017-05-12 16:21:37 +0200823const struct svc_version nfsd_version2 = {
824 .vs_vers = 2,
825 .vs_nproc = 18,
826 .vs_proc = nfsd_procedures2,
827 .vs_count = nfsd_count2,
828 .vs_dispatch = nfsd_dispatch,
829 .vs_xdrsize = NFS2_SVC_XDRSIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830};
831
832/*
833 * Map errnos to NFS errnos.
834 */
Al Viro63f103112006-10-19 23:28:54 -0700835__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836nfserrno (int errno)
837{
838 static struct {
Al Viro63f103112006-10-19 23:28:54 -0700839 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 int syserr;
841 } nfs_errtbl[] = {
842 { nfs_ok, 0 },
843 { nfserr_perm, -EPERM },
844 { nfserr_noent, -ENOENT },
845 { nfserr_io, -EIO },
846 { nfserr_nxio, -ENXIO },
Jeff Layton62814d62014-07-03 15:15:54 -0400847 { nfserr_fbig, -E2BIG },
Peng Taob3d0db72021-12-18 20:37:54 -0500848 { nfserr_stale, -EBADF },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 { nfserr_acces, -EACCES },
850 { nfserr_exist, -EEXIST },
851 { nfserr_xdev, -EXDEV },
852 { nfserr_mlink, -EMLINK },
853 { nfserr_nodev, -ENODEV },
854 { nfserr_notdir, -ENOTDIR },
855 { nfserr_isdir, -EISDIR },
856 { nfserr_inval, -EINVAL },
857 { nfserr_fbig, -EFBIG },
858 { nfserr_nospc, -ENOSPC },
859 { nfserr_rofs, -EROFS },
860 { nfserr_mlink, -EMLINK },
861 { nfserr_nametoolong, -ENAMETOOLONG },
862 { nfserr_notempty, -ENOTEMPTY },
863#ifdef EDQUOT
864 { nfserr_dquot, -EDQUOT },
865#endif
866 { nfserr_stale, -ESTALE },
867 { nfserr_jukebox, -ETIMEDOUT },
NeilBrown599eb302008-06-19 10:11:09 +1000868 { nfserr_jukebox, -ERESTARTSYS },
J. Bruce Fields062304a2011-01-02 22:05:33 -0500869 { nfserr_jukebox, -EAGAIN },
870 { nfserr_jukebox, -EWOULDBLOCK },
J. Bruce Fields3beb6cd2011-01-01 15:43:50 -0500871 { nfserr_jukebox, -ENOMEM },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 { nfserr_io, -ETXTBSY },
Andreas Gruenbachera838cc42005-06-22 17:16:24 +0000873 { nfserr_notsupp, -EOPNOTSUPP },
Dean Hildebrandb7aeda42008-12-15 19:40:15 +0200874 { nfserr_toosmall, -ETOOSMALL },
J. Bruce Fieldsf39bde22009-09-27 14:41:43 -0400875 { nfserr_serverfault, -ESERVERFAULT },
Jeff Laytonb3fbfe02014-07-29 21:37:44 -0400876 { nfserr_serverfault, -ENFILE },
Jeff Laytona2694e52021-12-18 20:37:55 -0500877 { nfserr_io, -EREMOTEIO },
Jeff Layton12bcbd42021-12-18 20:37:56 -0500878 { nfserr_stale, -EOPENSTALE },
J. Bruce Fields42e61612016-10-04 13:57:43 -0400879 { nfserr_io, -EUCLEAN },
Kinglong Meec952cd42017-03-10 09:52:20 +0800880 { nfserr_perm, -ENOKEY },
J. Bruce Fieldsbb0a55b2021-08-20 17:02:06 -0400881 { nfserr_no_grace, -ENOGRACE},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 };
883 int i;
884
Al Viro63f103112006-10-19 23:28:54 -0700885 for (i = 0; i < ARRAY_SIZE(nfs_errtbl); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (nfs_errtbl[i].syserr == errno)
887 return nfs_errtbl[i].nfserr;
888 }
J. Bruce Fieldsff30f082016-10-04 12:53:49 -0400889 WARN_ONCE(1, "nfsd: non-standard errno: %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return nfserr_io;
891}
892