]> git.ipfire.org Git - people/ms/linux.git/blame - fs/nfsd/nfs4proc.c
Merge branch 'for-6.0/dax' into libnvdimm-fixes
[people/ms/linux.git] / fs / nfsd / nfs4proc.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * 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.
1da177e4 34 */
880a3a53 35#include <linux/fs_struct.h>
7e06b7f9 36#include <linux/file.h>
b0cb9085 37#include <linux/falloc.h>
5a0e3ad6 38#include <linux/slab.h>
e0639dc5 39#include <linux/kthread.h>
254454a5
CL
40#include <linux/namei.h>
41
624322f1 42#include <linux/sunrpc/addr.h>
0cfcd405 43#include <linux/nfs_ssc.h>
1da177e4 44
58e7b33a 45#include "idmap.h"
9a74af21
BH
46#include "cache.h"
47#include "xdr4.h"
0a3adade 48#include "vfs.h"
8b70484c 49#include "current_stateid.h"
3320fef1 50#include "netns.h"
4ac7249e 51#include "acl.h"
9cf514cc 52#include "pnfs.h"
31ef83dc 53#include "trace.h"
1da177e4 54
d6c9e436
CL
55static bool inter_copy_offload_enable;
56module_param(inter_copy_offload_enable, bool, 0644);
57MODULE_PARM_DESC(inter_copy_offload_enable,
58 "Enable inter server to server copy offload. Default: false");
59
f4e44b39
DN
60#ifdef CONFIG_NFSD_V4_2_INTER_SSC
61static int nfsd4_ssc_umount_timeout = 900000; /* default to 15 mins */
62module_param(nfsd4_ssc_umount_timeout, int, 0644);
63MODULE_PARM_DESC(nfsd4_ssc_umount_timeout,
64 "idle msecs before unmount export from source server");
65#endif
66
1da177e4
LT
67#define NFSDDBG_FACILITY NFSDDBG_PROC
68
3c8e0316
YZ
69static u32 nfsd_attrmask[] = {
70 NFSD_WRITEABLE_ATTRS_WORD0,
71 NFSD_WRITEABLE_ATTRS_WORD1,
72 NFSD_WRITEABLE_ATTRS_WORD2
73};
74
75static u32 nfsd41_ex_attrmask[] = {
76 NFSD_SUPPATTR_EXCLCREAT_WORD0,
77 NFSD_SUPPATTR_EXCLCREAT_WORD1,
78 NFSD_SUPPATTR_EXCLCREAT_WORD2
79};
80
81static __be32
82check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
83 u32 *bmval, u32 *writable)
84{
85 struct dentry *dentry = cstate->current_fh.fh_dentry;
32ddd944 86 struct svc_export *exp = cstate->current_fh.fh_export;
3c8e0316 87
916d2d84 88 if (!nfsd_attrs_supported(cstate->minorversion, bmval))
3c8e0316 89 return nfserr_attrnotsupp;
916d2d84
BF
90 if ((bmval[0] & FATTR4_WORD0_ACL) && !IS_POSIXACL(d_inode(dentry)))
91 return nfserr_attrnotsupp;
32ddd944
BF
92 if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) &&
93 !(exp->ex_flags & NFSEXP_SECURITY_LABEL))
94 return nfserr_attrnotsupp;
916d2d84
BF
95 if (writable && !bmval_is_subset(bmval, writable))
96 return nfserr_inval;
47057abd
AG
97 if (writable && (bmval[2] & FATTR4_WORD2_MODE_UMASK) &&
98 (bmval[1] & FATTR4_WORD1_MODE))
99 return nfserr_inval;
3c8e0316
YZ
100 return nfs_ok;
101}
102
103static __be32
104nfsd4_check_open_attributes(struct svc_rqst *rqstp,
105 struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
106{
107 __be32 status = nfs_ok;
108
109 if (open->op_create == NFS4_OPEN_CREATE) {
110 if (open->op_createmode == NFS4_CREATE_UNCHECKED
111 || open->op_createmode == NFS4_CREATE_GUARDED)
112 status = check_attr_support(rqstp, cstate,
113 open->op_bmval, nfsd_attrmask);
114 else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1)
115 status = check_attr_support(rqstp, cstate,
116 open->op_bmval, nfsd41_ex_attrmask);
117 }
118
119 return status;
120}
121
9208faf2
YZ
122static int
123is_create_with_attrs(struct nfsd4_open *open)
124{
125 return open->op_create == NFS4_OPEN_CREATE
126 && (open->op_createmode == NFS4_CREATE_UNCHECKED
127 || open->op_createmode == NFS4_CREATE_GUARDED
128 || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1);
129}
130
1da177e4
LT
131static inline void
132fh_dup2(struct svc_fh *dst, struct svc_fh *src)
133{
134 fh_put(dst);
135 dget(src->fh_dentry);
136 if (src->fh_export)
bf18f163 137 exp_get(src->fh_export);
1da177e4
LT
138 *dst = *src;
139}
140
b37ad28b 141static __be32
dc730e17 142do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
1da177e4 143{
b37ad28b 144 __be32 status;
1da177e4
LT
145
146 if (open->op_truncate &&
147 !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
148 return nfserr_inval;
149
a043226b
BF
150 accmode |= NFSD_MAY_READ_IF_EXEC;
151
1da177e4 152 if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
8837abca 153 accmode |= NFSD_MAY_READ;
9801d8a3 154 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
8837abca 155 accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC);
57ecb34f 156 if (open->op_share_deny & NFS4_SHARE_DENY_READ)
8837abca 157 accmode |= NFSD_MAY_WRITE;
1da177e4
LT
158
159 status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
160
161 return status;
162}
163
aadab6c6
BF
164static __be32 nfsd_check_obj_isreg(struct svc_fh *fh)
165{
2b0143b5 166 umode_t mode = d_inode(fh->fh_dentry)->i_mode;
aadab6c6
BF
167
168 if (S_ISREG(mode))
169 return nfs_ok;
170 if (S_ISDIR(mode))
171 return nfserr_isdir;
172 /*
173 * Using err_symlink as our catch-all case may look odd; but
174 * there's no other obvious error for this case in 4.0, and we
175 * happen to know that it will cause the linux v4 client to do
176 * the right thing on attempts to open something other than a
177 * regular file.
178 */
179 return nfserr_symlink;
180}
181
bbc9c36c
BF
182static void nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh *resfh)
183{
184 if (nfsd4_has_session(cstate))
185 return;
186 fh_copy_shallow(&open->op_openowner->oo_owner.so_replay.rp_openfh,
187 &resfh->fh_handle);
188}
189
254454a5
CL
190static inline bool nfsd4_create_is_exclusive(int createmode)
191{
192 return createmode == NFS4_CREATE_EXCLUSIVE ||
193 createmode == NFS4_CREATE_EXCLUSIVE4_1;
194}
195
fb70bf12
CL
196static __be32
197nfsd4_vfs_create(struct svc_fh *fhp, struct dentry *child,
198 struct nfsd4_open *open)
199{
200 struct file *filp;
201 struct path path;
202 int oflags;
203
204 oflags = O_CREAT | O_LARGEFILE;
205 switch (open->op_share_access & NFS4_SHARE_ACCESS_BOTH) {
206 case NFS4_SHARE_ACCESS_WRITE:
207 oflags |= O_WRONLY;
208 break;
209 case NFS4_SHARE_ACCESS_BOTH:
210 oflags |= O_RDWR;
211 break;
212 default:
213 oflags |= O_RDONLY;
214 }
215
216 path.mnt = fhp->fh_export->ex_path.mnt;
217 path.dentry = child;
218 filp = dentry_create(&path, oflags, open->op_iattr.ia_mode,
219 current_cred());
220 if (IS_ERR(filp))
221 return nfserrno(PTR_ERR(filp));
222
223 open->op_filp = filp;
224 return nfs_ok;
225}
226
254454a5
CL
227/*
228 * Implement NFSv4's unchecked, guarded, and exclusive create
229 * semantics for regular files. Open state for this new file is
230 * subsequently fabricated in nfsd4_process_open2().
231 *
232 * Upon return, caller must release @fhp and @resfhp.
233 */
234static __be32
235nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
236 struct svc_fh *resfhp, struct nfsd4_open *open)
237{
238 struct iattr *iap = &open->op_iattr;
7fe2a71d
N
239 struct nfsd_attrs attrs = {
240 .na_iattr = iap,
d6a97d3f 241 .na_seclabel = &open->op_label,
7fe2a71d 242 };
254454a5
CL
243 struct dentry *parent, *child;
244 __u32 v_mtime, v_atime;
245 struct inode *inode;
246 __be32 status;
247 int host_err;
248
249 if (isdotent(open->op_fname, open->op_fnamelen))
250 return nfserr_exist;
251 if (!(iap->ia_valid & ATTR_MODE))
252 iap->ia_mode = 0;
253
254 status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
255 if (status != nfs_ok)
256 return status;
257 parent = fhp->fh_dentry;
258 inode = d_inode(parent);
259
260 host_err = fh_want_write(fhp);
261 if (host_err)
262 return nfserrno(host_err);
263
c0cbe707
N
264 if (is_create_with_attrs(open))
265 nfsd4_acl_to_attr(NF4REG, open->op_acl, &attrs);
266
debf16f0 267 inode_lock_nested(inode, I_MUTEX_PARENT);
254454a5
CL
268
269 child = lookup_one_len(open->op_fname, parent, open->op_fnamelen);
270 if (IS_ERR(child)) {
271 status = nfserrno(PTR_ERR(child));
272 goto out;
273 }
274
275 if (d_really_is_negative(child)) {
276 status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
277 if (status != nfs_ok)
278 goto out;
279 }
280
281 status = fh_compose(resfhp, fhp->fh_export, child, fhp);
282 if (status != nfs_ok)
283 goto out;
284
285 v_mtime = 0;
286 v_atime = 0;
287 if (nfsd4_create_is_exclusive(open->op_createmode)) {
288 u32 *verifier = (u32 *)open->op_verf.data;
289
290 /*
291 * Solaris 7 gets confused (bugid 4218508) if these have
292 * the high bit set, as do xfs filesystems without the
293 * "bigtime" feature. So just clear the high bits. If this
294 * is ever changed to use different attrs for storing the
295 * verifier, then do_open_lookup() will also need to be
296 * fixed accordingly.
297 */
298 v_mtime = verifier[0] & 0x7fffffff;
299 v_atime = verifier[1] & 0x7fffffff;
300 }
301
302 if (d_really_is_positive(child)) {
303 status = nfs_ok;
304
19d008b4
N
305 /* NFSv4 protocol requires change attributes even though
306 * no change happened.
307 */
308 fh_fill_both_attrs(fhp);
309
254454a5
CL
310 switch (open->op_createmode) {
311 case NFS4_CREATE_UNCHECKED:
312 if (!d_is_reg(child))
313 break;
314
315 /*
316 * In NFSv4, we don't want to truncate the file
317 * now. This would be wrong if the OPEN fails for
318 * some other reason. Furthermore, if the size is
319 * nonzero, we should ignore it according to spec!
320 */
321 open->op_truncate = (iap->ia_valid & ATTR_SIZE) &&
322 !iap->ia_size;
323 break;
324 case NFS4_CREATE_GUARDED:
325 status = nfserr_exist;
326 break;
327 case NFS4_CREATE_EXCLUSIVE:
328 if (d_inode(child)->i_mtime.tv_sec == v_mtime &&
329 d_inode(child)->i_atime.tv_sec == v_atime &&
330 d_inode(child)->i_size == 0) {
331 open->op_created = true;
332 break; /* subtle */
333 }
334 status = nfserr_exist;
335 break;
336 case NFS4_CREATE_EXCLUSIVE4_1:
337 if (d_inode(child)->i_mtime.tv_sec == v_mtime &&
338 d_inode(child)->i_atime.tv_sec == v_atime &&
339 d_inode(child)->i_size == 0) {
340 open->op_created = true;
341 goto set_attr; /* subtle */
342 }
343 status = nfserr_exist;
344 }
345 goto out;
346 }
347
348 if (!IS_POSIXACL(inode))
349 iap->ia_mode &= ~current_umask();
350
debf16f0 351 fh_fill_pre_attrs(fhp);
fb70bf12
CL
352 status = nfsd4_vfs_create(fhp, child, open);
353 if (status != nfs_ok)
254454a5 354 goto out;
254454a5 355 open->op_created = true;
debf16f0 356 fh_fill_post_attrs(fhp);
254454a5
CL
357
358 /* A newly created file already has a file size of zero. */
359 if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
360 iap->ia_valid &= ~ATTR_SIZE;
361 if (nfsd4_create_is_exclusive(open->op_createmode)) {
362 iap->ia_valid = ATTR_MTIME | ATTR_ATIME |
363 ATTR_MTIME_SET|ATTR_ATIME_SET;
364 iap->ia_mtime.tv_sec = v_mtime;
365 iap->ia_atime.tv_sec = v_atime;
366 iap->ia_mtime.tv_nsec = 0;
367 iap->ia_atime.tv_nsec = 0;
368 }
369
370set_attr:
7fe2a71d 371 status = nfsd_create_setattr(rqstp, fhp, resfhp, &attrs);
254454a5 372
d6a97d3f
N
373 if (attrs.na_labelerr)
374 open->op_bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
c0cbe707
N
375 if (attrs.na_aclerr)
376 open->op_bmval[0] &= ~FATTR4_WORD0_ACL;
254454a5 377out:
debf16f0 378 inode_unlock(inode);
c0cbe707 379 nfsd_attrs_free(&attrs);
254454a5
CL
380 if (child && !IS_ERR(child))
381 dput(child);
382 fh_drop_write(fhp);
383 return status;
384}
385
b37ad28b 386static __be32
c0e6bee4 387do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh **resfh)
1da177e4 388{
bbc9c36c 389 struct svc_fh *current_fh = &cstate->current_fh;
7007c90f 390 int accmode;
b37ad28b 391 __be32 status;
1da177e4 392
c0e6bee4
BF
393 *resfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
394 if (!*resfh)
59deeb9e 395 return nfserr_jukebox;
c0e6bee4 396 fh_init(*resfh, NFS4_FHSIZE);
500c2481 397 open->op_truncate = false;
1da177e4
LT
398
399 if (open->op_create) {
79fb54ab
BH
400 /* FIXME: check session persistence and pnfs flags.
401 * The nfsv4.1 spec requires the following semantics:
402 *
403 * Persistent | pNFS | Server REQUIRED | Client Allowed
404 * Reply Cache | server | |
405 * -------------+--------+-----------------+--------------------
406 * no | no | EXCLUSIVE4_1 | EXCLUSIVE4_1
407 * | | | (SHOULD)
408 * | | and EXCLUSIVE4 | or EXCLUSIVE4
409 * | | | (SHOULD NOT)
410 * no | yes | EXCLUSIVE4_1 | EXCLUSIVE4_1
411 * yes | no | GUARDED4 | GUARDED4
412 * yes | yes | GUARDED4 | GUARDED4
413 */
414
880a3a53 415 current->fs->umask = open->op_umask;
254454a5 416 status = nfsd4_create_file(rqstp, current_fh, *resfh, open);
880a3a53 417 current->fs->umask = 0;
749997e5 418
99f88726 419 /*
ead8fb8c
KM
420 * Following rfc 3530 14.2.16, and rfc 5661 18.16.4
421 * use the returned bitmask to indicate which attributes
422 * we used to store the verifier:
749997e5 423 */
254454a5 424 if (nfsd4_create_is_exclusive(open->op_createmode) && status == 0)
ead8fb8c
KM
425 open->op_bmval[1] |= (FATTR4_WORD1_TIME_ACCESS |
426 FATTR4_WORD1_TIME_MODIFY);
19d008b4 427 } else {
1da177e4 428 status = nfsd_lookup(rqstp, current_fh,
1708e50b 429 open->op_fname, open->op_fnamelen, *resfh);
19d008b4
N
430 if (!status)
431 /* NFSv4 protocol requires change attributes even though
432 * no change happened.
433 */
434 fh_fill_both_attrs(current_fh);
435 }
9dc4e6c4
BF
436 if (status)
437 goto out;
c0e6bee4 438 status = nfsd_check_obj_isreg(*resfh);
af85852d
BF
439 if (status)
440 goto out;
1da177e4 441
c0e6bee4 442 nfsd4_set_open_owner_reply_cache(cstate, open, *resfh);
7007c90f 443 accmode = NFSD_MAY_NOP;
89f6c336
BF
444 if (open->op_created ||
445 open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
7007c90f 446 accmode |= NFSD_MAY_OWNER_OVERRIDE;
c0e6bee4 447 status = do_open_permission(rqstp, *resfh, open, accmode);
41fd1e42 448 set_change_info(&open->op_cinfo, current_fh);
af85852d 449out:
1da177e4
LT
450 return status;
451}
452
b37ad28b 453static __be32
bbc9c36c 454do_open_fhandle(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
1da177e4 455{
bbc9c36c 456 struct svc_fh *current_fh = &cstate->current_fh;
b37ad28b 457 __be32 status;
9f415eb2 458 int accmode = 0;
1da177e4 459
1da177e4
LT
460 /* We don't know the target directory, and therefore can not
461 * set the change info
462 */
463
464 memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
465
bbc9c36c 466 nfsd4_set_open_owner_reply_cache(cstate, open, current_fh);
1da177e4
LT
467
468 open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
469 (open->op_iattr.ia_size == 0);
9f415eb2
BF
470 /*
471 * In the delegation case, the client is telling us about an
472 * open that it *already* performed locally, some time ago. We
473 * should let it succeed now if possible.
474 *
475 * In the case of a CLAIM_FH open, on the other hand, the client
476 * may be counting on us to enforce permissions (the Linux 4.1
477 * client uses this for normal opens, for example).
478 */
479 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH)
480 accmode = NFSD_MAY_OWNER_OVERRIDE;
1da177e4 481
9f415eb2 482 status = do_open_permission(rqstp, current_fh, open, accmode);
1da177e4
LT
483
484 return status;
485}
486
60adfc50
AA
487static void
488copy_clientid(clientid_t *clid, struct nfsd4_session *session)
489{
490 struct nfsd4_sessionid *sid =
491 (struct nfsd4_sessionid *)session->se_sessionid.data;
492
493 clid->cl_boot = sid->clientid.cl_boot;
494 clid->cl_id = sid->clientid.cl_id;
495}
1da177e4 496
7191155b 497static __be32
ca364317 498nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 499 union nfsd4_op_u *u)
1da177e4 500{
eb69853d 501 struct nfsd4_open *open = &u->open;
b37ad28b 502 __be32 status;
c0e6bee4 503 struct svc_fh *resfh = NULL;
3320fef1
SK
504 struct net *net = SVC_NET(rqstp);
505 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
03f318ca 506 bool reclaim = false;
6668958f 507
fe0750e5 508 dprintk("NFSD: nfsd4_open filename %.*s op_openowner %p\n",
1708e50b 509 (int)open->op_fnamelen, open->op_fname,
fe0750e5 510 open->op_openowner);
1da177e4 511
fb70bf12 512 open->op_filp = NULL;
876c553c 513 open->op_rqstp = rqstp;
fb70bf12 514
1da177e4
LT
515 /* This check required by spec. */
516 if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
517 return nfserr_inval;
518
500c2481 519 open->op_created = false;
ab1350b2
MJ
520 /*
521 * RFC5661 18.51.3
522 * Before RECLAIM_COMPLETE done, server should deny new lock
523 */
524 if (nfsd4_has_session(cstate) &&
ec59659b 525 !test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags) &&
ab1350b2
MJ
526 open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
527 return nfserr_grace;
528
60adfc50
AA
529 if (nfsd4_has_session(cstate))
530 copy_clientid(&open->op_clientid, cstate->session);
531
1da177e4 532 /* check seqid for replay. set nfs4_owner */
6cd22668 533 status = nfsd4_process_open1(cstate, open, nn);
a90b061c 534 if (status == nfserr_replay_me) {
fe0750e5 535 struct nfs4_replay *rp = &open->op_openowner->oo_owner.so_replay;
ca364317 536 fh_put(&cstate->current_fh);
a4773c08
BF
537 fh_copy_shallow(&cstate->current_fh.fh_handle,
538 &rp->rp_openfh);
8837abca 539 status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
1da177e4
LT
540 if (status)
541 dprintk("nfsd4_open: replay failed"
542 " restoring previous filehandle\n");
543 else
a90b061c 544 status = nfserr_replay_me;
1da177e4
LT
545 }
546 if (status)
547 goto out;
9d313b17
BF
548 if (open->op_xdr_error) {
549 status = open->op_xdr_error;
550 goto out;
551 }
fb553c0f 552
3c8e0316
YZ
553 status = nfsd4_check_open_attributes(rqstp, cstate, open);
554 if (status)
555 goto out;
556
fb553c0f
BF
557 /* Openowner is now set, so sequence id will get bumped. Now we need
558 * these checks before we do any creates: */
cbd0d51a 559 status = nfserr_grace;
c87fb4a3 560 if (opens_in_grace(net) && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
cbd0d51a
BF
561 goto out;
562 status = nfserr_no_grace;
c87fb4a3 563 if (!opens_in_grace(net) && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
cbd0d51a 564 goto out;
fb553c0f 565
1da177e4 566 switch (open->op_claim_type) {
26320d7e
CL
567 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
568 case NFS4_OPEN_CLAIM_NULL:
569 status = do_open_lookup(rqstp, cstate, open, &resfh);
570 if (status)
1da177e4 571 goto out;
26320d7e
CL
572 break;
573 case NFS4_OPEN_CLAIM_PREVIOUS:
574 status = nfs4_check_open_reclaim(cstate->clp);
575 if (status)
1da177e4 576 goto out;
26320d7e
CL
577 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
578 reclaim = true;
579 fallthrough;
580 case NFS4_OPEN_CLAIM_FH:
581 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
582 status = do_open_fhandle(rqstp, cstate, open);
583 if (status)
584 goto out;
585 resfh = &cstate->current_fh;
586 break;
587 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
588 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
589 status = nfserr_notsupp;
590 goto out;
591 default:
592 status = nfserr_inval;
593 goto out;
1da177e4 594 }
7e2ce0cc 595
c0e6bee4 596 status = nfsd4_process_open2(rqstp, resfh, open);
ca3f9acb
CL
597 if (status && open->op_created)
598 pr_warn("nfsd4_process_open2 failed to open newly-created file: status=%u\n",
599 be32_to_cpu(status));
03f318ca
BF
600 if (reclaim && !status)
601 nn->somebody_reclaimed = true;
1da177e4 602out:
fb70bf12
CL
603 if (open->op_filp) {
604 fput(open->op_filp);
605 open->op_filp = NULL;
606 }
c0e6bee4
BF
607 if (resfh && resfh != &cstate->current_fh) {
608 fh_dup2(&cstate->current_fh, resfh);
609 fh_put(resfh);
610 kfree(resfh);
611 }
42297899 612 nfsd4_cleanup_open_state(cstate, open);
9411b1d4 613 nfsd4_bump_seqid(cstate, status);
1da177e4
LT
614 return status;
615}
616
9d313b17
BF
617/*
618 * OPEN is the only seqid-mutating operation whose decoding can fail
619 * with a seqid-mutating error (specifically, decoding of user names in
620 * the attributes). Therefore we have to do some processing to look up
621 * the stateowner so that we can bump the seqid.
622 */
623static __be32 nfsd4_open_omfg(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_op *op)
624{
eb69853d 625 struct nfsd4_open *open = &op->u.open;
9d313b17
BF
626
627 if (!seqid_mutating_err(ntohl(op->status)))
628 return op->status;
629 if (nfsd4_has_session(cstate))
630 return op->status;
631 open->op_xdr_error = op->status;
eb69853d 632 return nfsd4_open(rqstp, cstate, &op->u);
9d313b17
BF
633}
634
1da177e4
LT
635/*
636 * filehandle-manipulating ops.
637 */
7191155b 638static __be32
b591480b 639nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 640 union nfsd4_op_u *u)
1da177e4 641{
eb69853d 642 u->getfh = &cstate->current_fh;
1da177e4
LT
643 return nfs_ok;
644}
645
7191155b 646static __be32
ca364317 647nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 648 union nfsd4_op_u *u)
1da177e4 649{
eb69853d 650 struct nfsd4_putfh *putfh = &u->putfh;
b9e8638e 651 __be32 ret;
eb69853d 652
ca364317
BF
653 fh_put(&cstate->current_fh);
654 cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
d8b26071 655 memcpy(&cstate->current_fh.fh_handle.fh_raw, putfh->pf_fhval,
ca364317 656 putfh->pf_fhlen);
b9e8638e
OK
657 ret = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
658#ifdef CONFIG_NFSD_V4_2_INTER_SSC
659 if (ret == nfserr_stale && putfh->no_verify) {
660 SET_FH_FLAG(&cstate->current_fh, NFSD4_FH_FOREIGN);
661 ret = 0;
662 }
663#endif
664 return ret;
1da177e4
LT
665}
666
7191155b 667static __be32
b591480b 668nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 669 union nfsd4_op_u *u)
1da177e4 670{
b37ad28b 671 __be32 status;
1da177e4 672
ca364317 673 fh_put(&cstate->current_fh);
df547efb 674 status = exp_pseudoroot(rqstp, &cstate->current_fh);
1da177e4
LT
675 return status;
676}
677
7191155b 678static __be32
b591480b 679nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 680 union nfsd4_op_u *u)
1da177e4 681{
ca364317 682 if (!cstate->save_fh.fh_dentry)
1da177e4
LT
683 return nfserr_restorefh;
684
ca364317 685 fh_dup2(&cstate->current_fh, &cstate->save_fh);
51100d2b 686 if (HAS_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG)) {
37c593c5 687 memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
51100d2b 688 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
37c593c5 689 }
1da177e4
LT
690 return nfs_ok;
691}
692
7191155b 693static __be32
b591480b 694nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 695 union nfsd4_op_u *u)
1da177e4 696{
ca364317 697 fh_dup2(&cstate->save_fh, &cstate->current_fh);
51100d2b 698 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG)) {
37c593c5 699 memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t));
51100d2b 700 SET_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG);
37c593c5 701 }
1da177e4
LT
702 return nfs_ok;
703}
704
705/*
706 * misc nfsv4 ops
707 */
7191155b 708static __be32
ca364317 709nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 710 union nfsd4_op_u *u)
1da177e4 711{
eb69853d 712 struct nfsd4_access *access = &u->access;
c11d7fd1 713 u32 access_full;
eb69853d 714
c11d7fd1
FL
715 access_full = NFS3_ACCESS_FULL;
716 if (cstate->minorversion >= 2)
717 access_full |= NFS4_ACCESS_XALIST | NFS4_ACCESS_XAREAD |
718 NFS4_ACCESS_XAWRITE;
719
720 if (access->ac_req_access & ~access_full)
1da177e4
LT
721 return nfserr_inval;
722
723 access->ac_resp_access = access->ac_req_access;
ca364317
BF
724 return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
725 &access->ac_supported);
1da177e4
LT
726}
727
b9c0ef85 728static void gen_boot_verifier(nfs4_verifier *verifier, struct net *net)
ab4684d1 729{
27c438f5 730 __be32 *verf = (__be32 *)verifier->data;
ab4684d1 731
27c438f5
TM
732 BUILD_BUG_ON(2*sizeof(*verf) != sizeof(verifier->data));
733
3988a578 734 nfsd_copy_write_verifier(verf, net_generic(net, nfsd_net_id));
ab4684d1
CL
735}
736
7191155b 737static __be32
ca364317 738nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 739 union nfsd4_op_u *u)
1da177e4 740{
eb69853d
CH
741 struct nfsd4_commit *commit = &u->commit;
742
75c096f7 743 return nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
524ff1af
TM
744 commit->co_count,
745 (__be32 *)commit->co_verf.data);
1da177e4
LT
746}
747
b37ad28b 748static __be32
ca364317 749nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 750 union nfsd4_op_u *u)
1da177e4 751{
eb69853d 752 struct nfsd4_create *create = &u->create;
7fe2a71d
N
753 struct nfsd_attrs attrs = {
754 .na_iattr = &create->cr_iattr,
d6a97d3f 755 .na_seclabel = &create->cr_label,
7fe2a71d 756 };
1da177e4 757 struct svc_fh resfh;
b37ad28b 758 __be32 status;
1da177e4
LT
759 dev_t rdev;
760
761 fh_init(&resfh, NFS4_FHSIZE);
762
fa08139d 763 status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_NOP);
1da177e4
LT
764 if (status)
765 return status;
766
3c8e0316
YZ
767 status = check_attr_support(rqstp, cstate, create->cr_bmval,
768 nfsd_attrmask);
769 if (status)
770 return status;
771
c0cbe707 772 status = nfsd4_acl_to_attr(create->cr_type, create->cr_acl, &attrs);
880a3a53 773 current->fs->umask = create->cr_umask;
1da177e4
LT
774 switch (create->cr_type) {
775 case NF4LNK:
ca364317
BF
776 status = nfsd_symlink(rqstp, &cstate->current_fh,
777 create->cr_name, create->cr_namelen,
93adc1e3 778 create->cr_data, &attrs, &resfh);
1da177e4
LT
779 break;
780
781 case NF4BLK:
880a3a53 782 status = nfserr_inval;
1da177e4
LT
783 rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
784 if (MAJOR(rdev) != create->cr_specdata1 ||
785 MINOR(rdev) != create->cr_specdata2)
880a3a53 786 goto out_umask;
ca364317
BF
787 status = nfsd_create(rqstp, &cstate->current_fh,
788 create->cr_name, create->cr_namelen,
7fe2a71d 789 &attrs, S_IFBLK, rdev, &resfh);
1da177e4
LT
790 break;
791
792 case NF4CHR:
880a3a53 793 status = nfserr_inval;
1da177e4
LT
794 rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
795 if (MAJOR(rdev) != create->cr_specdata1 ||
796 MINOR(rdev) != create->cr_specdata2)
880a3a53 797 goto out_umask;
ca364317
BF
798 status = nfsd_create(rqstp, &cstate->current_fh,
799 create->cr_name, create->cr_namelen,
7fe2a71d 800 &attrs, S_IFCHR, rdev, &resfh);
1da177e4
LT
801 break;
802
803 case NF4SOCK:
ca364317
BF
804 status = nfsd_create(rqstp, &cstate->current_fh,
805 create->cr_name, create->cr_namelen,
7fe2a71d 806 &attrs, S_IFSOCK, 0, &resfh);
1da177e4
LT
807 break;
808
809 case NF4FIFO:
ca364317
BF
810 status = nfsd_create(rqstp, &cstate->current_fh,
811 create->cr_name, create->cr_namelen,
7fe2a71d 812 &attrs, S_IFIFO, 0, &resfh);
1da177e4
LT
813 break;
814
815 case NF4DIR:
816 create->cr_iattr.ia_valid &= ~ATTR_SIZE;
ca364317
BF
817 status = nfsd_create(rqstp, &cstate->current_fh,
818 create->cr_name, create->cr_namelen,
7fe2a71d 819 &attrs, S_IFDIR, 0, &resfh);
1da177e4
LT
820 break;
821
822 default:
823 status = nfserr_badtype;
824 }
825
9208faf2
YZ
826 if (status)
827 goto out;
1da177e4 828
d6a97d3f
N
829 if (attrs.na_labelerr)
830 create->cr_bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
c0cbe707
N
831 if (attrs.na_aclerr)
832 create->cr_bmval[0] &= ~FATTR4_WORD0_ACL;
9208faf2
YZ
833 set_change_info(&create->cr_cinfo, &cstate->current_fh);
834 fh_dup2(&cstate->current_fh, &resfh);
835out:
1da177e4 836 fh_put(&resfh);
880a3a53
BF
837out_umask:
838 current->fs->umask = 0;
c0cbe707 839 nfsd_attrs_free(&attrs);
1da177e4
LT
840 return status;
841}
842
7191155b 843static __be32
ca364317 844nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 845 union nfsd4_op_u *u)
1da177e4 846{
eb69853d 847 struct nfsd4_getattr *getattr = &u->getattr;
b37ad28b 848 __be32 status;
1da177e4 849
8837abca 850 status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
1da177e4
LT
851 if (status)
852 return status;
853
854 if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
855 return nfserr_inval;
856
916d2d84
BF
857 getattr->ga_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
858 getattr->ga_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
859 getattr->ga_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
1da177e4 860
ca364317 861 getattr->ga_fhp = &cstate->current_fh;
1da177e4
LT
862 return nfs_ok;
863}
864
7191155b 865static __be32
ca364317 866nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 867 union nfsd4_op_u *u)
1da177e4 868{
eb69853d 869 struct nfsd4_link *link = &u->link;
95424460 870 __be32 status;
1da177e4 871
ca364317
BF
872 status = nfsd_link(rqstp, &cstate->current_fh,
873 link->li_name, link->li_namelen, &cstate->save_fh);
1da177e4 874 if (!status)
ca364317 875 set_change_info(&link->li_cinfo, &cstate->current_fh);
1da177e4
LT
876 return status;
877}
878
0ff7ab46 879static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
1da177e4
LT
880{
881 struct svc_fh tmp_fh;
b37ad28b 882 __be32 ret;
1da177e4
LT
883
884 fh_init(&tmp_fh, NFS4_FHSIZE);
df547efb
BF
885 ret = exp_pseudoroot(rqstp, &tmp_fh);
886 if (ret)
1da177e4 887 return ret;
0ff7ab46 888 if (tmp_fh.fh_dentry == fh->fh_dentry) {
1da177e4
LT
889 fh_put(&tmp_fh);
890 return nfserr_noent;
891 }
892 fh_put(&tmp_fh);
0ff7ab46
BF
893 return nfsd_lookup(rqstp, fh, "..", 2, fh);
894}
895
896static __be32
897nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 898 union nfsd4_op_u *u)
0ff7ab46
BF
899{
900 return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
1da177e4
LT
901}
902
7191155b 903static __be32
ca364317 904nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 905 union nfsd4_op_u *u)
1da177e4 906{
ca364317 907 return nfsd_lookup(rqstp, &cstate->current_fh,
eb69853d 908 u->lookup.lo_name, u->lookup.lo_len,
ca364317 909 &cstate->current_fh);
1da177e4
LT
910}
911
7191155b 912static __be32
ca364317 913nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 914 union nfsd4_op_u *u)
1da177e4 915{
eb69853d 916 struct nfsd4_read *read = &u->read;
b37ad28b 917 __be32 status;
1da177e4 918
5c4583b2 919 read->rd_nf = NULL;
1da177e4 920
87c5942e
CL
921 trace_nfsd_read_start(rqstp, &cstate->current_fh,
922 read->rd_offset, read->rd_length);
923
0cb4d23a
CL
924 read->rd_length = min_t(u32, read->rd_length, svc_max_payload(rqstp));
925 if (read->rd_offset > (u64)OFFSET_MAX)
926 read->rd_offset = (u64)OFFSET_MAX;
927 if (read->rd_offset + read->rd_length > (u64)OFFSET_MAX)
928 read->rd_length = (u64)OFFSET_MAX - read->rd_offset;
929
9b3234b9
BF
930 /*
931 * If we do a zero copy read, then a client will see read data
932 * that reflects the state of the file *after* performing the
933 * following compound.
934 *
935 * To ensure proper ordering, we therefore turn off zero copy if
936 * the client wants us to do more in this compound:
937 */
938 if (!nfsd4_last_compound_op(rqstp))
28df0988 939 __clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
9b3234b9 940
1da177e4 941 /* check stateid */
aa0d6aed
AS
942 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
943 &read->rd_stateid, RD_STATE,
624322f1 944 &read->rd_nf, NULL);
af90f707 945 if (status) {
1da177e4
LT
946 dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
947 goto out;
948 }
949 status = nfs_ok;
950out:
1da177e4 951 read->rd_rqstp = rqstp;
ca364317 952 read->rd_fhp = &cstate->current_fh;
1da177e4
LT
953 return status;
954}
955
34b1744c
BF
956
957static void
958nfsd4_read_release(union nfsd4_op_u *u)
959{
5c4583b2
JL
960 if (u->read.rd_nf)
961 nfsd_file_put(u->read.rd_nf);
87c5942e
CL
962 trace_nfsd_read_done(u->read.rd_rqstp, u->read.rd_fhp,
963 u->read.rd_offset, u->read.rd_length);
34b1744c
BF
964}
965
7191155b 966static __be32
ca364317 967nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 968 union nfsd4_op_u *u)
1da177e4 969{
eb69853d 970 struct nfsd4_readdir *readdir = &u->readdir;
1da177e4
LT
971 u64 cookie = readdir->rd_cookie;
972 static const nfs4_verifier zeroverf;
973
974 /* no need to check permission - this will be done in nfsd_readdir() */
975
976 if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
977 return nfserr_inval;
978
916d2d84
BF
979 readdir->rd_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
980 readdir->rd_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
981 readdir->rd_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
1da177e4 982
832023bf 983 if ((cookie == 1) || (cookie == 2) ||
1da177e4
LT
984 (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
985 return nfserr_bad_cookie;
986
987 readdir->rd_rqstp = rqstp;
ca364317 988 readdir->rd_fhp = &cstate->current_fh;
1da177e4
LT
989 return nfs_ok;
990}
991
7191155b 992static __be32
ca364317 993nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 994 union nfsd4_op_u *u)
1da177e4 995{
eb69853d
CH
996 u->readlink.rl_rqstp = rqstp;
997 u->readlink.rl_fhp = &cstate->current_fh;
1da177e4
LT
998 return nfs_ok;
999}
1000
7191155b 1001static __be32
ca364317 1002nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 1003 union nfsd4_op_u *u)
1da177e4 1004{
eb69853d 1005 struct nfsd4_remove *remove = &u->remove;
b37ad28b 1006 __be32 status;
1da177e4 1007
c87fb4a3 1008 if (opens_in_grace(SVC_NET(rqstp)))
c815afc7 1009 return nfserr_grace;
ca364317
BF
1010 status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
1011 remove->rm_name, remove->rm_namelen);
b677c0c6 1012 if (!status)
ca364317 1013 set_change_info(&remove->rm_cinfo, &cstate->current_fh);
1da177e4
LT
1014 return status;
1015}
1016
7191155b 1017static __be32
ca364317 1018nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 1019 union nfsd4_op_u *u)
1da177e4 1020{
eb69853d 1021 struct nfsd4_rename *rename = &u->rename;
95424460 1022 __be32 status;
1da177e4 1023
f8f71d00 1024 if (opens_in_grace(SVC_NET(rqstp)))
c815afc7 1025 return nfserr_grace;
ca364317
BF
1026 status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
1027 rename->rn_snamelen, &cstate->current_fh,
1da177e4 1028 rename->rn_tname, rename->rn_tnamelen);
2a6cf944
BF
1029 if (status)
1030 return status;
1031 set_change_info(&rename->rn_sinfo, &cstate->current_fh);
1032 set_change_info(&rename->rn_tinfo, &cstate->save_fh);
1033 return nfs_ok;
1da177e4
LT
1034}
1035
dcb488a3
AA
1036static __be32
1037nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 1038 union nfsd4_op_u *u)
dcb488a3 1039{
eb69853d 1040 struct nfsd4_secinfo *secinfo = &u->secinfo;
dcb488a3
AA
1041 struct svc_export *exp;
1042 struct dentry *dentry;
1043 __be32 err;
1044
29a78a3e
BF
1045 err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
1046 if (err)
1047 return err;
dcb488a3
AA
1048 err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
1049 secinfo->si_name, secinfo->si_namelen,
1050 &exp, &dentry);
1051 if (err)
1052 return err;
2b0143b5 1053 if (d_really_is_negative(dentry)) {
dcb488a3
AA
1054 exp_put(exp);
1055 err = nfserr_noent;
1056 } else
1057 secinfo->si_exp = exp;
1058 dput(dentry);
56560b9a
BF
1059 if (cstate->minorversion)
1060 /* See rfc 5661 section 2.6.3.1.1.8 */
1061 fh_put(&cstate->current_fh);
dcb488a3
AA
1062 return err;
1063}
1064
04f4ad16
BF
1065static __be32
1066nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 1067 union nfsd4_op_u *u)
04f4ad16
BF
1068{
1069 __be32 err;
1070
eb69853d 1071 switch (u->secinfo_no_name.sin_style) {
04f4ad16
BF
1072 case NFS4_SECINFO_STYLE4_CURRENT_FH:
1073 break;
1074 case NFS4_SECINFO_STYLE4_PARENT:
1075 err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
1076 if (err)
1077 return err;
1078 break;
1079 default:
1080 return nfserr_inval;
1081 }
bf18f163 1082
eb69853d 1083 u->secinfo_no_name.sin_exp = exp_get(cstate->current_fh.fh_export);
04f4ad16
BF
1084 fh_put(&cstate->current_fh);
1085 return nfs_ok;
1086}
1087
34b1744c
BF
1088static void
1089nfsd4_secinfo_release(union nfsd4_op_u *u)
1090{
1091 if (u->secinfo.si_exp)
1092 exp_put(u->secinfo.si_exp);
1093}
1094
ec572b9e
EG
1095static void
1096nfsd4_secinfo_no_name_release(union nfsd4_op_u *u)
1097{
1098 if (u->secinfo_no_name.sin_exp)
1099 exp_put(u->secinfo_no_name.sin_exp);
1100}
1101
7191155b 1102static __be32
ca364317 1103nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 1104 union nfsd4_op_u *u)
1da177e4 1105{
eb69853d 1106 struct nfsd4_setattr *setattr = &u->setattr;
7fe2a71d
N
1107 struct nfsd_attrs attrs = {
1108 .na_iattr = &setattr->sa_iattr,
d6a97d3f 1109 .na_seclabel = &setattr->sa_label,
7fe2a71d 1110 };
c0cbe707 1111 struct inode *inode;
b37ad28b 1112 __be32 status = nfs_ok;
96f6f985 1113 int err;
1da177e4 1114
1da177e4 1115 if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
af90f707 1116 status = nfs4_preprocess_stateid_op(rqstp, cstate,
aa0d6aed 1117 &cstate->current_fh, &setattr->sa_stateid,
624322f1 1118 WR_STATE, NULL, NULL);
375c5547 1119 if (status) {
3e3b4800 1120 dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
375c5547
BF
1121 return status;
1122 }
1da177e4 1123 }
96f6f985
AV
1124 err = fh_want_write(&cstate->current_fh);
1125 if (err)
1126 return nfserrno(err);
1da177e4 1127 status = nfs_ok;
3c8e0316
YZ
1128
1129 status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
1130 nfsd_attrmask);
1131 if (status)
1132 goto out;
1133
c0cbe707
N
1134 inode = cstate->current_fh.fh_dentry->d_inode;
1135 status = nfsd4_acl_to_attr(S_ISDIR(inode->i_mode) ? NF4DIR : NF4REG,
1136 setattr->sa_acl, &attrs);
1137
1da177e4 1138 if (status)
18f335af 1139 goto out;
7fe2a71d 1140 status = nfsd_setattr(rqstp, &cstate->current_fh, &attrs,
2a1aa489 1141 0, (time64_t)0);
d6a97d3f
N
1142 if (!status)
1143 status = nfserrno(attrs.na_labelerr);
18f335af 1144out:
c0cbe707 1145 nfsd_attrs_free(&attrs);
bad0dcff 1146 fh_drop_write(&cstate->current_fh);
1da177e4
LT
1147 return status;
1148}
1149
7191155b 1150static __be32
ca364317 1151nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 1152 union nfsd4_op_u *u)
1da177e4 1153{
eb69853d 1154 struct nfsd4_write *write = &u->write;
1da177e4 1155 stateid_t *stateid = &write->wr_stateid;
5c4583b2 1156 struct nfsd_file *nf = NULL;
b37ad28b 1157 __be32 status = nfs_ok;
31dec253 1158 unsigned long cnt;
ffe1137b 1159 int nvecs;
1da177e4 1160
6260d9a5
CL
1161 if (write->wr_offset > (u64)OFFSET_MAX ||
1162 write->wr_offset + write->wr_buflen > (u64)OFFSET_MAX)
1163 return nfserr_fbig;
1da177e4 1164
d890be15
CL
1165 cnt = write->wr_buflen;
1166 trace_nfsd_write_start(rqstp, &cstate->current_fh,
1167 write->wr_offset, cnt);
aa0d6aed 1168 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
624322f1 1169 stateid, WR_STATE, &nf, NULL);
375c5547
BF
1170 if (status) {
1171 dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
1172 return status;
1173 }
1174
1da177e4 1175 write->wr_how_written = write->wr_stable_how;
1da177e4 1176
dae9a6ca 1177 nvecs = svc_fill_write_vector(rqstp, &write->wr_payload);
ffe1137b
BF
1178 WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec));
1179
16f8f894 1180 status = nfsd_vfs_write(rqstp, &cstate->current_fh, nf,
af90f707 1181 write->wr_offset, rqstp->rq_vec, nvecs, &cnt,
19e0663f
TM
1182 write->wr_how_written,
1183 (__be32 *)write->wr_verifier.data);
5c4583b2 1184 nfsd_file_put(nf);
1da177e4 1185
31dec253 1186 write->wr_bytes_written = cnt;
d890be15
CL
1187 trace_nfsd_write_done(rqstp, &cstate->current_fh,
1188 write->wr_offset, cnt);
1da177e4 1189 return status;
1da177e4
LT
1190}
1191
ffa0160a 1192static __be32
29ae7f9d 1193nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5c4583b2
JL
1194 stateid_t *src_stateid, struct nfsd_file **src,
1195 stateid_t *dst_stateid, struct nfsd_file **dst)
ffa0160a 1196{
ffa0160a
CH
1197 __be32 status;
1198
01310bb7
SM
1199 if (!cstate->save_fh.fh_dentry)
1200 return nfserr_nofilehandle;
1201
ffa0160a 1202 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->save_fh,
624322f1 1203 src_stateid, RD_STATE, src, NULL);
ffa0160a
CH
1204 if (status) {
1205 dprintk("NFSD: %s: couldn't process src stateid!\n", __func__);
1206 goto out;
1207 }
1208
1209 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
624322f1 1210 dst_stateid, WR_STATE, dst, NULL);
ffa0160a
CH
1211 if (status) {
1212 dprintk("NFSD: %s: couldn't process dst stateid!\n", __func__);
1213 goto out_put_src;
1214 }
1215
1216 /* fix up for NFS-specific error code */
5c4583b2
JL
1217 if (!S_ISREG(file_inode((*src)->nf_file)->i_mode) ||
1218 !S_ISREG(file_inode((*dst)->nf_file)->i_mode)) {
ffa0160a
CH
1219 status = nfserr_wrong_type;
1220 goto out_put_dst;
1221 }
1222
29ae7f9d
AS
1223out:
1224 return status;
1225out_put_dst:
5c4583b2 1226 nfsd_file_put(*dst);
29ae7f9d 1227out_put_src:
5c4583b2 1228 nfsd_file_put(*src);
29ae7f9d
AS
1229 goto out;
1230}
1231
1232static __be32
1233nfsd4_clone(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 1234 union nfsd4_op_u *u)
29ae7f9d 1235{
eb69853d 1236 struct nfsd4_clone *clone = &u->clone;
5c4583b2 1237 struct nfsd_file *src, *dst;
29ae7f9d
AS
1238 __be32 status;
1239
1240 status = nfsd4_verify_copy(rqstp, cstate, &clone->cl_src_stateid, &src,
1241 &clone->cl_dst_stateid, &dst);
1242 if (status)
1243 goto out;
1244
a2f4c3fa 1245 status = nfsd4_clone_file_range(rqstp, src, clone->cl_src_pos,
b66ae6dd 1246 dst, clone->cl_dst_pos, clone->cl_count,
a25e3726 1247 EX_ISSYNC(cstate->current_fh.fh_export));
ffa0160a 1248
5c4583b2
JL
1249 nfsd_file_put(dst);
1250 nfsd_file_put(src);
ffa0160a
CH
1251out:
1252 return status;
1253}
1254
8ea6e2c9 1255static void nfs4_put_copy(struct nfsd4_copy *copy)
e0639dc5
OK
1256{
1257 if (!refcount_dec_and_test(&copy->refcount))
1258 return;
87689df6 1259 kfree(copy->cp_src);
e0639dc5
OK
1260 kfree(copy);
1261}
1262
e0639dc5
OK
1263static void nfsd4_stop_copy(struct nfsd4_copy *copy)
1264{
1913cdf5 1265 if (!test_and_set_bit(NFSD4_COPY_F_STOPPED, &copy->cp_flags))
e0639dc5
OK
1266 kthread_stop(copy->copy_task);
1267 nfs4_put_copy(copy);
1268}
1269
1270static struct nfsd4_copy *nfsd4_get_copy(struct nfs4_client *clp)
1271{
1272 struct nfsd4_copy *copy = NULL;
1273
1274 spin_lock(&clp->async_lock);
1275 if (!list_empty(&clp->async_copies)) {
1276 copy = list_first_entry(&clp->async_copies, struct nfsd4_copy,
1277 copies);
1278 refcount_inc(&copy->refcount);
1279 }
1280 spin_unlock(&clp->async_lock);
1281 return copy;
1282}
1283
1284void nfsd4_shutdown_copy(struct nfs4_client *clp)
1285{
1286 struct nfsd4_copy *copy;
1287
1288 while ((copy = nfsd4_get_copy(clp)) != NULL)
1289 nfsd4_stop_copy(copy);
1290}
ce0887ac
OK
1291#ifdef CONFIG_NFSD_V4_2_INTER_SSC
1292
1293extern struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
1294 struct nfs_fh *src_fh,
1295 nfs4_stateid *stateid);
1296extern void nfs42_ssc_close(struct file *filep);
1297
1298extern void nfs_sb_deactive(struct super_block *sb);
1299
1300#define NFSD42_INTERSSC_MOUNTOPS "vers=4.2,addr=%s,sec=sys"
1301
f4e44b39
DN
1302/*
1303 * setup a work entry in the ssc delayed unmount list.
1304 */
f47dc2d3 1305static __be32 nfsd4_ssc_setup_dul(struct nfsd_net *nn, char *ipaddr,
f4e44b39
DN
1306 struct nfsd4_ssc_umount_item **retwork, struct vfsmount **ss_mnt)
1307{
8e70bf27 1308 struct nfsd4_ssc_umount_item *ni = NULL;
f4e44b39
DN
1309 struct nfsd4_ssc_umount_item *work = NULL;
1310 struct nfsd4_ssc_umount_item *tmp;
1311 DEFINE_WAIT(wait);
1312
1313 *ss_mnt = NULL;
1314 *retwork = NULL;
1315 work = kzalloc(sizeof(*work), GFP_KERNEL);
1316try_again:
1317 spin_lock(&nn->nfsd_ssc_lock);
1318 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
1319 if (strncmp(ni->nsui_ipaddr, ipaddr, sizeof(ni->nsui_ipaddr)))
1320 continue;
1321 /* found a match */
1322 if (ni->nsui_busy) {
1323 /* wait - and try again */
1324 prepare_to_wait(&nn->nfsd_ssc_waitq, &wait,
1325 TASK_INTERRUPTIBLE);
1326 spin_unlock(&nn->nfsd_ssc_lock);
1327
1328 /* allow 20secs for mount/unmount for now - revisit */
1329 if (signal_pending(current) ||
1330 (schedule_timeout(20*HZ) == 0)) {
1331 kfree(work);
1332 return nfserr_eagain;
1333 }
1334 finish_wait(&nn->nfsd_ssc_waitq, &wait);
1335 goto try_again;
1336 }
1337 *ss_mnt = ni->nsui_vfsmount;
1338 refcount_inc(&ni->nsui_refcnt);
1339 spin_unlock(&nn->nfsd_ssc_lock);
1340 kfree(work);
1341
1342 /* return vfsmount in ss_mnt */
1343 return 0;
1344 }
1345 if (work) {
53048779 1346 strlcpy(work->nsui_ipaddr, ipaddr, sizeof(work->nsui_ipaddr) - 1);
f4e44b39
DN
1347 refcount_set(&work->nsui_refcnt, 2);
1348 work->nsui_busy = true;
1349 list_add_tail(&work->nsui_list, &nn->nfsd_ssc_mount_list);
1350 *retwork = work;
1351 }
1352 spin_unlock(&nn->nfsd_ssc_lock);
1353 return 0;
1354}
1355
1356static void nfsd4_ssc_update_dul_work(struct nfsd_net *nn,
1357 struct nfsd4_ssc_umount_item *work, struct vfsmount *ss_mnt)
1358{
1359 /* set nsui_vfsmount, clear busy flag and wakeup waiters */
1360 spin_lock(&nn->nfsd_ssc_lock);
1361 work->nsui_vfsmount = ss_mnt;
1362 work->nsui_busy = false;
1363 wake_up_all(&nn->nfsd_ssc_waitq);
1364 spin_unlock(&nn->nfsd_ssc_lock);
1365}
1366
1367static void nfsd4_ssc_cancel_dul_work(struct nfsd_net *nn,
1368 struct nfsd4_ssc_umount_item *work)
1369{
1370 spin_lock(&nn->nfsd_ssc_lock);
1371 list_del(&work->nsui_list);
1372 wake_up_all(&nn->nfsd_ssc_waitq);
1373 spin_unlock(&nn->nfsd_ssc_lock);
1374 kfree(work);
1375}
1376
f2453978 1377/*
ce0887ac
OK
1378 * Support one copy source server for now.
1379 */
1380static __be32
1381nfsd4_interssc_connect(struct nl4_server *nss, struct svc_rqst *rqstp,
1382 struct vfsmount **mount)
1383{
1384 struct file_system_type *type;
1385 struct vfsmount *ss_mnt;
1386 struct nfs42_netaddr *naddr;
1387 struct sockaddr_storage tmp_addr;
1388 size_t tmp_addrlen, match_netid_len = 3;
1389 char *startsep = "", *endsep = "", *match_netid = "tcp";
1390 char *ipaddr, *dev_name, *raw_data;
b8290ca2
OK
1391 int len, raw_len;
1392 __be32 status = nfserr_inval;
f4e44b39
DN
1393 struct nfsd4_ssc_umount_item *work = NULL;
1394 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
ce0887ac
OK
1395
1396 naddr = &nss->u.nl4_addr;
1397 tmp_addrlen = rpc_uaddr2sockaddr(SVC_NET(rqstp), naddr->addr,
1398 naddr->addr_len,
1399 (struct sockaddr *)&tmp_addr,
1400 sizeof(tmp_addr));
1401 if (tmp_addrlen == 0)
1402 goto out_err;
1403
1404 if (tmp_addr.ss_family == AF_INET6) {
1405 startsep = "[";
1406 endsep = "]";
1407 match_netid = "tcp6";
1408 match_netid_len = 4;
1409 }
1410
1411 if (naddr->netid_len != match_netid_len ||
1412 strncmp(naddr->netid, match_netid, naddr->netid_len))
1413 goto out_err;
1414
1415 /* Construct the raw data for the vfs_kern_mount call */
1416 len = RPC_MAX_ADDRBUFLEN + 1;
1417 ipaddr = kzalloc(len, GFP_KERNEL);
1418 if (!ipaddr)
1419 goto out_err;
1420
1421 rpc_ntop((struct sockaddr *)&tmp_addr, ipaddr, len);
1422
1423 /* 2 for ipv6 endsep and startsep. 3 for ":/" and trailing '/0'*/
1424
1425 raw_len = strlen(NFSD42_INTERSSC_MOUNTOPS) + strlen(ipaddr);
1426 raw_data = kzalloc(raw_len, GFP_KERNEL);
1427 if (!raw_data)
1428 goto out_free_ipaddr;
1429
1430 snprintf(raw_data, raw_len, NFSD42_INTERSSC_MOUNTOPS, ipaddr);
1431
b8290ca2 1432 status = nfserr_nodev;
ce0887ac
OK
1433 type = get_fs_type("nfs");
1434 if (!type)
1435 goto out_free_rawdata;
1436
1437 /* Set the server:<export> for the vfs_kern_mount call */
1438 dev_name = kzalloc(len + 5, GFP_KERNEL);
1439 if (!dev_name)
1440 goto out_free_rawdata;
1441 snprintf(dev_name, len + 5, "%s%s%s:/", startsep, ipaddr, endsep);
1442
f4e44b39
DN
1443 status = nfsd4_ssc_setup_dul(nn, ipaddr, &work, &ss_mnt);
1444 if (status)
1445 goto out_free_devname;
1446 if (ss_mnt)
1447 goto out_done;
1448
ce0887ac
OK
1449 /* Use an 'internal' mount: SB_KERNMOUNT -> MNT_INTERNAL */
1450 ss_mnt = vfs_kern_mount(type, SB_KERNMOUNT, dev_name, raw_data);
1451 module_put(type->owner);
f4e44b39 1452 if (IS_ERR(ss_mnt)) {
54185267 1453 status = nfserr_nodev;
f4e44b39
DN
1454 if (work)
1455 nfsd4_ssc_cancel_dul_work(nn, work);
ce0887ac 1456 goto out_free_devname;
f4e44b39
DN
1457 }
1458 if (work)
1459 nfsd4_ssc_update_dul_work(nn, work, ss_mnt);
1460out_done:
ce0887ac
OK
1461 status = 0;
1462 *mount = ss_mnt;
1463
1464out_free_devname:
1465 kfree(dev_name);
1466out_free_rawdata:
1467 kfree(raw_data);
1468out_free_ipaddr:
1469 kfree(ipaddr);
1470out_err:
1471 return status;
1472}
1473
1474static void
1475nfsd4_interssc_disconnect(struct vfsmount *ss_mnt)
1476{
0cfcd405 1477 nfs_do_sb_deactive(ss_mnt->mnt_sb);
ce0887ac
OK
1478 mntput(ss_mnt);
1479}
1480
f2453978 1481/*
ce0887ac 1482 * Verify COPY destination stateid.
f2453978 1483 *
ce0887ac
OK
1484 * Connect to the source server with NFSv4.1.
1485 * Create the source struct file for nfsd_copy_range.
1486 * Called with COPY cstate:
1487 * SAVED_FH: source filehandle
1488 * CURRENT_FH: destination filehandle
ce0887ac
OK
1489 */
1490static __be32
1491nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
1492 struct nfsd4_compound_state *cstate,
1493 struct nfsd4_copy *copy, struct vfsmount **mount)
1494{
1495 struct svc_fh *s_fh = NULL;
1496 stateid_t *s_stid = &copy->cp_src_stateid;
b8290ca2 1497 __be32 status = nfserr_inval;
ce0887ac
OK
1498
1499 /* Verify the destination stateid and set dst struct file*/
1500 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1501 &copy->cp_dst_stateid,
1502 WR_STATE, &copy->nf_dst, NULL);
1503 if (status)
1504 goto out;
1505
87689df6 1506 status = nfsd4_interssc_connect(copy->cp_src, rqstp, mount);
ce0887ac
OK
1507 if (status)
1508 goto out;
1509
1510 s_fh = &cstate->save_fh;
1511
1512 copy->c_fh.size = s_fh->fh_handle.fh_size;
d8b26071 1513 memcpy(copy->c_fh.data, &s_fh->fh_handle.fh_raw, copy->c_fh.size);
3f9544ca 1514 copy->stateid.seqid = cpu_to_be32(s_stid->si_generation);
ce0887ac
OK
1515 memcpy(copy->stateid.other, (void *)&s_stid->si_opaque,
1516 sizeof(stateid_opaque_t));
1517
1518 status = 0;
1519out:
1520 return status;
1521}
1522
1523static void
24d796ea 1524nfsd4_cleanup_inter_ssc(struct vfsmount *ss_mnt, struct file *filp,
ce0887ac
OK
1525 struct nfsd_file *dst)
1526{
f4e44b39
DN
1527 bool found = false;
1528 long timeout;
1529 struct nfsd4_ssc_umount_item *tmp;
f47dc2d3 1530 struct nfsd4_ssc_umount_item *ni = NULL;
f4e44b39
DN
1531 struct nfsd_net *nn = net_generic(dst->nf_net, nfsd_net_id);
1532
24d796ea 1533 nfs42_ssc_close(filp);
ce0887ac 1534 nfsd_file_put(dst);
24d796ea 1535 fput(filp);
f4e44b39
DN
1536
1537 if (!nn) {
1538 mntput(ss_mnt);
1539 return;
1540 }
1541 spin_lock(&nn->nfsd_ssc_lock);
1542 timeout = msecs_to_jiffies(nfsd4_ssc_umount_timeout);
1543 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
1544 if (ni->nsui_vfsmount->mnt_sb == ss_mnt->mnt_sb) {
1545 list_del(&ni->nsui_list);
1546 /*
1547 * vfsmount can be shared by multiple exports,
1548 * decrement refcnt. If the count drops to 1 it
1549 * will be unmounted when nsui_expire expires.
1550 */
1551 refcount_dec(&ni->nsui_refcnt);
1552 ni->nsui_expire = jiffies + timeout;
1553 list_add_tail(&ni->nsui_list, &nn->nfsd_ssc_mount_list);
1554 found = true;
1555 break;
1556 }
1557 }
1558 spin_unlock(&nn->nfsd_ssc_lock);
1559 if (!found) {
1560 mntput(ss_mnt);
1561 return;
1562 }
ce0887ac
OK
1563}
1564
1565#else /* CONFIG_NFSD_V4_2_INTER_SSC */
1566
1567static __be32
1568nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
1569 struct nfsd4_compound_state *cstate,
1570 struct nfsd4_copy *copy,
1571 struct vfsmount **mount)
1572{
1573 *mount = NULL;
b8290ca2 1574 return nfserr_inval;
ce0887ac
OK
1575}
1576
1577static void
24d796ea 1578nfsd4_cleanup_inter_ssc(struct vfsmount *ss_mnt, struct file *filp,
ce0887ac
OK
1579 struct nfsd_file *dst)
1580{
1581}
1582
1583static void
1584nfsd4_interssc_disconnect(struct vfsmount *ss_mnt)
1585{
1586}
1587
1588static struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
1589 struct nfs_fh *src_fh,
1590 nfs4_stateid *stateid)
1591{
1592 return NULL;
1593}
1594#endif /* CONFIG_NFSD_V4_2_INTER_SSC */
1595
1596static __be32
1597nfsd4_setup_intra_ssc(struct svc_rqst *rqstp,
1598 struct nfsd4_compound_state *cstate,
1599 struct nfsd4_copy *copy)
1600{
1601 return nfsd4_verify_copy(rqstp, cstate, &copy->cp_src_stateid,
1602 &copy->nf_src, &copy->cp_dst_stateid,
1603 &copy->nf_dst);
1604}
1605
1606static void
1607nfsd4_cleanup_intra_ssc(struct nfsd_file *src, struct nfsd_file *dst)
1608{
1609 nfsd_file_put(src);
1610 nfsd_file_put(dst);
1611}
e0639dc5
OK
1612
1613static void nfsd4_cb_offload_release(struct nfsd4_callback *cb)
1614{
a11ada99
CL
1615 struct nfsd4_cb_offload *cbo =
1616 container_of(cb, struct nfsd4_cb_offload, co_cb);
e0639dc5 1617
a11ada99 1618 kfree(cbo);
e0639dc5
OK
1619}
1620
1621static int nfsd4_cb_offload_done(struct nfsd4_callback *cb,
1622 struct rpc_task *task)
1623{
1624 return 1;
1625}
1626
1627static const struct nfsd4_callback_ops nfsd4_cb_offload_ops = {
1628 .release = nfsd4_cb_offload_release,
1629 .done = nfsd4_cb_offload_done
1630};
1631
1632static void nfsd4_init_copy_res(struct nfsd4_copy *copy, bool sync)
1633{
eac0b17a 1634 copy->cp_res.wr_stable_how =
1913cdf5
CL
1635 test_bit(NFSD4_COPY_F_COMMITTED, &copy->cp_flags) ?
1636 NFS_FILE_SYNC : NFS_UNSTABLE;
1637 nfsd4_copy_set_sync(copy, sync);
e0639dc5
OK
1638 gen_boot_verifier(&copy->cp_res.wr_verifier, copy->cp_clp->net);
1639}
1640
3b7bf593
CL
1641static ssize_t _nfsd_copy_file_range(struct nfsd4_copy *copy,
1642 struct file *dst,
1643 struct file *src)
e0639dc5 1644{
555dbf1a 1645 errseq_t since;
e0639dc5 1646 ssize_t bytes_copied = 0;
e7a833e9 1647 u64 bytes_total = copy->cp_count;
e0639dc5
OK
1648 u64 src_pos = copy->cp_src_pos;
1649 u64 dst_pos = copy->cp_dst_pos;
c2f1c4bd 1650 int status;
e0639dc5 1651
792a5112
BF
1652 /* See RFC 7862 p.67: */
1653 if (bytes_total == 0)
1654 bytes_total = ULLONG_MAX;
e0639dc5
OK
1655 do {
1656 if (kthread_should_stop())
1657 break;
555dbf1a
TM
1658 bytes_copied = nfsd_copy_file_range(src, src_pos, dst, dst_pos,
1659 bytes_total);
e0639dc5
OK
1660 if (bytes_copied <= 0)
1661 break;
1662 bytes_total -= bytes_copied;
1663 copy->cp_res.wr_bytes_written += bytes_copied;
1664 src_pos += bytes_copied;
1665 dst_pos += bytes_copied;
1913cdf5 1666 } while (bytes_total > 0 && nfsd4_copy_is_async(copy));
eac0b17a 1667 /* for a non-zero asynchronous copy do a commit of data */
1913cdf5 1668 if (nfsd4_copy_is_async(copy) && copy->cp_res.wr_bytes_written > 0) {
555dbf1a
TM
1669 since = READ_ONCE(dst->f_wb_err);
1670 status = vfs_fsync_range(dst, copy->cp_dst_pos,
eac0b17a 1671 copy->cp_res.wr_bytes_written, 0);
555dbf1a
TM
1672 if (!status)
1673 status = filemap_check_wb_err(dst->f_mapping, since);
eac0b17a 1674 if (!status)
1913cdf5 1675 set_bit(NFSD4_COPY_F_COMMITTED, &copy->cp_flags);
eac0b17a 1676 }
e0639dc5
OK
1677 return bytes_copied;
1678}
1679
3b7bf593
CL
1680static __be32 nfsd4_do_copy(struct nfsd4_copy *copy,
1681 struct file *src, struct file *dst,
1682 bool sync)
e0639dc5
OK
1683{
1684 __be32 status;
1685 ssize_t bytes;
1686
3b7bf593
CL
1687 bytes = _nfsd_copy_file_range(copy, dst, src);
1688
e0639dc5
OK
1689 /* for async copy, we ignore the error, client can always retry
1690 * to get the error
1691 */
1692 if (bytes < 0 && !copy->cp_res.wr_bytes_written)
1693 status = nfserrno(bytes);
1694 else {
1695 nfsd4_init_copy_res(copy, sync);
1696 status = nfs_ok;
1697 }
e0639dc5
OK
1698 return status;
1699}
1700
eb162e17 1701static void dup_copy_fields(struct nfsd4_copy *src, struct nfsd4_copy *dst)
e0639dc5
OK
1702{
1703 dst->cp_src_pos = src->cp_src_pos;
1704 dst->cp_dst_pos = src->cp_dst_pos;
1705 dst->cp_count = src->cp_count;
1913cdf5 1706 dst->cp_flags = src->cp_flags;
e0639dc5
OK
1707 memcpy(&dst->cp_res, &src->cp_res, sizeof(src->cp_res));
1708 memcpy(&dst->fh, &src->fh, sizeof(src->fh));
1709 dst->cp_clp = src->cp_clp;
5c4583b2 1710 dst->nf_dst = nfsd_file_get(src->nf_dst);
1913cdf5
CL
1711 /* for inter, nf_src doesn't exist yet */
1712 if (!nfsd4_ssc_is_inter(src))
ce0887ac
OK
1713 dst->nf_src = nfsd_file_get(src->nf_src);
1714
e0639dc5 1715 memcpy(&dst->cp_stateid, &src->cp_stateid, sizeof(src->cp_stateid));
87689df6 1716 memcpy(dst->cp_src, src->cp_src, sizeof(struct nl4_server));
ce0887ac
OK
1717 memcpy(&dst->stateid, &src->stateid, sizeof(src->stateid));
1718 memcpy(&dst->c_fh, &src->c_fh, sizeof(src->c_fh));
1719 dst->ss_mnt = src->ss_mnt;
e0639dc5
OK
1720}
1721
1722static void cleanup_async_copy(struct nfsd4_copy *copy)
1723{
624322f1 1724 nfs4_free_copy_state(copy);
5c4583b2 1725 nfsd_file_put(copy->nf_dst);
1913cdf5 1726 if (!nfsd4_ssc_is_inter(copy))
2e577f0f 1727 nfsd_file_put(copy->nf_src);
e0639dc5
OK
1728 spin_lock(&copy->cp_clp->async_lock);
1729 list_del(&copy->copies);
1730 spin_unlock(&copy->cp_clp->async_lock);
1731 nfs4_put_copy(copy);
1732}
1733
a11ada99 1734static void nfsd4_send_cb_offload(struct nfsd4_copy *copy, __be32 nfserr)
e72f9bc0 1735{
a11ada99 1736 struct nfsd4_cb_offload *cbo;
e72f9bc0 1737
a11ada99
CL
1738 cbo = kzalloc(sizeof(*cbo), GFP_KERNEL);
1739 if (!cbo)
e72f9bc0
CL
1740 return;
1741
a11ada99
CL
1742 memcpy(&cbo->co_res, &copy->cp_res, sizeof(copy->cp_res));
1743 memcpy(&cbo->co_fh, &copy->fh, sizeof(copy->fh));
1744 cbo->co_nfserr = nfserr;
e72f9bc0 1745
a11ada99
CL
1746 nfsd4_init_cb(&cbo->co_cb, copy->cp_clp, &nfsd4_cb_offload_ops,
1747 NFSPROC4_CLNT_CB_OFFLOAD);
1748 trace_nfsd_cb_offload(copy->cp_clp, &cbo->co_res.cb_stateid,
1749 &cbo->co_fh, copy->cp_count, nfserr);
1750 nfsd4_run_cb(&cbo->co_cb);
e72f9bc0
CL
1751}
1752
ad1e46c9
CL
1753/**
1754 * nfsd4_do_async_copy - kthread function for background server-side COPY
1755 * @data: arguments for COPY operation
1756 *
1757 * Return values:
1758 * %0: Copy operation is done.
1759 */
e0639dc5
OK
1760static int nfsd4_do_async_copy(void *data)
1761{
1762 struct nfsd4_copy *copy = (struct nfsd4_copy *)data;
a11ada99 1763 __be32 nfserr;
e0639dc5 1764
1913cdf5 1765 if (nfsd4_ssc_is_inter(copy)) {
ad1e46c9
CL
1766 struct file *filp;
1767
1768 filp = nfs42_ssc_open(copy->ss_mnt, &copy->c_fh,
1769 &copy->stateid);
1770 if (IS_ERR(filp)) {
a11ada99 1771 nfserr = nfserr_offload_denied;
ce0887ac
OK
1772 nfsd4_interssc_disconnect(copy->ss_mnt);
1773 goto do_callback;
1774 }
a11ada99
CL
1775 nfserr = nfsd4_do_copy(copy, filp, copy->nf_dst->nf_file,
1776 false);
ad1e46c9 1777 nfsd4_cleanup_inter_ssc(copy->ss_mnt, filp, copy->nf_dst);
478ed7b1 1778 } else {
a11ada99
CL
1779 nfserr = nfsd4_do_copy(copy, copy->nf_src->nf_file,
1780 copy->nf_dst->nf_file, false);
478ed7b1 1781 nfsd4_cleanup_intra_ssc(copy->nf_src, copy->nf_dst);
ce0887ac
OK
1782 }
1783
ce0887ac 1784do_callback:
a11ada99 1785 nfsd4_send_cb_offload(copy, nfserr);
e0639dc5
OK
1786 cleanup_async_copy(copy);
1787 return 0;
1788}
1789
29ae7f9d
AS
1790static __be32
1791nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 1792 union nfsd4_op_u *u)
29ae7f9d 1793{
eb69853d 1794 struct nfsd4_copy *copy = &u->copy;
29ae7f9d 1795 __be32 status;
e0639dc5 1796 struct nfsd4_copy *async_copy = NULL;
29ae7f9d 1797
1913cdf5
CL
1798 if (nfsd4_ssc_is_inter(copy)) {
1799 if (!inter_copy_offload_enable || nfsd4_copy_is_sync(copy)) {
ce0887ac
OK
1800 status = nfserr_notsupp;
1801 goto out;
1802 }
1803 status = nfsd4_setup_inter_ssc(rqstp, cstate, copy,
1804 &copy->ss_mnt);
1805 if (status)
1806 return nfserr_offload_denied;
1807 } else {
1808 status = nfsd4_setup_intra_ssc(rqstp, cstate, copy);
1809 if (status)
1810 return status;
1811 }
29ae7f9d 1812
e0639dc5
OK
1813 copy->cp_clp = cstate->clp;
1814 memcpy(&copy->fh, &cstate->current_fh.fh_handle,
1815 sizeof(struct knfsd_fh));
1913cdf5 1816 if (nfsd4_copy_is_async(copy)) {
e0639dc5 1817 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
29ae7f9d 1818
e0639dc5
OK
1819 status = nfserrno(-ENOMEM);
1820 async_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
1821 if (!async_copy)
ce0887ac 1822 goto out_err;
87689df6
CL
1823 async_copy->cp_src = kmalloc(sizeof(*async_copy->cp_src), GFP_KERNEL);
1824 if (!async_copy->cp_src)
1825 goto out_err;
ce0887ac
OK
1826 if (!nfs4_init_copy_state(nn, copy))
1827 goto out_err;
e0639dc5 1828 refcount_set(&async_copy->refcount, 1);
e739b120
OK
1829 memcpy(&copy->cp_res.cb_stateid, &copy->cp_stateid.stid,
1830 sizeof(copy->cp_res.cb_stateid));
eb162e17 1831 dup_copy_fields(copy, async_copy);
e0639dc5
OK
1832 async_copy->copy_task = kthread_create(nfsd4_do_async_copy,
1833 async_copy, "%s", "copy thread");
1834 if (IS_ERR(async_copy->copy_task))
1835 goto out_err;
1836 spin_lock(&async_copy->cp_clp->async_lock);
1837 list_add(&async_copy->copies,
1838 &async_copy->cp_clp->async_copies);
1839 spin_unlock(&async_copy->cp_clp->async_lock);
1840 wake_up_process(async_copy->copy_task);
29ae7f9d 1841 status = nfs_ok;
ce0887ac 1842 } else {
3b7bf593
CL
1843 status = nfsd4_do_copy(copy, copy->nf_src->nf_file,
1844 copy->nf_dst->nf_file, true);
478ed7b1 1845 nfsd4_cleanup_intra_ssc(copy->nf_src, copy->nf_dst);
ce0887ac 1846 }
29ae7f9d
AS
1847out:
1848 return status;
e0639dc5 1849out_err:
18f428d4
OK
1850 if (async_copy)
1851 cleanup_async_copy(async_copy);
ce0887ac 1852 status = nfserrno(-ENOMEM);
1913cdf5 1853 if (nfsd4_ssc_is_inter(copy))
ce0887ac 1854 nfsd4_interssc_disconnect(copy->ss_mnt);
e0639dc5
OK
1855 goto out;
1856}
1857
1858struct nfsd4_copy *
1859find_async_copy(struct nfs4_client *clp, stateid_t *stateid)
1860{
1861 struct nfsd4_copy *copy;
1862
1863 spin_lock(&clp->async_lock);
1864 list_for_each_entry(copy, &clp->async_copies, copies) {
ce0887ac 1865 if (memcmp(&copy->cp_stateid.stid, stateid, NFS4_STATEID_SIZE))
e0639dc5
OK
1866 continue;
1867 refcount_inc(&copy->refcount);
1868 spin_unlock(&clp->async_lock);
1869 return copy;
1870 }
1871 spin_unlock(&clp->async_lock);
1872 return NULL;
29ae7f9d
AS
1873}
1874
885e2bf3
OK
1875static __be32
1876nfsd4_offload_cancel(struct svc_rqst *rqstp,
1877 struct nfsd4_compound_state *cstate,
1878 union nfsd4_op_u *u)
1879{
e0639dc5 1880 struct nfsd4_offload_status *os = &u->offload_status;
e0639dc5
OK
1881 struct nfsd4_copy *copy;
1882 struct nfs4_client *clp = cstate->clp;
1883
1884 copy = find_async_copy(clp, &os->stateid);
ce0887ac
OK
1885 if (!copy) {
1886 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1887
1888 return manage_cpntf_state(nn, &os->stateid, clp, NULL);
1889 } else
e0639dc5 1890 nfsd4_stop_copy(copy);
e0639dc5 1891
ce0887ac 1892 return nfs_ok;
885e2bf3
OK
1893}
1894
51911868
OK
1895static __be32
1896nfsd4_copy_notify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1897 union nfsd4_op_u *u)
1898{
624322f1
OK
1899 struct nfsd4_copy_notify *cn = &u->copy_notify;
1900 __be32 status;
1901 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1902 struct nfs4_stid *stid;
1903 struct nfs4_cpntf_state *cps;
1904 struct nfs4_client *clp = cstate->clp;
1905
1906 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1907 &cn->cpn_src_stateid, RD_STATE, NULL,
1908 &stid);
1909 if (status)
1910 return status;
1911
1912 cn->cpn_sec = nn->nfsd4_lease;
1913 cn->cpn_nsec = 0;
1914
1915 status = nfserrno(-ENOMEM);
1916 cps = nfs4_alloc_init_cpntf_state(nn, stid);
1917 if (!cps)
1918 goto out;
1919 memcpy(&cn->cpn_cnr_stateid, &cps->cp_stateid.stid, sizeof(stateid_t));
1920 memcpy(&cps->cp_p_stateid, &stid->sc_stateid, sizeof(stateid_t));
1921 memcpy(&cps->cp_p_clid, &clp->cl_clientid, sizeof(clientid_t));
1922
1923 /* For now, only return one server address in cpn_src, the
1924 * address used by the client to connect to this server.
1925 */
09426ef2 1926 cn->cpn_src->nl4_type = NL4_NETADDR;
624322f1 1927 status = nfsd4_set_netaddr((struct sockaddr *)&rqstp->rq_daddr,
09426ef2 1928 &cn->cpn_src->u.nl4_addr);
624322f1
OK
1929 WARN_ON_ONCE(status);
1930 if (status) {
1931 nfs4_put_cpntf_state(nn, cps);
1932 goto out;
1933 }
1934out:
1935 nfs4_put_stid(stid);
1936 return status;
51911868
OK
1937}
1938
95d871f0
AS
1939static __be32
1940nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1941 struct nfsd4_fallocate *fallocate, int flags)
1942{
0d4d6720 1943 __be32 status;
5c4583b2 1944 struct nfsd_file *nf;
95d871f0 1945
aa0d6aed 1946 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
95d871f0 1947 &fallocate->falloc_stateid,
624322f1 1948 WR_STATE, &nf, NULL);
95d871f0
AS
1949 if (status != nfs_ok) {
1950 dprintk("NFSD: nfsd4_fallocate: couldn't process stateid!\n");
1951 return status;
1952 }
1953
5c4583b2 1954 status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, nf->nf_file,
95d871f0
AS
1955 fallocate->falloc_offset,
1956 fallocate->falloc_length,
1957 flags);
5c4583b2 1958 nfsd_file_put(nf);
95d871f0
AS
1959 return status;
1960}
6308bc98
OK
1961static __be32
1962nfsd4_offload_status(struct svc_rqst *rqstp,
1963 struct nfsd4_compound_state *cstate,
1964 union nfsd4_op_u *u)
1965{
e0639dc5
OK
1966 struct nfsd4_offload_status *os = &u->offload_status;
1967 __be32 status = 0;
1968 struct nfsd4_copy *copy;
1969 struct nfs4_client *clp = cstate->clp;
1970
1971 copy = find_async_copy(clp, &os->stateid);
1972 if (copy) {
1973 os->count = copy->cp_res.wr_bytes_written;
1974 nfs4_put_copy(copy);
1975 } else
1976 status = nfserr_bad_stateid;
1977
1978 return status;
6308bc98 1979}
95d871f0
AS
1980
1981static __be32
1982nfsd4_allocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 1983 union nfsd4_op_u *u)
95d871f0 1984{
eb69853d 1985 return nfsd4_fallocate(rqstp, cstate, &u->allocate, 0);
95d871f0
AS
1986}
1987
b0cb9085
AS
1988static __be32
1989nfsd4_deallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 1990 union nfsd4_op_u *u)
b0cb9085 1991{
eb69853d 1992 return nfsd4_fallocate(rqstp, cstate, &u->deallocate,
b0cb9085
AS
1993 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
1994}
1995
24bab491
AS
1996static __be32
1997nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 1998 union nfsd4_op_u *u)
24bab491 1999{
eb69853d 2000 struct nfsd4_seek *seek = &u->seek;
24bab491
AS
2001 int whence;
2002 __be32 status;
5c4583b2 2003 struct nfsd_file *nf;
24bab491 2004
aa0d6aed 2005 status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
24bab491 2006 &seek->seek_stateid,
624322f1 2007 RD_STATE, &nf, NULL);
24bab491
AS
2008 if (status) {
2009 dprintk("NFSD: nfsd4_seek: couldn't process stateid!\n");
2010 return status;
2011 }
2012
2013 switch (seek->seek_whence) {
2014 case NFS4_CONTENT_DATA:
2015 whence = SEEK_DATA;
2016 break;
2017 case NFS4_CONTENT_HOLE:
2018 whence = SEEK_HOLE;
2019 break;
2020 default:
2021 status = nfserr_union_notsupp;
2022 goto out;
2023 }
2024
2025 /*
2026 * Note: This call does change file->f_pos, but nothing in NFSD
2027 * should ever file->f_pos.
2028 */
5c4583b2 2029 seek->seek_pos = vfs_llseek(nf->nf_file, seek->seek_offset, whence);
24bab491
AS
2030 if (seek->seek_pos < 0)
2031 status = nfserrno(seek->seek_pos);
5c4583b2 2032 else if (seek->seek_pos >= i_size_read(file_inode(nf->nf_file)))
24bab491
AS
2033 seek->seek_eof = true;
2034
2035out:
5c4583b2 2036 nfsd_file_put(nf);
24bab491
AS
2037 return status;
2038}
2039
1da177e4
LT
2040/* This routine never returns NFS_OK! If there are no other errors, it
2041 * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
2042 * attributes matched. VERIFY is implemented by mapping NFSERR_SAME
2043 * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
2044 */
b37ad28b 2045static __be32
c954e2a5 2046_nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
ca364317 2047 struct nfsd4_verify *verify)
1da177e4 2048{
2ebbc012 2049 __be32 *buf, *p;
1da177e4 2050 int count;
b37ad28b 2051 __be32 status;
1da177e4 2052
8837abca 2053 status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
1da177e4
LT
2054 if (status)
2055 return status;
2056
3c8e0316
YZ
2057 status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
2058 if (status)
2059 return status;
2060
1da177e4
LT
2061 if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
2062 || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
2063 return nfserr_inval;
2064 if (verify->ve_attrlen & 3)
2065 return nfserr_inval;
2066
2067 /* count in words:
2068 * bitmap_len(1) + bitmap(2) + attr_len(1) = 4
2069 */
2070 count = 4 + (verify->ve_attrlen >> 2);
2071 buf = kmalloc(count << 2, GFP_KERNEL);
2072 if (!buf)
3e772463 2073 return nfserr_jukebox;
1da177e4 2074
84822d0b 2075 p = buf;
d5184658 2076 status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
ca364317 2077 cstate->current_fh.fh_export,
d5184658
BF
2078 cstate->current_fh.fh_dentry,
2079 verify->ve_bmval,
406a7ea9 2080 rqstp, 0);
41ae6e71
BF
2081 /*
2082 * If nfsd4_encode_fattr() ran out of space, assume that's because
2083 * the attributes are longer (hence different) than those given:
2084 */
84822d0b 2085 if (status == nfserr_resource)
1da177e4
LT
2086 status = nfserr_not_same;
2087 if (status)
2088 goto out_kfree;
2089
95ec28cd
BH
2090 /* skip bitmap */
2091 p = buf + 1 + ntohl(buf[0]);
1da177e4
LT
2092 status = nfserr_not_same;
2093 if (ntohl(*p++) != verify->ve_attrlen)
2094 goto out_kfree;
2095 if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
2096 status = nfserr_same;
2097
2098out_kfree:
2099 kfree(buf);
2100 return status;
2101}
2102
c954e2a5
BF
2103static __be32
2104nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 2105 union nfsd4_op_u *u)
c954e2a5
BF
2106{
2107 __be32 status;
2108
eb69853d 2109 status = _nfsd4_verify(rqstp, cstate, &u->verify);
c954e2a5
BF
2110 return status == nfserr_not_same ? nfs_ok : status;
2111}
2112
2113static __be32
2114nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 2115 union nfsd4_op_u *u)
c954e2a5
BF
2116{
2117 __be32 status;
2118
eb69853d 2119 status = _nfsd4_verify(rqstp, cstate, &u->nverify);
c954e2a5
BF
2120 return status == nfserr_same ? nfs_ok : status;
2121}
2122
9cf514cc
CH
2123#ifdef CONFIG_NFSD_PNFS
2124static const struct nfsd4_layout_ops *
2125nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type)
2126{
8a4c3926 2127 if (!exp->ex_layout_types) {
9cf514cc
CH
2128 dprintk("%s: export does not support pNFS\n", __func__);
2129 return NULL;
2130 }
2131
b550a32e
AK
2132 if (layout_type >= LAYOUT_TYPE_MAX ||
2133 !(exp->ex_layout_types & (1 << layout_type))) {
9cf514cc
CH
2134 dprintk("%s: layout type %d not supported\n",
2135 __func__, layout_type);
2136 return NULL;
2137 }
2138
2139 return nfsd4_layout_ops[layout_type];
2140}
2141
2142static __be32
2143nfsd4_getdeviceinfo(struct svc_rqst *rqstp,
eb69853d 2144 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
9cf514cc 2145{
eb69853d 2146 struct nfsd4_getdeviceinfo *gdp = &u->getdeviceinfo;
9cf514cc
CH
2147 const struct nfsd4_layout_ops *ops;
2148 struct nfsd4_deviceid_map *map;
2149 struct svc_export *exp;
2150 __be32 nfserr;
2151
2152 dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n",
2153 __func__,
2154 gdp->gd_layout_type,
2155 gdp->gd_devid.fsid_idx, gdp->gd_devid.generation,
2156 gdp->gd_maxcount);
2157
2158 map = nfsd4_find_devid_map(gdp->gd_devid.fsid_idx);
2159 if (!map) {
2160 dprintk("%s: couldn't find device ID to export mapping!\n",
2161 __func__);
2162 return nfserr_noent;
2163 }
2164
2165 exp = rqst_exp_find(rqstp, map->fsid_type, map->fsid);
2166 if (IS_ERR(exp)) {
2167 dprintk("%s: could not find device id\n", __func__);
2168 return nfserr_noent;
2169 }
2170
2171 nfserr = nfserr_layoutunavailable;
2172 ops = nfsd4_layout_verify(exp, gdp->gd_layout_type);
2173 if (!ops)
2174 goto out;
2175
2176 nfserr = nfs_ok;
f99d4fbd
CH
2177 if (gdp->gd_maxcount != 0) {
2178 nfserr = ops->proc_getdeviceinfo(exp->ex_path.mnt->mnt_sb,
ec59659b 2179 rqstp, cstate->clp, gdp);
f99d4fbd 2180 }
9cf514cc
CH
2181
2182 gdp->gd_notify_types &= ops->notify_types;
9cf514cc 2183out:
a1420384 2184 exp_put(exp);
9cf514cc
CH
2185 return nfserr;
2186}
2187
34b1744c
BF
2188static void
2189nfsd4_getdeviceinfo_release(union nfsd4_op_u *u)
2190{
2191 kfree(u->getdeviceinfo.gd_device);
2192}
2193
9cf514cc
CH
2194static __be32
2195nfsd4_layoutget(struct svc_rqst *rqstp,
eb69853d 2196 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
9cf514cc 2197{
eb69853d 2198 struct nfsd4_layoutget *lgp = &u->layoutget;
9cf514cc
CH
2199 struct svc_fh *current_fh = &cstate->current_fh;
2200 const struct nfsd4_layout_ops *ops;
2201 struct nfs4_layout_stateid *ls;
2202 __be32 nfserr;
66282ec1 2203 int accmode = NFSD_MAY_READ_IF_EXEC;
9cf514cc
CH
2204
2205 switch (lgp->lg_seg.iomode) {
2206 case IOMODE_READ:
66282ec1 2207 accmode |= NFSD_MAY_READ;
9cf514cc
CH
2208 break;
2209 case IOMODE_RW:
66282ec1 2210 accmode |= NFSD_MAY_READ | NFSD_MAY_WRITE;
9cf514cc
CH
2211 break;
2212 default:
2213 dprintk("%s: invalid iomode %d\n",
2214 __func__, lgp->lg_seg.iomode);
2215 nfserr = nfserr_badiomode;
2216 goto out;
2217 }
2218
2219 nfserr = fh_verify(rqstp, current_fh, 0, accmode);
2220 if (nfserr)
2221 goto out;
2222
2223 nfserr = nfserr_layoutunavailable;
2224 ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);
2225 if (!ops)
2226 goto out;
2227
2228 /*
2229 * Verify minlength and range as per RFC5661:
2230 * o If loga_length is less than loga_minlength,
2231 * the metadata server MUST return NFS4ERR_INVAL.
2232 * o If the sum of loga_offset and loga_minlength exceeds
2233 * NFS4_UINT64_MAX, and loga_minlength is not
2234 * NFS4_UINT64_MAX, the error NFS4ERR_INVAL MUST result.
2235 * o If the sum of loga_offset and loga_length exceeds
2236 * NFS4_UINT64_MAX, and loga_length is not NFS4_UINT64_MAX,
2237 * the error NFS4ERR_INVAL MUST result.
2238 */
2239 nfserr = nfserr_inval;
2240 if (lgp->lg_seg.length < lgp->lg_minlength ||
2241 (lgp->lg_minlength != NFS4_MAX_UINT64 &&
2242 lgp->lg_minlength > NFS4_MAX_UINT64 - lgp->lg_seg.offset) ||
2243 (lgp->lg_seg.length != NFS4_MAX_UINT64 &&
2244 lgp->lg_seg.length > NFS4_MAX_UINT64 - lgp->lg_seg.offset))
2245 goto out;
2246 if (lgp->lg_seg.length == 0)
2247 goto out;
2248
2249 nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lgp->lg_sid,
2250 true, lgp->lg_layout_type, &ls);
31ef83dc 2251 if (nfserr) {
f394b62b 2252 trace_nfsd_layout_get_lookup_fail(&lgp->lg_sid);
9cf514cc 2253 goto out;
31ef83dc 2254 }
9cf514cc 2255
c5c707f9
CH
2256 nfserr = nfserr_recallconflict;
2257 if (atomic_read(&ls->ls_stid.sc_file->fi_lo_recalls))
2258 goto out_put_stid;
2259
2b0143b5 2260 nfserr = ops->proc_layoutget(d_inode(current_fh->fh_dentry),
9cf514cc
CH
2261 current_fh, lgp);
2262 if (nfserr)
2263 goto out_put_stid;
2264
2265 nfserr = nfsd4_insert_layout(lgp, ls);
2266
2267out_put_stid:
cc8a5532 2268 mutex_unlock(&ls->ls_mutex);
9cf514cc
CH
2269 nfs4_put_stid(&ls->ls_stid);
2270out:
2271 return nfserr;
2272}
2273
34b1744c
BF
2274static void
2275nfsd4_layoutget_release(union nfsd4_op_u *u)
2276{
2277 kfree(u->layoutget.lg_content);
2278}
2279
9cf514cc
CH
2280static __be32
2281nfsd4_layoutcommit(struct svc_rqst *rqstp,
eb69853d 2282 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
9cf514cc 2283{
eb69853d 2284 struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
9cf514cc
CH
2285 const struct nfsd4_layout_seg *seg = &lcp->lc_seg;
2286 struct svc_fh *current_fh = &cstate->current_fh;
2287 const struct nfsd4_layout_ops *ops;
2288 loff_t new_size = lcp->lc_last_wr + 1;
2289 struct inode *inode;
2290 struct nfs4_layout_stateid *ls;
2291 __be32 nfserr;
2292
2293 nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_WRITE);
2294 if (nfserr)
2295 goto out;
2296
2297 nfserr = nfserr_layoutunavailable;
2298 ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
2299 if (!ops)
2300 goto out;
2b0143b5 2301 inode = d_inode(current_fh->fh_dentry);
9cf514cc
CH
2302
2303 nfserr = nfserr_inval;
2304 if (new_size <= seg->offset) {
2305 dprintk("pnfsd: last write before layout segment\n");
2306 goto out;
2307 }
2308 if (new_size > seg->offset + seg->length) {
2309 dprintk("pnfsd: last write beyond layout segment\n");
2310 goto out;
2311 }
2312 if (!lcp->lc_newoffset && new_size > i_size_read(inode)) {
2313 dprintk("pnfsd: layoutcommit beyond EOF\n");
2314 goto out;
2315 }
2316
2317 nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lcp->lc_sid,
2318 false, lcp->lc_layout_type,
2319 &ls);
2320 if (nfserr) {
f394b62b 2321 trace_nfsd_layout_commit_lookup_fail(&lcp->lc_sid);
9cf514cc
CH
2322 /* fixup error code as per RFC5661 */
2323 if (nfserr == nfserr_bad_stateid)
2324 nfserr = nfserr_badlayout;
2325 goto out;
2326 }
2327
cc8a5532
JL
2328 /* LAYOUTCOMMIT does not require any serialization */
2329 mutex_unlock(&ls->ls_mutex);
2330
9cf514cc
CH
2331 if (new_size > i_size_read(inode)) {
2332 lcp->lc_size_chg = 1;
2333 lcp->lc_newsize = new_size;
2334 } else {
2335 lcp->lc_size_chg = 0;
2336 }
2337
d8398fc1 2338 nfserr = ops->proc_layoutcommit(inode, lcp);
9cf514cc
CH
2339 nfs4_put_stid(&ls->ls_stid);
2340out:
2341 return nfserr;
2342}
2343
2344static __be32
2345nfsd4_layoutreturn(struct svc_rqst *rqstp,
eb69853d 2346 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
9cf514cc 2347{
eb69853d 2348 struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
9cf514cc
CH
2349 struct svc_fh *current_fh = &cstate->current_fh;
2350 __be32 nfserr;
2351
2352 nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_NOP);
2353 if (nfserr)
2354 goto out;
2355
2356 nfserr = nfserr_layoutunavailable;
2357 if (!nfsd4_layout_verify(current_fh->fh_export, lrp->lr_layout_type))
2358 goto out;
2359
2360 switch (lrp->lr_seg.iomode) {
2361 case IOMODE_READ:
2362 case IOMODE_RW:
2363 case IOMODE_ANY:
2364 break;
2365 default:
2366 dprintk("%s: invalid iomode %d\n", __func__,
2367 lrp->lr_seg.iomode);
2368 nfserr = nfserr_inval;
2369 goto out;
2370 }
2371
2372 switch (lrp->lr_return_type) {
2373 case RETURN_FILE:
2374 nfserr = nfsd4_return_file_layouts(rqstp, cstate, lrp);
2375 break;
2376 case RETURN_FSID:
2377 case RETURN_ALL:
2378 nfserr = nfsd4_return_client_layouts(rqstp, cstate, lrp);
2379 break;
2380 default:
2381 dprintk("%s: invalid return_type %d\n", __func__,
2382 lrp->lr_return_type);
2383 nfserr = nfserr_inval;
2384 break;
2385 }
2386out:
2387 return nfserr;
2388}
2389#endif /* CONFIG_NFSD_PNFS */
2390
23e50fe3
FL
2391static __be32
2392nfsd4_getxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2393 union nfsd4_op_u *u)
2394{
2395 struct nfsd4_getxattr *getxattr = &u->getxattr;
2396
2397 return nfsd_getxattr(rqstp, &cstate->current_fh,
2398 getxattr->getxa_name, &getxattr->getxa_buf,
2399 &getxattr->getxa_len);
2400}
2401
2402static __be32
2403nfsd4_setxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2404 union nfsd4_op_u *u)
2405{
2406 struct nfsd4_setxattr *setxattr = &u->setxattr;
2407 __be32 ret;
2408
2409 if (opens_in_grace(SVC_NET(rqstp)))
2410 return nfserr_grace;
2411
2412 ret = nfsd_setxattr(rqstp, &cstate->current_fh, setxattr->setxa_name,
2413 setxattr->setxa_buf, setxattr->setxa_len,
2414 setxattr->setxa_flags);
2415
2416 if (!ret)
2417 set_change_info(&setxattr->setxa_cinfo, &cstate->current_fh);
2418
2419 return ret;
2420}
2421
2422static __be32
2423nfsd4_listxattrs(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2424 union nfsd4_op_u *u)
2425{
2426 /*
2427 * Get the entire list, then copy out only the user attributes
2428 * in the encode function.
2429 */
2430 return nfsd_listxattr(rqstp, &cstate->current_fh,
2431 &u->listxattrs.lsxa_buf, &u->listxattrs.lsxa_len);
2432}
2433
2434static __be32
2435nfsd4_removexattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2436 union nfsd4_op_u *u)
2437{
2438 struct nfsd4_removexattr *removexattr = &u->removexattr;
2439 __be32 ret;
2440
2441 if (opens_in_grace(SVC_NET(rqstp)))
2442 return nfserr_grace;
2443
2444 ret = nfsd_removexattr(rqstp, &cstate->current_fh,
2445 removexattr->rmxa_name);
2446
2447 if (!ret)
2448 set_change_info(&removexattr->rmxa_cinfo, &cstate->current_fh);
2449
2450 return ret;
2451}
2452
1da177e4
LT
2453/*
2454 * NULL call.
2455 */
7111c66e 2456static __be32
a6beb732 2457nfsd4_proc_null(struct svc_rqst *rqstp)
1da177e4 2458{
cc028a10 2459 return rpc_success;
1da177e4
LT
2460}
2461
e2b20950
SA
2462static inline void nfsd4_increment_op_stats(u32 opnum)
2463{
2464 if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
e567b98c 2465 percpu_counter_inc(&nfsdstats.counter[NFSD_STATS_NFS4_OP(opnum)]);
e2b20950
SA
2466}
2467
bb2a8b0c 2468static const struct nfsd4_operation nfsd4_ops[];
b591480b 2469
f1c7f79b 2470static const char *nfsd4_op_name(unsigned opnum);
b001a1b6 2471
f9bb94c4 2472/*
57716355 2473 * Enforce NFSv4.1 COMPOUND ordering rules:
f9bb94c4 2474 *
57716355
BF
2475 * Also note, enforced elsewhere:
2476 * - SEQUENCE other than as first op results in
2477 * NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
1d1bc8f2
BF
2478 * - BIND_CONN_TO_SESSION must be the only op in its compound.
2479 * (Enforced in nfsd4_bind_conn_to_session().)
57716355
BF
2480 * - DESTROY_SESSION must be the final operation in a compound, if
2481 * sessionid's in SEQUENCE and DESTROY_SESSION are the same.
2482 * (Enforced in nfsd4_destroy_session().)
f9bb94c4 2483 */
57716355 2484static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
f9bb94c4 2485{
7a04cfda 2486 struct nfsd4_op *first_op = &args->ops[0];
57716355
BF
2487
2488 /* These ordering requirements don't apply to NFSv4.0: */
2489 if (args->minorversion == 0)
2490 return nfs_ok;
2491 /* This is weird, but OK, not our problem: */
2492 if (args->opcnt == 0)
2493 return nfs_ok;
7a04cfda 2494 if (first_op->status == nfserr_op_illegal)
57716355 2495 return nfs_ok;
7a04cfda 2496 if (!(nfsd4_ops[first_op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
57716355 2497 return nfserr_op_not_in_session;
7a04cfda 2498 if (first_op->opnum == OP_SEQUENCE)
57716355 2499 return nfs_ok;
7a04cfda
BF
2500 /*
2501 * So first_op is something allowed outside a session, like
2502 * EXCHANGE_ID; but then it has to be the only op in the
2503 * compound:
2504 */
57716355
BF
2505 if (args->opcnt != 1)
2506 return nfserr_not_only_op;
2507 return nfs_ok;
f9bb94c4
AA
2508}
2509
f4f9ef4a 2510const struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
22b03214
BF
2511{
2512 return &nfsd4_ops[op->opnum];
2513}
2514
1091006c
BF
2515bool nfsd4_cache_this_op(struct nfsd4_op *op)
2516{
e372ba60
BF
2517 if (op->opnum == OP_ILLEGAL)
2518 return false;
c856694e 2519 return OPDESC(op)->op_flags & OP_CACHEME;
1091006c
BF
2520}
2521
68d93184
BF
2522static bool need_wrongsec_check(struct svc_rqst *rqstp)
2523{
2524 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2525 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
2526 struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
2527 struct nfsd4_op *next = &argp->ops[resp->opcnt];
bb2a8b0c
CH
2528 const struct nfsd4_operation *thisd = OPDESC(this);
2529 const struct nfsd4_operation *nextd;
68d93184 2530
68d93184
BF
2531 /*
2532 * Most ops check wronsec on our own; only the putfh-like ops
2533 * have special rules.
2534 */
2535 if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
2536 return false;
2537 /*
2538 * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
2539 * put-filehandle operation if we're not going to use the
2540 * result:
2541 */
2542 if (argp->opcnt == resp->opcnt)
2543 return false;
51904b08
BF
2544 if (next->opnum == OP_ILLEGAL)
2545 return false;
68d93184
BF
2546 nextd = OPDESC(next);
2547 /*
2548 * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
2549 * errors themselves as necessary; others should check for them
2550 * now:
2551 */
2552 return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
2553}
2554
b9e8638e
OK
2555#ifdef CONFIG_NFSD_V4_2_INTER_SSC
2556static void
2557check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
2558{
2559 struct nfsd4_op *op, *current_op = NULL, *saved_op = NULL;
2560 struct nfsd4_copy *copy;
2561 struct nfsd4_putfh *putfh;
2562 int i;
2563
2564 /* traverse all operation and if it's a COPY compound, mark the
2565 * source filehandle to skip verification
2566 */
2567 for (i = 0; i < args->opcnt; i++) {
2568 op = &args->ops[i];
2569 if (op->opnum == OP_PUTFH)
2570 current_op = op;
2571 else if (op->opnum == OP_SAVEFH)
2572 saved_op = current_op;
2573 else if (op->opnum == OP_RESTOREFH)
2574 current_op = saved_op;
2575 else if (op->opnum == OP_COPY) {
2576 copy = (struct nfsd4_copy *)&op->u;
2577 if (!saved_op) {
2578 op->status = nfserr_nofilehandle;
2579 return;
2580 }
2581 putfh = (struct nfsd4_putfh *)&saved_op->u;
1913cdf5 2582 if (nfsd4_ssc_is_inter(copy))
b9e8638e
OK
2583 putfh->no_verify = true;
2584 }
2585 }
2586}
2587#else
2588static void
2589check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
2590{
2591}
2592#endif
2593
1da177e4
LT
2594/*
2595 * COMPOUND call.
2596 */
7111c66e 2597static __be32
a6beb732 2598nfsd4_proc_compound(struct svc_rqst *rqstp)
1da177e4 2599{
a6beb732
CH
2600 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2601 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1da177e4 2602 struct nfsd4_op *op;
e354d571 2603 struct nfsd4_compound_state *cstate = &resp->cstate;
4daeed25
KM
2604 struct svc_fh *current_fh = &cstate->current_fh;
2605 struct svc_fh *save_fh = &cstate->save_fh;
e333f3bb 2606 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
b37ad28b 2607 __be32 status;
1da177e4 2608
bddfdbcd 2609 resp->xdr = &rqstp->rq_res_stream;
3b0ebb25 2610 resp->statusp = resp->xdr->p;
bddfdbcd
CL
2611
2612 /* reserve space for: NFS status code */
2613 xdr_reserve_space(resp->xdr, XDR_UNIT);
2614
1da177e4 2615 /* reserve space for: taglen, tag, and opcnt */
bddfdbcd 2616 xdr_reserve_space(resp->xdr, XDR_UNIT * 2 + args->taglen);
1da177e4
LT
2617 resp->taglen = args->taglen;
2618 resp->tag = args->tag;
1da177e4 2619 resp->rqstp = rqstp;
4daeed25 2620 cstate->minorversion = args->minorversion;
4daeed25
KM
2621 fh_init(current_fh, NFS4_FHSIZE);
2622 fh_init(save_fh, NFS4_FHSIZE);
28df0988 2623
8ff30fa4
N
2624 /*
2625 * Don't use the deferral mechanism for NFSv4; compounds make it
2626 * too hard to avoid non-idempotency problems.
2627 */
28df0988 2628 __clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
1da177e4
LT
2629
2630 /*
2631 * According to RFC3010, this takes precedence over all other errors.
2632 */
2633 status = nfserr_minor_vers_mismatch;
e333f3bb 2634 if (nfsd_minorversion(nn, args->minorversion, NFSD_TEST) <= 0)
1da177e4 2635 goto out;
0078117c
BF
2636 status = nfserr_resource;
2637 if (args->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
2638 goto out;
1da177e4 2639
57716355
BF
2640 status = nfs41_check_op_ordering(args);
2641 if (status) {
f9bb94c4 2642 op = &args->ops[0];
57716355 2643 op->status = status;
5b7b15ae 2644 resp->opcnt = 1;
f9bb94c4
AA
2645 goto encode_op;
2646 }
b9e8638e 2647 check_if_stalefh_allowed(args);
f9bb94c4 2648
28df3d15
BF
2649 rqstp->rq_lease_breaker = (void **)&cstate->clp;
2650
fff4080b 2651 trace_nfsd_compound(rqstp, args->opcnt);
1da177e4
LT
2652 while (!status && resp->opcnt < args->opcnt) {
2653 op = &args->ops[resp->opcnt++];
2654
2655 /*
2656 * The XDR decode routines may have pre-set op->status;
2657 * for example, if there is a miscellaneous XDR error
2658 * it will be set to nfserr_bad_xdr.
2659 */
9d313b17
BF
2660 if (op->status) {
2661 if (op->opnum == OP_OPEN)
2662 op->status = nfsd4_open_omfg(rqstp, cstate, op);
1da177e4 2663 goto encode_op;
9d313b17 2664 }
b9e8638e
OK
2665 if (!current_fh->fh_dentry &&
2666 !HAS_FH_FLAG(current_fh, NFSD4_FH_FOREIGN)) {
f4f9ef4a 2667 if (!(op->opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
42ca0993
BF
2668 op->status = nfserr_nofilehandle;
2669 goto encode_op;
2670 }
b9e8638e
OK
2671 } else if (current_fh->fh_export &&
2672 current_fh->fh_export->ex_fslocs.migrated &&
f4f9ef4a 2673 !(op->opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
42ca0993 2674 op->status = nfserr_moved;
1da177e4
LT
2675 goto encode_op;
2676 }
b591480b 2677
fcb5e3fa 2678 fh_clear_pre_post_attrs(current_fh);
2336745e 2679
58e7b33a 2680 /* If op is non-idempotent */
f4f9ef4a 2681 if (op->opdesc->op_flags & OP_MODIFIES_SOMETHING) {
4c69d585 2682 /*
a8095f7e 2683 * Don't execute this op if we couldn't encode a
f532c9ff 2684 * successful reply:
a8095f7e 2685 */
f4f9ef4a 2686 u32 plen = op->opdesc->op_rsize_bop(rqstp, op);
a8095f7e
BF
2687 /*
2688 * Plus if there's another operation, make sure
4c69d585
BF
2689 * we'll have space to at least encode an error:
2690 */
2691 if (resp->opcnt < args->opcnt)
2692 plen += COMPOUND_ERR_SLACK_SPACE;
58e7b33a
MJ
2693 op->status = nfsd4_check_resp_size(resp, plen);
2694 }
2695
2696 if (op->status)
2697 goto encode_op;
2698
f4f9ef4a
BF
2699 if (op->opdesc->op_get_currentstateid)
2700 op->opdesc->op_get_currentstateid(cstate, &op->u);
2701 op->status = op->opdesc->op_func(rqstp, cstate, &op->u);
1da177e4 2702
9a307403
BF
2703 /* Only from SEQUENCE */
2704 if (cstate->status == nfserr_replay_cache) {
2705 dprintk("%s NFS4.1 replay from cache\n", __func__);
2706 status = op->status;
2707 goto out;
2708 }
8b70484c 2709 if (!op->status) {
f4f9ef4a
BF
2710 if (op->opdesc->op_set_currentstateid)
2711 op->opdesc->op_set_currentstateid(cstate, &op->u);
8b70484c 2712
f4f9ef4a 2713 if (op->opdesc->op_flags & OP_CLEAR_STATEID)
37c593c5 2714 clear_current_stateid(cstate);
8b70484c 2715
d781e3df
BF
2716 if (current_fh->fh_export &&
2717 need_wrongsec_check(rqstp))
4daeed25 2718 op->status = check_nfsd_access(current_fh->fh_export, rqstp);
8b70484c 2719 }
1da177e4 2720encode_op:
a90b061c 2721 if (op->status == nfserr_replay_me) {
a4f1706a 2722 op->replay = &cstate->replay_owner->so_replay;
bddfdbcd 2723 nfsd4_encode_replay(resp->xdr, op);
1da177e4
LT
2724 status = op->status = op->replay->rp_status;
2725 } else {
2726 nfsd4_encode_operation(resp, op);
2727 status = op->status;
2728 }
0407717d 2729
fff4080b
CL
2730 trace_nfsd_compound_status(args->opcnt, resp->opcnt, status,
2731 nfsd4_op_name(op->opnum));
0407717d 2732
58fb12e6 2733 nfsd4_cstate_clear_replay(cstate);
e2b20950 2734 nfsd4_increment_op_stats(op->opnum);
1da177e4
LT
2735 }
2736
4daeed25
KM
2737 fh_put(current_fh);
2738 fh_put(save_fh);
2739 BUG_ON(cstate->replay_owner);
1da177e4 2740out:
cc028a10 2741 cstate->status = status;
2f425878 2742 /* Reset deferral mechanism for RPC deferrals */
28df0988 2743 __set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
cc028a10 2744 return rpc_success;
1da177e4
LT
2745}
2746
58e7b33a
MJ
2747#define op_encode_hdr_size (2)
2748#define op_encode_stateid_maxsz (XDR_QUADLEN(NFS4_STATEID_SIZE))
2749#define op_encode_verifier_maxsz (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
2750#define op_encode_change_info_maxsz (5)
2751#define nfs4_fattr_bitmap_maxsz (4)
2752
8c7424cf
BF
2753/* We'll fall back on returning no lockowner if run out of space: */
2754#define op_encode_lockowner_maxsz (0)
58e7b33a
MJ
2755#define op_encode_lock_denied_maxsz (8 + op_encode_lockowner_maxsz)
2756
2757#define nfs4_owner_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
2758
2759#define op_encode_ace_maxsz (3 + nfs4_owner_maxsz)
2760#define op_encode_delegation_maxsz (1 + op_encode_stateid_maxsz + 1 + \
2761 op_encode_ace_maxsz)
2762
2763#define op_encode_channel_attrs_maxsz (6 + 1 + 1)
2764
2765static inline u32 nfsd4_only_status_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2766{
2767 return (op_encode_hdr_size) * sizeof(__be32);
2768}
2769
2770static inline u32 nfsd4_status_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2771{
2772 return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
2773}
2774
2282cd2c
KM
2775static inline u32 nfsd4_access_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2776{
2777 /* ac_supported, ac_resp_access */
2778 return (op_encode_hdr_size + 2)* sizeof(__be32);
2779}
2780
58e7b33a
MJ
2781static inline u32 nfsd4_commit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2782{
2783 return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
2784}
2785
2786static inline u32 nfsd4_create_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2787{
2788 return (op_encode_hdr_size + op_encode_change_info_maxsz
2789 + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
2790}
2791
b86cef60
BF
2792/*
2793 * Note since this is an idempotent operation we won't insist on failing
2794 * the op prematurely if the estimate is too large. We may turn off splice
2795 * reads unnecessarily.
2796 */
2797static inline u32 nfsd4_getattr_rsize(struct svc_rqst *rqstp,
2798 struct nfsd4_op *op)
2799{
2800 u32 *bmap = op->u.getattr.ga_bmval;
2801 u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
2802 u32 ret = 0;
2803
2804 if (bmap0 & FATTR4_WORD0_ACL)
2805 return svc_max_payload(rqstp);
2806 if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
2807 return svc_max_payload(rqstp);
2808
2809 if (bmap1 & FATTR4_WORD1_OWNER) {
2810 ret += IDMAP_NAMESZ + 4;
2811 bmap1 &= ~FATTR4_WORD1_OWNER;
2812 }
2813 if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
2814 ret += IDMAP_NAMESZ + 4;
2815 bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
2816 }
2817 if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
2818 ret += NFS4_FHSIZE + 4;
2819 bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
2820 }
2821 if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
1ec8c0c4 2822 ret += NFS4_MAXLABELLEN + 12;
b86cef60
BF
2823 bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2824 }
2825 /*
2826 * Largest of remaining attributes are 16 bytes (e.g.,
2827 * supported_attributes)
2828 */
2829 ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
2830 /* bitmask, length */
2831 ret += 20;
2832 return ret;
2833}
2834
2282cd2c
KM
2835static inline u32 nfsd4_getfh_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2836{
2837 return (op_encode_hdr_size + 1) * sizeof(__be32) + NFS4_FHSIZE;
2838}
2839
58e7b33a
MJ
2840static inline u32 nfsd4_link_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2841{
2842 return (op_encode_hdr_size + op_encode_change_info_maxsz)
2843 * sizeof(__be32);
2844}
2845
2846static inline u32 nfsd4_lock_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2847{
2848 return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
2849 * sizeof(__be32);
2850}
2851
2852static inline u32 nfsd4_open_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2853{
2854 return (op_encode_hdr_size + op_encode_stateid_maxsz
2855 + op_encode_change_info_maxsz + 1
2856 + nfs4_fattr_bitmap_maxsz
2857 + op_encode_delegation_maxsz) * sizeof(__be32);
2858}
2859
2860static inline u32 nfsd4_read_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2861{
2862 u32 maxcount = 0, rlen = 0;
2863
2864 maxcount = svc_max_payload(rqstp);
3c7aa15d 2865 rlen = min(op->u.read.rd_length, maxcount);
58e7b33a 2866
622f560e 2867 return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
58e7b33a
MJ
2868}
2869
528b8493
AS
2870static inline u32 nfsd4_read_plus_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2871{
2872 u32 maxcount = svc_max_payload(rqstp);
2873 u32 rlen = min(op->u.read.rd_length, maxcount);
2874 /*
2875 * If we detect that the file changed during hole encoding, then we
2876 * recover by encoding the remaining reply as data. This means we need
2877 * to set aside enough room to encode two data segments.
2878 */
2879 u32 seg_len = 2 * (1 + 2 + 1);
2880
2881 return (op_encode_hdr_size + 2 + seg_len + XDR_QUADLEN(rlen)) * sizeof(__be32);
2882}
2883
58e7b33a
MJ
2884static inline u32 nfsd4_readdir_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2885{
3c7aa15d 2886 u32 maxcount = 0, rlen = 0;
58e7b33a 2887
3c7aa15d
KM
2888 maxcount = svc_max_payload(rqstp);
2889 rlen = min(op->u.readdir.rd_maxcount, maxcount);
58e7b33a 2890
561f0ed4
BF
2891 return (op_encode_hdr_size + op_encode_verifier_maxsz +
2892 XDR_QUADLEN(rlen)) * sizeof(__be32);
58e7b33a
MJ
2893}
2894
2282cd2c
KM
2895static inline u32 nfsd4_readlink_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2896{
2897 return (op_encode_hdr_size + 1) * sizeof(__be32) + PAGE_SIZE;
2898}
2899
58e7b33a
MJ
2900static inline u32 nfsd4_remove_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2901{
2902 return (op_encode_hdr_size + op_encode_change_info_maxsz)
2903 * sizeof(__be32);
2904}
2905
2906static inline u32 nfsd4_rename_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2907{
2908 return (op_encode_hdr_size + op_encode_change_info_maxsz
2909 + op_encode_change_info_maxsz) * sizeof(__be32);
2910}
2911
ccae70a9
BF
2912static inline u32 nfsd4_sequence_rsize(struct svc_rqst *rqstp,
2913 struct nfsd4_op *op)
2914{
d1d84c96
BF
2915 return (op_encode_hdr_size
2916 + XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32);
ccae70a9
BF
2917}
2918
2282cd2c
KM
2919static inline u32 nfsd4_test_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2920{
2921 return (op_encode_hdr_size + 1 + op->u.test_stateid.ts_num_ids)
2922 * sizeof(__be32);
2923}
2924
58e7b33a
MJ
2925static inline u32 nfsd4_setattr_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2926{
2927 return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
2928}
2929
2282cd2c
KM
2930static inline u32 nfsd4_secinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2931{
2932 return (op_encode_hdr_size + RPC_AUTH_MAXFLAVOR *
2933 (4 + XDR_QUADLEN(GSS_OID_MAX_LEN))) * sizeof(__be32);
2934}
2935
58e7b33a
MJ
2936static inline u32 nfsd4_setclientid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2937{
480efaee
BF
2938 return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
2939 sizeof(__be32);
58e7b33a
MJ
2940}
2941
2942static inline u32 nfsd4_write_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2943{
f34e432b 2944 return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
58e7b33a
MJ
2945}
2946
2947static inline u32 nfsd4_exchange_id_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2948{
2949 return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
a8bb84bc
KM
2950 1 + 1 + /* eir_flags, spr_how */\
2951 4 + /* spo_must_enforce & _allow with bitmap */\
58e7b33a
MJ
2952 2 + /*eir_server_owner.so_minor_id */\
2953 /* eir_server_owner.so_major_id<> */\
2954 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
2955 /* eir_server_scope<> */\
2956 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
2957 1 + /* eir_server_impl_id array length */\
2958 0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
2959}
2960
2961static inline u32 nfsd4_bind_conn_to_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2962{
2963 return (op_encode_hdr_size + \
2964 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
2965 2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
2966}
2967
2968static inline u32 nfsd4_create_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2969{
2970 return (op_encode_hdr_size + \
2971 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
2972 2 + /* csr_sequence, csr_flags */\
2973 op_encode_channel_attrs_maxsz + \
2974 op_encode_channel_attrs_maxsz) * sizeof(__be32);
2975}
2976
29ae7f9d
AS
2977static inline u32 nfsd4_copy_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2978{
2979 return (op_encode_hdr_size +
2980 1 /* wr_callback */ +
2981 op_encode_stateid_maxsz /* wr_callback */ +
2982 2 /* wr_count */ +
2983 1 /* wr_committed */ +
2984 op_encode_verifier_maxsz +
2985 1 /* cr_consecutive */ +
2986 1 /* cr_synchronous */) * sizeof(__be32);
2987}
2988
6308bc98
OK
2989static inline u32 nfsd4_offload_status_rsize(struct svc_rqst *rqstp,
2990 struct nfsd4_op *op)
2991{
2992 return (op_encode_hdr_size +
2993 2 /* osr_count */ +
2994 1 /* osr_complete<1> optional 0 for now */) * sizeof(__be32);
2995}
2996
51911868
OK
2997static inline u32 nfsd4_copy_notify_rsize(struct svc_rqst *rqstp,
2998 struct nfsd4_op *op)
2999{
3000 return (op_encode_hdr_size +
3001 3 /* cnr_lease_time */ +
3002 1 /* We support one cnr_source_server */ +
3003 1 /* cnr_stateid seq */ +
3004 op_encode_stateid_maxsz /* cnr_stateid */ +
3005 1 /* num cnr_source_server*/ +
3006 1 /* nl4_type */ +
3007 1 /* nl4 size */ +
3008 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) /*nl4_loc + nl4_loc_sz */)
3009 * sizeof(__be32);
3010}
3011
9cf514cc 3012#ifdef CONFIG_NFSD_PNFS
2282cd2c
KM
3013static inline u32 nfsd4_getdeviceinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
3014{
3015 u32 maxcount = 0, rlen = 0;
3016
3017 maxcount = svc_max_payload(rqstp);
3018 rlen = min(op->u.getdeviceinfo.gd_maxcount, maxcount);
3019
3020 return (op_encode_hdr_size +
3021 1 /* gd_layout_type*/ +
3022 XDR_QUADLEN(rlen) +
3023 2 /* gd_notify_types */) * sizeof(__be32);
3024}
3025
9cf514cc
CH
3026/*
3027 * At this stage we don't really know what layout driver will handle the request,
3028 * so we need to define an arbitrary upper bound here.
3029 */
3030#define MAX_LAYOUT_SIZE 128
3031static inline u32 nfsd4_layoutget_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
3032{
3033 return (op_encode_hdr_size +
3034 1 /* logr_return_on_close */ +
3035 op_encode_stateid_maxsz +
3036 1 /* nr of layouts */ +
3037 MAX_LAYOUT_SIZE) * sizeof(__be32);
3038}
3039
3040static inline u32 nfsd4_layoutcommit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
3041{
3042 return (op_encode_hdr_size +
3043 1 /* locr_newsize */ +
3044 2 /* ns_size */) * sizeof(__be32);
3045}
3046
3047static inline u32 nfsd4_layoutreturn_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
3048{
3049 return (op_encode_hdr_size +
3050 1 /* lrs_stateid */ +
3051 op_encode_stateid_maxsz) * sizeof(__be32);
3052}
3053#endif /* CONFIG_NFSD_PNFS */
3054
2282cd2c
KM
3055
3056static inline u32 nfsd4_seek_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
3057{
3058 return (op_encode_hdr_size + 3) * sizeof(__be32);
3059}
3060
23e50fe3
FL
3061static inline u32 nfsd4_getxattr_rsize(struct svc_rqst *rqstp,
3062 struct nfsd4_op *op)
3063{
3064 u32 maxcount, rlen;
3065
3066 maxcount = svc_max_payload(rqstp);
3067 rlen = min_t(u32, XATTR_SIZE_MAX, maxcount);
3068
3069 return (op_encode_hdr_size + 1 + XDR_QUADLEN(rlen)) * sizeof(__be32);
3070}
3071
3072static inline u32 nfsd4_setxattr_rsize(struct svc_rqst *rqstp,
3073 struct nfsd4_op *op)
3074{
3075 return (op_encode_hdr_size + op_encode_change_info_maxsz)
3076 * sizeof(__be32);
3077}
3078static inline u32 nfsd4_listxattrs_rsize(struct svc_rqst *rqstp,
3079 struct nfsd4_op *op)
3080{
3081 u32 maxcount, rlen;
3082
3083 maxcount = svc_max_payload(rqstp);
3084 rlen = min(op->u.listxattrs.lsxa_maxcount, maxcount);
3085
3086 return (op_encode_hdr_size + 4 + XDR_QUADLEN(rlen)) * sizeof(__be32);
3087}
3088
3089static inline u32 nfsd4_removexattr_rsize(struct svc_rqst *rqstp,
3090 struct nfsd4_op *op)
3091{
3092 return (op_encode_hdr_size + op_encode_change_info_maxsz)
3093 * sizeof(__be32);
3094}
3095
3096
bb2a8b0c 3097static const struct nfsd4_operation nfsd4_ops[] = {
b591480b 3098 [OP_ACCESS] = {
eb69853d 3099 .op_func = nfsd4_access,
b001a1b6 3100 .op_name = "OP_ACCESS",
1c122638 3101 .op_rsize_bop = nfsd4_access_rsize,
b591480b
BF
3102 },
3103 [OP_CLOSE] = {
eb69853d 3104 .op_func = nfsd4_close,
58e7b33a 3105 .op_flags = OP_MODIFIES_SOMETHING,
b001a1b6 3106 .op_name = "OP_CLOSE",
1c122638 3107 .op_rsize_bop = nfsd4_status_stateid_rsize,
57832e7b 3108 .op_get_currentstateid = nfsd4_get_closestateid,
b60e9859 3109 .op_set_currentstateid = nfsd4_set_closestateid,
b591480b
BF
3110 },
3111 [OP_COMMIT] = {
eb69853d 3112 .op_func = nfsd4_commit,
58e7b33a 3113 .op_flags = OP_MODIFIES_SOMETHING,
b001a1b6 3114 .op_name = "OP_COMMIT",
1c122638 3115 .op_rsize_bop = nfsd4_commit_rsize,
b591480b
BF
3116 },
3117 [OP_CREATE] = {
eb69853d 3118 .op_func = nfsd4_create,
d1471053 3119 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
b001a1b6 3120 .op_name = "OP_CREATE",
1c122638 3121 .op_rsize_bop = nfsd4_create_rsize,
b591480b
BF
3122 },
3123 [OP_DELEGRETURN] = {
eb69853d 3124 .op_func = nfsd4_delegreturn,
58e7b33a 3125 .op_flags = OP_MODIFIES_SOMETHING,
b001a1b6 3126 .op_name = "OP_DELEGRETURN",
58e7b33a 3127 .op_rsize_bop = nfsd4_only_status_rsize,
57832e7b 3128 .op_get_currentstateid = nfsd4_get_delegreturnstateid,
b591480b
BF
3129 },
3130 [OP_GETATTR] = {
eb69853d 3131 .op_func = nfsd4_getattr,
eeac294e 3132 .op_flags = ALLOWED_ON_ABSENT_FS,
b86cef60 3133 .op_rsize_bop = nfsd4_getattr_rsize,
b001a1b6 3134 .op_name = "OP_GETATTR",
b591480b
BF
3135 },
3136 [OP_GETFH] = {
eb69853d 3137 .op_func = nfsd4_getfh,
b001a1b6 3138 .op_name = "OP_GETFH",
1c122638 3139 .op_rsize_bop = nfsd4_getfh_rsize,
b591480b
BF
3140 },
3141 [OP_LINK] = {
eb69853d 3142 .op_func = nfsd4_link,
c856694e
BF
3143 .op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
3144 | OP_CACHEME,
b001a1b6 3145 .op_name = "OP_LINK",
1c122638 3146 .op_rsize_bop = nfsd4_link_rsize,
b591480b
BF
3147 },
3148 [OP_LOCK] = {
eb69853d 3149 .op_func = nfsd4_lock,
b7571e4c
BF
3150 .op_flags = OP_MODIFIES_SOMETHING |
3151 OP_NONTRIVIAL_ERROR_ENCODE,
b001a1b6 3152 .op_name = "OP_LOCK",
1c122638 3153 .op_rsize_bop = nfsd4_lock_rsize,
b60e9859 3154 .op_set_currentstateid = nfsd4_set_lockstateid,
b591480b
BF
3155 },
3156 [OP_LOCKT] = {
eb69853d 3157 .op_func = nfsd4_lockt,
b7571e4c 3158 .op_flags = OP_NONTRIVIAL_ERROR_ENCODE,
b001a1b6 3159 .op_name = "OP_LOCKT",
1c122638 3160 .op_rsize_bop = nfsd4_lock_rsize,
b591480b
BF
3161 },
3162 [OP_LOCKU] = {
eb69853d 3163 .op_func = nfsd4_locku,
58e7b33a 3164 .op_flags = OP_MODIFIES_SOMETHING,
b001a1b6 3165 .op_name = "OP_LOCKU",
1c122638 3166 .op_rsize_bop = nfsd4_status_stateid_rsize,
57832e7b 3167 .op_get_currentstateid = nfsd4_get_lockustateid,
b591480b
BF
3168 },
3169 [OP_LOOKUP] = {
eb69853d 3170 .op_func = nfsd4_lookup,
d1471053 3171 .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
b001a1b6 3172 .op_name = "OP_LOOKUP",
1c122638 3173 .op_rsize_bop = nfsd4_only_status_rsize,
b591480b
BF
3174 },
3175 [OP_LOOKUPP] = {
eb69853d 3176 .op_func = nfsd4_lookupp,
d1471053 3177 .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
b001a1b6 3178 .op_name = "OP_LOOKUPP",
1c122638 3179 .op_rsize_bop = nfsd4_only_status_rsize,
b591480b
BF
3180 },
3181 [OP_NVERIFY] = {
eb69853d 3182 .op_func = nfsd4_nverify,
b001a1b6 3183 .op_name = "OP_NVERIFY",
1c122638 3184 .op_rsize_bop = nfsd4_only_status_rsize,
b591480b
BF
3185 },
3186 [OP_OPEN] = {
eb69853d 3187 .op_func = nfsd4_open,
58e7b33a 3188 .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
b001a1b6 3189 .op_name = "OP_OPEN",
1c122638 3190 .op_rsize_bop = nfsd4_open_rsize,
b60e9859 3191 .op_set_currentstateid = nfsd4_set_openstateid,
b591480b
BF
3192 },
3193 [OP_OPEN_CONFIRM] = {
eb69853d 3194 .op_func = nfsd4_open_confirm,
58e7b33a 3195 .op_flags = OP_MODIFIES_SOMETHING,
b001a1b6 3196 .op_name = "OP_OPEN_CONFIRM",
1c122638 3197 .op_rsize_bop = nfsd4_status_stateid_rsize,
b591480b
BF
3198 },
3199 [OP_OPEN_DOWNGRADE] = {
eb69853d 3200 .op_func = nfsd4_open_downgrade,
58e7b33a 3201 .op_flags = OP_MODIFIES_SOMETHING,
b001a1b6 3202 .op_name = "OP_OPEN_DOWNGRADE",
1c122638 3203 .op_rsize_bop = nfsd4_status_stateid_rsize,
57832e7b 3204 .op_get_currentstateid = nfsd4_get_opendowngradestateid,
b60e9859 3205 .op_set_currentstateid = nfsd4_set_opendowngradestateid,
b591480b
BF
3206 },
3207 [OP_PUTFH] = {
eb69853d 3208 .op_func = nfsd4_putfh,
68d93184 3209 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
5b648699 3210 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
b001a1b6 3211 .op_name = "OP_PUTFH",
1c122638 3212 .op_rsize_bop = nfsd4_only_status_rsize,
b591480b 3213 },
eeac294e 3214 [OP_PUTPUBFH] = {
eb69853d 3215 .op_func = nfsd4_putrootfh,
68d93184 3216 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
5b648699 3217 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
b001a1b6 3218 .op_name = "OP_PUTPUBFH",
1c122638 3219 .op_rsize_bop = nfsd4_only_status_rsize,
eeac294e 3220 },
b591480b 3221 [OP_PUTROOTFH] = {
eb69853d 3222 .op_func = nfsd4_putrootfh,
68d93184 3223 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
5b648699 3224 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
b001a1b6 3225 .op_name = "OP_PUTROOTFH",
1c122638 3226 .op_rsize_bop = nfsd4_only_status_rsize,
b591480b
BF
3227 },
3228 [OP_READ] = {
eb69853d 3229 .op_func = nfsd4_read,
34b1744c 3230 .op_release = nfsd4_read_release,
b001a1b6 3231 .op_name = "OP_READ",
1c122638 3232 .op_rsize_bop = nfsd4_read_rsize,
57832e7b 3233 .op_get_currentstateid = nfsd4_get_readstateid,
b591480b
BF
3234 },
3235 [OP_READDIR] = {
eb69853d 3236 .op_func = nfsd4_readdir,
b001a1b6 3237 .op_name = "OP_READDIR",
1c122638 3238 .op_rsize_bop = nfsd4_readdir_rsize,
b591480b
BF
3239 },
3240 [OP_READLINK] = {
eb69853d 3241 .op_func = nfsd4_readlink,
b001a1b6 3242 .op_name = "OP_READLINK",
1c122638 3243 .op_rsize_bop = nfsd4_readlink_rsize,
b591480b
BF
3244 },
3245 [OP_REMOVE] = {
eb69853d 3246 .op_func = nfsd4_remove,
c856694e 3247 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
b001a1b6 3248 .op_name = "OP_REMOVE",
1c122638 3249 .op_rsize_bop = nfsd4_remove_rsize,
b591480b
BF
3250 },
3251 [OP_RENAME] = {
eb69853d 3252 .op_func = nfsd4_rename,
c856694e 3253 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
58e7b33a 3254 .op_name = "OP_RENAME",
1c122638 3255 .op_rsize_bop = nfsd4_rename_rsize,
b591480b
BF
3256 },
3257 [OP_RENEW] = {
eb69853d 3258 .op_func = nfsd4_renew,
58e7b33a
MJ
3259 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3260 | OP_MODIFIES_SOMETHING,
b001a1b6 3261 .op_name = "OP_RENEW",
1c122638 3262 .op_rsize_bop = nfsd4_only_status_rsize,
58e7b33a 3263
b591480b
BF
3264 },
3265 [OP_RESTOREFH] = {
eb69853d 3266 .op_func = nfsd4_restorefh,
68d93184 3267 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
58e7b33a 3268 | OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
b001a1b6 3269 .op_name = "OP_RESTOREFH",
1c122638 3270 .op_rsize_bop = nfsd4_only_status_rsize,
b591480b
BF
3271 },
3272 [OP_SAVEFH] = {
eb69853d 3273 .op_func = nfsd4_savefh,
58e7b33a 3274 .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
b001a1b6 3275 .op_name = "OP_SAVEFH",
1c122638 3276 .op_rsize_bop = nfsd4_only_status_rsize,
b591480b 3277 },
dcb488a3 3278 [OP_SECINFO] = {
eb69853d 3279 .op_func = nfsd4_secinfo,
34b1744c 3280 .op_release = nfsd4_secinfo_release,
68d93184 3281 .op_flags = OP_HANDLES_WRONGSEC,
b001a1b6 3282 .op_name = "OP_SECINFO",
1c122638 3283 .op_rsize_bop = nfsd4_secinfo_rsize,
dcb488a3 3284 },
b591480b 3285 [OP_SETATTR] = {
eb69853d 3286 .op_func = nfsd4_setattr,
b001a1b6 3287 .op_name = "OP_SETATTR",
b7571e4c
BF
3288 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME
3289 | OP_NONTRIVIAL_ERROR_ENCODE,
1c122638 3290 .op_rsize_bop = nfsd4_setattr_rsize,
57832e7b 3291 .op_get_currentstateid = nfsd4_get_setattrstateid,
b591480b
BF
3292 },
3293 [OP_SETCLIENTID] = {
eb69853d 3294 .op_func = nfsd4_setclientid,
58e7b33a 3295 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
b7571e4c
BF
3296 | OP_MODIFIES_SOMETHING | OP_CACHEME
3297 | OP_NONTRIVIAL_ERROR_ENCODE,
b001a1b6 3298 .op_name = "OP_SETCLIENTID",
1c122638 3299 .op_rsize_bop = nfsd4_setclientid_rsize,
b591480b
BF
3300 },
3301 [OP_SETCLIENTID_CONFIRM] = {
eb69853d 3302 .op_func = nfsd4_setclientid_confirm,
58e7b33a 3303 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
c856694e 3304 | OP_MODIFIES_SOMETHING | OP_CACHEME,
b001a1b6 3305 .op_name = "OP_SETCLIENTID_CONFIRM",
1c122638 3306 .op_rsize_bop = nfsd4_only_status_rsize,
b591480b
BF
3307 },
3308 [OP_VERIFY] = {
eb69853d 3309 .op_func = nfsd4_verify,
b001a1b6 3310 .op_name = "OP_VERIFY",
1c122638 3311 .op_rsize_bop = nfsd4_only_status_rsize,
b591480b
BF
3312 },
3313 [OP_WRITE] = {
eb69853d 3314 .op_func = nfsd4_write,
c856694e 3315 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
b001a1b6 3316 .op_name = "OP_WRITE",
1c122638 3317 .op_rsize_bop = nfsd4_write_rsize,
57832e7b 3318 .op_get_currentstateid = nfsd4_get_writestateid,
b591480b
BF
3319 },
3320 [OP_RELEASE_LOCKOWNER] = {
eb69853d 3321 .op_func = nfsd4_release_lockowner,
58e7b33a
MJ
3322 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3323 | OP_MODIFIES_SOMETHING,
b001a1b6 3324 .op_name = "OP_RELEASE_LOCKOWNER",
1c122638 3325 .op_rsize_bop = nfsd4_only_status_rsize,
b591480b 3326 },
069b6ad4
AA
3327
3328 /* NFSv4.1 operations */
3329 [OP_EXCHANGE_ID] = {
eb69853d 3330 .op_func = nfsd4_exchange_id,
58e7b33a
MJ
3331 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3332 | OP_MODIFIES_SOMETHING,
069b6ad4 3333 .op_name = "OP_EXCHANGE_ID",
1c122638 3334 .op_rsize_bop = nfsd4_exchange_id_rsize,
069b6ad4 3335 },
cb73a9f4 3336 [OP_BACKCHANNEL_CTL] = {
eb69853d 3337 .op_func = nfsd4_backchannel_ctl,
cb73a9f4
BF
3338 .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
3339 .op_name = "OP_BACKCHANNEL_CTL",
1c122638 3340 .op_rsize_bop = nfsd4_only_status_rsize,
cb73a9f4 3341 },
1d1bc8f2 3342 [OP_BIND_CONN_TO_SESSION] = {
eb69853d 3343 .op_func = nfsd4_bind_conn_to_session,
58e7b33a
MJ
3344 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3345 | OP_MODIFIES_SOMETHING,
1d1bc8f2 3346 .op_name = "OP_BIND_CONN_TO_SESSION",
1c122638 3347 .op_rsize_bop = nfsd4_bind_conn_to_session_rsize,
1d1bc8f2 3348 },
069b6ad4 3349 [OP_CREATE_SESSION] = {
eb69853d 3350 .op_func = nfsd4_create_session,
58e7b33a
MJ
3351 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3352 | OP_MODIFIES_SOMETHING,
069b6ad4 3353 .op_name = "OP_CREATE_SESSION",
1c122638 3354 .op_rsize_bop = nfsd4_create_session_rsize,
069b6ad4
AA
3355 },
3356 [OP_DESTROY_SESSION] = {
eb69853d 3357 .op_func = nfsd4_destroy_session,
58e7b33a
MJ
3358 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3359 | OP_MODIFIES_SOMETHING,
069b6ad4 3360 .op_name = "OP_DESTROY_SESSION",
1c122638 3361 .op_rsize_bop = nfsd4_only_status_rsize,
069b6ad4
AA
3362 },
3363 [OP_SEQUENCE] = {
eb69853d 3364 .op_func = nfsd4_sequence,
f9bb94c4 3365 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
069b6ad4 3366 .op_name = "OP_SEQUENCE",
1c122638 3367 .op_rsize_bop = nfsd4_sequence_rsize,
069b6ad4 3368 },
094b5d74 3369 [OP_DESTROY_CLIENTID] = {
eb69853d 3370 .op_func = nfsd4_destroy_clientid,
58e7b33a
MJ
3371 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3372 | OP_MODIFIES_SOMETHING,
094b5d74 3373 .op_name = "OP_DESTROY_CLIENTID",
1c122638 3374 .op_rsize_bop = nfsd4_only_status_rsize,
094b5d74 3375 },
4dc6ec00 3376 [OP_RECLAIM_COMPLETE] = {
eb69853d 3377 .op_func = nfsd4_reclaim_complete,
58e7b33a 3378 .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
4dc6ec00 3379 .op_name = "OP_RECLAIM_COMPLETE",
1c122638 3380 .op_rsize_bop = nfsd4_only_status_rsize,
4dc6ec00 3381 },
04f4ad16 3382 [OP_SECINFO_NO_NAME] = {
eb69853d 3383 .op_func = nfsd4_secinfo_no_name,
ec572b9e 3384 .op_release = nfsd4_secinfo_no_name_release,
68d93184 3385 .op_flags = OP_HANDLES_WRONGSEC,
04f4ad16 3386 .op_name = "OP_SECINFO_NO_NAME",
1c122638 3387 .op_rsize_bop = nfsd4_secinfo_rsize,
04f4ad16 3388 },
17456804 3389 [OP_TEST_STATEID] = {
eb69853d 3390 .op_func = nfsd4_test_stateid,
17456804
BS
3391 .op_flags = ALLOWED_WITHOUT_FH,
3392 .op_name = "OP_TEST_STATEID",
1c122638 3393 .op_rsize_bop = nfsd4_test_stateid_rsize,
17456804 3394 },
e1ca12df 3395 [OP_FREE_STATEID] = {
eb69853d 3396 .op_func = nfsd4_free_stateid,
58e7b33a 3397 .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
e1ca12df 3398 .op_name = "OP_FREE_STATEID",
57832e7b 3399 .op_get_currentstateid = nfsd4_get_freestateid,
1c122638 3400 .op_rsize_bop = nfsd4_only_status_rsize,
e1ca12df 3401 },
9cf514cc
CH
3402#ifdef CONFIG_NFSD_PNFS
3403 [OP_GETDEVICEINFO] = {
eb69853d 3404 .op_func = nfsd4_getdeviceinfo,
34b1744c 3405 .op_release = nfsd4_getdeviceinfo_release,
9cf514cc
CH
3406 .op_flags = ALLOWED_WITHOUT_FH,
3407 .op_name = "OP_GETDEVICEINFO",
1c122638 3408 .op_rsize_bop = nfsd4_getdeviceinfo_rsize,
9cf514cc
CH
3409 },
3410 [OP_LAYOUTGET] = {
eb69853d 3411 .op_func = nfsd4_layoutget,
34b1744c 3412 .op_release = nfsd4_layoutget_release,
9cf514cc
CH
3413 .op_flags = OP_MODIFIES_SOMETHING,
3414 .op_name = "OP_LAYOUTGET",
1c122638 3415 .op_rsize_bop = nfsd4_layoutget_rsize,
9cf514cc
CH
3416 },
3417 [OP_LAYOUTCOMMIT] = {
eb69853d 3418 .op_func = nfsd4_layoutcommit,
9cf514cc
CH
3419 .op_flags = OP_MODIFIES_SOMETHING,
3420 .op_name = "OP_LAYOUTCOMMIT",
1c122638 3421 .op_rsize_bop = nfsd4_layoutcommit_rsize,
9cf514cc
CH
3422 },
3423 [OP_LAYOUTRETURN] = {
eb69853d 3424 .op_func = nfsd4_layoutreturn,
9cf514cc
CH
3425 .op_flags = OP_MODIFIES_SOMETHING,
3426 .op_name = "OP_LAYOUTRETURN",
1c122638 3427 .op_rsize_bop = nfsd4_layoutreturn_rsize,
9cf514cc
CH
3428 },
3429#endif /* CONFIG_NFSD_PNFS */
24bab491
AS
3430
3431 /* NFSv4.2 operations */
95d871f0 3432 [OP_ALLOCATE] = {
eb69853d 3433 .op_func = nfsd4_allocate,
03b31f48 3434 .op_flags = OP_MODIFIES_SOMETHING,
95d871f0 3435 .op_name = "OP_ALLOCATE",
1c122638 3436 .op_rsize_bop = nfsd4_only_status_rsize,
95d871f0 3437 },
b0cb9085 3438 [OP_DEALLOCATE] = {
eb69853d 3439 .op_func = nfsd4_deallocate,
03b31f48 3440 .op_flags = OP_MODIFIES_SOMETHING,
b0cb9085 3441 .op_name = "OP_DEALLOCATE",
1c122638 3442 .op_rsize_bop = nfsd4_only_status_rsize,
b0cb9085 3443 },
ffa0160a 3444 [OP_CLONE] = {
eb69853d 3445 .op_func = nfsd4_clone,
03b31f48 3446 .op_flags = OP_MODIFIES_SOMETHING,
ffa0160a 3447 .op_name = "OP_CLONE",
1c122638 3448 .op_rsize_bop = nfsd4_only_status_rsize,
ffa0160a 3449 },
29ae7f9d 3450 [OP_COPY] = {
eb69853d 3451 .op_func = nfsd4_copy,
03b31f48 3452 .op_flags = OP_MODIFIES_SOMETHING,
29ae7f9d 3453 .op_name = "OP_COPY",
1c122638 3454 .op_rsize_bop = nfsd4_copy_rsize,
29ae7f9d 3455 },
528b8493
AS
3456 [OP_READ_PLUS] = {
3457 .op_func = nfsd4_read,
3458 .op_release = nfsd4_read_release,
3459 .op_name = "OP_READ_PLUS",
3460 .op_rsize_bop = nfsd4_read_plus_rsize,
3461 .op_get_currentstateid = nfsd4_get_readstateid,
3462 },
24bab491 3463 [OP_SEEK] = {
eb69853d 3464 .op_func = nfsd4_seek,
24bab491 3465 .op_name = "OP_SEEK",
1c122638 3466 .op_rsize_bop = nfsd4_seek_rsize,
24bab491 3467 },
6308bc98
OK
3468 [OP_OFFLOAD_STATUS] = {
3469 .op_func = nfsd4_offload_status,
3470 .op_name = "OP_OFFLOAD_STATUS",
3471 .op_rsize_bop = nfsd4_offload_status_rsize,
3472 },
885e2bf3
OK
3473 [OP_OFFLOAD_CANCEL] = {
3474 .op_func = nfsd4_offload_cancel,
3475 .op_flags = OP_MODIFIES_SOMETHING,
3476 .op_name = "OP_OFFLOAD_CANCEL",
3477 .op_rsize_bop = nfsd4_only_status_rsize,
3478 },
51911868
OK
3479 [OP_COPY_NOTIFY] = {
3480 .op_func = nfsd4_copy_notify,
3481 .op_flags = OP_MODIFIES_SOMETHING,
3482 .op_name = "OP_COPY_NOTIFY",
3483 .op_rsize_bop = nfsd4_copy_notify_rsize,
3484 },
23e50fe3
FL
3485 [OP_GETXATTR] = {
3486 .op_func = nfsd4_getxattr,
3487 .op_name = "OP_GETXATTR",
3488 .op_rsize_bop = nfsd4_getxattr_rsize,
3489 },
3490 [OP_SETXATTR] = {
3491 .op_func = nfsd4_setxattr,
3492 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3493 .op_name = "OP_SETXATTR",
3494 .op_rsize_bop = nfsd4_setxattr_rsize,
3495 },
3496 [OP_LISTXATTRS] = {
3497 .op_func = nfsd4_listxattrs,
3498 .op_name = "OP_LISTXATTRS",
3499 .op_rsize_bop = nfsd4_listxattrs_rsize,
3500 },
3501 [OP_REMOVEXATTR] = {
3502 .op_func = nfsd4_removexattr,
3503 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3504 .op_name = "OP_REMOVEXATTR",
3505 .op_rsize_bop = nfsd4_removexattr_rsize,
3506 },
b591480b
BF
3507};
3508
ed941643
AE
3509/**
3510 * nfsd4_spo_must_allow - Determine if the compound op contains an
3511 * operation that is allowed to be sent with machine credentials
3512 *
3513 * @rqstp: a pointer to the struct svc_rqst
3514 *
3515 * Checks to see if the compound contains a spo_must_allow op
3516 * and confirms that it was sent with the proper machine creds.
3517 */
3518
3519bool nfsd4_spo_must_allow(struct svc_rqst *rqstp)
3520{
3521 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3522 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
e34c0ce9 3523 struct nfsd4_op *this;
ed941643
AE
3524 struct nfsd4_compound_state *cstate = &resp->cstate;
3525 struct nfs4_op_map *allow = &cstate->clp->cl_spo_must_allow;
3526 u32 opiter;
3527
3528 if (!cstate->minorversion)
3529 return false;
3530
44b49aa6 3531 if (cstate->spo_must_allowed)
ed941643
AE
3532 return true;
3533
3534 opiter = resp->opcnt;
3535 while (opiter < argp->opcnt) {
3536 this = &argp->ops[opiter++];
3537 if (test_bit(this->opnum, allow->u.longs) &&
3538 cstate->clp->cl_mach_cred &&
3539 nfsd4_mach_creds_match(cstate->clp, rqstp)) {
3540 cstate->spo_must_allowed = true;
3541 return true;
3542 }
3543 }
3544 cstate->spo_must_allowed = false;
3545 return false;
3546}
3547
4f0cefbf
BF
3548int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
3549{
05b7278d 3550 if (op->opnum == OP_ILLEGAL || op->status == nfserr_notsupp)
4f0cefbf 3551 return op_encode_hdr_size * sizeof(__be32);
2282cd2c
KM
3552
3553 BUG_ON(OPDESC(op)->op_rsize_bop == NULL);
3554 return OPDESC(op)->op_rsize_bop(rqstp, op);
4f0cefbf
BF
3555}
3556
07d1f802
BF
3557void warn_on_nonidempotent_op(struct nfsd4_op *op)
3558{
3559 if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
3a237b4a 3560 pr_err("unable to encode reply to nonidempotent op %u (%s)\n",
07d1f802
BF
3561 op->opnum, nfsd4_op_name(op->opnum));
3562 WARN_ON_ONCE(1);
3563 }
3564}
3565
f1c7f79b 3566static const char *nfsd4_op_name(unsigned opnum)
b001a1b6
BH
3567{
3568 if (opnum < ARRAY_SIZE(nfsd4_ops))
3569 return nfsd4_ops[opnum].op_name;
3570 return "unknown_operation";
3571}
3572
860bda29 3573static const struct svc_procedure nfsd_procedures4[2] = {
0a93a47f 3574 [NFSPROC4_NULL] = {
f7235b6b 3575 .pc_func = nfsd4_proc_null,
788f7183
CL
3576 .pc_decode = nfssvc_decode_voidarg,
3577 .pc_encode = nfssvc_encode_voidres,
3578 .pc_argsize = sizeof(struct nfsd_voidargs),
3579 .pc_ressize = sizeof(struct nfsd_voidres),
0a93a47f
YZ
3580 .pc_cachetype = RC_NOCACHE,
3581 .pc_xdrressize = 1,
2289e87b 3582 .pc_name = "NULL",
0a93a47f
YZ
3583 },
3584 [NFSPROC4_COMPOUND] = {
f7235b6b 3585 .pc_func = nfsd4_proc_compound,
026fec7e 3586 .pc_decode = nfs4svc_decode_compoundargs,
63f8de37 3587 .pc_encode = nfs4svc_encode_compoundres,
0a93a47f
YZ
3588 .pc_argsize = sizeof(struct nfsd4_compoundargs),
3589 .pc_ressize = sizeof(struct nfsd4_compoundres),
3e98abff 3590 .pc_release = nfsd4_release_compoundargs,
0a93a47f
YZ
3591 .pc_cachetype = RC_NOCACHE,
3592 .pc_xdrressize = NFSD_BUFSIZE/4,
2289e87b 3593 .pc_name = "COMPOUND",
0a93a47f 3594 },
1da177e4
LT
3595};
3596
7fd38af9 3597static unsigned int nfsd_count3[ARRAY_SIZE(nfsd_procedures4)];
e9679189 3598const struct svc_version nfsd_version4 = {
5283b03e
JL
3599 .vs_vers = 4,
3600 .vs_nproc = 2,
3601 .vs_proc = nfsd_procedures4,
7fd38af9 3602 .vs_count = nfsd_count3,
5283b03e
JL
3603 .vs_dispatch = nfsd_dispatch,
3604 .vs_xdrsize = NFS4_SVC_XDRSIZE,
3605 .vs_rpcb_optnl = true,
3606 .vs_need_cong_ctrl = true,
1da177e4 3607};