nfsd: implement pNFS operations
[deliverable/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 */
7e06b7f9 35#include <linux/file.h>
b0cb9085 36#include <linux/falloc.h>
5a0e3ad6 37#include <linux/slab.h>
1da177e4 38
58e7b33a 39#include "idmap.h"
9a74af21
BH
40#include "cache.h"
41#include "xdr4.h"
0a3adade 42#include "vfs.h"
8b70484c 43#include "current_stateid.h"
3320fef1 44#include "netns.h"
4ac7249e 45#include "acl.h"
9cf514cc 46#include "pnfs.h"
1da177e4 47
18032ca0
DQ
48#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
49#include <linux/security.h>
50
51static inline void
52nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
53{
54 struct inode *inode = resfh->fh_dentry->d_inode;
55 int status;
56
57 mutex_lock(&inode->i_mutex);
58 status = security_inode_setsecctx(resfh->fh_dentry,
59 label->data, label->len);
60 mutex_unlock(&inode->i_mutex);
61
62 if (status)
63 /*
64 * XXX: We should really fail the whole open, but we may
65 * already have created a new file, so it may be too
66 * late. For now this seems the least of evils:
67 */
68 bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
69
70 return;
71}
72#else
73static inline void
74nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
75{ }
76#endif
77
1da177e4
LT
78#define NFSDDBG_FACILITY NFSDDBG_PROC
79
3c8e0316
YZ
80static u32 nfsd_attrmask[] = {
81 NFSD_WRITEABLE_ATTRS_WORD0,
82 NFSD_WRITEABLE_ATTRS_WORD1,
83 NFSD_WRITEABLE_ATTRS_WORD2
84};
85
86static u32 nfsd41_ex_attrmask[] = {
87 NFSD_SUPPATTR_EXCLCREAT_WORD0,
88 NFSD_SUPPATTR_EXCLCREAT_WORD1,
89 NFSD_SUPPATTR_EXCLCREAT_WORD2
90};
91
92static __be32
93check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
94 u32 *bmval, u32 *writable)
95{
96 struct dentry *dentry = cstate->current_fh.fh_dentry;
3c8e0316
YZ
97
98 /*
99 * Check about attributes are supported by the NFSv4 server or not.
100 * According to spec, unsupported attributes return ERR_ATTRNOTSUPP.
101 */
102 if ((bmval[0] & ~nfsd_suppattrs0(cstate->minorversion)) ||
103 (bmval[1] & ~nfsd_suppattrs1(cstate->minorversion)) ||
104 (bmval[2] & ~nfsd_suppattrs2(cstate->minorversion)))
105 return nfserr_attrnotsupp;
106
107 /*
a06b1261 108 * Check FATTR4_WORD0_ACL can be supported
3c8e0316
YZ
109 * in current environment or not.
110 */
111 if (bmval[0] & FATTR4_WORD0_ACL) {
112 if (!IS_POSIXACL(dentry->d_inode))
113 return nfserr_attrnotsupp;
114 }
3c8e0316
YZ
115
116 /*
117 * According to spec, read-only attributes return ERR_INVAL.
118 */
119 if (writable) {
120 if ((bmval[0] & ~writable[0]) || (bmval[1] & ~writable[1]) ||
121 (bmval[2] & ~writable[2]))
122 return nfserr_inval;
123 }
124
125 return nfs_ok;
126}
127
128static __be32
129nfsd4_check_open_attributes(struct svc_rqst *rqstp,
130 struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
131{
132 __be32 status = nfs_ok;
133
134 if (open->op_create == NFS4_OPEN_CREATE) {
135 if (open->op_createmode == NFS4_CREATE_UNCHECKED
136 || open->op_createmode == NFS4_CREATE_GUARDED)
137 status = check_attr_support(rqstp, cstate,
138 open->op_bmval, nfsd_attrmask);
139 else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1)
140 status = check_attr_support(rqstp, cstate,
141 open->op_bmval, nfsd41_ex_attrmask);
142 }
143
144 return status;
145}
146
9208faf2
YZ
147static int
148is_create_with_attrs(struct nfsd4_open *open)
149{
150 return open->op_create == NFS4_OPEN_CREATE
151 && (open->op_createmode == NFS4_CREATE_UNCHECKED
152 || open->op_createmode == NFS4_CREATE_GUARDED
153 || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1);
154}
155
156/*
157 * if error occurs when setting the acl, just clear the acl bit
158 * in the returned attr bitmap.
159 */
160static void
161do_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
162 struct nfs4_acl *acl, u32 *bmval)
163{
164 __be32 status;
165
166 status = nfsd4_set_nfs4_acl(rqstp, fhp, acl);
167 if (status)
168 /*
169 * We should probably fail the whole open at this point,
170 * but we've already created the file, so it's too late;
171 * So this seems the least of evils:
172 */
173 bmval[0] &= ~FATTR4_WORD0_ACL;
174}
175
1da177e4
LT
176static inline void
177fh_dup2(struct svc_fh *dst, struct svc_fh *src)
178{
179 fh_put(dst);
180 dget(src->fh_dentry);
181 if (src->fh_export)
bf18f163 182 exp_get(src->fh_export);
1da177e4
LT
183 *dst = *src;
184}
185
b37ad28b 186static __be32
dc730e17 187do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
1da177e4 188{
b37ad28b 189 __be32 status;
1da177e4
LT
190
191 if (open->op_truncate &&
192 !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
193 return nfserr_inval;
194
a043226b
BF
195 accmode |= NFSD_MAY_READ_IF_EXEC;
196
1da177e4 197 if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
8837abca 198 accmode |= NFSD_MAY_READ;
9801d8a3 199 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
8837abca 200 accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC);
57ecb34f 201 if (open->op_share_deny & NFS4_SHARE_DENY_READ)
8837abca 202 accmode |= NFSD_MAY_WRITE;
1da177e4
LT
203
204 status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
205
206 return status;
207}
208
aadab6c6
BF
209static __be32 nfsd_check_obj_isreg(struct svc_fh *fh)
210{
211 umode_t mode = fh->fh_dentry->d_inode->i_mode;
212
213 if (S_ISREG(mode))
214 return nfs_ok;
215 if (S_ISDIR(mode))
216 return nfserr_isdir;
217 /*
218 * Using err_symlink as our catch-all case may look odd; but
219 * there's no other obvious error for this case in 4.0, and we
220 * happen to know that it will cause the linux v4 client to do
221 * the right thing on attempts to open something other than a
222 * regular file.
223 */
224 return nfserr_symlink;
225}
226
bbc9c36c
BF
227static void nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh *resfh)
228{
229 if (nfsd4_has_session(cstate))
230 return;
231 fh_copy_shallow(&open->op_openowner->oo_owner.so_replay.rp_openfh,
232 &resfh->fh_handle);
233}
234
b37ad28b 235static __be32
c0e6bee4 236do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh **resfh)
1da177e4 237{
bbc9c36c 238 struct svc_fh *current_fh = &cstate->current_fh;
7007c90f 239 int accmode;
b37ad28b 240 __be32 status;
1da177e4 241
c0e6bee4
BF
242 *resfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
243 if (!*resfh)
59deeb9e 244 return nfserr_jukebox;
c0e6bee4 245 fh_init(*resfh, NFS4_FHSIZE);
1da177e4
LT
246 open->op_truncate = 0;
247
248 if (open->op_create) {
79fb54ab
BH
249 /* FIXME: check session persistence and pnfs flags.
250 * The nfsv4.1 spec requires the following semantics:
251 *
252 * Persistent | pNFS | Server REQUIRED | Client Allowed
253 * Reply Cache | server | |
254 * -------------+--------+-----------------+--------------------
255 * no | no | EXCLUSIVE4_1 | EXCLUSIVE4_1
256 * | | | (SHOULD)
257 * | | and EXCLUSIVE4 | or EXCLUSIVE4
258 * | | | (SHOULD NOT)
259 * no | yes | EXCLUSIVE4_1 | EXCLUSIVE4_1
260 * yes | no | GUARDED4 | GUARDED4
261 * yes | yes | GUARDED4 | GUARDED4
262 */
263
1da177e4
LT
264 /*
265 * Note: create modes (UNCHECKED,GUARDED...) are the same
ac6721a1 266 * in NFSv4 as in v3 except EXCLUSIVE4_1.
1da177e4 267 */
ac6721a1 268 status = do_nfsd_create(rqstp, current_fh, open->op_fname.data,
1da177e4 269 open->op_fname.len, &open->op_iattr,
c0e6bee4 270 *resfh, open->op_createmode,
749997e5 271 (u32 *)open->op_verf.data,
856121b2 272 &open->op_truncate, &open->op_created);
749997e5 273
18032ca0 274 if (!status && open->op_label.len)
c0e6bee4 275 nfsd4_security_inode_setsecctx(*resfh, &open->op_label, open->op_bmval);
18032ca0 276
99f88726
BF
277 /*
278 * Following rfc 3530 14.2.16, use the returned bitmask
279 * to indicate which attributes we used to store the
280 * verifier:
749997e5
JL
281 */
282 if (open->op_createmode == NFS4_CREATE_EXCLUSIVE && status == 0)
99f88726 283 open->op_bmval[1] = (FATTR4_WORD1_TIME_ACCESS |
9dc4e6c4 284 FATTR4_WORD1_TIME_MODIFY);
4335723e
BF
285 } else
286 /*
287 * Note this may exit with the parent still locked.
288 * We will hold the lock until nfsd4_open's final
289 * lookup, to prevent renames or unlinks until we've had
290 * a chance to an acquire a delegation if appropriate.
291 */
1da177e4 292 status = nfsd_lookup(rqstp, current_fh,
c0e6bee4 293 open->op_fname.data, open->op_fname.len, *resfh);
9dc4e6c4
BF
294 if (status)
295 goto out;
c0e6bee4 296 status = nfsd_check_obj_isreg(*resfh);
af85852d
BF
297 if (status)
298 goto out;
1da177e4 299
9208faf2 300 if (is_create_with_attrs(open) && open->op_acl != NULL)
c0e6bee4 301 do_set_nfs4_acl(rqstp, *resfh, open->op_acl, open->op_bmval);
9208faf2 302
c0e6bee4 303 nfsd4_set_open_owner_reply_cache(cstate, open, *resfh);
7007c90f 304 accmode = NFSD_MAY_NOP;
89f6c336
BF
305 if (open->op_created ||
306 open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
7007c90f 307 accmode |= NFSD_MAY_OWNER_OVERRIDE;
c0e6bee4 308 status = do_open_permission(rqstp, *resfh, open, accmode);
41fd1e42 309 set_change_info(&open->op_cinfo, current_fh);
af85852d 310out:
1da177e4
LT
311 return status;
312}
313
b37ad28b 314static __be32
bbc9c36c 315do_open_fhandle(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
1da177e4 316{
bbc9c36c 317 struct svc_fh *current_fh = &cstate->current_fh;
b37ad28b 318 __be32 status;
9f415eb2 319 int accmode = 0;
1da177e4 320
1da177e4
LT
321 /* We don't know the target directory, and therefore can not
322 * set the change info
323 */
324
325 memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
326
bbc9c36c 327 nfsd4_set_open_owner_reply_cache(cstate, open, current_fh);
1da177e4
LT
328
329 open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
330 (open->op_iattr.ia_size == 0);
9f415eb2
BF
331 /*
332 * In the delegation case, the client is telling us about an
333 * open that it *already* performed locally, some time ago. We
334 * should let it succeed now if possible.
335 *
336 * In the case of a CLAIM_FH open, on the other hand, the client
337 * may be counting on us to enforce permissions (the Linux 4.1
338 * client uses this for normal opens, for example).
339 */
340 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH)
341 accmode = NFSD_MAY_OWNER_OVERRIDE;
1da177e4 342
9f415eb2 343 status = do_open_permission(rqstp, current_fh, open, accmode);
1da177e4
LT
344
345 return status;
346}
347
60adfc50
AA
348static void
349copy_clientid(clientid_t *clid, struct nfsd4_session *session)
350{
351 struct nfsd4_sessionid *sid =
352 (struct nfsd4_sessionid *)session->se_sessionid.data;
353
354 clid->cl_boot = sid->clientid.cl_boot;
355 clid->cl_id = sid->clientid.cl_id;
356}
1da177e4 357
7191155b 358static __be32
ca364317 359nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 360 struct nfsd4_open *open)
1da177e4 361{
b37ad28b 362 __be32 status;
c0e6bee4 363 struct svc_fh *resfh = NULL;
6668958f 364 struct nfsd4_compoundres *resp;
3320fef1
SK
365 struct net *net = SVC_NET(rqstp);
366 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6668958f 367
fe0750e5 368 dprintk("NFSD: nfsd4_open filename %.*s op_openowner %p\n",
1da177e4 369 (int)open->op_fname.len, open->op_fname.data,
fe0750e5 370 open->op_openowner);
1da177e4 371
1da177e4
LT
372 /* This check required by spec. */
373 if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
374 return nfserr_inval;
375
856121b2 376 open->op_created = 0;
ab1350b2
MJ
377 /*
378 * RFC5661 18.51.3
379 * Before RECLAIM_COMPLETE done, server should deny new lock
380 */
381 if (nfsd4_has_session(cstate) &&
a52d726b
JL
382 !test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
383 &cstate->session->se_client->cl_flags) &&
ab1350b2
MJ
384 open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
385 return nfserr_grace;
386
60adfc50
AA
387 if (nfsd4_has_session(cstate))
388 copy_clientid(&open->op_clientid, cstate->session);
389
1da177e4 390 /* check seqid for replay. set nfs4_owner */
6668958f 391 resp = rqstp->rq_resp;
3320fef1 392 status = nfsd4_process_open1(&resp->cstate, open, nn);
a90b061c 393 if (status == nfserr_replay_me) {
fe0750e5 394 struct nfs4_replay *rp = &open->op_openowner->oo_owner.so_replay;
ca364317 395 fh_put(&cstate->current_fh);
a4773c08
BF
396 fh_copy_shallow(&cstate->current_fh.fh_handle,
397 &rp->rp_openfh);
8837abca 398 status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
1da177e4
LT
399 if (status)
400 dprintk("nfsd4_open: replay failed"
401 " restoring previous filehandle\n");
402 else
a90b061c 403 status = nfserr_replay_me;
1da177e4
LT
404 }
405 if (status)
406 goto out;
9d313b17
BF
407 if (open->op_xdr_error) {
408 status = open->op_xdr_error;
409 goto out;
410 }
fb553c0f 411
3c8e0316
YZ
412 status = nfsd4_check_open_attributes(rqstp, cstate, open);
413 if (status)
414 goto out;
415
fb553c0f
BF
416 /* Openowner is now set, so sequence id will get bumped. Now we need
417 * these checks before we do any creates: */
cbd0d51a 418 status = nfserr_grace;
3320fef1 419 if (locks_in_grace(net) && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
cbd0d51a
BF
420 goto out;
421 status = nfserr_no_grace;
3320fef1 422 if (!locks_in_grace(net) && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
cbd0d51a 423 goto out;
fb553c0f 424
1da177e4 425 switch (open->op_claim_type) {
0dd3c192 426 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1da177e4 427 case NFS4_OPEN_CLAIM_NULL:
c0e6bee4 428 status = do_open_lookup(rqstp, cstate, open, &resfh);
1da177e4
LT
429 if (status)
430 goto out;
431 break;
432 case NFS4_OPEN_CLAIM_PREVIOUS:
3320fef1 433 status = nfs4_check_open_reclaim(&open->op_clientid,
0fe492db 434 cstate, nn);
0cf99b91
MJ
435 if (status)
436 goto out;
ba5378b6 437 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
8b289b2c
BF
438 case NFS4_OPEN_CLAIM_FH:
439 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
bbc9c36c 440 status = do_open_fhandle(rqstp, cstate, open);
1da177e4
LT
441 if (status)
442 goto out;
c0e6bee4 443 resfh = &cstate->current_fh;
1da177e4 444 break;
8b289b2c 445 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1da177e4 446 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2fdada03 447 dprintk("NFSD: unsupported OPEN claim type %d\n",
1da177e4
LT
448 open->op_claim_type);
449 status = nfserr_notsupp;
450 goto out;
451 default:
2fdada03 452 dprintk("NFSD: Invalid OPEN claim type %d\n",
1da177e4
LT
453 open->op_claim_type);
454 status = nfserr_inval;
455 goto out;
456 }
457 /*
458 * nfsd4_process_open2() does the actual opening of the file. If
459 * successful, it (1) truncates the file if open->op_truncate was
460 * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
461 */
c0e6bee4 462 status = nfsd4_process_open2(rqstp, resfh, open);
b3fbfe0e
JL
463 WARN(status && open->op_created,
464 "nfsd4_process_open2 failed to open newly-created file! status=%u\n",
465 be32_to_cpu(status));
1da177e4 466out:
c0e6bee4
BF
467 if (resfh && resfh != &cstate->current_fh) {
468 fh_dup2(&cstate->current_fh, resfh);
469 fh_put(resfh);
470 kfree(resfh);
471 }
58fb12e6 472 nfsd4_cleanup_open_state(cstate, open, status);
9411b1d4 473 nfsd4_bump_seqid(cstate, status);
1da177e4
LT
474 return status;
475}
476
9d313b17
BF
477/*
478 * OPEN is the only seqid-mutating operation whose decoding can fail
479 * with a seqid-mutating error (specifically, decoding of user names in
480 * the attributes). Therefore we have to do some processing to look up
481 * the stateowner so that we can bump the seqid.
482 */
483static __be32 nfsd4_open_omfg(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_op *op)
484{
485 struct nfsd4_open *open = (struct nfsd4_open *)&op->u;
486
487 if (!seqid_mutating_err(ntohl(op->status)))
488 return op->status;
489 if (nfsd4_has_session(cstate))
490 return op->status;
491 open->op_xdr_error = op->status;
492 return nfsd4_open(rqstp, cstate, open);
493}
494
1da177e4
LT
495/*
496 * filehandle-manipulating ops.
497 */
7191155b 498static __be32
b591480b
BF
499nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
500 struct svc_fh **getfh)
1da177e4 501{
ca364317 502 if (!cstate->current_fh.fh_dentry)
1da177e4
LT
503 return nfserr_nofilehandle;
504
ca364317 505 *getfh = &cstate->current_fh;
1da177e4
LT
506 return nfs_ok;
507}
508
7191155b 509static __be32
ca364317
BF
510nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
511 struct nfsd4_putfh *putfh)
1da177e4 512{
ca364317
BF
513 fh_put(&cstate->current_fh);
514 cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
515 memcpy(&cstate->current_fh.fh_handle.fh_base, putfh->pf_fhval,
516 putfh->pf_fhlen);
68d93184 517 return fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
1da177e4
LT
518}
519
7191155b 520static __be32
b591480b
BF
521nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
522 void *arg)
1da177e4 523{
b37ad28b 524 __be32 status;
1da177e4 525
ca364317 526 fh_put(&cstate->current_fh);
df547efb 527 status = exp_pseudoroot(rqstp, &cstate->current_fh);
1da177e4
LT
528 return status;
529}
530
7191155b 531static __be32
b591480b
BF
532nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
533 void *arg)
1da177e4 534{
ca364317 535 if (!cstate->save_fh.fh_dentry)
1da177e4
LT
536 return nfserr_restorefh;
537
ca364317 538 fh_dup2(&cstate->current_fh, &cstate->save_fh);
37c593c5
TM
539 if (HAS_STATE_ID(cstate, SAVED_STATE_ID_FLAG)) {
540 memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
541 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
542 }
1da177e4
LT
543 return nfs_ok;
544}
545
7191155b 546static __be32
b591480b
BF
547nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
548 void *arg)
1da177e4 549{
ca364317 550 if (!cstate->current_fh.fh_dentry)
1da177e4
LT
551 return nfserr_nofilehandle;
552
ca364317 553 fh_dup2(&cstate->save_fh, &cstate->current_fh);
37c593c5
TM
554 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG)) {
555 memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t));
556 SET_STATE_ID(cstate, SAVED_STATE_ID_FLAG);
557 }
1da177e4
LT
558 return nfs_ok;
559}
560
561/*
562 * misc nfsv4 ops
563 */
7191155b 564static __be32
ca364317
BF
565nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
566 struct nfsd4_access *access)
1da177e4
LT
567{
568 if (access->ac_req_access & ~NFS3_ACCESS_FULL)
569 return nfserr_inval;
570
571 access->ac_resp_access = access->ac_req_access;
ca364317
BF
572 return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
573 &access->ac_supported);
1da177e4
LT
574}
575
b9c0ef85 576static void gen_boot_verifier(nfs4_verifier *verifier, struct net *net)
ab4684d1
CL
577{
578 __be32 verf[2];
b9c0ef85 579 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
ab4684d1 580
f419992c
JL
581 /*
582 * This is opaque to client, so no need to byte-swap. Use
583 * __force to keep sparse happy
584 */
585 verf[0] = (__force __be32)nn->nfssvc_boot.tv_sec;
586 verf[1] = (__force __be32)nn->nfssvc_boot.tv_usec;
ab4684d1
CL
587 memcpy(verifier->data, verf, sizeof(verifier->data));
588}
589
7191155b 590static __be32
ca364317
BF
591nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
592 struct nfsd4_commit *commit)
1da177e4 593{
b9c0ef85 594 gen_boot_verifier(&commit->co_verf, SVC_NET(rqstp));
75c096f7 595 return nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
ca364317 596 commit->co_count);
1da177e4
LT
597}
598
b37ad28b 599static __be32
ca364317
BF
600nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
601 struct nfsd4_create *create)
1da177e4
LT
602{
603 struct svc_fh resfh;
b37ad28b 604 __be32 status;
1da177e4
LT
605 dev_t rdev;
606
607 fh_init(&resfh, NFS4_FHSIZE);
608
8837abca
MS
609 status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR,
610 NFSD_MAY_CREATE);
1da177e4
LT
611 if (status)
612 return status;
613
3c8e0316
YZ
614 status = check_attr_support(rqstp, cstate, create->cr_bmval,
615 nfsd_attrmask);
616 if (status)
617 return status;
618
1da177e4
LT
619 switch (create->cr_type) {
620 case NF4LNK:
ca364317
BF
621 status = nfsd_symlink(rqstp, &cstate->current_fh,
622 create->cr_name, create->cr_namelen,
1e444f5b 623 create->cr_data, &resfh);
1da177e4
LT
624 break;
625
626 case NF4BLK:
627 rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
628 if (MAJOR(rdev) != create->cr_specdata1 ||
629 MINOR(rdev) != create->cr_specdata2)
630 return nfserr_inval;
ca364317
BF
631 status = nfsd_create(rqstp, &cstate->current_fh,
632 create->cr_name, create->cr_namelen,
633 &create->cr_iattr, S_IFBLK, rdev, &resfh);
1da177e4
LT
634 break;
635
636 case NF4CHR:
637 rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
638 if (MAJOR(rdev) != create->cr_specdata1 ||
639 MINOR(rdev) != create->cr_specdata2)
640 return nfserr_inval;
ca364317
BF
641 status = nfsd_create(rqstp, &cstate->current_fh,
642 create->cr_name, create->cr_namelen,
643 &create->cr_iattr,S_IFCHR, rdev, &resfh);
1da177e4
LT
644 break;
645
646 case NF4SOCK:
ca364317
BF
647 status = nfsd_create(rqstp, &cstate->current_fh,
648 create->cr_name, create->cr_namelen,
649 &create->cr_iattr, S_IFSOCK, 0, &resfh);
1da177e4
LT
650 break;
651
652 case NF4FIFO:
ca364317
BF
653 status = nfsd_create(rqstp, &cstate->current_fh,
654 create->cr_name, create->cr_namelen,
655 &create->cr_iattr, S_IFIFO, 0, &resfh);
1da177e4
LT
656 break;
657
658 case NF4DIR:
659 create->cr_iattr.ia_valid &= ~ATTR_SIZE;
ca364317
BF
660 status = nfsd_create(rqstp, &cstate->current_fh,
661 create->cr_name, create->cr_namelen,
662 &create->cr_iattr, S_IFDIR, 0, &resfh);
1da177e4
LT
663 break;
664
665 default:
666 status = nfserr_badtype;
667 }
668
9208faf2
YZ
669 if (status)
670 goto out;
1da177e4 671
18032ca0
DQ
672 if (create->cr_label.len)
673 nfsd4_security_inode_setsecctx(&resfh, &create->cr_label, create->cr_bmval);
674
9208faf2
YZ
675 if (create->cr_acl != NULL)
676 do_set_nfs4_acl(rqstp, &resfh, create->cr_acl,
677 create->cr_bmval);
678
679 fh_unlock(&cstate->current_fh);
680 set_change_info(&create->cr_cinfo, &cstate->current_fh);
681 fh_dup2(&cstate->current_fh, &resfh);
682out:
1da177e4
LT
683 fh_put(&resfh);
684 return status;
685}
686
7191155b 687static __be32
ca364317
BF
688nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
689 struct nfsd4_getattr *getattr)
1da177e4 690{
b37ad28b 691 __be32 status;
1da177e4 692
8837abca 693 status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
1da177e4
LT
694 if (status)
695 return status;
696
697 if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
698 return nfserr_inval;
699
7e705706
AA
700 getattr->ga_bmval[0] &= nfsd_suppattrs0(cstate->minorversion);
701 getattr->ga_bmval[1] &= nfsd_suppattrs1(cstate->minorversion);
702 getattr->ga_bmval[2] &= nfsd_suppattrs2(cstate->minorversion);
1da177e4 703
ca364317 704 getattr->ga_fhp = &cstate->current_fh;
1da177e4
LT
705 return nfs_ok;
706}
707
7191155b 708static __be32
ca364317
BF
709nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
710 struct nfsd4_link *link)
1da177e4 711{
b37ad28b 712 __be32 status = nfserr_nofilehandle;
1da177e4 713
ca364317 714 if (!cstate->save_fh.fh_dentry)
1da177e4 715 return status;
ca364317
BF
716 status = nfsd_link(rqstp, &cstate->current_fh,
717 link->li_name, link->li_namelen, &cstate->save_fh);
1da177e4 718 if (!status)
ca364317 719 set_change_info(&link->li_cinfo, &cstate->current_fh);
1da177e4
LT
720 return status;
721}
722
0ff7ab46 723static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
1da177e4
LT
724{
725 struct svc_fh tmp_fh;
b37ad28b 726 __be32 ret;
1da177e4
LT
727
728 fh_init(&tmp_fh, NFS4_FHSIZE);
df547efb
BF
729 ret = exp_pseudoroot(rqstp, &tmp_fh);
730 if (ret)
1da177e4 731 return ret;
0ff7ab46 732 if (tmp_fh.fh_dentry == fh->fh_dentry) {
1da177e4
LT
733 fh_put(&tmp_fh);
734 return nfserr_noent;
735 }
736 fh_put(&tmp_fh);
0ff7ab46
BF
737 return nfsd_lookup(rqstp, fh, "..", 2, fh);
738}
739
740static __be32
741nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
742 void *arg)
743{
744 return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
1da177e4
LT
745}
746
7191155b 747static __be32
ca364317
BF
748nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
749 struct nfsd4_lookup *lookup)
1da177e4 750{
ca364317
BF
751 return nfsd_lookup(rqstp, &cstate->current_fh,
752 lookup->lo_name, lookup->lo_len,
753 &cstate->current_fh);
1da177e4
LT
754}
755
7191155b 756static __be32
ca364317
BF
757nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
758 struct nfsd4_read *read)
1da177e4 759{
b37ad28b 760 __be32 status;
1da177e4
LT
761
762 /* no need to check permission - this will be done in nfsd_read() */
763
7e06b7f9 764 read->rd_filp = NULL;
1da177e4
LT
765 if (read->rd_offset >= OFFSET_MAX)
766 return nfserr_inval;
767
9b3234b9
BF
768 /*
769 * If we do a zero copy read, then a client will see read data
770 * that reflects the state of the file *after* performing the
771 * following compound.
772 *
773 * To ensure proper ordering, we therefore turn off zero copy if
774 * the client wants us to do more in this compound:
775 */
776 if (!nfsd4_last_compound_op(rqstp))
779fb0f3 777 clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
9b3234b9 778
1da177e4 779 /* check stateid */
5ccb0066
SK
780 if ((status = nfs4_preprocess_stateid_op(SVC_NET(rqstp),
781 cstate, &read->rd_stateid,
dd453dfd 782 RD_STATE, &read->rd_filp))) {
1da177e4
LT
783 dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
784 goto out;
785 }
786 status = nfs_ok;
787out:
1da177e4 788 read->rd_rqstp = rqstp;
ca364317 789 read->rd_fhp = &cstate->current_fh;
1da177e4
LT
790 return status;
791}
792
7191155b 793static __be32
ca364317
BF
794nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
795 struct nfsd4_readdir *readdir)
1da177e4
LT
796{
797 u64 cookie = readdir->rd_cookie;
798 static const nfs4_verifier zeroverf;
799
800 /* no need to check permission - this will be done in nfsd_readdir() */
801
802 if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
803 return nfserr_inval;
804
7e705706
AA
805 readdir->rd_bmval[0] &= nfsd_suppattrs0(cstate->minorversion);
806 readdir->rd_bmval[1] &= nfsd_suppattrs1(cstate->minorversion);
807 readdir->rd_bmval[2] &= nfsd_suppattrs2(cstate->minorversion);
1da177e4 808
832023bf 809 if ((cookie == 1) || (cookie == 2) ||
1da177e4
LT
810 (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
811 return nfserr_bad_cookie;
812
813 readdir->rd_rqstp = rqstp;
ca364317 814 readdir->rd_fhp = &cstate->current_fh;
1da177e4
LT
815 return nfs_ok;
816}
817
7191155b 818static __be32
ca364317
BF
819nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
820 struct nfsd4_readlink *readlink)
1da177e4
LT
821{
822 readlink->rl_rqstp = rqstp;
ca364317 823 readlink->rl_fhp = &cstate->current_fh;
1da177e4
LT
824 return nfs_ok;
825}
826
7191155b 827static __be32
ca364317
BF
828nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
829 struct nfsd4_remove *remove)
1da177e4 830{
b37ad28b 831 __be32 status;
1da177e4 832
5ccb0066 833 if (locks_in_grace(SVC_NET(rqstp)))
c815afc7 834 return nfserr_grace;
ca364317
BF
835 status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
836 remove->rm_name, remove->rm_namelen);
1da177e4 837 if (!status) {
ca364317
BF
838 fh_unlock(&cstate->current_fh);
839 set_change_info(&remove->rm_cinfo, &cstate->current_fh);
1da177e4
LT
840 }
841 return status;
842}
843
7191155b 844static __be32
ca364317
BF
845nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
846 struct nfsd4_rename *rename)
1da177e4 847{
b37ad28b 848 __be32 status = nfserr_nofilehandle;
1da177e4 849
ca364317 850 if (!cstate->save_fh.fh_dentry)
1da177e4 851 return status;
5ccb0066
SK
852 if (locks_in_grace(SVC_NET(rqstp)) &&
853 !(cstate->save_fh.fh_export->ex_flags & NFSEXP_NOSUBTREECHECK))
c815afc7 854 return nfserr_grace;
ca364317
BF
855 status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
856 rename->rn_snamelen, &cstate->current_fh,
1da177e4 857 rename->rn_tname, rename->rn_tnamelen);
2a6cf944
BF
858 if (status)
859 return status;
860 set_change_info(&rename->rn_sinfo, &cstate->current_fh);
861 set_change_info(&rename->rn_tinfo, &cstate->save_fh);
862 return nfs_ok;
1da177e4
LT
863}
864
dcb488a3
AA
865static __be32
866nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
867 struct nfsd4_secinfo *secinfo)
868{
869 struct svc_fh resfh;
870 struct svc_export *exp;
871 struct dentry *dentry;
872 __be32 err;
873
874 fh_init(&resfh, NFS4_FHSIZE);
29a78a3e
BF
875 err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
876 if (err)
877 return err;
dcb488a3
AA
878 err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
879 secinfo->si_name, secinfo->si_namelen,
880 &exp, &dentry);
881 if (err)
882 return err;
883 if (dentry->d_inode == NULL) {
884 exp_put(exp);
885 err = nfserr_noent;
886 } else
887 secinfo->si_exp = exp;
888 dput(dentry);
56560b9a
BF
889 if (cstate->minorversion)
890 /* See rfc 5661 section 2.6.3.1.1.8 */
891 fh_put(&cstate->current_fh);
dcb488a3
AA
892 return err;
893}
894
04f4ad16
BF
895static __be32
896nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
897 struct nfsd4_secinfo_no_name *sin)
898{
899 __be32 err;
900
901 switch (sin->sin_style) {
902 case NFS4_SECINFO_STYLE4_CURRENT_FH:
903 break;
904 case NFS4_SECINFO_STYLE4_PARENT:
905 err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
906 if (err)
907 return err;
908 break;
909 default:
910 return nfserr_inval;
911 }
bf18f163
KM
912
913 sin->sin_exp = exp_get(cstate->current_fh.fh_export);
04f4ad16
BF
914 fh_put(&cstate->current_fh);
915 return nfs_ok;
916}
917
7191155b 918static __be32
ca364317
BF
919nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
920 struct nfsd4_setattr *setattr)
1da177e4 921{
b37ad28b 922 __be32 status = nfs_ok;
96f6f985 923 int err;
1da177e4 924
1da177e4 925 if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
5ccb0066 926 status = nfs4_preprocess_stateid_op(SVC_NET(rqstp), cstate,
6150ef0d 927 &setattr->sa_stateid, WR_STATE, NULL);
375c5547 928 if (status) {
3e3b4800 929 dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
375c5547
BF
930 return status;
931 }
1da177e4 932 }
96f6f985
AV
933 err = fh_want_write(&cstate->current_fh);
934 if (err)
935 return nfserrno(err);
1da177e4 936 status = nfs_ok;
3c8e0316
YZ
937
938 status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
939 nfsd_attrmask);
940 if (status)
941 goto out;
942
1da177e4 943 if (setattr->sa_acl != NULL)
ca364317
BF
944 status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
945 setattr->sa_acl);
1da177e4 946 if (status)
18f335af 947 goto out;
18032ca0
DQ
948 if (setattr->sa_label.len)
949 status = nfsd4_set_nfs4_label(rqstp, &cstate->current_fh,
950 &setattr->sa_label);
951 if (status)
952 goto out;
ca364317 953 status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
1da177e4 954 0, (time_t)0);
18f335af 955out:
bad0dcff 956 fh_drop_write(&cstate->current_fh);
1da177e4
LT
957 return status;
958}
959
ffe1137b
BF
960static int fill_in_write_vector(struct kvec *vec, struct nfsd4_write *write)
961{
962 int i = 1;
963 int buflen = write->wr_buflen;
964
965 vec[0].iov_base = write->wr_head.iov_base;
966 vec[0].iov_len = min_t(int, buflen, write->wr_head.iov_len);
967 buflen -= vec[0].iov_len;
968
969 while (buflen) {
970 vec[i].iov_base = page_address(write->wr_pagelist[i - 1]);
971 vec[i].iov_len = min_t(int, PAGE_SIZE, buflen);
972 buflen -= vec[i].iov_len;
973 i++;
974 }
975 return i;
976}
977
7191155b 978static __be32
ca364317
BF
979nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
980 struct nfsd4_write *write)
1da177e4
LT
981{
982 stateid_t *stateid = &write->wr_stateid;
983 struct file *filp = NULL;
b37ad28b 984 __be32 status = nfs_ok;
31dec253 985 unsigned long cnt;
ffe1137b 986 int nvecs;
1da177e4
LT
987
988 /* no need to check permission - this will be done in nfsd_write() */
989
990 if (write->wr_offset >= OFFSET_MAX)
991 return nfserr_inval;
992
5ccb0066
SK
993 status = nfs4_preprocess_stateid_op(SVC_NET(rqstp),
994 cstate, stateid, WR_STATE, &filp);
375c5547
BF
995 if (status) {
996 dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
997 return status;
998 }
999
31dec253 1000 cnt = write->wr_buflen;
1da177e4 1001 write->wr_how_written = write->wr_stable_how;
b9c0ef85 1002 gen_boot_verifier(&write->wr_verifier, SVC_NET(rqstp));
1da177e4 1003
ffe1137b
BF
1004 nvecs = fill_in_write_vector(rqstp->rq_vec, write);
1005 WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec));
1006
ca364317 1007 status = nfsd_write(rqstp, &cstate->current_fh, filp,
ffe1137b 1008 write->wr_offset, rqstp->rq_vec, nvecs,
31dec253 1009 &cnt, &write->wr_how_written);
7e06b7f9
N
1010 if (filp)
1011 fput(filp);
1da177e4 1012
31dec253
DS
1013 write->wr_bytes_written = cnt;
1014
1da177e4 1015 return status;
1da177e4
LT
1016}
1017
95d871f0
AS
1018static __be32
1019nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1020 struct nfsd4_fallocate *fallocate, int flags)
1021{
1022 __be32 status = nfserr_notsupp;
1023 struct file *file;
1024
1025 status = nfs4_preprocess_stateid_op(SVC_NET(rqstp), cstate,
1026 &fallocate->falloc_stateid,
1027 WR_STATE, &file);
1028 if (status != nfs_ok) {
1029 dprintk("NFSD: nfsd4_fallocate: couldn't process stateid!\n");
1030 return status;
1031 }
1032
1033 status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, file,
1034 fallocate->falloc_offset,
1035 fallocate->falloc_length,
1036 flags);
1037 fput(file);
1038 return status;
1039}
1040
1041static __be32
1042nfsd4_allocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1043 struct nfsd4_fallocate *fallocate)
1044{
1045 return nfsd4_fallocate(rqstp, cstate, fallocate, 0);
1046}
1047
b0cb9085
AS
1048static __be32
1049nfsd4_deallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1050 struct nfsd4_fallocate *fallocate)
1051{
1052 return nfsd4_fallocate(rqstp, cstate, fallocate,
1053 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
1054}
1055
24bab491
AS
1056static __be32
1057nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1058 struct nfsd4_seek *seek)
1059{
1060 int whence;
1061 __be32 status;
1062 struct file *file;
1063
1064 status = nfs4_preprocess_stateid_op(SVC_NET(rqstp), cstate,
1065 &seek->seek_stateid,
1066 RD_STATE, &file);
1067 if (status) {
1068 dprintk("NFSD: nfsd4_seek: couldn't process stateid!\n");
1069 return status;
1070 }
1071
1072 switch (seek->seek_whence) {
1073 case NFS4_CONTENT_DATA:
1074 whence = SEEK_DATA;
1075 break;
1076 case NFS4_CONTENT_HOLE:
1077 whence = SEEK_HOLE;
1078 break;
1079 default:
1080 status = nfserr_union_notsupp;
1081 goto out;
1082 }
1083
1084 /*
1085 * Note: This call does change file->f_pos, but nothing in NFSD
1086 * should ever file->f_pos.
1087 */
1088 seek->seek_pos = vfs_llseek(file, seek->seek_offset, whence);
1089 if (seek->seek_pos < 0)
1090 status = nfserrno(seek->seek_pos);
1091 else if (seek->seek_pos >= i_size_read(file_inode(file)))
1092 seek->seek_eof = true;
1093
1094out:
1095 fput(file);
1096 return status;
1097}
1098
1da177e4
LT
1099/* This routine never returns NFS_OK! If there are no other errors, it
1100 * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
1101 * attributes matched. VERIFY is implemented by mapping NFSERR_SAME
1102 * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
1103 */
b37ad28b 1104static __be32
c954e2a5 1105_nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
ca364317 1106 struct nfsd4_verify *verify)
1da177e4 1107{
2ebbc012 1108 __be32 *buf, *p;
1da177e4 1109 int count;
b37ad28b 1110 __be32 status;
1da177e4 1111
8837abca 1112 status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
1da177e4
LT
1113 if (status)
1114 return status;
1115
3c8e0316
YZ
1116 status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
1117 if (status)
1118 return status;
1119
1da177e4
LT
1120 if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
1121 || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
1122 return nfserr_inval;
1123 if (verify->ve_attrlen & 3)
1124 return nfserr_inval;
1125
1126 /* count in words:
1127 * bitmap_len(1) + bitmap(2) + attr_len(1) = 4
1128 */
1129 count = 4 + (verify->ve_attrlen >> 2);
1130 buf = kmalloc(count << 2, GFP_KERNEL);
1131 if (!buf)
3e772463 1132 return nfserr_jukebox;
1da177e4 1133
84822d0b 1134 p = buf;
d5184658 1135 status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
ca364317 1136 cstate->current_fh.fh_export,
d5184658
BF
1137 cstate->current_fh.fh_dentry,
1138 verify->ve_bmval,
406a7ea9 1139 rqstp, 0);
41ae6e71
BF
1140 /*
1141 * If nfsd4_encode_fattr() ran out of space, assume that's because
1142 * the attributes are longer (hence different) than those given:
1143 */
84822d0b 1144 if (status == nfserr_resource)
1da177e4
LT
1145 status = nfserr_not_same;
1146 if (status)
1147 goto out_kfree;
1148
95ec28cd
BH
1149 /* skip bitmap */
1150 p = buf + 1 + ntohl(buf[0]);
1da177e4
LT
1151 status = nfserr_not_same;
1152 if (ntohl(*p++) != verify->ve_attrlen)
1153 goto out_kfree;
1154 if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
1155 status = nfserr_same;
1156
1157out_kfree:
1158 kfree(buf);
1159 return status;
1160}
1161
c954e2a5
BF
1162static __be32
1163nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1164 struct nfsd4_verify *verify)
1165{
1166 __be32 status;
1167
1168 status = _nfsd4_verify(rqstp, cstate, verify);
1169 return status == nfserr_not_same ? nfs_ok : status;
1170}
1171
1172static __be32
1173nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1174 struct nfsd4_verify *verify)
1175{
1176 __be32 status;
1177
1178 status = _nfsd4_verify(rqstp, cstate, verify);
1179 return status == nfserr_same ? nfs_ok : status;
1180}
1181
9cf514cc
CH
1182#ifdef CONFIG_NFSD_PNFS
1183static const struct nfsd4_layout_ops *
1184nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type)
1185{
1186 if (!exp->ex_layout_type) {
1187 dprintk("%s: export does not support pNFS\n", __func__);
1188 return NULL;
1189 }
1190
1191 if (exp->ex_layout_type != layout_type) {
1192 dprintk("%s: layout type %d not supported\n",
1193 __func__, layout_type);
1194 return NULL;
1195 }
1196
1197 return nfsd4_layout_ops[layout_type];
1198}
1199
1200static __be32
1201nfsd4_getdeviceinfo(struct svc_rqst *rqstp,
1202 struct nfsd4_compound_state *cstate,
1203 struct nfsd4_getdeviceinfo *gdp)
1204{
1205 const struct nfsd4_layout_ops *ops;
1206 struct nfsd4_deviceid_map *map;
1207 struct svc_export *exp;
1208 __be32 nfserr;
1209
1210 dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n",
1211 __func__,
1212 gdp->gd_layout_type,
1213 gdp->gd_devid.fsid_idx, gdp->gd_devid.generation,
1214 gdp->gd_maxcount);
1215
1216 map = nfsd4_find_devid_map(gdp->gd_devid.fsid_idx);
1217 if (!map) {
1218 dprintk("%s: couldn't find device ID to export mapping!\n",
1219 __func__);
1220 return nfserr_noent;
1221 }
1222
1223 exp = rqst_exp_find(rqstp, map->fsid_type, map->fsid);
1224 if (IS_ERR(exp)) {
1225 dprintk("%s: could not find device id\n", __func__);
1226 return nfserr_noent;
1227 }
1228
1229 nfserr = nfserr_layoutunavailable;
1230 ops = nfsd4_layout_verify(exp, gdp->gd_layout_type);
1231 if (!ops)
1232 goto out;
1233
1234 nfserr = nfs_ok;
1235 if (gdp->gd_maxcount != 0)
1236 nfserr = ops->proc_getdeviceinfo(exp->ex_path.mnt->mnt_sb, gdp);
1237
1238 gdp->gd_notify_types &= ops->notify_types;
1239 exp_put(exp);
1240out:
1241 return nfserr;
1242}
1243
1244static __be32
1245nfsd4_layoutget(struct svc_rqst *rqstp,
1246 struct nfsd4_compound_state *cstate,
1247 struct nfsd4_layoutget *lgp)
1248{
1249 struct svc_fh *current_fh = &cstate->current_fh;
1250 const struct nfsd4_layout_ops *ops;
1251 struct nfs4_layout_stateid *ls;
1252 __be32 nfserr;
1253 int accmode;
1254
1255 switch (lgp->lg_seg.iomode) {
1256 case IOMODE_READ:
1257 accmode = NFSD_MAY_READ;
1258 break;
1259 case IOMODE_RW:
1260 accmode = NFSD_MAY_READ | NFSD_MAY_WRITE;
1261 break;
1262 default:
1263 dprintk("%s: invalid iomode %d\n",
1264 __func__, lgp->lg_seg.iomode);
1265 nfserr = nfserr_badiomode;
1266 goto out;
1267 }
1268
1269 nfserr = fh_verify(rqstp, current_fh, 0, accmode);
1270 if (nfserr)
1271 goto out;
1272
1273 nfserr = nfserr_layoutunavailable;
1274 ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);
1275 if (!ops)
1276 goto out;
1277
1278 /*
1279 * Verify minlength and range as per RFC5661:
1280 * o If loga_length is less than loga_minlength,
1281 * the metadata server MUST return NFS4ERR_INVAL.
1282 * o If the sum of loga_offset and loga_minlength exceeds
1283 * NFS4_UINT64_MAX, and loga_minlength is not
1284 * NFS4_UINT64_MAX, the error NFS4ERR_INVAL MUST result.
1285 * o If the sum of loga_offset and loga_length exceeds
1286 * NFS4_UINT64_MAX, and loga_length is not NFS4_UINT64_MAX,
1287 * the error NFS4ERR_INVAL MUST result.
1288 */
1289 nfserr = nfserr_inval;
1290 if (lgp->lg_seg.length < lgp->lg_minlength ||
1291 (lgp->lg_minlength != NFS4_MAX_UINT64 &&
1292 lgp->lg_minlength > NFS4_MAX_UINT64 - lgp->lg_seg.offset) ||
1293 (lgp->lg_seg.length != NFS4_MAX_UINT64 &&
1294 lgp->lg_seg.length > NFS4_MAX_UINT64 - lgp->lg_seg.offset))
1295 goto out;
1296 if (lgp->lg_seg.length == 0)
1297 goto out;
1298
1299 nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lgp->lg_sid,
1300 true, lgp->lg_layout_type, &ls);
1301 if (nfserr)
1302 goto out;
1303
1304 nfserr = ops->proc_layoutget(current_fh->fh_dentry->d_inode,
1305 current_fh, lgp);
1306 if (nfserr)
1307 goto out_put_stid;
1308
1309 nfserr = nfsd4_insert_layout(lgp, ls);
1310
1311out_put_stid:
1312 nfs4_put_stid(&ls->ls_stid);
1313out:
1314 return nfserr;
1315}
1316
1317static __be32
1318nfsd4_layoutcommit(struct svc_rqst *rqstp,
1319 struct nfsd4_compound_state *cstate,
1320 struct nfsd4_layoutcommit *lcp)
1321{
1322 const struct nfsd4_layout_seg *seg = &lcp->lc_seg;
1323 struct svc_fh *current_fh = &cstate->current_fh;
1324 const struct nfsd4_layout_ops *ops;
1325 loff_t new_size = lcp->lc_last_wr + 1;
1326 struct inode *inode;
1327 struct nfs4_layout_stateid *ls;
1328 __be32 nfserr;
1329
1330 nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_WRITE);
1331 if (nfserr)
1332 goto out;
1333
1334 nfserr = nfserr_layoutunavailable;
1335 ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
1336 if (!ops)
1337 goto out;
1338 inode = current_fh->fh_dentry->d_inode;
1339
1340 nfserr = nfserr_inval;
1341 if (new_size <= seg->offset) {
1342 dprintk("pnfsd: last write before layout segment\n");
1343 goto out;
1344 }
1345 if (new_size > seg->offset + seg->length) {
1346 dprintk("pnfsd: last write beyond layout segment\n");
1347 goto out;
1348 }
1349 if (!lcp->lc_newoffset && new_size > i_size_read(inode)) {
1350 dprintk("pnfsd: layoutcommit beyond EOF\n");
1351 goto out;
1352 }
1353
1354 nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lcp->lc_sid,
1355 false, lcp->lc_layout_type,
1356 &ls);
1357 if (nfserr) {
1358 /* fixup error code as per RFC5661 */
1359 if (nfserr == nfserr_bad_stateid)
1360 nfserr = nfserr_badlayout;
1361 goto out;
1362 }
1363
1364 nfserr = ops->proc_layoutcommit(inode, lcp);
1365 if (nfserr)
1366 goto out_put_stid;
1367
1368 if (new_size > i_size_read(inode)) {
1369 lcp->lc_size_chg = 1;
1370 lcp->lc_newsize = new_size;
1371 } else {
1372 lcp->lc_size_chg = 0;
1373 }
1374
1375out_put_stid:
1376 nfs4_put_stid(&ls->ls_stid);
1377out:
1378 return nfserr;
1379}
1380
1381static __be32
1382nfsd4_layoutreturn(struct svc_rqst *rqstp,
1383 struct nfsd4_compound_state *cstate,
1384 struct nfsd4_layoutreturn *lrp)
1385{
1386 struct svc_fh *current_fh = &cstate->current_fh;
1387 __be32 nfserr;
1388
1389 nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_NOP);
1390 if (nfserr)
1391 goto out;
1392
1393 nfserr = nfserr_layoutunavailable;
1394 if (!nfsd4_layout_verify(current_fh->fh_export, lrp->lr_layout_type))
1395 goto out;
1396
1397 switch (lrp->lr_seg.iomode) {
1398 case IOMODE_READ:
1399 case IOMODE_RW:
1400 case IOMODE_ANY:
1401 break;
1402 default:
1403 dprintk("%s: invalid iomode %d\n", __func__,
1404 lrp->lr_seg.iomode);
1405 nfserr = nfserr_inval;
1406 goto out;
1407 }
1408
1409 switch (lrp->lr_return_type) {
1410 case RETURN_FILE:
1411 nfserr = nfsd4_return_file_layouts(rqstp, cstate, lrp);
1412 break;
1413 case RETURN_FSID:
1414 case RETURN_ALL:
1415 nfserr = nfsd4_return_client_layouts(rqstp, cstate, lrp);
1416 break;
1417 default:
1418 dprintk("%s: invalid return_type %d\n", __func__,
1419 lrp->lr_return_type);
1420 nfserr = nfserr_inval;
1421 break;
1422 }
1423out:
1424 return nfserr;
1425}
1426#endif /* CONFIG_NFSD_PNFS */
1427
1da177e4
LT
1428/*
1429 * NULL call.
1430 */
7111c66e 1431static __be32
1da177e4
LT
1432nfsd4_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
1433{
1434 return nfs_ok;
1435}
1436
e2b20950
SA
1437static inline void nfsd4_increment_op_stats(u32 opnum)
1438{
1439 if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
1440 nfsdstats.nfs4_opcount[opnum]++;
1441}
1442
b591480b
BF
1443typedef __be32(*nfsd4op_func)(struct svc_rqst *, struct nfsd4_compound_state *,
1444 void *);
58e7b33a 1445typedef u32(*nfsd4op_rsize)(struct svc_rqst *, struct nfsd4_op *op);
8b70484c
TM
1446typedef void(*stateid_setter)(struct nfsd4_compound_state *, void *);
1447typedef void(*stateid_getter)(struct nfsd4_compound_state *, void *);
58e7b33a 1448
f9bb94c4
AA
1449enum nfsd4_op_flags {
1450 ALLOWED_WITHOUT_FH = 1 << 0, /* No current filehandle required */
5ece3caf
MJ
1451 ALLOWED_ON_ABSENT_FS = 1 << 1, /* ops processed on absent fs */
1452 ALLOWED_AS_FIRST_OP = 1 << 2, /* ops reqired first in compound */
68d93184
BF
1453 /* For rfc 5661 section 2.6.3.1.1: */
1454 OP_HANDLES_WRONGSEC = 1 << 3,
1455 OP_IS_PUTFH_LIKE = 1 << 4,
1091006c 1456 /*
c856694e
BF
1457 * These are the ops whose result size we estimate before
1458 * encoding, to avoid performing an op then not being able to
1459 * respond or cache a response. This includes writes and setattrs
1460 * as well as the operations usually called "nonidempotent":
1461 */
1462 OP_MODIFIES_SOMETHING = 1 << 5,
1463 /*
1464 * Cache compounds containing these ops in the xid-based drc:
1091006c
BF
1465 * We use the DRC for compounds containing non-idempotent
1466 * operations, *except* those that are 4.1-specific (since
1467 * sessions provide their own EOS), and except for stateful
1468 * operations other than setclientid and setclientid_confirm
1469 * (since sequence numbers provide EOS for open, lock, etc in
1470 * the v4.0 case).
1471 */
c856694e 1472 OP_CACHEME = 1 << 6,
8b70484c
TM
1473 /*
1474 * These are ops which clear current state id.
1475 */
1476 OP_CLEAR_STATEID = 1 << 7,
c856694e
BF
1477};
1478
1479struct nfsd4_operation {
1480 nfsd4op_func op_func;
1481 u32 op_flags;
1482 char *op_name;
58e7b33a
MJ
1483 /* Try to get response size before operation */
1484 nfsd4op_rsize op_rsize_bop;
24ff99c6
BS
1485 stateid_getter op_get_currentstateid;
1486 stateid_setter op_set_currentstateid;
b591480b
BF
1487};
1488
1489static struct nfsd4_operation nfsd4_ops[];
1490
f1c7f79b 1491static const char *nfsd4_op_name(unsigned opnum);
b001a1b6 1492
f9bb94c4 1493/*
57716355 1494 * Enforce NFSv4.1 COMPOUND ordering rules:
f9bb94c4 1495 *
57716355
BF
1496 * Also note, enforced elsewhere:
1497 * - SEQUENCE other than as first op results in
1498 * NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
1d1bc8f2
BF
1499 * - BIND_CONN_TO_SESSION must be the only op in its compound.
1500 * (Enforced in nfsd4_bind_conn_to_session().)
57716355
BF
1501 * - DESTROY_SESSION must be the final operation in a compound, if
1502 * sessionid's in SEQUENCE and DESTROY_SESSION are the same.
1503 * (Enforced in nfsd4_destroy_session().)
f9bb94c4 1504 */
57716355 1505static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
f9bb94c4 1506{
57716355
BF
1507 struct nfsd4_op *op = &args->ops[0];
1508
1509 /* These ordering requirements don't apply to NFSv4.0: */
1510 if (args->minorversion == 0)
1511 return nfs_ok;
1512 /* This is weird, but OK, not our problem: */
1513 if (args->opcnt == 0)
1514 return nfs_ok;
1515 if (op->status == nfserr_op_illegal)
1516 return nfs_ok;
1517 if (!(nfsd4_ops[op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
1518 return nfserr_op_not_in_session;
1519 if (op->opnum == OP_SEQUENCE)
1520 return nfs_ok;
1521 if (args->opcnt != 1)
1522 return nfserr_not_only_op;
1523 return nfs_ok;
f9bb94c4
AA
1524}
1525
22b03214
BF
1526static inline struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
1527{
1528 return &nfsd4_ops[op->opnum];
1529}
1530
1091006c
BF
1531bool nfsd4_cache_this_op(struct nfsd4_op *op)
1532{
e372ba60
BF
1533 if (op->opnum == OP_ILLEGAL)
1534 return false;
c856694e 1535 return OPDESC(op)->op_flags & OP_CACHEME;
1091006c
BF
1536}
1537
68d93184
BF
1538static bool need_wrongsec_check(struct svc_rqst *rqstp)
1539{
1540 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1541 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1542 struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
1543 struct nfsd4_op *next = &argp->ops[resp->opcnt];
1544 struct nfsd4_operation *thisd;
1545 struct nfsd4_operation *nextd;
1546
1547 thisd = OPDESC(this);
1548 /*
1549 * Most ops check wronsec on our own; only the putfh-like ops
1550 * have special rules.
1551 */
1552 if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
1553 return false;
1554 /*
1555 * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
1556 * put-filehandle operation if we're not going to use the
1557 * result:
1558 */
1559 if (argp->opcnt == resp->opcnt)
1560 return false;
51904b08
BF
1561 if (next->opnum == OP_ILLEGAL)
1562 return false;
68d93184
BF
1563 nextd = OPDESC(next);
1564 /*
1565 * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
1566 * errors themselves as necessary; others should check for them
1567 * now:
1568 */
1569 return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
1570}
1571
2d124dfa
BF
1572static void svcxdr_init_encode(struct svc_rqst *rqstp,
1573 struct nfsd4_compoundres *resp)
1574{
1575 struct xdr_stream *xdr = &resp->xdr;
1576 struct xdr_buf *buf = &rqstp->rq_res;
1577 struct kvec *head = buf->head;
1578
1579 xdr->buf = buf;
ddd1ea56 1580 xdr->iov = head;
2d124dfa 1581 xdr->p = head->iov_base + head->iov_len;
a5cddc88 1582 xdr->end = head->iov_base + PAGE_SIZE - rqstp->rq_auth_slack;
6ac90391
BF
1583 /* Tail and page_len should be zero at this point: */
1584 buf->len = buf->head[0].iov_len;
2825a7f9 1585 xdr->scratch.iov_len = 0;
05638dc7 1586 xdr->page_ptr = buf->pages - 1;
2825a7f9 1587 buf->buflen = PAGE_SIZE * (1 + rqstp->rq_page_end - buf->pages)
a5cddc88 1588 - rqstp->rq_auth_slack;
2d124dfa
BF
1589}
1590
1da177e4
LT
1591/*
1592 * COMPOUND call.
1593 */
7111c66e 1594static __be32
1da177e4
LT
1595nfsd4_proc_compound(struct svc_rqst *rqstp,
1596 struct nfsd4_compoundargs *args,
1597 struct nfsd4_compoundres *resp)
1598{
1599 struct nfsd4_op *op;
b591480b 1600 struct nfsd4_operation *opdesc;
e354d571 1601 struct nfsd4_compound_state *cstate = &resp->cstate;
4daeed25
KM
1602 struct svc_fh *current_fh = &cstate->current_fh;
1603 struct svc_fh *save_fh = &cstate->save_fh;
b37ad28b 1604 __be32 status;
1da177e4 1605
2d124dfa 1606 svcxdr_init_encode(rqstp, resp);
4aea24b2 1607 resp->tagp = resp->xdr.p;
1da177e4 1608 /* reserve space for: taglen, tag, and opcnt */
d3f627c8 1609 xdr_reserve_space(&resp->xdr, 8 + args->taglen);
1da177e4
LT
1610 resp->taglen = args->taglen;
1611 resp->tag = args->tag;
1da177e4 1612 resp->rqstp = rqstp;
4daeed25 1613 cstate->minorversion = args->minorversion;
4daeed25
KM
1614 fh_init(current_fh, NFS4_FHSIZE);
1615 fh_init(save_fh, NFS4_FHSIZE);
8ff30fa4
N
1616 /*
1617 * Don't use the deferral mechanism for NFSv4; compounds make it
1618 * too hard to avoid non-idempotency problems.
1619 */
30660e04 1620 clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
1da177e4
LT
1621
1622 /*
1623 * According to RFC3010, this takes precedence over all other errors.
1624 */
1625 status = nfserr_minor_vers_mismatch;
35f7a14f 1626 if (nfsd_minorversion(args->minorversion, NFSD_TEST) <= 0)
1da177e4
LT
1627 goto out;
1628
57716355
BF
1629 status = nfs41_check_op_ordering(args);
1630 if (status) {
f9bb94c4 1631 op = &args->ops[0];
57716355 1632 op->status = status;
f9bb94c4
AA
1633 goto encode_op;
1634 }
1635
1da177e4
LT
1636 while (!status && resp->opcnt < args->opcnt) {
1637 op = &args->ops[resp->opcnt++];
1638
b001a1b6
BH
1639 dprintk("nfsv4 compound op #%d/%d: %d (%s)\n",
1640 resp->opcnt, args->opcnt, op->opnum,
1641 nfsd4_op_name(op->opnum));
1da177e4
LT
1642 /*
1643 * The XDR decode routines may have pre-set op->status;
1644 * for example, if there is a miscellaneous XDR error
1645 * it will be set to nfserr_bad_xdr.
1646 */
9d313b17
BF
1647 if (op->status) {
1648 if (op->opnum == OP_OPEN)
1649 op->status = nfsd4_open_omfg(rqstp, cstate, op);
1da177e4 1650 goto encode_op;
9d313b17 1651 }
1da177e4 1652
22b03214 1653 opdesc = OPDESC(op);
b591480b 1654
4daeed25 1655 if (!current_fh->fh_dentry) {
27d630ec 1656 if (!(opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
42ca0993
BF
1657 op->status = nfserr_nofilehandle;
1658 goto encode_op;
1659 }
4daeed25 1660 } else if (current_fh->fh_export->ex_fslocs.migrated &&
eeac294e 1661 !(opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
42ca0993 1662 op->status = nfserr_moved;
1da177e4
LT
1663 goto encode_op;
1664 }
b591480b 1665
2336745e
KM
1666 fh_clear_wcc(current_fh);
1667
58e7b33a
MJ
1668 /* If op is non-idempotent */
1669 if (opdesc->op_flags & OP_MODIFIES_SOMETHING) {
4c69d585 1670 /*
a8095f7e
BF
1671 * Don't execute this op if we couldn't encode a
1672 * succesful reply:
1673 */
1674 u32 plen = opdesc->op_rsize_bop(rqstp, op);
1675 /*
1676 * Plus if there's another operation, make sure
4c69d585
BF
1677 * we'll have space to at least encode an error:
1678 */
1679 if (resp->opcnt < args->opcnt)
1680 plen += COMPOUND_ERR_SLACK_SPACE;
58e7b33a
MJ
1681 op->status = nfsd4_check_resp_size(resp, plen);
1682 }
1683
1684 if (op->status)
1685 goto encode_op;
1686
b600de7a
BF
1687 if (opdesc->op_get_currentstateid)
1688 opdesc->op_get_currentstateid(cstate, &op->u);
1689 op->status = opdesc->op_func(rqstp, cstate, &op->u);
1da177e4 1690
8b70484c
TM
1691 if (!op->status) {
1692 if (opdesc->op_set_currentstateid)
1693 opdesc->op_set_currentstateid(cstate, &op->u);
1694
1695 if (opdesc->op_flags & OP_CLEAR_STATEID)
37c593c5 1696 clear_current_stateid(cstate);
8b70484c
TM
1697
1698 if (need_wrongsec_check(rqstp))
4daeed25 1699 op->status = check_nfsd_access(current_fh->fh_export, rqstp);
8b70484c 1700 }
68d93184 1701
1da177e4 1702encode_op:
49557cc7 1703 /* Only from SEQUENCE */
4daeed25 1704 if (cstate->status == nfserr_replay_cache) {
da3846a2 1705 dprintk("%s NFS4.1 replay from cache\n", __func__);
abfabf8c 1706 status = op->status;
da3846a2
AA
1707 goto out;
1708 }
a90b061c 1709 if (op->status == nfserr_replay_me) {
a4f1706a 1710 op->replay = &cstate->replay_owner->so_replay;
d0a381dd 1711 nfsd4_encode_replay(&resp->xdr, op);
1da177e4
LT
1712 status = op->status = op->replay->rp_status;
1713 } else {
1714 nfsd4_encode_operation(resp, op);
1715 status = op->status;
1716 }
0407717d
BH
1717
1718 dprintk("nfsv4 compound op %p opcnt %d #%d: %d: status %d\n",
1719 args->ops, args->opcnt, resp->opcnt, op->opnum,
1720 be32_to_cpu(status));
1721
58fb12e6 1722 nfsd4_cstate_clear_replay(cstate);
7e06b7f9
N
1723 /* XXX Ugh, we need to get rid of this kind of special case: */
1724 if (op->opnum == OP_READ && op->u.read.rd_filp)
1725 fput(op->u.read.rd_filp);
e2b20950
SA
1726
1727 nfsd4_increment_op_stats(op->opnum);
1da177e4
LT
1728 }
1729
4daeed25
KM
1730 cstate->status = status;
1731 fh_put(current_fh);
1732 fh_put(save_fh);
1733 BUG_ON(cstate->replay_owner);
1da177e4 1734out:
2f425878 1735 /* Reset deferral mechanism for RPC deferrals */
30660e04 1736 set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
3b12cd98 1737 dprintk("nfsv4 compound returned %d\n", ntohl(status));
1da177e4
LT
1738 return status;
1739}
1740
58e7b33a
MJ
1741#define op_encode_hdr_size (2)
1742#define op_encode_stateid_maxsz (XDR_QUADLEN(NFS4_STATEID_SIZE))
1743#define op_encode_verifier_maxsz (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
1744#define op_encode_change_info_maxsz (5)
1745#define nfs4_fattr_bitmap_maxsz (4)
1746
8c7424cf
BF
1747/* We'll fall back on returning no lockowner if run out of space: */
1748#define op_encode_lockowner_maxsz (0)
58e7b33a
MJ
1749#define op_encode_lock_denied_maxsz (8 + op_encode_lockowner_maxsz)
1750
1751#define nfs4_owner_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
1752
1753#define op_encode_ace_maxsz (3 + nfs4_owner_maxsz)
1754#define op_encode_delegation_maxsz (1 + op_encode_stateid_maxsz + 1 + \
1755 op_encode_ace_maxsz)
1756
1757#define op_encode_channel_attrs_maxsz (6 + 1 + 1)
1758
1759static inline u32 nfsd4_only_status_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1760{
1761 return (op_encode_hdr_size) * sizeof(__be32);
1762}
1763
1764static inline u32 nfsd4_status_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1765{
1766 return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
1767}
1768
1769static inline u32 nfsd4_commit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1770{
1771 return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
1772}
1773
1774static inline u32 nfsd4_create_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1775{
1776 return (op_encode_hdr_size + op_encode_change_info_maxsz
1777 + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
1778}
1779
b86cef60
BF
1780/*
1781 * Note since this is an idempotent operation we won't insist on failing
1782 * the op prematurely if the estimate is too large. We may turn off splice
1783 * reads unnecessarily.
1784 */
1785static inline u32 nfsd4_getattr_rsize(struct svc_rqst *rqstp,
1786 struct nfsd4_op *op)
1787{
1788 u32 *bmap = op->u.getattr.ga_bmval;
1789 u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
1790 u32 ret = 0;
1791
1792 if (bmap0 & FATTR4_WORD0_ACL)
1793 return svc_max_payload(rqstp);
1794 if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
1795 return svc_max_payload(rqstp);
1796
1797 if (bmap1 & FATTR4_WORD1_OWNER) {
1798 ret += IDMAP_NAMESZ + 4;
1799 bmap1 &= ~FATTR4_WORD1_OWNER;
1800 }
1801 if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
1802 ret += IDMAP_NAMESZ + 4;
1803 bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
1804 }
1805 if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
1806 ret += NFS4_FHSIZE + 4;
1807 bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
1808 }
1809 if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
1810 ret += NFSD4_MAX_SEC_LABEL_LEN + 12;
1811 bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
1812 }
1813 /*
1814 * Largest of remaining attributes are 16 bytes (e.g.,
1815 * supported_attributes)
1816 */
1817 ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
1818 /* bitmask, length */
1819 ret += 20;
1820 return ret;
1821}
1822
58e7b33a
MJ
1823static inline u32 nfsd4_link_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1824{
1825 return (op_encode_hdr_size + op_encode_change_info_maxsz)
1826 * sizeof(__be32);
1827}
1828
1829static inline u32 nfsd4_lock_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1830{
1831 return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
1832 * sizeof(__be32);
1833}
1834
1835static inline u32 nfsd4_open_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1836{
1837 return (op_encode_hdr_size + op_encode_stateid_maxsz
1838 + op_encode_change_info_maxsz + 1
1839 + nfs4_fattr_bitmap_maxsz
1840 + op_encode_delegation_maxsz) * sizeof(__be32);
1841}
1842
1843static inline u32 nfsd4_read_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1844{
1845 u32 maxcount = 0, rlen = 0;
1846
1847 maxcount = svc_max_payload(rqstp);
3c7aa15d 1848 rlen = min(op->u.read.rd_length, maxcount);
58e7b33a 1849
622f560e 1850 return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
58e7b33a
MJ
1851}
1852
1853static inline u32 nfsd4_readdir_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1854{
3c7aa15d 1855 u32 maxcount = 0, rlen = 0;
58e7b33a 1856
3c7aa15d
KM
1857 maxcount = svc_max_payload(rqstp);
1858 rlen = min(op->u.readdir.rd_maxcount, maxcount);
58e7b33a 1859
561f0ed4
BF
1860 return (op_encode_hdr_size + op_encode_verifier_maxsz +
1861 XDR_QUADLEN(rlen)) * sizeof(__be32);
58e7b33a
MJ
1862}
1863
1864static inline u32 nfsd4_remove_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1865{
1866 return (op_encode_hdr_size + op_encode_change_info_maxsz)
1867 * sizeof(__be32);
1868}
1869
1870static inline u32 nfsd4_rename_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1871{
1872 return (op_encode_hdr_size + op_encode_change_info_maxsz
1873 + op_encode_change_info_maxsz) * sizeof(__be32);
1874}
1875
ccae70a9
BF
1876static inline u32 nfsd4_sequence_rsize(struct svc_rqst *rqstp,
1877 struct nfsd4_op *op)
1878{
d1d84c96
BF
1879 return (op_encode_hdr_size
1880 + XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32);
ccae70a9
BF
1881}
1882
58e7b33a
MJ
1883static inline u32 nfsd4_setattr_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1884{
1885 return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
1886}
1887
1888static inline u32 nfsd4_setclientid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1889{
480efaee
BF
1890 return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
1891 sizeof(__be32);
58e7b33a
MJ
1892}
1893
1894static inline u32 nfsd4_write_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1895{
f34e432b 1896 return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
58e7b33a
MJ
1897}
1898
1899static inline u32 nfsd4_exchange_id_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1900{
1901 return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
a8bb84bc
KM
1902 1 + 1 + /* eir_flags, spr_how */\
1903 4 + /* spo_must_enforce & _allow with bitmap */\
58e7b33a
MJ
1904 2 + /*eir_server_owner.so_minor_id */\
1905 /* eir_server_owner.so_major_id<> */\
1906 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
1907 /* eir_server_scope<> */\
1908 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
1909 1 + /* eir_server_impl_id array length */\
1910 0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
1911}
1912
1913static inline u32 nfsd4_bind_conn_to_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1914{
1915 return (op_encode_hdr_size + \
1916 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
1917 2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
1918}
1919
1920static inline u32 nfsd4_create_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1921{
1922 return (op_encode_hdr_size + \
1923 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
1924 2 + /* csr_sequence, csr_flags */\
1925 op_encode_channel_attrs_maxsz + \
1926 op_encode_channel_attrs_maxsz) * sizeof(__be32);
1927}
1928
9cf514cc
CH
1929#ifdef CONFIG_NFSD_PNFS
1930/*
1931 * At this stage we don't really know what layout driver will handle the request,
1932 * so we need to define an arbitrary upper bound here.
1933 */
1934#define MAX_LAYOUT_SIZE 128
1935static inline u32 nfsd4_layoutget_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1936{
1937 return (op_encode_hdr_size +
1938 1 /* logr_return_on_close */ +
1939 op_encode_stateid_maxsz +
1940 1 /* nr of layouts */ +
1941 MAX_LAYOUT_SIZE) * sizeof(__be32);
1942}
1943
1944static inline u32 nfsd4_layoutcommit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1945{
1946 return (op_encode_hdr_size +
1947 1 /* locr_newsize */ +
1948 2 /* ns_size */) * sizeof(__be32);
1949}
1950
1951static inline u32 nfsd4_layoutreturn_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
1952{
1953 return (op_encode_hdr_size +
1954 1 /* lrs_stateid */ +
1955 op_encode_stateid_maxsz) * sizeof(__be32);
1956}
1957#endif /* CONFIG_NFSD_PNFS */
1958
20766016 1959static struct nfsd4_operation nfsd4_ops[] = {
b591480b
BF
1960 [OP_ACCESS] = {
1961 .op_func = (nfsd4op_func)nfsd4_access,
b001a1b6 1962 .op_name = "OP_ACCESS",
b591480b
BF
1963 },
1964 [OP_CLOSE] = {
1965 .op_func = (nfsd4op_func)nfsd4_close,
58e7b33a 1966 .op_flags = OP_MODIFIES_SOMETHING,
b001a1b6 1967 .op_name = "OP_CLOSE",
58e7b33a 1968 .op_rsize_bop = (nfsd4op_rsize)nfsd4_status_stateid_rsize,
8b70484c
TM
1969 .op_get_currentstateid = (stateid_getter)nfsd4_get_closestateid,
1970 .op_set_currentstateid = (stateid_setter)nfsd4_set_closestateid,
b591480b
BF
1971 },
1972 [OP_COMMIT] = {
1973 .op_func = (nfsd4op_func)nfsd4_commit,
58e7b33a 1974 .op_flags = OP_MODIFIES_SOMETHING,
b001a1b6 1975 .op_name = "OP_COMMIT",
58e7b33a 1976 .op_rsize_bop = (nfsd4op_rsize)nfsd4_commit_rsize,
b591480b
BF
1977 },
1978 [OP_CREATE] = {
1979 .op_func = (nfsd4op_func)nfsd4_create,
d1471053 1980 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
b001a1b6 1981 .op_name = "OP_CREATE",
58e7b33a 1982 .op_rsize_bop = (nfsd4op_rsize)nfsd4_create_rsize,
b591480b
BF
1983 },
1984 [OP_DELEGRETURN] = {
1985 .op_func = (nfsd4op_func)nfsd4_delegreturn,
58e7b33a 1986 .op_flags = OP_MODIFIES_SOMETHING,
b001a1b6 1987 .op_name = "OP_DELEGRETURN",
58e7b33a 1988 .op_rsize_bop = nfsd4_only_status_rsize,
9428fe1a 1989 .op_get_currentstateid = (stateid_getter)nfsd4_get_delegreturnstateid,
b591480b
BF
1990 },
1991 [OP_GETATTR] = {
1992 .op_func = (nfsd4op_func)nfsd4_getattr,
eeac294e 1993 .op_flags = ALLOWED_ON_ABSENT_FS,
b86cef60 1994 .op_rsize_bop = nfsd4_getattr_rsize,
b001a1b6 1995 .op_name = "OP_GETATTR",
b591480b
BF
1996 },
1997 [OP_GETFH] = {
1998 .op_func = (nfsd4op_func)nfsd4_getfh,
b001a1b6 1999 .op_name = "OP_GETFH",
b591480b
BF
2000 },
2001 [OP_LINK] = {
2002 .op_func = (nfsd4op_func)nfsd4_link,
c856694e
BF
2003 .op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
2004 | OP_CACHEME,
b001a1b6 2005 .op_name = "OP_LINK",
58e7b33a 2006 .op_rsize_bop = (nfsd4op_rsize)nfsd4_link_rsize,
b591480b
BF
2007 },
2008 [OP_LOCK] = {
2009 .op_func = (nfsd4op_func)nfsd4_lock,
58e7b33a 2010 .op_flags = OP_MODIFIES_SOMETHING,
b001a1b6 2011 .op_name = "OP_LOCK",
58e7b33a 2012 .op_rsize_bop = (nfsd4op_rsize)nfsd4_lock_rsize,
62cd4a59 2013 .op_set_currentstateid = (stateid_setter)nfsd4_set_lockstateid,
b591480b
BF
2014 },
2015 [OP_LOCKT] = {
2016 .op_func = (nfsd4op_func)nfsd4_lockt,
b001a1b6 2017 .op_name = "OP_LOCKT",
b591480b
BF
2018 },
2019 [OP_LOCKU] = {
2020 .op_func = (nfsd4op_func)nfsd4_locku,
58e7b33a 2021 .op_flags = OP_MODIFIES_SOMETHING,
b001a1b6 2022 .op_name = "OP_LOCKU",
58e7b33a 2023 .op_rsize_bop = (nfsd4op_rsize)nfsd4_status_stateid_rsize,
62cd4a59 2024 .op_get_currentstateid = (stateid_getter)nfsd4_get_lockustateid,
b591480b
BF
2025 },
2026 [OP_LOOKUP] = {
2027 .op_func = (nfsd4op_func)nfsd4_lookup,
d1471053 2028 .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
b001a1b6 2029 .op_name = "OP_LOOKUP",
b591480b
BF
2030 },
2031 [OP_LOOKUPP] = {
2032 .op_func = (nfsd4op_func)nfsd4_lookupp,
d1471053 2033 .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
b001a1b6 2034 .op_name = "OP_LOOKUPP",
b591480b
BF
2035 },
2036 [OP_NVERIFY] = {
2037 .op_func = (nfsd4op_func)nfsd4_nverify,
b001a1b6 2038 .op_name = "OP_NVERIFY",
b591480b
BF
2039 },
2040 [OP_OPEN] = {
2041 .op_func = (nfsd4op_func)nfsd4_open,
58e7b33a 2042 .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
b001a1b6 2043 .op_name = "OP_OPEN",
58e7b33a 2044 .op_rsize_bop = (nfsd4op_rsize)nfsd4_open_rsize,
8b70484c 2045 .op_set_currentstateid = (stateid_setter)nfsd4_set_openstateid,
b591480b
BF
2046 },
2047 [OP_OPEN_CONFIRM] = {
2048 .op_func = (nfsd4op_func)nfsd4_open_confirm,
58e7b33a 2049 .op_flags = OP_MODIFIES_SOMETHING,
b001a1b6 2050 .op_name = "OP_OPEN_CONFIRM",
58e7b33a 2051 .op_rsize_bop = (nfsd4op_rsize)nfsd4_status_stateid_rsize,
b591480b
BF
2052 },
2053 [OP_OPEN_DOWNGRADE] = {
2054 .op_func = (nfsd4op_func)nfsd4_open_downgrade,
58e7b33a 2055 .op_flags = OP_MODIFIES_SOMETHING,
b001a1b6 2056 .op_name = "OP_OPEN_DOWNGRADE",
58e7b33a 2057 .op_rsize_bop = (nfsd4op_rsize)nfsd4_status_stateid_rsize,
9428fe1a
TM
2058 .op_get_currentstateid = (stateid_getter)nfsd4_get_opendowngradestateid,
2059 .op_set_currentstateid = (stateid_setter)nfsd4_set_opendowngradestateid,
b591480b
BF
2060 },
2061 [OP_PUTFH] = {
2062 .op_func = (nfsd4op_func)nfsd4_putfh,
68d93184 2063 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
5b648699 2064 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
b001a1b6 2065 .op_name = "OP_PUTFH",
58e7b33a 2066 .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
b591480b 2067 },
eeac294e 2068 [OP_PUTPUBFH] = {
a1c8c4d1 2069 .op_func = (nfsd4op_func)nfsd4_putrootfh,
68d93184 2070 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
5b648699 2071 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
b001a1b6 2072 .op_name = "OP_PUTPUBFH",
58e7b33a 2073 .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
eeac294e 2074 },
b591480b
BF
2075 [OP_PUTROOTFH] = {
2076 .op_func = (nfsd4op_func)nfsd4_putrootfh,
68d93184 2077 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
5b648699 2078 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
b001a1b6 2079 .op_name = "OP_PUTROOTFH",
58e7b33a 2080 .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
b591480b
BF
2081 },
2082 [OP_READ] = {
2083 .op_func = (nfsd4op_func)nfsd4_read,
b001a1b6 2084 .op_name = "OP_READ",
58e7b33a 2085 .op_rsize_bop = (nfsd4op_rsize)nfsd4_read_rsize,
30813e27 2086 .op_get_currentstateid = (stateid_getter)nfsd4_get_readstateid,
b591480b
BF
2087 },
2088 [OP_READDIR] = {
2089 .op_func = (nfsd4op_func)nfsd4_readdir,
b001a1b6 2090 .op_name = "OP_READDIR",
58e7b33a 2091 .op_rsize_bop = (nfsd4op_rsize)nfsd4_readdir_rsize,
b591480b
BF
2092 },
2093 [OP_READLINK] = {
2094 .op_func = (nfsd4op_func)nfsd4_readlink,
b001a1b6 2095 .op_name = "OP_READLINK",
b591480b
BF
2096 },
2097 [OP_REMOVE] = {
2098 .op_func = (nfsd4op_func)nfsd4_remove,
c856694e 2099 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
b001a1b6 2100 .op_name = "OP_REMOVE",
58e7b33a 2101 .op_rsize_bop = (nfsd4op_rsize)nfsd4_remove_rsize,
b591480b
BF
2102 },
2103 [OP_RENAME] = {
2104 .op_func = (nfsd4op_func)nfsd4_rename,
c856694e 2105 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
58e7b33a 2106 .op_name = "OP_RENAME",
58e7b33a 2107 .op_rsize_bop = (nfsd4op_rsize)nfsd4_rename_rsize,
b591480b
BF
2108 },
2109 [OP_RENEW] = {
2110 .op_func = (nfsd4op_func)nfsd4_renew,
58e7b33a
MJ
2111 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
2112 | OP_MODIFIES_SOMETHING,
b001a1b6 2113 .op_name = "OP_RENEW",
58e7b33a
MJ
2114 .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
2115
b591480b
BF
2116 },
2117 [OP_RESTOREFH] = {
2118 .op_func = (nfsd4op_func)nfsd4_restorefh,
68d93184 2119 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
58e7b33a 2120 | OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
b001a1b6 2121 .op_name = "OP_RESTOREFH",
58e7b33a 2122 .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
b591480b
BF
2123 },
2124 [OP_SAVEFH] = {
2125 .op_func = (nfsd4op_func)nfsd4_savefh,
58e7b33a 2126 .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
b001a1b6 2127 .op_name = "OP_SAVEFH",
58e7b33a 2128 .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
b591480b 2129 },
dcb488a3
AA
2130 [OP_SECINFO] = {
2131 .op_func = (nfsd4op_func)nfsd4_secinfo,
68d93184 2132 .op_flags = OP_HANDLES_WRONGSEC,
b001a1b6 2133 .op_name = "OP_SECINFO",
dcb488a3 2134 },
b591480b
BF
2135 [OP_SETATTR] = {
2136 .op_func = (nfsd4op_func)nfsd4_setattr,
b001a1b6 2137 .op_name = "OP_SETATTR",
c856694e 2138 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
58e7b33a 2139 .op_rsize_bop = (nfsd4op_rsize)nfsd4_setattr_rsize,
1e97b519 2140 .op_get_currentstateid = (stateid_getter)nfsd4_get_setattrstateid,
b591480b
BF
2141 },
2142 [OP_SETCLIENTID] = {
2143 .op_func = (nfsd4op_func)nfsd4_setclientid,
58e7b33a 2144 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
c856694e 2145 | OP_MODIFIES_SOMETHING | OP_CACHEME,
b001a1b6 2146 .op_name = "OP_SETCLIENTID",
58e7b33a 2147 .op_rsize_bop = (nfsd4op_rsize)nfsd4_setclientid_rsize,
b591480b
BF
2148 },
2149 [OP_SETCLIENTID_CONFIRM] = {
2150 .op_func = (nfsd4op_func)nfsd4_setclientid_confirm,
58e7b33a 2151 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
c856694e 2152 | OP_MODIFIES_SOMETHING | OP_CACHEME,
b001a1b6 2153 .op_name = "OP_SETCLIENTID_CONFIRM",
58e7b33a 2154 .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
b591480b
BF
2155 },
2156 [OP_VERIFY] = {
2157 .op_func = (nfsd4op_func)nfsd4_verify,
b001a1b6 2158 .op_name = "OP_VERIFY",
b591480b
BF
2159 },
2160 [OP_WRITE] = {
2161 .op_func = (nfsd4op_func)nfsd4_write,
c856694e 2162 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
b001a1b6 2163 .op_name = "OP_WRITE",
58e7b33a 2164 .op_rsize_bop = (nfsd4op_rsize)nfsd4_write_rsize,
30813e27 2165 .op_get_currentstateid = (stateid_getter)nfsd4_get_writestateid,
b591480b
BF
2166 },
2167 [OP_RELEASE_LOCKOWNER] = {
2168 .op_func = (nfsd4op_func)nfsd4_release_lockowner,
58e7b33a
MJ
2169 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
2170 | OP_MODIFIES_SOMETHING,
b001a1b6 2171 .op_name = "OP_RELEASE_LOCKOWNER",
58e7b33a 2172 .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
b591480b 2173 },
069b6ad4
AA
2174
2175 /* NFSv4.1 operations */
2176 [OP_EXCHANGE_ID] = {
2177 .op_func = (nfsd4op_func)nfsd4_exchange_id,
58e7b33a
MJ
2178 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
2179 | OP_MODIFIES_SOMETHING,
069b6ad4 2180 .op_name = "OP_EXCHANGE_ID",
58e7b33a 2181 .op_rsize_bop = (nfsd4op_rsize)nfsd4_exchange_id_rsize,
069b6ad4 2182 },
cb73a9f4
BF
2183 [OP_BACKCHANNEL_CTL] = {
2184 .op_func = (nfsd4op_func)nfsd4_backchannel_ctl,
2185 .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
2186 .op_name = "OP_BACKCHANNEL_CTL",
2187 .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
2188 },
1d1bc8f2
BF
2189 [OP_BIND_CONN_TO_SESSION] = {
2190 .op_func = (nfsd4op_func)nfsd4_bind_conn_to_session,
58e7b33a
MJ
2191 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
2192 | OP_MODIFIES_SOMETHING,
1d1bc8f2 2193 .op_name = "OP_BIND_CONN_TO_SESSION",
58e7b33a 2194 .op_rsize_bop = (nfsd4op_rsize)nfsd4_bind_conn_to_session_rsize,
1d1bc8f2 2195 },
069b6ad4
AA
2196 [OP_CREATE_SESSION] = {
2197 .op_func = (nfsd4op_func)nfsd4_create_session,
58e7b33a
MJ
2198 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
2199 | OP_MODIFIES_SOMETHING,
069b6ad4 2200 .op_name = "OP_CREATE_SESSION",
58e7b33a 2201 .op_rsize_bop = (nfsd4op_rsize)nfsd4_create_session_rsize,
069b6ad4
AA
2202 },
2203 [OP_DESTROY_SESSION] = {
2204 .op_func = (nfsd4op_func)nfsd4_destroy_session,
58e7b33a
MJ
2205 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
2206 | OP_MODIFIES_SOMETHING,
069b6ad4 2207 .op_name = "OP_DESTROY_SESSION",
58e7b33a 2208 .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
069b6ad4
AA
2209 },
2210 [OP_SEQUENCE] = {
2211 .op_func = (nfsd4op_func)nfsd4_sequence,
f9bb94c4 2212 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
069b6ad4 2213 .op_name = "OP_SEQUENCE",
d1d84c96 2214 .op_rsize_bop = (nfsd4op_rsize)nfsd4_sequence_rsize,
069b6ad4 2215 },
094b5d74 2216 [OP_DESTROY_CLIENTID] = {
345c2842 2217 .op_func = (nfsd4op_func)nfsd4_destroy_clientid,
58e7b33a
MJ
2218 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
2219 | OP_MODIFIES_SOMETHING,
094b5d74 2220 .op_name = "OP_DESTROY_CLIENTID",
58e7b33a 2221 .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
094b5d74 2222 },
4dc6ec00
BF
2223 [OP_RECLAIM_COMPLETE] = {
2224 .op_func = (nfsd4op_func)nfsd4_reclaim_complete,
58e7b33a 2225 .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
4dc6ec00 2226 .op_name = "OP_RECLAIM_COMPLETE",
58e7b33a 2227 .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
4dc6ec00 2228 },
04f4ad16
BF
2229 [OP_SECINFO_NO_NAME] = {
2230 .op_func = (nfsd4op_func)nfsd4_secinfo_no_name,
68d93184 2231 .op_flags = OP_HANDLES_WRONGSEC,
04f4ad16
BF
2232 .op_name = "OP_SECINFO_NO_NAME",
2233 },
17456804
BS
2234 [OP_TEST_STATEID] = {
2235 .op_func = (nfsd4op_func)nfsd4_test_stateid,
2236 .op_flags = ALLOWED_WITHOUT_FH,
2237 .op_name = "OP_TEST_STATEID",
2238 },
e1ca12df
BS
2239 [OP_FREE_STATEID] = {
2240 .op_func = (nfsd4op_func)nfsd4_free_stateid,
58e7b33a 2241 .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
e1ca12df 2242 .op_name = "OP_FREE_STATEID",
a1dc6955 2243 .op_get_currentstateid = (stateid_getter)nfsd4_get_freestateid,
58e7b33a 2244 .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
e1ca12df 2245 },
9cf514cc
CH
2246#ifdef CONFIG_NFSD_PNFS
2247 [OP_GETDEVICEINFO] = {
2248 .op_func = (nfsd4op_func)nfsd4_getdeviceinfo,
2249 .op_flags = ALLOWED_WITHOUT_FH,
2250 .op_name = "OP_GETDEVICEINFO",
2251 },
2252 [OP_LAYOUTGET] = {
2253 .op_func = (nfsd4op_func)nfsd4_layoutget,
2254 .op_flags = OP_MODIFIES_SOMETHING,
2255 .op_name = "OP_LAYOUTGET",
2256 .op_rsize_bop = (nfsd4op_rsize)nfsd4_layoutget_rsize,
2257 },
2258 [OP_LAYOUTCOMMIT] = {
2259 .op_func = (nfsd4op_func)nfsd4_layoutcommit,
2260 .op_flags = OP_MODIFIES_SOMETHING,
2261 .op_name = "OP_LAYOUTCOMMIT",
2262 .op_rsize_bop = (nfsd4op_rsize)nfsd4_layoutcommit_rsize,
2263 },
2264 [OP_LAYOUTRETURN] = {
2265 .op_func = (nfsd4op_func)nfsd4_layoutreturn,
2266 .op_flags = OP_MODIFIES_SOMETHING,
2267 .op_name = "OP_LAYOUTRETURN",
2268 .op_rsize_bop = (nfsd4op_rsize)nfsd4_layoutreturn_rsize,
2269 },
2270#endif /* CONFIG_NFSD_PNFS */
24bab491
AS
2271
2272 /* NFSv4.2 operations */
95d871f0
AS
2273 [OP_ALLOCATE] = {
2274 .op_func = (nfsd4op_func)nfsd4_allocate,
2275 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
2276 .op_name = "OP_ALLOCATE",
2277 .op_rsize_bop = (nfsd4op_rsize)nfsd4_write_rsize,
2278 },
b0cb9085
AS
2279 [OP_DEALLOCATE] = {
2280 .op_func = (nfsd4op_func)nfsd4_deallocate,
2281 .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
2282 .op_name = "OP_DEALLOCATE",
2283 .op_rsize_bop = (nfsd4op_rsize)nfsd4_write_rsize,
2284 },
24bab491
AS
2285 [OP_SEEK] = {
2286 .op_func = (nfsd4op_func)nfsd4_seek,
2287 .op_name = "OP_SEEK",
2288 },
b591480b
BF
2289};
2290
4f0cefbf
BF
2291int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
2292{
2293 struct nfsd4_operation *opdesc;
2294 nfsd4op_rsize estimator;
2295
2296 if (op->opnum == OP_ILLEGAL)
2297 return op_encode_hdr_size * sizeof(__be32);
2298 opdesc = OPDESC(op);
2299 estimator = opdesc->op_rsize_bop;
2300 return estimator ? estimator(rqstp, op) : PAGE_SIZE;
2301}
2302
07d1f802
BF
2303void warn_on_nonidempotent_op(struct nfsd4_op *op)
2304{
2305 if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
2306 pr_err("unable to encode reply to nonidempotent op %d (%s)\n",
2307 op->opnum, nfsd4_op_name(op->opnum));
2308 WARN_ON_ONCE(1);
2309 }
2310}
2311
f1c7f79b 2312static const char *nfsd4_op_name(unsigned opnum)
b001a1b6
BH
2313{
2314 if (opnum < ARRAY_SIZE(nfsd4_ops))
2315 return nfsd4_ops[opnum].op_name;
2316 return "unknown_operation";
2317}
2318
1da177e4 2319#define nfsd4_voidres nfsd4_voidargs
1da177e4
LT
2320struct nfsd4_voidargs { int dummy; };
2321
1da177e4 2322static struct svc_procedure nfsd_procedures4[2] = {
0a93a47f
YZ
2323 [NFSPROC4_NULL] = {
2324 .pc_func = (svc_procfunc) nfsd4_proc_null,
2325 .pc_encode = (kxdrproc_t) nfs4svc_encode_voidres,
2326 .pc_argsize = sizeof(struct nfsd4_voidargs),
2327 .pc_ressize = sizeof(struct nfsd4_voidres),
2328 .pc_cachetype = RC_NOCACHE,
2329 .pc_xdrressize = 1,
2330 },
2331 [NFSPROC4_COMPOUND] = {
2332 .pc_func = (svc_procfunc) nfsd4_proc_compound,
2333 .pc_decode = (kxdrproc_t) nfs4svc_decode_compoundargs,
2334 .pc_encode = (kxdrproc_t) nfs4svc_encode_compoundres,
2335 .pc_argsize = sizeof(struct nfsd4_compoundargs),
2336 .pc_ressize = sizeof(struct nfsd4_compoundres),
3e98abff 2337 .pc_release = nfsd4_release_compoundargs,
0a93a47f
YZ
2338 .pc_cachetype = RC_NOCACHE,
2339 .pc_xdrressize = NFSD_BUFSIZE/4,
2340 },
1da177e4
LT
2341};
2342
2343struct svc_version nfsd_version4 = {
2344 .vs_vers = 4,
2345 .vs_nproc = 2,
2346 .vs_proc = nfsd_procedures4,
2347 .vs_dispatch = nfsd_dispatch,
2348 .vs_xdrsize = NFS4_SVC_XDRSIZE,
7e55b59b 2349 .vs_rpcb_optnl = 1,
1da177e4
LT
2350};
2351
2352/*
2353 * Local variables:
2354 * c-basic-offset: 8
2355 * End:
2356 */
This page took 0.755222 seconds and 5 git commands to generate.