blob: 8cc9f6e91874c89bad3d926980ce980cc7e8543b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Mi Jinlong58e7b332011-08-28 18:18:56 +080042#include "idmap.h"
Boaz Harrosh9a74af22009-12-03 20:30:56 +020043#include "cache.h"
44#include "xdr4.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050045#include "vfs.h"
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +010046#include "current_stateid.h"
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +030047#include "netns.h"
Christoph Hellwig4ac72492013-12-20 05:16:55 -080048#include "acl.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020049#include "pnfs.h"
Christoph Hellwig31ef83d2014-08-16 19:02:22 -050050#include "trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
David Quigley18032ca2013-05-02 13:19:10 -040052#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
53#include <linux/security.h>
54
55static inline void
56nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
57{
David Howells2b0143b2015-03-17 22:25:59 +000058 struct inode *inode = d_inode(resfh->fh_dentry);
David Quigley18032ca2013-05-02 13:19:10 -040059 int status;
60
Al Viro59551022016-01-22 15:40:57 -050061 inode_lock(inode);
David Quigley18032ca2013-05-02 13:19:10 -040062 status = security_inode_setsecctx(resfh->fh_dentry,
63 label->data, label->len);
Al Viro59551022016-01-22 15:40:57 -050064 inode_unlock(inode);
David Quigley18032ca2013-05-02 13:19:10 -040065
66 if (status)
67 /*
68 * XXX: We should really fail the whole open, but we may
69 * already have created a new file, so it may be too
70 * late. For now this seems the least of evils:
71 */
72 bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
73
74 return;
75}
76#else
77static inline void
78nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
79{ }
80#endif
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#define NFSDDBG_FACILITY NFSDDBG_PROC
83
Yu Zhiguo3c8e0312009-05-16 16:22:31 +080084static u32 nfsd_attrmask[] = {
85 NFSD_WRITEABLE_ATTRS_WORD0,
86 NFSD_WRITEABLE_ATTRS_WORD1,
87 NFSD_WRITEABLE_ATTRS_WORD2
88};
89
90static u32 nfsd41_ex_attrmask[] = {
91 NFSD_SUPPATTR_EXCLCREAT_WORD0,
92 NFSD_SUPPATTR_EXCLCREAT_WORD1,
93 NFSD_SUPPATTR_EXCLCREAT_WORD2
94};
95
96static __be32
97check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
98 u32 *bmval, u32 *writable)
99{
100 struct dentry *dentry = cstate->current_fh.fh_dentry;
J. Bruce Fields32ddd942017-01-03 12:30:11 -0500101 struct svc_export *exp = cstate->current_fh.fh_export;
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800102
J. Bruce Fields916d2d82016-10-18 14:18:40 -0400103 if (!nfsd_attrs_supported(cstate->minorversion, bmval))
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800104 return nfserr_attrnotsupp;
J. Bruce Fields916d2d82016-10-18 14:18:40 -0400105 if ((bmval[0] & FATTR4_WORD0_ACL) && !IS_POSIXACL(d_inode(dentry)))
106 return nfserr_attrnotsupp;
J. Bruce Fields32ddd942017-01-03 12:30:11 -0500107 if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) &&
108 !(exp->ex_flags & NFSEXP_SECURITY_LABEL))
109 return nfserr_attrnotsupp;
J. Bruce Fields916d2d82016-10-18 14:18:40 -0400110 if (writable && !bmval_is_subset(bmval, writable))
111 return nfserr_inval;
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100112 if (writable && (bmval[2] & FATTR4_WORD2_MODE_UMASK) &&
113 (bmval[1] & FATTR4_WORD1_MODE))
114 return nfserr_inval;
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800115 return nfs_ok;
116}
117
118static __be32
119nfsd4_check_open_attributes(struct svc_rqst *rqstp,
120 struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
121{
122 __be32 status = nfs_ok;
123
124 if (open->op_create == NFS4_OPEN_CREATE) {
125 if (open->op_createmode == NFS4_CREATE_UNCHECKED
126 || open->op_createmode == NFS4_CREATE_GUARDED)
127 status = check_attr_support(rqstp, cstate,
128 open->op_bmval, nfsd_attrmask);
129 else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1)
130 status = check_attr_support(rqstp, cstate,
131 open->op_bmval, nfsd41_ex_attrmask);
132 }
133
134 return status;
135}
136
Yu Zhiguo9208faf2009-07-06 17:24:16 +0800137static int
138is_create_with_attrs(struct nfsd4_open *open)
139{
140 return open->op_create == NFS4_OPEN_CREATE
141 && (open->op_createmode == NFS4_CREATE_UNCHECKED
142 || open->op_createmode == NFS4_CREATE_GUARDED
143 || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1);
144}
145
146/*
147 * if error occurs when setting the acl, just clear the acl bit
148 * in the returned attr bitmap.
149 */
150static void
151do_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
152 struct nfs4_acl *acl, u32 *bmval)
153{
154 __be32 status;
155
156 status = nfsd4_set_nfs4_acl(rqstp, fhp, acl);
157 if (status)
158 /*
159 * We should probably fail the whole open at this point,
160 * but we've already created the file, so it's too late;
161 * So this seems the least of evils:
162 */
163 bmval[0] &= ~FATTR4_WORD0_ACL;
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static inline void
167fh_dup2(struct svc_fh *dst, struct svc_fh *src)
168{
169 fh_put(dst);
170 dget(src->fh_dentry);
171 if (src->fh_export)
Kinglong Meebf18f162014-06-10 22:06:44 +0800172 exp_get(src->fh_export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 *dst = *src;
174}
175
Al Virob37ad282006-10-19 23:28:59 -0700176static __be32
J. Bruce Fieldsdc730e12006-10-17 00:10:13 -0700177do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Al Virob37ad282006-10-19 23:28:59 -0700179 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 if (open->op_truncate &&
182 !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
183 return nfserr_inval;
184
J. Bruce Fieldsa0432262011-08-25 10:48:39 -0400185 accmode |= NFSD_MAY_READ_IF_EXEC;
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200188 accmode |= NFSD_MAY_READ;
J. Bruce Fields9801d8a2006-10-17 00:10:14 -0700189 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200190 accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC);
J. Bruce Fields57ecb342009-12-01 19:42:57 -0500191 if (open->op_share_deny & NFS4_SHARE_DENY_READ)
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200192 accmode |= NFSD_MAY_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
195
196 return status;
197}
198
J. Bruce Fieldsaadab6c2011-08-15 16:55:02 -0400199static __be32 nfsd_check_obj_isreg(struct svc_fh *fh)
200{
David Howells2b0143b2015-03-17 22:25:59 +0000201 umode_t mode = d_inode(fh->fh_dentry)->i_mode;
J. Bruce Fieldsaadab6c2011-08-15 16:55:02 -0400202
203 if (S_ISREG(mode))
204 return nfs_ok;
205 if (S_ISDIR(mode))
206 return nfserr_isdir;
207 /*
208 * Using err_symlink as our catch-all case may look odd; but
209 * there's no other obvious error for this case in 4.0, and we
210 * happen to know that it will cause the linux v4 client to do
211 * the right thing on attempts to open something other than a
212 * regular file.
213 */
214 return nfserr_symlink;
215}
216
J. Bruce Fieldsbbc9c362013-03-22 18:03:49 -0400217static 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 -0700218{
J. Bruce Fieldsbbc9c362013-03-22 18:03:49 -0400219 if (nfsd4_has_session(cstate))
220 return;
221 fh_copy_shallow(&open->op_openowner->oo_owner.so_replay.rp_openfh,
222 &resfh->fh_handle);
223}
224
225static __be32
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500226do_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 -0400227{
228 struct svc_fh *current_fh = &cstate->current_fh;
Neil Brown7007c902012-12-07 15:40:55 -0500229 int accmode;
Al Virob37ad282006-10-19 23:28:59 -0700230 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500232 *resfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
233 if (!*resfh)
J. Bruce Fields59deeb92012-01-27 16:49:55 -0500234 return nfserr_jukebox;
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500235 fh_init(*resfh, NFS4_FHSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 open->op_truncate = 0;
237
238 if (open->op_create) {
Benny Halevy79fb54a2009-04-03 08:29:17 +0300239 /* FIXME: check session persistence and pnfs flags.
240 * The nfsv4.1 spec requires the following semantics:
241 *
242 * Persistent | pNFS | Server REQUIRED | Client Allowed
243 * Reply Cache | server | |
244 * -------------+--------+-----------------+--------------------
245 * no | no | EXCLUSIVE4_1 | EXCLUSIVE4_1
246 * | | | (SHOULD)
247 * | | and EXCLUSIVE4 | or EXCLUSIVE4
248 * | | | (SHOULD NOT)
249 * no | yes | EXCLUSIVE4_1 | EXCLUSIVE4_1
250 * yes | no | GUARDED4 | GUARDED4
251 * yes | yes | GUARDED4 | GUARDED4
252 */
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /*
255 * Note: create modes (UNCHECKED,GUARDED...) are the same
Mi Jinlongac6721a2011-04-20 17:06:25 +0800256 * in NFSv4 as in v3 except EXCLUSIVE4_1.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400258 current->fs->umask = open->op_umask;
Mi Jinlongac6721a2011-04-20 17:06:25 +0800259 status = do_nfsd_create(rqstp, current_fh, open->op_fname.data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 open->op_fname.len, &open->op_iattr,
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500261 *resfh, open->op_createmode,
Jeff Layton749997e2007-07-31 00:37:51 -0700262 (u32 *)open->op_verf.data,
J. Bruce Fields856121b2011-10-13 11:37:11 -0400263 &open->op_truncate, &open->op_created);
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400264 current->fs->umask = 0;
Jeff Layton749997e2007-07-31 00:37:51 -0700265
David Quigley18032ca2013-05-02 13:19:10 -0400266 if (!status && open->op_label.len)
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500267 nfsd4_security_inode_setsecctx(*resfh, &open->op_label, open->op_bmval);
David Quigley18032ca2013-05-02 13:19:10 -0400268
J. Bruce Fields99f88722009-02-02 15:12:27 -0500269 /*
Kinglong Meeead8fb82015-07-30 21:55:02 +0800270 * Following rfc 3530 14.2.16, and rfc 5661 18.16.4
271 * use the returned bitmask to indicate which attributes
272 * we used to store the verifier:
Jeff Layton749997e2007-07-31 00:37:51 -0700273 */
Kinglong Meeead8fb82015-07-30 21:55:02 +0800274 if (nfsd_create_is_exclusive(open->op_createmode) && status == 0)
275 open->op_bmval[1] |= (FATTR4_WORD1_TIME_ACCESS |
276 FATTR4_WORD1_TIME_MODIFY);
J. Bruce Fields43357232014-01-24 18:04:40 -0500277 } else
278 /*
279 * Note this may exit with the parent still locked.
280 * We will hold the lock until nfsd4_open's final
281 * lookup, to prevent renames or unlinks until we've had
282 * a chance to an acquire a delegation if appropriate.
283 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 status = nfsd_lookup(rqstp, current_fh,
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500285 open->op_fname.data, open->op_fname.len, *resfh);
J. Bruce Fieldsaf858522006-11-08 17:44:39 -0800286 if (status)
287 goto out;
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500288 status = nfsd_check_obj_isreg(*resfh);
J. Bruce Fields9dc4e6c2012-04-09 18:06:49 -0400289 if (status)
290 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Yu Zhiguo9208faf2009-07-06 17:24:16 +0800292 if (is_create_with_attrs(open) && open->op_acl != NULL)
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500293 do_set_nfs4_acl(rqstp, *resfh, open->op_acl, open->op_bmval);
Yu Zhiguo9208faf2009-07-06 17:24:16 +0800294
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500295 nfsd4_set_open_owner_reply_cache(cstate, open, *resfh);
Neil Brown7007c902012-12-07 15:40:55 -0500296 accmode = NFSD_MAY_NOP;
J. Bruce Fields89f6c332013-06-19 15:47:37 -0400297 if (open->op_created ||
298 open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
Neil Brown7007c902012-12-07 15:40:55 -0500299 accmode |= NFSD_MAY_OWNER_OVERRIDE;
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500300 status = do_open_permission(rqstp, *resfh, open, accmode);
J. Bruce Fields41fd1e42012-01-27 16:26:02 -0500301 set_change_info(&open->op_cinfo, current_fh);
J. Bruce Fieldsaf858522006-11-08 17:44:39 -0800302out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return status;
304}
305
Al Virob37ad282006-10-19 23:28:59 -0700306static __be32
J. Bruce Fieldsbbc9c362013-03-22 18:03:49 -0400307do_open_fhandle(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
J. Bruce Fieldsbbc9c362013-03-22 18:03:49 -0400309 struct svc_fh *current_fh = &cstate->current_fh;
Al Virob37ad282006-10-19 23:28:59 -0700310 __be32 status;
J. Bruce Fields9f415eb2013-05-03 16:09:09 -0400311 int accmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* We don't know the target directory, and therefore can not
314 * set the change info
315 */
316
317 memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
318
J. Bruce Fieldsbbc9c362013-03-22 18:03:49 -0400319 nfsd4_set_open_owner_reply_cache(cstate, open, current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
322 (open->op_iattr.ia_size == 0);
J. Bruce Fields9f415eb2013-05-03 16:09:09 -0400323 /*
324 * In the delegation case, the client is telling us about an
325 * open that it *already* performed locally, some time ago. We
326 * should let it succeed now if possible.
327 *
328 * In the case of a CLAIM_FH open, on the other hand, the client
329 * may be counting on us to enforce permissions (the Linux 4.1
330 * client uses this for normal opens, for example).
331 */
332 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH)
333 accmode = NFSD_MAY_OWNER_OVERRIDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
J. Bruce Fields9f415eb2013-05-03 16:09:09 -0400335 status = do_open_permission(rqstp, current_fh, open, accmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 return status;
338}
339
Andy Adamson60adfc52009-04-03 08:28:50 +0300340static void
341copy_clientid(clientid_t *clid, struct nfsd4_session *session)
342{
343 struct nfsd4_sessionid *sid =
344 (struct nfsd4_sessionid *)session->se_sessionid.data;
345
346 clid->cl_boot = sid->clientid.cl_boot;
347 clid->cl_id = sid->clientid.cl_id;
348}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
J.Bruce Fields71911552006-12-13 00:35:29 -0800350static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800351nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200352 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200354 struct nfsd4_open *open = &u->open;
Al Virob37ad282006-10-19 23:28:59 -0700355 __be32 status;
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500356 struct svc_fh *resfh = NULL;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +0300357 struct net *net = SVC_NET(rqstp);
358 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields03f318c2018-06-08 12:28:47 -0400359 bool reclaim = false;
Andy Adamson66689582009-04-03 08:28:45 +0300360
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400361 dprintk("NFSD: nfsd4_open filename %.*s op_openowner %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 (int)open->op_fname.len, open->op_fname.data,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400363 open->op_openowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /* This check required by spec. */
366 if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
367 return nfserr_inval;
368
J. Bruce Fields856121b2011-10-13 11:37:11 -0400369 open->op_created = 0;
Mi Jinlongab1350b2011-07-14 15:06:26 +0800370 /*
371 * RFC5661 18.51.3
372 * Before RECLAIM_COMPLETE done, server should deny new lock
373 */
374 if (nfsd4_has_session(cstate) &&
Jeff Laytona52d7262012-03-21 09:52:02 -0400375 !test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
376 &cstate->session->se_client->cl_flags) &&
Mi Jinlongab1350b2011-07-14 15:06:26 +0800377 open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
378 return nfserr_grace;
379
Andy Adamson60adfc52009-04-03 08:28:50 +0300380 if (nfsd4_has_session(cstate))
381 copy_clientid(&open->op_clientid, cstate->session);
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* check seqid for replay. set nfs4_owner */
Kinglong Mee6cd22662015-07-13 17:32:05 +0800384 status = nfsd4_process_open1(cstate, open, nn);
Al Viroa90b0612006-10-19 23:29:03 -0700385 if (status == nfserr_replay_me) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400386 struct nfs4_replay *rp = &open->op_openowner->oo_owner.so_replay;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800387 fh_put(&cstate->current_fh);
J. Bruce Fieldsa4773c02009-02-02 17:23:10 -0500388 fh_copy_shallow(&cstate->current_fh.fh_handle,
389 &rp->rp_openfh);
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200390 status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (status)
392 dprintk("nfsd4_open: replay failed"
393 " restoring previous filehandle\n");
394 else
Al Viroa90b0612006-10-19 23:29:03 -0700395 status = nfserr_replay_me;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397 if (status)
398 goto out;
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800399 if (open->op_xdr_error) {
400 status = open->op_xdr_error;
401 goto out;
402 }
J. Bruce Fieldsfb553c02006-01-18 17:43:36 -0800403
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800404 status = nfsd4_check_open_attributes(rqstp, cstate, open);
405 if (status)
406 goto out;
407
J. Bruce Fieldsfb553c02006-01-18 17:43:36 -0800408 /* Openowner is now set, so sequence id will get bumped. Now we need
409 * these checks before we do any creates: */
J. Bruce Fieldscbd0d512006-02-07 12:58:32 -0800410 status = nfserr_grace;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -0400411 if (opens_in_grace(net) && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fieldscbd0d512006-02-07 12:58:32 -0800412 goto out;
413 status = nfserr_no_grace;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -0400414 if (!opens_in_grace(net) && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fieldscbd0d512006-02-07 12:58:32 -0800415 goto out;
J. Bruce Fieldsfb553c02006-01-18 17:43:36 -0800416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 switch (open->op_claim_type) {
NeilBrown0dd3c192005-06-23 22:02:56 -0700418 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 case NFS4_OPEN_CLAIM_NULL:
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500420 status = do_open_lookup(rqstp, cstate, open, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (status)
422 goto out;
423 break;
424 case NFS4_OPEN_CLAIM_PREVIOUS:
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +0300425 status = nfs4_check_open_reclaim(&open->op_clientid,
Trond Myklebust0fe492d2014-06-30 11:48:47 -0400426 cstate, nn);
Mi Jinlong0cf99b92011-11-23 10:48:40 +0800427 if (status)
428 goto out;
Jeff Laytonba5378b2014-05-30 09:09:28 -0400429 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fields03f318c2018-06-08 12:28:47 -0400430 reclaim = true;
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -0600431 /* fall through */
J. Bruce Fields8b289b22011-10-19 11:52:12 -0400432 case NFS4_OPEN_CLAIM_FH:
433 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
J. Bruce Fieldsbbc9c362013-03-22 18:03:49 -0400434 status = do_open_fhandle(rqstp, cstate, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (status)
436 goto out;
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500437 resfh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 break;
J. Bruce Fields8b289b22011-10-19 11:52:12 -0400439 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
J. Bruce Fields2fdada02007-07-27 16:10:37 -0400441 dprintk("NFSD: unsupported OPEN claim type %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 open->op_claim_type);
443 status = nfserr_notsupp;
444 goto out;
445 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -0400446 dprintk("NFSD: Invalid OPEN claim type %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 open->op_claim_type);
448 status = nfserr_inval;
449 goto out;
450 }
451 /*
452 * nfsd4_process_open2() does the actual opening of the file. If
453 * successful, it (1) truncates the file if open->op_truncate was
454 * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
455 */
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500456 status = nfsd4_process_open2(rqstp, resfh, open);
Jeff Laytonb3fbfe02014-07-29 21:37:44 -0400457 WARN(status && open->op_created,
458 "nfsd4_process_open2 failed to open newly-created file! status=%u\n",
459 be32_to_cpu(status));
J. Bruce Fields03f318c2018-06-08 12:28:47 -0400460 if (reclaim && !status)
461 nn->somebody_reclaimed = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462out:
J. Bruce Fieldsc0e6bee2012-01-27 17:26:06 -0500463 if (resfh && resfh != &cstate->current_fh) {
464 fh_dup2(&cstate->current_fh, resfh);
465 fh_put(resfh);
466 kfree(resfh);
467 }
Jeff Layton42297892015-03-23 10:53:44 -0400468 nfsd4_cleanup_open_state(cstate, open);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -0400469 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return status;
471}
472
473/*
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800474 * OPEN is the only seqid-mutating operation whose decoding can fail
475 * with a seqid-mutating error (specifically, decoding of user names in
476 * the attributes). Therefore we have to do some processing to look up
477 * the stateowner so that we can bump the seqid.
478 */
479static __be32 nfsd4_open_omfg(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_op *op)
480{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200481 struct nfsd4_open *open = &op->u.open;
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800482
483 if (!seqid_mutating_err(ntohl(op->status)))
484 return op->status;
485 if (nfsd4_has_session(cstate))
486 return op->status;
487 open->op_xdr_error = op->status;
Christoph Hellwigeb698532017-05-08 20:58:35 +0200488 return nfsd4_open(rqstp, cstate, &op->u);
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800489}
490
491/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * filehandle-manipulating ops.
493 */
J.Bruce Fields71911552006-12-13 00:35:29 -0800494static __be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800495nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200496 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200498 u->getfh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return nfs_ok;
500}
501
J.Bruce Fields71911552006-12-13 00:35:29 -0800502static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800503nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200504 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200506 struct nfsd4_putfh *putfh = &u->putfh;
507
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800508 fh_put(&cstate->current_fh);
509 cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
510 memcpy(&cstate->current_fh.fh_handle.fh_base, putfh->pf_fhval,
511 putfh->pf_fhlen);
J. Bruce Fields68d93182011-04-08 17:00:50 -0400512 return fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
J.Bruce Fields71911552006-12-13 00:35:29 -0800515static __be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800516nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200517 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Al Virob37ad282006-10-19 23:28:59 -0700519 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800521 fh_put(&cstate->current_fh);
J. Bruce Fieldsdf547ef2007-07-17 04:04:43 -0700522 status = exp_pseudoroot(rqstp, &cstate->current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return status;
524}
525
J.Bruce Fields71911552006-12-13 00:35:29 -0800526static __be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800527nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200528 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800530 if (!cstate->save_fh.fh_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return nfserr_restorefh;
532
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800533 fh_dup2(&cstate->current_fh, &cstate->save_fh);
Olga Kornievskaia51100d22018-09-13 13:58:24 -0400534 if (HAS_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG)) {
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +0100535 memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
Olga Kornievskaia51100d22018-09-13 13:58:24 -0400536 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +0100537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return nfs_ok;
539}
540
J.Bruce Fields71911552006-12-13 00:35:29 -0800541static __be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -0800542nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200543 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800545 fh_dup2(&cstate->save_fh, &cstate->current_fh);
Olga Kornievskaia51100d22018-09-13 13:58:24 -0400546 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG)) {
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +0100547 memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t));
Olga Kornievskaia51100d22018-09-13 13:58:24 -0400548 SET_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG);
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +0100549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return nfs_ok;
551}
552
553/*
554 * misc nfsv4 ops
555 */
J.Bruce Fields71911552006-12-13 00:35:29 -0800556static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800557nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200558 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200560 struct nfsd4_access *access = &u->access;
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (access->ac_req_access & ~NFS3_ACCESS_FULL)
563 return nfserr_inval;
564
565 access->ac_resp_access = access->ac_req_access;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800566 return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
567 &access->ac_supported);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +0300570static void gen_boot_verifier(nfs4_verifier *verifier, struct net *net)
Chuck Leverab4684d2012-03-02 17:13:50 -0500571{
Trond Myklebust27c438f2019-09-02 13:02:56 -0400572 __be32 *verf = (__be32 *)verifier->data;
Chuck Leverab4684d2012-03-02 17:13:50 -0500573
Trond Myklebust27c438f2019-09-02 13:02:56 -0400574 BUILD_BUG_ON(2*sizeof(*verf) != sizeof(verifier->data));
575
576 nfsd_copy_boot_verifier(verf, net_generic(net, nfsd_net_id));
Chuck Leverab4684d2012-03-02 17:13:50 -0500577}
578
J.Bruce Fields71911552006-12-13 00:35:29 -0800579static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800580nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200581 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200583 struct nfsd4_commit *commit = &u->commit;
584
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +0300585 gen_boot_verifier(&commit->co_verf, SVC_NET(rqstp));
J. Bruce Fields75c096f2011-08-15 18:39:32 -0400586 return nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800587 commit->co_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Al Virob37ad282006-10-19 23:28:59 -0700590static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800591nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200592 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200594 struct nfsd4_create *create = &u->create;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 struct svc_fh resfh;
Al Virob37ad282006-10-19 23:28:59 -0700596 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 dev_t rdev;
598
599 fh_init(&resfh, NFS4_FHSIZE);
600
J. Bruce Fieldsfa081392016-07-21 16:00:12 -0400601 status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (status)
603 return status;
604
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800605 status = check_attr_support(rqstp, cstate, create->cr_bmval,
606 nfsd_attrmask);
607 if (status)
608 return status;
609
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400610 current->fs->umask = create->cr_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 switch (create->cr_type) {
612 case NF4LNK:
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800613 status = nfsd_symlink(rqstp, &cstate->current_fh,
614 create->cr_name, create->cr_namelen,
Kinglong Mee1e444f52014-07-01 17:48:02 +0800615 create->cr_data, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 break;
617
618 case NF4BLK:
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400619 status = nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
621 if (MAJOR(rdev) != create->cr_specdata1 ||
622 MINOR(rdev) != create->cr_specdata2)
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400623 goto out_umask;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800624 status = nfsd_create(rqstp, &cstate->current_fh,
625 create->cr_name, create->cr_namelen,
626 &create->cr_iattr, S_IFBLK, rdev, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 break;
628
629 case NF4CHR:
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400630 status = nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
632 if (MAJOR(rdev) != create->cr_specdata1 ||
633 MINOR(rdev) != create->cr_specdata2)
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400634 goto out_umask;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800635 status = nfsd_create(rqstp, &cstate->current_fh,
636 create->cr_name, create->cr_namelen,
637 &create->cr_iattr,S_IFCHR, rdev, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 break;
639
640 case NF4SOCK:
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800641 status = nfsd_create(rqstp, &cstate->current_fh,
642 create->cr_name, create->cr_namelen,
643 &create->cr_iattr, S_IFSOCK, 0, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 break;
645
646 case NF4FIFO:
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800647 status = nfsd_create(rqstp, &cstate->current_fh,
648 create->cr_name, create->cr_namelen,
649 &create->cr_iattr, S_IFIFO, 0, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 break;
651
652 case NF4DIR:
653 create->cr_iattr.ia_valid &= ~ATTR_SIZE;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800654 status = nfsd_create(rqstp, &cstate->current_fh,
655 create->cr_name, create->cr_namelen,
656 &create->cr_iattr, S_IFDIR, 0, &resfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 break;
658
659 default:
660 status = nfserr_badtype;
661 }
662
Yu Zhiguo9208faf2009-07-06 17:24:16 +0800663 if (status)
664 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
David Quigley18032ca2013-05-02 13:19:10 -0400666 if (create->cr_label.len)
667 nfsd4_security_inode_setsecctx(&resfh, &create->cr_label, create->cr_bmval);
668
Yu Zhiguo9208faf2009-07-06 17:24:16 +0800669 if (create->cr_acl != NULL)
670 do_set_nfs4_acl(rqstp, &resfh, create->cr_acl,
671 create->cr_bmval);
672
673 fh_unlock(&cstate->current_fh);
674 set_change_info(&create->cr_cinfo, &cstate->current_fh);
675 fh_dup2(&cstate->current_fh, &resfh);
676out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 fh_put(&resfh);
J. Bruce Fields880a3a52018-03-21 17:19:02 -0400678out_umask:
679 current->fs->umask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return status;
681}
682
J.Bruce Fields71911552006-12-13 00:35:29 -0800683static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800684nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200685 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200687 struct nfsd4_getattr *getattr = &u->getattr;
Al Virob37ad282006-10-19 23:28:59 -0700688 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200690 status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (status)
692 return status;
693
694 if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
695 return nfserr_inval;
696
J. Bruce Fields916d2d82016-10-18 14:18:40 -0400697 getattr->ga_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
698 getattr->ga_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
699 getattr->ga_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800701 getattr->ga_fhp = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return nfs_ok;
703}
704
J.Bruce Fields71911552006-12-13 00:35:29 -0800705static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800706nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200707 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200709 struct nfsd4_link *link = &u->link;
J. Bruce Fields95424462017-09-15 16:02:52 -0400710 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800712 status = nfsd_link(rqstp, &cstate->current_fh,
713 link->li_name, link->li_namelen, &cstate->save_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (!status)
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800715 set_change_info(&link->li_cinfo, &cstate->current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return status;
717}
718
J. Bruce Fields0ff7ab42010-12-16 10:06:27 -0500719static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 struct svc_fh tmp_fh;
Al Virob37ad282006-10-19 23:28:59 -0700722 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 fh_init(&tmp_fh, NFS4_FHSIZE);
J. Bruce Fieldsdf547ef2007-07-17 04:04:43 -0700725 ret = exp_pseudoroot(rqstp, &tmp_fh);
726 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return ret;
J. Bruce Fields0ff7ab42010-12-16 10:06:27 -0500728 if (tmp_fh.fh_dentry == fh->fh_dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 fh_put(&tmp_fh);
730 return nfserr_noent;
731 }
732 fh_put(&tmp_fh);
J. Bruce Fields0ff7ab42010-12-16 10:06:27 -0500733 return nfsd_lookup(rqstp, fh, "..", 2, fh);
734}
735
736static __be32
737nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200738 union nfsd4_op_u *u)
J. Bruce Fields0ff7ab42010-12-16 10:06:27 -0500739{
740 return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
J.Bruce Fields71911552006-12-13 00:35:29 -0800743static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800744nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200745 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800747 return nfsd_lookup(rqstp, &cstate->current_fh,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200748 u->lookup.lo_name, u->lookup.lo_len,
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800749 &cstate->current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
J.Bruce Fields71911552006-12-13 00:35:29 -0800752static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800753nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200754 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200756 struct nfsd4_read *read = &u->read;
Al Virob37ad282006-10-19 23:28:59 -0700757 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Jeff Layton5c4583b2019-08-18 14:18:54 -0400759 read->rd_nf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (read->rd_offset >= OFFSET_MAX)
761 return nfserr_inval;
762
Chuck Lever87c59422018-03-28 13:29:11 -0400763 trace_nfsd_read_start(rqstp, &cstate->current_fh,
764 read->rd_offset, read->rd_length);
765
J. Bruce Fields9b3234b2012-12-04 18:03:46 -0500766 /*
767 * If we do a zero copy read, then a client will see read data
768 * that reflects the state of the file *after* performing the
769 * following compound.
770 *
771 * To ensure proper ordering, we therefore turn off zero copy if
772 * the client wants us to do more in this compound:
773 */
774 if (!nfsd4_last_compound_op(rqstp))
Jeff Layton779fb0f2014-11-19 07:51:18 -0500775 clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
J. Bruce Fields9b3234b2012-12-04 18:03:46 -0500776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 /* check stateid */
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +0100778 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
779 &read->rd_stateid, RD_STATE,
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400780 &read->rd_nf, NULL);
Christoph Hellwigaf90f702015-06-18 16:45:00 +0200781 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
783 goto out;
784 }
785 status = nfs_ok;
786out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 read->rd_rqstp = rqstp;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800788 read->rd_fhp = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return status;
790}
791
J. Bruce Fields34b1744c2017-05-05 17:09:37 -0400792
793static void
794nfsd4_read_release(union nfsd4_op_u *u)
795{
Jeff Layton5c4583b2019-08-18 14:18:54 -0400796 if (u->read.rd_nf)
797 nfsd_file_put(u->read.rd_nf);
Chuck Lever87c59422018-03-28 13:29:11 -0400798 trace_nfsd_read_done(u->read.rd_rqstp, u->read.rd_fhp,
799 u->read.rd_offset, u->read.rd_length);
J. Bruce Fields34b1744c2017-05-05 17:09:37 -0400800}
801
J.Bruce Fields71911552006-12-13 00:35:29 -0800802static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800803nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200804 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200806 struct nfsd4_readdir *readdir = &u->readdir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 u64 cookie = readdir->rd_cookie;
808 static const nfs4_verifier zeroverf;
809
810 /* no need to check permission - this will be done in nfsd_readdir() */
811
812 if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
813 return nfserr_inval;
814
J. Bruce Fields916d2d82016-10-18 14:18:40 -0400815 readdir->rd_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
816 readdir->rd_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
817 readdir->rd_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Bernd Schubert832023b2011-08-08 17:38:08 +0200819 if ((cookie == 1) || (cookie == 2) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
821 return nfserr_bad_cookie;
822
823 readdir->rd_rqstp = rqstp;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800824 readdir->rd_fhp = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return nfs_ok;
826}
827
J.Bruce Fields71911552006-12-13 00:35:29 -0800828static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800829nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200830 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200832 u->readlink.rl_rqstp = rqstp;
833 u->readlink.rl_fhp = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return nfs_ok;
835}
836
J.Bruce Fields71911552006-12-13 00:35:29 -0800837static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800838nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200839 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200841 struct nfsd4_remove *remove = &u->remove;
Al Virob37ad282006-10-19 23:28:59 -0700842 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -0400844 if (opens_in_grace(SVC_NET(rqstp)))
NeilBrownc815afc2005-06-23 22:03:00 -0700845 return nfserr_grace;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800846 status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
847 remove->rm_name, remove->rm_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (!status) {
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800849 fh_unlock(&cstate->current_fh);
850 set_change_info(&remove->rm_cinfo, &cstate->current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852 return status;
853}
854
J.Bruce Fields71911552006-12-13 00:35:29 -0800855static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800856nfsd4_rename(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_rename *rename = &u->rename;
J. Bruce Fields95424462017-09-15 16:02:52 -0400860 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
J. Bruce Fieldsf8f71d02018-11-06 11:47:20 -0500862 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_rename(rqstp, &cstate->save_fh, rename->rn_sname,
865 rename->rn_snamelen, &cstate->current_fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 rename->rn_tname, rename->rn_tnamelen);
J. Bruce Fields2a6cf942013-04-30 15:28:51 -0400867 if (status)
868 return status;
869 set_change_info(&rename->rn_sinfo, &cstate->current_fh);
870 set_change_info(&rename->rn_tinfo, &cstate->save_fh);
871 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
J.Bruce Fields71911552006-12-13 00:35:29 -0800874static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -0700875nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200876 union nfsd4_op_u *u)
Andy Adamsondcb488a32007-07-17 04:04:51 -0700877{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200878 struct nfsd4_secinfo *secinfo = &u->secinfo;
Andy Adamsondcb488a32007-07-17 04:04:51 -0700879 struct svc_export *exp;
880 struct dentry *dentry;
881 __be32 err;
882
J. Bruce Fields29a78a32011-04-09 11:28:53 -0400883 err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
884 if (err)
885 return err;
Andy Adamsondcb488a32007-07-17 04:04:51 -0700886 err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
887 secinfo->si_name, secinfo->si_namelen,
888 &exp, &dentry);
889 if (err)
890 return err;
J. Bruce Fields2f6fc052016-03-02 16:36:21 -0800891 fh_unlock(&cstate->current_fh);
David Howells2b0143b2015-03-17 22:25:59 +0000892 if (d_really_is_negative(dentry)) {
Andy Adamsondcb488a32007-07-17 04:04:51 -0700893 exp_put(exp);
894 err = nfserr_noent;
895 } else
896 secinfo->si_exp = exp;
897 dput(dentry);
J. Bruce Fields56560b92010-12-16 09:57:15 -0500898 if (cstate->minorversion)
899 /* See rfc 5661 section 2.6.3.1.1.8 */
900 fh_put(&cstate->current_fh);
Andy Adamsondcb488a32007-07-17 04:04:51 -0700901 return err;
902}
903
904static __be32
J. Bruce Fields04f4ad12010-12-16 09:51:13 -0500905nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200906 union nfsd4_op_u *u)
J. Bruce Fields04f4ad12010-12-16 09:51:13 -0500907{
908 __be32 err;
909
Christoph Hellwigeb698532017-05-08 20:58:35 +0200910 switch (u->secinfo_no_name.sin_style) {
J. Bruce Fields04f4ad12010-12-16 09:51:13 -0500911 case NFS4_SECINFO_STYLE4_CURRENT_FH:
912 break;
913 case NFS4_SECINFO_STYLE4_PARENT:
914 err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
915 if (err)
916 return err;
917 break;
918 default:
919 return nfserr_inval;
920 }
Kinglong Meebf18f162014-06-10 22:06:44 +0800921
Christoph Hellwigeb698532017-05-08 20:58:35 +0200922 u->secinfo_no_name.sin_exp = exp_get(cstate->current_fh.fh_export);
J. Bruce Fields04f4ad12010-12-16 09:51:13 -0500923 fh_put(&cstate->current_fh);
924 return nfs_ok;
925}
926
J. Bruce Fields34b1744c2017-05-05 17:09:37 -0400927static void
928nfsd4_secinfo_release(union nfsd4_op_u *u)
929{
930 if (u->secinfo.si_exp)
931 exp_put(u->secinfo.si_exp);
932}
933
Eryu Guanec572b92017-09-29 15:01:10 +0800934static void
935nfsd4_secinfo_no_name_release(union nfsd4_op_u *u)
936{
937 if (u->secinfo_no_name.sin_exp)
938 exp_put(u->secinfo_no_name.sin_exp);
939}
940
J. Bruce Fields04f4ad12010-12-16 09:51:13 -0500941static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800942nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200943 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200945 struct nfsd4_setattr *setattr = &u->setattr;
Al Virob37ad282006-10-19 23:28:59 -0700946 __be32 status = nfs_ok;
Al Viro96f6f982012-04-12 23:47:00 -0400947 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
Christoph Hellwigaf90f702015-06-18 16:45:00 +0200950 status = nfs4_preprocess_stateid_op(rqstp, cstate,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +0100951 &cstate->current_fh, &setattr->sa_stateid,
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400952 WR_STATE, NULL, NULL);
J. Bruce Fields375c5542006-01-18 17:43:33 -0800953 if (status) {
Greg Banks3e3b4802006-10-02 02:17:41 -0700954 dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
J. Bruce Fields375c5542006-01-18 17:43:33 -0800955 return status;
956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
Al Viro96f6f982012-04-12 23:47:00 -0400958 err = fh_want_write(&cstate->current_fh);
959 if (err)
960 return nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 status = nfs_ok;
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800962
963 status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
964 nfsd_attrmask);
965 if (status)
966 goto out;
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (setattr->sa_acl != NULL)
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800969 status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
970 setattr->sa_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (status)
Dave Hansen18f335a2008-02-15 14:37:38 -0800972 goto out;
David Quigley18032ca2013-05-02 13:19:10 -0400973 if (setattr->sa_label.len)
974 status = nfsd4_set_nfs4_label(rqstp, &cstate->current_fh,
975 &setattr->sa_label);
976 if (status)
977 goto out;
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800978 status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 0, (time_t)0);
Dave Hansen18f335a2008-02-15 14:37:38 -0800980out:
Al Virobad0dcf2011-11-23 12:03:18 -0500981 fh_drop_write(&cstate->current_fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return status;
983}
984
J.Bruce Fields71911552006-12-13 00:35:29 -0800985static __be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -0800986nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +0200987 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Christoph Hellwigeb698532017-05-08 20:58:35 +0200989 struct nfsd4_write *write = &u->write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 stateid_t *stateid = &write->wr_stateid;
Jeff Layton5c4583b2019-08-18 14:18:54 -0400991 struct nfsd_file *nf = NULL;
Al Virob37ad282006-10-19 23:28:59 -0700992 __be32 status = nfs_ok;
David Shaw31dec252009-03-05 20:16:14 -0500993 unsigned long cnt;
J. Bruce Fieldsffe11372012-11-15 14:52:19 -0500994 int nvecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (write->wr_offset >= OFFSET_MAX)
997 return nfserr_inval;
998
Chuck Leverd890be12018-03-27 10:53:27 -0400999 cnt = write->wr_buflen;
1000 trace_nfsd_write_start(rqstp, &cstate->current_fh,
1001 write->wr_offset, cnt);
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01001002 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001003 stateid, WR_STATE, &nf, NULL);
J. Bruce Fields375c5542006-01-18 17:43:33 -08001004 if (status) {
1005 dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
1006 return status;
1007 }
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 write->wr_how_written = write->wr_stable_how;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +03001010 gen_boot_verifier(&write->wr_verifier, SVC_NET(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Chuck Lever3fd95572018-07-27 11:19:05 -04001012 nvecs = svc_fill_write_vector(rqstp, write->wr_pagelist,
1013 &write->wr_head, write->wr_buflen);
J. Bruce Fieldsffe11372012-11-15 14:52:19 -05001014 WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec));
1015
Jeff Layton5c4583b2019-08-18 14:18:54 -04001016 status = nfsd_vfs_write(rqstp, &cstate->current_fh, nf->nf_file,
Christoph Hellwigaf90f702015-06-18 16:45:00 +02001017 write->wr_offset, rqstp->rq_vec, nvecs, &cnt,
Kinglong Mee54bbb7d2016-12-31 20:59:53 +08001018 write->wr_how_written);
Jeff Layton5c4583b2019-08-18 14:18:54 -04001019 nfsd_file_put(nf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
David Shaw31dec252009-03-05 20:16:14 -05001021 write->wr_bytes_written = cnt;
Chuck Leverd890be12018-03-27 10:53:27 -04001022 trace_nfsd_write_done(rqstp, &cstate->current_fh,
1023 write->wr_offset, cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
Anna Schumaker24bab492014-09-26 13:58:27 -04001027static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001028nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Jeff Layton5c4583b2019-08-18 14:18:54 -04001029 stateid_t *src_stateid, struct nfsd_file **src,
1030 stateid_t *dst_stateid, struct nfsd_file **dst)
Christoph Hellwigffa01602015-12-03 12:59:52 +01001031{
Christoph Hellwigffa01602015-12-03 12:59:52 +01001032 __be32 status;
1033
Scott Mayhew01310bb2018-11-08 11:11:36 -05001034 if (!cstate->save_fh.fh_dentry)
1035 return nfserr_nofilehandle;
1036
Christoph Hellwigffa01602015-12-03 12:59:52 +01001037 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->save_fh,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001038 src_stateid, RD_STATE, src, NULL);
Christoph Hellwigffa01602015-12-03 12:59:52 +01001039 if (status) {
1040 dprintk("NFSD: %s: couldn't process src stateid!\n", __func__);
1041 goto out;
1042 }
1043
1044 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001045 dst_stateid, WR_STATE, dst, NULL);
Christoph Hellwigffa01602015-12-03 12:59:52 +01001046 if (status) {
1047 dprintk("NFSD: %s: couldn't process dst stateid!\n", __func__);
1048 goto out_put_src;
1049 }
1050
1051 /* fix up for NFS-specific error code */
Jeff Layton5c4583b2019-08-18 14:18:54 -04001052 if (!S_ISREG(file_inode((*src)->nf_file)->i_mode) ||
1053 !S_ISREG(file_inode((*dst)->nf_file)->i_mode)) {
Christoph Hellwigffa01602015-12-03 12:59:52 +01001054 status = nfserr_wrong_type;
1055 goto out_put_dst;
1056 }
1057
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001058out:
1059 return status;
1060out_put_dst:
Jeff Layton5c4583b2019-08-18 14:18:54 -04001061 nfsd_file_put(*dst);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001062out_put_src:
Jeff Layton5c4583b2019-08-18 14:18:54 -04001063 nfsd_file_put(*src);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001064 goto out;
1065}
1066
1067static __be32
1068nfsd4_clone(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001069 union nfsd4_op_u *u)
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001070{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001071 struct nfsd4_clone *clone = &u->clone;
Jeff Layton5c4583b2019-08-18 14:18:54 -04001072 struct nfsd_file *src, *dst;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001073 __be32 status;
1074
1075 status = nfsd4_verify_copy(rqstp, cstate, &clone->cl_src_stateid, &src,
1076 &clone->cl_dst_stateid, &dst);
1077 if (status)
1078 goto out;
1079
Jeff Layton5c4583b2019-08-18 14:18:54 -04001080 status = nfsd4_clone_file_range(src->nf_file, clone->cl_src_pos,
Trond Myklebusta25e37262019-11-27 17:05:51 -05001081 dst->nf_file, clone->cl_dst_pos, clone->cl_count,
1082 EX_ISSYNC(cstate->current_fh.fh_export));
Christoph Hellwigffa01602015-12-03 12:59:52 +01001083
Jeff Layton5c4583b2019-08-18 14:18:54 -04001084 nfsd_file_put(dst);
1085 nfsd_file_put(src);
Christoph Hellwigffa01602015-12-03 12:59:52 +01001086out:
1087 return status;
1088}
1089
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001090void nfs4_put_copy(struct nfsd4_copy *copy)
1091{
1092 if (!refcount_dec_and_test(&copy->refcount))
1093 return;
1094 kfree(copy);
1095}
1096
1097static bool
1098check_and_set_stop_copy(struct nfsd4_copy *copy)
1099{
1100 bool value;
1101
1102 spin_lock(&copy->cp_clp->async_lock);
1103 value = copy->stopped;
1104 if (!copy->stopped)
1105 copy->stopped = true;
1106 spin_unlock(&copy->cp_clp->async_lock);
1107 return value;
1108}
1109
1110static void nfsd4_stop_copy(struct nfsd4_copy *copy)
1111{
1112 /* only 1 thread should stop the copy */
1113 if (!check_and_set_stop_copy(copy))
1114 kthread_stop(copy->copy_task);
1115 nfs4_put_copy(copy);
1116}
1117
1118static struct nfsd4_copy *nfsd4_get_copy(struct nfs4_client *clp)
1119{
1120 struct nfsd4_copy *copy = NULL;
1121
1122 spin_lock(&clp->async_lock);
1123 if (!list_empty(&clp->async_copies)) {
1124 copy = list_first_entry(&clp->async_copies, struct nfsd4_copy,
1125 copies);
1126 refcount_inc(&copy->refcount);
1127 }
1128 spin_unlock(&clp->async_lock);
1129 return copy;
1130}
1131
1132void nfsd4_shutdown_copy(struct nfs4_client *clp)
1133{
1134 struct nfsd4_copy *copy;
1135
1136 while ((copy = nfsd4_get_copy(clp)) != NULL)
1137 nfsd4_stop_copy(copy);
1138}
1139
1140static void nfsd4_cb_offload_release(struct nfsd4_callback *cb)
1141{
1142 struct nfsd4_copy *copy = container_of(cb, struct nfsd4_copy, cp_cb);
1143
1144 nfs4_put_copy(copy);
1145}
1146
1147static int nfsd4_cb_offload_done(struct nfsd4_callback *cb,
1148 struct rpc_task *task)
1149{
1150 return 1;
1151}
1152
1153static const struct nfsd4_callback_ops nfsd4_cb_offload_ops = {
1154 .release = nfsd4_cb_offload_release,
1155 .done = nfsd4_cb_offload_done
1156};
1157
1158static void nfsd4_init_copy_res(struct nfsd4_copy *copy, bool sync)
1159{
1160 copy->cp_res.wr_stable_how = NFS_UNSTABLE;
1161 copy->cp_synchronous = sync;
1162 gen_boot_verifier(&copy->cp_res.wr_verifier, copy->cp_clp->net);
1163}
1164
1165static ssize_t _nfsd_copy_file_range(struct nfsd4_copy *copy)
1166{
1167 ssize_t bytes_copied = 0;
1168 size_t bytes_total = copy->cp_count;
1169 u64 src_pos = copy->cp_src_pos;
1170 u64 dst_pos = copy->cp_dst_pos;
1171
1172 do {
1173 if (kthread_should_stop())
1174 break;
Jeff Layton5c4583b2019-08-18 14:18:54 -04001175 bytes_copied = nfsd_copy_file_range(copy->nf_src->nf_file,
1176 src_pos, copy->nf_dst->nf_file, dst_pos,
1177 bytes_total);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001178 if (bytes_copied <= 0)
1179 break;
1180 bytes_total -= bytes_copied;
1181 copy->cp_res.wr_bytes_written += bytes_copied;
1182 src_pos += bytes_copied;
1183 dst_pos += bytes_copied;
1184 } while (bytes_total > 0 && !copy->cp_synchronous);
1185 return bytes_copied;
1186}
1187
1188static __be32 nfsd4_do_copy(struct nfsd4_copy *copy, bool sync)
1189{
1190 __be32 status;
1191 ssize_t bytes;
1192
1193 bytes = _nfsd_copy_file_range(copy);
1194 /* for async copy, we ignore the error, client can always retry
1195 * to get the error
1196 */
1197 if (bytes < 0 && !copy->cp_res.wr_bytes_written)
1198 status = nfserrno(bytes);
1199 else {
1200 nfsd4_init_copy_res(copy, sync);
1201 status = nfs_ok;
1202 }
1203
Jeff Layton5c4583b2019-08-18 14:18:54 -04001204 nfsd_file_put(copy->nf_src);
1205 nfsd_file_put(copy->nf_dst);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001206 return status;
1207}
1208
1209static void dup_copy_fields(struct nfsd4_copy *src, struct nfsd4_copy *dst)
1210{
1211 dst->cp_src_pos = src->cp_src_pos;
1212 dst->cp_dst_pos = src->cp_dst_pos;
1213 dst->cp_count = src->cp_count;
1214 dst->cp_synchronous = src->cp_synchronous;
1215 memcpy(&dst->cp_res, &src->cp_res, sizeof(src->cp_res));
1216 memcpy(&dst->fh, &src->fh, sizeof(src->fh));
1217 dst->cp_clp = src->cp_clp;
Jeff Layton5c4583b2019-08-18 14:18:54 -04001218 dst->nf_dst = nfsd_file_get(src->nf_dst);
1219 dst->nf_src = nfsd_file_get(src->nf_src);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001220 memcpy(&dst->cp_stateid, &src->cp_stateid, sizeof(src->cp_stateid));
1221}
1222
1223static void cleanup_async_copy(struct nfsd4_copy *copy)
1224{
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001225 nfs4_free_copy_state(copy);
Jeff Layton5c4583b2019-08-18 14:18:54 -04001226 nfsd_file_put(copy->nf_dst);
1227 nfsd_file_put(copy->nf_src);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001228 spin_lock(&copy->cp_clp->async_lock);
1229 list_del(&copy->copies);
1230 spin_unlock(&copy->cp_clp->async_lock);
1231 nfs4_put_copy(copy);
1232}
1233
1234static int nfsd4_do_async_copy(void *data)
1235{
1236 struct nfsd4_copy *copy = (struct nfsd4_copy *)data;
1237 struct nfsd4_copy *cb_copy;
1238
1239 copy->nfserr = nfsd4_do_copy(copy, 0);
1240 cb_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
1241 if (!cb_copy)
1242 goto out;
1243 memcpy(&cb_copy->cp_res, &copy->cp_res, sizeof(copy->cp_res));
1244 cb_copy->cp_clp = copy->cp_clp;
1245 cb_copy->nfserr = copy->nfserr;
1246 memcpy(&cb_copy->fh, &copy->fh, sizeof(copy->fh));
1247 nfsd4_init_cb(&cb_copy->cp_cb, cb_copy->cp_clp,
1248 &nfsd4_cb_offload_ops, NFSPROC4_CLNT_CB_OFFLOAD);
1249 nfsd4_run_cb(&cb_copy->cp_cb);
1250out:
1251 cleanup_async_copy(copy);
1252 return 0;
1253}
1254
Christoph Hellwigffa01602015-12-03 12:59:52 +01001255static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001256nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001257 union nfsd4_op_u *u)
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001258{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001259 struct nfsd4_copy *copy = &u->copy;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001260 __be32 status;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001261 struct nfsd4_copy *async_copy = NULL;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001262
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001263 status = nfsd4_verify_copy(rqstp, cstate, &copy->cp_src_stateid,
Jeff Layton5c4583b2019-08-18 14:18:54 -04001264 &copy->nf_src, &copy->cp_dst_stateid,
1265 &copy->nf_dst);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001266 if (status)
1267 goto out;
1268
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001269 copy->cp_clp = cstate->clp;
1270 memcpy(&copy->fh, &cstate->current_fh.fh_handle,
1271 sizeof(struct knfsd_fh));
1272 if (!copy->cp_synchronous) {
1273 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001274
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001275 status = nfserrno(-ENOMEM);
1276 async_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
1277 if (!async_copy)
1278 goto out;
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001279 if (!nfs4_init_copy_state(nn, copy)) {
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001280 kfree(async_copy);
1281 goto out;
1282 }
1283 refcount_set(&async_copy->refcount, 1);
1284 memcpy(&copy->cp_res.cb_stateid, &copy->cp_stateid,
1285 sizeof(copy->cp_stateid));
1286 dup_copy_fields(copy, async_copy);
1287 async_copy->copy_task = kthread_create(nfsd4_do_async_copy,
1288 async_copy, "%s", "copy thread");
1289 if (IS_ERR(async_copy->copy_task))
1290 goto out_err;
1291 spin_lock(&async_copy->cp_clp->async_lock);
1292 list_add(&async_copy->copies,
1293 &async_copy->cp_clp->async_copies);
1294 spin_unlock(&async_copy->cp_clp->async_lock);
1295 wake_up_process(async_copy->copy_task);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001296 status = nfs_ok;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001297 } else
1298 status = nfsd4_do_copy(copy, 1);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001299out:
1300 return status;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001301out_err:
Olga Kornievskaia18f428d2019-12-04 15:13:54 -05001302 if (async_copy)
1303 cleanup_async_copy(async_copy);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001304 goto out;
1305}
1306
1307struct nfsd4_copy *
1308find_async_copy(struct nfs4_client *clp, stateid_t *stateid)
1309{
1310 struct nfsd4_copy *copy;
1311
1312 spin_lock(&clp->async_lock);
1313 list_for_each_entry(copy, &clp->async_copies, copies) {
1314 if (memcmp(&copy->cp_stateid, stateid, NFS4_STATEID_SIZE))
1315 continue;
1316 refcount_inc(&copy->refcount);
1317 spin_unlock(&clp->async_lock);
1318 return copy;
1319 }
1320 spin_unlock(&clp->async_lock);
1321 return NULL;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001322}
1323
1324static __be32
Olga Kornievskaia885e2bf2018-07-20 18:19:19 -04001325nfsd4_offload_cancel(struct svc_rqst *rqstp,
1326 struct nfsd4_compound_state *cstate,
1327 union nfsd4_op_u *u)
1328{
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001329 struct nfsd4_offload_status *os = &u->offload_status;
1330 __be32 status = 0;
1331 struct nfsd4_copy *copy;
1332 struct nfs4_client *clp = cstate->clp;
1333
1334 copy = find_async_copy(clp, &os->stateid);
1335 if (copy)
1336 nfsd4_stop_copy(copy);
1337 else
1338 status = nfserr_bad_stateid;
1339
1340 return status;
Olga Kornievskaia885e2bf2018-07-20 18:19:19 -04001341}
1342
1343static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04001344nfsd4_copy_notify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1345 union nfsd4_op_u *u)
1346{
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001347 struct nfsd4_copy_notify *cn = &u->copy_notify;
1348 __be32 status;
1349 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1350 struct nfs4_stid *stid;
1351 struct nfs4_cpntf_state *cps;
1352 struct nfs4_client *clp = cstate->clp;
1353
1354 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1355 &cn->cpn_src_stateid, RD_STATE, NULL,
1356 &stid);
1357 if (status)
1358 return status;
1359
1360 cn->cpn_sec = nn->nfsd4_lease;
1361 cn->cpn_nsec = 0;
1362
1363 status = nfserrno(-ENOMEM);
1364 cps = nfs4_alloc_init_cpntf_state(nn, stid);
1365 if (!cps)
1366 goto out;
1367 memcpy(&cn->cpn_cnr_stateid, &cps->cp_stateid.stid, sizeof(stateid_t));
1368 memcpy(&cps->cp_p_stateid, &stid->sc_stateid, sizeof(stateid_t));
1369 memcpy(&cps->cp_p_clid, &clp->cl_clientid, sizeof(clientid_t));
1370
1371 /* For now, only return one server address in cpn_src, the
1372 * address used by the client to connect to this server.
1373 */
1374 cn->cpn_src.nl4_type = NL4_NETADDR;
1375 status = nfsd4_set_netaddr((struct sockaddr *)&rqstp->rq_daddr,
1376 &cn->cpn_src.u.nl4_addr);
1377 WARN_ON_ONCE(status);
1378 if (status) {
1379 nfs4_put_cpntf_state(nn, cps);
1380 goto out;
1381 }
1382out:
1383 nfs4_put_stid(stid);
1384 return status;
Olga Kornievskaia51911862019-08-08 11:14:59 -04001385}
1386
1387static __be32
Anna Schumaker95d871f2014-11-07 14:44:26 -05001388nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1389 struct nfsd4_fallocate *fallocate, int flags)
1390{
J. Bruce Fields0d4d6722018-11-06 17:44:03 -05001391 __be32 status;
Jeff Layton5c4583b2019-08-18 14:18:54 -04001392 struct nfsd_file *nf;
Anna Schumaker95d871f2014-11-07 14:44:26 -05001393
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01001394 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
Anna Schumaker95d871f2014-11-07 14:44:26 -05001395 &fallocate->falloc_stateid,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001396 WR_STATE, &nf, NULL);
Anna Schumaker95d871f2014-11-07 14:44:26 -05001397 if (status != nfs_ok) {
1398 dprintk("NFSD: nfsd4_fallocate: couldn't process stateid!\n");
1399 return status;
1400 }
1401
Jeff Layton5c4583b2019-08-18 14:18:54 -04001402 status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, nf->nf_file,
Anna Schumaker95d871f2014-11-07 14:44:26 -05001403 fallocate->falloc_offset,
1404 fallocate->falloc_length,
1405 flags);
Jeff Layton5c4583b2019-08-18 14:18:54 -04001406 nfsd_file_put(nf);
Anna Schumaker95d871f2014-11-07 14:44:26 -05001407 return status;
1408}
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04001409static __be32
1410nfsd4_offload_status(struct svc_rqst *rqstp,
1411 struct nfsd4_compound_state *cstate,
1412 union nfsd4_op_u *u)
1413{
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001414 struct nfsd4_offload_status *os = &u->offload_status;
1415 __be32 status = 0;
1416 struct nfsd4_copy *copy;
1417 struct nfs4_client *clp = cstate->clp;
1418
1419 copy = find_async_copy(clp, &os->stateid);
1420 if (copy) {
1421 os->count = copy->cp_res.wr_bytes_written;
1422 nfs4_put_copy(copy);
1423 } else
1424 status = nfserr_bad_stateid;
1425
1426 return status;
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04001427}
Anna Schumaker95d871f2014-11-07 14:44:26 -05001428
1429static __be32
1430nfsd4_allocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001431 union nfsd4_op_u *u)
Anna Schumaker95d871f2014-11-07 14:44:26 -05001432{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001433 return nfsd4_fallocate(rqstp, cstate, &u->allocate, 0);
Anna Schumaker95d871f2014-11-07 14:44:26 -05001434}
1435
1436static __be32
Anna Schumakerb0cb9082014-11-07 14:44:27 -05001437nfsd4_deallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001438 union nfsd4_op_u *u)
Anna Schumakerb0cb9082014-11-07 14:44:27 -05001439{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001440 return nfsd4_fallocate(rqstp, cstate, &u->deallocate,
Anna Schumakerb0cb9082014-11-07 14:44:27 -05001441 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
1442}
1443
1444static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04001445nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001446 union nfsd4_op_u *u)
Anna Schumaker24bab492014-09-26 13:58:27 -04001447{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001448 struct nfsd4_seek *seek = &u->seek;
Anna Schumaker24bab492014-09-26 13:58:27 -04001449 int whence;
1450 __be32 status;
Jeff Layton5c4583b2019-08-18 14:18:54 -04001451 struct nfsd_file *nf;
Anna Schumaker24bab492014-09-26 13:58:27 -04001452
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01001453 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
Anna Schumaker24bab492014-09-26 13:58:27 -04001454 &seek->seek_stateid,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001455 RD_STATE, &nf, NULL);
Anna Schumaker24bab492014-09-26 13:58:27 -04001456 if (status) {
1457 dprintk("NFSD: nfsd4_seek: couldn't process stateid!\n");
1458 return status;
1459 }
1460
1461 switch (seek->seek_whence) {
1462 case NFS4_CONTENT_DATA:
1463 whence = SEEK_DATA;
1464 break;
1465 case NFS4_CONTENT_HOLE:
1466 whence = SEEK_HOLE;
1467 break;
1468 default:
1469 status = nfserr_union_notsupp;
1470 goto out;
1471 }
1472
1473 /*
1474 * Note: This call does change file->f_pos, but nothing in NFSD
1475 * should ever file->f_pos.
1476 */
Jeff Layton5c4583b2019-08-18 14:18:54 -04001477 seek->seek_pos = vfs_llseek(nf->nf_file, seek->seek_offset, whence);
Anna Schumaker24bab492014-09-26 13:58:27 -04001478 if (seek->seek_pos < 0)
1479 status = nfserrno(seek->seek_pos);
Jeff Layton5c4583b2019-08-18 14:18:54 -04001480 else if (seek->seek_pos >= i_size_read(file_inode(nf->nf_file)))
Anna Schumaker24bab492014-09-26 13:58:27 -04001481 seek->seek_eof = true;
1482
1483out:
Jeff Layton5c4583b2019-08-18 14:18:54 -04001484 nfsd_file_put(nf);
Anna Schumaker24bab492014-09-26 13:58:27 -04001485 return status;
1486}
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488/* This routine never returns NFS_OK! If there are no other errors, it
1489 * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
1490 * attributes matched. VERIFY is implemented by mapping NFSERR_SAME
1491 * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
1492 */
Al Virob37ad282006-10-19 23:28:59 -07001493static __be32
J.Bruce Fieldsc954e2a2006-12-13 00:35:31 -08001494_nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08001495 struct nfsd4_verify *verify)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Al Viro2ebbc012006-10-19 23:28:58 -07001497 __be32 *buf, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 int count;
Al Virob37ad282006-10-19 23:28:59 -07001499 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Miklos Szeredi8837abc2008-06-16 13:20:29 +02001501 status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (status)
1503 return status;
1504
Yu Zhiguo3c8e0312009-05-16 16:22:31 +08001505 status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
1506 if (status)
1507 return status;
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
1510 || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
1511 return nfserr_inval;
1512 if (verify->ve_attrlen & 3)
1513 return nfserr_inval;
1514
1515 /* count in words:
1516 * bitmap_len(1) + bitmap(2) + attr_len(1) = 4
1517 */
1518 count = 4 + (verify->ve_attrlen >> 2);
1519 buf = kmalloc(count << 2, GFP_KERNEL);
1520 if (!buf)
J. Bruce Fields3e772462011-08-10 19:07:33 -04001521 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
J. Bruce Fields84822d02012-12-14 17:57:50 -05001523 p = buf;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04001524 status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08001525 cstate->current_fh.fh_export,
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04001526 cstate->current_fh.fh_dentry,
1527 verify->ve_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08001528 rqstp, 0);
J. Bruce Fields41ae6e712013-08-21 15:32:50 -04001529 /*
1530 * If nfsd4_encode_fattr() ran out of space, assume that's because
1531 * the attributes are longer (hence different) than those given:
1532 */
J. Bruce Fields84822d02012-12-14 17:57:50 -05001533 if (status == nfserr_resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 status = nfserr_not_same;
1535 if (status)
1536 goto out_kfree;
1537
Benny Halevy95ec28c2009-04-03 08:29:08 +03001538 /* skip bitmap */
1539 p = buf + 1 + ntohl(buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 status = nfserr_not_same;
1541 if (ntohl(*p++) != verify->ve_attrlen)
1542 goto out_kfree;
1543 if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
1544 status = nfserr_same;
1545
1546out_kfree:
1547 kfree(buf);
1548 return status;
1549}
1550
J.Bruce Fieldsc954e2a2006-12-13 00:35:31 -08001551static __be32
1552nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001553 union nfsd4_op_u *u)
J.Bruce Fieldsc954e2a2006-12-13 00:35:31 -08001554{
1555 __be32 status;
1556
Christoph Hellwigeb698532017-05-08 20:58:35 +02001557 status = _nfsd4_verify(rqstp, cstate, &u->verify);
J.Bruce Fieldsc954e2a2006-12-13 00:35:31 -08001558 return status == nfserr_not_same ? nfs_ok : status;
1559}
1560
1561static __be32
1562nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001563 union nfsd4_op_u *u)
J.Bruce Fieldsc954e2a2006-12-13 00:35:31 -08001564{
1565 __be32 status;
1566
Christoph Hellwigeb698532017-05-08 20:58:35 +02001567 status = _nfsd4_verify(rqstp, cstate, &u->nverify);
J.Bruce Fieldsc954e2a2006-12-13 00:35:31 -08001568 return status == nfserr_same ? nfs_ok : status;
1569}
1570
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001571#ifdef CONFIG_NFSD_PNFS
1572static const struct nfsd4_layout_ops *
1573nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type)
1574{
Jeff Layton8a4c3922016-07-10 15:55:58 -04001575 if (!exp->ex_layout_types) {
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001576 dprintk("%s: export does not support pNFS\n", __func__);
1577 return NULL;
1578 }
1579
Ari Kauppib550a322017-05-05 16:07:55 -04001580 if (layout_type >= LAYOUT_TYPE_MAX ||
1581 !(exp->ex_layout_types & (1 << layout_type))) {
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001582 dprintk("%s: layout type %d not supported\n",
1583 __func__, layout_type);
1584 return NULL;
1585 }
1586
1587 return nfsd4_layout_ops[layout_type];
1588}
1589
1590static __be32
1591nfsd4_getdeviceinfo(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001592 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001593{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001594 struct nfsd4_getdeviceinfo *gdp = &u->getdeviceinfo;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001595 const struct nfsd4_layout_ops *ops;
1596 struct nfsd4_deviceid_map *map;
1597 struct svc_export *exp;
1598 __be32 nfserr;
1599
1600 dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n",
1601 __func__,
1602 gdp->gd_layout_type,
1603 gdp->gd_devid.fsid_idx, gdp->gd_devid.generation,
1604 gdp->gd_maxcount);
1605
1606 map = nfsd4_find_devid_map(gdp->gd_devid.fsid_idx);
1607 if (!map) {
1608 dprintk("%s: couldn't find device ID to export mapping!\n",
1609 __func__);
1610 return nfserr_noent;
1611 }
1612
1613 exp = rqst_exp_find(rqstp, map->fsid_type, map->fsid);
1614 if (IS_ERR(exp)) {
1615 dprintk("%s: could not find device id\n", __func__);
1616 return nfserr_noent;
1617 }
1618
1619 nfserr = nfserr_layoutunavailable;
1620 ops = nfsd4_layout_verify(exp, gdp->gd_layout_type);
1621 if (!ops)
1622 goto out;
1623
1624 nfserr = nfs_ok;
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +01001625 if (gdp->gd_maxcount != 0) {
1626 nfserr = ops->proc_getdeviceinfo(exp->ex_path.mnt->mnt_sb,
Tom Haynesd7c920d2016-06-14 13:41:27 -07001627 rqstp, cstate->session->se_client, gdp);
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +01001628 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001629
1630 gdp->gd_notify_types &= ops->notify_types;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001631out:
Kinglong Meea1420382015-03-15 23:12:15 +08001632 exp_put(exp);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001633 return nfserr;
1634}
1635
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04001636static void
1637nfsd4_getdeviceinfo_release(union nfsd4_op_u *u)
1638{
1639 kfree(u->getdeviceinfo.gd_device);
1640}
1641
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001642static __be32
1643nfsd4_layoutget(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001644 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001645{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001646 struct nfsd4_layoutget *lgp = &u->layoutget;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001647 struct svc_fh *current_fh = &cstate->current_fh;
1648 const struct nfsd4_layout_ops *ops;
1649 struct nfs4_layout_stateid *ls;
1650 __be32 nfserr;
Benjamin Coddington66282ec2017-12-19 09:35:25 -05001651 int accmode = NFSD_MAY_READ_IF_EXEC;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001652
1653 switch (lgp->lg_seg.iomode) {
1654 case IOMODE_READ:
Benjamin Coddington66282ec2017-12-19 09:35:25 -05001655 accmode |= NFSD_MAY_READ;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001656 break;
1657 case IOMODE_RW:
Benjamin Coddington66282ec2017-12-19 09:35:25 -05001658 accmode |= NFSD_MAY_READ | NFSD_MAY_WRITE;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001659 break;
1660 default:
1661 dprintk("%s: invalid iomode %d\n",
1662 __func__, lgp->lg_seg.iomode);
1663 nfserr = nfserr_badiomode;
1664 goto out;
1665 }
1666
1667 nfserr = fh_verify(rqstp, current_fh, 0, accmode);
1668 if (nfserr)
1669 goto out;
1670
1671 nfserr = nfserr_layoutunavailable;
1672 ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);
1673 if (!ops)
1674 goto out;
1675
1676 /*
1677 * Verify minlength and range as per RFC5661:
1678 * o If loga_length is less than loga_minlength,
1679 * the metadata server MUST return NFS4ERR_INVAL.
1680 * o If the sum of loga_offset and loga_minlength exceeds
1681 * NFS4_UINT64_MAX, and loga_minlength is not
1682 * NFS4_UINT64_MAX, the error NFS4ERR_INVAL MUST result.
1683 * o If the sum of loga_offset and loga_length exceeds
1684 * NFS4_UINT64_MAX, and loga_length is not NFS4_UINT64_MAX,
1685 * the error NFS4ERR_INVAL MUST result.
1686 */
1687 nfserr = nfserr_inval;
1688 if (lgp->lg_seg.length < lgp->lg_minlength ||
1689 (lgp->lg_minlength != NFS4_MAX_UINT64 &&
1690 lgp->lg_minlength > NFS4_MAX_UINT64 - lgp->lg_seg.offset) ||
1691 (lgp->lg_seg.length != NFS4_MAX_UINT64 &&
1692 lgp->lg_seg.length > NFS4_MAX_UINT64 - lgp->lg_seg.offset))
1693 goto out;
1694 if (lgp->lg_seg.length == 0)
1695 goto out;
1696
1697 nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lgp->lg_sid,
1698 true, lgp->lg_layout_type, &ls);
Christoph Hellwig31ef83d2014-08-16 19:02:22 -05001699 if (nfserr) {
Chuck Leverf394b622018-03-27 10:53:11 -04001700 trace_nfsd_layout_get_lookup_fail(&lgp->lg_sid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001701 goto out;
Christoph Hellwig31ef83d2014-08-16 19:02:22 -05001702 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001703
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02001704 nfserr = nfserr_recallconflict;
1705 if (atomic_read(&ls->ls_stid.sc_file->fi_lo_recalls))
1706 goto out_put_stid;
1707
David Howells2b0143b2015-03-17 22:25:59 +00001708 nfserr = ops->proc_layoutget(d_inode(current_fh->fh_dentry),
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001709 current_fh, lgp);
1710 if (nfserr)
1711 goto out_put_stid;
1712
1713 nfserr = nfsd4_insert_layout(lgp, ls);
1714
1715out_put_stid:
Jeff Laytoncc8a5532015-09-17 07:58:24 -04001716 mutex_unlock(&ls->ls_mutex);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001717 nfs4_put_stid(&ls->ls_stid);
1718out:
1719 return nfserr;
1720}
1721
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04001722static void
1723nfsd4_layoutget_release(union nfsd4_op_u *u)
1724{
1725 kfree(u->layoutget.lg_content);
1726}
1727
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001728static __be32
1729nfsd4_layoutcommit(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001730 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001731{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001732 struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001733 const struct nfsd4_layout_seg *seg = &lcp->lc_seg;
1734 struct svc_fh *current_fh = &cstate->current_fh;
1735 const struct nfsd4_layout_ops *ops;
1736 loff_t new_size = lcp->lc_last_wr + 1;
1737 struct inode *inode;
1738 struct nfs4_layout_stateid *ls;
1739 __be32 nfserr;
1740
1741 nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_WRITE);
1742 if (nfserr)
1743 goto out;
1744
1745 nfserr = nfserr_layoutunavailable;
1746 ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
1747 if (!ops)
1748 goto out;
David Howells2b0143b2015-03-17 22:25:59 +00001749 inode = d_inode(current_fh->fh_dentry);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001750
1751 nfserr = nfserr_inval;
1752 if (new_size <= seg->offset) {
1753 dprintk("pnfsd: last write before layout segment\n");
1754 goto out;
1755 }
1756 if (new_size > seg->offset + seg->length) {
1757 dprintk("pnfsd: last write beyond layout segment\n");
1758 goto out;
1759 }
1760 if (!lcp->lc_newoffset && new_size > i_size_read(inode)) {
1761 dprintk("pnfsd: layoutcommit beyond EOF\n");
1762 goto out;
1763 }
1764
1765 nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lcp->lc_sid,
1766 false, lcp->lc_layout_type,
1767 &ls);
1768 if (nfserr) {
Chuck Leverf394b622018-03-27 10:53:11 -04001769 trace_nfsd_layout_commit_lookup_fail(&lcp->lc_sid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001770 /* fixup error code as per RFC5661 */
1771 if (nfserr == nfserr_bad_stateid)
1772 nfserr = nfserr_badlayout;
1773 goto out;
1774 }
1775
Jeff Laytoncc8a5532015-09-17 07:58:24 -04001776 /* LAYOUTCOMMIT does not require any serialization */
1777 mutex_unlock(&ls->ls_mutex);
1778
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001779 if (new_size > i_size_read(inode)) {
1780 lcp->lc_size_chg = 1;
1781 lcp->lc_newsize = new_size;
1782 } else {
1783 lcp->lc_size_chg = 0;
1784 }
1785
Kinglong Meed8398fc2015-07-07 10:12:03 +08001786 nfserr = ops->proc_layoutcommit(inode, lcp);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001787 nfs4_put_stid(&ls->ls_stid);
1788out:
1789 return nfserr;
1790}
1791
1792static __be32
1793nfsd4_layoutreturn(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02001794 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001795{
Christoph Hellwigeb698532017-05-08 20:58:35 +02001796 struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001797 struct svc_fh *current_fh = &cstate->current_fh;
1798 __be32 nfserr;
1799
1800 nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_NOP);
1801 if (nfserr)
1802 goto out;
1803
1804 nfserr = nfserr_layoutunavailable;
1805 if (!nfsd4_layout_verify(current_fh->fh_export, lrp->lr_layout_type))
1806 goto out;
1807
1808 switch (lrp->lr_seg.iomode) {
1809 case IOMODE_READ:
1810 case IOMODE_RW:
1811 case IOMODE_ANY:
1812 break;
1813 default:
1814 dprintk("%s: invalid iomode %d\n", __func__,
1815 lrp->lr_seg.iomode);
1816 nfserr = nfserr_inval;
1817 goto out;
1818 }
1819
1820 switch (lrp->lr_return_type) {
1821 case RETURN_FILE:
1822 nfserr = nfsd4_return_file_layouts(rqstp, cstate, lrp);
1823 break;
1824 case RETURN_FSID:
1825 case RETURN_ALL:
1826 nfserr = nfsd4_return_client_layouts(rqstp, cstate, lrp);
1827 break;
1828 default:
1829 dprintk("%s: invalid return_type %d\n", __func__,
1830 lrp->lr_return_type);
1831 nfserr = nfserr_inval;
1832 break;
1833 }
1834out:
1835 return nfserr;
1836}
1837#endif /* CONFIG_NFSD_PNFS */
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839/*
1840 * NULL call.
1841 */
Al Viro7111c662006-10-19 23:28:45 -07001842static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +02001843nfsd4_proc_null(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
1845 return nfs_ok;
1846}
1847
Shankar Anande2b20952006-07-10 04:45:44 -07001848static inline void nfsd4_increment_op_stats(u32 opnum)
1849{
1850 if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
1851 nfsdstats.nfs4_opcount[opnum]++;
1852}
1853
Christoph Hellwigbb2a8b02017-05-08 23:46:47 +02001854static const struct nfsd4_operation nfsd4_ops[];
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08001855
Adrian Bunkf1c7f792008-08-08 19:26:42 +03001856static const char *nfsd4_op_name(unsigned opnum);
Benny Halevyb001a1b2008-07-02 11:15:03 +03001857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858/*
J. Bruce Fields57716352010-04-21 12:27:19 -04001859 * Enforce NFSv4.1 COMPOUND ordering rules:
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03001860 *
J. Bruce Fields57716352010-04-21 12:27:19 -04001861 * Also note, enforced elsewhere:
1862 * - SEQUENCE other than as first op results in
1863 * NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001864 * - BIND_CONN_TO_SESSION must be the only op in its compound.
1865 * (Enforced in nfsd4_bind_conn_to_session().)
J. Bruce Fields57716352010-04-21 12:27:19 -04001866 * - DESTROY_SESSION must be the final operation in a compound, if
1867 * sessionid's in SEQUENCE and DESTROY_SESSION are the same.
1868 * (Enforced in nfsd4_destroy_session().)
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03001869 */
J. Bruce Fields57716352010-04-21 12:27:19 -04001870static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03001871{
J. Bruce Fields7a04cfd2018-06-13 15:04:59 -04001872 struct nfsd4_op *first_op = &args->ops[0];
J. Bruce Fields57716352010-04-21 12:27:19 -04001873
1874 /* These ordering requirements don't apply to NFSv4.0: */
1875 if (args->minorversion == 0)
1876 return nfs_ok;
1877 /* This is weird, but OK, not our problem: */
1878 if (args->opcnt == 0)
1879 return nfs_ok;
J. Bruce Fields7a04cfd2018-06-13 15:04:59 -04001880 if (first_op->status == nfserr_op_illegal)
J. Bruce Fields57716352010-04-21 12:27:19 -04001881 return nfs_ok;
J. Bruce Fields7a04cfd2018-06-13 15:04:59 -04001882 if (!(nfsd4_ops[first_op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
J. Bruce Fields57716352010-04-21 12:27:19 -04001883 return nfserr_op_not_in_session;
J. Bruce Fields7a04cfd2018-06-13 15:04:59 -04001884 if (first_op->opnum == OP_SEQUENCE)
J. Bruce Fields57716352010-04-21 12:27:19 -04001885 return nfs_ok;
J. Bruce Fields7a04cfd2018-06-13 15:04:59 -04001886 /*
1887 * So first_op is something allowed outside a session, like
1888 * EXCHANGE_ID; but then it has to be the only op in the
1889 * compound:
1890 */
J. Bruce Fields57716352010-04-21 12:27:19 -04001891 if (args->opcnt != 1)
1892 return nfserr_not_only_op;
1893 return nfs_ok;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03001894}
1895
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04001896const struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
J. Bruce Fields22b03212011-04-09 11:23:24 -04001897{
1898 return &nfsd4_ops[op->opnum];
1899}
1900
J. Bruce Fields10910062011-01-24 12:11:02 -05001901bool nfsd4_cache_this_op(struct nfsd4_op *op)
1902{
J. Bruce Fieldse372ba62014-05-19 12:27:11 -04001903 if (op->opnum == OP_ILLEGAL)
1904 return false;
J. Bruce Fieldsc8566942011-09-20 08:49:51 -04001905 return OPDESC(op)->op_flags & OP_CACHEME;
J. Bruce Fields10910062011-01-24 12:11:02 -05001906}
1907
J. Bruce Fields68d93182011-04-08 17:00:50 -04001908static bool need_wrongsec_check(struct svc_rqst *rqstp)
1909{
1910 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1911 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1912 struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
1913 struct nfsd4_op *next = &argp->ops[resp->opcnt];
Christoph Hellwigbb2a8b02017-05-08 23:46:47 +02001914 const struct nfsd4_operation *thisd = OPDESC(this);
1915 const struct nfsd4_operation *nextd;
J. Bruce Fields68d93182011-04-08 17:00:50 -04001916
J. Bruce Fields68d93182011-04-08 17:00:50 -04001917 /*
1918 * Most ops check wronsec on our own; only the putfh-like ops
1919 * have special rules.
1920 */
1921 if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
1922 return false;
1923 /*
1924 * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
1925 * put-filehandle operation if we're not going to use the
1926 * result:
1927 */
1928 if (argp->opcnt == resp->opcnt)
1929 return false;
J. Bruce Fields51904b02014-10-22 14:46:29 -04001930 if (next->opnum == OP_ILLEGAL)
1931 return false;
J. Bruce Fields68d93182011-04-08 17:00:50 -04001932 nextd = OPDESC(next);
1933 /*
1934 * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
1935 * errors themselves as necessary; others should check for them
1936 * now:
1937 */
1938 return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
1939}
1940
J. Bruce Fields2d124dfaa2014-01-15 16:26:42 -05001941static void svcxdr_init_encode(struct svc_rqst *rqstp,
1942 struct nfsd4_compoundres *resp)
1943{
1944 struct xdr_stream *xdr = &resp->xdr;
1945 struct xdr_buf *buf = &rqstp->rq_res;
1946 struct kvec *head = buf->head;
1947
1948 xdr->buf = buf;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04001949 xdr->iov = head;
J. Bruce Fields2d124dfaa2014-01-15 16:26:42 -05001950 xdr->p = head->iov_base + head->iov_len;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04001951 xdr->end = head->iov_base + PAGE_SIZE - rqstp->rq_auth_slack;
J. Bruce Fields6ac90392014-02-26 17:39:35 -05001952 /* Tail and page_len should be zero at this point: */
1953 buf->len = buf->head[0].iov_len;
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04001954 xdr->scratch.iov_len = 0;
J. Bruce Fields05638dc2014-06-02 12:05:47 -04001955 xdr->page_ptr = buf->pages - 1;
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04001956 buf->buflen = PAGE_SIZE * (1 + rqstp->rq_page_end - buf->pages)
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04001957 - rqstp->rq_auth_slack;
J. Bruce Fields2d124dfaa2014-01-15 16:26:42 -05001958}
1959
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03001960/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 * COMPOUND call.
1962 */
Al Viro7111c662006-10-19 23:28:45 -07001963static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +02001964nfsd4_proc_compound(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
Christoph Hellwiga6beb732017-05-08 17:35:49 +02001966 struct nfsd4_compoundargs *args = rqstp->rq_argp;
1967 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 struct nfsd4_op *op;
Andy Adamsone354d572009-03-28 11:30:52 +03001969 struct nfsd4_compound_state *cstate = &resp->cstate;
Kinglong Mee4daeed22014-03-26 17:10:37 +08001970 struct svc_fh *current_fh = &cstate->current_fh;
1971 struct svc_fh *save_fh = &cstate->save_fh;
Trond Myklebuste333f3b2019-04-09 11:46:19 -04001972 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Al Virob37ad282006-10-19 23:28:59 -07001973 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
J. Bruce Fields2d124dfaa2014-01-15 16:26:42 -05001975 svcxdr_init_encode(rqstp, resp);
J. Bruce Fields4aea24b2014-01-15 15:17:58 -05001976 resp->tagp = resp->xdr.p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 /* reserve space for: taglen, tag, and opcnt */
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05001978 xdr_reserve_space(&resp->xdr, 8 + args->taglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 resp->taglen = args->taglen;
1980 resp->tag = args->tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 resp->rqstp = rqstp;
Kinglong Mee4daeed22014-03-26 17:10:37 +08001982 cstate->minorversion = args->minorversion;
Kinglong Mee4daeed22014-03-26 17:10:37 +08001983 fh_init(current_fh, NFS4_FHSIZE);
1984 fh_init(save_fh, NFS4_FHSIZE);
NeilBrown8ff30fa2010-08-12 17:04:07 +10001985 /*
1986 * Don't use the deferral mechanism for NFSv4; compounds make it
1987 * too hard to avoid non-idempotency problems.
1988 */
Jeff Layton30660e04b2014-11-19 07:51:16 -05001989 clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991 /*
1992 * According to RFC3010, this takes precedence over all other errors.
1993 */
1994 status = nfserr_minor_vers_mismatch;
Trond Myklebuste333f3b2019-04-09 11:46:19 -04001995 if (nfsd_minorversion(nn, args->minorversion, NFSD_TEST) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 goto out;
J. Bruce Fields00781172017-11-15 12:30:27 -05001997 status = nfserr_resource;
1998 if (args->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
1999 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
J. Bruce Fields57716352010-04-21 12:27:19 -04002001 status = nfs41_check_op_ordering(args);
2002 if (status) {
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002003 op = &args->ops[0];
J. Bruce Fields57716352010-04-21 12:27:19 -04002004 op->status = status;
J. Bruce Fields5b7b15ae2018-06-13 15:21:35 -04002005 resp->opcnt = 1;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002006 goto encode_op;
2007 }
2008
Chuck Leverfff40802018-03-27 10:53:54 -04002009 trace_nfsd_compound(rqstp, args->opcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 while (!status && resp->opcnt < args->opcnt) {
2011 op = &args->ops[resp->opcnt++];
2012
2013 /*
2014 * The XDR decode routines may have pre-set op->status;
2015 * for example, if there is a miscellaneous XDR error
2016 * it will be set to nfserr_bad_xdr.
2017 */
J. Bruce Fields9d313b12013-02-28 12:51:49 -08002018 if (op->status) {
2019 if (op->opnum == OP_OPEN)
2020 op->status = nfsd4_open_omfg(rqstp, cstate, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 goto encode_op;
J. Bruce Fields9d313b12013-02-28 12:51:49 -08002022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Kinglong Mee4daeed22014-03-26 17:10:37 +08002024 if (!current_fh->fh_dentry) {
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002025 if (!(op->opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002026 op->status = nfserr_nofilehandle;
2027 goto encode_op;
2028 }
Kinglong Mee4daeed22014-03-26 17:10:37 +08002029 } else if (current_fh->fh_export->ex_fslocs.migrated &&
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002030 !(op->opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002031 op->status = nfserr_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 goto encode_op;
2033 }
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002034
Kinglong Mee23367452014-03-29 10:23:47 +08002035 fh_clear_wcc(current_fh);
2036
Mi Jinlong58e7b332011-08-28 18:18:56 +08002037 /* If op is non-idempotent */
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002038 if (op->opdesc->op_flags & OP_MODIFIES_SOMETHING) {
J. Bruce Fields4c69d582014-01-28 16:01:04 -05002039 /*
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04002040 * Don't execute this op if we couldn't encode a
2041 * succesful reply:
2042 */
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002043 u32 plen = op->opdesc->op_rsize_bop(rqstp, op);
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04002044 /*
2045 * Plus if there's another operation, make sure
J. Bruce Fields4c69d582014-01-28 16:01:04 -05002046 * we'll have space to at least encode an error:
2047 */
2048 if (resp->opcnt < args->opcnt)
2049 plen += COMPOUND_ERR_SLACK_SPACE;
Mi Jinlong58e7b332011-08-28 18:18:56 +08002050 op->status = nfsd4_check_resp_size(resp, plen);
2051 }
2052
2053 if (op->status)
2054 goto encode_op;
2055
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002056 if (op->opdesc->op_get_currentstateid)
2057 op->opdesc->op_get_currentstateid(cstate, &op->u);
2058 op->status = op->opdesc->op_func(rqstp, cstate, &op->u);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
J. Bruce Fields9a307402017-05-23 12:24:40 -04002060 /* Only from SEQUENCE */
2061 if (cstate->status == nfserr_replay_cache) {
2062 dprintk("%s NFS4.1 replay from cache\n", __func__);
2063 status = op->status;
2064 goto out;
2065 }
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01002066 if (!op->status) {
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002067 if (op->opdesc->op_set_currentstateid)
2068 op->opdesc->op_set_currentstateid(cstate, &op->u);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01002069
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002070 if (op->opdesc->op_flags & OP_CLEAR_STATEID)
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01002071 clear_current_stateid(cstate);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01002072
2073 if (need_wrongsec_check(rqstp))
Kinglong Mee4daeed22014-03-26 17:10:37 +08002074 op->status = check_nfsd_access(current_fh->fh_export, rqstp);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01002075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076encode_op:
Al Viroa90b0612006-10-19 23:29:03 -07002077 if (op->status == nfserr_replay_me) {
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08002078 op->replay = &cstate->replay_owner->so_replay;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05002079 nfsd4_encode_replay(&resp->xdr, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 status = op->status = op->replay->rp_status;
2081 } else {
2082 nfsd4_encode_operation(resp, op);
2083 status = op->status;
2084 }
Benny Halevy04077172008-12-15 19:40:49 +02002085
Chuck Leverfff40802018-03-27 10:53:54 -04002086 trace_nfsd_compound_status(args->opcnt, resp->opcnt, status,
2087 nfsd4_op_name(op->opnum));
Benny Halevy04077172008-12-15 19:40:49 +02002088
Jeff Layton58fb12e2014-07-29 21:34:27 -04002089 nfsd4_cstate_clear_replay(cstate);
Shankar Anande2b20952006-07-10 04:45:44 -07002090 nfsd4_increment_op_stats(op->opnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
2092
Kinglong Mee4daeed22014-03-26 17:10:37 +08002093 cstate->status = status;
2094 fh_put(current_fh);
2095 fh_put(save_fh);
2096 BUG_ON(cstate->replay_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097out:
Andy Adamson2f425872009-04-03 08:27:32 +03002098 /* Reset deferral mechanism for RPC deferrals */
Jeff Layton30660e04b2014-11-19 07:51:16 -05002099 set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
J. Bruce Fields3b12cd92008-05-05 17:17:44 -04002100 dprintk("nfsv4 compound returned %d\n", ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 return status;
2102}
2103
Mi Jinlong58e7b332011-08-28 18:18:56 +08002104#define op_encode_hdr_size (2)
2105#define op_encode_stateid_maxsz (XDR_QUADLEN(NFS4_STATEID_SIZE))
2106#define op_encode_verifier_maxsz (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
2107#define op_encode_change_info_maxsz (5)
2108#define nfs4_fattr_bitmap_maxsz (4)
2109
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04002110/* We'll fall back on returning no lockowner if run out of space: */
2111#define op_encode_lockowner_maxsz (0)
Mi Jinlong58e7b332011-08-28 18:18:56 +08002112#define op_encode_lock_denied_maxsz (8 + op_encode_lockowner_maxsz)
2113
2114#define nfs4_owner_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
2115
2116#define op_encode_ace_maxsz (3 + nfs4_owner_maxsz)
2117#define op_encode_delegation_maxsz (1 + op_encode_stateid_maxsz + 1 + \
2118 op_encode_ace_maxsz)
2119
2120#define op_encode_channel_attrs_maxsz (6 + 1 + 1)
2121
2122static inline u32 nfsd4_only_status_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2123{
2124 return (op_encode_hdr_size) * sizeof(__be32);
2125}
2126
2127static inline u32 nfsd4_status_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2128{
2129 return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
2130}
2131
Kinglong Mee2282cd22017-02-03 22:36:00 +08002132static inline u32 nfsd4_access_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2133{
2134 /* ac_supported, ac_resp_access */
2135 return (op_encode_hdr_size + 2)* sizeof(__be32);
2136}
2137
Mi Jinlong58e7b332011-08-28 18:18:56 +08002138static inline u32 nfsd4_commit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2139{
2140 return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
2141}
2142
2143static inline u32 nfsd4_create_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2144{
2145 return (op_encode_hdr_size + op_encode_change_info_maxsz
2146 + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
2147}
2148
J. Bruce Fieldsb86cef62014-03-23 12:01:48 -04002149/*
2150 * Note since this is an idempotent operation we won't insist on failing
2151 * the op prematurely if the estimate is too large. We may turn off splice
2152 * reads unnecessarily.
2153 */
2154static inline u32 nfsd4_getattr_rsize(struct svc_rqst *rqstp,
2155 struct nfsd4_op *op)
2156{
2157 u32 *bmap = op->u.getattr.ga_bmval;
2158 u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
2159 u32 ret = 0;
2160
2161 if (bmap0 & FATTR4_WORD0_ACL)
2162 return svc_max_payload(rqstp);
2163 if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
2164 return svc_max_payload(rqstp);
2165
2166 if (bmap1 & FATTR4_WORD1_OWNER) {
2167 ret += IDMAP_NAMESZ + 4;
2168 bmap1 &= ~FATTR4_WORD1_OWNER;
2169 }
2170 if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
2171 ret += IDMAP_NAMESZ + 4;
2172 bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
2173 }
2174 if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
2175 ret += NFS4_FHSIZE + 4;
2176 bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
2177 }
2178 if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
Kinglong Mee1ec8c0c2015-03-28 23:46:09 +08002179 ret += NFS4_MAXLABELLEN + 12;
J. Bruce Fieldsb86cef62014-03-23 12:01:48 -04002180 bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2181 }
2182 /*
2183 * Largest of remaining attributes are 16 bytes (e.g.,
2184 * supported_attributes)
2185 */
2186 ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
2187 /* bitmask, length */
2188 ret += 20;
2189 return ret;
2190}
2191
Kinglong Mee2282cd22017-02-03 22:36:00 +08002192static inline u32 nfsd4_getfh_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2193{
2194 return (op_encode_hdr_size + 1) * sizeof(__be32) + NFS4_FHSIZE;
2195}
2196
Mi Jinlong58e7b332011-08-28 18:18:56 +08002197static inline u32 nfsd4_link_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2198{
2199 return (op_encode_hdr_size + op_encode_change_info_maxsz)
2200 * sizeof(__be32);
2201}
2202
2203static inline u32 nfsd4_lock_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2204{
2205 return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
2206 * sizeof(__be32);
2207}
2208
2209static inline u32 nfsd4_open_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2210{
2211 return (op_encode_hdr_size + op_encode_stateid_maxsz
2212 + op_encode_change_info_maxsz + 1
2213 + nfs4_fattr_bitmap_maxsz
2214 + op_encode_delegation_maxsz) * sizeof(__be32);
2215}
2216
2217static inline u32 nfsd4_read_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2218{
2219 u32 maxcount = 0, rlen = 0;
2220
2221 maxcount = svc_max_payload(rqstp);
Kinglong Mee3c7aa152014-06-10 18:08:19 +08002222 rlen = min(op->u.read.rd_length, maxcount);
Mi Jinlong58e7b332011-08-28 18:18:56 +08002223
J. Bruce Fields622f5602014-05-16 14:38:20 -04002224 return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
Mi Jinlong58e7b332011-08-28 18:18:56 +08002225}
2226
2227static inline u32 nfsd4_readdir_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2228{
Kinglong Mee3c7aa152014-06-10 18:08:19 +08002229 u32 maxcount = 0, rlen = 0;
Mi Jinlong58e7b332011-08-28 18:18:56 +08002230
Kinglong Mee3c7aa152014-06-10 18:08:19 +08002231 maxcount = svc_max_payload(rqstp);
2232 rlen = min(op->u.readdir.rd_maxcount, maxcount);
Mi Jinlong58e7b332011-08-28 18:18:56 +08002233
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05002234 return (op_encode_hdr_size + op_encode_verifier_maxsz +
2235 XDR_QUADLEN(rlen)) * sizeof(__be32);
Mi Jinlong58e7b332011-08-28 18:18:56 +08002236}
2237
Kinglong Mee2282cd22017-02-03 22:36:00 +08002238static inline u32 nfsd4_readlink_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2239{
2240 return (op_encode_hdr_size + 1) * sizeof(__be32) + PAGE_SIZE;
2241}
2242
Mi Jinlong58e7b332011-08-28 18:18:56 +08002243static inline u32 nfsd4_remove_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2244{
2245 return (op_encode_hdr_size + op_encode_change_info_maxsz)
2246 * sizeof(__be32);
2247}
2248
2249static inline u32 nfsd4_rename_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2250{
2251 return (op_encode_hdr_size + op_encode_change_info_maxsz
2252 + op_encode_change_info_maxsz) * sizeof(__be32);
2253}
2254
J. Bruce Fieldsccae70a2014-03-23 09:34:22 -07002255static inline u32 nfsd4_sequence_rsize(struct svc_rqst *rqstp,
2256 struct nfsd4_op *op)
2257{
J. Bruce Fieldsd1d84c92014-08-21 15:04:31 -04002258 return (op_encode_hdr_size
2259 + XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32);
J. Bruce Fieldsccae70a2014-03-23 09:34:22 -07002260}
2261
Kinglong Mee2282cd22017-02-03 22:36:00 +08002262static inline u32 nfsd4_test_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2263{
2264 return (op_encode_hdr_size + 1 + op->u.test_stateid.ts_num_ids)
2265 * sizeof(__be32);
2266}
2267
Mi Jinlong58e7b332011-08-28 18:18:56 +08002268static inline u32 nfsd4_setattr_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2269{
2270 return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
2271}
2272
Kinglong Mee2282cd22017-02-03 22:36:00 +08002273static inline u32 nfsd4_secinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2274{
2275 return (op_encode_hdr_size + RPC_AUTH_MAXFLAVOR *
2276 (4 + XDR_QUADLEN(GSS_OID_MAX_LEN))) * sizeof(__be32);
2277}
2278
Mi Jinlong58e7b332011-08-28 18:18:56 +08002279static inline u32 nfsd4_setclientid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2280{
J. Bruce Fields480efae2014-03-10 14:17:55 -04002281 return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
2282 sizeof(__be32);
Mi Jinlong58e7b332011-08-28 18:18:56 +08002283}
2284
2285static inline u32 nfsd4_write_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2286{
J. Bruce Fieldsf34e4322014-05-16 21:24:56 -04002287 return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
Mi Jinlong58e7b332011-08-28 18:18:56 +08002288}
2289
2290static inline u32 nfsd4_exchange_id_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2291{
2292 return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
Kinglong Meea8bb84b2013-12-10 15:24:36 +08002293 1 + 1 + /* eir_flags, spr_how */\
2294 4 + /* spo_must_enforce & _allow with bitmap */\
Mi Jinlong58e7b332011-08-28 18:18:56 +08002295 2 + /*eir_server_owner.so_minor_id */\
2296 /* eir_server_owner.so_major_id<> */\
2297 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
2298 /* eir_server_scope<> */\
2299 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
2300 1 + /* eir_server_impl_id array length */\
2301 0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
2302}
2303
2304static inline u32 nfsd4_bind_conn_to_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2305{
2306 return (op_encode_hdr_size + \
2307 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
2308 2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
2309}
2310
2311static inline u32 nfsd4_create_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2312{
2313 return (op_encode_hdr_size + \
2314 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
2315 2 + /* csr_sequence, csr_flags */\
2316 op_encode_channel_attrs_maxsz + \
2317 op_encode_channel_attrs_maxsz) * sizeof(__be32);
2318}
2319
Anna Schumaker29ae7f92016-09-07 15:57:30 -04002320static inline u32 nfsd4_copy_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2321{
2322 return (op_encode_hdr_size +
2323 1 /* wr_callback */ +
2324 op_encode_stateid_maxsz /* wr_callback */ +
2325 2 /* wr_count */ +
2326 1 /* wr_committed */ +
2327 op_encode_verifier_maxsz +
2328 1 /* cr_consecutive */ +
2329 1 /* cr_synchronous */) * sizeof(__be32);
2330}
2331
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04002332static inline u32 nfsd4_offload_status_rsize(struct svc_rqst *rqstp,
2333 struct nfsd4_op *op)
2334{
2335 return (op_encode_hdr_size +
2336 2 /* osr_count */ +
2337 1 /* osr_complete<1> optional 0 for now */) * sizeof(__be32);
2338}
2339
Olga Kornievskaia51911862019-08-08 11:14:59 -04002340static inline u32 nfsd4_copy_notify_rsize(struct svc_rqst *rqstp,
2341 struct nfsd4_op *op)
2342{
2343 return (op_encode_hdr_size +
2344 3 /* cnr_lease_time */ +
2345 1 /* We support one cnr_source_server */ +
2346 1 /* cnr_stateid seq */ +
2347 op_encode_stateid_maxsz /* cnr_stateid */ +
2348 1 /* num cnr_source_server*/ +
2349 1 /* nl4_type */ +
2350 1 /* nl4 size */ +
2351 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) /*nl4_loc + nl4_loc_sz */)
2352 * sizeof(__be32);
2353}
2354
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002355#ifdef CONFIG_NFSD_PNFS
Kinglong Mee2282cd22017-02-03 22:36:00 +08002356static inline u32 nfsd4_getdeviceinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2357{
2358 u32 maxcount = 0, rlen = 0;
2359
2360 maxcount = svc_max_payload(rqstp);
2361 rlen = min(op->u.getdeviceinfo.gd_maxcount, maxcount);
2362
2363 return (op_encode_hdr_size +
2364 1 /* gd_layout_type*/ +
2365 XDR_QUADLEN(rlen) +
2366 2 /* gd_notify_types */) * sizeof(__be32);
2367}
2368
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002369/*
2370 * At this stage we don't really know what layout driver will handle the request,
2371 * so we need to define an arbitrary upper bound here.
2372 */
2373#define MAX_LAYOUT_SIZE 128
2374static inline u32 nfsd4_layoutget_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2375{
2376 return (op_encode_hdr_size +
2377 1 /* logr_return_on_close */ +
2378 op_encode_stateid_maxsz +
2379 1 /* nr of layouts */ +
2380 MAX_LAYOUT_SIZE) * sizeof(__be32);
2381}
2382
2383static inline u32 nfsd4_layoutcommit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2384{
2385 return (op_encode_hdr_size +
2386 1 /* locr_newsize */ +
2387 2 /* ns_size */) * sizeof(__be32);
2388}
2389
2390static inline u32 nfsd4_layoutreturn_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2391{
2392 return (op_encode_hdr_size +
2393 1 /* lrs_stateid */ +
2394 op_encode_stateid_maxsz) * sizeof(__be32);
2395}
2396#endif /* CONFIG_NFSD_PNFS */
2397
Kinglong Mee2282cd22017-02-03 22:36:00 +08002398
2399static inline u32 nfsd4_seek_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2400{
2401 return (op_encode_hdr_size + 3) * sizeof(__be32);
2402}
2403
Christoph Hellwigbb2a8b02017-05-08 23:46:47 +02002404static const struct nfsd4_operation nfsd4_ops[] = {
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002405 [OP_ACCESS] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002406 .op_func = nfsd4_access,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002407 .op_name = "OP_ACCESS",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002408 .op_rsize_bop = nfsd4_access_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002409 },
2410 [OP_CLOSE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002411 .op_func = nfsd4_close,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002412 .op_flags = OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002413 .op_name = "OP_CLOSE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002414 .op_rsize_bop = nfsd4_status_stateid_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02002415 .op_get_currentstateid = nfsd4_get_closestateid,
Christoph Hellwigb60e9852017-05-08 20:03:15 +02002416 .op_set_currentstateid = nfsd4_set_closestateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002417 },
2418 [OP_COMMIT] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002419 .op_func = nfsd4_commit,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002420 .op_flags = OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002421 .op_name = "OP_COMMIT",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002422 .op_rsize_bop = nfsd4_commit_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002423 },
2424 [OP_CREATE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002425 .op_func = nfsd4_create,
Tigran Mkrtchyand1471052012-02-13 22:55:29 +01002426 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002427 .op_name = "OP_CREATE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002428 .op_rsize_bop = nfsd4_create_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002429 },
2430 [OP_DELEGRETURN] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002431 .op_func = nfsd4_delegreturn,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002432 .op_flags = OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002433 .op_name = "OP_DELEGRETURN",
Mi Jinlong58e7b332011-08-28 18:18:56 +08002434 .op_rsize_bop = nfsd4_only_status_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02002435 .op_get_currentstateid = nfsd4_get_delegreturnstateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002436 },
2437 [OP_GETATTR] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002438 .op_func = nfsd4_getattr,
J.Bruce Fieldseeac2942006-12-13 00:35:39 -08002439 .op_flags = ALLOWED_ON_ABSENT_FS,
J. Bruce Fieldsb86cef62014-03-23 12:01:48 -04002440 .op_rsize_bop = nfsd4_getattr_rsize,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002441 .op_name = "OP_GETATTR",
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002442 },
2443 [OP_GETFH] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002444 .op_func = nfsd4_getfh,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002445 .op_name = "OP_GETFH",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002446 .op_rsize_bop = nfsd4_getfh_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002447 },
2448 [OP_LINK] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002449 .op_func = nfsd4_link,
J. Bruce Fieldsc8566942011-09-20 08:49:51 -04002450 .op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
2451 | OP_CACHEME,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002452 .op_name = "OP_LINK",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002453 .op_rsize_bop = nfsd4_link_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002454 },
2455 [OP_LOCK] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002456 .op_func = nfsd4_lock,
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04002457 .op_flags = OP_MODIFIES_SOMETHING |
2458 OP_NONTRIVIAL_ERROR_ENCODE,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002459 .op_name = "OP_LOCK",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002460 .op_rsize_bop = nfsd4_lock_rsize,
Christoph Hellwigb60e9852017-05-08 20:03:15 +02002461 .op_set_currentstateid = nfsd4_set_lockstateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002462 },
2463 [OP_LOCKT] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002464 .op_func = nfsd4_lockt,
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04002465 .op_flags = OP_NONTRIVIAL_ERROR_ENCODE,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002466 .op_name = "OP_LOCKT",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002467 .op_rsize_bop = nfsd4_lock_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002468 },
2469 [OP_LOCKU] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002470 .op_func = nfsd4_locku,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002471 .op_flags = OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002472 .op_name = "OP_LOCKU",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002473 .op_rsize_bop = nfsd4_status_stateid_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02002474 .op_get_currentstateid = nfsd4_get_lockustateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002475 },
2476 [OP_LOOKUP] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002477 .op_func = nfsd4_lookup,
Tigran Mkrtchyand1471052012-02-13 22:55:29 +01002478 .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002479 .op_name = "OP_LOOKUP",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002480 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002481 },
2482 [OP_LOOKUPP] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002483 .op_func = nfsd4_lookupp,
Tigran Mkrtchyand1471052012-02-13 22:55:29 +01002484 .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002485 .op_name = "OP_LOOKUPP",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002486 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002487 },
2488 [OP_NVERIFY] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002489 .op_func = nfsd4_nverify,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002490 .op_name = "OP_NVERIFY",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002491 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002492 },
2493 [OP_OPEN] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002494 .op_func = nfsd4_open,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002495 .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002496 .op_name = "OP_OPEN",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002497 .op_rsize_bop = nfsd4_open_rsize,
Christoph Hellwigb60e9852017-05-08 20:03:15 +02002498 .op_set_currentstateid = nfsd4_set_openstateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002499 },
2500 [OP_OPEN_CONFIRM] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002501 .op_func = nfsd4_open_confirm,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002502 .op_flags = OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002503 .op_name = "OP_OPEN_CONFIRM",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002504 .op_rsize_bop = nfsd4_status_stateid_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002505 },
2506 [OP_OPEN_DOWNGRADE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002507 .op_func = nfsd4_open_downgrade,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002508 .op_flags = OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002509 .op_name = "OP_OPEN_DOWNGRADE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002510 .op_rsize_bop = nfsd4_status_stateid_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02002511 .op_get_currentstateid = nfsd4_get_opendowngradestateid,
Christoph Hellwigb60e9852017-05-08 20:03:15 +02002512 .op_set_currentstateid = nfsd4_set_opendowngradestateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002513 },
2514 [OP_PUTFH] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002515 .op_func = nfsd4_putfh,
J. Bruce Fields68d93182011-04-08 17:00:50 -04002516 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
J. Bruce Fields5b648692014-03-07 11:43:58 -05002517 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002518 .op_name = "OP_PUTFH",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002519 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002520 },
J.Bruce Fieldseeac2942006-12-13 00:35:39 -08002521 [OP_PUTPUBFH] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002522 .op_func = nfsd4_putrootfh,
J. Bruce Fields68d93182011-04-08 17:00:50 -04002523 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
J. Bruce Fields5b648692014-03-07 11:43:58 -05002524 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002525 .op_name = "OP_PUTPUBFH",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002526 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldseeac2942006-12-13 00:35:39 -08002527 },
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002528 [OP_PUTROOTFH] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002529 .op_func = nfsd4_putrootfh,
J. Bruce Fields68d93182011-04-08 17:00:50 -04002530 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
J. Bruce Fields5b648692014-03-07 11:43:58 -05002531 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002532 .op_name = "OP_PUTROOTFH",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002533 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002534 },
2535 [OP_READ] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002536 .op_func = nfsd4_read,
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04002537 .op_release = nfsd4_read_release,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002538 .op_name = "OP_READ",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002539 .op_rsize_bop = nfsd4_read_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02002540 .op_get_currentstateid = nfsd4_get_readstateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002541 },
2542 [OP_READDIR] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002543 .op_func = nfsd4_readdir,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002544 .op_name = "OP_READDIR",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002545 .op_rsize_bop = nfsd4_readdir_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002546 },
2547 [OP_READLINK] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002548 .op_func = nfsd4_readlink,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002549 .op_name = "OP_READLINK",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002550 .op_rsize_bop = nfsd4_readlink_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002551 },
2552 [OP_REMOVE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002553 .op_func = nfsd4_remove,
J. Bruce Fieldsc8566942011-09-20 08:49:51 -04002554 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002555 .op_name = "OP_REMOVE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002556 .op_rsize_bop = nfsd4_remove_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002557 },
2558 [OP_RENAME] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002559 .op_func = nfsd4_rename,
J. Bruce Fieldsc8566942011-09-20 08:49:51 -04002560 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002561 .op_name = "OP_RENAME",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002562 .op_rsize_bop = nfsd4_rename_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002563 },
2564 [OP_RENEW] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002565 .op_func = nfsd4_renew,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002566 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
2567 | OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002568 .op_name = "OP_RENEW",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002569 .op_rsize_bop = nfsd4_only_status_rsize,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002570
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002571 },
2572 [OP_RESTOREFH] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002573 .op_func = nfsd4_restorefh,
J. Bruce Fields68d93182011-04-08 17:00:50 -04002574 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
Mi Jinlong58e7b332011-08-28 18:18:56 +08002575 | OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002576 .op_name = "OP_RESTOREFH",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002577 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002578 },
2579 [OP_SAVEFH] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002580 .op_func = nfsd4_savefh,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002581 .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002582 .op_name = "OP_SAVEFH",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002583 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002584 },
Andy Adamsondcb488a32007-07-17 04:04:51 -07002585 [OP_SECINFO] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002586 .op_func = nfsd4_secinfo,
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04002587 .op_release = nfsd4_secinfo_release,
J. Bruce Fields68d93182011-04-08 17:00:50 -04002588 .op_flags = OP_HANDLES_WRONGSEC,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002589 .op_name = "OP_SECINFO",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002590 .op_rsize_bop = nfsd4_secinfo_rsize,
Andy Adamsondcb488a32007-07-17 04:04:51 -07002591 },
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002592 [OP_SETATTR] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002593 .op_func = nfsd4_setattr,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002594 .op_name = "OP_SETATTR",
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04002595 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME
2596 | OP_NONTRIVIAL_ERROR_ENCODE,
Christoph Hellwig1c122632017-05-08 20:42:10 +02002597 .op_rsize_bop = nfsd4_setattr_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02002598 .op_get_currentstateid = nfsd4_get_setattrstateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002599 },
2600 [OP_SETCLIENTID] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002601 .op_func = nfsd4_setclientid,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002602 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04002603 | OP_MODIFIES_SOMETHING | OP_CACHEME
2604 | OP_NONTRIVIAL_ERROR_ENCODE,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002605 .op_name = "OP_SETCLIENTID",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002606 .op_rsize_bop = nfsd4_setclientid_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002607 },
2608 [OP_SETCLIENTID_CONFIRM] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002609 .op_func = nfsd4_setclientid_confirm,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002610 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
J. Bruce Fieldsc8566942011-09-20 08:49:51 -04002611 | OP_MODIFIES_SOMETHING | OP_CACHEME,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002612 .op_name = "OP_SETCLIENTID_CONFIRM",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002613 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002614 },
2615 [OP_VERIFY] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002616 .op_func = nfsd4_verify,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002617 .op_name = "OP_VERIFY",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002618 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002619 },
2620 [OP_WRITE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002621 .op_func = nfsd4_write,
J. Bruce Fieldsc8566942011-09-20 08:49:51 -04002622 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002623 .op_name = "OP_WRITE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002624 .op_rsize_bop = nfsd4_write_rsize,
Christoph Hellwig57832e72017-05-08 20:37:33 +02002625 .op_get_currentstateid = nfsd4_get_writestateid,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002626 },
2627 [OP_RELEASE_LOCKOWNER] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002628 .op_func = nfsd4_release_lockowner,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002629 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
2630 | OP_MODIFIES_SOMETHING,
Benny Halevyb001a1b2008-07-02 11:15:03 +03002631 .op_name = "OP_RELEASE_LOCKOWNER",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002632 .op_rsize_bop = nfsd4_only_status_rsize,
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002633 },
Andy Adamson069b6ad2009-04-03 08:27:58 +03002634
2635 /* NFSv4.1 operations */
2636 [OP_EXCHANGE_ID] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002637 .op_func = nfsd4_exchange_id,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002638 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
2639 | OP_MODIFIES_SOMETHING,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002640 .op_name = "OP_EXCHANGE_ID",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002641 .op_rsize_bop = nfsd4_exchange_id_rsize,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002642 },
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002643 [OP_BACKCHANNEL_CTL] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002644 .op_func = nfsd4_backchannel_ctl,
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002645 .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
2646 .op_name = "OP_BACKCHANNEL_CTL",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002647 .op_rsize_bop = nfsd4_only_status_rsize,
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002648 },
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002649 [OP_BIND_CONN_TO_SESSION] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002650 .op_func = nfsd4_bind_conn_to_session,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002651 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
2652 | OP_MODIFIES_SOMETHING,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002653 .op_name = "OP_BIND_CONN_TO_SESSION",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002654 .op_rsize_bop = nfsd4_bind_conn_to_session_rsize,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002655 },
Andy Adamson069b6ad2009-04-03 08:27:58 +03002656 [OP_CREATE_SESSION] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002657 .op_func = nfsd4_create_session,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002658 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
2659 | OP_MODIFIES_SOMETHING,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002660 .op_name = "OP_CREATE_SESSION",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002661 .op_rsize_bop = nfsd4_create_session_rsize,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002662 },
2663 [OP_DESTROY_SESSION] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002664 .op_func = nfsd4_destroy_session,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002665 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
2666 | OP_MODIFIES_SOMETHING,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002667 .op_name = "OP_DESTROY_SESSION",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002668 .op_rsize_bop = nfsd4_only_status_rsize,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002669 },
2670 [OP_SEQUENCE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002671 .op_func = nfsd4_sequence,
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002672 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002673 .op_name = "OP_SEQUENCE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002674 .op_rsize_bop = nfsd4_sequence_rsize,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002675 },
Benny Halevy094b5d72011-06-16 11:39:10 -04002676 [OP_DESTROY_CLIENTID] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002677 .op_func = nfsd4_destroy_clientid,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002678 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
2679 | OP_MODIFIES_SOMETHING,
Benny Halevy094b5d72011-06-16 11:39:10 -04002680 .op_name = "OP_DESTROY_CLIENTID",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002681 .op_rsize_bop = nfsd4_only_status_rsize,
Benny Halevy094b5d72011-06-16 11:39:10 -04002682 },
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002683 [OP_RECLAIM_COMPLETE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002684 .op_func = nfsd4_reclaim_complete,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002685 .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002686 .op_name = "OP_RECLAIM_COMPLETE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002687 .op_rsize_bop = nfsd4_only_status_rsize,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002688 },
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05002689 [OP_SECINFO_NO_NAME] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002690 .op_func = nfsd4_secinfo_no_name,
Eryu Guanec572b92017-09-29 15:01:10 +08002691 .op_release = nfsd4_secinfo_no_name_release,
J. Bruce Fields68d93182011-04-08 17:00:50 -04002692 .op_flags = OP_HANDLES_WRONGSEC,
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05002693 .op_name = "OP_SECINFO_NO_NAME",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002694 .op_rsize_bop = nfsd4_secinfo_rsize,
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05002695 },
Bryan Schumaker17456802011-07-13 10:50:48 -04002696 [OP_TEST_STATEID] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002697 .op_func = nfsd4_test_stateid,
Bryan Schumaker17456802011-07-13 10:50:48 -04002698 .op_flags = ALLOWED_WITHOUT_FH,
2699 .op_name = "OP_TEST_STATEID",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002700 .op_rsize_bop = nfsd4_test_stateid_rsize,
Bryan Schumaker17456802011-07-13 10:50:48 -04002701 },
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04002702 [OP_FREE_STATEID] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002703 .op_func = nfsd4_free_stateid,
Mi Jinlong58e7b332011-08-28 18:18:56 +08002704 .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04002705 .op_name = "OP_FREE_STATEID",
Christoph Hellwig57832e72017-05-08 20:37:33 +02002706 .op_get_currentstateid = nfsd4_get_freestateid,
Christoph Hellwig1c122632017-05-08 20:42:10 +02002707 .op_rsize_bop = nfsd4_only_status_rsize,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04002708 },
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002709#ifdef CONFIG_NFSD_PNFS
2710 [OP_GETDEVICEINFO] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002711 .op_func = nfsd4_getdeviceinfo,
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04002712 .op_release = nfsd4_getdeviceinfo_release,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002713 .op_flags = ALLOWED_WITHOUT_FH,
2714 .op_name = "OP_GETDEVICEINFO",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002715 .op_rsize_bop = nfsd4_getdeviceinfo_rsize,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002716 },
2717 [OP_LAYOUTGET] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002718 .op_func = nfsd4_layoutget,
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04002719 .op_release = nfsd4_layoutget_release,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002720 .op_flags = OP_MODIFIES_SOMETHING,
2721 .op_name = "OP_LAYOUTGET",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002722 .op_rsize_bop = nfsd4_layoutget_rsize,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002723 },
2724 [OP_LAYOUTCOMMIT] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002725 .op_func = nfsd4_layoutcommit,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002726 .op_flags = OP_MODIFIES_SOMETHING,
2727 .op_name = "OP_LAYOUTCOMMIT",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002728 .op_rsize_bop = nfsd4_layoutcommit_rsize,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002729 },
2730 [OP_LAYOUTRETURN] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002731 .op_func = nfsd4_layoutreturn,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002732 .op_flags = OP_MODIFIES_SOMETHING,
2733 .op_name = "OP_LAYOUTRETURN",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002734 .op_rsize_bop = nfsd4_layoutreturn_rsize,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002735 },
2736#endif /* CONFIG_NFSD_PNFS */
Anna Schumaker24bab492014-09-26 13:58:27 -04002737
2738 /* NFSv4.2 operations */
Anna Schumaker95d871f2014-11-07 14:44:26 -05002739 [OP_ALLOCATE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002740 .op_func = nfsd4_allocate,
Olga Kornievskaia03b31f42018-12-04 14:09:21 -05002741 .op_flags = OP_MODIFIES_SOMETHING,
Anna Schumaker95d871f2014-11-07 14:44:26 -05002742 .op_name = "OP_ALLOCATE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002743 .op_rsize_bop = nfsd4_only_status_rsize,
Anna Schumaker95d871f2014-11-07 14:44:26 -05002744 },
Anna Schumakerb0cb9082014-11-07 14:44:27 -05002745 [OP_DEALLOCATE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002746 .op_func = nfsd4_deallocate,
Olga Kornievskaia03b31f42018-12-04 14:09:21 -05002747 .op_flags = OP_MODIFIES_SOMETHING,
Anna Schumakerb0cb9082014-11-07 14:44:27 -05002748 .op_name = "OP_DEALLOCATE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002749 .op_rsize_bop = nfsd4_only_status_rsize,
Anna Schumakerb0cb9082014-11-07 14:44:27 -05002750 },
Christoph Hellwigffa01602015-12-03 12:59:52 +01002751 [OP_CLONE] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002752 .op_func = nfsd4_clone,
Olga Kornievskaia03b31f42018-12-04 14:09:21 -05002753 .op_flags = OP_MODIFIES_SOMETHING,
Christoph Hellwigffa01602015-12-03 12:59:52 +01002754 .op_name = "OP_CLONE",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002755 .op_rsize_bop = nfsd4_only_status_rsize,
Christoph Hellwigffa01602015-12-03 12:59:52 +01002756 },
Anna Schumaker29ae7f92016-09-07 15:57:30 -04002757 [OP_COPY] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002758 .op_func = nfsd4_copy,
Olga Kornievskaia03b31f42018-12-04 14:09:21 -05002759 .op_flags = OP_MODIFIES_SOMETHING,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04002760 .op_name = "OP_COPY",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002761 .op_rsize_bop = nfsd4_copy_rsize,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04002762 },
Anna Schumaker24bab492014-09-26 13:58:27 -04002763 [OP_SEEK] = {
Christoph Hellwigeb698532017-05-08 20:58:35 +02002764 .op_func = nfsd4_seek,
Anna Schumaker24bab492014-09-26 13:58:27 -04002765 .op_name = "OP_SEEK",
Christoph Hellwig1c122632017-05-08 20:42:10 +02002766 .op_rsize_bop = nfsd4_seek_rsize,
Anna Schumaker24bab492014-09-26 13:58:27 -04002767 },
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04002768 [OP_OFFLOAD_STATUS] = {
2769 .op_func = nfsd4_offload_status,
2770 .op_name = "OP_OFFLOAD_STATUS",
2771 .op_rsize_bop = nfsd4_offload_status_rsize,
2772 },
Olga Kornievskaia885e2bf2018-07-20 18:19:19 -04002773 [OP_OFFLOAD_CANCEL] = {
2774 .op_func = nfsd4_offload_cancel,
2775 .op_flags = OP_MODIFIES_SOMETHING,
2776 .op_name = "OP_OFFLOAD_CANCEL",
2777 .op_rsize_bop = nfsd4_only_status_rsize,
2778 },
Olga Kornievskaia51911862019-08-08 11:14:59 -04002779 [OP_COPY_NOTIFY] = {
2780 .op_func = nfsd4_copy_notify,
2781 .op_flags = OP_MODIFIES_SOMETHING,
2782 .op_name = "OP_COPY_NOTIFY",
2783 .op_rsize_bop = nfsd4_copy_notify_rsize,
2784 },
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002785};
2786
Andrew Elbleed941642016-06-15 12:52:09 -04002787/**
2788 * nfsd4_spo_must_allow - Determine if the compound op contains an
2789 * operation that is allowed to be sent with machine credentials
2790 *
2791 * @rqstp: a pointer to the struct svc_rqst
2792 *
2793 * Checks to see if the compound contains a spo_must_allow op
2794 * and confirms that it was sent with the proper machine creds.
2795 */
2796
2797bool nfsd4_spo_must_allow(struct svc_rqst *rqstp)
2798{
2799 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2800 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
2801 struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
2802 struct nfsd4_compound_state *cstate = &resp->cstate;
2803 struct nfs4_op_map *allow = &cstate->clp->cl_spo_must_allow;
2804 u32 opiter;
2805
2806 if (!cstate->minorversion)
2807 return false;
2808
2809 if (cstate->spo_must_allowed == true)
2810 return true;
2811
2812 opiter = resp->opcnt;
2813 while (opiter < argp->opcnt) {
2814 this = &argp->ops[opiter++];
2815 if (test_bit(this->opnum, allow->u.longs) &&
2816 cstate->clp->cl_mach_cred &&
2817 nfsd4_mach_creds_match(cstate->clp, rqstp)) {
2818 cstate->spo_must_allowed = true;
2819 return true;
2820 }
2821 }
2822 cstate->spo_must_allowed = false;
2823 return false;
2824}
2825
J. Bruce Fields4f0cefb2014-03-11 15:39:13 -04002826int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
2827{
Olga Kornievskaia05b72782017-03-23 14:36:20 -04002828 if (op->opnum == OP_ILLEGAL || op->status == nfserr_notsupp)
J. Bruce Fields4f0cefb2014-03-11 15:39:13 -04002829 return op_encode_hdr_size * sizeof(__be32);
Kinglong Mee2282cd22017-02-03 22:36:00 +08002830
2831 BUG_ON(OPDESC(op)->op_rsize_bop == NULL);
2832 return OPDESC(op)->op_rsize_bop(rqstp, op);
J. Bruce Fields4f0cefb2014-03-11 15:39:13 -04002833}
2834
J. Bruce Fields07d1f802014-03-06 20:39:29 -05002835void warn_on_nonidempotent_op(struct nfsd4_op *op)
2836{
2837 if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
2838 pr_err("unable to encode reply to nonidempotent op %d (%s)\n",
2839 op->opnum, nfsd4_op_name(op->opnum));
2840 WARN_ON_ONCE(1);
2841 }
2842}
2843
Adrian Bunkf1c7f792008-08-08 19:26:42 +03002844static const char *nfsd4_op_name(unsigned opnum)
Benny Halevyb001a1b2008-07-02 11:15:03 +03002845{
2846 if (opnum < ARRAY_SIZE(nfsd4_ops))
2847 return nfsd4_ops[opnum].op_name;
2848 return "unknown_operation";
2849}
2850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851#define nfsd4_voidres nfsd4_voidargs
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852struct nfsd4_voidargs { int dummy; };
2853
Christoph Hellwig860bda22017-05-12 16:11:49 +02002854static const struct svc_procedure nfsd_procedures4[2] = {
Yu Zhiguo0a93a472009-05-19 14:09:54 +08002855 [NFSPROC4_NULL] = {
Christoph Hellwigf7235b62017-05-08 17:59:13 +02002856 .pc_func = nfsd4_proc_null,
Christoph Hellwig63f8de32017-05-08 19:42:02 +02002857 .pc_encode = nfs4svc_encode_voidres,
Yu Zhiguo0a93a472009-05-19 14:09:54 +08002858 .pc_argsize = sizeof(struct nfsd4_voidargs),
2859 .pc_ressize = sizeof(struct nfsd4_voidres),
2860 .pc_cachetype = RC_NOCACHE,
2861 .pc_xdrressize = 1,
2862 },
2863 [NFSPROC4_COMPOUND] = {
Christoph Hellwigf7235b62017-05-08 17:59:13 +02002864 .pc_func = nfsd4_proc_compound,
Christoph Hellwig026fec72017-05-08 19:01:48 +02002865 .pc_decode = nfs4svc_decode_compoundargs,
Christoph Hellwig63f8de32017-05-08 19:42:02 +02002866 .pc_encode = nfs4svc_encode_compoundres,
Yu Zhiguo0a93a472009-05-19 14:09:54 +08002867 .pc_argsize = sizeof(struct nfsd4_compoundargs),
2868 .pc_ressize = sizeof(struct nfsd4_compoundres),
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04002869 .pc_release = nfsd4_release_compoundargs,
Yu Zhiguo0a93a472009-05-19 14:09:54 +08002870 .pc_cachetype = RC_NOCACHE,
2871 .pc_xdrressize = NFSD_BUFSIZE/4,
2872 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873};
2874
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02002875static unsigned int nfsd_count3[ARRAY_SIZE(nfsd_procedures4)];
Christoph Hellwige9679182017-05-12 16:21:37 +02002876const struct svc_version nfsd_version4 = {
Jeff Layton5283b032017-02-24 13:25:24 -05002877 .vs_vers = 4,
2878 .vs_nproc = 2,
2879 .vs_proc = nfsd_procedures4,
Christoph Hellwig7fd38af2017-05-08 23:40:27 +02002880 .vs_count = nfsd_count3,
Jeff Layton5283b032017-02-24 13:25:24 -05002881 .vs_dispatch = nfsd_dispatch,
2882 .vs_xdrsize = NFS4_SVC_XDRSIZE,
2883 .vs_rpcb_optnl = true,
2884 .vs_need_cong_ctrl = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885};
2886
2887/*
2888 * Local variables:
2889 * c-basic-offset: 8
2890 * End:
2891 */