blob: acdb3cd806a1537da45f975b5f4ee528830b97e3 [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) &&
J. Bruce Fieldsec596592021-01-21 17:57:45 -0500381 !test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags) &&
Mi Jinlongab1350b2011-07-14 15:06:26 +0800382 open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
383 return nfserr_grace;
384
Andy Adamson60adfc52009-04-03 08:28:50 +0300385 if (nfsd4_has_session(cstate))
386 copy_clientid(&open->op_clientid, cstate->session);
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /* check seqid for replay. set nfs4_owner */
Kinglong Mee6cd22662015-07-13 17:32:05 +0800389 status = nfsd4_process_open1(cstate, open, nn);
Al Viroa90b0612006-10-19 23:29:03 -0700390 if (status == nfserr_replay_me) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400391 struct nfs4_replay *rp = &open->op_openowner->oo_owner.so_replay;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800392 fh_put(&cstate->current_fh);
J. Bruce Fieldsa4773c02009-02-02 17:23:10 -0500393 fh_copy_shallow(&cstate->current_fh.fh_handle,
394 &rp->rp_openfh);
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200395 status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (status)
397 dprintk("nfsd4_open: replay failed"
398 " restoring previous filehandle\n");
399 else
Al Viroa90b0612006-10-19 23:29:03 -0700400 status = nfserr_replay_me;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 if (status)
403 goto out;
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800404 if (open->op_xdr_error) {
405 status = open->op_xdr_error;
406 goto out;
407 }
J. Bruce Fieldsfb553c02006-01-18 17:43:36 -0800408
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800409 status = nfsd4_check_open_attributes(rqstp, cstate, open);
410 if (status)
411 goto out;
412
J. Bruce Fieldsfb553c02006-01-18 17:43:36 -0800413 /* Openowner is now set, so sequence id will get bumped. Now we need
414 * these checks before we do any creates: */
J. Bruce Fieldscbd0d512006-02-07 12:58:32 -0800415 status = nfserr_grace;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -0400416 if (opens_in_grace(net) && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fieldscbd0d512006-02-07 12:58:32 -0800417 goto out;
418 status = nfserr_no_grace;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -0400419 if (!opens_in_grace(net) && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fieldscbd0d512006-02-07 12:58:32 -0800420 goto out;
J. Bruce Fieldsfb553c02006-01-18 17:43:36 -0800421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 switch (open->op_claim_type) {
NeilBrown0dd3c192005-06-23 22:02:56 -0700423 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 case NFS4_OPEN_CLAIM_NULL:
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500425 status = do_open_lookup(rqstp, cstate, open, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (status)
427 goto out;
428 break;
429 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields1722b042021-01-21 17:57:44 -0500430 status = nfs4_check_open_reclaim(cstate->clp);
Mi Jinlong0cf99b92011-11-23 10:48:40 +0800431 if (status)
432 goto out;
Jeff Laytonba5378b2014-05-30 09:09:28 -0400433 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fields03f318c2018-06-08 12:28:47 -0400434 reclaim = true;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500435 fallthrough;
J. Bruce Fields8b289b22011-10-19 11:52:12 -0400436 case NFS4_OPEN_CLAIM_FH:
437 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
J. Bruce Fieldsbbc9c362013-03-22 18:03:49 -0400438 status = do_open_fhandle(rqstp, cstate, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (status)
440 goto out;
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500441 resfh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 break;
J. Bruce Fields8b289b22011-10-19 11:52:12 -0400443 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
J. Bruce Fields2fdada02007-07-27 16:10:37 -0400445 dprintk("NFSD: unsupported OPEN claim type %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 open->op_claim_type);
447 status = nfserr_notsupp;
448 goto out;
449 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -0400450 dprintk("NFSD: Invalid OPEN claim type %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 open->op_claim_type);
452 status = nfserr_inval;
453 goto out;
454 }
455 /*
456 * nfsd4_process_open2() does the actual opening of the file. If
457 * successful, it (1) truncates the file if open->op_truncate was
458 * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
459 */
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500460 status = nfsd4_process_open2(rqstp, resfh, open);
Jeff Laytonb3fbfe02014-07-29 21:37:44 -0400461 WARN(status && open->op_created,
462 "nfsd4_process_open2 failed to open newly-created file! status=%u\n",
463 be32_to_cpu(status));
J. Bruce Fields03f318c2018-06-08 12:28:47 -0400464 if (reclaim && !status)
465 nn->somebody_reclaimed = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466out:
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500467 if (resfh && resfh != &cstate->current_fh) {
468 fh_dup2(&cstate->current_fh, resfh);
469 fh_put(resfh);
470 kfree(resfh);
471 }
Jeff Layton42297892015-03-23 10:53:44 -0400472 nfsd4_cleanup_open_state(cstate, open);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -0400473 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return status;
475}
476
477/*
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800478 * OPEN is the only seqid-mutating operation whose decoding can fail
479 * with a seqid-mutating error (specifically, decoding of user names in
480 * the attributes). Therefore we have to do some processing to look up
481 * the stateowner so that we can bump the seqid.
482 */
483static __be32 nfsd4_open_omfg(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_op *op)
484{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200485 struct nfsd4_open *open = &op->u.open;
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800486
487 if (!seqid_mutating_err(ntohl(op->status)))
488 return op->status;
489 if (nfsd4_has_session(cstate))
490 return op->status;
491 open->op_xdr_error = op->status;
Christoph Hellwigeb698532017-05-08 20:58:35 +0200492 return nfsd4_open(rqstp, cstate, &op->u);
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800493}
494
495/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * filehandle-manipulating ops.
497 */
J.Bruce Fields71911552006-12-13 00:35:29 -0800498static __be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800499nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200500 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200502 u->getfh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return nfs_ok;
504}
505
J.Bruce Fields71911552006-12-13 00:35:29 -0800506static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800507nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200508 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200510 struct nfsd4_putfh *putfh = &u->putfh;
Olga Kornievskaiab9e86382019-10-07 10:56:48 -0400511 __be32 ret;
Christoph Hellwigeb698532017-05-08 20:58:35 +0200512
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800513 fh_put(&cstate->current_fh);
514 cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
515 memcpy(&cstate->current_fh.fh_handle.fh_base, putfh->pf_fhval,
516 putfh->pf_fhlen);
Olga Kornievskaiab9e86382019-10-07 10:56:48 -0400517 ret = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
518#ifdef CONFIG_NFSD_V4_2_INTER_SSC
519 if (ret == nfserr_stale && putfh->no_verify) {
520 SET_FH_FLAG(&cstate->current_fh, NFSD4_FH_FOREIGN);
521 ret = 0;
522 }
523#endif
524 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
J.Bruce Fields71911552006-12-13 00:35:29 -0800527static __be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800528nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200529 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Al Virob37ad282006-10-19 23:28:59 -0700531 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800533 fh_put(&cstate->current_fh);
J. Bruce Fieldsdf547ef2007-07-17 04:04:43 -0700534 status = exp_pseudoroot(rqstp, &cstate->current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return status;
536}
537
J.Bruce Fields71911552006-12-13 00:35:29 -0800538static __be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800539nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200540 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800542 if (!cstate->save_fh.fh_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return nfserr_restorefh;
544
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800545 fh_dup2(&cstate->current_fh, &cstate->save_fh);
Olga Kornievskaia51100d22018-09-13 13:58:24 -0400546 if (HAS_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG)) {
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +0100547 memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
Olga Kornievskaia51100d22018-09-13 13:58:24 -0400548 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +0100549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return nfs_ok;
551}
552
J.Bruce Fields71911552006-12-13 00:35:29 -0800553static __be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800554nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200555 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800557 fh_dup2(&cstate->save_fh, &cstate->current_fh);
Olga Kornievskaia51100d22018-09-13 13:58:24 -0400558 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG)) {
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +0100559 memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t));
Olga Kornievskaia51100d22018-09-13 13:58:24 -0400560 SET_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG);
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +0100561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return nfs_ok;
563}
564
565/*
566 * misc nfsv4 ops
567 */
J.Bruce Fields71911552006-12-13 00:35:29 -0800568static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800569nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200570 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200572 struct nfsd4_access *access = &u->access;
Frank van der Lindenc11d7fd2020-06-23 22:39:24 +0000573 u32 access_full;
Christoph Hellwigeb698532017-05-08 20:58:35 +0200574
Frank van der Lindenc11d7fd2020-06-23 22:39:24 +0000575 access_full = NFS3_ACCESS_FULL;
576 if (cstate->minorversion >= 2)
577 access_full |= NFS4_ACCESS_XALIST | NFS4_ACCESS_XAREAD |
578 NFS4_ACCESS_XAWRITE;
579
580 if (access->ac_req_access & ~access_full)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return nfserr_inval;
582
583 access->ac_resp_access = access->ac_req_access;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800584 return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
585 &access->ac_supported);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +0300588static void gen_boot_verifier(nfs4_verifier *verifier, struct net *net)
Chuck Leverab4684d2012-03-02 17:13:50 -0500589{
Trond Myklebust27c438f2019-09-02 13:02:56 -0400590 __be32 *verf = (__be32 *)verifier->data;
Chuck Leverab4684d2012-03-02 17:13:50 -0500591
Trond Myklebust27c438f2019-09-02 13:02:56 -0400592 BUILD_BUG_ON(2*sizeof(*verf) != sizeof(verifier->data));
593
594 nfsd_copy_boot_verifier(verf, net_generic(net, nfsd_net_id));
Chuck Leverab4684d2012-03-02 17:13:50 -0500595}
596
J.Bruce Fields71911552006-12-13 00:35:29 -0800597static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800598nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200599 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200601 struct nfsd4_commit *commit = &u->commit;
602
J. Bruce Fields75c096f2011-08-15 18:39:32 -0400603 return nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
Trond Myklebust524ff1a2020-01-06 13:40:36 -0500604 commit->co_count,
605 (__be32 *)commit->co_verf.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Al Virob37ad282006-10-19 23:28:59 -0700608static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800609nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200610 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200612 struct nfsd4_create *create = &u->create;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 struct svc_fh resfh;
Al Virob37ad282006-10-19 23:28:59 -0700614 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 dev_t rdev;
616
617 fh_init(&resfh, NFS4_FHSIZE);
618
J. Bruce Fieldsfa081392016-07-21 16:00:12 -0400619 status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (status)
621 return status;
622
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800623 status = check_attr_support(rqstp, cstate, create->cr_bmval,
624 nfsd_attrmask);
625 if (status)
626 return status;
627
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400628 current->fs->umask = create->cr_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 switch (create->cr_type) {
630 case NF4LNK:
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800631 status = nfsd_symlink(rqstp, &cstate->current_fh,
632 create->cr_name, create->cr_namelen,
Kinglong Mee1e444f52014-07-01 17:48:02 +0800633 create->cr_data, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 break;
635
636 case NF4BLK:
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400637 status = nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
639 if (MAJOR(rdev) != create->cr_specdata1 ||
640 MINOR(rdev) != create->cr_specdata2)
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400641 goto out_umask;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800642 status = nfsd_create(rqstp, &cstate->current_fh,
643 create->cr_name, create->cr_namelen,
644 &create->cr_iattr, S_IFBLK, rdev, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 break;
646
647 case NF4CHR:
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400648 status = nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
650 if (MAJOR(rdev) != create->cr_specdata1 ||
651 MINOR(rdev) != create->cr_specdata2)
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400652 goto out_umask;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800653 status = nfsd_create(rqstp, &cstate->current_fh,
654 create->cr_name, create->cr_namelen,
655 &create->cr_iattr,S_IFCHR, rdev, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 break;
657
658 case NF4SOCK:
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800659 status = nfsd_create(rqstp, &cstate->current_fh,
660 create->cr_name, create->cr_namelen,
661 &create->cr_iattr, S_IFSOCK, 0, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 break;
663
664 case NF4FIFO:
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800665 status = nfsd_create(rqstp, &cstate->current_fh,
666 create->cr_name, create->cr_namelen,
667 &create->cr_iattr, S_IFIFO, 0, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 break;
669
670 case NF4DIR:
671 create->cr_iattr.ia_valid &= ~ATTR_SIZE;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800672 status = nfsd_create(rqstp, &cstate->current_fh,
673 create->cr_name, create->cr_namelen,
674 &create->cr_iattr, S_IFDIR, 0, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 break;
676
677 default:
678 status = nfserr_badtype;
679 }
680
Yu Zhiguo9208faf2009-07-06 17:24:16 +0800681 if (status)
682 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
David Quigley18032ca2013-05-02 13:19:10 -0400684 if (create->cr_label.len)
685 nfsd4_security_inode_setsecctx(&resfh, &create->cr_label, create->cr_bmval);
686
Yu Zhiguo9208faf2009-07-06 17:24:16 +0800687 if (create->cr_acl != NULL)
688 do_set_nfs4_acl(rqstp, &resfh, create->cr_acl,
689 create->cr_bmval);
690
691 fh_unlock(&cstate->current_fh);
692 set_change_info(&create->cr_cinfo, &cstate->current_fh);
693 fh_dup2(&cstate->current_fh, &resfh);
694out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 fh_put(&resfh);
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400696out_umask:
697 current->fs->umask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return status;
699}
700
J.Bruce Fields71911552006-12-13 00:35:29 -0800701static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800702nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200703 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200705 struct nfsd4_getattr *getattr = &u->getattr;
Al Virob37ad282006-10-19 23:28:59 -0700706 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200708 status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (status)
710 return status;
711
712 if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
713 return nfserr_inval;
714
J. Bruce Fields916d2d82016-10-18 14:18:40 -0400715 getattr->ga_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
716 getattr->ga_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
717 getattr->ga_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800719 getattr->ga_fhp = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return nfs_ok;
721}
722
J.Bruce Fields71911552006-12-13 00:35:29 -0800723static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800724nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200725 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200727 struct nfsd4_link *link = &u->link;
J. Bruce Fields95424462017-09-15 16:02:52 -0400728 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800730 status = nfsd_link(rqstp, &cstate->current_fh,
731 link->li_name, link->li_namelen, &cstate->save_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (!status)
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800733 set_change_info(&link->li_cinfo, &cstate->current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return status;
735}
736
J. Bruce Fields0ff7ab42010-12-16 10:06:27 -0500737static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 struct svc_fh tmp_fh;
Al Virob37ad282006-10-19 23:28:59 -0700740 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 fh_init(&tmp_fh, NFS4_FHSIZE);
J. Bruce Fieldsdf547ef2007-07-17 04:04:43 -0700743 ret = exp_pseudoroot(rqstp, &tmp_fh);
744 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return ret;
J. Bruce Fields0ff7ab42010-12-16 10:06:27 -0500746 if (tmp_fh.fh_dentry == fh->fh_dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 fh_put(&tmp_fh);
748 return nfserr_noent;
749 }
750 fh_put(&tmp_fh);
J. Bruce Fields0ff7ab42010-12-16 10:06:27 -0500751 return nfsd_lookup(rqstp, fh, "..", 2, fh);
752}
753
754static __be32
755nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200756 union nfsd4_op_u *u)
J. Bruce Fields0ff7ab42010-12-16 10:06:27 -0500757{
758 return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
J.Bruce Fields71911552006-12-13 00:35:29 -0800761static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800762nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200763 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800765 return nfsd_lookup(rqstp, &cstate->current_fh,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200766 u->lookup.lo_name, u->lookup.lo_len,
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800767 &cstate->current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
J.Bruce Fields71911552006-12-13 00:35:29 -0800770static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800771nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200772 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200774 struct nfsd4_read *read = &u->read;
Al Virob37ad282006-10-19 23:28:59 -0700775 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Jeff Layton5c4583b2019-08-18 14:18:54 -0400777 read->rd_nf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (read->rd_offset >= OFFSET_MAX)
779 return nfserr_inval;
780
Chuck Lever87c59422018-03-28 13:29:11 -0400781 trace_nfsd_read_start(rqstp, &cstate->current_fh,
782 read->rd_offset, read->rd_length);
783
J. Bruce Fields9b3234b2012-12-04 18:03:46 -0500784 /*
785 * If we do a zero copy read, then a client will see read data
786 * that reflects the state of the file *after* performing the
787 * following compound.
788 *
789 * To ensure proper ordering, we therefore turn off zero copy if
790 * the client wants us to do more in this compound:
791 */
792 if (!nfsd4_last_compound_op(rqstp))
Jeff Layton779fb0f2014-11-19 07:51:18 -0500793 clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
J. Bruce Fields9b3234b2012-12-04 18:03:46 -0500794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /* check stateid */
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +0100796 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
797 &read->rd_stateid, RD_STATE,
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400798 &read->rd_nf, NULL);
Christoph Hellwigaf90f702015-06-18 16:45:00 +0200799 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
801 goto out;
802 }
803 status = nfs_ok;
804out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 read->rd_rqstp = rqstp;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800806 read->rd_fhp = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return status;
808}
809
J. Bruce Fields34b1744c2017-05-05 17:09:37 -0400810
811static void
812nfsd4_read_release(union nfsd4_op_u *u)
813{
Jeff Layton5c4583b2019-08-18 14:18:54 -0400814 if (u->read.rd_nf)
815 nfsd_file_put(u->read.rd_nf);
Chuck Lever87c59422018-03-28 13:29:11 -0400816 trace_nfsd_read_done(u->read.rd_rqstp, u->read.rd_fhp,
817 u->read.rd_offset, u->read.rd_length);
J. Bruce Fields34b1744c2017-05-05 17:09:37 -0400818}
819
J.Bruce Fields71911552006-12-13 00:35:29 -0800820static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800821nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200822 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200824 struct nfsd4_readdir *readdir = &u->readdir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 u64 cookie = readdir->rd_cookie;
826 static const nfs4_verifier zeroverf;
827
828 /* no need to check permission - this will be done in nfsd_readdir() */
829
830 if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
831 return nfserr_inval;
832
J. Bruce Fields916d2d82016-10-18 14:18:40 -0400833 readdir->rd_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
834 readdir->rd_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
835 readdir->rd_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Bernd Schubert832023b2011-08-08 17:38:08 +0200837 if ((cookie == 1) || (cookie == 2) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
839 return nfserr_bad_cookie;
840
841 readdir->rd_rqstp = rqstp;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800842 readdir->rd_fhp = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return nfs_ok;
844}
845
J.Bruce Fields71911552006-12-13 00:35:29 -0800846static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800847nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200848 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200850 u->readlink.rl_rqstp = rqstp;
851 u->readlink.rl_fhp = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return nfs_ok;
853}
854
J.Bruce Fields71911552006-12-13 00:35:29 -0800855static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800856nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200857 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200859 struct nfsd4_remove *remove = &u->remove;
Al Virob37ad282006-10-19 23:28:59 -0700860 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -0400862 if (opens_in_grace(SVC_NET(rqstp)))
NeilBrownc815afc2005-06-23 22:03:00 -0700863 return nfserr_grace;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800864 status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
865 remove->rm_name, remove->rm_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (!status) {
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800867 fh_unlock(&cstate->current_fh);
868 set_change_info(&remove->rm_cinfo, &cstate->current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870 return status;
871}
872
J.Bruce Fields71911552006-12-13 00:35:29 -0800873static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800874nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200875 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200877 struct nfsd4_rename *rename = &u->rename;
J. Bruce Fields95424462017-09-15 16:02:52 -0400878 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
J. Bruce Fieldsf8f71d02018-11-06 11:47:20 -0500880 if (opens_in_grace(SVC_NET(rqstp)))
NeilBrownc815afc2005-06-23 22:03:00 -0700881 return nfserr_grace;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800882 status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
883 rename->rn_snamelen, &cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 rename->rn_tname, rename->rn_tnamelen);
J. Bruce Fields2a6cf942013-04-30 15:28:51 -0400885 if (status)
886 return status;
887 set_change_info(&rename->rn_sinfo, &cstate->current_fh);
888 set_change_info(&rename->rn_tinfo, &cstate->save_fh);
889 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
J.Bruce Fields71911552006-12-13 00:35:29 -0800892static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -0700893nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200894 union nfsd4_op_u *u)
Andy Adamsondcb488a32007-07-17 04:04:51 -0700895{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200896 struct nfsd4_secinfo *secinfo = &u->secinfo;
Andy Adamsondcb488a32007-07-17 04:04:51 -0700897 struct svc_export *exp;
898 struct dentry *dentry;
899 __be32 err;
900
J. Bruce Fields29a78a32011-04-09 11:28:53 -0400901 err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
902 if (err)
903 return err;
Andy Adamsondcb488a32007-07-17 04:04:51 -0700904 err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
905 secinfo->si_name, secinfo->si_namelen,
906 &exp, &dentry);
907 if (err)
908 return err;
J. Bruce Fields2f6fc052016-03-02 16:36:21 -0800909 fh_unlock(&cstate->current_fh);
David Howells2b0143b2015-03-17 22:25:59 +0000910 if (d_really_is_negative(dentry)) {
Andy Adamsondcb488a32007-07-17 04:04:51 -0700911 exp_put(exp);
912 err = nfserr_noent;
913 } else
914 secinfo->si_exp = exp;
915 dput(dentry);
J. Bruce Fields56560b92010-12-16 09:57:15 -0500916 if (cstate->minorversion)
917 /* See rfc 5661 section 2.6.3.1.1.8 */
918 fh_put(&cstate->current_fh);
Andy Adamsondcb488a32007-07-17 04:04:51 -0700919 return err;
920}
921
922static __be32
J. Bruce Fields04f4ad12010-12-16 09:51:13 -0500923nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200924 union nfsd4_op_u *u)
J. Bruce Fields04f4ad12010-12-16 09:51:13 -0500925{
926 __be32 err;
927
Christoph Hellwigeb698532017-05-08 20:58:35 +0200928 switch (u->secinfo_no_name.sin_style) {
J. Bruce Fields04f4ad12010-12-16 09:51:13 -0500929 case NFS4_SECINFO_STYLE4_CURRENT_FH:
930 break;
931 case NFS4_SECINFO_STYLE4_PARENT:
932 err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
933 if (err)
934 return err;
935 break;
936 default:
937 return nfserr_inval;
938 }
Kinglong Meebf18f162014-06-10 22:06:44 +0800939
Christoph Hellwigeb698532017-05-08 20:58:35 +0200940 u->secinfo_no_name.sin_exp = exp_get(cstate->current_fh.fh_export);
J. Bruce Fields04f4ad12010-12-16 09:51:13 -0500941 fh_put(&cstate->current_fh);
942 return nfs_ok;
943}
944
J. Bruce Fields34b1744c2017-05-05 17:09:37 -0400945static void
946nfsd4_secinfo_release(union nfsd4_op_u *u)
947{
948 if (u->secinfo.si_exp)
949 exp_put(u->secinfo.si_exp);
950}
951
Eryu Guanec572b92017-09-29 15:01:10 +0800952static void
953nfsd4_secinfo_no_name_release(union nfsd4_op_u *u)
954{
955 if (u->secinfo_no_name.sin_exp)
956 exp_put(u->secinfo_no_name.sin_exp);
957}
958
J. Bruce Fields04f4ad12010-12-16 09:51:13 -0500959static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800960nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200961 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200963 struct nfsd4_setattr *setattr = &u->setattr;
Al Virob37ad282006-10-19 23:28:59 -0700964 __be32 status = nfs_ok;
Al Viro96f6f982012-04-12 23:47:00 -0400965 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
Christoph Hellwigaf90f702015-06-18 16:45:00 +0200968 status = nfs4_preprocess_stateid_op(rqstp, cstate,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +0100969 &cstate->current_fh, &setattr->sa_stateid,
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400970 WR_STATE, NULL, NULL);
J. Bruce Fields375c5542006-01-18 17:43:33 -0800971 if (status) {
Greg Banks3e3b4802006-10-02 02:17:41 -0700972 dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
J. Bruce Fields375c5542006-01-18 17:43:33 -0800973 return status;
974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
Al Viro96f6f982012-04-12 23:47:00 -0400976 err = fh_want_write(&cstate->current_fh);
977 if (err)
978 return nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 status = nfs_ok;
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800980
981 status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
982 nfsd_attrmask);
983 if (status)
984 goto out;
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (setattr->sa_acl != NULL)
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800987 status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
988 setattr->sa_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (status)
Dave Hansen18f335a2008-02-15 14:37:38 -0800990 goto out;
David Quigley18032ca2013-05-02 13:19:10 -0400991 if (setattr->sa_label.len)
992 status = nfsd4_set_nfs4_label(rqstp, &cstate->current_fh,
993 &setattr->sa_label);
994 if (status)
995 goto out;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800996 status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
Arnd Bergmann2a1aa482019-11-03 17:50:18 +0100997 0, (time64_t)0);
Dave Hansen18f335a2008-02-15 14:37:38 -0800998out:
Al Virobad0dcf2011-11-23 12:03:18 -0500999 fh_drop_write(&cstate->current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return status;
1001}
1002
J.Bruce Fields71911552006-12-13 00:35:29 -08001003static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08001004nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001005 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001007 struct nfsd4_write *write = &u->write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 stateid_t *stateid = &write->wr_stateid;
Jeff Layton5c4583b2019-08-18 14:18:54 -04001009 struct nfsd_file *nf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07001010 __be32 status = nfs_ok;
David Shaw31dec252009-03-05 20:16:14 -05001011 unsigned long cnt;
J. Bruce Fieldsffe11372012-11-15 14:52:19 -05001012 int nvecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (write->wr_offset >= OFFSET_MAX)
1015 return nfserr_inval;
1016
Chuck Leverd890be12018-03-27 10:53:27 -04001017 cnt = write->wr_buflen;
1018 trace_nfsd_write_start(rqstp, &cstate->current_fh,
1019 write->wr_offset, cnt);
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01001020 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001021 stateid, WR_STATE, &nf, NULL);
J. Bruce Fields375c5542006-01-18 17:43:33 -08001022 if (status) {
1023 dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
1024 return status;
1025 }
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 write->wr_how_written = write->wr_stable_how;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Chuck Leverc1346a122020-11-03 11:54:23 -05001029 nvecs = svc_fill_write_vector(rqstp, write->wr_payload.pages,
1030 write->wr_payload.head, write->wr_buflen);
J. Bruce Fieldsffe11372012-11-15 14:52:19 -05001031 WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec));
1032
Trond Myklebust16f8f892020-01-06 13:40:29 -05001033 status = nfsd_vfs_write(rqstp, &cstate->current_fh, nf,
Christoph Hellwigaf90f702015-06-18 16:45:00 +02001034 write->wr_offset, rqstp->rq_vec, nvecs, &cnt,
Trond Myklebust19e06632020-01-06 13:40:37 -05001035 write->wr_how_written,
1036 (__be32 *)write->wr_verifier.data);
Jeff Layton5c4583b2019-08-18 14:18:54 -04001037 nfsd_file_put(nf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
David Shaw31dec252009-03-05 20:16:14 -05001039 write->wr_bytes_written = cnt;
Chuck Leverd890be12018-03-27 10:53:27 -04001040 trace_nfsd_write_done(rqstp, &cstate->current_fh,
1041 write->wr_offset, cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
Anna Schumaker24bab492014-09-26 13:58:27 -04001045static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001046nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Jeff Layton5c4583b2019-08-18 14:18:54 -04001047 stateid_t *src_stateid, struct nfsd_file **src,
1048 stateid_t *dst_stateid, struct nfsd_file **dst)
Christoph Hellwigffa01602015-12-03 12:59:52 +01001049{
Christoph Hellwigffa01602015-12-03 12:59:52 +01001050 __be32 status;
1051
Scott Mayhew01310bb2018-11-08 11:11:36 -05001052 if (!cstate->save_fh.fh_dentry)
1053 return nfserr_nofilehandle;
1054
Christoph Hellwigffa01602015-12-03 12:59:52 +01001055 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->save_fh,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001056 src_stateid, RD_STATE, src, NULL);
Christoph Hellwigffa01602015-12-03 12:59:52 +01001057 if (status) {
1058 dprintk("NFSD: %s: couldn't process src stateid!\n", __func__);
1059 goto out;
1060 }
1061
1062 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001063 dst_stateid, WR_STATE, dst, NULL);
Christoph Hellwigffa01602015-12-03 12:59:52 +01001064 if (status) {
1065 dprintk("NFSD: %s: couldn't process dst stateid!\n", __func__);
1066 goto out_put_src;
1067 }
1068
1069 /* fix up for NFS-specific error code */
Jeff Layton5c4583b2019-08-18 14:18:54 -04001070 if (!S_ISREG(file_inode((*src)->nf_file)->i_mode) ||
1071 !S_ISREG(file_inode((*dst)->nf_file)->i_mode)) {
Christoph Hellwigffa01602015-12-03 12:59:52 +01001072 status = nfserr_wrong_type;
1073 goto out_put_dst;
1074 }
1075
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001076out:
1077 return status;
1078out_put_dst:
Jeff Layton5c4583b2019-08-18 14:18:54 -04001079 nfsd_file_put(*dst);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001080out_put_src:
Jeff Layton5c4583b2019-08-18 14:18:54 -04001081 nfsd_file_put(*src);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001082 goto out;
1083}
1084
1085static __be32
1086nfsd4_clone(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001087 union nfsd4_op_u *u)
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001088{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001089 struct nfsd4_clone *clone = &u->clone;
Jeff Layton5c4583b2019-08-18 14:18:54 -04001090 struct nfsd_file *src, *dst;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001091 __be32 status;
1092
1093 status = nfsd4_verify_copy(rqstp, cstate, &clone->cl_src_stateid, &src,
1094 &clone->cl_dst_stateid, &dst);
1095 if (status)
1096 goto out;
1097
Trond Myklebustb66ae6d2020-01-06 13:40:32 -05001098 status = nfsd4_clone_file_range(src, clone->cl_src_pos,
1099 dst, clone->cl_dst_pos, clone->cl_count,
Trond Myklebusta25e37262019-11-27 17:05:51 -05001100 EX_ISSYNC(cstate->current_fh.fh_export));
Christoph Hellwigffa01602015-12-03 12:59:52 +01001101
Jeff Layton5c4583b2019-08-18 14:18:54 -04001102 nfsd_file_put(dst);
1103 nfsd_file_put(src);
Christoph Hellwigffa01602015-12-03 12:59:52 +01001104out:
1105 return status;
1106}
1107
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001108void nfs4_put_copy(struct nfsd4_copy *copy)
1109{
1110 if (!refcount_dec_and_test(&copy->refcount))
1111 return;
1112 kfree(copy);
1113}
1114
1115static bool
1116check_and_set_stop_copy(struct nfsd4_copy *copy)
1117{
1118 bool value;
1119
1120 spin_lock(&copy->cp_clp->async_lock);
1121 value = copy->stopped;
1122 if (!copy->stopped)
1123 copy->stopped = true;
1124 spin_unlock(&copy->cp_clp->async_lock);
1125 return value;
1126}
1127
1128static void nfsd4_stop_copy(struct nfsd4_copy *copy)
1129{
1130 /* only 1 thread should stop the copy */
1131 if (!check_and_set_stop_copy(copy))
1132 kthread_stop(copy->copy_task);
1133 nfs4_put_copy(copy);
1134}
1135
1136static struct nfsd4_copy *nfsd4_get_copy(struct nfs4_client *clp)
1137{
1138 struct nfsd4_copy *copy = NULL;
1139
1140 spin_lock(&clp->async_lock);
1141 if (!list_empty(&clp->async_copies)) {
1142 copy = list_first_entry(&clp->async_copies, struct nfsd4_copy,
1143 copies);
1144 refcount_inc(&copy->refcount);
1145 }
1146 spin_unlock(&clp->async_lock);
1147 return copy;
1148}
1149
1150void nfsd4_shutdown_copy(struct nfs4_client *clp)
1151{
1152 struct nfsd4_copy *copy;
1153
1154 while ((copy = nfsd4_get_copy(clp)) != NULL)
1155 nfsd4_stop_copy(copy);
1156}
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001157#ifdef CONFIG_NFSD_V4_2_INTER_SSC
1158
1159extern struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
1160 struct nfs_fh *src_fh,
1161 nfs4_stateid *stateid);
1162extern void nfs42_ssc_close(struct file *filep);
1163
1164extern void nfs_sb_deactive(struct super_block *sb);
1165
1166#define NFSD42_INTERSSC_MOUNTOPS "vers=4.2,addr=%s,sec=sys"
1167
Chuck Leverf2453972020-04-06 16:01:42 -04001168/*
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001169 * Support one copy source server for now.
1170 */
1171static __be32
1172nfsd4_interssc_connect(struct nl4_server *nss, struct svc_rqst *rqstp,
1173 struct vfsmount **mount)
1174{
1175 struct file_system_type *type;
1176 struct vfsmount *ss_mnt;
1177 struct nfs42_netaddr *naddr;
1178 struct sockaddr_storage tmp_addr;
1179 size_t tmp_addrlen, match_netid_len = 3;
1180 char *startsep = "", *endsep = "", *match_netid = "tcp";
1181 char *ipaddr, *dev_name, *raw_data;
Olga Kornievskaiab8290ca2019-12-04 15:13:53 -05001182 int len, raw_len;
1183 __be32 status = nfserr_inval;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001184
1185 naddr = &nss->u.nl4_addr;
1186 tmp_addrlen = rpc_uaddr2sockaddr(SVC_NET(rqstp), naddr->addr,
1187 naddr->addr_len,
1188 (struct sockaddr *)&tmp_addr,
1189 sizeof(tmp_addr));
1190 if (tmp_addrlen == 0)
1191 goto out_err;
1192
1193 if (tmp_addr.ss_family == AF_INET6) {
1194 startsep = "[";
1195 endsep = "]";
1196 match_netid = "tcp6";
1197 match_netid_len = 4;
1198 }
1199
1200 if (naddr->netid_len != match_netid_len ||
1201 strncmp(naddr->netid, match_netid, naddr->netid_len))
1202 goto out_err;
1203
1204 /* Construct the raw data for the vfs_kern_mount call */
1205 len = RPC_MAX_ADDRBUFLEN + 1;
1206 ipaddr = kzalloc(len, GFP_KERNEL);
1207 if (!ipaddr)
1208 goto out_err;
1209
1210 rpc_ntop((struct sockaddr *)&tmp_addr, ipaddr, len);
1211
1212 /* 2 for ipv6 endsep and startsep. 3 for ":/" and trailing '/0'*/
1213
1214 raw_len = strlen(NFSD42_INTERSSC_MOUNTOPS) + strlen(ipaddr);
1215 raw_data = kzalloc(raw_len, GFP_KERNEL);
1216 if (!raw_data)
1217 goto out_free_ipaddr;
1218
1219 snprintf(raw_data, raw_len, NFSD42_INTERSSC_MOUNTOPS, ipaddr);
1220
Olga Kornievskaiab8290ca2019-12-04 15:13:53 -05001221 status = nfserr_nodev;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001222 type = get_fs_type("nfs");
1223 if (!type)
1224 goto out_free_rawdata;
1225
1226 /* Set the server:<export> for the vfs_kern_mount call */
1227 dev_name = kzalloc(len + 5, GFP_KERNEL);
1228 if (!dev_name)
1229 goto out_free_rawdata;
1230 snprintf(dev_name, len + 5, "%s%s%s:/", startsep, ipaddr, endsep);
1231
1232 /* Use an 'internal' mount: SB_KERNMOUNT -> MNT_INTERNAL */
1233 ss_mnt = vfs_kern_mount(type, SB_KERNMOUNT, dev_name, raw_data);
1234 module_put(type->owner);
1235 if (IS_ERR(ss_mnt))
1236 goto out_free_devname;
1237
1238 status = 0;
1239 *mount = ss_mnt;
1240
1241out_free_devname:
1242 kfree(dev_name);
1243out_free_rawdata:
1244 kfree(raw_data);
1245out_free_ipaddr:
1246 kfree(ipaddr);
1247out_err:
1248 return status;
1249}
1250
1251static void
1252nfsd4_interssc_disconnect(struct vfsmount *ss_mnt)
1253{
Dai Ngo0cfcd402020-10-18 23:42:49 -04001254 nfs_do_sb_deactive(ss_mnt->mnt_sb);
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001255 mntput(ss_mnt);
1256}
1257
Chuck Leverf2453972020-04-06 16:01:42 -04001258/*
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001259 * Verify COPY destination stateid.
Chuck Leverf2453972020-04-06 16:01:42 -04001260 *
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001261 * Connect to the source server with NFSv4.1.
1262 * Create the source struct file for nfsd_copy_range.
1263 * Called with COPY cstate:
1264 * SAVED_FH: source filehandle
1265 * CURRENT_FH: destination filehandle
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001266 */
1267static __be32
1268nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
1269 struct nfsd4_compound_state *cstate,
1270 struct nfsd4_copy *copy, struct vfsmount **mount)
1271{
1272 struct svc_fh *s_fh = NULL;
1273 stateid_t *s_stid = &copy->cp_src_stateid;
Olga Kornievskaiab8290ca2019-12-04 15:13:53 -05001274 __be32 status = nfserr_inval;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001275
1276 /* Verify the destination stateid and set dst struct file*/
1277 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1278 &copy->cp_dst_stateid,
1279 WR_STATE, &copy->nf_dst, NULL);
1280 if (status)
1281 goto out;
1282
1283 status = nfsd4_interssc_connect(&copy->cp_src, rqstp, mount);
1284 if (status)
1285 goto out;
1286
1287 s_fh = &cstate->save_fh;
1288
1289 copy->c_fh.size = s_fh->fh_handle.fh_size;
1290 memcpy(copy->c_fh.data, &s_fh->fh_handle.fh_base, copy->c_fh.size);
Olga Kornievskaia3f9544c2019-12-04 15:13:53 -05001291 copy->stateid.seqid = cpu_to_be32(s_stid->si_generation);
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001292 memcpy(copy->stateid.other, (void *)&s_stid->si_opaque,
1293 sizeof(stateid_opaque_t));
1294
1295 status = 0;
1296out:
1297 return status;
1298}
1299
1300static void
1301nfsd4_cleanup_inter_ssc(struct vfsmount *ss_mnt, struct nfsd_file *src,
1302 struct nfsd_file *dst)
1303{
1304 nfs42_ssc_close(src->nf_file);
Dai Ngo36e1e5b2020-10-29 15:07:15 -04001305 /* 'src' is freed by nfsd4_do_async_copy */
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001306 nfsd_file_put(dst);
1307 mntput(ss_mnt);
1308}
1309
1310#else /* CONFIG_NFSD_V4_2_INTER_SSC */
1311
1312static __be32
1313nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
1314 struct nfsd4_compound_state *cstate,
1315 struct nfsd4_copy *copy,
1316 struct vfsmount **mount)
1317{
1318 *mount = NULL;
Olga Kornievskaiab8290ca2019-12-04 15:13:53 -05001319 return nfserr_inval;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001320}
1321
1322static void
1323nfsd4_cleanup_inter_ssc(struct vfsmount *ss_mnt, struct nfsd_file *src,
1324 struct nfsd_file *dst)
1325{
1326}
1327
1328static void
1329nfsd4_interssc_disconnect(struct vfsmount *ss_mnt)
1330{
1331}
1332
1333static struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
1334 struct nfs_fh *src_fh,
1335 nfs4_stateid *stateid)
1336{
1337 return NULL;
1338}
1339#endif /* CONFIG_NFSD_V4_2_INTER_SSC */
1340
1341static __be32
1342nfsd4_setup_intra_ssc(struct svc_rqst *rqstp,
1343 struct nfsd4_compound_state *cstate,
1344 struct nfsd4_copy *copy)
1345{
1346 return nfsd4_verify_copy(rqstp, cstate, &copy->cp_src_stateid,
1347 &copy->nf_src, &copy->cp_dst_stateid,
1348 &copy->nf_dst);
1349}
1350
1351static void
1352nfsd4_cleanup_intra_ssc(struct nfsd_file *src, struct nfsd_file *dst)
1353{
1354 nfsd_file_put(src);
1355 nfsd_file_put(dst);
1356}
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001357
1358static void nfsd4_cb_offload_release(struct nfsd4_callback *cb)
1359{
1360 struct nfsd4_copy *copy = container_of(cb, struct nfsd4_copy, cp_cb);
1361
1362 nfs4_put_copy(copy);
1363}
1364
1365static int nfsd4_cb_offload_done(struct nfsd4_callback *cb,
1366 struct rpc_task *task)
1367{
1368 return 1;
1369}
1370
1371static const struct nfsd4_callback_ops nfsd4_cb_offload_ops = {
1372 .release = nfsd4_cb_offload_release,
1373 .done = nfsd4_cb_offload_done
1374};
1375
1376static void nfsd4_init_copy_res(struct nfsd4_copy *copy, bool sync)
1377{
1378 copy->cp_res.wr_stable_how = NFS_UNSTABLE;
1379 copy->cp_synchronous = sync;
1380 gen_boot_verifier(&copy->cp_res.wr_verifier, copy->cp_clp->net);
1381}
1382
1383static ssize_t _nfsd_copy_file_range(struct nfsd4_copy *copy)
1384{
1385 ssize_t bytes_copied = 0;
1386 size_t bytes_total = copy->cp_count;
1387 u64 src_pos = copy->cp_src_pos;
1388 u64 dst_pos = copy->cp_dst_pos;
1389
1390 do {
1391 if (kthread_should_stop())
1392 break;
Jeff Layton5c4583b2019-08-18 14:18:54 -04001393 bytes_copied = nfsd_copy_file_range(copy->nf_src->nf_file,
1394 src_pos, copy->nf_dst->nf_file, dst_pos,
1395 bytes_total);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001396 if (bytes_copied <= 0)
1397 break;
1398 bytes_total -= bytes_copied;
1399 copy->cp_res.wr_bytes_written += bytes_copied;
1400 src_pos += bytes_copied;
1401 dst_pos += bytes_copied;
1402 } while (bytes_total > 0 && !copy->cp_synchronous);
1403 return bytes_copied;
1404}
1405
1406static __be32 nfsd4_do_copy(struct nfsd4_copy *copy, bool sync)
1407{
1408 __be32 status;
1409 ssize_t bytes;
1410
1411 bytes = _nfsd_copy_file_range(copy);
1412 /* for async copy, we ignore the error, client can always retry
1413 * to get the error
1414 */
1415 if (bytes < 0 && !copy->cp_res.wr_bytes_written)
1416 status = nfserrno(bytes);
1417 else {
1418 nfsd4_init_copy_res(copy, sync);
1419 status = nfs_ok;
1420 }
1421
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001422 if (!copy->cp_intra) /* Inter server SSC */
1423 nfsd4_cleanup_inter_ssc(copy->ss_mnt, copy->nf_src,
1424 copy->nf_dst);
1425 else
1426 nfsd4_cleanup_intra_ssc(copy->nf_src, copy->nf_dst);
1427
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001428 return status;
1429}
1430
Chuck Levereb162e12020-11-30 16:17:40 -05001431static void dup_copy_fields(struct nfsd4_copy *src, struct nfsd4_copy *dst)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001432{
1433 dst->cp_src_pos = src->cp_src_pos;
1434 dst->cp_dst_pos = src->cp_dst_pos;
1435 dst->cp_count = src->cp_count;
1436 dst->cp_synchronous = src->cp_synchronous;
1437 memcpy(&dst->cp_res, &src->cp_res, sizeof(src->cp_res));
1438 memcpy(&dst->fh, &src->fh, sizeof(src->fh));
1439 dst->cp_clp = src->cp_clp;
Jeff Layton5c4583b2019-08-18 14:18:54 -04001440 dst->nf_dst = nfsd_file_get(src->nf_dst);
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001441 dst->cp_intra = src->cp_intra;
1442 if (src->cp_intra) /* for inter, file_src doesn't exist yet */
1443 dst->nf_src = nfsd_file_get(src->nf_src);
1444
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001445 memcpy(&dst->cp_stateid, &src->cp_stateid, sizeof(src->cp_stateid));
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001446 memcpy(&dst->cp_src, &src->cp_src, sizeof(struct nl4_server));
1447 memcpy(&dst->stateid, &src->stateid, sizeof(src->stateid));
1448 memcpy(&dst->c_fh, &src->c_fh, sizeof(src->c_fh));
1449 dst->ss_mnt = src->ss_mnt;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001450}
1451
1452static void cleanup_async_copy(struct nfsd4_copy *copy)
1453{
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001454 nfs4_free_copy_state(copy);
Jeff Layton5c4583b2019-08-18 14:18:54 -04001455 nfsd_file_put(copy->nf_dst);
Olga Kornievskaia2e577f02019-12-04 15:13:54 -05001456 if (copy->cp_intra)
1457 nfsd_file_put(copy->nf_src);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001458 spin_lock(&copy->cp_clp->async_lock);
1459 list_del(&copy->copies);
1460 spin_unlock(&copy->cp_clp->async_lock);
1461 nfs4_put_copy(copy);
1462}
1463
1464static int nfsd4_do_async_copy(void *data)
1465{
1466 struct nfsd4_copy *copy = (struct nfsd4_copy *)data;
1467 struct nfsd4_copy *cb_copy;
1468
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001469 if (!copy->cp_intra) { /* Inter server SSC */
1470 copy->nf_src = kzalloc(sizeof(struct nfsd_file), GFP_KERNEL);
1471 if (!copy->nf_src) {
1472 copy->nfserr = nfserr_serverfault;
1473 nfsd4_interssc_disconnect(copy->ss_mnt);
1474 goto do_callback;
1475 }
1476 copy->nf_src->nf_file = nfs42_ssc_open(copy->ss_mnt, &copy->c_fh,
1477 &copy->stateid);
1478 if (IS_ERR(copy->nf_src->nf_file)) {
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001479 copy->nfserr = nfserr_offload_denied;
1480 nfsd4_interssc_disconnect(copy->ss_mnt);
1481 goto do_callback;
1482 }
1483 }
1484
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001485 copy->nfserr = nfsd4_do_copy(copy, 0);
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001486do_callback:
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001487 cb_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
1488 if (!cb_copy)
1489 goto out;
Dai Ngo49a36132020-10-29 15:07:16 -04001490 refcount_set(&cb_copy->refcount, 1);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001491 memcpy(&cb_copy->cp_res, &copy->cp_res, sizeof(copy->cp_res));
1492 cb_copy->cp_clp = copy->cp_clp;
1493 cb_copy->nfserr = copy->nfserr;
1494 memcpy(&cb_copy->fh, &copy->fh, sizeof(copy->fh));
1495 nfsd4_init_cb(&cb_copy->cp_cb, cb_copy->cp_clp,
1496 &nfsd4_cb_offload_ops, NFSPROC4_CLNT_CB_OFFLOAD);
1497 nfsd4_run_cb(&cb_copy->cp_cb);
1498out:
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001499 if (!copy->cp_intra)
1500 kfree(copy->nf_src);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001501 cleanup_async_copy(copy);
1502 return 0;
1503}
1504
Christoph Hellwigffa01602015-12-03 12:59:52 +01001505static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001506nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001507 union nfsd4_op_u *u)
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001508{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001509 struct nfsd4_copy *copy = &u->copy;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001510 __be32 status;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001511 struct nfsd4_copy *async_copy = NULL;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001512
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001513 if (!copy->cp_intra) { /* Inter server SSC */
1514 if (!inter_copy_offload_enable || copy->cp_synchronous) {
1515 status = nfserr_notsupp;
1516 goto out;
1517 }
1518 status = nfsd4_setup_inter_ssc(rqstp, cstate, copy,
1519 &copy->ss_mnt);
1520 if (status)
1521 return nfserr_offload_denied;
1522 } else {
1523 status = nfsd4_setup_intra_ssc(rqstp, cstate, copy);
1524 if (status)
1525 return status;
1526 }
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001527
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001528 copy->cp_clp = cstate->clp;
1529 memcpy(&copy->fh, &cstate->current_fh.fh_handle,
1530 sizeof(struct knfsd_fh));
1531 if (!copy->cp_synchronous) {
1532 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001533
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001534 status = nfserrno(-ENOMEM);
1535 async_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
1536 if (!async_copy)
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001537 goto out_err;
1538 if (!nfs4_init_copy_state(nn, copy))
1539 goto out_err;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001540 refcount_set(&async_copy->refcount, 1);
1541 memcpy(&copy->cp_res.cb_stateid, &copy->cp_stateid,
1542 sizeof(copy->cp_stateid));
Chuck Levereb162e12020-11-30 16:17:40 -05001543 dup_copy_fields(copy, async_copy);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001544 async_copy->copy_task = kthread_create(nfsd4_do_async_copy,
1545 async_copy, "%s", "copy thread");
1546 if (IS_ERR(async_copy->copy_task))
1547 goto out_err;
1548 spin_lock(&async_copy->cp_clp->async_lock);
1549 list_add(&async_copy->copies,
1550 &async_copy->cp_clp->async_copies);
1551 spin_unlock(&async_copy->cp_clp->async_lock);
1552 wake_up_process(async_copy->copy_task);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001553 status = nfs_ok;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001554 } else {
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001555 status = nfsd4_do_copy(copy, 1);
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001556 }
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001557out:
1558 return status;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001559out_err:
Olga Kornievskaia18f428d2019-12-04 15:13:54 -05001560 if (async_copy)
1561 cleanup_async_copy(async_copy);
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001562 status = nfserrno(-ENOMEM);
1563 if (!copy->cp_intra)
1564 nfsd4_interssc_disconnect(copy->ss_mnt);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001565 goto out;
1566}
1567
1568struct nfsd4_copy *
1569find_async_copy(struct nfs4_client *clp, stateid_t *stateid)
1570{
1571 struct nfsd4_copy *copy;
1572
1573 spin_lock(&clp->async_lock);
1574 list_for_each_entry(copy, &clp->async_copies, copies) {
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001575 if (memcmp(&copy->cp_stateid.stid, stateid, NFS4_STATEID_SIZE))
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001576 continue;
1577 refcount_inc(&copy->refcount);
1578 spin_unlock(&clp->async_lock);
1579 return copy;
1580 }
1581 spin_unlock(&clp->async_lock);
1582 return NULL;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001583}
1584
1585static __be32
Olga Kornievskaia885e2bf2018-07-20 18:19:19 -04001586nfsd4_offload_cancel(struct svc_rqst *rqstp,
1587 struct nfsd4_compound_state *cstate,
1588 union nfsd4_op_u *u)
1589{
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001590 struct nfsd4_offload_status *os = &u->offload_status;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001591 struct nfsd4_copy *copy;
1592 struct nfs4_client *clp = cstate->clp;
1593
1594 copy = find_async_copy(clp, &os->stateid);
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001595 if (!copy) {
1596 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001597
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04001598 return manage_cpntf_state(nn, &os->stateid, clp, NULL);
1599 } else
1600 nfsd4_stop_copy(copy);
1601
1602 return nfs_ok;
Olga Kornievskaia885e2bf2018-07-20 18:19:19 -04001603}
1604
1605static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04001606nfsd4_copy_notify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1607 union nfsd4_op_u *u)
1608{
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001609 struct nfsd4_copy_notify *cn = &u->copy_notify;
1610 __be32 status;
1611 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1612 struct nfs4_stid *stid;
1613 struct nfs4_cpntf_state *cps;
1614 struct nfs4_client *clp = cstate->clp;
1615
1616 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1617 &cn->cpn_src_stateid, RD_STATE, NULL,
1618 &stid);
1619 if (status)
1620 return status;
1621
1622 cn->cpn_sec = nn->nfsd4_lease;
1623 cn->cpn_nsec = 0;
1624
1625 status = nfserrno(-ENOMEM);
1626 cps = nfs4_alloc_init_cpntf_state(nn, stid);
1627 if (!cps)
1628 goto out;
1629 memcpy(&cn->cpn_cnr_stateid, &cps->cp_stateid.stid, sizeof(stateid_t));
1630 memcpy(&cps->cp_p_stateid, &stid->sc_stateid, sizeof(stateid_t));
1631 memcpy(&cps->cp_p_clid, &clp->cl_clientid, sizeof(clientid_t));
1632
1633 /* For now, only return one server address in cpn_src, the
1634 * address used by the client to connect to this server.
1635 */
1636 cn->cpn_src.nl4_type = NL4_NETADDR;
1637 status = nfsd4_set_netaddr((struct sockaddr *)&rqstp->rq_daddr,
1638 &cn->cpn_src.u.nl4_addr);
1639 WARN_ON_ONCE(status);
1640 if (status) {
1641 nfs4_put_cpntf_state(nn, cps);
1642 goto out;
1643 }
1644out:
1645 nfs4_put_stid(stid);
1646 return status;
Olga Kornievskaia51911862019-08-08 11:14:59 -04001647}
1648
1649static __be32
Anna Schumaker95d871f2014-11-07 14:44:26 -05001650nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1651 struct nfsd4_fallocate *fallocate, int flags)
1652{
J. Bruce Fields0d4d6722018-11-06 17:44:03 -05001653 __be32 status;
Jeff Layton5c4583b2019-08-18 14:18:54 -04001654 struct nfsd_file *nf;
Anna Schumaker95d871f2014-11-07 14:44:26 -05001655
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01001656 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
Anna Schumaker95d871f2014-11-07 14:44:26 -05001657 &fallocate->falloc_stateid,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001658 WR_STATE, &nf, NULL);
Anna Schumaker95d871f2014-11-07 14:44:26 -05001659 if (status != nfs_ok) {
1660 dprintk("NFSD: nfsd4_fallocate: couldn't process stateid!\n");
1661 return status;
1662 }
1663
Jeff Layton5c4583b2019-08-18 14:18:54 -04001664 status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, nf->nf_file,
Anna Schumaker95d871f2014-11-07 14:44:26 -05001665 fallocate->falloc_offset,
1666 fallocate->falloc_length,
1667 flags);
Jeff Layton5c4583b2019-08-18 14:18:54 -04001668 nfsd_file_put(nf);
Anna Schumaker95d871f2014-11-07 14:44:26 -05001669 return status;
1670}
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04001671static __be32
1672nfsd4_offload_status(struct svc_rqst *rqstp,
1673 struct nfsd4_compound_state *cstate,
1674 union nfsd4_op_u *u)
1675{
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001676 struct nfsd4_offload_status *os = &u->offload_status;
1677 __be32 status = 0;
1678 struct nfsd4_copy *copy;
1679 struct nfs4_client *clp = cstate->clp;
1680
1681 copy = find_async_copy(clp, &os->stateid);
1682 if (copy) {
1683 os->count = copy->cp_res.wr_bytes_written;
1684 nfs4_put_copy(copy);
1685 } else
1686 status = nfserr_bad_stateid;
1687
1688 return status;
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04001689}
Anna Schumaker95d871f2014-11-07 14:44:26 -05001690
1691static __be32
1692nfsd4_allocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001693 union nfsd4_op_u *u)
Anna Schumaker95d871f2014-11-07 14:44:26 -05001694{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001695 return nfsd4_fallocate(rqstp, cstate, &u->allocate, 0);
Anna Schumaker95d871f2014-11-07 14:44:26 -05001696}
1697
1698static __be32
Anna Schumakerb0cb9082014-11-07 14:44:27 -05001699nfsd4_deallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001700 union nfsd4_op_u *u)
Anna Schumakerb0cb9082014-11-07 14:44:27 -05001701{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001702 return nfsd4_fallocate(rqstp, cstate, &u->deallocate,
Anna Schumakerb0cb9082014-11-07 14:44:27 -05001703 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
1704}
1705
1706static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04001707nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001708 union nfsd4_op_u *u)
Anna Schumaker24bab492014-09-26 13:58:27 -04001709{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001710 struct nfsd4_seek *seek = &u->seek;
Anna Schumaker24bab492014-09-26 13:58:27 -04001711 int whence;
1712 __be32 status;
Jeff Layton5c4583b2019-08-18 14:18:54 -04001713 struct nfsd_file *nf;
Anna Schumaker24bab492014-09-26 13:58:27 -04001714
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01001715 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
Anna Schumaker24bab492014-09-26 13:58:27 -04001716 &seek->seek_stateid,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001717 RD_STATE, &nf, NULL);
Anna Schumaker24bab492014-09-26 13:58:27 -04001718 if (status) {
1719 dprintk("NFSD: nfsd4_seek: couldn't process stateid!\n");
1720 return status;
1721 }
1722
1723 switch (seek->seek_whence) {
1724 case NFS4_CONTENT_DATA:
1725 whence = SEEK_DATA;
1726 break;
1727 case NFS4_CONTENT_HOLE:
1728 whence = SEEK_HOLE;
1729 break;
1730 default:
1731 status = nfserr_union_notsupp;
1732 goto out;
1733 }
1734
1735 /*
1736 * Note: This call does change file->f_pos, but nothing in NFSD
1737 * should ever file->f_pos.
1738 */
Jeff Layton5c4583b2019-08-18 14:18:54 -04001739 seek->seek_pos = vfs_llseek(nf->nf_file, seek->seek_offset, whence);
Anna Schumaker24bab492014-09-26 13:58:27 -04001740 if (seek->seek_pos < 0)
1741 status = nfserrno(seek->seek_pos);
Jeff Layton5c4583b2019-08-18 14:18:54 -04001742 else if (seek->seek_pos >= i_size_read(file_inode(nf->nf_file)))
Anna Schumaker24bab492014-09-26 13:58:27 -04001743 seek->seek_eof = true;
1744
1745out:
Jeff Layton5c4583b2019-08-18 14:18:54 -04001746 nfsd_file_put(nf);
Anna Schumaker24bab492014-09-26 13:58:27 -04001747 return status;
1748}
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750/* This routine never returns NFS_OK! If there are no other errors, it
1751 * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
1752 * attributes matched. VERIFY is implemented by mapping NFSERR_SAME
1753 * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
1754 */
Al Virob37ad282006-10-19 23:28:59 -07001755static __be32
J.Bruce Fieldsc954e2a2006-12-13 00:35:31 -08001756_nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08001757 struct nfsd4_verify *verify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Al Viro2ebbc012006-10-19 23:28:58 -07001759 __be32 *buf, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 int count;
Al Virob37ad282006-10-19 23:28:59 -07001761 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001763 status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (status)
1765 return status;
1766
Yu Zhiguo3c8e0312009-05-16 16:22:31 +08001767 status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
1768 if (status)
1769 return status;
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
1772 || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
1773 return nfserr_inval;
1774 if (verify->ve_attrlen & 3)
1775 return nfserr_inval;
1776
1777 /* count in words:
1778 * bitmap_len(1) + bitmap(2) + attr_len(1) = 4
1779 */
1780 count = 4 + (verify->ve_attrlen >> 2);
1781 buf = kmalloc(count << 2, GFP_KERNEL);
1782 if (!buf)
J. Bruce Fields3e772462011-08-10 19:07:33 -04001783 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
J. Bruce Fields84822d02012-12-14 17:57:50 -05001785 p = buf;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04001786 status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08001787 cstate->current_fh.fh_export,
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04001788 cstate->current_fh.fh_dentry,
1789 verify->ve_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08001790 rqstp, 0);
J. Bruce Fields41ae6e712013-08-21 15:32:50 -04001791 /*
1792 * If nfsd4_encode_fattr() ran out of space, assume that's because
1793 * the attributes are longer (hence different) than those given:
1794 */
J. Bruce Fields84822d02012-12-14 17:57:50 -05001795 if (status == nfserr_resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 status = nfserr_not_same;
1797 if (status)
1798 goto out_kfree;
1799
Benny Halevy95ec28c2009-04-03 08:29:08 +03001800 /* skip bitmap */
1801 p = buf + 1 + ntohl(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 status = nfserr_not_same;
1803 if (ntohl(*p++) != verify->ve_attrlen)
1804 goto out_kfree;
1805 if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
1806 status = nfserr_same;
1807
1808out_kfree:
1809 kfree(buf);
1810 return status;
1811}
1812
J.Bruce Fieldsc954e2a2006-12-13 00:35:31 -08001813static __be32
1814nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001815 union nfsd4_op_u *u)
J.Bruce Fieldsc954e2a2006-12-13 00:35:31 -08001816{
1817 __be32 status;
1818
Christoph Hellwigeb698532017-05-08 20:58:35 +02001819 status = _nfsd4_verify(rqstp, cstate, &u->verify);
J.Bruce Fieldsc954e2a2006-12-13 00:35:31 -08001820 return status == nfserr_not_same ? nfs_ok : status;
1821}
1822
1823static __be32
1824nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001825 union nfsd4_op_u *u)
J.Bruce Fieldsc954e2a2006-12-13 00:35:31 -08001826{
1827 __be32 status;
1828
Christoph Hellwigeb698532017-05-08 20:58:35 +02001829 status = _nfsd4_verify(rqstp, cstate, &u->nverify);
J.Bruce Fieldsc954e2a2006-12-13 00:35:31 -08001830 return status == nfserr_same ? nfs_ok : status;
1831}
1832
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001833#ifdef CONFIG_NFSD_PNFS
1834static const struct nfsd4_layout_ops *
1835nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type)
1836{
Jeff Layton8a4c3922016-07-10 15:55:58 -04001837 if (!exp->ex_layout_types) {
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001838 dprintk("%s: export does not support pNFS\n", __func__);
1839 return NULL;
1840 }
1841
Ari Kauppib550a322017-05-05 16:07:55 -04001842 if (layout_type >= LAYOUT_TYPE_MAX ||
1843 !(exp->ex_layout_types & (1 << layout_type))) {
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001844 dprintk("%s: layout type %d not supported\n",
1845 __func__, layout_type);
1846 return NULL;
1847 }
1848
1849 return nfsd4_layout_ops[layout_type];
1850}
1851
1852static __be32
1853nfsd4_getdeviceinfo(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001854 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001855{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001856 struct nfsd4_getdeviceinfo *gdp = &u->getdeviceinfo;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001857 const struct nfsd4_layout_ops *ops;
1858 struct nfsd4_deviceid_map *map;
1859 struct svc_export *exp;
1860 __be32 nfserr;
1861
1862 dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n",
1863 __func__,
1864 gdp->gd_layout_type,
1865 gdp->gd_devid.fsid_idx, gdp->gd_devid.generation,
1866 gdp->gd_maxcount);
1867
1868 map = nfsd4_find_devid_map(gdp->gd_devid.fsid_idx);
1869 if (!map) {
1870 dprintk("%s: couldn't find device ID to export mapping!\n",
1871 __func__);
1872 return nfserr_noent;
1873 }
1874
1875 exp = rqst_exp_find(rqstp, map->fsid_type, map->fsid);
1876 if (IS_ERR(exp)) {
1877 dprintk("%s: could not find device id\n", __func__);
1878 return nfserr_noent;
1879 }
1880
1881 nfserr = nfserr_layoutunavailable;
1882 ops = nfsd4_layout_verify(exp, gdp->gd_layout_type);
1883 if (!ops)
1884 goto out;
1885
1886 nfserr = nfs_ok;
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +01001887 if (gdp->gd_maxcount != 0) {
1888 nfserr = ops->proc_getdeviceinfo(exp->ex_path.mnt->mnt_sb,
J. Bruce Fieldsec596592021-01-21 17:57:45 -05001889 rqstp, cstate->clp, gdp);
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +01001890 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001891
1892 gdp->gd_notify_types &= ops->notify_types;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001893out:
Kinglong Meea1420382015-03-15 23:12:15 +08001894 exp_put(exp);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001895 return nfserr;
1896}
1897
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04001898static void
1899nfsd4_getdeviceinfo_release(union nfsd4_op_u *u)
1900{
1901 kfree(u->getdeviceinfo.gd_device);
1902}
1903
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001904static __be32
1905nfsd4_layoutget(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001906 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001907{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001908 struct nfsd4_layoutget *lgp = &u->layoutget;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001909 struct svc_fh *current_fh = &cstate->current_fh;
1910 const struct nfsd4_layout_ops *ops;
1911 struct nfs4_layout_stateid *ls;
1912 __be32 nfserr;
Benjamin Coddington66282ec2017-12-19 09:35:25 -05001913 int accmode = NFSD_MAY_READ_IF_EXEC;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001914
1915 switch (lgp->lg_seg.iomode) {
1916 case IOMODE_READ:
Benjamin Coddington66282ec2017-12-19 09:35:25 -05001917 accmode |= NFSD_MAY_READ;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001918 break;
1919 case IOMODE_RW:
Benjamin Coddington66282ec2017-12-19 09:35:25 -05001920 accmode |= NFSD_MAY_READ | NFSD_MAY_WRITE;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001921 break;
1922 default:
1923 dprintk("%s: invalid iomode %d\n",
1924 __func__, lgp->lg_seg.iomode);
1925 nfserr = nfserr_badiomode;
1926 goto out;
1927 }
1928
1929 nfserr = fh_verify(rqstp, current_fh, 0, accmode);
1930 if (nfserr)
1931 goto out;
1932
1933 nfserr = nfserr_layoutunavailable;
1934 ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);
1935 if (!ops)
1936 goto out;
1937
1938 /*
1939 * Verify minlength and range as per RFC5661:
1940 * o If loga_length is less than loga_minlength,
1941 * the metadata server MUST return NFS4ERR_INVAL.
1942 * o If the sum of loga_offset and loga_minlength exceeds
1943 * NFS4_UINT64_MAX, and loga_minlength is not
1944 * NFS4_UINT64_MAX, the error NFS4ERR_INVAL MUST result.
1945 * o If the sum of loga_offset and loga_length exceeds
1946 * NFS4_UINT64_MAX, and loga_length is not NFS4_UINT64_MAX,
1947 * the error NFS4ERR_INVAL MUST result.
1948 */
1949 nfserr = nfserr_inval;
1950 if (lgp->lg_seg.length < lgp->lg_minlength ||
1951 (lgp->lg_minlength != NFS4_MAX_UINT64 &&
1952 lgp->lg_minlength > NFS4_MAX_UINT64 - lgp->lg_seg.offset) ||
1953 (lgp->lg_seg.length != NFS4_MAX_UINT64 &&
1954 lgp->lg_seg.length > NFS4_MAX_UINT64 - lgp->lg_seg.offset))
1955 goto out;
1956 if (lgp->lg_seg.length == 0)
1957 goto out;
1958
1959 nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lgp->lg_sid,
1960 true, lgp->lg_layout_type, &ls);
Christoph Hellwig31ef83d2014-08-16 19:02:22 -05001961 if (nfserr) {
Chuck Leverf394b622018-03-27 10:53:11 -04001962 trace_nfsd_layout_get_lookup_fail(&lgp->lg_sid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001963 goto out;
Christoph Hellwig31ef83d2014-08-16 19:02:22 -05001964 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001965
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02001966 nfserr = nfserr_recallconflict;
1967 if (atomic_read(&ls->ls_stid.sc_file->fi_lo_recalls))
1968 goto out_put_stid;
1969
David Howells2b0143b2015-03-17 22:25:59 +00001970 nfserr = ops->proc_layoutget(d_inode(current_fh->fh_dentry),
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001971 current_fh, lgp);
1972 if (nfserr)
1973 goto out_put_stid;
1974
1975 nfserr = nfsd4_insert_layout(lgp, ls);
1976
1977out_put_stid:
Jeff Laytoncc8a5532015-09-17 07:58:24 -04001978 mutex_unlock(&ls->ls_mutex);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001979 nfs4_put_stid(&ls->ls_stid);
1980out:
1981 return nfserr;
1982}
1983
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04001984static void
1985nfsd4_layoutget_release(union nfsd4_op_u *u)
1986{
1987 kfree(u->layoutget.lg_content);
1988}
1989
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001990static __be32
1991nfsd4_layoutcommit(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001992 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001993{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001994 struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001995 const struct nfsd4_layout_seg *seg = &lcp->lc_seg;
1996 struct svc_fh *current_fh = &cstate->current_fh;
1997 const struct nfsd4_layout_ops *ops;
1998 loff_t new_size = lcp->lc_last_wr + 1;
1999 struct inode *inode;
2000 struct nfs4_layout_stateid *ls;
2001 __be32 nfserr;
2002
2003 nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_WRITE);
2004 if (nfserr)
2005 goto out;
2006
2007 nfserr = nfserr_layoutunavailable;
2008 ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
2009 if (!ops)
2010 goto out;
David Howells2b0143b2015-03-17 22:25:59 +00002011 inode = d_inode(current_fh->fh_dentry);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002012
2013 nfserr = nfserr_inval;
2014 if (new_size <= seg->offset) {
2015 dprintk("pnfsd: last write before layout segment\n");
2016 goto out;
2017 }
2018 if (new_size > seg->offset + seg->length) {
2019 dprintk("pnfsd: last write beyond layout segment\n");
2020 goto out;
2021 }
2022 if (!lcp->lc_newoffset && new_size > i_size_read(inode)) {
2023 dprintk("pnfsd: layoutcommit beyond EOF\n");
2024 goto out;
2025 }
2026
2027 nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lcp->lc_sid,
2028 false, lcp->lc_layout_type,
2029 &ls);
2030 if (nfserr) {
Chuck Leverf394b622018-03-27 10:53:11 -04002031 trace_nfsd_layout_commit_lookup_fail(&lcp->lc_sid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002032 /* fixup error code as per RFC5661 */
2033 if (nfserr == nfserr_bad_stateid)
2034 nfserr = nfserr_badlayout;
2035 goto out;
2036 }
2037
Jeff Laytoncc8a5532015-09-17 07:58:24 -04002038 /* LAYOUTCOMMIT does not require any serialization */
2039 mutex_unlock(&ls->ls_mutex);
2040
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002041 if (new_size > i_size_read(inode)) {
2042 lcp->lc_size_chg = 1;
2043 lcp->lc_newsize = new_size;
2044 } else {
2045 lcp->lc_size_chg = 0;
2046 }
2047
Kinglong Meed8398fc2015-07-07 10:12:03 +08002048 nfserr = ops->proc_layoutcommit(inode, lcp);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002049 nfs4_put_stid(&ls->ls_stid);
2050out:
2051 return nfserr;
2052}
2053
2054static __be32
2055nfsd4_layoutreturn(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02002056 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002057{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002058 struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002059 struct svc_fh *current_fh = &cstate->current_fh;
2060 __be32 nfserr;
2061
2062 nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_NOP);
2063 if (nfserr)
2064 goto out;
2065
2066 nfserr = nfserr_layoutunavailable;
2067 if (!nfsd4_layout_verify(current_fh->fh_export, lrp->lr_layout_type))
2068 goto out;
2069
2070 switch (lrp->lr_seg.iomode) {
2071 case IOMODE_READ:
2072 case IOMODE_RW:
2073 case IOMODE_ANY:
2074 break;
2075 default:
2076 dprintk("%s: invalid iomode %d\n", __func__,
2077 lrp->lr_seg.iomode);
2078 nfserr = nfserr_inval;
2079 goto out;
2080 }
2081
2082 switch (lrp->lr_return_type) {
2083 case RETURN_FILE:
2084 nfserr = nfsd4_return_file_layouts(rqstp, cstate, lrp);
2085 break;
2086 case RETURN_FSID:
2087 case RETURN_ALL:
2088 nfserr = nfsd4_return_client_layouts(rqstp, cstate, lrp);
2089 break;
2090 default:
2091 dprintk("%s: invalid return_type %d\n", __func__,
2092 lrp->lr_return_type);
2093 nfserr = nfserr_inval;
2094 break;
2095 }
2096out:
2097 return nfserr;
2098}
2099#endif /* CONFIG_NFSD_PNFS */
2100
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002101static __be32
2102nfsd4_getxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2103 union nfsd4_op_u *u)
2104{
2105 struct nfsd4_getxattr *getxattr = &u->getxattr;
2106
2107 return nfsd_getxattr(rqstp, &cstate->current_fh,
2108 getxattr->getxa_name, &getxattr->getxa_buf,
2109 &getxattr->getxa_len);
2110}
2111
2112static __be32
2113nfsd4_setxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2114 union nfsd4_op_u *u)
2115{
2116 struct nfsd4_setxattr *setxattr = &u->setxattr;
2117 __be32 ret;
2118
2119 if (opens_in_grace(SVC_NET(rqstp)))
2120 return nfserr_grace;
2121
2122 ret = nfsd_setxattr(rqstp, &cstate->current_fh, setxattr->setxa_name,
2123 setxattr->setxa_buf, setxattr->setxa_len,
2124 setxattr->setxa_flags);
2125
2126 if (!ret)
2127 set_change_info(&setxattr->setxa_cinfo, &cstate->current_fh);
2128
2129 return ret;
2130}
2131
2132static __be32
2133nfsd4_listxattrs(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2134 union nfsd4_op_u *u)
2135{
2136 /*
2137 * Get the entire list, then copy out only the user attributes
2138 * in the encode function.
2139 */
2140 return nfsd_listxattr(rqstp, &cstate->current_fh,
2141 &u->listxattrs.lsxa_buf, &u->listxattrs.lsxa_len);
2142}
2143
2144static __be32
2145nfsd4_removexattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2146 union nfsd4_op_u *u)
2147{
2148 struct nfsd4_removexattr *removexattr = &u->removexattr;
2149 __be32 ret;
2150
2151 if (opens_in_grace(SVC_NET(rqstp)))
2152 return nfserr_grace;
2153
2154 ret = nfsd_removexattr(rqstp, &cstate->current_fh,
2155 removexattr->rmxa_name);
2156
2157 if (!ret)
2158 set_change_info(&removexattr->rmxa_cinfo, &cstate->current_fh);
2159
2160 return ret;
2161}
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163/*
2164 * NULL call.
2165 */
Al Viro7111c662006-10-19 23:28:45 -07002166static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +02002167nfsd4_proc_null(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Chuck Levercc028a12020-10-02 15:52:44 -04002169 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
Shankar Anande2b20952006-07-10 04:45:44 -07002172static inline void nfsd4_increment_op_stats(u32 opnum)
2173{
2174 if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
Amir Goldsteine567b982021-01-06 09:52:35 +02002175 percpu_counter_inc(&nfsdstats.counter[NFSD_STATS_NFS4_OP(opnum)]);
Shankar Anande2b20952006-07-10 04:45:44 -07002176}
2177
Christoph Hellwigbb2a8b02017-05-08 23:46:47 +02002178static const struct nfsd4_operation nfsd4_ops[];
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002179
Adrian Bunkf1c7f792008-08-08 19:26:42 +03002180static const char *nfsd4_op_name(unsigned opnum);
Benny Halevyb001a1b2008-07-02 11:15:03 +03002181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182/*
J. Bruce Fields57716352010-04-21 12:27:19 -04002183 * Enforce NFSv4.1 COMPOUND ordering rules:
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002184 *
J. Bruce Fields57716352010-04-21 12:27:19 -04002185 * Also note, enforced elsewhere:
2186 * - SEQUENCE other than as first op results in
2187 * NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002188 * - BIND_CONN_TO_SESSION must be the only op in its compound.
2189 * (Enforced in nfsd4_bind_conn_to_session().)
J. Bruce Fields57716352010-04-21 12:27:19 -04002190 * - DESTROY_SESSION must be the final operation in a compound, if
2191 * sessionid's in SEQUENCE and DESTROY_SESSION are the same.
2192 * (Enforced in nfsd4_destroy_session().)
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002193 */
J. Bruce Fields57716352010-04-21 12:27:19 -04002194static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002195{
J. Bruce Fields7a04cfd2018-06-13 15:04:59 -04002196 struct nfsd4_op *first_op = &args->ops[0];
J. Bruce Fields57716352010-04-21 12:27:19 -04002197
2198 /* These ordering requirements don't apply to NFSv4.0: */
2199 if (args->minorversion == 0)
2200 return nfs_ok;
2201 /* This is weird, but OK, not our problem: */
2202 if (args->opcnt == 0)
2203 return nfs_ok;
J. Bruce Fields7a04cfd2018-06-13 15:04:59 -04002204 if (first_op->status == nfserr_op_illegal)
J. Bruce Fields57716352010-04-21 12:27:19 -04002205 return nfs_ok;
J. Bruce Fields7a04cfd2018-06-13 15:04:59 -04002206 if (!(nfsd4_ops[first_op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
J. Bruce Fields57716352010-04-21 12:27:19 -04002207 return nfserr_op_not_in_session;
J. Bruce Fields7a04cfd2018-06-13 15:04:59 -04002208 if (first_op->opnum == OP_SEQUENCE)
J. Bruce Fields57716352010-04-21 12:27:19 -04002209 return nfs_ok;
J. Bruce Fields7a04cfd2018-06-13 15:04:59 -04002210 /*
2211 * So first_op is something allowed outside a session, like
2212 * EXCHANGE_ID; but then it has to be the only op in the
2213 * compound:
2214 */
J. Bruce Fields57716352010-04-21 12:27:19 -04002215 if (args->opcnt != 1)
2216 return nfserr_not_only_op;
2217 return nfs_ok;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002218}
2219
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002220const struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
J. Bruce Fields22b03212011-04-09 11:23:24 -04002221{
2222 return &nfsd4_ops[op->opnum];
2223}
2224
J. Bruce Fields10910062011-01-24 12:11:02 -05002225bool nfsd4_cache_this_op(struct nfsd4_op *op)
2226{
J. Bruce Fieldse372ba62014-05-19 12:27:11 -04002227 if (op->opnum == OP_ILLEGAL)
2228 return false;
J. Bruce Fieldsc8566942011-09-20 08:49:51 -04002229 return OPDESC(op)->op_flags & OP_CACHEME;
J. Bruce Fields10910062011-01-24 12:11:02 -05002230}
2231
J. Bruce Fields68d93182011-04-08 17:00:50 -04002232static bool need_wrongsec_check(struct svc_rqst *rqstp)
2233{
2234 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2235 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
2236 struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
2237 struct nfsd4_op *next = &argp->ops[resp->opcnt];
Christoph Hellwigbb2a8b02017-05-08 23:46:47 +02002238 const struct nfsd4_operation *thisd = OPDESC(this);
2239 const struct nfsd4_operation *nextd;
J. Bruce Fields68d93182011-04-08 17:00:50 -04002240
J. Bruce Fields68d93182011-04-08 17:00:50 -04002241 /*
2242 * Most ops check wronsec on our own; only the putfh-like ops
2243 * have special rules.
2244 */
2245 if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
2246 return false;
2247 /*
2248 * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
2249 * put-filehandle operation if we're not going to use the
2250 * result:
2251 */
2252 if (argp->opcnt == resp->opcnt)
2253 return false;
J. Bruce Fields51904b02014-10-22 14:46:29 -04002254 if (next->opnum == OP_ILLEGAL)
2255 return false;
J. Bruce Fields68d93182011-04-08 17:00:50 -04002256 nextd = OPDESC(next);
2257 /*
2258 * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
2259 * errors themselves as necessary; others should check for them
2260 * now:
2261 */
2262 return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
2263}
2264
J. Bruce Fields2d124dfaa2014-01-15 16:26:42 -05002265static void svcxdr_init_encode(struct svc_rqst *rqstp,
2266 struct nfsd4_compoundres *resp)
2267{
2268 struct xdr_stream *xdr = &resp->xdr;
2269 struct xdr_buf *buf = &rqstp->rq_res;
2270 struct kvec *head = buf->head;
2271
2272 xdr->buf = buf;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002273 xdr->iov = head;
J. Bruce Fields2d124dfaa2014-01-15 16:26:42 -05002274 xdr->p = head->iov_base + head->iov_len;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002275 xdr->end = head->iov_base + PAGE_SIZE - rqstp->rq_auth_slack;
J. Bruce Fields6ac90392014-02-26 17:39:35 -05002276 /* Tail and page_len should be zero at this point: */
2277 buf->len = buf->head[0].iov_len;
Chuck Lever0ae4c3e2020-11-11 15:52:47 -05002278 xdr_reset_scratch_buffer(xdr);
J. Bruce Fields05638dc2014-06-02 12:05:47 -04002279 xdr->page_ptr = buf->pages - 1;
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04002280 buf->buflen = PAGE_SIZE * (1 + rqstp->rq_page_end - buf->pages)
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002281 - rqstp->rq_auth_slack;
J. Bruce Fields2d124dfaa2014-01-15 16:26:42 -05002282}
2283
Olga Kornievskaiab9e86382019-10-07 10:56:48 -04002284#ifdef CONFIG_NFSD_V4_2_INTER_SSC
2285static void
2286check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
2287{
2288 struct nfsd4_op *op, *current_op = NULL, *saved_op = NULL;
2289 struct nfsd4_copy *copy;
2290 struct nfsd4_putfh *putfh;
2291 int i;
2292
2293 /* traverse all operation and if it's a COPY compound, mark the
2294 * source filehandle to skip verification
2295 */
2296 for (i = 0; i < args->opcnt; i++) {
2297 op = &args->ops[i];
2298 if (op->opnum == OP_PUTFH)
2299 current_op = op;
2300 else if (op->opnum == OP_SAVEFH)
2301 saved_op = current_op;
2302 else if (op->opnum == OP_RESTOREFH)
2303 current_op = saved_op;
2304 else if (op->opnum == OP_COPY) {
2305 copy = (struct nfsd4_copy *)&op->u;
2306 if (!saved_op) {
2307 op->status = nfserr_nofilehandle;
2308 return;
2309 }
2310 putfh = (struct nfsd4_putfh *)&saved_op->u;
2311 if (!copy->cp_intra)
2312 putfh->no_verify = true;
2313 }
2314 }
2315}
2316#else
2317static void
2318check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
2319{
2320}
2321#endif
2322
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002323/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 * COMPOUND call.
2325 */
Al Viro7111c662006-10-19 23:28:45 -07002326static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +02002327nfsd4_proc_compound(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
Christoph Hellwiga6beb732017-05-08 17:35:49 +02002329 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2330 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 struct nfsd4_op *op;
Andy Adamsone354d572009-03-28 11:30:52 +03002332 struct nfsd4_compound_state *cstate = &resp->cstate;
Kinglong Mee4daeed22014-03-26 17:10:37 +08002333 struct svc_fh *current_fh = &cstate->current_fh;
2334 struct svc_fh *save_fh = &cstate->save_fh;
Trond Myklebuste333f3b2019-04-09 11:46:19 -04002335 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Al Virob37ad282006-10-19 23:28:59 -07002336 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
J. Bruce Fields2d124dfaa2014-01-15 16:26:42 -05002338 svcxdr_init_encode(rqstp, resp);
J. Bruce Fields4aea24b2014-01-15 15:17:58 -05002339 resp->tagp = resp->xdr.p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 /* reserve space for: taglen, tag, and opcnt */
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05002341 xdr_reserve_space(&resp->xdr, 8 + args->taglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 resp->taglen = args->taglen;
2343 resp->tag = args->tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 resp->rqstp = rqstp;
Kinglong Mee4daeed22014-03-26 17:10:37 +08002345 cstate->minorversion = args->minorversion;
Kinglong Mee4daeed22014-03-26 17:10:37 +08002346 fh_init(current_fh, NFS4_FHSIZE);
2347 fh_init(save_fh, NFS4_FHSIZE);
NeilBrown8ff30fa2010-08-12 17:04:07 +10002348 /*
2349 * Don't use the deferral mechanism for NFSv4; compounds make it
2350 * too hard to avoid non-idempotency problems.
2351 */
Jeff Layton30660e04b2014-11-19 07:51:16 -05002352 clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354 /*
2355 * According to RFC3010, this takes precedence over all other errors.
2356 */
2357 status = nfserr_minor_vers_mismatch;
Trond Myklebuste333f3b2019-04-09 11:46:19 -04002358 if (nfsd_minorversion(nn, args->minorversion, NFSD_TEST) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 goto out;
J. Bruce Fields00781172017-11-15 12:30:27 -05002360 status = nfserr_resource;
2361 if (args->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
2362 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
J. Bruce Fields57716352010-04-21 12:27:19 -04002364 status = nfs41_check_op_ordering(args);
2365 if (status) {
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002366 op = &args->ops[0];
J. Bruce Fields57716352010-04-21 12:27:19 -04002367 op->status = status;
J. Bruce Fields5b7b15ae2018-06-13 15:21:35 -04002368 resp->opcnt = 1;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002369 goto encode_op;
2370 }
Olga Kornievskaiab9e86382019-10-07 10:56:48 -04002371 check_if_stalefh_allowed(args);
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002372
J. Bruce Fields28df3d12017-07-28 16:35:15 -04002373 rqstp->rq_lease_breaker = (void **)&cstate->clp;
2374
Chuck Leverfff40802018-03-27 10:53:54 -04002375 trace_nfsd_compound(rqstp, args->opcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 while (!status && resp->opcnt < args->opcnt) {
2377 op = &args->ops[resp->opcnt++];
2378
2379 /*
2380 * The XDR decode routines may have pre-set op->status;
2381 * for example, if there is a miscellaneous XDR error
2382 * it will be set to nfserr_bad_xdr.
2383 */
J. Bruce Fields9d313b12013-02-28 12:51:49 -08002384 if (op->status) {
2385 if (op->opnum == OP_OPEN)
2386 op->status = nfsd4_open_omfg(rqstp, cstate, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 goto encode_op;
J. Bruce Fields9d313b12013-02-28 12:51:49 -08002388 }
Olga Kornievskaiab9e86382019-10-07 10:56:48 -04002389 if (!current_fh->fh_dentry &&
2390 !HAS_FH_FLAG(current_fh, NFSD4_FH_FOREIGN)) {
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002391 if (!(op->opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002392 op->status = nfserr_nofilehandle;
2393 goto encode_op;
2394 }
Olga Kornievskaiab9e86382019-10-07 10:56:48 -04002395 } else if (current_fh->fh_export &&
2396 current_fh->fh_export->ex_fslocs.migrated &&
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002397 !(op->opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002398 op->status = nfserr_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 goto encode_op;
2400 }
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002401
Kinglong Mee23367452014-03-29 10:23:47 +08002402 fh_clear_wcc(current_fh);
2403
Mi Jinlong58e7b332011-08-28 18:18:56 +08002404 /* If op is non-idempotent */
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002405 if (op->opdesc->op_flags & OP_MODIFIES_SOMETHING) {
J. Bruce Fields4c69d582014-01-28 16:01:04 -05002406 /*
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04002407 * Don't execute this op if we couldn't encode a
2408 * succesful reply:
2409 */
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002410 u32 plen = op->opdesc->op_rsize_bop(rqstp, op);
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04002411 /*
2412 * Plus if there's another operation, make sure
J. Bruce Fields4c69d582014-01-28 16:01:04 -05002413 * we'll have space to at least encode an error:
2414 */
2415 if (resp->opcnt < args->opcnt)
2416 plen += COMPOUND_ERR_SLACK_SPACE;
Mi Jinlong58e7b332011-08-28 18:18:56 +08002417 op->status = nfsd4_check_resp_size(resp, plen);
2418 }
2419
2420 if (op->status)
2421 goto encode_op;
2422
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002423 if (op->opdesc->op_get_currentstateid)
2424 op->opdesc->op_get_currentstateid(cstate, &op->u);
2425 op->status = op->opdesc->op_func(rqstp, cstate, &op->u);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
J. Bruce Fields9a307402017-05-23 12:24:40 -04002427 /* Only from SEQUENCE */
2428 if (cstate->status == nfserr_replay_cache) {
2429 dprintk("%s NFS4.1 replay from cache\n", __func__);
2430 status = op->status;
2431 goto out;
2432 }
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01002433 if (!op->status) {
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002434 if (op->opdesc->op_set_currentstateid)
2435 op->opdesc->op_set_currentstateid(cstate, &op->u);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01002436
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002437 if (op->opdesc->op_flags & OP_CLEAR_STATEID)
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01002438 clear_current_stateid(cstate);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01002439
J. Bruce Fieldsd781e3d2019-12-06 16:07:32 -05002440 if (current_fh->fh_export &&
2441 need_wrongsec_check(rqstp))
Kinglong Mee4daeed22014-03-26 17:10:37 +08002442 op->status = check_nfsd_access(current_fh->fh_export, rqstp);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01002443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444encode_op:
Al Viroa90b0612006-10-19 23:29:03 -07002445 if (op->status == nfserr_replay_me) {
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002446 op->replay = &cstate->replay_owner->so_replay;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002447 nfsd4_encode_replay(&resp->xdr, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 status = op->status = op->replay->rp_status;
2449 } else {
2450 nfsd4_encode_operation(resp, op);
2451 status = op->status;
2452 }
Benny Halevy04077172008-12-15 19:40:49 +02002453
Chuck Leverfff40802018-03-27 10:53:54 -04002454 trace_nfsd_compound_status(args->opcnt, resp->opcnt, status,
2455 nfsd4_op_name(op->opnum));
Benny Halevy04077172008-12-15 19:40:49 +02002456
Jeff Layton58fb12e2014-07-29 21:34:27 -04002457 nfsd4_cstate_clear_replay(cstate);
Shankar Anande2b20952006-07-10 04:45:44 -07002458 nfsd4_increment_op_stats(op->opnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
2460
Kinglong Mee4daeed22014-03-26 17:10:37 +08002461 fh_put(current_fh);
2462 fh_put(save_fh);
2463 BUG_ON(cstate->replay_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464out:
Chuck Levercc028a12020-10-02 15:52:44 -04002465 cstate->status = status;
Andy Adamson2f425872009-04-03 08:27:32 +03002466 /* Reset deferral mechanism for RPC deferrals */
Jeff Layton30660e04b2014-11-19 07:51:16 -05002467 set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
Chuck Levercc028a12020-10-02 15:52:44 -04002468 return rpc_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469}
2470
Mi Jinlong58e7b332011-08-28 18:18:56 +08002471#define op_encode_hdr_size (2)
2472#define op_encode_stateid_maxsz (XDR_QUADLEN(NFS4_STATEID_SIZE))
2473#define op_encode_verifier_maxsz (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
2474#define op_encode_change_info_maxsz (5)
2475#define nfs4_fattr_bitmap_maxsz (4)
2476
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04002477/* We'll fall back on returning no lockowner if run out of space: */
2478#define op_encode_lockowner_maxsz (0)
Mi Jinlong58e7b332011-08-28 18:18:56 +08002479#define op_encode_lock_denied_maxsz (8 + op_encode_lockowner_maxsz)
2480
2481#define nfs4_owner_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
2482
2483#define op_encode_ace_maxsz (3 + nfs4_owner_maxsz)
2484#define op_encode_delegation_maxsz (1 + op_encode_stateid_maxsz + 1 + \
2485 op_encode_ace_maxsz)
2486
2487#define op_encode_channel_attrs_maxsz (6 + 1 + 1)
2488
2489static inline u32 nfsd4_only_status_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2490{
2491 return (op_encode_hdr_size) * sizeof(__be32);
2492}
2493
2494static inline u32 nfsd4_status_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2495{
2496 return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
2497}
2498
Kinglong Mee2282cd22017-02-03 22:36:00 +08002499static inline u32 nfsd4_access_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2500{
2501 /* ac_supported, ac_resp_access */
2502 return (op_encode_hdr_size + 2)* sizeof(__be32);
2503}
2504
Mi Jinlong58e7b332011-08-28 18:18:56 +08002505static inline u32 nfsd4_commit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2506{
2507 return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
2508}
2509
2510static inline u32 nfsd4_create_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2511{
2512 return (op_encode_hdr_size + op_encode_change_info_maxsz
2513 + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
2514}
2515
J. Bruce Fieldsb86cef62014-03-23 12:01:48 -04002516/*
2517 * Note since this is an idempotent operation we won't insist on failing
2518 * the op prematurely if the estimate is too large. We may turn off splice
2519 * reads unnecessarily.
2520 */
2521static inline u32 nfsd4_getattr_rsize(struct svc_rqst *rqstp,
2522 struct nfsd4_op *op)
2523{
2524 u32 *bmap = op->u.getattr.ga_bmval;
2525 u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
2526 u32 ret = 0;
2527
2528 if (bmap0 & FATTR4_WORD0_ACL)
2529 return svc_max_payload(rqstp);
2530 if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
2531 return svc_max_payload(rqstp);
2532
2533 if (bmap1 & FATTR4_WORD1_OWNER) {
2534 ret += IDMAP_NAMESZ + 4;
2535 bmap1 &= ~FATTR4_WORD1_OWNER;
2536 }
2537 if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
2538 ret += IDMAP_NAMESZ + 4;
2539 bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
2540 }
2541 if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
2542 ret += NFS4_FHSIZE + 4;
2543 bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
2544 }
2545 if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
Kinglong Mee1ec8c0c2015-03-28 23:46:09 +08002546 ret += NFS4_MAXLABELLEN + 12;
J. Bruce Fieldsb86cef62014-03-23 12:01:48 -04002547 bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2548 }
2549 /*
2550 * Largest of remaining attributes are 16 bytes (e.g.,
2551 * supported_attributes)
2552 */
2553 ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
2554 /* bitmask, length */
2555 ret += 20;
2556 return ret;
2557}
2558
Kinglong Mee2282cd22017-02-03 22:36:00 +08002559static inline u32 nfsd4_getfh_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2560{
2561 return (op_encode_hdr_size + 1) * sizeof(__be32) + NFS4_FHSIZE;
2562}
2563
Mi Jinlong58e7b332011-08-28 18:18:56 +08002564static inline u32 nfsd4_link_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2565{
2566 return (op_encode_hdr_size + op_encode_change_info_maxsz)
2567 * sizeof(__be32);
2568}
2569
2570static inline u32 nfsd4_lock_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2571{
2572 return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
2573 * sizeof(__be32);
2574}
2575
2576static inline u32 nfsd4_open_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2577{
2578 return (op_encode_hdr_size + op_encode_stateid_maxsz
2579 + op_encode_change_info_maxsz + 1
2580 + nfs4_fattr_bitmap_maxsz
2581 + op_encode_delegation_maxsz) * sizeof(__be32);
2582}
2583
2584static inline u32 nfsd4_read_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2585{
2586 u32 maxcount = 0, rlen = 0;
2587
2588 maxcount = svc_max_payload(rqstp);
Kinglong Mee3c7aa152014-06-10 18:08:19 +08002589 rlen = min(op->u.read.rd_length, maxcount);
Mi Jinlong58e7b332011-08-28 18:18:56 +08002590
J. Bruce Fields622f5602014-05-16 14:38:20 -04002591 return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
Mi Jinlong58e7b332011-08-28 18:18:56 +08002592}
2593
Anna Schumaker528b8492020-09-28 13:08:58 -04002594static inline u32 nfsd4_read_plus_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2595{
2596 u32 maxcount = svc_max_payload(rqstp);
2597 u32 rlen = min(op->u.read.rd_length, maxcount);
2598 /*
2599 * If we detect that the file changed during hole encoding, then we
2600 * recover by encoding the remaining reply as data. This means we need
2601 * to set aside enough room to encode two data segments.
2602 */
2603 u32 seg_len = 2 * (1 + 2 + 1);
2604
2605 return (op_encode_hdr_size + 2 + seg_len + XDR_QUADLEN(rlen)) * sizeof(__be32);
2606}
2607
Mi Jinlong58e7b332011-08-28 18:18:56 +08002608static inline u32 nfsd4_readdir_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2609{
Kinglong Mee3c7aa152014-06-10 18:08:19 +08002610 u32 maxcount = 0, rlen = 0;
Mi Jinlong58e7b332011-08-28 18:18:56 +08002611
Kinglong Mee3c7aa152014-06-10 18:08:19 +08002612 maxcount = svc_max_payload(rqstp);
2613 rlen = min(op->u.readdir.rd_maxcount, maxcount);
Mi Jinlong58e7b332011-08-28 18:18:56 +08002614
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002615 return (op_encode_hdr_size + op_encode_verifier_maxsz +
2616 XDR_QUADLEN(rlen)) * sizeof(__be32);
Mi Jinlong58e7b332011-08-28 18:18:56 +08002617}
2618
Kinglong Mee2282cd22017-02-03 22:36:00 +08002619static inline u32 nfsd4_readlink_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2620{
2621 return (op_encode_hdr_size + 1) * sizeof(__be32) + PAGE_SIZE;
2622}
2623
Mi Jinlong58e7b332011-08-28 18:18:56 +08002624static inline u32 nfsd4_remove_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2625{
2626 return (op_encode_hdr_size + op_encode_change_info_maxsz)
2627 * sizeof(__be32);
2628}
2629
2630static inline u32 nfsd4_rename_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2631{
2632 return (op_encode_hdr_size + op_encode_change_info_maxsz
2633 + op_encode_change_info_maxsz) * sizeof(__be32);
2634}
2635
J. Bruce Fieldsccae70a2014-03-23 09:34:22 -07002636static inline u32 nfsd4_sequence_rsize(struct svc_rqst *rqstp,
2637 struct nfsd4_op *op)
2638{
J. Bruce Fieldsd1d84c92014-08-21 15:04:31 -04002639 return (op_encode_hdr_size
2640 + XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32);
J. Bruce Fieldsccae70a2014-03-23 09:34:22 -07002641}
2642
Kinglong Mee2282cd22017-02-03 22:36:00 +08002643static inline u32 nfsd4_test_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2644{
2645 return (op_encode_hdr_size + 1 + op->u.test_stateid.ts_num_ids)
2646 * sizeof(__be32);
2647}
2648
Mi Jinlong58e7b332011-08-28 18:18:56 +08002649static inline u32 nfsd4_setattr_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2650{
2651 return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
2652}
2653
Kinglong Mee2282cd22017-02-03 22:36:00 +08002654static inline u32 nfsd4_secinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2655{
2656 return (op_encode_hdr_size + RPC_AUTH_MAXFLAVOR *
2657 (4 + XDR_QUADLEN(GSS_OID_MAX_LEN))) * sizeof(__be32);
2658}
2659
Mi Jinlong58e7b332011-08-28 18:18:56 +08002660static inline u32 nfsd4_setclientid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2661{
J. Bruce Fields480efae2014-03-10 14:17:55 -04002662 return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
2663 sizeof(__be32);
Mi Jinlong58e7b332011-08-28 18:18:56 +08002664}
2665
2666static inline u32 nfsd4_write_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2667{
J. Bruce Fieldsf34e4322014-05-16 21:24:56 -04002668 return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
Mi Jinlong58e7b332011-08-28 18:18:56 +08002669}
2670
2671static inline u32 nfsd4_exchange_id_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2672{
2673 return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
Kinglong Meea8bb84b2013-12-10 15:24:36 +08002674 1 + 1 + /* eir_flags, spr_how */\
2675 4 + /* spo_must_enforce & _allow with bitmap */\
Mi Jinlong58e7b332011-08-28 18:18:56 +08002676 2 + /*eir_server_owner.so_minor_id */\
2677 /* eir_server_owner.so_major_id<> */\
2678 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
2679 /* eir_server_scope<> */\
2680 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
2681 1 + /* eir_server_impl_id array length */\
2682 0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
2683}
2684
2685static inline u32 nfsd4_bind_conn_to_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2686{
2687 return (op_encode_hdr_size + \
2688 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
2689 2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
2690}
2691
2692static inline u32 nfsd4_create_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2693{
2694 return (op_encode_hdr_size + \
2695 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
2696 2 + /* csr_sequence, csr_flags */\
2697 op_encode_channel_attrs_maxsz + \
2698 op_encode_channel_attrs_maxsz) * sizeof(__be32);
2699}
2700
Anna Schumaker29ae7f92016-09-07 15:57:30 -04002701static inline u32 nfsd4_copy_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2702{
2703 return (op_encode_hdr_size +
2704 1 /* wr_callback */ +
2705 op_encode_stateid_maxsz /* wr_callback */ +
2706 2 /* wr_count */ +
2707 1 /* wr_committed */ +
2708 op_encode_verifier_maxsz +
2709 1 /* cr_consecutive */ +
2710 1 /* cr_synchronous */) * sizeof(__be32);
2711}
2712
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04002713static inline u32 nfsd4_offload_status_rsize(struct svc_rqst *rqstp,
2714 struct nfsd4_op *op)
2715{
2716 return (op_encode_hdr_size +
2717 2 /* osr_count */ +
2718 1 /* osr_complete<1> optional 0 for now */) * sizeof(__be32);
2719}
2720
Olga Kornievskaia51911862019-08-08 11:14:59 -04002721static inline u32 nfsd4_copy_notify_rsize(struct svc_rqst *rqstp,
2722 struct nfsd4_op *op)
2723{
2724 return (op_encode_hdr_size +
2725 3 /* cnr_lease_time */ +
2726 1 /* We support one cnr_source_server */ +
2727 1 /* cnr_stateid seq */ +
2728 op_encode_stateid_maxsz /* cnr_stateid */ +
2729 1 /* num cnr_source_server*/ +
2730 1 /* nl4_type */ +
2731 1 /* nl4 size */ +
2732 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) /*nl4_loc + nl4_loc_sz */)
2733 * sizeof(__be32);
2734}
2735
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002736#ifdef CONFIG_NFSD_PNFS
Kinglong Mee2282cd22017-02-03 22:36:00 +08002737static inline u32 nfsd4_getdeviceinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2738{
2739 u32 maxcount = 0, rlen = 0;
2740
2741 maxcount = svc_max_payload(rqstp);
2742 rlen = min(op->u.getdeviceinfo.gd_maxcount, maxcount);
2743
2744 return (op_encode_hdr_size +
2745 1 /* gd_layout_type*/ +
2746 XDR_QUADLEN(rlen) +
2747 2 /* gd_notify_types */) * sizeof(__be32);
2748}
2749
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002750/*
2751 * At this stage we don't really know what layout driver will handle the request,
2752 * so we need to define an arbitrary upper bound here.
2753 */
2754#define MAX_LAYOUT_SIZE 128
2755static inline u32 nfsd4_layoutget_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2756{
2757 return (op_encode_hdr_size +
2758 1 /* logr_return_on_close */ +
2759 op_encode_stateid_maxsz +
2760 1 /* nr of layouts */ +
2761 MAX_LAYOUT_SIZE) * sizeof(__be32);
2762}
2763
2764static inline u32 nfsd4_layoutcommit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2765{
2766 return (op_encode_hdr_size +
2767 1 /* locr_newsize */ +
2768 2 /* ns_size */) * sizeof(__be32);
2769}
2770
2771static inline u32 nfsd4_layoutreturn_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2772{
2773 return (op_encode_hdr_size +
2774 1 /* lrs_stateid */ +
2775 op_encode_stateid_maxsz) * sizeof(__be32);
2776}
2777#endif /* CONFIG_NFSD_PNFS */
2778
Kinglong Mee2282cd22017-02-03 22:36:00 +08002779
2780static inline u32 nfsd4_seek_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2781{
2782 return (op_encode_hdr_size + 3) * sizeof(__be32);
2783}
2784
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002785static inline u32 nfsd4_getxattr_rsize(struct svc_rqst *rqstp,
2786 struct nfsd4_op *op)
2787{
2788 u32 maxcount, rlen;
2789
2790 maxcount = svc_max_payload(rqstp);
2791 rlen = min_t(u32, XATTR_SIZE_MAX, maxcount);
2792
2793 return (op_encode_hdr_size + 1 + XDR_QUADLEN(rlen)) * sizeof(__be32);
2794}
2795
2796static inline u32 nfsd4_setxattr_rsize(struct svc_rqst *rqstp,
2797 struct nfsd4_op *op)
2798{
2799 return (op_encode_hdr_size + op_encode_change_info_maxsz)
2800 * sizeof(__be32);
2801}
2802static inline u32 nfsd4_listxattrs_rsize(struct svc_rqst *rqstp,
2803 struct nfsd4_op *op)
2804{
2805 u32 maxcount, rlen;
2806
2807 maxcount = svc_max_payload(rqstp);
2808 rlen = min(op->u.listxattrs.lsxa_maxcount, maxcount);
2809
2810 return (op_encode_hdr_size + 4 + XDR_QUADLEN(rlen)) * sizeof(__be32);
2811}
2812
2813static inline u32 nfsd4_removexattr_rsize(struct svc_rqst *rqstp,
2814 struct nfsd4_op *op)
2815{
2816 return (op_encode_hdr_size + op_encode_change_info_maxsz)
2817 * sizeof(__be32);
2818}
2819
2820
Christoph Hellwigbb2a8b02017-05-08 23:46:47 +02002821static const struct nfsd4_operation nfsd4_ops[] = {
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002822 [OP_ACCESS] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002823 .op_func = nfsd4_access,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002824 .op_name = "OP_ACCESS",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002825 .op_rsize_bop = nfsd4_access_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002826 },
2827 [OP_CLOSE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002828 .op_func = nfsd4_close,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002829 .op_flags = OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002830 .op_name = "OP_CLOSE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002831 .op_rsize_bop = nfsd4_status_stateid_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02002832 .op_get_currentstateid = nfsd4_get_closestateid,
Christoph Hellwigb60e9852017-05-08 20:03:15 +02002833 .op_set_currentstateid = nfsd4_set_closestateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002834 },
2835 [OP_COMMIT] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002836 .op_func = nfsd4_commit,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002837 .op_flags = OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002838 .op_name = "OP_COMMIT",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002839 .op_rsize_bop = nfsd4_commit_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002840 },
2841 [OP_CREATE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002842 .op_func = nfsd4_create,
Tigran Mkrtchyand1471052012-02-13 22:55:29 +01002843 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002844 .op_name = "OP_CREATE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002845 .op_rsize_bop = nfsd4_create_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002846 },
2847 [OP_DELEGRETURN] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002848 .op_func = nfsd4_delegreturn,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002849 .op_flags = OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002850 .op_name = "OP_DELEGRETURN",
Mi Jinlong58e7b332011-08-28 18:18:56 +08002851 .op_rsize_bop = nfsd4_only_status_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02002852 .op_get_currentstateid = nfsd4_get_delegreturnstateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002853 },
2854 [OP_GETATTR] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002855 .op_func = nfsd4_getattr,
J.Bruce Fieldseeac2942006-12-13 00:35:39 -08002856 .op_flags = ALLOWED_ON_ABSENT_FS,
J. Bruce Fieldsb86cef62014-03-23 12:01:48 -04002857 .op_rsize_bop = nfsd4_getattr_rsize,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002858 .op_name = "OP_GETATTR",
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002859 },
2860 [OP_GETFH] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002861 .op_func = nfsd4_getfh,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002862 .op_name = "OP_GETFH",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002863 .op_rsize_bop = nfsd4_getfh_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002864 },
2865 [OP_LINK] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002866 .op_func = nfsd4_link,
J. Bruce Fieldsc8566942011-09-20 08:49:51 -04002867 .op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
2868 | OP_CACHEME,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002869 .op_name = "OP_LINK",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002870 .op_rsize_bop = nfsd4_link_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002871 },
2872 [OP_LOCK] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002873 .op_func = nfsd4_lock,
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04002874 .op_flags = OP_MODIFIES_SOMETHING |
2875 OP_NONTRIVIAL_ERROR_ENCODE,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002876 .op_name = "OP_LOCK",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002877 .op_rsize_bop = nfsd4_lock_rsize,
Christoph Hellwigb60e9852017-05-08 20:03:15 +02002878 .op_set_currentstateid = nfsd4_set_lockstateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002879 },
2880 [OP_LOCKT] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002881 .op_func = nfsd4_lockt,
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04002882 .op_flags = OP_NONTRIVIAL_ERROR_ENCODE,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002883 .op_name = "OP_LOCKT",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002884 .op_rsize_bop = nfsd4_lock_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002885 },
2886 [OP_LOCKU] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002887 .op_func = nfsd4_locku,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002888 .op_flags = OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002889 .op_name = "OP_LOCKU",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002890 .op_rsize_bop = nfsd4_status_stateid_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02002891 .op_get_currentstateid = nfsd4_get_lockustateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002892 },
2893 [OP_LOOKUP] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002894 .op_func = nfsd4_lookup,
Tigran Mkrtchyand1471052012-02-13 22:55:29 +01002895 .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002896 .op_name = "OP_LOOKUP",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002897 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002898 },
2899 [OP_LOOKUPP] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002900 .op_func = nfsd4_lookupp,
Tigran Mkrtchyand1471052012-02-13 22:55:29 +01002901 .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002902 .op_name = "OP_LOOKUPP",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002903 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002904 },
2905 [OP_NVERIFY] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002906 .op_func = nfsd4_nverify,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002907 .op_name = "OP_NVERIFY",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002908 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002909 },
2910 [OP_OPEN] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002911 .op_func = nfsd4_open,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002912 .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002913 .op_name = "OP_OPEN",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002914 .op_rsize_bop = nfsd4_open_rsize,
Christoph Hellwigb60e9852017-05-08 20:03:15 +02002915 .op_set_currentstateid = nfsd4_set_openstateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002916 },
2917 [OP_OPEN_CONFIRM] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002918 .op_func = nfsd4_open_confirm,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002919 .op_flags = OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002920 .op_name = "OP_OPEN_CONFIRM",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002921 .op_rsize_bop = nfsd4_status_stateid_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002922 },
2923 [OP_OPEN_DOWNGRADE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002924 .op_func = nfsd4_open_downgrade,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002925 .op_flags = OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002926 .op_name = "OP_OPEN_DOWNGRADE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002927 .op_rsize_bop = nfsd4_status_stateid_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02002928 .op_get_currentstateid = nfsd4_get_opendowngradestateid,
Christoph Hellwigb60e9852017-05-08 20:03:15 +02002929 .op_set_currentstateid = nfsd4_set_opendowngradestateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002930 },
2931 [OP_PUTFH] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002932 .op_func = nfsd4_putfh,
J. Bruce Fields68d93182011-04-08 17:00:50 -04002933 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
J. Bruce Fields5b648692014-03-07 11:43:58 -05002934 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002935 .op_name = "OP_PUTFH",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002936 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002937 },
J.Bruce Fieldseeac2942006-12-13 00:35:39 -08002938 [OP_PUTPUBFH] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002939 .op_func = nfsd4_putrootfh,
J. Bruce Fields68d93182011-04-08 17:00:50 -04002940 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
J. Bruce Fields5b648692014-03-07 11:43:58 -05002941 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002942 .op_name = "OP_PUTPUBFH",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002943 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldseeac2942006-12-13 00:35:39 -08002944 },
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002945 [OP_PUTROOTFH] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002946 .op_func = nfsd4_putrootfh,
J. Bruce Fields68d93182011-04-08 17:00:50 -04002947 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
J. Bruce Fields5b648692014-03-07 11:43:58 -05002948 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002949 .op_name = "OP_PUTROOTFH",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002950 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002951 },
2952 [OP_READ] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002953 .op_func = nfsd4_read,
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04002954 .op_release = nfsd4_read_release,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002955 .op_name = "OP_READ",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002956 .op_rsize_bop = nfsd4_read_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02002957 .op_get_currentstateid = nfsd4_get_readstateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002958 },
2959 [OP_READDIR] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002960 .op_func = nfsd4_readdir,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002961 .op_name = "OP_READDIR",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002962 .op_rsize_bop = nfsd4_readdir_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002963 },
2964 [OP_READLINK] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002965 .op_func = nfsd4_readlink,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002966 .op_name = "OP_READLINK",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002967 .op_rsize_bop = nfsd4_readlink_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002968 },
2969 [OP_REMOVE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002970 .op_func = nfsd4_remove,
J. Bruce Fieldsc8566942011-09-20 08:49:51 -04002971 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002972 .op_name = "OP_REMOVE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002973 .op_rsize_bop = nfsd4_remove_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002974 },
2975 [OP_RENAME] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002976 .op_func = nfsd4_rename,
J. Bruce Fieldsc8566942011-09-20 08:49:51 -04002977 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002978 .op_name = "OP_RENAME",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002979 .op_rsize_bop = nfsd4_rename_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002980 },
2981 [OP_RENEW] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002982 .op_func = nfsd4_renew,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002983 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
2984 | OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002985 .op_name = "OP_RENEW",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002986 .op_rsize_bop = nfsd4_only_status_rsize,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002987
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002988 },
2989 [OP_RESTOREFH] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002990 .op_func = nfsd4_restorefh,
J. Bruce Fields68d93182011-04-08 17:00:50 -04002991 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
Mi Jinlong58e7b332011-08-28 18:18:56 +08002992 | OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002993 .op_name = "OP_RESTOREFH",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002994 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002995 },
2996 [OP_SAVEFH] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002997 .op_func = nfsd4_savefh,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002998 .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002999 .op_name = "OP_SAVEFH",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003000 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003001 },
Andy Adamsondcb488a32007-07-17 04:04:51 -07003002 [OP_SECINFO] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003003 .op_func = nfsd4_secinfo,
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04003004 .op_release = nfsd4_secinfo_release,
J. Bruce Fields68d93182011-04-08 17:00:50 -04003005 .op_flags = OP_HANDLES_WRONGSEC,
Benny Halevyb001a1b2008-07-02 11:15:03 +03003006 .op_name = "OP_SECINFO",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003007 .op_rsize_bop = nfsd4_secinfo_rsize,
Andy Adamsondcb488a32007-07-17 04:04:51 -07003008 },
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003009 [OP_SETATTR] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003010 .op_func = nfsd4_setattr,
Benny Halevyb001a1b2008-07-02 11:15:03 +03003011 .op_name = "OP_SETATTR",
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04003012 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME
3013 | OP_NONTRIVIAL_ERROR_ENCODE,
Christoph Hellwig1c122632017-05-08 20:42:10 +02003014 .op_rsize_bop = nfsd4_setattr_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02003015 .op_get_currentstateid = nfsd4_get_setattrstateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003016 },
3017 [OP_SETCLIENTID] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003018 .op_func = nfsd4_setclientid,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003019 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04003020 | OP_MODIFIES_SOMETHING | OP_CACHEME
3021 | OP_NONTRIVIAL_ERROR_ENCODE,
Benny Halevyb001a1b2008-07-02 11:15:03 +03003022 .op_name = "OP_SETCLIENTID",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003023 .op_rsize_bop = nfsd4_setclientid_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003024 },
3025 [OP_SETCLIENTID_CONFIRM] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003026 .op_func = nfsd4_setclientid_confirm,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003027 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
J. Bruce Fieldsc8566942011-09-20 08:49:51 -04003028 | OP_MODIFIES_SOMETHING | OP_CACHEME,
Benny Halevyb001a1b2008-07-02 11:15:03 +03003029 .op_name = "OP_SETCLIENTID_CONFIRM",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003030 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003031 },
3032 [OP_VERIFY] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003033 .op_func = nfsd4_verify,
Benny Halevyb001a1b2008-07-02 11:15:03 +03003034 .op_name = "OP_VERIFY",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003035 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003036 },
3037 [OP_WRITE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003038 .op_func = nfsd4_write,
J. Bruce Fieldsc8566942011-09-20 08:49:51 -04003039 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
Benny Halevyb001a1b2008-07-02 11:15:03 +03003040 .op_name = "OP_WRITE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003041 .op_rsize_bop = nfsd4_write_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02003042 .op_get_currentstateid = nfsd4_get_writestateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003043 },
3044 [OP_RELEASE_LOCKOWNER] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003045 .op_func = nfsd4_release_lockowner,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003046 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3047 | OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03003048 .op_name = "OP_RELEASE_LOCKOWNER",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003049 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003050 },
Andy Adamson069b6ad2009-04-03 08:27:58 +03003051
3052 /* NFSv4.1 operations */
3053 [OP_EXCHANGE_ID] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003054 .op_func = nfsd4_exchange_id,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003055 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3056 | OP_MODIFIES_SOMETHING,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003057 .op_name = "OP_EXCHANGE_ID",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003058 .op_rsize_bop = nfsd4_exchange_id_rsize,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003059 },
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003060 [OP_BACKCHANNEL_CTL] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003061 .op_func = nfsd4_backchannel_ctl,
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003062 .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
3063 .op_name = "OP_BACKCHANNEL_CTL",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003064 .op_rsize_bop = nfsd4_only_status_rsize,
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003065 },
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003066 [OP_BIND_CONN_TO_SESSION] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003067 .op_func = nfsd4_bind_conn_to_session,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003068 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3069 | OP_MODIFIES_SOMETHING,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003070 .op_name = "OP_BIND_CONN_TO_SESSION",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003071 .op_rsize_bop = nfsd4_bind_conn_to_session_rsize,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003072 },
Andy Adamson069b6ad2009-04-03 08:27:58 +03003073 [OP_CREATE_SESSION] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003074 .op_func = nfsd4_create_session,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003075 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3076 | OP_MODIFIES_SOMETHING,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003077 .op_name = "OP_CREATE_SESSION",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003078 .op_rsize_bop = nfsd4_create_session_rsize,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003079 },
3080 [OP_DESTROY_SESSION] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003081 .op_func = nfsd4_destroy_session,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003082 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3083 | OP_MODIFIES_SOMETHING,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003084 .op_name = "OP_DESTROY_SESSION",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003085 .op_rsize_bop = nfsd4_only_status_rsize,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003086 },
3087 [OP_SEQUENCE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003088 .op_func = nfsd4_sequence,
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003089 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003090 .op_name = "OP_SEQUENCE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003091 .op_rsize_bop = nfsd4_sequence_rsize,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003092 },
Benny Halevy094b5d72011-06-16 11:39:10 -04003093 [OP_DESTROY_CLIENTID] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003094 .op_func = nfsd4_destroy_clientid,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003095 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3096 | OP_MODIFIES_SOMETHING,
Benny Halevy094b5d72011-06-16 11:39:10 -04003097 .op_name = "OP_DESTROY_CLIENTID",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003098 .op_rsize_bop = nfsd4_only_status_rsize,
Benny Halevy094b5d72011-06-16 11:39:10 -04003099 },
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003100 [OP_RECLAIM_COMPLETE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003101 .op_func = nfsd4_reclaim_complete,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003102 .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003103 .op_name = "OP_RECLAIM_COMPLETE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003104 .op_rsize_bop = nfsd4_only_status_rsize,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003105 },
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05003106 [OP_SECINFO_NO_NAME] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003107 .op_func = nfsd4_secinfo_no_name,
Eryu Guanec572b92017-09-29 15:01:10 +08003108 .op_release = nfsd4_secinfo_no_name_release,
J. Bruce Fields68d93182011-04-08 17:00:50 -04003109 .op_flags = OP_HANDLES_WRONGSEC,
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05003110 .op_name = "OP_SECINFO_NO_NAME",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003111 .op_rsize_bop = nfsd4_secinfo_rsize,
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05003112 },
Bryan Schumaker17456802011-07-13 10:50:48 -04003113 [OP_TEST_STATEID] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003114 .op_func = nfsd4_test_stateid,
Bryan Schumaker17456802011-07-13 10:50:48 -04003115 .op_flags = ALLOWED_WITHOUT_FH,
3116 .op_name = "OP_TEST_STATEID",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003117 .op_rsize_bop = nfsd4_test_stateid_rsize,
Bryan Schumaker17456802011-07-13 10:50:48 -04003118 },
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003119 [OP_FREE_STATEID] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003120 .op_func = nfsd4_free_stateid,
Mi Jinlong58e7b332011-08-28 18:18:56 +08003121 .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003122 .op_name = "OP_FREE_STATEID",
Christoph Hellwig57832e72017-05-08 20:37:33 +02003123 .op_get_currentstateid = nfsd4_get_freestateid,
Christoph Hellwig1c122632017-05-08 20:42:10 +02003124 .op_rsize_bop = nfsd4_only_status_rsize,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003125 },
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003126#ifdef CONFIG_NFSD_PNFS
3127 [OP_GETDEVICEINFO] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003128 .op_func = nfsd4_getdeviceinfo,
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04003129 .op_release = nfsd4_getdeviceinfo_release,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003130 .op_flags = ALLOWED_WITHOUT_FH,
3131 .op_name = "OP_GETDEVICEINFO",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003132 .op_rsize_bop = nfsd4_getdeviceinfo_rsize,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003133 },
3134 [OP_LAYOUTGET] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003135 .op_func = nfsd4_layoutget,
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04003136 .op_release = nfsd4_layoutget_release,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003137 .op_flags = OP_MODIFIES_SOMETHING,
3138 .op_name = "OP_LAYOUTGET",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003139 .op_rsize_bop = nfsd4_layoutget_rsize,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003140 },
3141 [OP_LAYOUTCOMMIT] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003142 .op_func = nfsd4_layoutcommit,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003143 .op_flags = OP_MODIFIES_SOMETHING,
3144 .op_name = "OP_LAYOUTCOMMIT",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003145 .op_rsize_bop = nfsd4_layoutcommit_rsize,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003146 },
3147 [OP_LAYOUTRETURN] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003148 .op_func = nfsd4_layoutreturn,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003149 .op_flags = OP_MODIFIES_SOMETHING,
3150 .op_name = "OP_LAYOUTRETURN",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003151 .op_rsize_bop = nfsd4_layoutreturn_rsize,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003152 },
3153#endif /* CONFIG_NFSD_PNFS */
Anna Schumaker24bab492014-09-26 13:58:27 -04003154
3155 /* NFSv4.2 operations */
Anna Schumaker95d871f2014-11-07 14:44:26 -05003156 [OP_ALLOCATE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003157 .op_func = nfsd4_allocate,
Olga Kornievskaia03b31f42018-12-04 14:09:21 -05003158 .op_flags = OP_MODIFIES_SOMETHING,
Anna Schumaker95d871f2014-11-07 14:44:26 -05003159 .op_name = "OP_ALLOCATE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003160 .op_rsize_bop = nfsd4_only_status_rsize,
Anna Schumaker95d871f2014-11-07 14:44:26 -05003161 },
Anna Schumakerb0cb9082014-11-07 14:44:27 -05003162 [OP_DEALLOCATE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003163 .op_func = nfsd4_deallocate,
Olga Kornievskaia03b31f42018-12-04 14:09:21 -05003164 .op_flags = OP_MODIFIES_SOMETHING,
Anna Schumakerb0cb9082014-11-07 14:44:27 -05003165 .op_name = "OP_DEALLOCATE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003166 .op_rsize_bop = nfsd4_only_status_rsize,
Anna Schumakerb0cb9082014-11-07 14:44:27 -05003167 },
Christoph Hellwigffa01602015-12-03 12:59:52 +01003168 [OP_CLONE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003169 .op_func = nfsd4_clone,
Olga Kornievskaia03b31f42018-12-04 14:09:21 -05003170 .op_flags = OP_MODIFIES_SOMETHING,
Christoph Hellwigffa01602015-12-03 12:59:52 +01003171 .op_name = "OP_CLONE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003172 .op_rsize_bop = nfsd4_only_status_rsize,
Christoph Hellwigffa01602015-12-03 12:59:52 +01003173 },
Anna Schumaker29ae7f92016-09-07 15:57:30 -04003174 [OP_COPY] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003175 .op_func = nfsd4_copy,
Olga Kornievskaia03b31f42018-12-04 14:09:21 -05003176 .op_flags = OP_MODIFIES_SOMETHING,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04003177 .op_name = "OP_COPY",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003178 .op_rsize_bop = nfsd4_copy_rsize,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04003179 },
Anna Schumaker528b8492020-09-28 13:08:58 -04003180 [OP_READ_PLUS] = {
3181 .op_func = nfsd4_read,
3182 .op_release = nfsd4_read_release,
3183 .op_name = "OP_READ_PLUS",
3184 .op_rsize_bop = nfsd4_read_plus_rsize,
3185 .op_get_currentstateid = nfsd4_get_readstateid,
3186 },
Anna Schumaker24bab492014-09-26 13:58:27 -04003187 [OP_SEEK] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02003188 .op_func = nfsd4_seek,
Anna Schumaker24bab492014-09-26 13:58:27 -04003189 .op_name = "OP_SEEK",
Christoph Hellwig1c122632017-05-08 20:42:10 +02003190 .op_rsize_bop = nfsd4_seek_rsize,
Anna Schumaker24bab492014-09-26 13:58:27 -04003191 },
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04003192 [OP_OFFLOAD_STATUS] = {
3193 .op_func = nfsd4_offload_status,
3194 .op_name = "OP_OFFLOAD_STATUS",
3195 .op_rsize_bop = nfsd4_offload_status_rsize,
3196 },
Olga Kornievskaia885e2bf2018-07-20 18:19:19 -04003197 [OP_OFFLOAD_CANCEL] = {
3198 .op_func = nfsd4_offload_cancel,
3199 .op_flags = OP_MODIFIES_SOMETHING,
3200 .op_name = "OP_OFFLOAD_CANCEL",
3201 .op_rsize_bop = nfsd4_only_status_rsize,
3202 },
Olga Kornievskaia51911862019-08-08 11:14:59 -04003203 [OP_COPY_NOTIFY] = {
3204 .op_func = nfsd4_copy_notify,
3205 .op_flags = OP_MODIFIES_SOMETHING,
3206 .op_name = "OP_COPY_NOTIFY",
3207 .op_rsize_bop = nfsd4_copy_notify_rsize,
3208 },
Frank van der Linden23e50fe2020-06-23 22:39:26 +00003209 [OP_GETXATTR] = {
3210 .op_func = nfsd4_getxattr,
3211 .op_name = "OP_GETXATTR",
3212 .op_rsize_bop = nfsd4_getxattr_rsize,
3213 },
3214 [OP_SETXATTR] = {
3215 .op_func = nfsd4_setxattr,
3216 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3217 .op_name = "OP_SETXATTR",
3218 .op_rsize_bop = nfsd4_setxattr_rsize,
3219 },
3220 [OP_LISTXATTRS] = {
3221 .op_func = nfsd4_listxattrs,
3222 .op_name = "OP_LISTXATTRS",
3223 .op_rsize_bop = nfsd4_listxattrs_rsize,
3224 },
3225 [OP_REMOVEXATTR] = {
3226 .op_func = nfsd4_removexattr,
3227 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3228 .op_name = "OP_REMOVEXATTR",
3229 .op_rsize_bop = nfsd4_removexattr_rsize,
3230 },
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003231};
3232
Andrew Elbleed941642016-06-15 12:52:09 -04003233/**
3234 * nfsd4_spo_must_allow - Determine if the compound op contains an
3235 * operation that is allowed to be sent with machine credentials
3236 *
3237 * @rqstp: a pointer to the struct svc_rqst
3238 *
3239 * Checks to see if the compound contains a spo_must_allow op
3240 * and confirms that it was sent with the proper machine creds.
3241 */
3242
3243bool nfsd4_spo_must_allow(struct svc_rqst *rqstp)
3244{
3245 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3246 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3247 struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
3248 struct nfsd4_compound_state *cstate = &resp->cstate;
3249 struct nfs4_op_map *allow = &cstate->clp->cl_spo_must_allow;
3250 u32 opiter;
3251
3252 if (!cstate->minorversion)
3253 return false;
3254
Zheng Bin44b49aa2020-09-11 12:10:14 +08003255 if (cstate->spo_must_allowed)
Andrew Elbleed941642016-06-15 12:52:09 -04003256 return true;
3257
3258 opiter = resp->opcnt;
3259 while (opiter < argp->opcnt) {
3260 this = &argp->ops[opiter++];
3261 if (test_bit(this->opnum, allow->u.longs) &&
3262 cstate->clp->cl_mach_cred &&
3263 nfsd4_mach_creds_match(cstate->clp, rqstp)) {
3264 cstate->spo_must_allowed = true;
3265 return true;
3266 }
3267 }
3268 cstate->spo_must_allowed = false;
3269 return false;
3270}
3271
J. Bruce Fields4f0cefb2014-03-11 15:39:13 -04003272int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
3273{
Olga Kornievskaia05b72782017-03-23 14:36:20 -04003274 if (op->opnum == OP_ILLEGAL || op->status == nfserr_notsupp)
J. Bruce Fields4f0cefb2014-03-11 15:39:13 -04003275 return op_encode_hdr_size * sizeof(__be32);
Kinglong Mee2282cd22017-02-03 22:36:00 +08003276
3277 BUG_ON(OPDESC(op)->op_rsize_bop == NULL);
3278 return OPDESC(op)->op_rsize_bop(rqstp, op);
J. Bruce Fields4f0cefb2014-03-11 15:39:13 -04003279}
3280
J. Bruce Fields07d1f802014-03-06 20:39:29 -05003281void warn_on_nonidempotent_op(struct nfsd4_op *op)
3282{
3283 if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
Chuck Lever3a237b42020-11-22 12:49:52 -05003284 pr_err("unable to encode reply to nonidempotent op %u (%s)\n",
J. Bruce Fields07d1f802014-03-06 20:39:29 -05003285 op->opnum, nfsd4_op_name(op->opnum));
3286 WARN_ON_ONCE(1);
3287 }
3288}
3289
Adrian Bunkf1c7f792008-08-08 19:26:42 +03003290static const char *nfsd4_op_name(unsigned opnum)
Benny Halevyb001a1b2008-07-02 11:15:03 +03003291{
3292 if (opnum < ARRAY_SIZE(nfsd4_ops))
3293 return nfsd4_ops[opnum].op_name;
3294 return "unknown_operation";
3295}
3296
Christoph Hellwig860bda22017-05-12 16:11:49 +02003297static const struct svc_procedure nfsd_procedures4[2] = {
Yu Zhiguo0a93a472009-05-19 14:09:54 +08003298 [NFSPROC4_NULL] = {
Christoph Hellwigf7235b62017-05-08 17:59:13 +02003299 .pc_func = nfsd4_proc_null,
Chuck Lever788f7182020-11-05 14:48:29 -05003300 .pc_decode = nfssvc_decode_voidarg,
3301 .pc_encode = nfssvc_encode_voidres,
3302 .pc_argsize = sizeof(struct nfsd_voidargs),
3303 .pc_ressize = sizeof(struct nfsd_voidres),
Yu Zhiguo0a93a472009-05-19 14:09:54 +08003304 .pc_cachetype = RC_NOCACHE,
3305 .pc_xdrressize = 1,
Chuck Lever2289e872020-09-17 17:22:49 -04003306 .pc_name = "NULL",
Yu Zhiguo0a93a472009-05-19 14:09:54 +08003307 },
3308 [NFSPROC4_COMPOUND] = {
Christoph Hellwigf7235b62017-05-08 17:59:13 +02003309 .pc_func = nfsd4_proc_compound,
Christoph Hellwig026fec72017-05-08 19:01:48 +02003310 .pc_decode = nfs4svc_decode_compoundargs,
Christoph Hellwig63f8de32017-05-08 19:42:02 +02003311 .pc_encode = nfs4svc_encode_compoundres,
Yu Zhiguo0a93a472009-05-19 14:09:54 +08003312 .pc_argsize = sizeof(struct nfsd4_compoundargs),
3313 .pc_ressize = sizeof(struct nfsd4_compoundres),
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003314 .pc_release = nfsd4_release_compoundargs,
Yu Zhiguo0a93a472009-05-19 14:09:54 +08003315 .pc_cachetype = RC_NOCACHE,
3316 .pc_xdrressize = NFSD_BUFSIZE/4,
Chuck Lever2289e872020-09-17 17:22:49 -04003317 .pc_name = "COMPOUND",
Yu Zhiguo0a93a472009-05-19 14:09:54 +08003318 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319};
3320
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02003321static unsigned int nfsd_count3[ARRAY_SIZE(nfsd_procedures4)];
Christoph Hellwige9679182017-05-12 16:21:37 +02003322const struct svc_version nfsd_version4 = {
Jeff Layton5283b032017-02-24 13:25:24 -05003323 .vs_vers = 4,
3324 .vs_nproc = 2,
3325 .vs_proc = nfsd_procedures4,
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02003326 .vs_count = nfsd_count3,
Jeff Layton5283b032017-02-24 13:25:24 -05003327 .vs_dispatch = nfsd_dispatch,
3328 .vs_xdrsize = NFS4_SVC_XDRSIZE,
3329 .vs_rpcb_optnl = true,
3330 .vs_need_cong_ctrl = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331};
3332
3333/*
3334 * Local variables:
3335 * c-basic-offset: 8
3336 * End:
3337 */