blob: 8273a99359d1de0e4911d09963aa276d63249283 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Server-side procedures for NFSv4.
3 *
4 * Copyright (c) 2002 The Regents of the University of Michigan.
5 * All rights reserved.
6 *
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <andros@umich.edu>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
J. Bruce Fields880a3a52018-03-21 17:19:02 -040035#include <linux/fs_struct.h>
NeilBrown7e06b7f2005-06-23 22:03:13 -070036#include <linux/file.h>
Anna Schumakerb0cb9082014-11-07 14:44:27 -050037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -040039#include <linux/kthread.h>
Olga Kornievskaia624322f2019-10-04 16:34:26 -040040#include <linux/sunrpc/addr.h>
Dai Ngo0cfcd402020-10-18 23:42:49 -040041#include <linux/nfs_ssc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Mi Jinlong58e7b332011-08-28 18:18:56 +080043#include "idmap.h"
Boaz Harrosh9a74af22009-12-03 20:30:56 +020044#include "cache.h"
45#include "xdr4.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050046#include "vfs.h"
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +010047#include "current_stateid.h"
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +030048#include "netns.h"
Christoph Hellwig4ac72492013-12-20 05:16:55 -080049#include "acl.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020050#include "pnfs.h"
Christoph Hellwig31ef83d2014-08-16 19:02:22 -050051#include "trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Chuck Leverd6c9e432020-12-18 12:28:23 -050053static bool inter_copy_offload_enable;
54module_param(inter_copy_offload_enable, bool, 0644);
55MODULE_PARM_DESC(inter_copy_offload_enable,
56 "Enable inter server to server copy offload. Default: false");
57
David Quigley18032ca2013-05-02 13:19:10 -040058#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
59#include <linux/security.h>
60
61static inline void
62nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
63{
David Howells2b0143b2015-03-17 22:25:59 +000064 struct inode *inode = d_inode(resfh->fh_dentry);
David Quigley18032ca2013-05-02 13:19:10 -040065 int status;
66
Al Viro59551022016-01-22 15:40:57 -050067 inode_lock(inode);
David Quigley18032ca2013-05-02 13:19:10 -040068 status = security_inode_setsecctx(resfh->fh_dentry,
69 label->data, label->len);
Al Viro59551022016-01-22 15:40:57 -050070 inode_unlock(inode);
David Quigley18032ca2013-05-02 13:19:10 -040071
72 if (status)
73 /*
74 * XXX: We should really fail the whole open, but we may
75 * already have created a new file, so it may be too
76 * late. For now this seems the least of evils:
77 */
78 bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
79
80 return;
81}
82#else
83static inline void
84nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
85{ }
86#endif
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define NFSDDBG_FACILITY NFSDDBG_PROC
89
Yu Zhiguo3c8e0312009-05-16 16:22:31 +080090static u32 nfsd_attrmask[] = {
91 NFSD_WRITEABLE_ATTRS_WORD0,
92 NFSD_WRITEABLE_ATTRS_WORD1,
93 NFSD_WRITEABLE_ATTRS_WORD2
94};
95
96static u32 nfsd41_ex_attrmask[] = {
97 NFSD_SUPPATTR_EXCLCREAT_WORD0,
98 NFSD_SUPPATTR_EXCLCREAT_WORD1,
99 NFSD_SUPPATTR_EXCLCREAT_WORD2
100};
101
102static __be32
103check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
104 u32 *bmval, u32 *writable)
105{
106 struct dentry *dentry = cstate->current_fh.fh_dentry;
J. Bruce Fields32ddd942017-01-03 12:30:11 -0500107 struct svc_export *exp = cstate->current_fh.fh_export;
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800108
J. Bruce Fields916d2d82016-10-18 14:18:40 -0400109 if (!nfsd_attrs_supported(cstate->minorversion, bmval))
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800110 return nfserr_attrnotsupp;
J. Bruce Fields916d2d82016-10-18 14:18:40 -0400111 if ((bmval[0] & FATTR4_WORD0_ACL) && !IS_POSIXACL(d_inode(dentry)))
112 return nfserr_attrnotsupp;
J. Bruce Fields32ddd942017-01-03 12:30:11 -0500113 if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) &&
114 !(exp->ex_flags & NFSEXP_SECURITY_LABEL))
115 return nfserr_attrnotsupp;
J. Bruce Fields916d2d82016-10-18 14:18:40 -0400116 if (writable && !bmval_is_subset(bmval, writable))
117 return nfserr_inval;
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100118 if (writable && (bmval[2] & FATTR4_WORD2_MODE_UMASK) &&
119 (bmval[1] & FATTR4_WORD1_MODE))
120 return nfserr_inval;
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800121 return nfs_ok;
122}
123
124static __be32
125nfsd4_check_open_attributes(struct svc_rqst *rqstp,
126 struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
127{
128 __be32 status = nfs_ok;
129
130 if (open->op_create == NFS4_OPEN_CREATE) {
131 if (open->op_createmode == NFS4_CREATE_UNCHECKED
132 || open->op_createmode == NFS4_CREATE_GUARDED)
133 status = check_attr_support(rqstp, cstate,
134 open->op_bmval, nfsd_attrmask);
135 else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1)
136 status = check_attr_support(rqstp, cstate,
137 open->op_bmval, nfsd41_ex_attrmask);
138 }
139
140 return status;
141}
142
Yu Zhiguo9208faf2009-07-06 17:24:16 +0800143static int
144is_create_with_attrs(struct nfsd4_open *open)
145{
146 return open->op_create == NFS4_OPEN_CREATE
147 && (open->op_createmode == NFS4_CREATE_UNCHECKED
148 || open->op_createmode == NFS4_CREATE_GUARDED
149 || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1);
150}
151
152/*
153 * if error occurs when setting the acl, just clear the acl bit
154 * in the returned attr bitmap.
155 */
156static void
157do_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
158 struct nfs4_acl *acl, u32 *bmval)
159{
160 __be32 status;
161
162 status = nfsd4_set_nfs4_acl(rqstp, fhp, acl);
163 if (status)
164 /*
165 * We should probably fail the whole open at this point,
166 * but we've already created the file, so it's too late;
167 * So this seems the least of evils:
168 */
169 bmval[0] &= ~FATTR4_WORD0_ACL;
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static inline void
173fh_dup2(struct svc_fh *dst, struct svc_fh *src)
174{
175 fh_put(dst);
176 dget(src->fh_dentry);
177 if (src->fh_export)
Kinglong Meebf18f162014-06-10 22:06:44 +0800178 exp_get(src->fh_export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 *dst = *src;
180}
181
Al Virob37ad282006-10-19 23:28:59 -0700182static __be32
J. Bruce Fieldsdc730e12006-10-17 00:10:13 -0700183do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Al Virob37ad282006-10-19 23:28:59 -0700185 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 if (open->op_truncate &&
188 !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
189 return nfserr_inval;
190
J. Bruce Fieldsa0432262011-08-25 10:48:39 -0400191 accmode |= NFSD_MAY_READ_IF_EXEC;
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200194 accmode |= NFSD_MAY_READ;
J. Bruce Fields9801d8a2006-10-17 00:10:14 -0700195 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200196 accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC);
J. Bruce Fields57ecb342009-12-01 19:42:57 -0500197 if (open->op_share_deny & NFS4_SHARE_DENY_READ)
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200198 accmode |= NFSD_MAY_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
201
202 return status;
203}
204
J. Bruce Fieldsaadab6c2011-08-15 16:55:02 -0400205static __be32 nfsd_check_obj_isreg(struct svc_fh *fh)
206{
David Howells2b0143b2015-03-17 22:25:59 +0000207 umode_t mode = d_inode(fh->fh_dentry)->i_mode;
J. Bruce Fieldsaadab6c2011-08-15 16:55:02 -0400208
209 if (S_ISREG(mode))
210 return nfs_ok;
211 if (S_ISDIR(mode))
212 return nfserr_isdir;
213 /*
214 * Using err_symlink as our catch-all case may look odd; but
215 * there's no other obvious error for this case in 4.0, and we
216 * happen to know that it will cause the linux v4 client to do
217 * the right thing on attempts to open something other than a
218 * regular file.
219 */
220 return nfserr_symlink;
221}
222
J. Bruce Fieldsbbc9c362013-03-22 18:03:49 -0400223static void nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh *resfh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
J. Bruce Fieldsbbc9c362013-03-22 18:03:49 -0400225 if (nfsd4_has_session(cstate))
226 return;
227 fh_copy_shallow(&open->op_openowner->oo_owner.so_replay.rp_openfh,
228 &resfh->fh_handle);
229}
230
231static __be32
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500232do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh **resfh)
J. Bruce Fieldsbbc9c362013-03-22 18:03:49 -0400233{
234 struct svc_fh *current_fh = &cstate->current_fh;
Neil Brown7007c902012-12-07 15:40:55 -0500235 int accmode;
Al Virob37ad282006-10-19 23:28:59 -0700236 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500238 *resfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
239 if (!*resfh)
J. Bruce Fields59deeb92012-01-27 16:49:55 -0500240 return nfserr_jukebox;
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500241 fh_init(*resfh, NFS4_FHSIZE);
zhengbin500c24812019-12-25 11:19:35 +0800242 open->op_truncate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 if (open->op_create) {
Benny Halevy79fb54a2009-04-03 08:29:17 +0300245 /* FIXME: check session persistence and pnfs flags.
246 * The nfsv4.1 spec requires the following semantics:
247 *
248 * Persistent | pNFS | Server REQUIRED | Client Allowed
249 * Reply Cache | server | |
250 * -------------+--------+-----------------+--------------------
251 * no | no | EXCLUSIVE4_1 | EXCLUSIVE4_1
252 * | | | (SHOULD)
253 * | | and EXCLUSIVE4 | or EXCLUSIVE4
254 * | | | (SHOULD NOT)
255 * no | yes | EXCLUSIVE4_1 | EXCLUSIVE4_1
256 * yes | no | GUARDED4 | GUARDED4
257 * yes | yes | GUARDED4 | GUARDED4
258 */
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /*
261 * Note: create modes (UNCHECKED,GUARDED...) are the same
Mi Jinlongac6721a2011-04-20 17:06:25 +0800262 * in NFSv4 as in v3 except EXCLUSIVE4_1.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 */
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400264 current->fs->umask = open->op_umask;
Chuck Lever1708e502020-11-16 17:45:04 -0500265 status = do_nfsd_create(rqstp, current_fh, open->op_fname,
266 open->op_fnamelen, &open->op_iattr,
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500267 *resfh, open->op_createmode,
Jeff Layton749997e2007-07-31 00:37:51 -0700268 (u32 *)open->op_verf.data,
J. Bruce Fields856121b2011-10-13 11:37:11 -0400269 &open->op_truncate, &open->op_created);
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400270 current->fs->umask = 0;
Jeff Layton749997e2007-07-31 00:37:51 -0700271
David Quigley18032ca2013-05-02 13:19:10 -0400272 if (!status && open->op_label.len)
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500273 nfsd4_security_inode_setsecctx(*resfh, &open->op_label, open->op_bmval);
David Quigley18032ca2013-05-02 13:19:10 -0400274
J. Bruce Fields99f88722009-02-02 15:12:27 -0500275 /*
Kinglong Meeead8fb82015-07-30 21:55:02 +0800276 * Following rfc 3530 14.2.16, and rfc 5661 18.16.4
277 * use the returned bitmask to indicate which attributes
278 * we used to store the verifier:
Jeff Layton749997e2007-07-31 00:37:51 -0700279 */
Kinglong Meeead8fb82015-07-30 21:55:02 +0800280 if (nfsd_create_is_exclusive(open->op_createmode) && status == 0)
281 open->op_bmval[1] |= (FATTR4_WORD1_TIME_ACCESS |
282 FATTR4_WORD1_TIME_MODIFY);
J. Bruce Fields43357232014-01-24 18:04:40 -0500283 } else
284 /*
285 * Note this may exit with the parent still locked.
286 * We will hold the lock until nfsd4_open's final
287 * lookup, to prevent renames or unlinks until we've had
288 * a chance to an acquire a delegation if appropriate.
289 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 status = nfsd_lookup(rqstp, current_fh,
Chuck Lever1708e502020-11-16 17:45:04 -0500291 open->op_fname, open->op_fnamelen, *resfh);
J. Bruce Fieldsaf858522006-11-08 17:44:39 -0800292 if (status)
293 goto out;
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500294 status = nfsd_check_obj_isreg(*resfh);
J. Bruce Fields9dc4e6c2012-04-09 18:06:49 -0400295 if (status)
296 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Yu Zhiguo9208faf2009-07-06 17:24:16 +0800298 if (is_create_with_attrs(open) && open->op_acl != NULL)
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500299 do_set_nfs4_acl(rqstp, *resfh, open->op_acl, open->op_bmval);
Yu Zhiguo9208faf2009-07-06 17:24:16 +0800300
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500301 nfsd4_set_open_owner_reply_cache(cstate, open, *resfh);
Neil Brown7007c902012-12-07 15:40:55 -0500302 accmode = NFSD_MAY_NOP;
J. Bruce Fields89f6c332013-06-19 15:47:37 -0400303 if (open->op_created ||
304 open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
Neil Brown7007c902012-12-07 15:40:55 -0500305 accmode |= NFSD_MAY_OWNER_OVERRIDE;
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500306 status = do_open_permission(rqstp, *resfh, open, accmode);
J. Bruce Fields41fd1e42012-01-27 16:26:02 -0500307 set_change_info(&open->op_cinfo, current_fh);
J. Bruce Fieldsaf858522006-11-08 17:44:39 -0800308out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return status;
310}
311
Al Virob37ad282006-10-19 23:28:59 -0700312static __be32
J. Bruce Fieldsbbc9c362013-03-22 18:03:49 -0400313do_open_fhandle(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
J. Bruce Fieldsbbc9c362013-03-22 18:03:49 -0400315 struct svc_fh *current_fh = &cstate->current_fh;
Al Virob37ad282006-10-19 23:28:59 -0700316 __be32 status;
J. Bruce Fields9f415eb2013-05-03 16:09:09 -0400317 int accmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* We don't know the target directory, and therefore can not
320 * set the change info
321 */
322
323 memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
324
J. Bruce Fieldsbbc9c362013-03-22 18:03:49 -0400325 nfsd4_set_open_owner_reply_cache(cstate, open, current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
328 (open->op_iattr.ia_size == 0);
J. Bruce Fields9f415eb2013-05-03 16:09:09 -0400329 /*
330 * In the delegation case, the client is telling us about an
331 * open that it *already* performed locally, some time ago. We
332 * should let it succeed now if possible.
333 *
334 * In the case of a CLAIM_FH open, on the other hand, the client
335 * may be counting on us to enforce permissions (the Linux 4.1
336 * client uses this for normal opens, for example).
337 */
338 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH)
339 accmode = NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
J. Bruce Fields9f415eb2013-05-03 16:09:09 -0400341 status = do_open_permission(rqstp, current_fh, open, accmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 return status;
344}
345
Andy Adamson60adfc52009-04-03 08:28:50 +0300346static void
347copy_clientid(clientid_t *clid, struct nfsd4_session *session)
348{
349 struct nfsd4_sessionid *sid =
350 (struct nfsd4_sessionid *)session->se_sessionid.data;
351
352 clid->cl_boot = sid->clientid.cl_boot;
353 clid->cl_id = sid->clientid.cl_id;
354}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
J.Bruce Fields71911552006-12-13 00:35:29 -0800356static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800357nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200358 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200360 struct nfsd4_open *open = &u->open;
Al Virob37ad282006-10-19 23:28:59 -0700361 __be32 status;
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500362 struct svc_fh *resfh = NULL;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +0300363 struct net *net = SVC_NET(rqstp);
364 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields03f318c2018-06-08 12:28:47 -0400365 bool reclaim = false;
Andy Adamson66689582009-04-03 08:28:45 +0300366
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400367 dprintk("NFSD: nfsd4_open filename %.*s op_openowner %p\n",
Chuck Lever1708e502020-11-16 17:45:04 -0500368 (int)open->op_fnamelen, open->op_fname,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400369 open->op_openowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /* This check required by spec. */
372 if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
373 return nfserr_inval;
374
zhengbin500c24812019-12-25 11:19:35 +0800375 open->op_created = false;
Mi Jinlongab1350b2011-07-14 15:06:26 +0800376 /*
377 * RFC5661 18.51.3
378 * Before RECLAIM_COMPLETE done, server should deny new lock
379 */
380 if (nfsd4_has_session(cstate) &&
Jeff Laytona52d7262012-03-21 09:52:02 -0400381 !test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
382 &cstate->session->se_client->cl_flags) &&
Mi Jinlongab1350b2011-07-14 15:06:26 +0800383 open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
384 return nfserr_grace;
385
Andy Adamson60adfc52009-04-03 08:28:50 +0300386 if (nfsd4_has_session(cstate))
387 copy_clientid(&open->op_clientid, cstate->session);
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /* check seqid for replay. set nfs4_owner */
Kinglong Mee6cd22662015-07-13 17:32:05 +0800390 status = nfsd4_process_open1(cstate, open, nn);
Al Viroa90b0612006-10-19 23:29:03 -0700391 if (status == nfserr_replay_me) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400392 struct nfs4_replay *rp = &open->op_openowner->oo_owner.so_replay;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800393 fh_put(&cstate->current_fh);
J. Bruce Fieldsa4773c02009-02-02 17:23:10 -0500394 fh_copy_shallow(&cstate->current_fh.fh_handle,
395 &rp->rp_openfh);
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200396 status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (status)
398 dprintk("nfsd4_open: replay failed"
399 " restoring previous filehandle\n");
400 else
Al Viroa90b0612006-10-19 23:29:03 -0700401 status = nfserr_replay_me;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403 if (status)
404 goto out;
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800405 if (open->op_xdr_error) {
406 status = open->op_xdr_error;
407 goto out;
408 }
J. Bruce Fieldsfb553c02006-01-18 17:43:36 -0800409
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800410 status = nfsd4_check_open_attributes(rqstp, cstate, open);
411 if (status)
412 goto out;
413
J. Bruce Fieldsfb553c02006-01-18 17:43:36 -0800414 /* Openowner is now set, so sequence id will get bumped. Now we need
415 * these checks before we do any creates: */
J. Bruce Fieldscbd0d512006-02-07 12:58:32 -0800416 status = nfserr_grace;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -0400417 if (opens_in_grace(net) && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fieldscbd0d512006-02-07 12:58:32 -0800418 goto out;
419 status = nfserr_no_grace;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -0400420 if (!opens_in_grace(net) && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fieldscbd0d512006-02-07 12:58:32 -0800421 goto out;
J. Bruce Fieldsfb553c02006-01-18 17:43:36 -0800422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 switch (open->op_claim_type) {
NeilBrown0dd3c192005-06-23 22:02:56 -0700424 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 case NFS4_OPEN_CLAIM_NULL:
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500426 status = do_open_lookup(rqstp, cstate, open, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (status)
428 goto out;
429 break;
430 case NFS4_OPEN_CLAIM_PREVIOUS:
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +0300431 status = nfs4_check_open_reclaim(&open->op_clientid,
Trond Myklebust0fe492d2014-06-30 11:48:47 -0400432 cstate, nn);
Mi Jinlong0cf99b92011-11-23 10:48:40 +0800433 if (status)
434 goto out;
Jeff Laytonba5378b2014-05-30 09:09:28 -0400435 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fields03f318c2018-06-08 12:28:47 -0400436 reclaim = true;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500437 fallthrough;
J. Bruce Fields8b289b22011-10-19 11:52:12 -0400438 case NFS4_OPEN_CLAIM_FH:
439 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
J. Bruce Fieldsbbc9c362013-03-22 18:03:49 -0400440 status = do_open_fhandle(rqstp, cstate, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (status)
442 goto out;
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500443 resfh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 break;
J. Bruce Fields8b289b22011-10-19 11:52:12 -0400445 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
J. Bruce Fields2fdada02007-07-27 16:10:37 -0400447 dprintk("NFSD: unsupported OPEN claim type %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 open->op_claim_type);
449 status = nfserr_notsupp;
450 goto out;
451 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -0400452 dprintk("NFSD: Invalid OPEN claim type %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 open->op_claim_type);
454 status = nfserr_inval;
455 goto out;
456 }
457 /*
458 * nfsd4_process_open2() does the actual opening of the file. If
459 * successful, it (1) truncates the file if open->op_truncate was
460 * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
461 */
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500462 status = nfsd4_process_open2(rqstp, resfh, open);
Jeff Laytonb3fbfe02014-07-29 21:37:44 -0400463 WARN(status && open->op_created,
464 "nfsd4_process_open2 failed to open newly-created file! status=%u\n",
465 be32_to_cpu(status));
J. Bruce Fields03f318c2018-06-08 12:28:47 -0400466 if (reclaim && !status)
467 nn->somebody_reclaimed = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468out:
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500469 if (resfh && resfh != &cstate->current_fh) {
470 fh_dup2(&cstate->current_fh, resfh);
471 fh_put(resfh);
472 kfree(resfh);
473 }
Jeff Layton42297892015-03-23 10:53:44 -0400474 nfsd4_cleanup_open_state(cstate, open);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -0400475 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return status;
477}
478
479/*
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800480 * OPEN is the only seqid-mutating operation whose decoding can fail
481 * with a seqid-mutating error (specifically, decoding of user names in
482 * the attributes). Therefore we have to do some processing to look up
483 * the stateowner so that we can bump the seqid.
484 */
485static __be32 nfsd4_open_omfg(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_op *op)
486{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200487 struct nfsd4_open *open = &op->u.open;
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800488
489 if (!seqid_mutating_err(ntohl(op->status)))
490 return op->status;
491 if (nfsd4_has_session(cstate))
492 return op->status;
493 open->op_xdr_error = op->status;
Christoph Hellwigeb698532017-05-08 20:58:35 +0200494 return nfsd4_open(rqstp, cstate, &op->u);
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800495}
496
497/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 * filehandle-manipulating ops.
499 */
J.Bruce Fields71911552006-12-13 00:35:29 -0800500static __be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800501nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200502 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200504 u->getfh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return nfs_ok;
506}
507
J.Bruce Fields71911552006-12-13 00:35:29 -0800508static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800509nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200510 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200512 struct nfsd4_putfh *putfh = &u->putfh;
Olga Kornievskaiab9e86382019-10-07 10:56:48 -0400513 __be32 ret;
Christoph Hellwigeb698532017-05-08 20:58:35 +0200514
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800515 fh_put(&cstate->current_fh);
516 cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
517 memcpy(&cstate->current_fh.fh_handle.fh_base, putfh->pf_fhval,
518 putfh->pf_fhlen);
Olga Kornievskaiab9e86382019-10-07 10:56:48 -0400519 ret = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
520#ifdef CONFIG_NFSD_V4_2_INTER_SSC
521 if (ret == nfserr_stale && putfh->no_verify) {
522 SET_FH_FLAG(&cstate->current_fh, NFSD4_FH_FOREIGN);
523 ret = 0;
524 }
525#endif
526 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
J.Bruce Fields71911552006-12-13 00:35:29 -0800529static __be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800530nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200531 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Al Virob37ad282006-10-19 23:28:59 -0700533 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800535 fh_put(&cstate->current_fh);
J. Bruce Fieldsdf547ef2007-07-17 04:04:43 -0700536 status = exp_pseudoroot(rqstp, &cstate->current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return status;
538}
539
J.Bruce Fields71911552006-12-13 00:35:29 -0800540static __be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800541nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200542 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800544 if (!cstate->save_fh.fh_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return nfserr_restorefh;
546
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800547 fh_dup2(&cstate->current_fh, &cstate->save_fh);
Olga Kornievskaia51100d22018-09-13 13:58:24 -0400548 if (HAS_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG)) {
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +0100549 memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
Olga Kornievskaia51100d22018-09-13 13:58:24 -0400550 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +0100551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return nfs_ok;
553}
554
J.Bruce Fields71911552006-12-13 00:35:29 -0800555static __be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800556nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200557 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800559 fh_dup2(&cstate->save_fh, &cstate->current_fh);
Olga Kornievskaia51100d22018-09-13 13:58:24 -0400560 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG)) {
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +0100561 memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t));
Olga Kornievskaia51100d22018-09-13 13:58:24 -0400562 SET_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG);
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +0100563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return nfs_ok;
565}
566
567/*
568 * misc nfsv4 ops
569 */
J.Bruce Fields71911552006-12-13 00:35:29 -0800570static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800571nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200572 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200574 struct nfsd4_access *access = &u->access;
Frank van der Lindenc11d7fd2020-06-23 22:39:24 +0000575 u32 access_full;
Christoph Hellwigeb698532017-05-08 20:58:35 +0200576
Frank van der Lindenc11d7fd2020-06-23 22:39:24 +0000577 access_full = NFS3_ACCESS_FULL;
578 if (cstate->minorversion >= 2)
579 access_full |= NFS4_ACCESS_XALIST | NFS4_ACCESS_XAREAD |
580 NFS4_ACCESS_XAWRITE;
581
582 if (access->ac_req_access & ~access_full)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return nfserr_inval;
584
585 access->ac_resp_access = access->ac_req_access;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800586 return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
587 &access->ac_supported);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +0300590static void gen_boot_verifier(nfs4_verifier *verifier, struct net *net)
Chuck Leverab4684d2012-03-02 17:13:50 -0500591{
Trond Myklebust27c438f2019-09-02 13:02:56 -0400592 __be32 *verf = (__be32 *)verifier->data;
Chuck Leverab4684d2012-03-02 17:13:50 -0500593
Trond Myklebust27c438f2019-09-02 13:02:56 -0400594 BUILD_BUG_ON(2*sizeof(*verf) != sizeof(verifier->data));
595
596 nfsd_copy_boot_verifier(verf, net_generic(net, nfsd_net_id));
Chuck Leverab4684d2012-03-02 17:13:50 -0500597}
598
J.Bruce Fields71911552006-12-13 00:35:29 -0800599static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800600nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200601 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200603 struct nfsd4_commit *commit = &u->commit;
604
J. Bruce Fields75c096f2011-08-15 18:39:32 -0400605 return nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
Trond Myklebust524ff1a2020-01-06 13:40:36 -0500606 commit->co_count,
607 (__be32 *)commit->co_verf.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Al Virob37ad282006-10-19 23:28:59 -0700610static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800611nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200612 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200614 struct nfsd4_create *create = &u->create;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 struct svc_fh resfh;
Al Virob37ad282006-10-19 23:28:59 -0700616 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 dev_t rdev;
618
619 fh_init(&resfh, NFS4_FHSIZE);
620
J. Bruce Fieldsfa081392016-07-21 16:00:12 -0400621 status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (status)
623 return status;
624
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800625 status = check_attr_support(rqstp, cstate, create->cr_bmval,
626 nfsd_attrmask);
627 if (status)
628 return status;
629
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400630 current->fs->umask = create->cr_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 switch (create->cr_type) {
632 case NF4LNK:
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800633 status = nfsd_symlink(rqstp, &cstate->current_fh,
634 create->cr_name, create->cr_namelen,
Kinglong Mee1e444f52014-07-01 17:48:02 +0800635 create->cr_data, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 break;
637
638 case NF4BLK:
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400639 status = nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
641 if (MAJOR(rdev) != create->cr_specdata1 ||
642 MINOR(rdev) != create->cr_specdata2)
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400643 goto out_umask;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800644 status = nfsd_create(rqstp, &cstate->current_fh,
645 create->cr_name, create->cr_namelen,
646 &create->cr_iattr, S_IFBLK, rdev, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 break;
648
649 case NF4CHR:
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400650 status = nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
652 if (MAJOR(rdev) != create->cr_specdata1 ||
653 MINOR(rdev) != create->cr_specdata2)
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400654 goto out_umask;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800655 status = nfsd_create(rqstp, &cstate->current_fh,
656 create->cr_name, create->cr_namelen,
657 &create->cr_iattr,S_IFCHR, rdev, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 break;
659
660 case NF4SOCK:
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800661 status = nfsd_create(rqstp, &cstate->current_fh,
662 create->cr_name, create->cr_namelen,
663 &create->cr_iattr, S_IFSOCK, 0, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 break;
665
666 case NF4FIFO:
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800667 status = nfsd_create(rqstp, &cstate->current_fh,
668 create->cr_name, create->cr_namelen,
669 &create->cr_iattr, S_IFIFO, 0, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 break;
671
672 case NF4DIR:
673 create->cr_iattr.ia_valid &= ~ATTR_SIZE;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800674 status = nfsd_create(rqstp, &cstate->current_fh,
675 create->cr_name, create->cr_namelen,
676 &create->cr_iattr, S_IFDIR, 0, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 break;
678
679 default:
680 status = nfserr_badtype;
681 }
682
Yu Zhiguo9208faf2009-07-06 17:24:16 +0800683 if (status)
684 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
David Quigley18032ca2013-05-02 13:19:10 -0400686 if (create->cr_label.len)
687 nfsd4_security_inode_setsecctx(&resfh, &create->cr_label, create->cr_bmval);
688
Yu Zhiguo9208faf2009-07-06 17:24:16 +0800689 if (create->cr_acl != NULL)
690 do_set_nfs4_acl(rqstp, &resfh, create->cr_acl,
691 create->cr_bmval);
692
693 fh_unlock(&cstate->current_fh);
694 set_change_info(&create->cr_cinfo, &cstate->current_fh);
695 fh_dup2(&cstate->current_fh, &resfh);
696out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 fh_put(&resfh);
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400698out_umask:
699 current->fs->umask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return status;
701}
702
J.Bruce Fields71911552006-12-13 00:35:29 -0800703static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800704nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200705 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200707 struct nfsd4_getattr *getattr = &u->getattr;
Al Virob37ad282006-10-19 23:28:59 -0700708 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200710 status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (status)
712 return status;
713
714 if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
715 return nfserr_inval;
716
J. Bruce Fields916d2d82016-10-18 14:18:40 -0400717 getattr->ga_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
718 getattr->ga_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
719 getattr->ga_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800721 getattr->ga_fhp = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return nfs_ok;
723}
724
J.Bruce Fields71911552006-12-13 00:35:29 -0800725static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800726nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200727 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200729 struct nfsd4_link *link = &u->link;
J. Bruce Fields95424462017-09-15 16:02:52 -0400730 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800732 status = nfsd_link(rqstp, &cstate->current_fh,
733 link->li_name, link->li_namelen, &cstate->save_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (!status)
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800735 set_change_info(&link->li_cinfo, &cstate->current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return status;
737}
738
J. Bruce Fields0ff7ab42010-12-16 10:06:27 -0500739static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 struct svc_fh tmp_fh;
Al Virob37ad282006-10-19 23:28:59 -0700742 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 fh_init(&tmp_fh, NFS4_FHSIZE);
J. Bruce Fieldsdf547ef2007-07-17 04:04:43 -0700745 ret = exp_pseudoroot(rqstp, &tmp_fh);
746 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return ret;
J. Bruce Fields0ff7ab42010-12-16 10:06:27 -0500748 if (tmp_fh.fh_dentry == fh->fh_dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 fh_put(&tmp_fh);
750 return nfserr_noent;
751 }
752 fh_put(&tmp_fh);
J. Bruce Fields0ff7ab42010-12-16 10:06:27 -0500753 return nfsd_lookup(rqstp, fh, "..", 2, fh);
754}
755
756static __be32
757nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200758 union nfsd4_op_u *u)
J. Bruce Fields0ff7ab42010-12-16 10:06:27 -0500759{
760 return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
J.Bruce Fields71911552006-12-13 00:35:29 -0800763static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800764nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200765 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800767 return nfsd_lookup(rqstp, &cstate->current_fh,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200768 u->lookup.lo_name, u->lookup.lo_len,
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800769 &cstate->current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
J.Bruce Fields71911552006-12-13 00:35:29 -0800772static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800773nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200774 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200776 struct nfsd4_read *read = &u->read;
Al Virob37ad282006-10-19 23:28:59 -0700777 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Jeff Layton5c4583b2019-08-18 14:18:54 -0400779 read->rd_nf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (read->rd_offset >= OFFSET_MAX)
781 return nfserr_inval;
782
Chuck Lever87c59422018-03-28 13:29:11 -0400783 trace_nfsd_read_start(rqstp, &cstate->current_fh,
784 read->rd_offset, read->rd_length);
785
J. Bruce Fields9b3234b2012-12-04 18:03:46 -0500786 /*
787 * If we do a zero copy read, then a client will see read data
788 * that reflects the state of the file *after* performing the
789 * following compound.
790 *
791 * To ensure proper ordering, we therefore turn off zero copy if
792 * the client wants us to do more in this compound:
793 */
794 if (!nfsd4_last_compound_op(rqstp))
Jeff Layton779fb0f2014-11-19 07:51:18 -0500795 clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
J. Bruce Fields9b3234b2012-12-04 18:03:46 -0500796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /* check stateid */
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +0100798 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
799 &read->rd_stateid, RD_STATE,
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400800 &read->rd_nf, NULL);
Christoph Hellwigaf90f702015-06-18 16:45:00 +0200801 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
803 goto out;
804 }
805 status = nfs_ok;
806out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 read->rd_rqstp = rqstp;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800808 read->rd_fhp = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return status;
810}
811
J. Bruce Fields34b1744c2017-05-05 17:09:37 -0400812
813static void
814nfsd4_read_release(union nfsd4_op_u *u)
815{
Jeff Layton5c4583b2019-08-18 14:18:54 -0400816 if (u->read.rd_nf)
817 nfsd_file_put(u->read.rd_nf);
Chuck Lever87c59422018-03-28 13:29:11 -0400818 trace_nfsd_read_done(u->read.rd_rqstp, u->read.rd_fhp,
819 u->read.rd_offset, u->read.rd_length);
J. Bruce Fields34b1744c2017-05-05 17:09:37 -0400820}
821
J.Bruce Fields71911552006-12-13 00:35:29 -0800822static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800823nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200824 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200826 struct nfsd4_readdir *readdir = &u->readdir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 u64 cookie = readdir->rd_cookie;
828 static const nfs4_verifier zeroverf;
829
830 /* no need to check permission - this will be done in nfsd_readdir() */
831
832 if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
833 return nfserr_inval;
834
J. Bruce Fields916d2d82016-10-18 14:18:40 -0400835 readdir->rd_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
836 readdir->rd_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
837 readdir->rd_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Bernd Schubert832023b2011-08-08 17:38:08 +0200839 if ((cookie == 1) || (cookie == 2) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
841 return nfserr_bad_cookie;
842
843 readdir->rd_rqstp = rqstp;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800844 readdir->rd_fhp = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return nfs_ok;
846}
847
J.Bruce Fields71911552006-12-13 00:35:29 -0800848static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800849nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200850 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200852 u->readlink.rl_rqstp = rqstp;
853 u->readlink.rl_fhp = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return nfs_ok;
855}
856
J.Bruce Fields71911552006-12-13 00:35:29 -0800857static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800858nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200859 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200861 struct nfsd4_remove *remove = &u->remove;
Al Virob37ad282006-10-19 23:28:59 -0700862 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -0400864 if (opens_in_grace(SVC_NET(rqstp)))
NeilBrownc815afc2005-06-23 22:03:00 -0700865 return nfserr_grace;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800866 status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
867 remove->rm_name, remove->rm_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (!status) {
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800869 fh_unlock(&cstate->current_fh);
870 set_change_info(&remove->rm_cinfo, &cstate->current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872 return status;
873}
874
J.Bruce Fields71911552006-12-13 00:35:29 -0800875static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800876nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200877 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200879 struct nfsd4_rename *rename = &u->rename;
J. Bruce Fields95424462017-09-15 16:02:52 -0400880 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
J. Bruce Fieldsf8f71d02018-11-06 11:47:20 -0500882 if (opens_in_grace(SVC_NET(rqstp)))
NeilBrownc815afc2005-06-23 22:03:00 -0700883 return nfserr_grace;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800884 status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
885 rename->rn_snamelen, &cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 rename->rn_tname, rename->rn_tnamelen);
J. Bruce Fields2a6cf942013-04-30 15:28:51 -0400887 if (status)
888 return status;
889 set_change_info(&rename->rn_sinfo, &cstate->current_fh);
890 set_change_info(&rename->rn_tinfo, &cstate->save_fh);
891 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
J.Bruce Fields71911552006-12-13 00:35:29 -0800894static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -0700895nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200896 union nfsd4_op_u *u)
Andy Adamsondcb488a32007-07-17 04:04:51 -0700897{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200898 struct nfsd4_secinfo *secinfo = &u->secinfo;
Andy Adamsondcb488a32007-07-17 04:04:51 -0700899 struct svc_export *exp;
900 struct dentry *dentry;
901 __be32 err;
902
J. Bruce Fields29a78a32011-04-09 11:28:53 -0400903 err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
904 if (err)
905 return err;
Andy Adamsondcb488a32007-07-17 04:04:51 -0700906 err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
907 secinfo->si_name, secinfo->si_namelen,
908 &exp, &dentry);
909 if (err)
910 return err;
J. Bruce Fields2f6fc052016-03-02 16:36:21 -0800911 fh_unlock(&cstate->current_fh);
David Howells2b0143b2015-03-17 22:25:59 +0000912 if (d_really_is_negative(dentry)) {
Andy Adamsondcb488a32007-07-17 04:04:51 -0700913 exp_put(exp);
914 err = nfserr_noent;
915 } else
916 secinfo->si_exp = exp;
917 dput(dentry);
J. Bruce Fields56560b92010-12-16 09:57:15 -0500918 if (cstate->minorversion)
919 /* See rfc 5661 section 2.6.3.1.1.8 */
920 fh_put(&cstate->current_fh);
Andy Adamsondcb488a32007-07-17 04:04:51 -0700921 return err;
922}
923
924static __be32
J. Bruce Fields04f4ad12010-12-16 09:51:13 -0500925nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200926 union nfsd4_op_u *u)
J. Bruce Fields04f4ad12010-12-16 09:51:13 -0500927{
928 __be32 err;
929
Christoph Hellwigeb698532017-05-08 20:58:35 +0200930 switch (u->secinfo_no_name.sin_style) {
J. Bruce Fields04f4ad12010-12-16 09:51:13 -0500931 case NFS4_SECINFO_STYLE4_CURRENT_FH:
932 break;
933 case NFS4_SECINFO_STYLE4_PARENT:
934 err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
935 if (err)
936 return err;
937 break;
938 default:
939 return nfserr_inval;
940 }
Kinglong Meebf18f162014-06-10 22:06:44 +0800941
Christoph Hellwigeb698532017-05-08 20:58:35 +0200942 u->secinfo_no_name.sin_exp = exp_get(cstate->current_fh.fh_export);
J. Bruce Fields04f4ad12010-12-16 09:51:13 -0500943 fh_put(&cstate->current_fh);
944 return nfs_ok;
945}
946
J. Bruce Fields34b1744c2017-05-05 17:09:37 -0400947static void
948nfsd4_secinfo_release(union nfsd4_op_u *u)
949{
950 if (u->secinfo.si_exp)
951 exp_put(u->secinfo.si_exp);
952}
953
Eryu Guanec572b92017-09-29 15:01:10 +0800954static void
955nfsd4_secinfo_no_name_release(union nfsd4_op_u *u)
956{
957 if (u->secinfo_no_name.sin_exp)
958 exp_put(u->secinfo_no_name.sin_exp);
959}
960
J. Bruce Fields04f4ad12010-12-16 09:51:13 -0500961static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800962nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200963 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200965 struct nfsd4_setattr *setattr = &u->setattr;
Al Virob37ad282006-10-19 23:28:59 -0700966 __be32 status = nfs_ok;
Al Viro96f6f982012-04-12 23:47:00 -0400967 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
Christoph Hellwigaf90f702015-06-18 16:45:00 +0200970 status = nfs4_preprocess_stateid_op(rqstp, cstate,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +0100971 &cstate->current_fh, &setattr->sa_stateid,
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400972 WR_STATE, NULL, NULL);
J. Bruce Fields375c5542006-01-18 17:43:33 -0800973 if (status) {
Greg Banks3e3b4802006-10-02 02:17:41 -0700974 dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
J. Bruce Fields375c5542006-01-18 17:43:33 -0800975 return status;
976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
Al Viro96f6f982012-04-12 23:47:00 -0400978 err = fh_want_write(&cstate->current_fh);
979 if (err)
980 return nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 status = nfs_ok;
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800982
983 status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
984 nfsd_attrmask);
985 if (status)
986 goto out;
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (setattr->sa_acl != NULL)
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800989 status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
990 setattr->sa_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (status)
Dave Hansen18f335a2008-02-15 14:37:38 -0800992 goto out;
David Quigley18032ca2013-05-02 13:19:10 -0400993 if (setattr->sa_label.len)
994 status = nfsd4_set_nfs4_label(rqstp, &cstate->current_fh,
995 &setattr->sa_label);
996 if (status)
997 goto out;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800998 status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
Arnd Bergmann2a1aa482019-11-03 17:50:18 +0100999 0, (time64_t)0);
Dave Hansen18f335a2008-02-15 14:37:38 -08001000out:
Al Virobad0dcf2011-11-23 12:03:18 -05001001 fh_drop_write(&cstate->current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return status;
1003}
1004
J.Bruce Fields71911552006-12-13 00:35:29 -08001005static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08001006nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001007 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001009 struct nfsd4_write *write = &u->write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 stateid_t *stateid = &write->wr_stateid;
Jeff Layton5c4583b2019-08-18 14:18:54 -04001011 struct nfsd_file *nf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07001012 __be32 status = nfs_ok;
David Shaw31dec252009-03-05 20:16:14 -05001013 unsigned long cnt;
J. Bruce Fieldsffe11372012-11-15 14:52:19 -05001014 int nvecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (write->wr_offset >= OFFSET_MAX)
1017 return nfserr_inval;
1018
Chuck Leverd890be12018-03-27 10:53:27 -04001019 cnt = write->wr_buflen;
1020 trace_nfsd_write_start(rqstp, &cstate->current_fh,
1021 write->wr_offset, cnt);
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01001022 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001023 stateid, WR_STATE, &nf, NULL);
J. Bruce Fields375c5542006-01-18 17:43:33 -08001024 if (status) {
1025 dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
1026 return status;
1027 }
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 write->wr_how_written = write->wr_stable_how;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Chuck Leverc1346a122020-11-03 11:54:23 -05001031 nvecs = svc_fill_write_vector(rqstp, write->wr_payload.pages,
1032 write->wr_payload.head, write->wr_buflen);
J. Bruce Fieldsffe11372012-11-15 14:52:19 -05001033 WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec));
1034
Trond Myklebust16f8f892020-01-06 13:40:29 -05001035 status = nfsd_vfs_write(rqstp, &cstate->current_fh, nf,
Christoph Hellwigaf90f702015-06-18 16:45:00 +02001036 write->wr_offset, rqstp->rq_vec, nvecs, &cnt,
Trond Myklebust19e06632020-01-06 13:40:37 -05001037 write->wr_how_written,
1038 (__be32 *)write->wr_verifier.data);
Jeff Layton5c4583b2019-08-18 14:18:54 -04001039 nfsd_file_put(nf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
David Shaw31dec252009-03-05 20:16:14 -05001041 write->wr_bytes_written = cnt;
Chuck Leverd890be12018-03-27 10:53:27 -04001042 trace_nfsd_write_done(rqstp, &cstate->current_fh,
1043 write->wr_offset, cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
Anna Schumaker24bab492014-09-26 13:58:27 -04001047static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001048nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Jeff Layton5c4583b2019-08-18 14:18:54 -04001049 stateid_t *src_stateid, struct nfsd_file **src,
1050 stateid_t *dst_stateid, struct nfsd_file **dst)
Christoph Hellwigffa01602015-12-03 12:59:52 +01001051{
Christoph Hellwigffa01602015-12-03 12:59:52 +01001052 __be32 status;
1053
Scott Mayhew01310bb2018-11-08 11:11:36 -05001054 if (!cstate->save_fh.fh_dentry)
1055 return nfserr_nofilehandle;
1056
Christoph Hellwigffa01602015-12-03 12:59:52 +01001057 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->save_fh,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001058 src_stateid, RD_STATE, src, NULL);
Christoph Hellwigffa01602015-12-03 12:59:52 +01001059 if (status) {
1060 dprintk("NFSD: %s: couldn't process src stateid!\n", __func__);
1061 goto out;
1062 }
1063
1064 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001065 dst_stateid, WR_STATE, dst, NULL);
Christoph Hellwigffa01602015-12-03 12:59:52 +01001066 if (status) {
1067 dprintk("NFSD: %s: couldn't process dst stateid!\n", __func__);
1068 goto out_put_src;
1069 }
1070
1071 /* fix up for NFS-specific error code */
Jeff Layton5c4583b2019-08-18 14:18:54 -04001072 if (!S_ISREG(file_inode((*src)->nf_file)->i_mode) ||
1073 !S_ISREG(file_inode((*dst)->nf_file)->i_mode)) {
Christoph Hellwigffa01602015-12-03 12:59:52 +01001074 status = nfserr_wrong_type;
1075 goto out_put_dst;
1076 }
1077
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001078out:
1079 return status;
1080out_put_dst:
Jeff Layton5c4583b2019-08-18 14:18:54 -04001081 nfsd_file_put(*dst);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001082out_put_src:
Jeff Layton5c4583b2019-08-18 14:18:54 -04001083 nfsd_file_put(*src);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001084 goto out;
1085}
1086
1087static __be32
1088nfsd4_clone(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001089 union nfsd4_op_u *u)
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001090{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001091 struct nfsd4_clone *clone = &u->clone;
Jeff Layton5c4583b2019-08-18 14:18:54 -04001092 struct nfsd_file *src, *dst;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001093 __be32 status;
1094
1095 status = nfsd4_verify_copy(rqstp, cstate, &clone->cl_src_stateid, &src,
1096 &clone->cl_dst_stateid, &dst);
1097 if (status)
1098 goto out;
1099
Trond Myklebustb66ae6d2020-01-06 13:40:32 -05001100 status = nfsd4_clone_file_range(src, clone->cl_src_pos,
1101 dst, clone->cl_dst_pos, clone->cl_count,
Trond Myklebusta25e37262019-11-27 17:05:51 -05001102 EX_ISSYNC(cstate->current_fh.fh_export));
Christoph Hellwigffa01602015-12-03 12:59:52 +01001103
Jeff Layton5c4583b2019-08-18 14:18:54 -04001104 nfsd_file_put(dst);
1105 nfsd_file_put(src);
Christoph Hellwigffa01602015-12-03 12:59:52 +01001106out:
1107 return status;
1108}
1109
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001110void nfs4_put_copy(struct nfsd4_copy *copy)
1111{
1112 if (!refcount_dec_and_test(&copy->refcount))
1113 return;
1114 kfree(copy);
1115}
1116
1117static bool
1118check_and_set_stop_copy(struct nfsd4_copy *copy)
1119{
1120 bool value;
1121
1122 spin_lock(&copy->cp_clp->async_lock);
1123 value = copy->stopped;
1124 if (!copy->stopped)
1125 copy->stopped = true;
1126 spin_unlock(&copy->cp_clp->async_lock);
1127 return value;
1128}
1129
1130static void nfsd4_stop_copy(struct nfsd4_copy *copy)
1131{
1132 /* only 1 thread should stop the copy */
1133 if (!check_and_set_stop_copy(copy))
1134 kthread_stop(copy->copy_task);
1135 nfs4_put_copy(copy);
1136}
1137
1138static struct nfsd4_copy *nfsd4_get_copy(struct nfs4_client *clp)
1139{
1140 struct nfsd4_copy *copy = NULL;
1141
1142 spin_lock(&clp->async_lock);
1143 if (!list_empty(&clp->async_copies)) {
1144 copy = list_first_entry(&clp->async_copies, struct nfsd4_copy,
1145 copies);
1146 refcount_inc(&copy->refcount);
1147 }
1148 spin_unlock(&clp->async_lock);
1149 return copy;
1150}
1151
1152void nfsd4_shutdown_copy(struct nfs4_client *clp)
1153{
1154 struct nfsd4_copy *copy;
1155
1156 while ((copy = nfsd4_get_copy(clp)) != NULL)
1157 nfsd4_stop_copy(copy);
1158}
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001159#ifdef CONFIG_NFSD_V4_2_INTER_SSC
1160
1161extern struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
1162 struct nfs_fh *src_fh,
1163 nfs4_stateid *stateid);
1164extern void nfs42_ssc_close(struct file *filep);
1165
1166extern void nfs_sb_deactive(struct super_block *sb);
1167
1168#define NFSD42_INTERSSC_MOUNTOPS "vers=4.2,addr=%s,sec=sys"
1169
Chuck Leverf2453972020-04-06 16:01:42 -04001170/*
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001171 * Support one copy source server for now.
1172 */
1173static __be32
1174nfsd4_interssc_connect(struct nl4_server *nss, struct svc_rqst *rqstp,
1175 struct vfsmount **mount)
1176{
1177 struct file_system_type *type;
1178 struct vfsmount *ss_mnt;
1179 struct nfs42_netaddr *naddr;
1180 struct sockaddr_storage tmp_addr;
1181 size_t tmp_addrlen, match_netid_len = 3;
1182 char *startsep = "", *endsep = "", *match_netid = "tcp";
1183 char *ipaddr, *dev_name, *raw_data;
Olga Kornievskaiab8290ca2019-12-04 15:13:53 -05001184 int len, raw_len;
1185 __be32 status = nfserr_inval;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001186
1187 naddr = &nss->u.nl4_addr;
1188 tmp_addrlen = rpc_uaddr2sockaddr(SVC_NET(rqstp), naddr->addr,
1189 naddr->addr_len,
1190 (struct sockaddr *)&tmp_addr,
1191 sizeof(tmp_addr));
1192 if (tmp_addrlen == 0)
1193 goto out_err;
1194
1195 if (tmp_addr.ss_family == AF_INET6) {
1196 startsep = "[";
1197 endsep = "]";
1198 match_netid = "tcp6";
1199 match_netid_len = 4;
1200 }
1201
1202 if (naddr->netid_len != match_netid_len ||
1203 strncmp(naddr->netid, match_netid, naddr->netid_len))
1204 goto out_err;
1205
1206 /* Construct the raw data for the vfs_kern_mount call */
1207 len = RPC_MAX_ADDRBUFLEN + 1;
1208 ipaddr = kzalloc(len, GFP_KERNEL);
1209 if (!ipaddr)
1210 goto out_err;
1211
1212 rpc_ntop((struct sockaddr *)&tmp_addr, ipaddr, len);
1213
1214 /* 2 for ipv6 endsep and startsep. 3 for ":/" and trailing '/0'*/
1215
1216 raw_len = strlen(NFSD42_INTERSSC_MOUNTOPS) + strlen(ipaddr);
1217 raw_data = kzalloc(raw_len, GFP_KERNEL);
1218 if (!raw_data)
1219 goto out_free_ipaddr;
1220
1221 snprintf(raw_data, raw_len, NFSD42_INTERSSC_MOUNTOPS, ipaddr);
1222
Olga Kornievskaiab8290ca2019-12-04 15:13:53 -05001223 status = nfserr_nodev;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001224 type = get_fs_type("nfs");
1225 if (!type)
1226 goto out_free_rawdata;
1227
1228 /* Set the server:<export> for the vfs_kern_mount call */
1229 dev_name = kzalloc(len + 5, GFP_KERNEL);
1230 if (!dev_name)
1231 goto out_free_rawdata;
1232 snprintf(dev_name, len + 5, "%s%s%s:/", startsep, ipaddr, endsep);
1233
1234 /* Use an 'internal' mount: SB_KERNMOUNT -> MNT_INTERNAL */
1235 ss_mnt = vfs_kern_mount(type, SB_KERNMOUNT, dev_name, raw_data);
1236 module_put(type->owner);
1237 if (IS_ERR(ss_mnt))
1238 goto out_free_devname;
1239
1240 status = 0;
1241 *mount = ss_mnt;
1242
1243out_free_devname:
1244 kfree(dev_name);
1245out_free_rawdata:
1246 kfree(raw_data);
1247out_free_ipaddr:
1248 kfree(ipaddr);
1249out_err:
1250 return status;
1251}
1252
1253static void
1254nfsd4_interssc_disconnect(struct vfsmount *ss_mnt)
1255{
Dai Ngo0cfcd402020-10-18 23:42:49 -04001256 nfs_do_sb_deactive(ss_mnt->mnt_sb);
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001257 mntput(ss_mnt);
1258}
1259
Chuck Leverf2453972020-04-06 16:01:42 -04001260/*
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001261 * Verify COPY destination stateid.
Chuck Leverf2453972020-04-06 16:01:42 -04001262 *
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001263 * Connect to the source server with NFSv4.1.
1264 * Create the source struct file for nfsd_copy_range.
1265 * Called with COPY cstate:
1266 * SAVED_FH: source filehandle
1267 * CURRENT_FH: destination filehandle
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001268 */
1269static __be32
1270nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
1271 struct nfsd4_compound_state *cstate,
1272 struct nfsd4_copy *copy, struct vfsmount **mount)
1273{
1274 struct svc_fh *s_fh = NULL;
1275 stateid_t *s_stid = &copy->cp_src_stateid;
Olga Kornievskaiab8290ca2019-12-04 15:13:53 -05001276 __be32 status = nfserr_inval;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001277
1278 /* Verify the destination stateid and set dst struct file*/
1279 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1280 &copy->cp_dst_stateid,
1281 WR_STATE, &copy->nf_dst, NULL);
1282 if (status)
1283 goto out;
1284
1285 status = nfsd4_interssc_connect(&copy->cp_src, rqstp, mount);
1286 if (status)
1287 goto out;
1288
1289 s_fh = &cstate->save_fh;
1290
1291 copy->c_fh.size = s_fh->fh_handle.fh_size;
1292 memcpy(copy->c_fh.data, &s_fh->fh_handle.fh_base, copy->c_fh.size);
Olga Kornievskaia3f9544c2019-12-04 15:13:53 -05001293 copy->stateid.seqid = cpu_to_be32(s_stid->si_generation);
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001294 memcpy(copy->stateid.other, (void *)&s_stid->si_opaque,
1295 sizeof(stateid_opaque_t));
1296
1297 status = 0;
1298out:
1299 return status;
1300}
1301
1302static void
1303nfsd4_cleanup_inter_ssc(struct vfsmount *ss_mnt, struct nfsd_file *src,
1304 struct nfsd_file *dst)
1305{
1306 nfs42_ssc_close(src->nf_file);
Dai Ngo36e1e5b2020-10-29 15:07:15 -04001307 /* 'src' is freed by nfsd4_do_async_copy */
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001308 nfsd_file_put(dst);
1309 mntput(ss_mnt);
1310}
1311
1312#else /* CONFIG_NFSD_V4_2_INTER_SSC */
1313
1314static __be32
1315nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
1316 struct nfsd4_compound_state *cstate,
1317 struct nfsd4_copy *copy,
1318 struct vfsmount **mount)
1319{
1320 *mount = NULL;
Olga Kornievskaiab8290ca2019-12-04 15:13:53 -05001321 return nfserr_inval;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001322}
1323
1324static void
1325nfsd4_cleanup_inter_ssc(struct vfsmount *ss_mnt, struct nfsd_file *src,
1326 struct nfsd_file *dst)
1327{
1328}
1329
1330static void
1331nfsd4_interssc_disconnect(struct vfsmount *ss_mnt)
1332{
1333}
1334
1335static struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
1336 struct nfs_fh *src_fh,
1337 nfs4_stateid *stateid)
1338{
1339 return NULL;
1340}
1341#endif /* CONFIG_NFSD_V4_2_INTER_SSC */
1342
1343static __be32
1344nfsd4_setup_intra_ssc(struct svc_rqst *rqstp,
1345 struct nfsd4_compound_state *cstate,
1346 struct nfsd4_copy *copy)
1347{
1348 return nfsd4_verify_copy(rqstp, cstate, &copy->cp_src_stateid,
1349 &copy->nf_src, &copy->cp_dst_stateid,
1350 &copy->nf_dst);
1351}
1352
1353static void
1354nfsd4_cleanup_intra_ssc(struct nfsd_file *src, struct nfsd_file *dst)
1355{
1356 nfsd_file_put(src);
1357 nfsd_file_put(dst);
1358}
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001359
1360static void nfsd4_cb_offload_release(struct nfsd4_callback *cb)
1361{
1362 struct nfsd4_copy *copy = container_of(cb, struct nfsd4_copy, cp_cb);
1363
1364 nfs4_put_copy(copy);
1365}
1366
1367static int nfsd4_cb_offload_done(struct nfsd4_callback *cb,
1368 struct rpc_task *task)
1369{
1370 return 1;
1371}
1372
1373static const struct nfsd4_callback_ops nfsd4_cb_offload_ops = {
1374 .release = nfsd4_cb_offload_release,
1375 .done = nfsd4_cb_offload_done
1376};
1377
1378static void nfsd4_init_copy_res(struct nfsd4_copy *copy, bool sync)
1379{
1380 copy->cp_res.wr_stable_how = NFS_UNSTABLE;
1381 copy->cp_synchronous = sync;
1382 gen_boot_verifier(&copy->cp_res.wr_verifier, copy->cp_clp->net);
1383}
1384
1385static ssize_t _nfsd_copy_file_range(struct nfsd4_copy *copy)
1386{
1387 ssize_t bytes_copied = 0;
1388 size_t bytes_total = copy->cp_count;
1389 u64 src_pos = copy->cp_src_pos;
1390 u64 dst_pos = copy->cp_dst_pos;
1391
1392 do {
1393 if (kthread_should_stop())
1394 break;
Jeff Layton5c4583b2019-08-18 14:18:54 -04001395 bytes_copied = nfsd_copy_file_range(copy->nf_src->nf_file,
1396 src_pos, copy->nf_dst->nf_file, dst_pos,
1397 bytes_total);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001398 if (bytes_copied <= 0)
1399 break;
1400 bytes_total -= bytes_copied;
1401 copy->cp_res.wr_bytes_written += bytes_copied;
1402 src_pos += bytes_copied;
1403 dst_pos += bytes_copied;
1404 } while (bytes_total > 0 && !copy->cp_synchronous);
1405 return bytes_copied;
1406}
1407
1408static __be32 nfsd4_do_copy(struct nfsd4_copy *copy, bool sync)
1409{
1410 __be32 status;
1411 ssize_t bytes;
1412
1413 bytes = _nfsd_copy_file_range(copy);
1414 /* for async copy, we ignore the error, client can always retry
1415 * to get the error
1416 */
1417 if (bytes < 0 && !copy->cp_res.wr_bytes_written)
1418 status = nfserrno(bytes);
1419 else {
1420 nfsd4_init_copy_res(copy, sync);
1421 status = nfs_ok;
1422 }
1423
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001424 if (!copy->cp_intra) /* Inter server SSC */
1425 nfsd4_cleanup_inter_ssc(copy->ss_mnt, copy->nf_src,
1426 copy->nf_dst);
1427 else
1428 nfsd4_cleanup_intra_ssc(copy->nf_src, copy->nf_dst);
1429
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001430 return status;
1431}
1432
Chuck Levereb162e12020-11-30 16:17:40 -05001433static void dup_copy_fields(struct nfsd4_copy *src, struct nfsd4_copy *dst)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001434{
1435 dst->cp_src_pos = src->cp_src_pos;
1436 dst->cp_dst_pos = src->cp_dst_pos;
1437 dst->cp_count = src->cp_count;
1438 dst->cp_synchronous = src->cp_synchronous;
1439 memcpy(&dst->cp_res, &src->cp_res, sizeof(src->cp_res));
1440 memcpy(&dst->fh, &src->fh, sizeof(src->fh));
1441 dst->cp_clp = src->cp_clp;
Jeff Layton5c4583b2019-08-18 14:18:54 -04001442 dst->nf_dst = nfsd_file_get(src->nf_dst);
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001443 dst->cp_intra = src->cp_intra;
1444 if (src->cp_intra) /* for inter, file_src doesn't exist yet */
1445 dst->nf_src = nfsd_file_get(src->nf_src);
1446
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001447 memcpy(&dst->cp_stateid, &src->cp_stateid, sizeof(src->cp_stateid));
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001448 memcpy(&dst->cp_src, &src->cp_src, sizeof(struct nl4_server));
1449 memcpy(&dst->stateid, &src->stateid, sizeof(src->stateid));
1450 memcpy(&dst->c_fh, &src->c_fh, sizeof(src->c_fh));
1451 dst->ss_mnt = src->ss_mnt;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001452}
1453
1454static void cleanup_async_copy(struct nfsd4_copy *copy)
1455{
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001456 nfs4_free_copy_state(copy);
Jeff Layton5c4583b2019-08-18 14:18:54 -04001457 nfsd_file_put(copy->nf_dst);
Olga Kornievskaia2e577f02019-12-04 15:13:54 -05001458 if (copy->cp_intra)
1459 nfsd_file_put(copy->nf_src);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001460 spin_lock(&copy->cp_clp->async_lock);
1461 list_del(&copy->copies);
1462 spin_unlock(&copy->cp_clp->async_lock);
1463 nfs4_put_copy(copy);
1464}
1465
1466static int nfsd4_do_async_copy(void *data)
1467{
1468 struct nfsd4_copy *copy = (struct nfsd4_copy *)data;
1469 struct nfsd4_copy *cb_copy;
1470
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001471 if (!copy->cp_intra) { /* Inter server SSC */
1472 copy->nf_src = kzalloc(sizeof(struct nfsd_file), GFP_KERNEL);
1473 if (!copy->nf_src) {
1474 copy->nfserr = nfserr_serverfault;
1475 nfsd4_interssc_disconnect(copy->ss_mnt);
1476 goto do_callback;
1477 }
1478 copy->nf_src->nf_file = nfs42_ssc_open(copy->ss_mnt, &copy->c_fh,
1479 &copy->stateid);
1480 if (IS_ERR(copy->nf_src->nf_file)) {
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001481 copy->nfserr = nfserr_offload_denied;
1482 nfsd4_interssc_disconnect(copy->ss_mnt);
1483 goto do_callback;
1484 }
1485 }
1486
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001487 copy->nfserr = nfsd4_do_copy(copy, 0);
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001488do_callback:
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001489 cb_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
1490 if (!cb_copy)
1491 goto out;
Dai Ngo49a36132020-10-29 15:07:16 -04001492 refcount_set(&cb_copy->refcount, 1);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001493 memcpy(&cb_copy->cp_res, &copy->cp_res, sizeof(copy->cp_res));
1494 cb_copy->cp_clp = copy->cp_clp;
1495 cb_copy->nfserr = copy->nfserr;
1496 memcpy(&cb_copy->fh, &copy->fh, sizeof(copy->fh));
1497 nfsd4_init_cb(&cb_copy->cp_cb, cb_copy->cp_clp,
1498 &nfsd4_cb_offload_ops, NFSPROC4_CLNT_CB_OFFLOAD);
1499 nfsd4_run_cb(&cb_copy->cp_cb);
1500out:
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001501 if (!copy->cp_intra)
1502 kfree(copy->nf_src);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001503 cleanup_async_copy(copy);
1504 return 0;
1505}
1506
Christoph Hellwigffa01602015-12-03 12:59:52 +01001507static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001508nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001509 union nfsd4_op_u *u)
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001510{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001511 struct nfsd4_copy *copy = &u->copy;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001512 __be32 status;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001513 struct nfsd4_copy *async_copy = NULL;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001514
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001515 if (!copy->cp_intra) { /* Inter server SSC */
1516 if (!inter_copy_offload_enable || copy->cp_synchronous) {
1517 status = nfserr_notsupp;
1518 goto out;
1519 }
1520 status = nfsd4_setup_inter_ssc(rqstp, cstate, copy,
1521 &copy->ss_mnt);
1522 if (status)
1523 return nfserr_offload_denied;
1524 } else {
1525 status = nfsd4_setup_intra_ssc(rqstp, cstate, copy);
1526 if (status)
1527 return status;
1528 }
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001529
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001530 copy->cp_clp = cstate->clp;
1531 memcpy(&copy->fh, &cstate->current_fh.fh_handle,
1532 sizeof(struct knfsd_fh));
1533 if (!copy->cp_synchronous) {
1534 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001535
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001536 status = nfserrno(-ENOMEM);
1537 async_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
1538 if (!async_copy)
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001539 goto out_err;
1540 if (!nfs4_init_copy_state(nn, copy))
1541 goto out_err;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001542 refcount_set(&async_copy->refcount, 1);
1543 memcpy(&copy->cp_res.cb_stateid, &copy->cp_stateid,
1544 sizeof(copy->cp_stateid));
Chuck Levereb162e12020-11-30 16:17:40 -05001545 dup_copy_fields(copy, async_copy);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001546 async_copy->copy_task = kthread_create(nfsd4_do_async_copy,
1547 async_copy, "%s", "copy thread");
1548 if (IS_ERR(async_copy->copy_task))
1549 goto out_err;
1550 spin_lock(&async_copy->cp_clp->async_lock);
1551 list_add(&async_copy->copies,
1552 &async_copy->cp_clp->async_copies);
1553 spin_unlock(&async_copy->cp_clp->async_lock);
1554 wake_up_process(async_copy->copy_task);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001555 status = nfs_ok;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001556 } else {
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001557 status = nfsd4_do_copy(copy, 1);
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001558 }
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001559out:
1560 return status;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001561out_err:
Olga Kornievskaia18f428d2019-12-04 15:13:54 -05001562 if (async_copy)
1563 cleanup_async_copy(async_copy);
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001564 status = nfserrno(-ENOMEM);
1565 if (!copy->cp_intra)
1566 nfsd4_interssc_disconnect(copy->ss_mnt);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001567 goto out;
1568}
1569
1570struct nfsd4_copy *
1571find_async_copy(struct nfs4_client *clp, stateid_t *stateid)
1572{
1573 struct nfsd4_copy *copy;
1574
1575 spin_lock(&clp->async_lock);
1576 list_for_each_entry(copy, &clp->async_copies, copies) {
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001577 if (memcmp(&copy->cp_stateid.stid, stateid, NFS4_STATEID_SIZE))
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001578 continue;
1579 refcount_inc(&copy->refcount);
1580 spin_unlock(&clp->async_lock);
1581 return copy;
1582 }
1583 spin_unlock(&clp->async_lock);
1584 return NULL;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001585}
1586
1587static __be32
Olga Kornievskaia885e2bf2018-07-20 18:19:19 -04001588nfsd4_offload_cancel(struct svc_rqst *rqstp,
1589 struct nfsd4_compound_state *cstate,
1590 union nfsd4_op_u *u)
1591{
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001592 struct nfsd4_offload_status *os = &u->offload_status;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001593 struct nfsd4_copy *copy;
1594 struct nfs4_client *clp = cstate->clp;
1595
1596 copy = find_async_copy(clp, &os->stateid);
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001597 if (!copy) {
1598 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001599
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001600 return manage_cpntf_state(nn, &os->stateid, clp, NULL);
1601 } else
1602 nfsd4_stop_copy(copy);
1603
1604 return nfs_ok;
Olga Kornievskaia885e2bf2018-07-20 18:19:19 -04001605}
1606
1607static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04001608nfsd4_copy_notify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1609 union nfsd4_op_u *u)
1610{
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001611 struct nfsd4_copy_notify *cn = &u->copy_notify;
1612 __be32 status;
1613 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1614 struct nfs4_stid *stid;
1615 struct nfs4_cpntf_state *cps;
1616 struct nfs4_client *clp = cstate->clp;
1617
1618 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1619 &cn->cpn_src_stateid, RD_STATE, NULL,
1620 &stid);
1621 if (status)
1622 return status;
1623
1624 cn->cpn_sec = nn->nfsd4_lease;
1625 cn->cpn_nsec = 0;
1626
1627 status = nfserrno(-ENOMEM);
1628 cps = nfs4_alloc_init_cpntf_state(nn, stid);
1629 if (!cps)
1630 goto out;
1631 memcpy(&cn->cpn_cnr_stateid, &cps->cp_stateid.stid, sizeof(stateid_t));
1632 memcpy(&cps->cp_p_stateid, &stid->sc_stateid, sizeof(stateid_t));
1633 memcpy(&cps->cp_p_clid, &clp->cl_clientid, sizeof(clientid_t));
1634
1635 /* For now, only return one server address in cpn_src, the
1636 * address used by the client to connect to this server.
1637 */
1638 cn->cpn_src.nl4_type = NL4_NETADDR;
1639 status = nfsd4_set_netaddr((struct sockaddr *)&rqstp->rq_daddr,
1640 &cn->cpn_src.u.nl4_addr);
1641 WARN_ON_ONCE(status);
1642 if (status) {
1643 nfs4_put_cpntf_state(nn, cps);
1644 goto out;
1645 }
1646out:
1647 nfs4_put_stid(stid);
1648 return status;
Olga Kornievskaia51911862019-08-08 11:14:59 -04001649}
1650
1651static __be32
Anna Schumaker95d871f2014-11-07 14:44:26 -05001652nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1653 struct nfsd4_fallocate *fallocate, int flags)
1654{
J. Bruce Fields0d4d6722018-11-06 17:44:03 -05001655 __be32 status;
Jeff Layton5c4583b2019-08-18 14:18:54 -04001656 struct nfsd_file *nf;
Anna Schumaker95d871f2014-11-07 14:44:26 -05001657
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01001658 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
Anna Schumaker95d871f2014-11-07 14:44:26 -05001659 &fallocate->falloc_stateid,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001660 WR_STATE, &nf, NULL);
Anna Schumaker95d871f2014-11-07 14:44:26 -05001661 if (status != nfs_ok) {
1662 dprintk("NFSD: nfsd4_fallocate: couldn't process stateid!\n");
1663 return status;
1664 }
1665
Jeff Layton5c4583b2019-08-18 14:18:54 -04001666 status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, nf->nf_file,
Anna Schumaker95d871f2014-11-07 14:44:26 -05001667 fallocate->falloc_offset,
1668 fallocate->falloc_length,
1669 flags);
Jeff Layton5c4583b2019-08-18 14:18:54 -04001670 nfsd_file_put(nf);
Anna Schumaker95d871f2014-11-07 14:44:26 -05001671 return status;
1672}
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04001673static __be32
1674nfsd4_offload_status(struct svc_rqst *rqstp,
1675 struct nfsd4_compound_state *cstate,
1676 union nfsd4_op_u *u)
1677{
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001678 struct nfsd4_offload_status *os = &u->offload_status;
1679 __be32 status = 0;
1680 struct nfsd4_copy *copy;
1681 struct nfs4_client *clp = cstate->clp;
1682
1683 copy = find_async_copy(clp, &os->stateid);
1684 if (copy) {
1685 os->count = copy->cp_res.wr_bytes_written;
1686 nfs4_put_copy(copy);
1687 } else
1688 status = nfserr_bad_stateid;
1689
1690 return status;
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04001691}
Anna Schumaker95d871f2014-11-07 14:44:26 -05001692
1693static __be32
1694nfsd4_allocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001695 union nfsd4_op_u *u)
Anna Schumaker95d871f2014-11-07 14:44:26 -05001696{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001697 return nfsd4_fallocate(rqstp, cstate, &u->allocate, 0);
Anna Schumaker95d871f2014-11-07 14:44:26 -05001698}
1699
1700static __be32
Anna Schumakerb0cb9082014-11-07 14:44:27 -05001701nfsd4_deallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001702 union nfsd4_op_u *u)
Anna Schumakerb0cb9082014-11-07 14:44:27 -05001703{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001704 return nfsd4_fallocate(rqstp, cstate, &u->deallocate,
Anna Schumakerb0cb9082014-11-07 14:44:27 -05001705 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
1706}
1707
1708static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04001709nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001710 union nfsd4_op_u *u)
Anna Schumaker24bab492014-09-26 13:58:27 -04001711{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001712 struct nfsd4_seek *seek = &u->seek;
Anna Schumaker24bab492014-09-26 13:58:27 -04001713 int whence;
1714 __be32 status;
Jeff Layton5c4583b2019-08-18 14:18:54 -04001715 struct nfsd_file *nf;
Anna Schumaker24bab492014-09-26 13:58:27 -04001716
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01001717 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
Anna Schumaker24bab492014-09-26 13:58:27 -04001718 &seek->seek_stateid,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001719 RD_STATE, &nf, NULL);
Anna Schumaker24bab492014-09-26 13:58:27 -04001720 if (status) {
1721 dprintk("NFSD: nfsd4_seek: couldn't process stateid!\n");
1722 return status;
1723 }
1724
1725 switch (seek->seek_whence) {
1726 case NFS4_CONTENT_DATA:
1727 whence = SEEK_DATA;
1728 break;
1729 case NFS4_CONTENT_HOLE:
1730 whence = SEEK_HOLE;
1731 break;
1732 default:
1733 status = nfserr_union_notsupp;
1734 goto out;
1735 }
1736
1737 /*
1738 * Note: This call does change file->f_pos, but nothing in NFSD
1739 * should ever file->f_pos.
1740 */
Jeff Layton5c4583b2019-08-18 14:18:54 -04001741 seek->seek_pos = vfs_llseek(nf->nf_file, seek->seek_offset, whence);
Anna Schumaker24bab492014-09-26 13:58:27 -04001742 if (seek->seek_pos < 0)
1743 status = nfserrno(seek->seek_pos);
Jeff Layton5c4583b2019-08-18 14:18:54 -04001744 else if (seek->seek_pos >= i_size_read(file_inode(nf->nf_file)))
Anna Schumaker24bab492014-09-26 13:58:27 -04001745 seek->seek_eof = true;
1746
1747out:
Jeff Layton5c4583b2019-08-18 14:18:54 -04001748 nfsd_file_put(nf);
Anna Schumaker24bab492014-09-26 13:58:27 -04001749 return status;
1750}
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752/* This routine never returns NFS_OK! If there are no other errors, it
1753 * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
1754 * attributes matched. VERIFY is implemented by mapping NFSERR_SAME
1755 * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
1756 */
Al Virob37ad282006-10-19 23:28:59 -07001757static __be32
J.Bruce Fieldsc954e2a2006-12-13 00:35:31 -08001758_nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08001759 struct nfsd4_verify *verify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
Al Viro2ebbc012006-10-19 23:28:58 -07001761 __be32 *buf, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 int count;
Al Virob37ad282006-10-19 23:28:59 -07001763 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001765 status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (status)
1767 return status;
1768
Yu Zhiguo3c8e0312009-05-16 16:22:31 +08001769 status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
1770 if (status)
1771 return status;
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
1774 || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
1775 return nfserr_inval;
1776 if (verify->ve_attrlen & 3)
1777 return nfserr_inval;
1778
1779 /* count in words:
1780 * bitmap_len(1) + bitmap(2) + attr_len(1) = 4
1781 */
1782 count = 4 + (verify->ve_attrlen >> 2);
1783 buf = kmalloc(count << 2, GFP_KERNEL);
1784 if (!buf)
J. Bruce Fields3e772462011-08-10 19:07:33 -04001785 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
J. Bruce Fields84822d02012-12-14 17:57:50 -05001787 p = buf;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04001788 status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08001789 cstate->current_fh.fh_export,
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04001790 cstate->current_fh.fh_dentry,
1791 verify->ve_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08001792 rqstp, 0);
J. Bruce Fields41ae6e712013-08-21 15:32:50 -04001793 /*
1794 * If nfsd4_encode_fattr() ran out of space, assume that's because
1795 * the attributes are longer (hence different) than those given:
1796 */
J. Bruce Fields84822d02012-12-14 17:57:50 -05001797 if (status == nfserr_resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 status = nfserr_not_same;
1799 if (status)
1800 goto out_kfree;
1801
Benny Halevy95ec28c2009-04-03 08:29:08 +03001802 /* skip bitmap */
1803 p = buf + 1 + ntohl(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 status = nfserr_not_same;
1805 if (ntohl(*p++) != verify->ve_attrlen)
1806 goto out_kfree;
1807 if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
1808 status = nfserr_same;
1809
1810out_kfree:
1811 kfree(buf);
1812 return status;
1813}
1814
J.Bruce Fieldsc954e2a2006-12-13 00:35:31 -08001815static __be32
1816nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001817 union nfsd4_op_u *u)
J.Bruce Fieldsc954e2a2006-12-13 00:35:31 -08001818{
1819 __be32 status;
1820
Christoph Hellwigeb698532017-05-08 20:58:35 +02001821 status = _nfsd4_verify(rqstp, cstate, &u->verify);
J.Bruce Fieldsc954e2a2006-12-13 00:35:31 -08001822 return status == nfserr_not_same ? nfs_ok : status;
1823}
1824
1825static __be32
1826nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001827 union nfsd4_op_u *u)
J.Bruce Fieldsc954e2a2006-12-13 00:35:31 -08001828{
1829 __be32 status;
1830
Christoph Hellwigeb698532017-05-08 20:58:35 +02001831 status = _nfsd4_verify(rqstp, cstate, &u->nverify);
J.Bruce Fieldsc954e2a2006-12-13 00:35:31 -08001832 return status == nfserr_same ? nfs_ok : status;
1833}
1834
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001835#ifdef CONFIG_NFSD_PNFS
1836static const struct nfsd4_layout_ops *
1837nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type)
1838{
Jeff Layton8a4c3922016-07-10 15:55:58 -04001839 if (!exp->ex_layout_types) {
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001840 dprintk("%s: export does not support pNFS\n", __func__);
1841 return NULL;
1842 }
1843
Ari Kauppib550a322017-05-05 16:07:55 -04001844 if (layout_type >= LAYOUT_TYPE_MAX ||
1845 !(exp->ex_layout_types & (1 << layout_type))) {
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001846 dprintk("%s: layout type %d not supported\n",
1847 __func__, layout_type);
1848 return NULL;
1849 }
1850
1851 return nfsd4_layout_ops[layout_type];
1852}
1853
1854static __be32
1855nfsd4_getdeviceinfo(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001856 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001857{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001858 struct nfsd4_getdeviceinfo *gdp = &u->getdeviceinfo;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001859 const struct nfsd4_layout_ops *ops;
1860 struct nfsd4_deviceid_map *map;
1861 struct svc_export *exp;
1862 __be32 nfserr;
1863
1864 dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n",
1865 __func__,
1866 gdp->gd_layout_type,
1867 gdp->gd_devid.fsid_idx, gdp->gd_devid.generation,
1868 gdp->gd_maxcount);
1869
1870 map = nfsd4_find_devid_map(gdp->gd_devid.fsid_idx);
1871 if (!map) {
1872 dprintk("%s: couldn't find device ID to export mapping!\n",
1873 __func__);
1874 return nfserr_noent;
1875 }
1876
1877 exp = rqst_exp_find(rqstp, map->fsid_type, map->fsid);
1878 if (IS_ERR(exp)) {
1879 dprintk("%s: could not find device id\n", __func__);
1880 return nfserr_noent;
1881 }
1882
1883 nfserr = nfserr_layoutunavailable;
1884 ops = nfsd4_layout_verify(exp, gdp->gd_layout_type);
1885 if (!ops)
1886 goto out;
1887
1888 nfserr = nfs_ok;
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +01001889 if (gdp->gd_maxcount != 0) {
1890 nfserr = ops->proc_getdeviceinfo(exp->ex_path.mnt->mnt_sb,
Tom Haynesd7c920d2016-06-14 13:41:27 -07001891 rqstp, cstate->session->se_client, gdp);
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +01001892 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001893
1894 gdp->gd_notify_types &= ops->notify_types;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001895out:
Kinglong Meea1420382015-03-15 23:12:15 +08001896 exp_put(exp);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001897 return nfserr;
1898}
1899
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04001900static void
1901nfsd4_getdeviceinfo_release(union nfsd4_op_u *u)
1902{
1903 kfree(u->getdeviceinfo.gd_device);
1904}
1905
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001906static __be32
1907nfsd4_layoutget(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001908 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001909{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001910 struct nfsd4_layoutget *lgp = &u->layoutget;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001911 struct svc_fh *current_fh = &cstate->current_fh;
1912 const struct nfsd4_layout_ops *ops;
1913 struct nfs4_layout_stateid *ls;
1914 __be32 nfserr;
Benjamin Coddington66282ec2017-12-19 09:35:25 -05001915 int accmode = NFSD_MAY_READ_IF_EXEC;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001916
1917 switch (lgp->lg_seg.iomode) {
1918 case IOMODE_READ:
Benjamin Coddington66282ec2017-12-19 09:35:25 -05001919 accmode |= NFSD_MAY_READ;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001920 break;
1921 case IOMODE_RW:
Benjamin Coddington66282ec2017-12-19 09:35:25 -05001922 accmode |= NFSD_MAY_READ | NFSD_MAY_WRITE;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001923 break;
1924 default:
1925 dprintk("%s: invalid iomode %d\n",
1926 __func__, lgp->lg_seg.iomode);
1927 nfserr = nfserr_badiomode;
1928 goto out;
1929 }
1930
1931 nfserr = fh_verify(rqstp, current_fh, 0, accmode);
1932 if (nfserr)
1933 goto out;
1934
1935 nfserr = nfserr_layoutunavailable;
1936 ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);
1937 if (!ops)
1938 goto out;
1939
1940 /*
1941 * Verify minlength and range as per RFC5661:
1942 * o If loga_length is less than loga_minlength,
1943 * the metadata server MUST return NFS4ERR_INVAL.
1944 * o If the sum of loga_offset and loga_minlength exceeds
1945 * NFS4_UINT64_MAX, and loga_minlength is not
1946 * NFS4_UINT64_MAX, the error NFS4ERR_INVAL MUST result.
1947 * o If the sum of loga_offset and loga_length exceeds
1948 * NFS4_UINT64_MAX, and loga_length is not NFS4_UINT64_MAX,
1949 * the error NFS4ERR_INVAL MUST result.
1950 */
1951 nfserr = nfserr_inval;
1952 if (lgp->lg_seg.length < lgp->lg_minlength ||
1953 (lgp->lg_minlength != NFS4_MAX_UINT64 &&
1954 lgp->lg_minlength > NFS4_MAX_UINT64 - lgp->lg_seg.offset) ||
1955 (lgp->lg_seg.length != NFS4_MAX_UINT64 &&
1956 lgp->lg_seg.length > NFS4_MAX_UINT64 - lgp->lg_seg.offset))
1957 goto out;
1958 if (lgp->lg_seg.length == 0)
1959 goto out;
1960
1961 nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lgp->lg_sid,
1962 true, lgp->lg_layout_type, &ls);
Christoph Hellwig31ef83d2014-08-16 19:02:22 -05001963 if (nfserr) {
Chuck Leverf394b622018-03-27 10:53:11 -04001964 trace_nfsd_layout_get_lookup_fail(&lgp->lg_sid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001965 goto out;
Christoph Hellwig31ef83d2014-08-16 19:02:22 -05001966 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001967
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02001968 nfserr = nfserr_recallconflict;
1969 if (atomic_read(&ls->ls_stid.sc_file->fi_lo_recalls))
1970 goto out_put_stid;
1971
David Howells2b0143b2015-03-17 22:25:59 +00001972 nfserr = ops->proc_layoutget(d_inode(current_fh->fh_dentry),
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001973 current_fh, lgp);
1974 if (nfserr)
1975 goto out_put_stid;
1976
1977 nfserr = nfsd4_insert_layout(lgp, ls);
1978
1979out_put_stid:
Jeff Laytoncc8a5532015-09-17 07:58:24 -04001980 mutex_unlock(&ls->ls_mutex);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001981 nfs4_put_stid(&ls->ls_stid);
1982out:
1983 return nfserr;
1984}
1985
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04001986static void
1987nfsd4_layoutget_release(union nfsd4_op_u *u)
1988{
1989 kfree(u->layoutget.lg_content);
1990}
1991
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001992static __be32
1993nfsd4_layoutcommit(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001994 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001995{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001996 struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001997 const struct nfsd4_layout_seg *seg = &lcp->lc_seg;
1998 struct svc_fh *current_fh = &cstate->current_fh;
1999 const struct nfsd4_layout_ops *ops;
2000 loff_t new_size = lcp->lc_last_wr + 1;
2001 struct inode *inode;
2002 struct nfs4_layout_stateid *ls;
2003 __be32 nfserr;
2004
2005 nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_WRITE);
2006 if (nfserr)
2007 goto out;
2008
2009 nfserr = nfserr_layoutunavailable;
2010 ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
2011 if (!ops)
2012 goto out;
David Howells2b0143b2015-03-17 22:25:59 +00002013 inode = d_inode(current_fh->fh_dentry);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002014
2015 nfserr = nfserr_inval;
2016 if (new_size <= seg->offset) {
2017 dprintk("pnfsd: last write before layout segment\n");
2018 goto out;
2019 }
2020 if (new_size > seg->offset + seg->length) {
2021 dprintk("pnfsd: last write beyond layout segment\n");
2022 goto out;
2023 }
2024 if (!lcp->lc_newoffset && new_size > i_size_read(inode)) {
2025 dprintk("pnfsd: layoutcommit beyond EOF\n");
2026 goto out;
2027 }
2028
2029 nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lcp->lc_sid,
2030 false, lcp->lc_layout_type,
2031 &ls);
2032 if (nfserr) {
Chuck Leverf394b622018-03-27 10:53:11 -04002033 trace_nfsd_layout_commit_lookup_fail(&lcp->lc_sid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002034 /* fixup error code as per RFC5661 */
2035 if (nfserr == nfserr_bad_stateid)
2036 nfserr = nfserr_badlayout;
2037 goto out;
2038 }
2039
Jeff Laytoncc8a5532015-09-17 07:58:24 -04002040 /* LAYOUTCOMMIT does not require any serialization */
2041 mutex_unlock(&ls->ls_mutex);
2042
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002043 if (new_size > i_size_read(inode)) {
2044 lcp->lc_size_chg = 1;
2045 lcp->lc_newsize = new_size;
2046 } else {
2047 lcp->lc_size_chg = 0;
2048 }
2049
Kinglong Meed8398fc2015-07-07 10:12:03 +08002050 nfserr = ops->proc_layoutcommit(inode, lcp);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002051 nfs4_put_stid(&ls->ls_stid);
2052out:
2053 return nfserr;
2054}
2055
2056static __be32
2057nfsd4_layoutreturn(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02002058 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002059{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002060 struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002061 struct svc_fh *current_fh = &cstate->current_fh;
2062 __be32 nfserr;
2063
2064 nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_NOP);
2065 if (nfserr)
2066 goto out;
2067
2068 nfserr = nfserr_layoutunavailable;
2069 if (!nfsd4_layout_verify(current_fh->fh_export, lrp->lr_layout_type))
2070 goto out;
2071
2072 switch (lrp->lr_seg.iomode) {
2073 case IOMODE_READ:
2074 case IOMODE_RW:
2075 case IOMODE_ANY:
2076 break;
2077 default:
2078 dprintk("%s: invalid iomode %d\n", __func__,
2079 lrp->lr_seg.iomode);
2080 nfserr = nfserr_inval;
2081 goto out;
2082 }
2083
2084 switch (lrp->lr_return_type) {
2085 case RETURN_FILE:
2086 nfserr = nfsd4_return_file_layouts(rqstp, cstate, lrp);
2087 break;
2088 case RETURN_FSID:
2089 case RETURN_ALL:
2090 nfserr = nfsd4_return_client_layouts(rqstp, cstate, lrp);
2091 break;
2092 default:
2093 dprintk("%s: invalid return_type %d\n", __func__,
2094 lrp->lr_return_type);
2095 nfserr = nfserr_inval;
2096 break;
2097 }
2098out:
2099 return nfserr;
2100}
2101#endif /* CONFIG_NFSD_PNFS */
2102
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002103static __be32
2104nfsd4_getxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2105 union nfsd4_op_u *u)
2106{
2107 struct nfsd4_getxattr *getxattr = &u->getxattr;
2108
2109 return nfsd_getxattr(rqstp, &cstate->current_fh,
2110 getxattr->getxa_name, &getxattr->getxa_buf,
2111 &getxattr->getxa_len);
2112}
2113
2114static __be32
2115nfsd4_setxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2116 union nfsd4_op_u *u)
2117{
2118 struct nfsd4_setxattr *setxattr = &u->setxattr;
2119 __be32 ret;
2120
2121 if (opens_in_grace(SVC_NET(rqstp)))
2122 return nfserr_grace;
2123
2124 ret = nfsd_setxattr(rqstp, &cstate->current_fh, setxattr->setxa_name,
2125 setxattr->setxa_buf, setxattr->setxa_len,
2126 setxattr->setxa_flags);
2127
2128 if (!ret)
2129 set_change_info(&setxattr->setxa_cinfo, &cstate->current_fh);
2130
2131 return ret;
2132}
2133
2134static __be32
2135nfsd4_listxattrs(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2136 union nfsd4_op_u *u)
2137{
2138 /*
2139 * Get the entire list, then copy out only the user attributes
2140 * in the encode function.
2141 */
2142 return nfsd_listxattr(rqstp, &cstate->current_fh,
2143 &u->listxattrs.lsxa_buf, &u->listxattrs.lsxa_len);
2144}
2145
2146static __be32
2147nfsd4_removexattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2148 union nfsd4_op_u *u)
2149{
2150 struct nfsd4_removexattr *removexattr = &u->removexattr;
2151 __be32 ret;
2152
2153 if (opens_in_grace(SVC_NET(rqstp)))
2154 return nfserr_grace;
2155
2156 ret = nfsd_removexattr(rqstp, &cstate->current_fh,
2157 removexattr->rmxa_name);
2158
2159 if (!ret)
2160 set_change_info(&removexattr->rmxa_cinfo, &cstate->current_fh);
2161
2162 return ret;
2163}
2164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165/*
2166 * NULL call.
2167 */
Al Viro7111c662006-10-19 23:28:45 -07002168static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +02002169nfsd4_proc_null(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
Chuck Levercc028a12020-10-02 15:52:44 -04002171 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
Shankar Anande2b20952006-07-10 04:45:44 -07002174static inline void nfsd4_increment_op_stats(u32 opnum)
2175{
2176 if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
Amir Goldsteine567b982021-01-06 09:52:35 +02002177 percpu_counter_inc(&nfsdstats.counter[NFSD_STATS_NFS4_OP(opnum)]);
Shankar Anande2b20952006-07-10 04:45:44 -07002178}
2179
Christoph Hellwigbb2a8b02017-05-08 23:46:47 +02002180static const struct nfsd4_operation nfsd4_ops[];
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002181
Adrian Bunkf1c7f792008-08-08 19:26:42 +03002182static const char *nfsd4_op_name(unsigned opnum);
Benny Halevyb001a1b2008-07-02 11:15:03 +03002183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184/*
J. Bruce Fields57716352010-04-21 12:27:19 -04002185 * Enforce NFSv4.1 COMPOUND ordering rules:
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002186 *
J. Bruce Fields57716352010-04-21 12:27:19 -04002187 * Also note, enforced elsewhere:
2188 * - SEQUENCE other than as first op results in
2189 * NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002190 * - BIND_CONN_TO_SESSION must be the only op in its compound.
2191 * (Enforced in nfsd4_bind_conn_to_session().)
J. Bruce Fields57716352010-04-21 12:27:19 -04002192 * - DESTROY_SESSION must be the final operation in a compound, if
2193 * sessionid's in SEQUENCE and DESTROY_SESSION are the same.
2194 * (Enforced in nfsd4_destroy_session().)
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002195 */
J. Bruce Fields57716352010-04-21 12:27:19 -04002196static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002197{
J. Bruce Fields7a04cfd2018-06-13 15:04:59 -04002198 struct nfsd4_op *first_op = &args->ops[0];
J. Bruce Fields57716352010-04-21 12:27:19 -04002199
2200 /* These ordering requirements don't apply to NFSv4.0: */
2201 if (args->minorversion == 0)
2202 return nfs_ok;
2203 /* This is weird, but OK, not our problem: */
2204 if (args->opcnt == 0)
2205 return nfs_ok;
J. Bruce Fields7a04cfd2018-06-13 15:04:59 -04002206 if (first_op->status == nfserr_op_illegal)
J. Bruce Fields57716352010-04-21 12:27:19 -04002207 return nfs_ok;
J. Bruce Fields7a04cfd2018-06-13 15:04:59 -04002208 if (!(nfsd4_ops[first_op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
J. Bruce Fields57716352010-04-21 12:27:19 -04002209 return nfserr_op_not_in_session;
J. Bruce Fields7a04cfd2018-06-13 15:04:59 -04002210 if (first_op->opnum == OP_SEQUENCE)
J. Bruce Fields57716352010-04-21 12:27:19 -04002211 return nfs_ok;
J. Bruce Fields7a04cfd2018-06-13 15:04:59 -04002212 /*
2213 * So first_op is something allowed outside a session, like
2214 * EXCHANGE_ID; but then it has to be the only op in the
2215 * compound:
2216 */
J. Bruce Fields57716352010-04-21 12:27:19 -04002217 if (args->opcnt != 1)
2218 return nfserr_not_only_op;
2219 return nfs_ok;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002220}
2221
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002222const struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
J. Bruce Fields22b03212011-04-09 11:23:24 -04002223{
2224 return &nfsd4_ops[op->opnum];
2225}
2226
J. Bruce Fields10910062011-01-24 12:11:02 -05002227bool nfsd4_cache_this_op(struct nfsd4_op *op)
2228{
J. Bruce Fieldse372ba62014-05-19 12:27:11 -04002229 if (op->opnum == OP_ILLEGAL)
2230 return false;
J. Bruce Fieldsc8566942011-09-20 08:49:51 -04002231 return OPDESC(op)->op_flags & OP_CACHEME;
J. Bruce Fields10910062011-01-24 12:11:02 -05002232}
2233
J. Bruce Fields68d93182011-04-08 17:00:50 -04002234static bool need_wrongsec_check(struct svc_rqst *rqstp)
2235{
2236 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2237 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
2238 struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
2239 struct nfsd4_op *next = &argp->ops[resp->opcnt];
Christoph Hellwigbb2a8b02017-05-08 23:46:47 +02002240 const struct nfsd4_operation *thisd = OPDESC(this);
2241 const struct nfsd4_operation *nextd;
J. Bruce Fields68d93182011-04-08 17:00:50 -04002242
J. Bruce Fields68d93182011-04-08 17:00:50 -04002243 /*
2244 * Most ops check wronsec on our own; only the putfh-like ops
2245 * have special rules.
2246 */
2247 if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
2248 return false;
2249 /*
2250 * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
2251 * put-filehandle operation if we're not going to use the
2252 * result:
2253 */
2254 if (argp->opcnt == resp->opcnt)
2255 return false;
J. Bruce Fields51904b02014-10-22 14:46:29 -04002256 if (next->opnum == OP_ILLEGAL)
2257 return false;
J. Bruce Fields68d93182011-04-08 17:00:50 -04002258 nextd = OPDESC(next);
2259 /*
2260 * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
2261 * errors themselves as necessary; others should check for them
2262 * now:
2263 */
2264 return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
2265}
2266
J. Bruce Fields2d124dfaa2014-01-15 16:26:42 -05002267static void svcxdr_init_encode(struct svc_rqst *rqstp,
2268 struct nfsd4_compoundres *resp)
2269{
2270 struct xdr_stream *xdr = &resp->xdr;
2271 struct xdr_buf *buf = &rqstp->rq_res;
2272 struct kvec *head = buf->head;
2273
2274 xdr->buf = buf;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002275 xdr->iov = head;
J. Bruce Fields2d124dfaa2014-01-15 16:26:42 -05002276 xdr->p = head->iov_base + head->iov_len;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002277 xdr->end = head->iov_base + PAGE_SIZE - rqstp->rq_auth_slack;
J. Bruce Fields6ac90392014-02-26 17:39:35 -05002278 /* Tail and page_len should be zero at this point: */
2279 buf->len = buf->head[0].iov_len;
Chuck Lever0ae4c3e2020-11-11 15:52:47 -05002280 xdr_reset_scratch_buffer(xdr);
J. Bruce Fields05638dc2014-06-02 12:05:47 -04002281 xdr->page_ptr = buf->pages - 1;
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04002282 buf->buflen = PAGE_SIZE * (1 + rqstp->rq_page_end - buf->pages)
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002283 - rqstp->rq_auth_slack;
J. Bruce Fields2d124dfaa2014-01-15 16:26:42 -05002284}
2285
Olga Kornievskaiab9e86382019-10-07 10:56:48 -04002286#ifdef CONFIG_NFSD_V4_2_INTER_SSC
2287static void
2288check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
2289{
2290 struct nfsd4_op *op, *current_op = NULL, *saved_op = NULL;
2291 struct nfsd4_copy *copy;
2292 struct nfsd4_putfh *putfh;
2293 int i;
2294
2295 /* traverse all operation and if it's a COPY compound, mark the
2296 * source filehandle to skip verification
2297 */
2298 for (i = 0; i < args->opcnt; i++) {
2299 op = &args->ops[i];
2300 if (op->opnum == OP_PUTFH)
2301 current_op = op;
2302 else if (op->opnum == OP_SAVEFH)
2303 saved_op = current_op;
2304 else if (op->opnum == OP_RESTOREFH)
2305 current_op = saved_op;
2306 else if (op->opnum == OP_COPY) {
2307 copy = (struct nfsd4_copy *)&op->u;
2308 if (!saved_op) {
2309 op->status = nfserr_nofilehandle;
2310 return;
2311 }
2312 putfh = (struct nfsd4_putfh *)&saved_op->u;
2313 if (!copy->cp_intra)
2314 putfh->no_verify = true;
2315 }
2316 }
2317}
2318#else
2319static void
2320check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
2321{
2322}
2323#endif
2324
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002325/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 * COMPOUND call.
2327 */
Al Viro7111c662006-10-19 23:28:45 -07002328static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +02002329nfsd4_proc_compound(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
Christoph Hellwiga6beb732017-05-08 17:35:49 +02002331 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2332 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 struct nfsd4_op *op;
Andy Adamsone354d572009-03-28 11:30:52 +03002334 struct nfsd4_compound_state *cstate = &resp->cstate;
Kinglong Mee4daeed22014-03-26 17:10:37 +08002335 struct svc_fh *current_fh = &cstate->current_fh;
2336 struct svc_fh *save_fh = &cstate->save_fh;
Trond Myklebuste333f3b2019-04-09 11:46:19 -04002337 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Al Virob37ad282006-10-19 23:28:59 -07002338 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
J. Bruce Fields2d124dfaa2014-01-15 16:26:42 -05002340 svcxdr_init_encode(rqstp, resp);
J. Bruce Fields4aea24b2014-01-15 15:17:58 -05002341 resp->tagp = resp->xdr.p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 /* reserve space for: taglen, tag, and opcnt */
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05002343 xdr_reserve_space(&resp->xdr, 8 + args->taglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 resp->taglen = args->taglen;
2345 resp->tag = args->tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 resp->rqstp = rqstp;
Kinglong Mee4daeed22014-03-26 17:10:37 +08002347 cstate->minorversion = args->minorversion;
Kinglong Mee4daeed22014-03-26 17:10:37 +08002348 fh_init(current_fh, NFS4_FHSIZE);
2349 fh_init(save_fh, NFS4_FHSIZE);
NeilBrown8ff30fa2010-08-12 17:04:07 +10002350 /*
2351 * Don't use the deferral mechanism for NFSv4; compounds make it
2352 * too hard to avoid non-idempotency problems.
2353 */
Jeff Layton30660e04b2014-11-19 07:51:16 -05002354 clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
2356 /*
2357 * According to RFC3010, this takes precedence over all other errors.
2358 */
2359 status = nfserr_minor_vers_mismatch;
Trond Myklebuste333f3b2019-04-09 11:46:19 -04002360 if (nfsd_minorversion(nn, args->minorversion, NFSD_TEST) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 goto out;
J. Bruce Fields00781172017-11-15 12:30:27 -05002362 status = nfserr_resource;
2363 if (args->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
2364 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
J. Bruce Fields57716352010-04-21 12:27:19 -04002366 status = nfs41_check_op_ordering(args);
2367 if (status) {
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002368 op = &args->ops[0];
J. Bruce Fields57716352010-04-21 12:27:19 -04002369 op->status = status;
J. Bruce Fields5b7b15ae2018-06-13 15:21:35 -04002370 resp->opcnt = 1;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002371 goto encode_op;
2372 }
Olga Kornievskaiab9e86382019-10-07 10:56:48 -04002373 check_if_stalefh_allowed(args);
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002374
J. Bruce Fields28df3d12017-07-28 16:35:15 -04002375 rqstp->rq_lease_breaker = (void **)&cstate->clp;
2376
Chuck Leverfff40802018-03-27 10:53:54 -04002377 trace_nfsd_compound(rqstp, args->opcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 while (!status && resp->opcnt < args->opcnt) {
2379 op = &args->ops[resp->opcnt++];
2380
2381 /*
2382 * The XDR decode routines may have pre-set op->status;
2383 * for example, if there is a miscellaneous XDR error
2384 * it will be set to nfserr_bad_xdr.
2385 */
J. Bruce Fields9d313b12013-02-28 12:51:49 -08002386 if (op->status) {
2387 if (op->opnum == OP_OPEN)
2388 op->status = nfsd4_open_omfg(rqstp, cstate, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 goto encode_op;
J. Bruce Fields9d313b12013-02-28 12:51:49 -08002390 }
Olga Kornievskaiab9e86382019-10-07 10:56:48 -04002391 if (!current_fh->fh_dentry &&
2392 !HAS_FH_FLAG(current_fh, NFSD4_FH_FOREIGN)) {
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002393 if (!(op->opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002394 op->status = nfserr_nofilehandle;
2395 goto encode_op;
2396 }
Olga Kornievskaiab9e86382019-10-07 10:56:48 -04002397 } else if (current_fh->fh_export &&
2398 current_fh->fh_export->ex_fslocs.migrated &&
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002399 !(op->opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002400 op->status = nfserr_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 goto encode_op;
2402 }
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002403
Kinglong Mee23367452014-03-29 10:23:47 +08002404 fh_clear_wcc(current_fh);
2405
Mi Jinlong58e7b332011-08-28 18:18:56 +08002406 /* If op is non-idempotent */
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002407 if (op->opdesc->op_flags & OP_MODIFIES_SOMETHING) {
J. Bruce Fields4c69d582014-01-28 16:01:04 -05002408 /*
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04002409 * Don't execute this op if we couldn't encode a
2410 * succesful reply:
2411 */
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002412 u32 plen = op->opdesc->op_rsize_bop(rqstp, op);
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04002413 /*
2414 * Plus if there's another operation, make sure
J. Bruce Fields4c69d582014-01-28 16:01:04 -05002415 * we'll have space to at least encode an error:
2416 */
2417 if (resp->opcnt < args->opcnt)
2418 plen += COMPOUND_ERR_SLACK_SPACE;
Mi Jinlong58e7b332011-08-28 18:18:56 +08002419 op->status = nfsd4_check_resp_size(resp, plen);
2420 }
2421
2422 if (op->status)
2423 goto encode_op;
2424
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002425 if (op->opdesc->op_get_currentstateid)
2426 op->opdesc->op_get_currentstateid(cstate, &op->u);
2427 op->status = op->opdesc->op_func(rqstp, cstate, &op->u);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
J. Bruce Fields9a307402017-05-23 12:24:40 -04002429 /* Only from SEQUENCE */
2430 if (cstate->status == nfserr_replay_cache) {
2431 dprintk("%s NFS4.1 replay from cache\n", __func__);
2432 status = op->status;
2433 goto out;
2434 }
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01002435 if (!op->status) {
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002436 if (op->opdesc->op_set_currentstateid)
2437 op->opdesc->op_set_currentstateid(cstate, &op->u);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01002438
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002439 if (op->opdesc->op_flags & OP_CLEAR_STATEID)
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01002440 clear_current_stateid(cstate);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01002441
J. Bruce Fieldsd781e3d2019-12-06 16:07:32 -05002442 if (current_fh->fh_export &&
2443 need_wrongsec_check(rqstp))
Kinglong Mee4daeed22014-03-26 17:10:37 +08002444 op->status = check_nfsd_access(current_fh->fh_export, rqstp);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01002445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446encode_op:
Al Viroa90b0612006-10-19 23:29:03 -07002447 if (op->status == nfserr_replay_me) {
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002448 op->replay = &cstate->replay_owner->so_replay;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002449 nfsd4_encode_replay(&resp->xdr, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 status = op->status = op->replay->rp_status;
2451 } else {
2452 nfsd4_encode_operation(resp, op);
2453 status = op->status;
2454 }
Benny Halevy04077172008-12-15 19:40:49 +02002455
Chuck Leverfff40802018-03-27 10:53:54 -04002456 trace_nfsd_compound_status(args->opcnt, resp->opcnt, status,
2457 nfsd4_op_name(op->opnum));
Benny Halevy04077172008-12-15 19:40:49 +02002458
Jeff Layton58fb12e2014-07-29 21:34:27 -04002459 nfsd4_cstate_clear_replay(cstate);
Shankar Anande2b20952006-07-10 04:45:44 -07002460 nfsd4_increment_op_stats(op->opnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
2462
Kinglong Mee4daeed22014-03-26 17:10:37 +08002463 fh_put(current_fh);
2464 fh_put(save_fh);
2465 BUG_ON(cstate->replay_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466out:
Chuck Levercc028a12020-10-02 15:52:44 -04002467 cstate->status = status;
Andy Adamson2f425872009-04-03 08:27:32 +03002468 /* Reset deferral mechanism for RPC deferrals */
Jeff Layton30660e04b2014-11-19 07:51:16 -05002469 set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
Chuck Levercc028a12020-10-02 15:52:44 -04002470 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
2472
Mi Jinlong58e7b332011-08-28 18:18:56 +08002473#define op_encode_hdr_size (2)
2474#define op_encode_stateid_maxsz (XDR_QUADLEN(NFS4_STATEID_SIZE))
2475#define op_encode_verifier_maxsz (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
2476#define op_encode_change_info_maxsz (5)
2477#define nfs4_fattr_bitmap_maxsz (4)
2478
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04002479/* We'll fall back on returning no lockowner if run out of space: */
2480#define op_encode_lockowner_maxsz (0)
Mi Jinlong58e7b332011-08-28 18:18:56 +08002481#define op_encode_lock_denied_maxsz (8 + op_encode_lockowner_maxsz)
2482
2483#define nfs4_owner_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
2484
2485#define op_encode_ace_maxsz (3 + nfs4_owner_maxsz)
2486#define op_encode_delegation_maxsz (1 + op_encode_stateid_maxsz + 1 + \
2487 op_encode_ace_maxsz)
2488
2489#define op_encode_channel_attrs_maxsz (6 + 1 + 1)
2490
2491static inline u32 nfsd4_only_status_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2492{
2493 return (op_encode_hdr_size) * sizeof(__be32);
2494}
2495
2496static inline u32 nfsd4_status_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2497{
2498 return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
2499}
2500
Kinglong Mee2282cd22017-02-03 22:36:00 +08002501static inline u32 nfsd4_access_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2502{
2503 /* ac_supported, ac_resp_access */
2504 return (op_encode_hdr_size + 2)* sizeof(__be32);
2505}
2506
Mi Jinlong58e7b332011-08-28 18:18:56 +08002507static inline u32 nfsd4_commit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2508{
2509 return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
2510}
2511
2512static inline u32 nfsd4_create_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2513{
2514 return (op_encode_hdr_size + op_encode_change_info_maxsz
2515 + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
2516}
2517
J. Bruce Fieldsb86cef62014-03-23 12:01:48 -04002518/*
2519 * Note since this is an idempotent operation we won't insist on failing
2520 * the op prematurely if the estimate is too large. We may turn off splice
2521 * reads unnecessarily.
2522 */
2523static inline u32 nfsd4_getattr_rsize(struct svc_rqst *rqstp,
2524 struct nfsd4_op *op)
2525{
2526 u32 *bmap = op->u.getattr.ga_bmval;
2527 u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
2528 u32 ret = 0;
2529
2530 if (bmap0 & FATTR4_WORD0_ACL)
2531 return svc_max_payload(rqstp);
2532 if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
2533 return svc_max_payload(rqstp);
2534
2535 if (bmap1 & FATTR4_WORD1_OWNER) {
2536 ret += IDMAP_NAMESZ + 4;
2537 bmap1 &= ~FATTR4_WORD1_OWNER;
2538 }
2539 if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
2540 ret += IDMAP_NAMESZ + 4;
2541 bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
2542 }
2543 if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
2544 ret += NFS4_FHSIZE + 4;
2545 bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
2546 }
2547 if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
Kinglong Mee1ec8c0c2015-03-28 23:46:09 +08002548 ret += NFS4_MAXLABELLEN + 12;
J. Bruce Fieldsb86cef62014-03-23 12:01:48 -04002549 bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2550 }
2551 /*
2552 * Largest of remaining attributes are 16 bytes (e.g.,
2553 * supported_attributes)
2554 */
2555 ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
2556 /* bitmask, length */
2557 ret += 20;
2558 return ret;
2559}
2560
Kinglong Mee2282cd22017-02-03 22:36:00 +08002561static inline u32 nfsd4_getfh_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2562{
2563 return (op_encode_hdr_size + 1) * sizeof(__be32) + NFS4_FHSIZE;
2564}
2565
Mi Jinlong58e7b332011-08-28 18:18:56 +08002566static inline u32 nfsd4_link_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2567{
2568 return (op_encode_hdr_size + op_encode_change_info_maxsz)
2569 * sizeof(__be32);
2570}
2571
2572static inline u32 nfsd4_lock_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2573{
2574 return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
2575 * sizeof(__be32);
2576}
2577
2578static inline u32 nfsd4_open_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2579{
2580 return (op_encode_hdr_size + op_encode_stateid_maxsz
2581 + op_encode_change_info_maxsz + 1
2582 + nfs4_fattr_bitmap_maxsz
2583 + op_encode_delegation_maxsz) * sizeof(__be32);
2584}
2585
2586static inline u32 nfsd4_read_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2587{
2588 u32 maxcount = 0, rlen = 0;
2589
2590 maxcount = svc_max_payload(rqstp);
Kinglong Mee3c7aa152014-06-10 18:08:19 +08002591 rlen = min(op->u.read.rd_length, maxcount);
Mi Jinlong58e7b332011-08-28 18:18:56 +08002592
J. Bruce Fields622f5602014-05-16 14:38:20 -04002593 return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
Mi Jinlong58e7b332011-08-28 18:18:56 +08002594}
2595
Anna Schumaker528b8492020-09-28 13:08:58 -04002596static inline u32 nfsd4_read_plus_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2597{
2598 u32 maxcount = svc_max_payload(rqstp);
2599 u32 rlen = min(op->u.read.rd_length, maxcount);
2600 /*
2601 * If we detect that the file changed during hole encoding, then we
2602 * recover by encoding the remaining reply as data. This means we need
2603 * to set aside enough room to encode two data segments.
2604 */
2605 u32 seg_len = 2 * (1 + 2 + 1);
2606
2607 return (op_encode_hdr_size + 2 + seg_len + XDR_QUADLEN(rlen)) * sizeof(__be32);
2608}
2609
Mi Jinlong58e7b332011-08-28 18:18:56 +08002610static inline u32 nfsd4_readdir_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2611{
Kinglong Mee3c7aa152014-06-10 18:08:19 +08002612 u32 maxcount = 0, rlen = 0;
Mi Jinlong58e7b332011-08-28 18:18:56 +08002613
Kinglong Mee3c7aa152014-06-10 18:08:19 +08002614 maxcount = svc_max_payload(rqstp);
2615 rlen = min(op->u.readdir.rd_maxcount, maxcount);
Mi Jinlong58e7b332011-08-28 18:18:56 +08002616
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002617 return (op_encode_hdr_size + op_encode_verifier_maxsz +
2618 XDR_QUADLEN(rlen)) * sizeof(__be32);
Mi Jinlong58e7b332011-08-28 18:18:56 +08002619}
2620
Kinglong Mee2282cd22017-02-03 22:36:00 +08002621static inline u32 nfsd4_readlink_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2622{
2623 return (op_encode_hdr_size + 1) * sizeof(__be32) + PAGE_SIZE;
2624}
2625
Mi Jinlong58e7b332011-08-28 18:18:56 +08002626static inline u32 nfsd4_remove_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2627{
2628 return (op_encode_hdr_size + op_encode_change_info_maxsz)
2629 * sizeof(__be32);
2630}
2631
2632static inline u32 nfsd4_rename_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2633{
2634 return (op_encode_hdr_size + op_encode_change_info_maxsz
2635 + op_encode_change_info_maxsz) * sizeof(__be32);
2636}
2637
J. Bruce Fieldsccae70a2014-03-23 09:34:22 -07002638static inline u32 nfsd4_sequence_rsize(struct svc_rqst *rqstp,
2639 struct nfsd4_op *op)
2640{
J. Bruce Fieldsd1d84c92014-08-21 15:04:31 -04002641 return (op_encode_hdr_size
2642 + XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32);
J. Bruce Fieldsccae70a2014-03-23 09:34:22 -07002643}
2644
Kinglong Mee2282cd22017-02-03 22:36:00 +08002645static inline u32 nfsd4_test_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2646{
2647 return (op_encode_hdr_size + 1 + op->u.test_stateid.ts_num_ids)
2648 * sizeof(__be32);
2649}
2650
Mi Jinlong58e7b332011-08-28 18:18:56 +08002651static inline u32 nfsd4_setattr_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2652{
2653 return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
2654}
2655
Kinglong Mee2282cd22017-02-03 22:36:00 +08002656static inline u32 nfsd4_secinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2657{
2658 return (op_encode_hdr_size + RPC_AUTH_MAXFLAVOR *
2659 (4 + XDR_QUADLEN(GSS_OID_MAX_LEN))) * sizeof(__be32);
2660}
2661
Mi Jinlong58e7b332011-08-28 18:18:56 +08002662static inline u32 nfsd4_setclientid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2663{
J. Bruce Fields480efae2014-03-10 14:17:55 -04002664 return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
2665 sizeof(__be32);
Mi Jinlong58e7b332011-08-28 18:18:56 +08002666}
2667
2668static inline u32 nfsd4_write_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2669{
J. Bruce Fieldsf34e4322014-05-16 21:24:56 -04002670 return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
Mi Jinlong58e7b332011-08-28 18:18:56 +08002671}
2672
2673static inline u32 nfsd4_exchange_id_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2674{
2675 return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
Kinglong Meea8bb84b2013-12-10 15:24:36 +08002676 1 + 1 + /* eir_flags, spr_how */\
2677 4 + /* spo_must_enforce & _allow with bitmap */\
Mi Jinlong58e7b332011-08-28 18:18:56 +08002678 2 + /*eir_server_owner.so_minor_id */\
2679 /* eir_server_owner.so_major_id<> */\
2680 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
2681 /* eir_server_scope<> */\
2682 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
2683 1 + /* eir_server_impl_id array length */\
2684 0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
2685}
2686
2687static inline u32 nfsd4_bind_conn_to_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2688{
2689 return (op_encode_hdr_size + \
2690 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
2691 2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
2692}
2693
2694static inline u32 nfsd4_create_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2695{
2696 return (op_encode_hdr_size + \
2697 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
2698 2 + /* csr_sequence, csr_flags */\
2699 op_encode_channel_attrs_maxsz + \
2700 op_encode_channel_attrs_maxsz) * sizeof(__be32);
2701}
2702
Anna Schumaker29ae7f92016-09-07 15:57:30 -04002703static inline u32 nfsd4_copy_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2704{
2705 return (op_encode_hdr_size +
2706 1 /* wr_callback */ +
2707 op_encode_stateid_maxsz /* wr_callback */ +
2708 2 /* wr_count */ +
2709 1 /* wr_committed */ +
2710 op_encode_verifier_maxsz +
2711 1 /* cr_consecutive */ +
2712 1 /* cr_synchronous */) * sizeof(__be32);
2713}
2714
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04002715static inline u32 nfsd4_offload_status_rsize(struct svc_rqst *rqstp,
2716 struct nfsd4_op *op)
2717{
2718 return (op_encode_hdr_size +
2719 2 /* osr_count */ +
2720 1 /* osr_complete<1> optional 0 for now */) * sizeof(__be32);
2721}
2722
Olga Kornievskaia51911862019-08-08 11:14:59 -04002723static inline u32 nfsd4_copy_notify_rsize(struct svc_rqst *rqstp,
2724 struct nfsd4_op *op)
2725{
2726 return (op_encode_hdr_size +
2727 3 /* cnr_lease_time */ +
2728 1 /* We support one cnr_source_server */ +
2729 1 /* cnr_stateid seq */ +
2730 op_encode_stateid_maxsz /* cnr_stateid */ +
2731 1 /* num cnr_source_server*/ +
2732 1 /* nl4_type */ +
2733 1 /* nl4 size */ +
2734 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) /*nl4_loc + nl4_loc_sz */)
2735 * sizeof(__be32);
2736}
2737
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002738#ifdef CONFIG_NFSD_PNFS
Kinglong Mee2282cd22017-02-03 22:36:00 +08002739static inline u32 nfsd4_getdeviceinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2740{
2741 u32 maxcount = 0, rlen = 0;
2742
2743 maxcount = svc_max_payload(rqstp);
2744 rlen = min(op->u.getdeviceinfo.gd_maxcount, maxcount);
2745
2746 return (op_encode_hdr_size +
2747 1 /* gd_layout_type*/ +
2748 XDR_QUADLEN(rlen) +
2749 2 /* gd_notify_types */) * sizeof(__be32);
2750}
2751
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002752/*
2753 * At this stage we don't really know what layout driver will handle the request,
2754 * so we need to define an arbitrary upper bound here.
2755 */
2756#define MAX_LAYOUT_SIZE 128
2757static inline u32 nfsd4_layoutget_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2758{
2759 return (op_encode_hdr_size +
2760 1 /* logr_return_on_close */ +
2761 op_encode_stateid_maxsz +
2762 1 /* nr of layouts */ +
2763 MAX_LAYOUT_SIZE) * sizeof(__be32);
2764}
2765
2766static inline u32 nfsd4_layoutcommit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2767{
2768 return (op_encode_hdr_size +
2769 1 /* locr_newsize */ +
2770 2 /* ns_size */) * sizeof(__be32);
2771}
2772
2773static inline u32 nfsd4_layoutreturn_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2774{
2775 return (op_encode_hdr_size +
2776 1 /* lrs_stateid */ +
2777 op_encode_stateid_maxsz) * sizeof(__be32);
2778}
2779#endif /* CONFIG_NFSD_PNFS */
2780
Kinglong Mee2282cd22017-02-03 22:36:00 +08002781
2782static inline u32 nfsd4_seek_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2783{
2784 return (op_encode_hdr_size + 3) * sizeof(__be32);
2785}
2786
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002787static inline u32 nfsd4_getxattr_rsize(struct svc_rqst *rqstp,
2788 struct nfsd4_op *op)
2789{
2790 u32 maxcount, rlen;
2791
2792 maxcount = svc_max_payload(rqstp);
2793 rlen = min_t(u32, XATTR_SIZE_MAX, maxcount);
2794
2795 return (op_encode_hdr_size + 1 + XDR_QUADLEN(rlen)) * sizeof(__be32);
2796}
2797
2798static inline u32 nfsd4_setxattr_rsize(struct svc_rqst *rqstp,
2799 struct nfsd4_op *op)
2800{
2801 return (op_encode_hdr_size + op_encode_change_info_maxsz)
2802 * sizeof(__be32);
2803}
2804static inline u32 nfsd4_listxattrs_rsize(struct svc_rqst *rqstp,
2805 struct nfsd4_op *op)
2806{
2807 u32 maxcount, rlen;
2808
2809 maxcount = svc_max_payload(rqstp);
2810 rlen = min(op->u.listxattrs.lsxa_maxcount, maxcount);
2811
2812 return (op_encode_hdr_size + 4 + XDR_QUADLEN(rlen)) * sizeof(__be32);
2813}
2814
2815static inline u32 nfsd4_removexattr_rsize(struct svc_rqst *rqstp,
2816 struct nfsd4_op *op)
2817{
2818 return (op_encode_hdr_size + op_encode_change_info_maxsz)
2819 * sizeof(__be32);
2820}
2821
2822
Christoph Hellwigbb2a8b02017-05-08 23:46:47 +02002823static const struct nfsd4_operation nfsd4_ops[] = {
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002824 [OP_ACCESS] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002825 .op_func = nfsd4_access,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002826 .op_name = "OP_ACCESS",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002827 .op_rsize_bop = nfsd4_access_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002828 },
2829 [OP_CLOSE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002830 .op_func = nfsd4_close,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002831 .op_flags = OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002832 .op_name = "OP_CLOSE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002833 .op_rsize_bop = nfsd4_status_stateid_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02002834 .op_get_currentstateid = nfsd4_get_closestateid,
Christoph Hellwigb60e9852017-05-08 20:03:15 +02002835 .op_set_currentstateid = nfsd4_set_closestateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002836 },
2837 [OP_COMMIT] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002838 .op_func = nfsd4_commit,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002839 .op_flags = OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002840 .op_name = "OP_COMMIT",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002841 .op_rsize_bop = nfsd4_commit_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002842 },
2843 [OP_CREATE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002844 .op_func = nfsd4_create,
Tigran Mkrtchyand1471052012-02-13 22:55:29 +01002845 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002846 .op_name = "OP_CREATE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002847 .op_rsize_bop = nfsd4_create_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002848 },
2849 [OP_DELEGRETURN] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002850 .op_func = nfsd4_delegreturn,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002851 .op_flags = OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002852 .op_name = "OP_DELEGRETURN",
Mi Jinlong58e7b332011-08-28 18:18:56 +08002853 .op_rsize_bop = nfsd4_only_status_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02002854 .op_get_currentstateid = nfsd4_get_delegreturnstateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002855 },
2856 [OP_GETATTR] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002857 .op_func = nfsd4_getattr,
J.Bruce Fieldseeac2942006-12-13 00:35:39 -08002858 .op_flags = ALLOWED_ON_ABSENT_FS,
J. Bruce Fieldsb86cef62014-03-23 12:01:48 -04002859 .op_rsize_bop = nfsd4_getattr_rsize,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002860 .op_name = "OP_GETATTR",
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002861 },
2862 [OP_GETFH] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002863 .op_func = nfsd4_getfh,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002864 .op_name = "OP_GETFH",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002865 .op_rsize_bop = nfsd4_getfh_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002866 },
2867 [OP_LINK] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002868 .op_func = nfsd4_link,
J. Bruce Fieldsc8566942011-09-20 08:49:51 -04002869 .op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
2870 | OP_CACHEME,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002871 .op_name = "OP_LINK",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002872 .op_rsize_bop = nfsd4_link_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002873 },
2874 [OP_LOCK] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002875 .op_func = nfsd4_lock,
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04002876 .op_flags = OP_MODIFIES_SOMETHING |
2877 OP_NONTRIVIAL_ERROR_ENCODE,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002878 .op_name = "OP_LOCK",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002879 .op_rsize_bop = nfsd4_lock_rsize,
Christoph Hellwigb60e9852017-05-08 20:03:15 +02002880 .op_set_currentstateid = nfsd4_set_lockstateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002881 },
2882 [OP_LOCKT] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002883 .op_func = nfsd4_lockt,
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04002884 .op_flags = OP_NONTRIVIAL_ERROR_ENCODE,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002885 .op_name = "OP_LOCKT",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002886 .op_rsize_bop = nfsd4_lock_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002887 },
2888 [OP_LOCKU] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002889 .op_func = nfsd4_locku,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002890 .op_flags = OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002891 .op_name = "OP_LOCKU",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002892 .op_rsize_bop = nfsd4_status_stateid_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02002893 .op_get_currentstateid = nfsd4_get_lockustateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002894 },
2895 [OP_LOOKUP] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002896 .op_func = nfsd4_lookup,
Tigran Mkrtchyand1471052012-02-13 22:55:29 +01002897 .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002898 .op_name = "OP_LOOKUP",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002899 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002900 },
2901 [OP_LOOKUPP] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002902 .op_func = nfsd4_lookupp,
Tigran Mkrtchyand1471052012-02-13 22:55:29 +01002903 .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002904 .op_name = "OP_LOOKUPP",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002905 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002906 },
2907 [OP_NVERIFY] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002908 .op_func = nfsd4_nverify,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002909 .op_name = "OP_NVERIFY",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002910 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002911 },
2912 [OP_OPEN] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002913 .op_func = nfsd4_open,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002914 .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002915 .op_name = "OP_OPEN",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002916 .op_rsize_bop = nfsd4_open_rsize,
Christoph Hellwigb60e9852017-05-08 20:03:15 +02002917 .op_set_currentstateid = nfsd4_set_openstateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002918 },
2919 [OP_OPEN_CONFIRM] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002920 .op_func = nfsd4_open_confirm,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002921 .op_flags = OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002922 .op_name = "OP_OPEN_CONFIRM",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002923 .op_rsize_bop = nfsd4_status_stateid_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002924 },
2925 [OP_OPEN_DOWNGRADE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002926 .op_func = nfsd4_open_downgrade,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002927 .op_flags = OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002928 .op_name = "OP_OPEN_DOWNGRADE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002929 .op_rsize_bop = nfsd4_status_stateid_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02002930 .op_get_currentstateid = nfsd4_get_opendowngradestateid,
Christoph Hellwigb60e9852017-05-08 20:03:15 +02002931 .op_set_currentstateid = nfsd4_set_opendowngradestateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002932 },
2933 [OP_PUTFH] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002934 .op_func = nfsd4_putfh,
J. Bruce Fields68d93182011-04-08 17:00:50 -04002935 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
J. Bruce Fields5b648692014-03-07 11:43:58 -05002936 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002937 .op_name = "OP_PUTFH",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002938 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002939 },
J.Bruce Fieldseeac2942006-12-13 00:35:39 -08002940 [OP_PUTPUBFH] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002941 .op_func = nfsd4_putrootfh,
J. Bruce Fields68d93182011-04-08 17:00:50 -04002942 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
J. Bruce Fields5b648692014-03-07 11:43:58 -05002943 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002944 .op_name = "OP_PUTPUBFH",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002945 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldseeac2942006-12-13 00:35:39 -08002946 },
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002947 [OP_PUTROOTFH] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002948 .op_func = nfsd4_putrootfh,
J. Bruce Fields68d93182011-04-08 17:00:50 -04002949 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
J. Bruce Fields5b648692014-03-07 11:43:58 -05002950 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002951 .op_name = "OP_PUTROOTFH",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002952 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002953 },
2954 [OP_READ] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002955 .op_func = nfsd4_read,
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04002956 .op_release = nfsd4_read_release,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002957 .op_name = "OP_READ",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002958 .op_rsize_bop = nfsd4_read_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02002959 .op_get_currentstateid = nfsd4_get_readstateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002960 },
2961 [OP_READDIR] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002962 .op_func = nfsd4_readdir,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002963 .op_name = "OP_READDIR",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002964 .op_rsize_bop = nfsd4_readdir_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002965 },
2966 [OP_READLINK] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002967 .op_func = nfsd4_readlink,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002968 .op_name = "OP_READLINK",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002969 .op_rsize_bop = nfsd4_readlink_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002970 },
2971 [OP_REMOVE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002972 .op_func = nfsd4_remove,
J. Bruce Fieldsc8566942011-09-20 08:49:51 -04002973 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002974 .op_name = "OP_REMOVE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002975 .op_rsize_bop = nfsd4_remove_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002976 },
2977 [OP_RENAME] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002978 .op_func = nfsd4_rename,
J. Bruce Fieldsc8566942011-09-20 08:49:51 -04002979 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002980 .op_name = "OP_RENAME",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002981 .op_rsize_bop = nfsd4_rename_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002982 },
2983 [OP_RENEW] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002984 .op_func = nfsd4_renew,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002985 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
2986 | OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002987 .op_name = "OP_RENEW",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002988 .op_rsize_bop = nfsd4_only_status_rsize,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002989
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002990 },
2991 [OP_RESTOREFH] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002992 .op_func = nfsd4_restorefh,
J. Bruce Fields68d93182011-04-08 17:00:50 -04002993 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
Mi Jinlong58e7b332011-08-28 18:18:56 +08002994 | OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002995 .op_name = "OP_RESTOREFH",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002996 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002997 },
2998 [OP_SAVEFH] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002999 .op_func = nfsd4_savefh,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003000 .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03003001 .op_name = "OP_SAVEFH",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003002 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003003 },
Andy Adamsondcb488a32007-07-17 04:04:51 -07003004 [OP_SECINFO] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003005 .op_func = nfsd4_secinfo,
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04003006 .op_release = nfsd4_secinfo_release,
J. Bruce Fields68d93182011-04-08 17:00:50 -04003007 .op_flags = OP_HANDLES_WRONGSEC,
Benny Halevyb001a1b2008-07-02 11:15:03 +03003008 .op_name = "OP_SECINFO",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003009 .op_rsize_bop = nfsd4_secinfo_rsize,
Andy Adamsondcb488a32007-07-17 04:04:51 -07003010 },
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003011 [OP_SETATTR] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003012 .op_func = nfsd4_setattr,
Benny Halevyb001a1b2008-07-02 11:15:03 +03003013 .op_name = "OP_SETATTR",
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04003014 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME
3015 | OP_NONTRIVIAL_ERROR_ENCODE,
Christoph Hellwig1c122632017-05-08 20:42:10 +02003016 .op_rsize_bop = nfsd4_setattr_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02003017 .op_get_currentstateid = nfsd4_get_setattrstateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003018 },
3019 [OP_SETCLIENTID] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003020 .op_func = nfsd4_setclientid,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003021 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04003022 | OP_MODIFIES_SOMETHING | OP_CACHEME
3023 | OP_NONTRIVIAL_ERROR_ENCODE,
Benny Halevyb001a1b2008-07-02 11:15:03 +03003024 .op_name = "OP_SETCLIENTID",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003025 .op_rsize_bop = nfsd4_setclientid_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003026 },
3027 [OP_SETCLIENTID_CONFIRM] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003028 .op_func = nfsd4_setclientid_confirm,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003029 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
J. Bruce Fieldsc8566942011-09-20 08:49:51 -04003030 | OP_MODIFIES_SOMETHING | OP_CACHEME,
Benny Halevyb001a1b2008-07-02 11:15:03 +03003031 .op_name = "OP_SETCLIENTID_CONFIRM",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003032 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003033 },
3034 [OP_VERIFY] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003035 .op_func = nfsd4_verify,
Benny Halevyb001a1b2008-07-02 11:15:03 +03003036 .op_name = "OP_VERIFY",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003037 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003038 },
3039 [OP_WRITE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003040 .op_func = nfsd4_write,
J. Bruce Fieldsc8566942011-09-20 08:49:51 -04003041 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
Benny Halevyb001a1b2008-07-02 11:15:03 +03003042 .op_name = "OP_WRITE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003043 .op_rsize_bop = nfsd4_write_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02003044 .op_get_currentstateid = nfsd4_get_writestateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003045 },
3046 [OP_RELEASE_LOCKOWNER] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003047 .op_func = nfsd4_release_lockowner,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003048 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3049 | OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03003050 .op_name = "OP_RELEASE_LOCKOWNER",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003051 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003052 },
Andy Adamson069b6ad2009-04-03 08:27:58 +03003053
3054 /* NFSv4.1 operations */
3055 [OP_EXCHANGE_ID] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003056 .op_func = nfsd4_exchange_id,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003057 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3058 | OP_MODIFIES_SOMETHING,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003059 .op_name = "OP_EXCHANGE_ID",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003060 .op_rsize_bop = nfsd4_exchange_id_rsize,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003061 },
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003062 [OP_BACKCHANNEL_CTL] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003063 .op_func = nfsd4_backchannel_ctl,
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003064 .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
3065 .op_name = "OP_BACKCHANNEL_CTL",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003066 .op_rsize_bop = nfsd4_only_status_rsize,
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003067 },
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003068 [OP_BIND_CONN_TO_SESSION] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003069 .op_func = nfsd4_bind_conn_to_session,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003070 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3071 | OP_MODIFIES_SOMETHING,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003072 .op_name = "OP_BIND_CONN_TO_SESSION",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003073 .op_rsize_bop = nfsd4_bind_conn_to_session_rsize,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003074 },
Andy Adamson069b6ad2009-04-03 08:27:58 +03003075 [OP_CREATE_SESSION] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003076 .op_func = nfsd4_create_session,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003077 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3078 | OP_MODIFIES_SOMETHING,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003079 .op_name = "OP_CREATE_SESSION",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003080 .op_rsize_bop = nfsd4_create_session_rsize,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003081 },
3082 [OP_DESTROY_SESSION] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003083 .op_func = nfsd4_destroy_session,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003084 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3085 | OP_MODIFIES_SOMETHING,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003086 .op_name = "OP_DESTROY_SESSION",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003087 .op_rsize_bop = nfsd4_only_status_rsize,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003088 },
3089 [OP_SEQUENCE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003090 .op_func = nfsd4_sequence,
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003091 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003092 .op_name = "OP_SEQUENCE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003093 .op_rsize_bop = nfsd4_sequence_rsize,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003094 },
Benny Halevy094b5d72011-06-16 11:39:10 -04003095 [OP_DESTROY_CLIENTID] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003096 .op_func = nfsd4_destroy_clientid,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003097 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3098 | OP_MODIFIES_SOMETHING,
Benny Halevy094b5d72011-06-16 11:39:10 -04003099 .op_name = "OP_DESTROY_CLIENTID",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003100 .op_rsize_bop = nfsd4_only_status_rsize,
Benny Halevy094b5d72011-06-16 11:39:10 -04003101 },
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003102 [OP_RECLAIM_COMPLETE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003103 .op_func = nfsd4_reclaim_complete,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003104 .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003105 .op_name = "OP_RECLAIM_COMPLETE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003106 .op_rsize_bop = nfsd4_only_status_rsize,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003107 },
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05003108 [OP_SECINFO_NO_NAME] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003109 .op_func = nfsd4_secinfo_no_name,
Eryu Guanec572b92017-09-29 15:01:10 +08003110 .op_release = nfsd4_secinfo_no_name_release,
J. Bruce Fields68d93182011-04-08 17:00:50 -04003111 .op_flags = OP_HANDLES_WRONGSEC,
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05003112 .op_name = "OP_SECINFO_NO_NAME",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003113 .op_rsize_bop = nfsd4_secinfo_rsize,
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05003114 },
Bryan Schumaker17456802011-07-13 10:50:48 -04003115 [OP_TEST_STATEID] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003116 .op_func = nfsd4_test_stateid,
Bryan Schumaker17456802011-07-13 10:50:48 -04003117 .op_flags = ALLOWED_WITHOUT_FH,
3118 .op_name = "OP_TEST_STATEID",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003119 .op_rsize_bop = nfsd4_test_stateid_rsize,
Bryan Schumaker17456802011-07-13 10:50:48 -04003120 },
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003121 [OP_FREE_STATEID] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003122 .op_func = nfsd4_free_stateid,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003123 .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003124 .op_name = "OP_FREE_STATEID",
Christoph Hellwig57832e72017-05-08 20:37:33 +02003125 .op_get_currentstateid = nfsd4_get_freestateid,
Christoph Hellwig1c122632017-05-08 20:42:10 +02003126 .op_rsize_bop = nfsd4_only_status_rsize,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003127 },
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003128#ifdef CONFIG_NFSD_PNFS
3129 [OP_GETDEVICEINFO] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003130 .op_func = nfsd4_getdeviceinfo,
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04003131 .op_release = nfsd4_getdeviceinfo_release,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003132 .op_flags = ALLOWED_WITHOUT_FH,
3133 .op_name = "OP_GETDEVICEINFO",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003134 .op_rsize_bop = nfsd4_getdeviceinfo_rsize,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003135 },
3136 [OP_LAYOUTGET] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003137 .op_func = nfsd4_layoutget,
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04003138 .op_release = nfsd4_layoutget_release,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003139 .op_flags = OP_MODIFIES_SOMETHING,
3140 .op_name = "OP_LAYOUTGET",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003141 .op_rsize_bop = nfsd4_layoutget_rsize,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003142 },
3143 [OP_LAYOUTCOMMIT] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003144 .op_func = nfsd4_layoutcommit,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003145 .op_flags = OP_MODIFIES_SOMETHING,
3146 .op_name = "OP_LAYOUTCOMMIT",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003147 .op_rsize_bop = nfsd4_layoutcommit_rsize,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003148 },
3149 [OP_LAYOUTRETURN] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003150 .op_func = nfsd4_layoutreturn,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003151 .op_flags = OP_MODIFIES_SOMETHING,
3152 .op_name = "OP_LAYOUTRETURN",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003153 .op_rsize_bop = nfsd4_layoutreturn_rsize,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003154 },
3155#endif /* CONFIG_NFSD_PNFS */
Anna Schumaker24bab492014-09-26 13:58:27 -04003156
3157 /* NFSv4.2 operations */
Anna Schumaker95d871f2014-11-07 14:44:26 -05003158 [OP_ALLOCATE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003159 .op_func = nfsd4_allocate,
Olga Kornievskaia03b31f42018-12-04 14:09:21 -05003160 .op_flags = OP_MODIFIES_SOMETHING,
Anna Schumaker95d871f2014-11-07 14:44:26 -05003161 .op_name = "OP_ALLOCATE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003162 .op_rsize_bop = nfsd4_only_status_rsize,
Anna Schumaker95d871f2014-11-07 14:44:26 -05003163 },
Anna Schumakerb0cb9082014-11-07 14:44:27 -05003164 [OP_DEALLOCATE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003165 .op_func = nfsd4_deallocate,
Olga Kornievskaia03b31f42018-12-04 14:09:21 -05003166 .op_flags = OP_MODIFIES_SOMETHING,
Anna Schumakerb0cb9082014-11-07 14:44:27 -05003167 .op_name = "OP_DEALLOCATE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003168 .op_rsize_bop = nfsd4_only_status_rsize,
Anna Schumakerb0cb9082014-11-07 14:44:27 -05003169 },
Christoph Hellwigffa01602015-12-03 12:59:52 +01003170 [OP_CLONE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003171 .op_func = nfsd4_clone,
Olga Kornievskaia03b31f42018-12-04 14:09:21 -05003172 .op_flags = OP_MODIFIES_SOMETHING,
Christoph Hellwigffa01602015-12-03 12:59:52 +01003173 .op_name = "OP_CLONE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003174 .op_rsize_bop = nfsd4_only_status_rsize,
Christoph Hellwigffa01602015-12-03 12:59:52 +01003175 },
Anna Schumaker29ae7f92016-09-07 15:57:30 -04003176 [OP_COPY] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003177 .op_func = nfsd4_copy,
Olga Kornievskaia03b31f42018-12-04 14:09:21 -05003178 .op_flags = OP_MODIFIES_SOMETHING,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04003179 .op_name = "OP_COPY",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003180 .op_rsize_bop = nfsd4_copy_rsize,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04003181 },
Anna Schumaker528b8492020-09-28 13:08:58 -04003182 [OP_READ_PLUS] = {
3183 .op_func = nfsd4_read,
3184 .op_release = nfsd4_read_release,
3185 .op_name = "OP_READ_PLUS",
3186 .op_rsize_bop = nfsd4_read_plus_rsize,
3187 .op_get_currentstateid = nfsd4_get_readstateid,
3188 },
Anna Schumaker24bab492014-09-26 13:58:27 -04003189 [OP_SEEK] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003190 .op_func = nfsd4_seek,
Anna Schumaker24bab492014-09-26 13:58:27 -04003191 .op_name = "OP_SEEK",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003192 .op_rsize_bop = nfsd4_seek_rsize,
Anna Schumaker24bab492014-09-26 13:58:27 -04003193 },
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04003194 [OP_OFFLOAD_STATUS] = {
3195 .op_func = nfsd4_offload_status,
3196 .op_name = "OP_OFFLOAD_STATUS",
3197 .op_rsize_bop = nfsd4_offload_status_rsize,
3198 },
Olga Kornievskaia885e2bf2018-07-20 18:19:19 -04003199 [OP_OFFLOAD_CANCEL] = {
3200 .op_func = nfsd4_offload_cancel,
3201 .op_flags = OP_MODIFIES_SOMETHING,
3202 .op_name = "OP_OFFLOAD_CANCEL",
3203 .op_rsize_bop = nfsd4_only_status_rsize,
3204 },
Olga Kornievskaia51911862019-08-08 11:14:59 -04003205 [OP_COPY_NOTIFY] = {
3206 .op_func = nfsd4_copy_notify,
3207 .op_flags = OP_MODIFIES_SOMETHING,
3208 .op_name = "OP_COPY_NOTIFY",
3209 .op_rsize_bop = nfsd4_copy_notify_rsize,
3210 },
Frank van der Linden23e50fe2020-06-23 22:39:26 +00003211 [OP_GETXATTR] = {
3212 .op_func = nfsd4_getxattr,
3213 .op_name = "OP_GETXATTR",
3214 .op_rsize_bop = nfsd4_getxattr_rsize,
3215 },
3216 [OP_SETXATTR] = {
3217 .op_func = nfsd4_setxattr,
3218 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3219 .op_name = "OP_SETXATTR",
3220 .op_rsize_bop = nfsd4_setxattr_rsize,
3221 },
3222 [OP_LISTXATTRS] = {
3223 .op_func = nfsd4_listxattrs,
3224 .op_name = "OP_LISTXATTRS",
3225 .op_rsize_bop = nfsd4_listxattrs_rsize,
3226 },
3227 [OP_REMOVEXATTR] = {
3228 .op_func = nfsd4_removexattr,
3229 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3230 .op_name = "OP_REMOVEXATTR",
3231 .op_rsize_bop = nfsd4_removexattr_rsize,
3232 },
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003233};
3234
Andrew Elbleed941642016-06-15 12:52:09 -04003235/**
3236 * nfsd4_spo_must_allow - Determine if the compound op contains an
3237 * operation that is allowed to be sent with machine credentials
3238 *
3239 * @rqstp: a pointer to the struct svc_rqst
3240 *
3241 * Checks to see if the compound contains a spo_must_allow op
3242 * and confirms that it was sent with the proper machine creds.
3243 */
3244
3245bool nfsd4_spo_must_allow(struct svc_rqst *rqstp)
3246{
3247 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3248 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3249 struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
3250 struct nfsd4_compound_state *cstate = &resp->cstate;
3251 struct nfs4_op_map *allow = &cstate->clp->cl_spo_must_allow;
3252 u32 opiter;
3253
3254 if (!cstate->minorversion)
3255 return false;
3256
Zheng Bin44b49aa2020-09-11 12:10:14 +08003257 if (cstate->spo_must_allowed)
Andrew Elbleed941642016-06-15 12:52:09 -04003258 return true;
3259
3260 opiter = resp->opcnt;
3261 while (opiter < argp->opcnt) {
3262 this = &argp->ops[opiter++];
3263 if (test_bit(this->opnum, allow->u.longs) &&
3264 cstate->clp->cl_mach_cred &&
3265 nfsd4_mach_creds_match(cstate->clp, rqstp)) {
3266 cstate->spo_must_allowed = true;
3267 return true;
3268 }
3269 }
3270 cstate->spo_must_allowed = false;
3271 return false;
3272}
3273
J. Bruce Fields4f0cefb2014-03-11 15:39:13 -04003274int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
3275{
Olga Kornievskaia05b72782017-03-23 14:36:20 -04003276 if (op->opnum == OP_ILLEGAL || op->status == nfserr_notsupp)
J. Bruce Fields4f0cefb2014-03-11 15:39:13 -04003277 return op_encode_hdr_size * sizeof(__be32);
Kinglong Mee2282cd22017-02-03 22:36:00 +08003278
3279 BUG_ON(OPDESC(op)->op_rsize_bop == NULL);
3280 return OPDESC(op)->op_rsize_bop(rqstp, op);
J. Bruce Fields4f0cefb2014-03-11 15:39:13 -04003281}
3282
J. Bruce Fields07d1f802014-03-06 20:39:29 -05003283void warn_on_nonidempotent_op(struct nfsd4_op *op)
3284{
3285 if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
Chuck Lever3a237b42020-11-22 12:49:52 -05003286 pr_err("unable to encode reply to nonidempotent op %u (%s)\n",
J. Bruce Fields07d1f802014-03-06 20:39:29 -05003287 op->opnum, nfsd4_op_name(op->opnum));
3288 WARN_ON_ONCE(1);
3289 }
3290}
3291
Adrian Bunkf1c7f792008-08-08 19:26:42 +03003292static const char *nfsd4_op_name(unsigned opnum)
Benny Halevyb001a1b2008-07-02 11:15:03 +03003293{
3294 if (opnum < ARRAY_SIZE(nfsd4_ops))
3295 return nfsd4_ops[opnum].op_name;
3296 return "unknown_operation";
3297}
3298
Christoph Hellwig860bda22017-05-12 16:11:49 +02003299static const struct svc_procedure nfsd_procedures4[2] = {
Yu Zhiguo0a93a472009-05-19 14:09:54 +08003300 [NFSPROC4_NULL] = {
Christoph Hellwigf7235b62017-05-08 17:59:13 +02003301 .pc_func = nfsd4_proc_null,
Chuck Lever788f7182020-11-05 14:48:29 -05003302 .pc_decode = nfssvc_decode_voidarg,
3303 .pc_encode = nfssvc_encode_voidres,
3304 .pc_argsize = sizeof(struct nfsd_voidargs),
3305 .pc_ressize = sizeof(struct nfsd_voidres),
Yu Zhiguo0a93a472009-05-19 14:09:54 +08003306 .pc_cachetype = RC_NOCACHE,
3307 .pc_xdrressize = 1,
Chuck Lever2289e872020-09-17 17:22:49 -04003308 .pc_name = "NULL",
Yu Zhiguo0a93a472009-05-19 14:09:54 +08003309 },
3310 [NFSPROC4_COMPOUND] = {
Christoph Hellwigf7235b62017-05-08 17:59:13 +02003311 .pc_func = nfsd4_proc_compound,
Christoph Hellwig026fec72017-05-08 19:01:48 +02003312 .pc_decode = nfs4svc_decode_compoundargs,
Christoph Hellwig63f8de32017-05-08 19:42:02 +02003313 .pc_encode = nfs4svc_encode_compoundres,
Yu Zhiguo0a93a472009-05-19 14:09:54 +08003314 .pc_argsize = sizeof(struct nfsd4_compoundargs),
3315 .pc_ressize = sizeof(struct nfsd4_compoundres),
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003316 .pc_release = nfsd4_release_compoundargs,
Yu Zhiguo0a93a472009-05-19 14:09:54 +08003317 .pc_cachetype = RC_NOCACHE,
3318 .pc_xdrressize = NFSD_BUFSIZE/4,
Chuck Lever2289e872020-09-17 17:22:49 -04003319 .pc_name = "COMPOUND",
Yu Zhiguo0a93a472009-05-19 14:09:54 +08003320 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321};
3322
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02003323static unsigned int nfsd_count3[ARRAY_SIZE(nfsd_procedures4)];
Christoph Hellwige9679182017-05-12 16:21:37 +02003324const struct svc_version nfsd_version4 = {
Jeff Layton5283b032017-02-24 13:25:24 -05003325 .vs_vers = 4,
3326 .vs_nproc = 2,
3327 .vs_proc = nfsd_procedures4,
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02003328 .vs_count = nfsd_count3,
Jeff Layton5283b032017-02-24 13:25:24 -05003329 .vs_dispatch = nfsd_dispatch,
3330 .vs_xdrsize = NFS4_SVC_XDRSIZE,
3331 .vs_rpcb_optnl = true,
3332 .vs_need_cong_ctrl = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333};
3334
3335/*
3336 * Local variables:
3337 * c-basic-offset: 8
3338 * End:
3339 */