]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/nfs/nfs4proc.c
UBUNTU: Ubuntu-5.11.0-22.23
[mirror_ubuntu-hirsute-kernel.git] / fs / nfs / nfs4proc.c
CommitLineData
1da177e4
LT
1/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
1da177e4
LT
39#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
652f89f6
TM
42#include <linux/ratelimit.h>
43#include <linux/printk.h>
5a0e3ad6 44#include <linux/slab.h>
1da177e4
LT
45#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
9b7160c5 50#include <linux/nfs_mount.h>
1da177e4 51#include <linux/namei.h>
02a913a7 52#include <linux/mount.h>
99fe60d0 53#include <linux/module.h>
64c2ce8b 54#include <linux/xattr.h>
c7a360b0 55#include <linux/utsname.h>
d310310c 56#include <linux/freezer.h>
1eb5d98f 57#include <linux/iversion.h>
1da177e4 58
4ce79717 59#include "nfs4_fs.h"
1da177e4 60#include "delegation.h"
101070ca 61#include "internal.h"
006ea73e 62#include "iostat.h"
fc931582 63#include "callback.h"
b1f69b75 64#include "pnfs.h"
f092075d 65#include "netns.h"
39d43d16 66#include "sysfs.h"
40c64c26 67#include "nfs4idmap.h"
73e39aaa 68#include "nfs4session.h"
de242c0b 69#include "fscache.h"
012a211a 70#include "nfs42.h"
1da177e4 71
c6d01c6f
TM
72#include "nfs4trace.h"
73
c5675526
AS
74#ifdef CONFIG_NFS_V4_2
75#include "nfs42.h"
76#endif /* CONFIG_NFS_V4_2 */
77
1da177e4
LT
78#define NFSDBG_FACILITY NFSDBG_PROC
79
30846df0
TM
80#define NFS4_BITMASK_SZ 3
81
2066fe89 82#define NFS4_POLL_RETRY_MIN (HZ/10)
1da177e4
LT
83#define NFS4_POLL_RETRY_MAX (15*HZ)
84
a1d1c4f1
TM
85/* file attributes which can be mapped to nfs attributes */
86#define NFS4_VALID_ATTRS (ATTR_MODE \
87 | ATTR_UID \
88 | ATTR_GID \
89 | ATTR_SIZE \
90 | ATTR_ATIME \
91 | ATTR_MTIME \
92 | ATTR_CTIME \
93 | ATTR_ATIME_SET \
94 | ATTR_MTIME_SET)
95
cdd4e68b 96struct nfs4_opendata;
b257957e 97static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
1da177e4 98static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
81934ddb 99static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
a841b54d 100static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
a52458b4 101static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
0ab64e0e 102 struct nfs_fattr *fattr, struct iattr *sattr,
29b59f94 103 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
1775fd3e 104 struct nfs4_label *olabel);
f062eb6c 105#ifdef CONFIG_NFS_V4_1
3be0f80b 106static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
a52458b4 107 const struct cred *cred,
3be0f80b
TM
108 struct nfs4_slot *slot,
109 bool is_privileged);
ab7cb0df 110static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
a52458b4 111 const struct cred *);
f0b0bf88 112static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
a52458b4 113 const struct cred *, bool);
f062eb6c 114#endif
76bd5c01
OK
115static void nfs4_bitmask_adjust(__u32 *bitmask, struct inode *inode,
116 struct nfs_server *server,
117 struct nfs4_label *label);
aa9c2669
DQ
118
119#ifdef CONFIG_NFS_V4_SECURITY_LABEL
120static inline struct nfs4_label *
121nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
122 struct iattr *sattr, struct nfs4_label *label)
123{
124 int err;
125
126 if (label == NULL)
127 return NULL;
128
129 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
130 return NULL;
131
aa9c2669
DQ
132 err = security_dentry_init_security(dentry, sattr->ia_mode,
133 &dentry->d_name, (void **)&label->label, &label->len);
134 if (err == 0)
135 return label;
136
137 return NULL;
138}
139static inline void
140nfs4_label_release_security(struct nfs4_label *label)
141{
a6ea17fe
CS
142 struct lsmcontext scaff; /* scaffolding */
143
144 if (label) {
145 lsmcontext_init(&scaff, label->label, label->len, 0);
146 security_release_secctx(&scaff);
147 }
aa9c2669
DQ
148}
149static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
150{
151 if (label)
152 return server->attr_bitmask;
153
154 return server->attr_bitmask_nl;
155}
156#else
157static inline struct nfs4_label *
158nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
159 struct iattr *sattr, struct nfs4_label *l)
160{ return NULL; }
161static inline void
162nfs4_label_release_security(struct nfs4_label *label)
163{ return; }
164static inline u32 *
165nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
166{ return server->attr_bitmask; }
167#endif
168
1da177e4 169/* Prevent leaks of NFSv4 errors into userland */
46f72f57 170static int nfs4_map_errors(int err)
1da177e4 171{
52567b03
TM
172 if (err >= -1000)
173 return err;
174 switch (err) {
175 case -NFS4ERR_RESOURCE:
30005121
WAA
176 case -NFS4ERR_LAYOUTTRYLATER:
177 case -NFS4ERR_RECALLCONFLICT:
52567b03 178 return -EREMOTEIO;
7ebb9315 179 case -NFS4ERR_WRONGSEC:
8897538e 180 case -NFS4ERR_WRONG_CRED:
7ebb9315 181 return -EPERM;
3ddeb7c5
TM
182 case -NFS4ERR_BADOWNER:
183 case -NFS4ERR_BADNAME:
184 return -EINVAL;
fb13bfa7
TM
185 case -NFS4ERR_SHARE_DENIED:
186 return -EACCES;
f25efd85
SD
187 case -NFS4ERR_MINOR_VERS_MISMATCH:
188 return -EPROTONOSUPPORT;
6e3cf241
TM
189 case -NFS4ERR_FILE_OPEN:
190 return -EBUSY;
9fff59ed
TM
191 case -NFS4ERR_NOT_SAME:
192 return -ENOTSYNC;
52567b03 193 default:
1da177e4 194 dprintk("%s could not handle NFSv4 error %d\n",
3110ff80 195 __func__, -err);
52567b03 196 break;
1da177e4 197 }
52567b03 198 return -EIO;
1da177e4
LT
199}
200
201/*
202 * This is our standard bitmap for GETATTR requests.
203 */
1549210f 204const u32 nfs4_fattr_bitmap[3] = {
1da177e4
LT
205 FATTR4_WORD0_TYPE
206 | FATTR4_WORD0_CHANGE
207 | FATTR4_WORD0_SIZE
208 | FATTR4_WORD0_FSID
209 | FATTR4_WORD0_FILEID,
210 FATTR4_WORD1_MODE
211 | FATTR4_WORD1_NUMLINKS
212 | FATTR4_WORD1_OWNER
213 | FATTR4_WORD1_OWNER_GROUP
214 | FATTR4_WORD1_RAWDEV
215 | FATTR4_WORD1_SPACE_USED
216 | FATTR4_WORD1_TIME_ACCESS
217 | FATTR4_WORD1_TIME_METADATA
ea96d1ec
AS
218 | FATTR4_WORD1_TIME_MODIFY
219 | FATTR4_WORD1_MOUNTED_ON_FILEID,
aa9c2669
DQ
220#ifdef CONFIG_NFS_V4_SECURITY_LABEL
221 FATTR4_WORD2_SECURITY_LABEL
222#endif
1da177e4
LT
223};
224
1549210f
TM
225static const u32 nfs4_pnfs_open_bitmap[3] = {
226 FATTR4_WORD0_TYPE
227 | FATTR4_WORD0_CHANGE
228 | FATTR4_WORD0_SIZE
229 | FATTR4_WORD0_FSID
230 | FATTR4_WORD0_FILEID,
231 FATTR4_WORD1_MODE
232 | FATTR4_WORD1_NUMLINKS
233 | FATTR4_WORD1_OWNER
234 | FATTR4_WORD1_OWNER_GROUP
235 | FATTR4_WORD1_RAWDEV
236 | FATTR4_WORD1_SPACE_USED
237 | FATTR4_WORD1_TIME_ACCESS
238 | FATTR4_WORD1_TIME_METADATA
239 | FATTR4_WORD1_TIME_MODIFY,
240 FATTR4_WORD2_MDSTHRESHOLD
95864c91
TM
241#ifdef CONFIG_NFS_V4_SECURITY_LABEL
242 | FATTR4_WORD2_SECURITY_LABEL
243#endif
1549210f
TM
244};
245
e23008ec
AA
246static const u32 nfs4_open_noattr_bitmap[3] = {
247 FATTR4_WORD0_TYPE
e23008ec
AA
248 | FATTR4_WORD0_FILEID,
249};
250
a09df2ca 251const u32 nfs4_statfs_bitmap[3] = {
1da177e4
LT
252 FATTR4_WORD0_FILES_AVAIL
253 | FATTR4_WORD0_FILES_FREE
254 | FATTR4_WORD0_FILES_TOTAL,
255 FATTR4_WORD1_SPACE_AVAIL
256 | FATTR4_WORD1_SPACE_FREE
257 | FATTR4_WORD1_SPACE_TOTAL
258};
259
a09df2ca 260const u32 nfs4_pathconf_bitmap[3] = {
1da177e4
LT
261 FATTR4_WORD0_MAXLINK
262 | FATTR4_WORD0_MAXNAME,
263 0
264};
265
dae100c2 266const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
1da177e4
LT
267 | FATTR4_WORD0_MAXREAD
268 | FATTR4_WORD0_MAXWRITE
269 | FATTR4_WORD0_LEASE_TIME,
55b6e774 270 FATTR4_WORD1_TIME_DELTA
dae100c2
FI
271 | FATTR4_WORD1_FS_LAYOUT_TYPES,
272 FATTR4_WORD2_LAYOUT_BLKSIZE
2a92ee92 273 | FATTR4_WORD2_CLONE_BLKSIZE
b78ef845 274 | FATTR4_WORD2_XATTR_SUPPORT
1da177e4
LT
275};
276
a09df2ca 277const u32 nfs4_fs_locations_bitmap[3] = {
c05cefcc 278 FATTR4_WORD0_CHANGE
830b8e33
MN
279 | FATTR4_WORD0_SIZE
280 | FATTR4_WORD0_FSID
281 | FATTR4_WORD0_FILEID
282 | FATTR4_WORD0_FS_LOCATIONS,
c05cefcc 283 FATTR4_WORD1_OWNER
830b8e33
MN
284 | FATTR4_WORD1_OWNER_GROUP
285 | FATTR4_WORD1_RAWDEV
286 | FATTR4_WORD1_SPACE_USED
287 | FATTR4_WORD1_TIME_ACCESS
288 | FATTR4_WORD1_TIME_METADATA
289 | FATTR4_WORD1_TIME_MODIFY
a09df2ca 290 | FATTR4_WORD1_MOUNTED_ON_FILEID,
830b8e33
MN
291};
292
30846df0
TM
293static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
294 struct inode *inode)
295{
296 unsigned long cache_validity;
297
298 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
299 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
300 return;
301
302 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
303 if (!(cache_validity & NFS_INO_REVAL_FORCED))
304 cache_validity &= ~(NFS_INO_INVALID_CHANGE
305 | NFS_INO_INVALID_SIZE);
306
307 if (!(cache_validity & NFS_INO_INVALID_SIZE))
308 dst[0] &= ~FATTR4_WORD0_SIZE;
309
310 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
311 dst[0] &= ~FATTR4_WORD0_CHANGE;
312}
313
314static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
315 const __u32 *src, struct inode *inode)
316{
317 nfs4_bitmap_copy_adjust(dst, src, inode);
318}
319
bc4785cd 320static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
1da177e4
LT
321 struct nfs4_readdir_arg *readdir)
322{
18fe6a23 323 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
0dbb4c67 324 __be32 *start, *p;
1da177e4 325
1da177e4 326 if (cookie > 2) {
b7ef1956 327 readdir->cookie = cookie;
1da177e4
LT
328 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
329 return;
330 }
331
332 readdir->cookie = 0;
333 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
334 if (cookie == 2)
335 return;
336
337 /*
338 * NFSv4 servers do not return entries for '.' and '..'
339 * Therefore, we fake these entries here. We let '.'
340 * have cookie 0 and '..' have cookie 1. Note that
341 * when talking to the server, we always send cookie 0
342 * instead of 1 or 2.
343 */
2b86ce2d 344 start = p = kmap_atomic(*readdir->pages);
1da177e4
LT
345
346 if (cookie == 0) {
347 *p++ = xdr_one; /* next */
348 *p++ = xdr_zero; /* cookie, first word */
349 *p++ = xdr_one; /* cookie, second word */
350 *p++ = xdr_one; /* entry len */
351 memcpy(p, ".\0\0\0", 4); /* entry */
352 p++;
353 *p++ = xdr_one; /* bitmap length */
18fe6a23
AS
354 *p++ = htonl(attrs); /* bitmap */
355 *p++ = htonl(12); /* attribute buffer length */
356 *p++ = htonl(NF4DIR);
2b0143b5 357 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
1da177e4
LT
358 }
359
360 *p++ = xdr_one; /* next */
361 *p++ = xdr_zero; /* cookie, first word */
362 *p++ = xdr_two; /* cookie, second word */
363 *p++ = xdr_two; /* entry len */
364 memcpy(p, "..\0\0", 4); /* entry */
365 p++;
366 *p++ = xdr_one; /* bitmap length */
18fe6a23
AS
367 *p++ = htonl(attrs); /* bitmap */
368 *p++ = htonl(12); /* attribute buffer length */
369 *p++ = htonl(NF4DIR);
2b0143b5 370 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
1da177e4
LT
371
372 readdir->pgbase = (char *)p - (char *)start;
373 readdir->count -= readdir->pgbase;
2b86ce2d 374 kunmap_atomic(start);
1da177e4
LT
375}
376
26d36301
TM
377static void nfs4_test_and_free_stateid(struct nfs_server *server,
378 nfs4_stateid *stateid,
a52458b4 379 const struct cred *cred)
26d36301
TM
380{
381 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
382
383 ops->test_and_free_expired(server, stateid, cred);
384}
385
386static void __nfs4_free_revoked_stateid(struct nfs_server *server,
387 nfs4_stateid *stateid,
a52458b4 388 const struct cred *cred)
26d36301
TM
389{
390 stateid->type = NFS4_REVOKED_STATEID_TYPE;
391 nfs4_test_and_free_stateid(server, stateid, cred);
392}
393
394static void nfs4_free_revoked_stateid(struct nfs_server *server,
395 const nfs4_stateid *stateid,
a52458b4 396 const struct cred *cred)
26d36301
TM
397{
398 nfs4_stateid tmp;
399
400 nfs4_stateid_copy(&tmp, stateid);
401 __nfs4_free_revoked_stateid(server, &tmp, cred);
402}
403
8478eaa1
N
404static long nfs4_update_delay(long *timeout)
405{
406 long ret;
407 if (!timeout)
408 return NFS4_POLL_RETRY_MAX;
409 if (*timeout <= 0)
410 *timeout = NFS4_POLL_RETRY_MIN;
411 if (*timeout > NFS4_POLL_RETRY_MAX)
412 *timeout = NFS4_POLL_RETRY_MAX;
413 ret = *timeout;
414 *timeout <<= 1;
415 return ret;
416}
417
0688e64b 418static int nfs4_delay_killable(long *timeout)
65de872e 419{
65de872e
TM
420 might_sleep();
421
8478eaa1
N
422 freezable_schedule_timeout_killable_unsafe(
423 nfs4_update_delay(timeout));
0688e64b
TM
424 if (!__fatal_signal_pending(current))
425 return 0;
426 return -EINTR;
427}
428
429static int nfs4_delay_interruptible(long *timeout)
430{
431 might_sleep();
432
59679d99 433 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
0688e64b
TM
434 if (!signal_pending(current))
435 return 0;
436 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
437}
438
439static int nfs4_delay(long *timeout, bool interruptible)
440{
441 if (interruptible)
442 return nfs4_delay_interruptible(timeout);
443 return nfs4_delay_killable(timeout);
65de872e
TM
444}
445
50c80007
TM
446static const nfs4_stateid *
447nfs4_recoverable_stateid(const nfs4_stateid *stateid)
448{
449 if (!stateid)
450 return NULL;
451 switch (stateid->type) {
452 case NFS4_OPEN_STATEID_TYPE:
453 case NFS4_LOCK_STATEID_TYPE:
454 case NFS4_DELEGATION_STATEID_TYPE:
455 return stateid;
456 default:
457 break;
458 }
459 return NULL;
460}
461
65de872e
TM
462/* This is the error handling routine for processes that are allowed
463 * to sleep.
464 */
b3c2aa07
TM
465static int nfs4_do_handle_exception(struct nfs_server *server,
466 int errorcode, struct nfs4_exception *exception)
65de872e
TM
467{
468 struct nfs_client *clp = server->nfs_client;
9e33bed5 469 struct nfs4_state *state = exception->state;
50c80007 470 const nfs4_stateid *stateid;
3114ea7a 471 struct inode *inode = exception->inode;
65de872e
TM
472 int ret = errorcode;
473
b3c2aa07
TM
474 exception->delay = 0;
475 exception->recovering = 0;
65de872e 476 exception->retry = 0;
272289a3 477
50c80007 478 stateid = nfs4_recoverable_stateid(exception->stateid);
272289a3 479 if (stateid == NULL && state != NULL)
50c80007 480 stateid = nfs4_recoverable_stateid(&state->stateid);
272289a3 481
65de872e
TM
482 switch(errorcode) {
483 case 0:
484 return 0;
cf61eb26
TM
485 case -NFS4ERR_BADHANDLE:
486 case -ESTALE:
487 if (inode != NULL && S_ISREG(inode->i_mode))
488 pnfs_destroy_layout(NFS_I(inode));
489 break;
5ba12443
TM
490 case -NFS4ERR_DELEG_REVOKED:
491 case -NFS4ERR_ADMIN_REVOKED:
272289a3 492 case -NFS4ERR_EXPIRED:
5ba12443 493 case -NFS4ERR_BAD_STATEID:
fefa1a81 494 case -NFS4ERR_PARTNER_NO_AUTH:
272289a3
TM
495 if (inode != NULL && stateid != NULL) {
496 nfs_inode_find_state_and_recover(inode,
497 stateid);
498 goto wait_on_recovery;
499 }
df561f66 500 fallthrough;
272289a3 501 case -NFS4ERR_OPENMODE:
8487c479
TM
502 if (inode) {
503 int err;
504
505 err = nfs_async_inode_return_delegation(inode,
506 stateid);
507 if (err == 0)
508 goto wait_on_recovery;
509 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
510 exception->retry = 1;
511 break;
512 }
513 }
3114ea7a
TM
514 if (state == NULL)
515 break;
5d422301
TM
516 ret = nfs4_schedule_stateid_recovery(server, state);
517 if (ret < 0)
518 break;
3114ea7a 519 goto wait_on_recovery;
65de872e 520 case -NFS4ERR_STALE_STATEID:
a2c0b9e2 521 case -NFS4ERR_STALE_CLIENTID:
0400a6b0
TM
522 nfs4_schedule_lease_recovery(clp);
523 goto wait_on_recovery;
519ae255
CL
524 case -NFS4ERR_MOVED:
525 ret = nfs4_schedule_migration_recovery(server);
526 if (ret < 0)
527 break;
528 goto wait_on_recovery;
8ef2f8d4
CL
529 case -NFS4ERR_LEASE_MOVED:
530 nfs4_schedule_lease_moved_recovery(clp);
531 goto wait_on_recovery;
03391693 532#if defined(CONFIG_NFS_V4_1)
4745e315
AA
533 case -NFS4ERR_BADSESSION:
534 case -NFS4ERR_BADSLOT:
535 case -NFS4ERR_BAD_HIGH_SLOT:
536 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
537 case -NFS4ERR_DEADSESSION:
538 case -NFS4ERR_SEQ_FALSE_RETRY:
539 case -NFS4ERR_SEQ_MISORDERED:
5c441544 540 /* Handled in nfs41_sequence_process() */
399f11c3 541 goto wait_on_recovery;
03391693 542#endif /* defined(CONFIG_NFS_V4_1) */
65de872e 543 case -NFS4ERR_FILE_OPEN:
44ed3556
N
544 if (exception->timeout > HZ) {
545 /* We have retried a decent amount, time to
546 * fail
547 */
548 ret = -EBUSY;
549 break;
550 }
df561f66 551 fallthrough;
65de872e 552 case -NFS4ERR_DELAY:
2598ed34 553 nfs_inc_server_stats(server, NFSIOS_DELAY);
df561f66 554 fallthrough;
2598ed34 555 case -NFS4ERR_GRACE:
e85d7ee4 556 case -NFS4ERR_LAYOUTTRYLATER:
183d9e7b 557 case -NFS4ERR_RECALLCONFLICT:
b3c2aa07
TM
558 exception->delay = 1;
559 return 0;
560
a8a4ae3a 561 case -NFS4ERR_RETRY_UNCACHED_REP:
65de872e
TM
562 case -NFS4ERR_OLD_STATEID:
563 exception->retry = 1;
b064eca2
TM
564 break;
565 case -NFS4ERR_BADOWNER:
566 /* The following works around a Linux server bug! */
567 case -NFS4ERR_BADNAME:
568 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
569 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
570 exception->retry = 1;
571 printk(KERN_WARNING "NFS: v4 server %s "
572 "does not accept raw "
573 "uid/gids. "
574 "Reenabling the idmapper.\n",
575 server->nfs_client->cl_hostname);
576 }
65de872e
TM
577 }
578 /* We failed to handle the error */
579 return nfs4_map_errors(ret);
0400a6b0 580wait_on_recovery:
b3c2aa07
TM
581 exception->recovering = 1;
582 return 0;
583}
584
585/* This is the error handling routine for processes that are allowed
586 * to sleep.
587 */
588int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
589{
590 struct nfs_client *clp = server->nfs_client;
591 int ret;
592
593 ret = nfs4_do_handle_exception(server, errorcode, exception);
594 if (exception->delay) {
0688e64b
TM
595 ret = nfs4_delay(&exception->timeout,
596 exception->interruptible);
b3c2aa07
TM
597 goto out_retry;
598 }
599 if (exception->recovering) {
600 ret = nfs4_wait_clnt_recover(clp);
601 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
602 return -EIO;
603 goto out_retry;
604 }
605 return ret;
606out_retry:
a2c0b9e2
TM
607 if (ret == 0)
608 exception->retry = 1;
609 return ret;
65de872e
TM
610}
611
037fc980
TM
612static int
613nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
614 int errorcode, struct nfs4_exception *exception)
615{
616 struct nfs_client *clp = server->nfs_client;
617 int ret;
618
619 ret = nfs4_do_handle_exception(server, errorcode, exception);
620 if (exception->delay) {
621 rpc_delay(task, nfs4_update_delay(&exception->timeout));
622 goto out_retry;
623 }
624 if (exception->recovering) {
625 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
626 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
627 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
628 goto out_retry;
629 }
519ae255 630 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
037fc980
TM
631 ret = -EIO;
632 return ret;
633out_retry:
0f90be13 634 if (ret == 0) {
a2c0b9e2 635 exception->retry = 1;
0f90be13
BB
636 /*
637 * For NFS4ERR_MOVED, the client transport will need to
638 * be recomputed after migration recovery has completed.
639 */
640 if (errorcode == -NFS4ERR_MOVED)
641 rpc_task_release_transport(task);
642 }
a2c0b9e2 643 return ret;
65de872e
TM
644}
645
0f913a57 646int
037fc980
TM
647nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
648 struct nfs4_state *state, long *timeout)
649{
650 struct nfs4_exception exception = {
651 .state = state,
652 };
653
654 if (task->tk_status >= 0)
655 return 0;
656 if (timeout)
657 exception.timeout = *timeout;
658 task->tk_status = nfs4_async_handle_exception(task, server,
659 task->tk_status,
660 &exception);
661 if (exception.delay && timeout)
662 *timeout = exception.timeout;
663 if (exception.retry)
664 return -EAGAIN;
665 return 0;
666}
667
a5250def
WAA
668/*
669 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
670 * or 'false' otherwise.
671 */
672static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
673{
674 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
eeea5361 675 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
a5250def 676}
65de872e 677
452e9352 678static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
1da177e4 679{
1da177e4
LT
680 spin_lock(&clp->cl_lock);
681 if (time_before(clp->cl_last_renewal,timestamp))
682 clp->cl_last_renewal = timestamp;
683 spin_unlock(&clp->cl_lock);
684}
685
452e9352
TM
686static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
687{
be824167
TM
688 struct nfs_client *clp = server->nfs_client;
689
690 if (!nfs4_has_session(clp))
691 do_renew_lease(clp, timestamp);
452e9352
TM
692}
693
2a3eb2b9
CL
694struct nfs4_call_sync_data {
695 const struct nfs_server *seq_server;
696 struct nfs4_sequence_args *seq_args;
697 struct nfs4_sequence_res *seq_res;
698};
699
be3a5d23 700void nfs4_init_sequence(struct nfs4_sequence_args *args,
fba83f34
AS
701 struct nfs4_sequence_res *res, int cache_reply,
702 int privileged)
a9c92d6b
CL
703{
704 args->sa_slot = NULL;
705 args->sa_cache_this = cache_reply;
fba83f34 706 args->sa_privileged = privileged;
a9c92d6b
CL
707
708 res->sr_slot = NULL;
709}
710
2e80dbe7 711static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
3bd2384a
CL
712{
713 struct nfs4_slot *slot = res->sr_slot;
714 struct nfs4_slot_table *tbl;
715
3bd2384a
CL
716 tbl = slot->table;
717 spin_lock(&tbl->slot_tbl_lock);
718 if (!nfs41_wake_and_assign_slot(tbl, slot))
719 nfs4_free_slot(tbl, slot);
720 spin_unlock(&tbl->slot_tbl_lock);
721
722 res->sr_slot = NULL;
2e80dbe7
TM
723}
724
725static int nfs40_sequence_done(struct rpc_task *task,
726 struct nfs4_sequence_res *res)
727{
728 if (res->sr_slot != NULL)
729 nfs40_sequence_free_slot(res);
3bd2384a
CL
730 return 1;
731}
732
cccef3b9
AA
733#if defined(CONFIG_NFS_V4_1)
734
3be0f80b 735static void nfs41_release_slot(struct nfs4_slot *slot)
13615871 736{
e3725ec0 737 struct nfs4_session *session;
13615871 738 struct nfs4_slot_table *tbl;
c10e4498 739 bool send_new_highest_used_slotid = false;
13615871 740
3be0f80b
TM
741 if (!slot)
742 return;
a13ce7c6 743 tbl = slot->table;
e3725ec0 744 session = tbl->session;
ea028ac9 745
07e8dcbd
TM
746 /* Bump the slot sequence number */
747 if (slot->seq_done)
748 slot->seq_nr++;
749 slot->seq_done = 0;
750
35dc1d74 751 spin_lock(&tbl->slot_tbl_lock);
c10e4498
TM
752 /* Be nice to the server: try to ensure that the last transmitted
753 * value for highest_user_slotid <= target_highest_slotid
754 */
755 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
756 send_new_highest_used_slotid = true;
757
a13ce7c6 758 if (nfs41_wake_and_assign_slot(tbl, slot)) {
b75ad4cd
TM
759 send_new_highest_used_slotid = false;
760 goto out_unlock;
761 }
a13ce7c6 762 nfs4_free_slot(tbl, slot);
c10e4498
TM
763
764 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
765 send_new_highest_used_slotid = false;
b75ad4cd 766out_unlock:
35dc1d74 767 spin_unlock(&tbl->slot_tbl_lock);
c10e4498 768 if (send_new_highest_used_slotid)
3f10a6af 769 nfs41_notify_server(session->clp);
045d2a6d
TM
770 if (waitqueue_active(&tbl->slot_waitq))
771 wake_up_all(&tbl->slot_waitq);
13615871
AA
772}
773
3be0f80b
TM
774static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
775{
776 nfs41_release_slot(res->sr_slot);
777 res->sr_slot = NULL;
778}
779
3453d570
TM
780static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
781 u32 seqnr)
782{
783 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
784 slot->seq_nr_highest_sent = seqnr;
785}
786static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
787 u32 seqnr)
788{
789 slot->seq_nr_highest_sent = seqnr;
790 slot->seq_nr_last_acked = seqnr;
791}
792
913fadc5
AS
793static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
794 struct nfs4_slot *slot)
795{
796 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
797 if (!IS_ERR(task))
798 rpc_put_task_async(task);
799}
800
2e80dbe7
TM
801static int nfs41_sequence_process(struct rpc_task *task,
802 struct nfs4_sequence_res *res)
b0df806c 803{
e3725ec0 804 struct nfs4_session *session;
a13ce7c6 805 struct nfs4_slot *slot = res->sr_slot;
14516c3a 806 struct nfs_client *clp;
5c441544 807 int status;
85563073 808 int ret = 1;
b0df806c 809
a13ce7c6
TM
810 if (slot == NULL)
811 goto out_noaction;
468f8613 812 /* don't increment the sequence number if the task wasn't sent */
c71c46f0 813 if (!RPC_WAS_SENT(task) || slot->seq_done)
b0df806c
AA
814 goto out;
815
e3725ec0 816 session = slot->table->session;
913fadc5 817 clp = session->clp;
933602e3 818
2f92ae34 819 trace_nfs4_sequence_done(session, res);
5c441544
TM
820
821 status = res->sr_status;
822 if (task->tk_status == -NFS4ERR_DEADSESSION)
823 status = -NFS4ERR_DEADSESSION;
824
691daf3b 825 /* Check the SEQUENCE operation status */
5c441544 826 switch (status) {
14516c3a 827 case 0:
3453d570
TM
828 /* Mark this sequence number as having been acked */
829 nfs4_slot_sequence_acked(slot, slot->seq_nr);
b0df806c 830 /* Update the slot's sequence and clientid lease timer */
07e8dcbd 831 slot->seq_done = 1;
8e63b6a8 832 do_renew_lease(clp, res->sr_timestamp);
0629e370 833 /* Check sequence flags */
0a014a44
TM
834 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
835 !!slot->privileged);
464ee9f9 836 nfs41_update_target_slotid(slot->table, slot, res);
14516c3a 837 break;
ac20d163
TM
838 case 1:
839 /*
840 * sr_status remains 1 if an RPC level error occurred.
841 * The server may or may not have processed the sequence
842 * operation..
ac20d163 843 */
3453d570
TM
844 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
845 slot->seq_done = 1;
ac20d163 846 goto out;
14516c3a
TM
847 case -NFS4ERR_DELAY:
848 /* The server detected a resend of the RPC call and
849 * returned NFS4ERR_DELAY as per Section 2.10.6.2
850 * of RFC5661.
851 */
df2fabff 852 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
dfb4f309 853 __func__,
df2fabff 854 slot->slot_nr,
933602e3 855 slot->seq_nr);
3453d570 856 nfs4_slot_sequence_acked(slot, slot->seq_nr);
14516c3a 857 goto out_retry;
f9312a54
TM
858 case -NFS4ERR_RETRY_UNCACHED_REP:
859 case -NFS4ERR_SEQ_FALSE_RETRY:
860 /*
861 * The server thinks we tried to replay a request.
862 * Retry the call after bumping the sequence ID.
863 */
3453d570 864 nfs4_slot_sequence_acked(slot, slot->seq_nr);
f9312a54 865 goto retry_new_seq;
85563073
TM
866 case -NFS4ERR_BADSLOT:
867 /*
868 * The slot id we used was probably retired. Try again
869 * using a different slot id.
870 */
99589100 871 if (slot->slot_nr < slot->table->target_highest_slotid)
3be0f80b 872 goto session_recover;
e8794440
TM
873 goto retry_nowait;
874 case -NFS4ERR_SEQ_MISORDERED:
3453d570 875 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
ac20d163 876 /*
3453d570
TM
877 * Were one or more calls using this slot interrupted?
878 * If the server never received the request, then our
913fadc5
AS
879 * transmitted slot sequence number may be too high. However,
880 * if the server did receive the request then it might
881 * accidentally give us a reply with a mismatched operation.
882 * We can sort this out by sending a lone sequence operation
883 * to the server on the same slot.
e8794440 884 */
3453d570
TM
885 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
886 slot->seq_nr--;
913fadc5
AS
887 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
888 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
889 res->sr_slot = NULL;
890 }
8e63b6a8
TM
891 goto retry_nowait;
892 }
3453d570
TM
893 /*
894 * RFC5661:
895 * A retry might be sent while the original request is
896 * still in progress on the replier. The replier SHOULD
897 * deal with the issue by returning NFS4ERR_DELAY as the
898 * reply to SEQUENCE or CB_SEQUENCE operation, but
899 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
900 *
901 * Restart the search after a delay.
902 */
903 slot->seq_nr = slot->seq_nr_highest_sent;
904 goto out_retry;
5c441544
TM
905 case -NFS4ERR_BADSESSION:
906 case -NFS4ERR_DEADSESSION:
907 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
908 goto session_recover;
14516c3a
TM
909 default:
910 /* Just update the slot sequence no. */
07e8dcbd 911 slot->seq_done = 1;
b0df806c
AA
912 }
913out:
914 /* The session may be reset by one of the error handlers. */
915 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
a13ce7c6 916out_noaction:
85563073 917 return ret;
3be0f80b 918session_recover:
5c441544
TM
919 nfs4_schedule_session_recovery(session, status);
920 dprintk("%s ERROR: %d Reset session\n", __func__, status);
921 nfs41_sequence_free_slot(res);
922 goto out;
3be0f80b
TM
923retry_new_seq:
924 ++slot->seq_nr;
e8794440
TM
925retry_nowait:
926 if (rpc_restart_call_prepare(task)) {
2e80dbe7 927 nfs41_sequence_free_slot(res);
e8794440
TM
928 task->tk_status = 0;
929 ret = 0;
930 }
931 goto out;
14516c3a 932out_retry:
d05dd4e9 933 if (!rpc_restart_call(task))
14516c3a
TM
934 goto out;
935 rpc_delay(task, NFS4_POLL_RETRY_MAX);
936 return 0;
b0df806c 937}
2e80dbe7
TM
938
939int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
940{
941 if (!nfs41_sequence_process(task, res))
942 return 0;
943 if (res->sr_slot != NULL)
944 nfs41_sequence_free_slot(res);
945 return 1;
946
947}
f9c96fcc 948EXPORT_SYMBOL_GPL(nfs41_sequence_done);
b0df806c 949
2e80dbe7
TM
950static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
951{
952 if (res->sr_slot == NULL)
953 return 1;
954 if (res->sr_slot->table->session != NULL)
955 return nfs41_sequence_process(task, res);
956 return nfs40_sequence_done(task, res);
957}
958
959static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
960{
961 if (res->sr_slot != NULL) {
962 if (res->sr_slot->table->session != NULL)
963 nfs41_sequence_free_slot(res);
964 else
965 nfs40_sequence_free_slot(res);
966 }
967}
968
2c4b131d 969int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
df896455 970{
e3725ec0 971 if (res->sr_slot == NULL)
14516c3a 972 return 1;
3bd2384a
CL
973 if (!res->sr_slot->table->session)
974 return nfs40_sequence_done(task, res);
14516c3a 975 return nfs41_sequence_done(task, res);
df896455 976}
2c4b131d 977EXPORT_SYMBOL_GPL(nfs4_sequence_done);
df896455 978
ce5039c1
AA
979static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
980{
2a3eb2b9 981 struct nfs4_call_sync_data *data = calldata;
ce5039c1 982
035168ab
TM
983 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
984
7981c8a6
AS
985 nfs4_setup_sequence(data->seq_server->nfs_client,
986 data->seq_args, data->seq_res, task);
ce5039c1
AA
987}
988
69ab40c4
AA
989static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
990{
2a3eb2b9 991 struct nfs4_call_sync_data *data = calldata;
69ab40c4 992
14516c3a 993 nfs41_sequence_done(task, data->seq_res);
69ab40c4
AA
994}
995
17280175 996static const struct rpc_call_ops nfs41_call_sync_ops = {
ce5039c1 997 .rpc_call_prepare = nfs41_call_sync_prepare,
69ab40c4 998 .rpc_call_done = nfs41_call_sync_done,
ce5039c1
AA
999};
1000
3bd2384a
CL
1001#else /* !CONFIG_NFS_V4_1 */
1002
2e80dbe7
TM
1003static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1004{
1005 return nfs40_sequence_done(task, res);
1006}
1007
1008static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1009{
1010 if (res->sr_slot != NULL)
1011 nfs40_sequence_free_slot(res);
1012}
1013
2c4b131d
PT
1014int nfs4_sequence_done(struct rpc_task *task,
1015 struct nfs4_sequence_res *res)
9915ea7e 1016{
3bd2384a 1017 return nfs40_sequence_done(task, res);
9915ea7e 1018}
2c4b131d 1019EXPORT_SYMBOL_GPL(nfs4_sequence_done);
3bd2384a
CL
1020
1021#endif /* !CONFIG_NFS_V4_1 */
9915ea7e 1022
c1dffe0b
TM
1023static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1024{
1025 res->sr_timestamp = jiffies;
1026 res->sr_status_flags = 0;
1027 res->sr_status = 1;
1028}
1029
3be0f80b
TM
1030static
1031void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1032 struct nfs4_sequence_res *res,
1033 struct nfs4_slot *slot)
1034{
1035 if (!slot)
1036 return;
1037 slot->privileged = args->sa_privileged ? 1 : 0;
1038 args->sa_slot = slot;
1039
1040 res->sr_slot = slot;
3be0f80b
TM
1041}
1042
1043int nfs4_setup_sequence(struct nfs_client *client,
7981c8a6
AS
1044 struct nfs4_sequence_args *args,
1045 struct nfs4_sequence_res *res,
1046 struct rpc_task *task)
1047{
7981c8a6 1048 struct nfs4_session *session = nfs4_get_session(client);
76ee0354 1049 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
3d35808b 1050 struct nfs4_slot *slot;
7981c8a6 1051
9dd9107f
AS
1052 /* slot already allocated? */
1053 if (res->sr_slot != NULL)
1054 goto out_start;
1055
6b2e6856 1056 if (session)
76ee0354 1057 tbl = &session->fc_slot_table;
76ee0354 1058
3453d570
TM
1059 spin_lock(&tbl->slot_tbl_lock);
1060 /* The state manager will wait until the slot table is empty */
1061 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1062 goto out_sleep;
1063
1064 slot = nfs4_alloc_slot(tbl);
1065 if (IS_ERR(slot)) {
3453d570 1066 if (slot == ERR_PTR(-ENOMEM))
6b2e6856 1067 goto out_sleep_timeout;
3453d570 1068 goto out_sleep;
3d35808b 1069 }
3453d570 1070 spin_unlock(&tbl->slot_tbl_lock);
3d35808b 1071
3be0f80b 1072 nfs4_sequence_attach_slot(args, res, slot);
3d35808b 1073
ad05cc0f 1074 trace_nfs4_setup_sequence(session, args);
9dd9107f 1075out_start:
c1dffe0b 1076 nfs41_sequence_res_init(res);
9dd9107f
AS
1077 rpc_call_start(task);
1078 return 0;
6b2e6856
TM
1079out_sleep_timeout:
1080 /* Try again in 1/4 second */
1081 if (args->sa_privileged)
1082 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1083 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1084 else
1085 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1086 NULL, jiffies + (HZ >> 2));
1087 spin_unlock(&tbl->slot_tbl_lock);
1088 return -EAGAIN;
0dcee8bb
AS
1089out_sleep:
1090 if (args->sa_privileged)
1091 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
8357a9b6 1092 RPC_PRIORITY_PRIVILEGED);
0dcee8bb
AS
1093 else
1094 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1095 spin_unlock(&tbl->slot_tbl_lock);
1096 return -EAGAIN;
7981c8a6
AS
1097}
1098EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1099
9915ea7e
CL
1100static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1101{
1102 struct nfs4_call_sync_data *data = calldata;
42e1cca7 1103 nfs4_setup_sequence(data->seq_server->nfs_client,
9915ea7e
CL
1104 data->seq_args, data->seq_res, task);
1105}
1106
1107static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1108{
1109 struct nfs4_call_sync_data *data = calldata;
1110 nfs4_sequence_done(task, data->seq_res);
1111}
1112
1113static const struct rpc_call_ops nfs40_call_sync_ops = {
1114 .rpc_call_prepare = nfs40_call_sync_prepare,
1115 .rpc_call_done = nfs40_call_sync_done,
1116};
1117
48c05854
AS
1118static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1119{
1120 int ret;
1121 struct rpc_task *task;
1122
1123 task = rpc_run_task(task_setup);
1124 if (IS_ERR(task))
1125 return PTR_ERR(task);
1126
1127 ret = task->tk_status;
1128 rpc_put_task(task);
1129 return ret;
1130}
1131
c74dfe97
TM
1132static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1133 struct nfs_server *server,
1134 struct rpc_message *msg,
1135 struct nfs4_sequence_args *args,
1136 struct nfs4_sequence_res *res,
1137 unsigned short task_flags)
ce5039c1 1138{
9915ea7e 1139 struct nfs_client *clp = server->nfs_client;
2a3eb2b9 1140 struct nfs4_call_sync_data data = {
035168ab 1141 .seq_server = server,
ce5039c1
AA
1142 .seq_args = args,
1143 .seq_res = res,
ce5039c1
AA
1144 };
1145 struct rpc_task_setup task_setup = {
7c513058 1146 .rpc_client = clnt,
ce5039c1 1147 .rpc_message = msg,
9915ea7e 1148 .callback_ops = clp->cl_mvops->call_sync_ops,
c74dfe97
TM
1149 .callback_data = &data,
1150 .flags = task_flags,
ce5039c1
AA
1151 };
1152
48c05854 1153 return nfs4_call_sync_custom(&task_setup);
ce5039c1
AA
1154}
1155
c74dfe97
TM
1156static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1157 struct nfs_server *server,
1158 struct rpc_message *msg,
1159 struct nfs4_sequence_args *args,
1160 struct nfs4_sequence_res *res)
1161{
1162 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1163}
1164
1165
7c513058
BS
1166int nfs4_call_sync(struct rpc_clnt *clnt,
1167 struct nfs_server *server,
e73b83f2
BS
1168 struct rpc_message *msg,
1169 struct nfs4_sequence_args *args,
1170 struct nfs4_sequence_res *res,
1171 int cache_reply)
1172{
fba83f34 1173 nfs4_init_sequence(args, res, cache_reply, 0);
9915ea7e 1174 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
e73b83f2 1175}
cccef3b9 1176
3c591175
TM
1177static void
1178nfs4_inc_nlink_locked(struct inode *inode)
1179{
1180 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1181 inc_nlink(inode);
1182}
1183
1184static void
1185nfs4_dec_nlink_locked(struct inode *inode)
1186{
1187 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1188 drop_nlink(inode);
1189}
1190
1191static void
1b523ca9
FL
1192nfs4_update_changeattr_locked(struct inode *inode,
1193 struct nfs4_change_info *cinfo,
5636ec4e 1194 unsigned long timestamp, unsigned long cache_validity)
1da177e4 1195{
1b523ca9 1196 struct nfs_inode *nfsi = NFS_I(inode);
1da177e4 1197
16e14375
TM
1198 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1199 | NFS_INO_INVALID_MTIME
5636ec4e 1200 | cache_validity;
1b523ca9
FL
1201
1202 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(inode)) {
e603a4c1
TM
1203 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1204 nfsi->attrtimeo_timestamp = jiffies;
1205 } else {
1b523ca9
FL
1206 if (S_ISDIR(inode->i_mode)) {
1207 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
1208 nfs_force_lookup_revalidate(inode);
1209 } else {
1210 if (!NFS_PROTO(inode)->have_delegation(inode,
1211 FMODE_READ))
1212 nfsi->cache_validity |= NFS_INO_REVAL_PAGECACHE;
1213 }
1214
1215 if (cinfo->before != inode_peek_iversion_raw(inode))
e603a4c1 1216 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
0f44da51
FL
1217 NFS_INO_INVALID_ACL |
1218 NFS_INO_INVALID_XATTR;
e603a4c1 1219 }
1b523ca9 1220 inode_set_iversion_raw(inode, cinfo->after);
d3129ef6 1221 nfsi->read_cache_jiffies = timestamp;
3235b403 1222 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
c8d07159 1223 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1b523ca9
FL
1224
1225 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1226 nfs_fscache_invalidate(inode);
3c591175
TM
1227}
1228
1b523ca9
FL
1229void
1230nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
5636ec4e 1231 unsigned long timestamp, unsigned long cache_validity)
3c591175
TM
1232{
1233 spin_lock(&dir->i_lock);
1b523ca9 1234 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
38478b24 1235 spin_unlock(&dir->i_lock);
1da177e4
LT
1236}
1237
8fd1ab74
TM
1238struct nfs4_open_createattrs {
1239 struct nfs4_label *label;
1240 struct iattr *sattr;
1241 const __u32 verf[2];
1242};
1243
49f9a0fa
TM
1244static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1245 int err, struct nfs4_exception *exception)
1246{
1247 if (err != -EINVAL)
1248 return false;
1249 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1250 return false;
1251 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1252 exception->retry = 1;
1253 return true;
1254}
1255
1bf85d8c
TM
1256static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1257{
1258 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1259}
1260
1261static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1262{
1263 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1264
1265 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1266}
1267
6ae37339
TM
1268static u32
1269nfs4_map_atomic_open_share(struct nfs_server *server,
1270 fmode_t fmode, int openflags)
1271{
1272 u32 res = 0;
1273
1274 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1275 case FMODE_READ:
1276 res = NFS4_SHARE_ACCESS_READ;
1277 break;
1278 case FMODE_WRITE:
1279 res = NFS4_SHARE_ACCESS_WRITE;
1280 break;
1281 case FMODE_READ|FMODE_WRITE:
1282 res = NFS4_SHARE_ACCESS_BOTH;
1283 }
1284 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1285 goto out;
1286 /* Want no delegation if we're using O_DIRECT */
1287 if (openflags & O_DIRECT)
1288 res |= NFS4_SHARE_WANT_NO_DELEG;
1289out:
1290 return res;
1291}
1292
49f9a0fa
TM
1293static enum open_claim_type4
1294nfs4_map_atomic_open_claim(struct nfs_server *server,
1295 enum open_claim_type4 claim)
1296{
1297 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1298 return claim;
1299 switch (claim) {
1300 default:
1301 return claim;
1302 case NFS4_OPEN_CLAIM_FH:
1303 return NFS4_OPEN_CLAIM_NULL;
1304 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1305 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1306 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1307 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1308 }
1309}
2ced46c2
TM
1310
1311static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1312{
1313 p->o_res.f_attr = &p->f_attr;
1775fd3e 1314 p->o_res.f_label = p->f_label;
c1d51931
TM
1315 p->o_res.seqid = p->o_arg.seqid;
1316 p->c_res.seqid = p->c_arg.seqid;
2ced46c2 1317 p->o_res.server = p->o_arg.server;
5f657530 1318 p->o_res.access_request = p->o_arg.access;
2ced46c2 1319 nfs_fattr_init(&p->f_attr);
6926afd1 1320 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
2ced46c2
TM
1321}
1322
82a2c1b7 1323static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
dc0b027d 1324 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
8fd1ab74 1325 const struct nfs4_open_createattrs *c,
4a1c0893 1326 enum open_claim_type4 claim,
8535b2be 1327 gfp_t gfp_mask)
e56e0b78 1328{
82a2c1b7 1329 struct dentry *parent = dget_parent(dentry);
2b0143b5 1330 struct inode *dir = d_inode(parent);
e56e0b78 1331 struct nfs_server *server = NFS_SERVER(dir);
63f5f796 1332 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
8fd1ab74 1333 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
e56e0b78
TM
1334 struct nfs4_opendata *p;
1335
8535b2be 1336 p = kzalloc(sizeof(*p), gfp_mask);
e56e0b78
TM
1337 if (p == NULL)
1338 goto err;
14c43f76
DQ
1339
1340 p->f_label = nfs4_label_alloc(server, gfp_mask);
1341 if (IS_ERR(p->f_label))
1342 goto err_free_p;
1343
a49c2691
KM
1344 p->a_label = nfs4_label_alloc(server, gfp_mask);
1345 if (IS_ERR(p->a_label))
1346 goto err_free_f;
1347
63f5f796
TM
1348 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1349 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
badc76dd 1350 if (IS_ERR(p->o_arg.seqid))
14c43f76 1351 goto err_free_label;
82a2c1b7
AV
1352 nfs_sb_active(dentry->d_sb);
1353 p->dentry = dget(dentry);
e56e0b78
TM
1354 p->dir = parent;
1355 p->owner = sp;
1356 atomic_inc(&sp->so_count);
dc0b027d
TM
1357 p->o_arg.open_flags = flags;
1358 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
536585cc 1359 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
6ae37339
TM
1360 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1361 fmode, flags);
90910519
BC
1362 if (flags & O_CREAT) {
1363 p->o_arg.umask = current_umask();
1364 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1365 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1366 p->o_arg.u.attrs = &p->attrs;
1367 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1368
1369 memcpy(p->o_arg.u.verifier.data, c->verf,
1370 sizeof(p->o_arg.u.verifier.data));
1371 }
1372 }
ae2bb032
WAA
1373 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1374 * will return permission denied for all bits until close */
1375 if (!(flags & O_EXCL)) {
1376 /* ask server to check for all possible rights as results
1377 * are cached */
536585cc
TM
1378 switch (p->o_arg.claim) {
1379 default:
1380 break;
1381 case NFS4_OPEN_CLAIM_NULL:
1382 case NFS4_OPEN_CLAIM_FH:
1383 p->o_arg.access = NFS4_ACCESS_READ |
1384 NFS4_ACCESS_MODIFY |
1385 NFS4_ACCESS_EXTEND |
1386 NFS4_ACCESS_EXECUTE;
72832a24
FL
1387#ifdef CONFIG_NFS_V4_2
1388 if (server->caps & NFS_CAP_XATTR)
1389 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1390 NFS4_ACCESS_XAWRITE |
1391 NFS4_ACCESS_XALIST;
1392#endif
536585cc 1393 }
ae2bb032 1394 }
7539bbab 1395 p->o_arg.clientid = server->nfs_client->cl_clientid;
95b72eb0
TM
1396 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1397 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
82a2c1b7 1398 p->o_arg.name = &dentry->d_name;
e56e0b78 1399 p->o_arg.server = server;
aa9c2669 1400 p->o_arg.bitmask = nfs4_bitmask(server, label);
1549210f 1401 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
49f9a0fa 1402 switch (p->o_arg.claim) {
4a1c0893
TM
1403 case NFS4_OPEN_CLAIM_NULL:
1404 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1405 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1406 p->o_arg.fh = NFS_FH(dir);
1407 break;
1408 case NFS4_OPEN_CLAIM_PREVIOUS:
1409 case NFS4_OPEN_CLAIM_FH:
1410 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1411 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2b0143b5 1412 p->o_arg.fh = NFS_FH(d_inode(dentry));
4a1c0893 1413 }
cdd4e68b
TM
1414 p->c_arg.fh = &p->o_res.fh;
1415 p->c_arg.stateid = &p->o_res.stateid;
1416 p->c_arg.seqid = p->o_arg.seqid;
2ced46c2 1417 nfs4_init_opendata_res(p);
c6d00e63 1418 kref_init(&p->kref);
e56e0b78 1419 return p;
14c43f76
DQ
1420
1421err_free_label:
a49c2691
KM
1422 nfs4_label_free(p->a_label);
1423err_free_f:
14c43f76
DQ
1424 nfs4_label_free(p->f_label);
1425err_free_p:
e56e0b78
TM
1426 kfree(p);
1427err:
1428 dput(parent);
1429 return NULL;
1430}
1431
c6d00e63 1432static void nfs4_opendata_free(struct kref *kref)
e56e0b78 1433{
c6d00e63
TM
1434 struct nfs4_opendata *p = container_of(kref,
1435 struct nfs4_opendata, kref);
82a2c1b7 1436 struct super_block *sb = p->dentry->d_sb;
c6d00e63 1437
30ae2412 1438 nfs4_lgopen_release(p->lgp);
c6d00e63 1439 nfs_free_seqid(p->o_arg.seqid);
2e80dbe7 1440 nfs4_sequence_free_slot(&p->o_res.seq_res);
aac00a8d
TM
1441 if (p->state != NULL)
1442 nfs4_put_open_state(p->state);
c6d00e63 1443 nfs4_put_state_owner(p->owner);
14c43f76 1444
a49c2691 1445 nfs4_label_free(p->a_label);
14c43f76
DQ
1446 nfs4_label_free(p->f_label);
1447
c6d00e63 1448 dput(p->dir);
82a2c1b7
AV
1449 dput(p->dentry);
1450 nfs_sb_deactive(sb);
6926afd1 1451 nfs_fattr_free_names(&p->f_attr);
e911b815 1452 kfree(p->f_attr.mdsthreshold);
c6d00e63
TM
1453 kfree(p);
1454}
1455
1456static void nfs4_opendata_put(struct nfs4_opendata *p)
1457{
1458 if (p != NULL)
1459 kref_put(&p->kref, nfs4_opendata_free);
e56e0b78
TM
1460}
1461
24311f88
TM
1462static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1463 fmode_t fmode)
1464{
1465 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1466 case FMODE_READ|FMODE_WRITE:
1467 return state->n_rdwr != 0;
1468 case FMODE_WRITE:
1469 return state->n_wronly != 0;
1470 case FMODE_READ:
1471 return state->n_rdonly != 0;
1472 }
1473 WARN_ON_ONCE(1);
1474 return false;
1475}
1476
be189f7e
TM
1477static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1478 int open_mode, enum open_claim_type4 claim)
6ee41268
TM
1479{
1480 int ret = 0;
dc0b027d 1481
536e43d1 1482 if (open_mode & (O_EXCL|O_TRUNC))
dc0b027d 1483 goto out;
be189f7e
TM
1484 switch (claim) {
1485 case NFS4_OPEN_CLAIM_NULL:
1486 case NFS4_OPEN_CLAIM_FH:
1487 goto out;
1488 default:
1489 break;
1490 }
dc0b027d 1491 switch (mode & (FMODE_READ|FMODE_WRITE)) {
6ee41268 1492 case FMODE_READ:
88069f77
TM
1493 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1494 && state->n_rdonly != 0;
6ee41268
TM
1495 break;
1496 case FMODE_WRITE:
88069f77
TM
1497 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1498 && state->n_wronly != 0;
6ee41268
TM
1499 break;
1500 case FMODE_READ|FMODE_WRITE:
88069f77
TM
1501 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1502 && state->n_rdwr != 0;
6ee41268 1503 }
dc0b027d 1504out:
6ee41268
TM
1505 return ret;
1506}
1507
2a606188
TM
1508static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1509 enum open_claim_type4 claim)
aac00a8d 1510{
652f89f6
TM
1511 if (delegation == NULL)
1512 return 0;
dc0b027d 1513 if ((delegation->type & fmode) != fmode)
aac00a8d 1514 return 0;
2a606188
TM
1515 switch (claim) {
1516 case NFS4_OPEN_CLAIM_NULL:
1517 case NFS4_OPEN_CLAIM_FH:
1518 break;
1519 case NFS4_OPEN_CLAIM_PREVIOUS:
1520 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1521 break;
df561f66 1522 fallthrough;
2a606188
TM
1523 default:
1524 return 0;
1525 }
b7391f44 1526 nfs_mark_delegation_referenced(delegation);
aac00a8d
TM
1527 return 1;
1528}
1529
dc0b027d 1530static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
e7616923 1531{
dc0b027d 1532 switch (fmode) {
e7616923
TM
1533 case FMODE_WRITE:
1534 state->n_wronly++;
1535 break;
1536 case FMODE_READ:
1537 state->n_rdonly++;
1538 break;
1539 case FMODE_READ|FMODE_WRITE:
1540 state->n_rdwr++;
1541 }
dc0b027d 1542 nfs4_state_set_mode_locked(state, state->state | fmode);
003707c7
TM
1543}
1544
8a64c4ef
TM
1545#ifdef CONFIG_NFS_V4_1
1546static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1547{
1548 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1549 return true;
1550 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1551 return true;
1552 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1553 return true;
1554 return false;
1555}
1556#endif /* CONFIG_NFS_V4_1 */
1557
c9399f21
TM
1558static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1559{
1560 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1561 wake_up_all(&state->waitq);
1562}
1563
4f14c194
TM
1564static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1565{
1566 struct nfs_client *clp = state->owner->so_server->nfs_client;
1567 bool need_recover = false;
1568
1569 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1570 need_recover = true;
1571 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1572 need_recover = true;
1573 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1574 need_recover = true;
1575 if (need_recover)
1576 nfs4_state_mark_reclaim_nograce(clp, state);
1577}
1578
c9399f21
TM
1579/*
1580 * Check for whether or not the caller may update the open stateid
1581 * to the value passed in by stateid.
1582 *
1583 * Note: This function relies heavily on the server implementing
1584 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1585 * correctly.
1586 * i.e. The stateid seqids have to be initialised to 1, and
1587 * are then incremented on every state transition.
1588 */
b4868b44 1589static bool nfs_stateid_is_sequential(struct nfs4_state *state,
c9399f21 1590 const nfs4_stateid *stateid)
e999e80e 1591{
b4868b44
BC
1592 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1593 /* The common case - we're updating to a new sequence number */
1594 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1595 nfs4_stateid_is_next(&state->open_stateid, stateid)) {
1596 return true;
1597 }
1598 } else {
1599 /* This is the first OPEN in this generation */
c9399f21 1600 if (stateid->seqid == cpu_to_be32(1))
b4868b44 1601 return true;
c9399f21 1602 }
e999e80e
TM
1603 return false;
1604}
1605
f95549cf
TM
1606static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1607{
3c38cbe2
TM
1608 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1609 return;
f95549cf
TM
1610 if (state->n_wronly)
1611 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1612 if (state->n_rdonly)
1613 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1614 if (state->n_rdwr)
1615 set_bit(NFS_O_RDWR_STATE, &state->flags);
3c38cbe2 1616 set_bit(NFS_OPEN_STATE, &state->flags);
f95549cf
TM
1617}
1618
226056c5
TM
1619static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1620 nfs4_stateid *stateid, fmode_t fmode)
003707c7 1621{
226056c5
TM
1622 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1623 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1624 case FMODE_WRITE:
1625 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1626 break;
1627 case FMODE_READ:
1628 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1629 break;
1630 case 0:
1631 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1632 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1633 clear_bit(NFS_OPEN_STATE, &state->flags);
1634 }
1635 if (stateid == NULL)
1636 return;
3e7dfb16
TM
1637 /* Handle OPEN+OPEN_DOWNGRADE races */
1638 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1639 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
f95549cf 1640 nfs_resync_open_stateid_locked(state);
c9399f21 1641 goto out;
f95549cf 1642 }
003707c7 1643 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
f597c537
TM
1644 nfs4_stateid_copy(&state->stateid, stateid);
1645 nfs4_stateid_copy(&state->open_stateid, stateid);
ad9e02dc 1646 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
c9399f21
TM
1647out:
1648 nfs_state_log_update_open_stateid(state);
226056c5
TM
1649}
1650
4a1e2feb
TM
1651static void nfs_clear_open_stateid(struct nfs4_state *state,
1652 nfs4_stateid *arg_stateid,
1653 nfs4_stateid *stateid, fmode_t fmode)
226056c5
TM
1654{
1655 write_seqlock(&state->seqlock);
3e7dfb16
TM
1656 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1657 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1658 nfs_clear_open_stateid_locked(state, stateid, fmode);
226056c5 1659 write_sequnlock(&state->seqlock);
4f14c194
TM
1660 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1661 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
226056c5
TM
1662}
1663
1393d961 1664static void nfs_set_open_stateid_locked(struct nfs4_state *state,
c9399f21 1665 const nfs4_stateid *stateid, nfs4_stateid *freeme)
e9acf210
TM
1666 __must_hold(&state->owner->so_lock)
1667 __must_hold(&state->seqlock)
1668 __must_hold(RCU)
1669
003707c7 1670{
c9399f21
TM
1671 DEFINE_WAIT(wait);
1672 int status = 0;
1673 for (;;) {
1674
b4868b44 1675 if (nfs_stateid_is_sequential(state, stateid))
003707c7 1676 break;
b4868b44 1677
c9399f21 1678 if (status)
003707c7 1679 break;
c9399f21
TM
1680 /* Rely on seqids for serialisation with NFSv4.0 */
1681 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1682 break;
1683
b4868b44 1684 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
c9399f21
TM
1685 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1686 /*
1687 * Ensure we process the state changes in the same order
1688 * in which the server processed them by delaying the
1689 * update of the stateid until we are in sequence.
1690 */
1691 write_sequnlock(&state->seqlock);
1692 spin_unlock(&state->owner->so_lock);
1693 rcu_read_unlock();
ad9e02dc 1694 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
b4868b44 1695
c9399f21
TM
1696 if (!signal_pending(current)) {
1697 if (schedule_timeout(5*HZ) == 0)
1698 status = -EAGAIN;
1699 else
1700 status = 0;
1701 } else
1702 status = -EINTR;
1703 finish_wait(&state->waitq, &wait);
1704 rcu_read_lock();
1705 spin_lock(&state->owner->so_lock);
1706 write_seqlock(&state->seqlock);
003707c7 1707 }
c9399f21 1708
e1fff5df
TM
1709 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1710 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
c9399f21
TM
1711 nfs4_stateid_copy(freeme, &state->open_stateid);
1712 nfs_test_and_clear_all_open_stateid(state);
1713 }
1714
e999e80e
TM
1715 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1716 nfs4_stateid_copy(&state->stateid, stateid);
1717 nfs4_stateid_copy(&state->open_stateid, stateid);
ad9e02dc 1718 trace_nfs4_open_stateid_update(state->inode, stateid, status);
c9399f21 1719 nfs_state_log_update_open_stateid(state);
003707c7
TM
1720}
1721
c9399f21 1722static void nfs_state_set_open_stateid(struct nfs4_state *state,
1393d961 1723 const nfs4_stateid *open_stateid,
1393d961
TM
1724 fmode_t fmode,
1725 nfs4_stateid *freeme)
1da177e4 1726{
8bda4e4c
TM
1727 /*
1728 * Protect the call to nfs4_state_set_mode_locked and
1729 * serialise the stateid update
1730 */
1731 write_seqlock(&state->seqlock);
c9399f21
TM
1732 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1733 switch (fmode) {
1734 case FMODE_READ:
1735 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1736 break;
1737 case FMODE_WRITE:
1738 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1739 break;
1740 case FMODE_READ|FMODE_WRITE:
1741 set_bit(NFS_O_RDWR_STATE, &state->flags);
003707c7 1742 }
c9399f21
TM
1743 set_bit(NFS_OPEN_STATE, &state->flags);
1744 write_sequnlock(&state->seqlock);
1745}
1746
27a30cf6
TM
1747static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1748{
1749 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1750 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1751 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1752 clear_bit(NFS_OPEN_STATE, &state->flags);
1753}
1754
c9399f21
TM
1755static void nfs_state_set_delegation(struct nfs4_state *state,
1756 const nfs4_stateid *deleg_stateid,
1757 fmode_t fmode)
1758{
1759 /*
1760 * Protect the call to nfs4_state_set_mode_locked and
1761 * serialise the stateid update
1762 */
1763 write_seqlock(&state->seqlock);
1764 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1765 set_bit(NFS_DELEGATED_STATE, &state->flags);
8bda4e4c 1766 write_sequnlock(&state->seqlock);
1da177e4
LT
1767}
1768
9f0c5124
TM
1769static void nfs_state_clear_delegation(struct nfs4_state *state)
1770{
1771 write_seqlock(&state->seqlock);
1772 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1773 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1774 write_sequnlock(&state->seqlock);
1775}
1776
ec4b0925 1777int update_open_stateid(struct nfs4_state *state,
1393d961
TM
1778 const nfs4_stateid *open_stateid,
1779 const nfs4_stateid *delegation,
1780 fmode_t fmode)
34310430 1781{
1393d961
TM
1782 struct nfs_server *server = NFS_SERVER(state->inode);
1783 struct nfs_client *clp = server->nfs_client;
34310430
TM
1784 struct nfs_inode *nfsi = NFS_I(state->inode);
1785 struct nfs_delegation *deleg_cur;
83aa3e0f 1786 nfs4_stateid freeme = { };
34310430
TM
1787 int ret = 0;
1788
dc0b027d 1789 fmode &= (FMODE_READ|FMODE_WRITE);
34310430
TM
1790
1791 rcu_read_lock();
c9399f21
TM
1792 spin_lock(&state->owner->so_lock);
1793 if (open_stateid != NULL) {
1794 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1795 ret = 1;
1796 }
1797
333ac786 1798 deleg_cur = nfs4_get_valid_delegation(state->inode);
34310430
TM
1799 if (deleg_cur == NULL)
1800 goto no_delegation;
1801
1802 spin_lock(&deleg_cur->lock);
17f26b12 1803 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
d25be546 1804 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
dc0b027d 1805 (deleg_cur->type & fmode) != fmode)
34310430
TM
1806 goto no_delegation_unlock;
1807
1808 if (delegation == NULL)
1809 delegation = &deleg_cur->stateid;
333ac786 1810 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
34310430
TM
1811 goto no_delegation_unlock;
1812
b7391f44 1813 nfs_mark_delegation_referenced(deleg_cur);
c9399f21 1814 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
34310430
TM
1815 ret = 1;
1816no_delegation_unlock:
1817 spin_unlock(&deleg_cur->lock);
1818no_delegation:
c9399f21
TM
1819 if (ret)
1820 update_open_stateflags(state, fmode);
1821 spin_unlock(&state->owner->so_lock);
34310430
TM
1822 rcu_read_unlock();
1823
4f14c194 1824 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1393d961
TM
1825 nfs4_schedule_state_manager(clp);
1826 if (freeme.type != 0)
1827 nfs4_test_and_free_stateid(server, &freeme,
1828 state->owner->so_cred);
34310430
TM
1829
1830 return ret;
1831}
1832
39071e6f
TM
1833static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1834 const nfs4_stateid *stateid)
1835{
1836 struct nfs4_state *state = lsp->ls_state;
1837 bool ret = false;
1838
1839 spin_lock(&state->state_lock);
1840 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1841 goto out_noupdate;
1842 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1843 goto out_noupdate;
1844 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1845 ret = true;
1846out_noupdate:
1847 spin_unlock(&state->state_lock);
1848 return ret;
1849}
34310430 1850
dc0b027d 1851static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
aac00a8d
TM
1852{
1853 struct nfs_delegation *delegation;
1854
f5086242 1855 fmode &= FMODE_READ|FMODE_WRITE;
aac00a8d 1856 rcu_read_lock();
40e6aa10 1857 delegation = nfs4_get_valid_delegation(inode);
dc0b027d 1858 if (delegation == NULL || (delegation->type & fmode) == fmode) {
aac00a8d
TM
1859 rcu_read_unlock();
1860 return;
1861 }
1862 rcu_read_unlock();
57ec14c5 1863 nfs4_inode_return_delegation(inode);
aac00a8d
TM
1864}
1865
6ee41268 1866static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
aac00a8d
TM
1867{
1868 struct nfs4_state *state = opendata->state;
aac00a8d 1869 struct nfs_delegation *delegation;
f448badd 1870 int open_mode = opendata->o_arg.open_flags;
dc0b027d 1871 fmode_t fmode = opendata->o_arg.fmode;
2a606188 1872 enum open_claim_type4 claim = opendata->o_arg.claim;
aac00a8d
TM
1873 nfs4_stateid stateid;
1874 int ret = -EAGAIN;
1875
aac00a8d 1876 for (;;) {
61beef75 1877 spin_lock(&state->owner->so_lock);
be189f7e 1878 if (can_open_cached(state, fmode, open_mode, claim)) {
61beef75 1879 update_open_stateflags(state, fmode);
6ee41268 1880 spin_unlock(&state->owner->so_lock);
61beef75 1881 goto out_return_state;
6ee41268 1882 }
61beef75 1883 spin_unlock(&state->owner->so_lock);
34310430 1884 rcu_read_lock();
be3df3dd 1885 delegation = nfs4_get_valid_delegation(state->inode);
2a606188 1886 if (!can_open_delegated(delegation, fmode, claim)) {
34310430 1887 rcu_read_unlock();
aac00a8d 1888 break;
34310430 1889 }
aac00a8d 1890 /* Save the delegation */
f597c537 1891 nfs4_stateid_copy(&stateid, &delegation->stateid);
aac00a8d 1892 rcu_read_unlock();
fa332941 1893 nfs_release_seqid(opendata->o_arg.seqid);
bdeca1b7
TM
1894 if (!opendata->is_recover) {
1895 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1896 if (ret != 0)
1897 goto out;
1898 }
aac00a8d 1899 ret = -EAGAIN;
34310430
TM
1900
1901 /* Try to update the stateid using the delegation */
dc0b027d 1902 if (update_open_stateid(state, NULL, &stateid, fmode))
34310430 1903 goto out_return_state;
aac00a8d 1904 }
aac00a8d
TM
1905out:
1906 return ERR_PTR(ret);
1907out_return_state:
ace9fad4 1908 refcount_inc(&state->count);
aac00a8d
TM
1909 return state;
1910}
1911
e23008ec
AA
1912static void
1913nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1914{
1915 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1916 struct nfs_delegation *delegation;
1917 int delegation_flags = 0;
1918
1919 rcu_read_lock();
1920 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1921 if (delegation)
1922 delegation_flags = delegation->flags;
1923 rcu_read_unlock();
72d79ff8
TM
1924 switch (data->o_arg.claim) {
1925 default:
1926 break;
1927 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1928 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
e23008ec
AA
1929 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1930 "returning a delegation for "
1931 "OPEN(CLAIM_DELEGATE_CUR)\n",
1932 clp->cl_hostname);
72d79ff8
TM
1933 return;
1934 }
1935 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
e23008ec 1936 nfs_inode_set_delegation(state->inode,
35156bff
TM
1937 data->owner->so_cred,
1938 data->o_res.delegation_type,
1939 &data->o_res.delegation,
1940 data->o_res.pagemod_limit);
e23008ec
AA
1941 else
1942 nfs_inode_reclaim_delegation(state->inode,
35156bff
TM
1943 data->owner->so_cred,
1944 data->o_res.delegation_type,
1945 &data->o_res.delegation,
1946 data->o_res.pagemod_limit);
8b199e58
JL
1947
1948 if (data->o_res.do_recall)
1949 nfs_async_inode_return_delegation(state->inode,
1950 &data->o_res.delegation);
e23008ec
AA
1951}
1952
1953/*
1954 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1955 * and update the nfs4_state.
1956 */
1957static struct nfs4_state *
1958_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1959{
1960 struct inode *inode = data->state->inode;
1961 struct nfs4_state *state = data->state;
1962 int ret;
1963
d2bfda2e 1964 if (!data->rpc_done) {
37a8484a
AS
1965 if (data->rpc_status)
1966 return ERR_PTR(data->rpc_status);
d2bfda2e
WAA
1967 /* cached opens have already been processed */
1968 goto update;
e23008ec
AA
1969 }
1970
e23008ec
AA
1971 ret = nfs_refresh_inode(inode, &data->f_attr);
1972 if (ret)
37a8484a 1973 return ERR_PTR(ret);
e23008ec
AA
1974
1975 if (data->o_res.delegation_type != 0)
1976 nfs4_opendata_check_deleg(data, state);
d2bfda2e 1977update:
e3c8dc76
TM
1978 if (!update_open_stateid(state, &data->o_res.stateid,
1979 NULL, data->o_arg.fmode))
1980 return ERR_PTR(-EAGAIN);
ace9fad4 1981 refcount_inc(&state->count);
e23008ec
AA
1982
1983 return state;
e23008ec
AA
1984}
1985
4e2fcac7
TM
1986static struct inode *
1987nfs4_opendata_get_inode(struct nfs4_opendata *data)
1988{
1989 struct inode *inode;
1990
1991 switch (data->o_arg.claim) {
1992 case NFS4_OPEN_CLAIM_NULL:
1993 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1994 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1995 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1996 return ERR_PTR(-EAGAIN);
1997 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1998 &data->f_attr, data->f_label);
1999 break;
2000 default:
2001 inode = d_inode(data->dentry);
2002 ihold(inode);
2003 nfs_refresh_inode(inode, &data->f_attr);
2004 }
2005 return inode;
2006}
2007
e23008ec 2008static struct nfs4_state *
75e8c48b 2009nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
24ac23ab 2010{
75e8c48b 2011 struct nfs4_state *state;
24ac23ab 2012 struct inode *inode;
75e8c48b
TM
2013
2014 inode = nfs4_opendata_get_inode(data);
2015 if (IS_ERR(inode))
2016 return ERR_CAST(inode);
2017 if (data->state != NULL && data->state->inode == inode) {
2018 state = data->state;
ace9fad4 2019 refcount_inc(&state->count);
75e8c48b
TM
2020 } else
2021 state = nfs4_get_open_state(inode, data->owner);
2022 iput(inode);
2023 if (state == NULL)
2024 state = ERR_PTR(-ENOMEM);
2025 return state;
2026}
2027
2028static struct nfs4_state *
2029_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2030{
2031 struct nfs4_state *state;
24ac23ab 2032
aac00a8d 2033 if (!data->rpc_done) {
6ee41268 2034 state = nfs4_try_open_cached(data);
9759b0fb 2035 trace_nfs4_cached_open(data->state);
aac00a8d
TM
2036 goto out;
2037 }
2038
75e8c48b
TM
2039 state = nfs4_opendata_find_nfs4_state(data);
2040 if (IS_ERR(state))
2041 goto out;
2042
e23008ec
AA
2043 if (data->o_res.delegation_type != 0)
2044 nfs4_opendata_check_deleg(data, state);
e3c8dc76
TM
2045 if (!update_open_stateid(state, &data->o_res.stateid,
2046 NULL, data->o_arg.fmode)) {
2047 nfs4_put_open_state(state);
2048 state = ERR_PTR(-EAGAIN);
2049 }
aac00a8d 2050out:
7aa262b5 2051 nfs_release_seqid(data->o_arg.seqid);
24ac23ab
TM
2052 return state;
2053}
2054
e23008ec
AA
2055static struct nfs4_state *
2056nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2057{
2e80dbe7
TM
2058 struct nfs4_state *ret;
2059
e23008ec 2060 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2e80dbe7
TM
2061 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2062 else
2063 ret = _nfs4_opendata_to_nfs4_state(data);
2064 nfs4_sequence_free_slot(&data->o_res.seq_res);
2065 return ret;
e23008ec
AA
2066}
2067
0de43976
TM
2068static struct nfs_open_context *
2069nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
864472e9
TM
2070{
2071 struct nfs_inode *nfsi = NFS_I(state->inode);
2072 struct nfs_open_context *ctx;
2073
0de43976
TM
2074 rcu_read_lock();
2075 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
864472e9
TM
2076 if (ctx->state != state)
2077 continue;
0de43976
TM
2078 if ((ctx->mode & mode) != mode)
2079 continue;
2080 if (!get_nfs_open_context(ctx))
2081 continue;
2082 rcu_read_unlock();
864472e9
TM
2083 return ctx;
2084 }
0de43976 2085 rcu_read_unlock();
864472e9
TM
2086 return ERR_PTR(-ENOENT);
2087}
2088
0de43976
TM
2089static struct nfs_open_context *
2090nfs4_state_find_open_context(struct nfs4_state *state)
2091{
2092 struct nfs_open_context *ctx;
2093
2094 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2095 if (!IS_ERR(ctx))
2096 return ctx;
2097 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2098 if (!IS_ERR(ctx))
2099 return ctx;
2100 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2101}
2102
4a1c0893
TM
2103static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2104 struct nfs4_state *state, enum open_claim_type4 claim)
6f220ed5
TM
2105{
2106 struct nfs4_opendata *opendata;
2107
4a1c0893 2108 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
8fd1ab74 2109 NULL, claim, GFP_NOFS);
6f220ed5
TM
2110 if (opendata == NULL)
2111 return ERR_PTR(-ENOMEM);
2112 opendata->state = state;
ace9fad4 2113 refcount_inc(&state->count);
6f220ed5
TM
2114 return opendata;
2115}
2116
24311f88
TM
2117static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2118 fmode_t fmode)
864472e9 2119{
2ced46c2 2120 struct nfs4_state *newstate;
864472e9
TM
2121 int ret;
2122
24311f88 2123 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
39f897fd 2124 return 0;
dc0b027d
TM
2125 opendata->o_arg.open_flags = 0;
2126 opendata->o_arg.fmode = fmode;
be36e185
TM
2127 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2128 NFS_SB(opendata->dentry->d_sb),
2129 fmode, 0);
2ced46c2
TM
2130 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2131 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2132 nfs4_init_opendata_res(opendata);
b257957e 2133 ret = _nfs4_recover_proc_open(opendata);
864472e9
TM
2134 if (ret != 0)
2135 return ret;
2ced46c2 2136 newstate = nfs4_opendata_to_nfs4_state(opendata);
1b370bc2
TM
2137 if (IS_ERR(newstate))
2138 return PTR_ERR(newstate);
24311f88
TM
2139 if (newstate != opendata->state)
2140 ret = -ESTALE;
643168c2 2141 nfs4_close_state(newstate, fmode);
24311f88 2142 return ret;
864472e9
TM
2143}
2144
2145static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2146{
864472e9
TM
2147 int ret;
2148
2149 /* memory barrier prior to reading state->n_* */
2150 smp_rmb();
24311f88
TM
2151 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2152 if (ret != 0)
2153 return ret;
2154 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2155 if (ret != 0)
2156 return ret;
2157 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2158 if (ret != 0)
2159 return ret;
1ac7e2fd
TM
2160 /*
2161 * We may have performed cached opens for all three recoveries.
2162 * Check if we need to update the current stateid.
2163 */
2164 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
f597c537 2165 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
8bda4e4c 2166 write_seqlock(&state->seqlock);
1ac7e2fd 2167 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
f597c537 2168 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
8bda4e4c 2169 write_sequnlock(&state->seqlock);
1ac7e2fd 2170 }
864472e9
TM
2171 return 0;
2172}
2173
1da177e4
LT
2174/*
2175 * OPEN_RECLAIM:
2176 * reclaim state on the server after a reboot.
1da177e4 2177 */
539cd03a 2178static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1da177e4 2179{
1ac7e2fd 2180 struct nfs_delegation *delegation;
864472e9 2181 struct nfs4_opendata *opendata;
dc0b027d 2182 fmode_t delegation_type = 0;
1da177e4
LT
2183 int status;
2184
4a1c0893
TM
2185 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2186 NFS4_OPEN_CLAIM_PREVIOUS);
6f220ed5
TM
2187 if (IS_ERR(opendata))
2188 return PTR_ERR(opendata);
1ac7e2fd
TM
2189 rcu_read_lock();
2190 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
15c831bf 2191 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
65bbf6bd 2192 delegation_type = delegation->type;
1ac7e2fd 2193 rcu_read_unlock();
864472e9
TM
2194 opendata->o_arg.u.delegation_type = delegation_type;
2195 status = nfs4_open_recover(opendata, state);
c6d00e63 2196 nfs4_opendata_put(opendata);
1da177e4
LT
2197 return status;
2198}
2199
539cd03a 2200static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1da177e4
LT
2201{
2202 struct nfs_server *server = NFS_SERVER(state->inode);
2203 struct nfs4_exception exception = { };
2204 int err;
2205 do {
539cd03a 2206 err = _nfs4_do_open_reclaim(ctx, state);
42113a75 2207 trace_nfs4_open_reclaim(ctx, 0, err);
49f9a0fa
TM
2208 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2209 continue;
168667c4 2210 if (err != -NFS4ERR_DELAY)
202b50dc
TM
2211 break;
2212 nfs4_handle_exception(server, err, &exception);
1da177e4
LT
2213 } while (exception.retry);
2214 return err;
2215}
2216
864472e9
TM
2217static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2218{
2219 struct nfs_open_context *ctx;
2220 int ret;
2221
2222 ctx = nfs4_state_find_open_context(state);
2223 if (IS_ERR(ctx))
91876b13 2224 return -EAGAIN;
27a30cf6
TM
2225 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2226 nfs_state_clear_open_state_flags(state);
539cd03a 2227 ret = nfs4_do_open_reclaim(ctx, state);
864472e9
TM
2228 put_nfs_open_context(ctx);
2229 return ret;
2230}
2231
dce2630c 2232static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
1da177e4 2233{
be76b5b6
TM
2234 switch (err) {
2235 default:
2236 printk(KERN_ERR "NFS: %s: unhandled error "
2237 "%d.\n", __func__, err);
2238 case 0:
2239 case -ENOENT:
8eee52af 2240 case -EAGAIN:
be76b5b6 2241 case -ESTALE:
67e7b52d 2242 case -ETIMEDOUT:
be76b5b6
TM
2243 break;
2244 case -NFS4ERR_BADSESSION:
2245 case -NFS4ERR_BADSLOT:
2246 case -NFS4ERR_BAD_HIGH_SLOT:
2247 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2248 case -NFS4ERR_DEADSESSION:
be76b5b6
TM
2249 return -EAGAIN;
2250 case -NFS4ERR_STALE_CLIENTID:
2251 case -NFS4ERR_STALE_STATEID:
be76b5b6
TM
2252 /* Don't recall a delegation if it was lost */
2253 nfs4_schedule_lease_recovery(server->nfs_client);
2254 return -EAGAIN;
352297b9
CL
2255 case -NFS4ERR_MOVED:
2256 nfs4_schedule_migration_recovery(server);
2257 return -EAGAIN;
8ef2f8d4
CL
2258 case -NFS4ERR_LEASE_MOVED:
2259 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2260 return -EAGAIN;
be76b5b6
TM
2261 case -NFS4ERR_DELEG_REVOKED:
2262 case -NFS4ERR_ADMIN_REVOKED:
404ea356 2263 case -NFS4ERR_EXPIRED:
be76b5b6 2264 case -NFS4ERR_BAD_STATEID:
db4f2e63 2265 case -NFS4ERR_OPENMODE:
be76b5b6
TM
2266 nfs_inode_find_state_and_recover(state->inode,
2267 stateid);
2268 nfs4_schedule_stateid_recovery(server, state);
869f9dfa 2269 return -EAGAIN;
be76b5b6
TM
2270 case -NFS4ERR_DELAY:
2271 case -NFS4ERR_GRACE:
be76b5b6
TM
2272 ssleep(1);
2273 return -EAGAIN;
db4f2e63
TM
2274 case -ENOMEM:
2275 case -NFS4ERR_DENIED:
dce2630c
N
2276 if (fl) {
2277 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2278 if (lsp)
2279 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2280 }
db4f2e63 2281 return 0;
be76b5b6 2282 }
1da177e4
LT
2283 return err;
2284}
2285
24311f88 2286int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
5eb8d18c 2287 struct nfs4_state *state, const nfs4_stateid *stateid)
db4f2e63
TM
2288{
2289 struct nfs_server *server = NFS_SERVER(state->inode);
2290 struct nfs4_opendata *opendata;
24311f88 2291 int err = 0;
db4f2e63
TM
2292
2293 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2294 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2295 if (IS_ERR(opendata))
2296 return PTR_ERR(opendata);
2297 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
5eb8d18c 2298 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
24311f88
TM
2299 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2300 if (err)
5eb8d18c
TM
2301 goto out;
2302 }
2303 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
24311f88
TM
2304 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2305 if (err)
5eb8d18c
TM
2306 goto out;
2307 }
2308 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
24311f88 2309 err = nfs4_open_recover_helper(opendata, FMODE_READ);
5eb8d18c
TM
2310 if (err)
2311 goto out;
24311f88 2312 }
5eb8d18c
TM
2313 nfs_state_clear_delegation(state);
2314out:
db4f2e63 2315 nfs4_opendata_put(opendata);
dce2630c 2316 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
db4f2e63
TM
2317}
2318
be05c860
CL
2319static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2320{
2321 struct nfs4_opendata *data = calldata;
2322
7981c8a6
AS
2323 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2324 &data->c_arg.seq_args, &data->c_res.seq_res, task);
be05c860
CL
2325}
2326
cdd4e68b
TM
2327static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2328{
2329 struct nfs4_opendata *data = calldata;
2330
17ead6c8 2331 nfs40_sequence_done(task, &data->c_res.seq_res);
be05c860 2332
cdd4e68b 2333 data->rpc_status = task->tk_status;
26e976a8 2334 if (data->rpc_status == 0) {
f597c537 2335 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
bb22629e 2336 nfs_confirm_seqid(&data->owner->so_seqid, 0);
26e976a8 2337 renew_lease(data->o_res.server, data->timestamp);
a7a3b1e9 2338 data->rpc_done = true;
26e976a8 2339 }
cdd4e68b
TM
2340}
2341
2342static void nfs4_open_confirm_release(void *calldata)
2343{
2344 struct nfs4_opendata *data = calldata;
2345 struct nfs4_state *state = NULL;
2346
2347 /* If this request hasn't been cancelled, do nothing */
a7a3b1e9 2348 if (!data->cancelled)
cdd4e68b
TM
2349 goto out_free;
2350 /* In case of error, no cleanup! */
3e309914 2351 if (!data->rpc_done)
cdd4e68b 2352 goto out_free;
cdd4e68b 2353 state = nfs4_opendata_to_nfs4_state(data);
1b370bc2 2354 if (!IS_ERR(state))
643168c2 2355 nfs4_close_state(state, data->o_arg.fmode);
cdd4e68b 2356out_free:
c6d00e63 2357 nfs4_opendata_put(data);
cdd4e68b
TM
2358}
2359
2360static const struct rpc_call_ops nfs4_open_confirm_ops = {
be05c860 2361 .rpc_call_prepare = nfs4_open_confirm_prepare,
cdd4e68b
TM
2362 .rpc_call_done = nfs4_open_confirm_done,
2363 .rpc_release = nfs4_open_confirm_release,
2364};
2365
2366/*
2367 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2368 */
2369static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2370{
2b0143b5 2371 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
cdd4e68b 2372 struct rpc_task *task;
5138fde0
TM
2373 struct rpc_message msg = {
2374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2375 .rpc_argp = &data->c_arg,
2376 .rpc_resp = &data->c_res,
2377 .rpc_cred = data->owner->so_cred,
2378 };
c970aa85
TM
2379 struct rpc_task_setup task_setup_data = {
2380 .rpc_client = server->client,
5138fde0 2381 .rpc_message = &msg,
c970aa85
TM
2382 .callback_ops = &nfs4_open_confirm_ops,
2383 .callback_data = data,
101070ca 2384 .workqueue = nfsiod_workqueue,
61296507 2385 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
c970aa85 2386 };
1da177e4
LT
2387 int status;
2388
fba83f34
AS
2389 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2390 data->is_recover);
c6d00e63 2391 kref_get(&data->kref);
a7a3b1e9 2392 data->rpc_done = false;
3e309914 2393 data->rpc_status = 0;
5138fde0 2394 data->timestamp = jiffies;
c970aa85 2395 task = rpc_run_task(&task_setup_data);
7a1218a2 2396 if (IS_ERR(task))
cdd4e68b 2397 return PTR_ERR(task);
820bf85c 2398 status = rpc_wait_for_completion_task(task);
cdd4e68b 2399 if (status != 0) {
a7a3b1e9 2400 data->cancelled = true;
cdd4e68b
TM
2401 smp_wmb();
2402 } else
2403 status = data->rpc_status;
e6b3c4db 2404 rpc_put_task(task);
1da177e4
LT
2405 return status;
2406}
2407
24ac23ab 2408static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1da177e4 2409{
24ac23ab
TM
2410 struct nfs4_opendata *data = calldata;
2411 struct nfs4_state_owner *sp = data->owner;
549b19cc 2412 struct nfs_client *clp = sp->so_server->nfs_client;
2a606188 2413 enum open_claim_type4 claim = data->o_arg.claim;
5138fde0 2414
24ac23ab 2415 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
c8da19b9 2416 goto out_wait;
aac00a8d
TM
2417 /*
2418 * Check if we still need to send an OPEN call, or if we can use
2419 * a delegation instead.
2420 */
2421 if (data->state != NULL) {
2422 struct nfs_delegation *delegation;
2423
be189f7e
TM
2424 if (can_open_cached(data->state, data->o_arg.fmode,
2425 data->o_arg.open_flags, claim))
6ee41268 2426 goto out_no_action;
aac00a8d 2427 rcu_read_lock();
be3df3dd 2428 delegation = nfs4_get_valid_delegation(data->state->inode);
2a606188 2429 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
652f89f6 2430 goto unlock_no_action;
aac00a8d
TM
2431 rcu_read_unlock();
2432 }
95b72eb0 2433 /* Update client id. */
549b19cc 2434 data->o_arg.clientid = clp->cl_clientid;
2a606188
TM
2435 switch (claim) {
2436 default:
2437 break;
8188df17
TM
2438 case NFS4_OPEN_CLAIM_PREVIOUS:
2439 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2440 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
e23008ec 2441 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
df561f66 2442 fallthrough;
8188df17
TM
2443 case NFS4_OPEN_CLAIM_FH:
2444 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
6f220ed5 2445 }
26e976a8 2446 data->timestamp = jiffies;
42e1cca7 2447 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
d898528c 2448 &data->o_arg.seq_args,
2240a9e2
TM
2449 &data->o_res.seq_res,
2450 task) != 0)
2451 nfs_release_seqid(data->o_arg.seqid);
549b19cc
TM
2452
2453 /* Set the create mode (note dependency on the session type) */
2454 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2455 if (data->o_arg.open_flags & O_EXCL) {
2456 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2457 if (nfs4_has_persistent_session(clp))
2458 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2459 else if (clp->cl_mvops->minor_version > 0)
2460 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2461 }
6ee41268 2462 return;
652f89f6 2463unlock_no_action:
9759b0fb 2464 trace_nfs4_cached_open(data->state);
652f89f6 2465 rcu_read_unlock();
6ee41268
TM
2466out_no_action:
2467 task->tk_action = NULL;
c8da19b9 2468out_wait:
b75ad4cd 2469 nfs4_sequence_done(task, &data->o_res.seq_res);
b257957e
AB
2470}
2471
24ac23ab
TM
2472static void nfs4_open_done(struct rpc_task *task, void *calldata)
2473{
2474 struct nfs4_opendata *data = calldata;
1da177e4 2475
24ac23ab 2476 data->rpc_status = task->tk_status;
d898528c 2477
2e80dbe7 2478 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
14516c3a 2479 return;
d898528c 2480
24ac23ab 2481 if (task->tk_status == 0) {
807d66d8
TM
2482 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2483 switch (data->o_res.f_attr->mode & S_IFMT) {
6f926b5b
TM
2484 case S_IFREG:
2485 break;
2486 case S_IFLNK:
24ac23ab 2487 data->rpc_status = -ELOOP;
6f926b5b
TM
2488 break;
2489 case S_IFDIR:
24ac23ab 2490 data->rpc_status = -EISDIR;
6f926b5b
TM
2491 break;
2492 default:
24ac23ab 2493 data->rpc_status = -ENOTDIR;
807d66d8 2494 }
6f926b5b 2495 }
26e976a8 2496 renew_lease(data->o_res.server, data->timestamp);
0f9f95e0
TM
2497 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2498 nfs_confirm_seqid(&data->owner->so_seqid, 0);
6f926b5b 2499 }
a7a3b1e9 2500 data->rpc_done = true;
24ac23ab 2501}
6f926b5b 2502
24ac23ab
TM
2503static void nfs4_open_release(void *calldata)
2504{
2505 struct nfs4_opendata *data = calldata;
2506 struct nfs4_state *state = NULL;
2507
2508 /* If this request hasn't been cancelled, do nothing */
a7a3b1e9 2509 if (!data->cancelled)
24ac23ab
TM
2510 goto out_free;
2511 /* In case of error, no cleanup! */
3e309914 2512 if (data->rpc_status != 0 || !data->rpc_done)
24ac23ab
TM
2513 goto out_free;
2514 /* In case we need an open_confirm, no cleanup! */
2515 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2516 goto out_free;
24ac23ab 2517 state = nfs4_opendata_to_nfs4_state(data);
1b370bc2 2518 if (!IS_ERR(state))
643168c2 2519 nfs4_close_state(state, data->o_arg.fmode);
24ac23ab 2520out_free:
c6d00e63 2521 nfs4_opendata_put(data);
24ac23ab
TM
2522}
2523
2524static const struct rpc_call_ops nfs4_open_ops = {
2525 .rpc_call_prepare = nfs4_open_prepare,
2526 .rpc_call_done = nfs4_open_done,
2527 .rpc_release = nfs4_open_release,
2528};
2529
3b65a30d
FI
2530static int nfs4_run_open_task(struct nfs4_opendata *data,
2531 struct nfs_open_context *ctx)
24ac23ab 2532{
2b0143b5 2533 struct inode *dir = d_inode(data->dir);
24ac23ab
TM
2534 struct nfs_server *server = NFS_SERVER(dir);
2535 struct nfs_openargs *o_arg = &data->o_arg;
2536 struct nfs_openres *o_res = &data->o_res;
2537 struct rpc_task *task;
5138fde0
TM
2538 struct rpc_message msg = {
2539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2540 .rpc_argp = o_arg,
2541 .rpc_resp = o_res,
2542 .rpc_cred = data->owner->so_cred,
2543 };
c970aa85
TM
2544 struct rpc_task_setup task_setup_data = {
2545 .rpc_client = server->client,
5138fde0 2546 .rpc_message = &msg,
c970aa85
TM
2547 .callback_ops = &nfs4_open_ops,
2548 .callback_data = data,
101070ca 2549 .workqueue = nfsiod_workqueue,
61296507 2550 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
c970aa85 2551 };
24ac23ab
TM
2552 int status;
2553
c6d00e63 2554 kref_get(&data->kref);
a7a3b1e9 2555 data->rpc_done = false;
3e309914 2556 data->rpc_status = 0;
a7a3b1e9
BC
2557 data->cancelled = false;
2558 data->is_recover = false;
3b65a30d
FI
2559 if (!ctx) {
2560 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
a7a3b1e9 2561 data->is_recover = true;
67e7b52d 2562 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2409a976 2563 } else {
3b65a30d 2564 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2409a976
FI
2565 pnfs_lgopen_prepare(data, ctx);
2566 }
c970aa85 2567 task = rpc_run_task(&task_setup_data);
d9b67e1e
AS
2568 if (IS_ERR(task))
2569 return PTR_ERR(task);
820bf85c 2570 status = rpc_wait_for_completion_task(task);
d9b67e1e 2571 if (status != 0) {
a7a3b1e9 2572 data->cancelled = true;
d9b67e1e
AS
2573 smp_wmb();
2574 } else
2575 status = data->rpc_status;
2576 rpc_put_task(task);
b257957e
AB
2577
2578 return status;
2579}
2580
2581static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2582{
2b0143b5 2583 struct inode *dir = d_inode(data->dir);
b257957e 2584 struct nfs_openres *o_res = &data->o_res;
d9b67e1e 2585 int status;
b257957e 2586
3b65a30d 2587 status = nfs4_run_open_task(data, NULL);
b257957e
AB
2588 if (status != 0 || !data->rpc_done)
2589 return status;
2590
6926afd1
TM
2591 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2592
d7e98258 2593 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
b257957e 2594 status = _nfs4_proc_open_confirm(data);
b257957e
AB
2595
2596 return status;
2597}
2598
f3792d63
TM
2599/*
2600 * Additional permission checks in order to distinguish between an
2601 * open for read, and an open for execute. This works around the
2602 * fact that NFSv4 OPEN treats read and execute permissions as being
2603 * the same.
2604 * Note that in the non-execute case, we want to turn off permission
2605 * checking if we just created a new file (POSIX open() semantics).
2606 */
a52458b4 2607static int nfs4_opendata_access(const struct cred *cred,
6168f62c 2608 struct nfs4_opendata *opendata,
f8d9a897
WAA
2609 struct nfs4_state *state, fmode_t fmode,
2610 int openflags)
6168f62c
WAA
2611{
2612 struct nfs_access_entry cache;
1e6f2095 2613 u32 mask, flags;
6168f62c
WAA
2614
2615 /* access call failed or for some reason the server doesn't
2616 * support any access modes -- defer access call until later */
2617 if (opendata->o_res.access_supported == 0)
2618 return 0;
2619
2620 mask = 0;
f3792d63
TM
2621 /*
2622 * Use openflags to check for exec, because fmode won't
2623 * always have FMODE_EXEC set when file open for exec.
2624 */
f8d9a897
WAA
2625 if (openflags & __FMODE_EXEC) {
2626 /* ONLY check for exec rights */
1e6f2095
AS
2627 if (S_ISDIR(state->inode->i_mode))
2628 mask = NFS4_ACCESS_LOOKUP;
2629 else
2630 mask = NFS4_ACCESS_EXECUTE;
f3792d63 2631 } else if ((fmode & FMODE_READ) && !opendata->file_created)
1e6f2095 2632 mask = NFS4_ACCESS_READ;
6168f62c
WAA
2633
2634 cache.cred = cred;
6168f62c
WAA
2635 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2636 nfs_access_add_cache(state->inode, &cache);
2637
1e6f2095
AS
2638 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2639 if ((mask & ~cache.mask & flags) == 0)
6168f62c
WAA
2640 return 0;
2641
998f40b5 2642 return -EACCES;
6168f62c
WAA
2643}
2644
b257957e
AB
2645/*
2646 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2647 */
3b65a30d
FI
2648static int _nfs4_proc_open(struct nfs4_opendata *data,
2649 struct nfs_open_context *ctx)
b257957e 2650{
2b0143b5 2651 struct inode *dir = d_inode(data->dir);
b257957e
AB
2652 struct nfs_server *server = NFS_SERVER(dir);
2653 struct nfs_openargs *o_arg = &data->o_arg;
2654 struct nfs_openres *o_res = &data->o_res;
2655 int status;
2656
3b65a30d 2657 status = nfs4_run_open_task(data, ctx);
08ef7bd3
TM
2658 if (!data->rpc_done)
2659 return status;
2660 if (status != 0) {
2661 if (status == -NFS4ERR_BADNAME &&
2662 !(o_arg->open_flags & O_CREAT))
2663 return -ENOENT;
24ac23ab 2664 return status;
08ef7bd3 2665 }
24ac23ab 2666
6926afd1
TM
2667 nfs_fattr_map_and_free_names(server, &data->f_attr);
2668
5bc2afc2 2669 if (o_arg->open_flags & O_CREAT) {
5bc2afc2 2670 if (o_arg->open_flags & O_EXCL)
a7a3b1e9 2671 data->file_created = true;
5bc2afc2 2672 else if (o_res->cinfo.before != o_res->cinfo.after)
a7a3b1e9 2673 data->file_created = true;
1eb5d98f
JL
2674 if (data->file_created ||
2675 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
1b523ca9
FL
2676 nfs4_update_changeattr(dir, &o_res->cinfo,
2677 o_res->f_attr->time_start,
2678 NFS_INO_INVALID_DATA);
5bc2afc2 2679 }
0df5dd4a
TM
2680 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2681 server->caps &= ~NFS_CAP_POSIX_LOCK;
1da177e4 2682 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
cdd4e68b 2683 status = _nfs4_proc_open_confirm(data);
1da177e4 2684 if (status != 0)
24ac23ab 2685 return status;
1da177e4 2686 }
56e0d71e
TM
2687 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2688 nfs4_sequence_free_slot(&o_res->seq_res);
a841b54d
TM
2689 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2690 o_res->f_label, NULL);
56e0d71e 2691 }
24ac23ab 2692 return 0;
1da177e4
LT
2693}
2694
1da177e4
LT
2695/*
2696 * OPEN_EXPIRED:
2697 * reclaim state on the server after a network partition.
2698 * Assumes caller holds the appropriate lock
2699 */
539cd03a 2700static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1da177e4 2701{
e56e0b78 2702 struct nfs4_opendata *opendata;
864472e9 2703 int ret;
1da177e4 2704
4a1c0893 2705 opendata = nfs4_open_recoverdata_alloc(ctx, state,
49f9a0fa 2706 NFS4_OPEN_CLAIM_FH);
6f220ed5
TM
2707 if (IS_ERR(opendata))
2708 return PTR_ERR(opendata);
864472e9 2709 ret = nfs4_open_recover(opendata, state);
35d05778 2710 if (ret == -ESTALE)
3d4ff43d 2711 d_drop(ctx->dentry);
c6d00e63 2712 nfs4_opendata_put(opendata);
864472e9 2713 return ret;
1da177e4
LT
2714}
2715
a9ed2e25 2716static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
202b50dc 2717{
539cd03a 2718 struct nfs_server *server = NFS_SERVER(state->inode);
202b50dc
TM
2719 struct nfs4_exception exception = { };
2720 int err;
2721
2722 do {
539cd03a 2723 err = _nfs4_open_expired(ctx, state);
42113a75 2724 trace_nfs4_open_expired(ctx, 0, err);
49f9a0fa
TM
2725 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2726 continue;
a9ed2e25
TM
2727 switch (err) {
2728 default:
2729 goto out;
2730 case -NFS4ERR_GRACE:
2731 case -NFS4ERR_DELAY:
2732 nfs4_handle_exception(server, err, &exception);
2733 err = 0;
2734 }
202b50dc 2735 } while (exception.retry);
a9ed2e25 2736out:
202b50dc
TM
2737 return err;
2738}
2739
1da177e4
LT
2740static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2741{
1da177e4 2742 struct nfs_open_context *ctx;
864472e9 2743 int ret;
1da177e4 2744
864472e9
TM
2745 ctx = nfs4_state_find_open_context(state);
2746 if (IS_ERR(ctx))
91876b13 2747 return -EAGAIN;
539cd03a 2748 ret = nfs4_do_open_expired(ctx, state);
864472e9
TM
2749 put_nfs_open_context(ctx);
2750 return ret;
1da177e4
LT
2751}
2752
41020b67
TM
2753static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2754 const nfs4_stateid *stateid)
4dfd4f7a 2755{
41020b67 2756 nfs_remove_bad_delegation(state->inode, stateid);
9f0c5124 2757 nfs_state_clear_delegation(state);
4dfd4f7a
TM
2758}
2759
2760static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2761{
2762 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
41020b67 2763 nfs_finish_clear_delegation_stateid(state, NULL);
4dfd4f7a
TM
2764}
2765
2766static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2767{
2768 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2769 nfs40_clear_delegation_stateid(state);
27a30cf6 2770 nfs_state_clear_open_state_flags(state);
4dfd4f7a
TM
2771 return nfs4_open_expired(sp, state);
2772}
2773
45870d69
TM
2774static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2775 nfs4_stateid *stateid,
a52458b4 2776 const struct cred *cred)
45870d69
TM
2777{
2778 return -NFS4ERR_BAD_STATEID;
2779}
2780
f062eb6c 2781#if defined(CONFIG_NFS_V4_1)
4586f6e2
TM
2782static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2783 nfs4_stateid *stateid,
a52458b4 2784 const struct cred *cred)
4586f6e2
TM
2785{
2786 int status;
2787
f7a62ada
TM
2788 switch (stateid->type) {
2789 default:
2790 break;
2791 case NFS4_INVALID_STATEID_TYPE:
2792 case NFS4_SPECIAL_STATEID_TYPE:
2793 return -NFS4ERR_BAD_STATEID;
2794 case NFS4_REVOKED_STATEID_TYPE:
2795 goto out_free;
2796 }
4586f6e2 2797
f7a62ada 2798 status = nfs41_test_stateid(server, stateid, cred);
4586f6e2
TM
2799 switch (status) {
2800 case -NFS4ERR_EXPIRED:
2801 case -NFS4ERR_ADMIN_REVOKED:
2802 case -NFS4ERR_DELEG_REVOKED:
f7a62ada
TM
2803 break;
2804 default:
4586f6e2
TM
2805 return status;
2806 }
f7a62ada
TM
2807out_free:
2808 /* Ack the revoked state to the server */
f0b0bf88 2809 nfs41_free_stateid(server, stateid, cred, true);
f7a62ada 2810 return -NFS4ERR_EXPIRED;
4586f6e2
TM
2811}
2812
27a30cf6 2813static int nfs41_check_delegation_stateid(struct nfs4_state *state)
f062eb6c 2814{
f062eb6c 2815 struct nfs_server *server = NFS_SERVER(state->inode);
0c116cad 2816 nfs4_stateid stateid;
ab7cb0df 2817 struct nfs_delegation *delegation;
a52458b4 2818 const struct cred *cred = NULL;
27a30cf6 2819 int status, ret = NFS_OK;
3e60ffdd 2820
ab7cb0df
TM
2821 /* Get the delegation credential for use by test/free_stateid */
2822 rcu_read_lock();
2823 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
0c116cad 2824 if (delegation == NULL) {
ab7cb0df 2825 rcu_read_unlock();
9f0c5124 2826 nfs_state_clear_delegation(state);
27a30cf6 2827 return NFS_OK;
0c116cad
TM
2828 }
2829
fc51b1cf 2830 spin_lock(&delegation->lock);
0c116cad 2831 nfs4_stateid_copy(&stateid, &delegation->stateid);
0c116cad 2832
994b15b9
TM
2833 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2834 &delegation->flags)) {
fc51b1cf 2835 spin_unlock(&delegation->lock);
4c8e5447 2836 rcu_read_unlock();
27a30cf6 2837 return NFS_OK;
4c8e5447 2838 }
0c116cad 2839
a52458b4
N
2840 if (delegation->cred)
2841 cred = get_cred(delegation->cred);
fc51b1cf 2842 spin_unlock(&delegation->lock);
0c116cad 2843 rcu_read_unlock();
4586f6e2 2844 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
0c116cad 2845 trace_nfs4_test_delegation_stateid(state, NULL, status);
f7a62ada 2846 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
41020b67 2847 nfs_finish_clear_delegation_stateid(state, &stateid);
27a30cf6
TM
2848 else
2849 ret = status;
ab7cb0df 2850
8c39a39e 2851 put_cred(cred);
27a30cf6
TM
2852 return ret;
2853}
2854
2855static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2856{
2857 nfs4_stateid tmp;
2858
2859 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2860 nfs4_copy_delegation_stateid(state->inode, state->state,
2861 &tmp, NULL) &&
2862 nfs4_stateid_match_other(&state->stateid, &tmp))
2863 nfs_state_set_delegation(state, &tmp, state->state);
2864 else
2865 nfs_state_clear_delegation(state);
3e60ffdd
CL
2866}
2867
c5896fc8
TM
2868/**
2869 * nfs41_check_expired_locks - possibly free a lock stateid
2870 *
2871 * @state: NFSv4 state for an inode
2872 *
2873 * Returns NFS_OK if recovery for this stateid is now finished.
2874 * Otherwise a negative NFS4ERR value is returned.
2875 */
2876static int nfs41_check_expired_locks(struct nfs4_state *state)
2877{
2878 int status, ret = NFS_OK;
d75a6a0e 2879 struct nfs4_lock_state *lsp, *prev = NULL;
c5896fc8
TM
2880 struct nfs_server *server = NFS_SERVER(state->inode);
2881
2882 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2883 goto out;
d75a6a0e
BC
2884
2885 spin_lock(&state->state_lock);
c5896fc8
TM
2886 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2887 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
a52458b4 2888 const struct cred *cred = lsp->ls_state->owner->so_cred;
c5896fc8 2889
194bc1f4 2890 refcount_inc(&lsp->ls_count);
d75a6a0e
BC
2891 spin_unlock(&state->state_lock);
2892
2893 nfs4_put_lock_state(prev);
2894 prev = lsp;
2895
c5896fc8
TM
2896 status = nfs41_test_and_free_expired_stateid(server,
2897 &lsp->ls_stateid,
2898 cred);
2899 trace_nfs4_test_lock_stateid(state, lsp, status);
2900 if (status == -NFS4ERR_EXPIRED ||
2901 status == -NFS4ERR_BAD_STATEID) {
2902 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
67dd4830 2903 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
c5896fc8
TM
2904 if (!recover_lost_locks)
2905 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2906 } else if (status != NFS_OK) {
2907 ret = status;
d75a6a0e
BC
2908 nfs4_put_lock_state(prev);
2909 goto out;
c5896fc8 2910 }
d75a6a0e 2911 spin_lock(&state->state_lock);
c5896fc8 2912 }
d75a6a0e
BC
2913 }
2914 spin_unlock(&state->state_lock);
2915 nfs4_put_lock_state(prev);
c5896fc8
TM
2916out:
2917 return ret;
2918}
2919
3e60ffdd
CL
2920/**
2921 * nfs41_check_open_stateid - possibly free an open stateid
2922 *
2923 * @state: NFSv4 state for an inode
2924 *
2925 * Returns NFS_OK if recovery for this stateid is now finished.
2926 * Otherwise a negative NFS4ERR value is returned.
2927 */
2928static int nfs41_check_open_stateid(struct nfs4_state *state)
2929{
2930 struct nfs_server *server = NFS_SERVER(state->inode);
fcb6d9c6 2931 nfs4_stateid *stateid = &state->open_stateid;
a52458b4 2932 const struct cred *cred = state->owner->so_cred;
3e60ffdd
CL
2933 int status;
2934
27a30cf6 2935 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
3e60ffdd 2936 return -NFS4ERR_BAD_STATEID;
4586f6e2 2937 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
08cb47fa 2938 trace_nfs4_test_open_stateid(state, NULL, status);
f7a62ada 2939 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
27a30cf6 2940 nfs_state_clear_open_state_flags(state);
67dd4830 2941 stateid->type = NFS4_INVALID_STATEID_TYPE;
8a64c4ef 2942 return status;
c0ca0e59 2943 }
8a64c4ef
TM
2944 if (nfs_open_stateid_recover_openmode(state))
2945 return -NFS4ERR_OPENMODE;
2946 return NFS_OK;
b01dd1d8
BS
2947}
2948
2949static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2950{
eb64cf96 2951 int status;
b01dd1d8 2952
27a30cf6
TM
2953 status = nfs41_check_delegation_stateid(state);
2954 if (status != NFS_OK)
2955 return status;
2956 nfs41_delegation_recover_stateid(state);
2957
c5896fc8
TM
2958 status = nfs41_check_expired_locks(state);
2959 if (status != NFS_OK)
2960 return status;
3e60ffdd 2961 status = nfs41_check_open_stateid(state);
eb64cf96
CL
2962 if (status != NFS_OK)
2963 status = nfs4_open_expired(sp, state);
2964 return status;
f062eb6c
BS
2965}
2966#endif
2967
aa53ed54
JL
2968/*
2969 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2970 * fields corresponding to attributes that were used to store the verifier.
2971 * Make sure we clobber those fields in the later setattr call
2972 */
609339c1 2973static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
5334c5bd 2974 struct iattr *sattr, struct nfs4_label **label)
aa53ed54 2975{
609339c1
TM
2976 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2977 __u32 attrset[3];
2978 unsigned ret;
2979 unsigned i;
5334c5bd 2980
609339c1
TM
2981 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2982 attrset[i] = opendata->o_res.attrset[i];
2983 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2984 attrset[i] &= ~bitmask[i];
2985 }
2986
2987 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2988 sattr->ia_valid : 0;
aa53ed54 2989
609339c1
TM
2990 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2991 if (sattr->ia_valid & ATTR_ATIME_SET)
2992 ret |= ATTR_ATIME_SET;
2993 else
2994 ret |= ATTR_ATIME;
2995 }
5334c5bd 2996
609339c1
TM
2997 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2998 if (sattr->ia_valid & ATTR_MTIME_SET)
2999 ret |= ATTR_MTIME_SET;
3000 else
3001 ret |= ATTR_MTIME;
3002 }
5334c5bd 3003
609339c1 3004 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
5334c5bd 3005 *label = NULL;
609339c1 3006 return ret;
aa53ed54
JL
3007}
3008
c21443c2 3009static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1bf85d8c 3010 int flags, struct nfs_open_context *ctx)
c21443c2
TM
3011{
3012 struct nfs4_state_owner *sp = opendata->owner;
3013 struct nfs_server *server = sp->so_server;
275bb307 3014 struct dentry *dentry;
c21443c2 3015 struct nfs4_state *state;
1bf85d8c 3016 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
cf5b4059
TM
3017 struct inode *dir = d_inode(opendata->dir);
3018 unsigned long dir_verifier;
c21443c2
TM
3019 unsigned int seq;
3020 int ret;
3021
3022 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
cf5b4059 3023 dir_verifier = nfs_save_change_attribute(dir);
c21443c2 3024
3b65a30d 3025 ret = _nfs4_proc_open(opendata, ctx);
dca78001 3026 if (ret != 0)
c21443c2
TM
3027 goto out;
3028
ae55e59d 3029 state = _nfs4_opendata_to_nfs4_state(opendata);
c21443c2
TM
3030 ret = PTR_ERR(state);
3031 if (IS_ERR(state))
3032 goto out;
a974deee 3033 ctx->state = state;
c21443c2
TM
3034 if (server->caps & NFS_CAP_POSIX_LOCK)
3035 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
a8ce377a
JL
3036 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3037 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
c21443c2 3038
275bb307 3039 dentry = opendata->dentry;
2b0143b5 3040 if (d_really_is_negative(dentry)) {
668d0cd5 3041 struct dentry *alias;
275bb307 3042 d_drop(dentry);
668d0cd5
AV
3043 alias = d_exact_alias(dentry, state->inode);
3044 if (!alias)
3045 alias = d_splice_alias(igrab(state->inode), dentry);
3046 /* d_splice_alias() can't fail here - it's a non-directory */
3047 if (alias) {
275bb307 3048 dput(ctx->dentry);
668d0cd5 3049 ctx->dentry = dentry = alias;
275bb307 3050 }
cf5b4059
TM
3051 }
3052
3053 switch(opendata->o_arg.claim) {
3054 default:
3055 break;
3056 case NFS4_OPEN_CLAIM_NULL:
3057 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3058 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3059 if (!opendata->rpc_done)
3060 break;
3061 if (opendata->o_res.delegation_type != 0)
3062 dir_verifier = nfs_save_change_attribute(dir);
3063 nfs_set_verifier(dentry, dir_verifier);
275bb307
TM
3064 }
3065
af9b6d75
TM
3066 /* Parse layoutget results before we check for access */
3067 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3068
1bf85d8c
TM
3069 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3070 acc_mode, flags);
c21443c2
TM
3071 if (ret != 0)
3072 goto out;
3073
2b0143b5 3074 if (d_inode(dentry) == state->inode) {
c45ffdd2
TM
3075 nfs_inode_attach_open_context(ctx);
3076 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3077 nfs4_schedule_stateid_recovery(server, state);
3078 }
2409a976 3079
c21443c2 3080out:
0cb98abb
OK
3081 if (!opendata->cancelled)
3082 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
c21443c2
TM
3083 return ret;
3084}
3085
1da177e4 3086/*
24ac23ab 3087 * Returns a referenced nfs4_state
1da177e4 3088 */
82be417a 3089static int _nfs4_do_open(struct inode *dir,
4197a055 3090 struct nfs_open_context *ctx,
82be417a 3091 int flags,
8fd1ab74 3092 const struct nfs4_open_createattrs *c,
5bc2afc2 3093 int *opened)
1da177e4
LT
3094{
3095 struct nfs4_state_owner *sp;
3096 struct nfs4_state *state = NULL;
3097 struct nfs_server *server = NFS_SERVER(dir);
e56e0b78 3098 struct nfs4_opendata *opendata;
4197a055 3099 struct dentry *dentry = ctx->dentry;
a52458b4 3100 const struct cred *cred = ctx->cred;
4197a055 3101 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
1bf85d8c 3102 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
49f9a0fa 3103 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
8fd1ab74
TM
3104 struct iattr *sattr = c->sattr;
3105 struct nfs4_label *label = c->label;
1775fd3e 3106 struct nfs4_label *olabel = NULL;
aac00a8d 3107 int status;
1da177e4
LT
3108
3109 /* Protect against reboot recovery conflicts */
1da177e4 3110 status = -ENOMEM;
d1e284d5
TM
3111 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3112 if (sp == NULL) {
1da177e4
LT
3113 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3114 goto out_err;
3115 }
334f87dd 3116 status = nfs4_client_recover_expired_lease(server->nfs_client);
58d9714a 3117 if (status != 0)
b4454fe1 3118 goto err_put_state_owner;
2b0143b5
DH
3119 if (d_really_is_positive(dentry))
3120 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
58d9714a 3121 status = -ENOMEM;
2b0143b5 3122 if (d_really_is_positive(dentry))
49f9a0fa 3123 claim = NFS4_OPEN_CLAIM_FH;
8fd1ab74
TM
3124 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3125 c, claim, GFP_KERNEL);
e56e0b78 3126 if (opendata == NULL)
95d35cb4 3127 goto err_put_state_owner;
1da177e4 3128
14c43f76
DQ
3129 if (label) {
3130 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3131 if (IS_ERR(olabel)) {
3132 status = PTR_ERR(olabel);
3133 goto err_opendata_put;
3134 }
3135 }
3136
e911b815
TM
3137 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3138 if (!opendata->f_attr.mdsthreshold) {
3139 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3140 if (!opendata->f_attr.mdsthreshold)
3141 goto err_free_label;
3142 }
1549210f 3143 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
82be417a 3144 }
2b0143b5
DH
3145 if (d_really_is_positive(dentry))
3146 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
aac00a8d 3147
1bf85d8c 3148 status = _nfs4_open_and_get_state(opendata, flags, ctx);
6168f62c 3149 if (status != 0)
14c43f76 3150 goto err_free_label;
3efb9722 3151 state = ctx->state;
6168f62c 3152
efcbc04e 3153 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
549b19cc 3154 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
609339c1 3155 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
a1d1c4f1
TM
3156 /*
3157 * send create attributes which was not set by open
3158 * with an extra setattr.
3159 */
609339c1
TM
3160 if (attrs || label) {
3161 unsigned ia_old = sattr->ia_valid;
3162
3163 sattr->ia_valid = attrs;
a1d1c4f1
TM
3164 nfs_fattr_init(opendata->o_res.f_attr);
3165 status = nfs4_do_setattr(state->inode, cred,
3166 opendata->o_res.f_attr, sattr,
29b59f94 3167 ctx, label, olabel);
a1d1c4f1
TM
3168 if (status == 0) {
3169 nfs_setattr_update_inode(state->inode, sattr,
3170 opendata->o_res.f_attr);
3171 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3172 }
609339c1 3173 sattr->ia_valid = ia_old;
1775fd3e 3174 }
0ab64e0e 3175 }
c5c3fb5f 3176 if (opened && opendata->file_created)
73a09dd9 3177 *opened = 1;
82be417a 3178
e911b815 3179 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
82be417a 3180 *ctx_th = opendata->f_attr.mdsthreshold;
e911b815
TM
3181 opendata->f_attr.mdsthreshold = NULL;
3182 }
82be417a 3183
14c43f76
DQ
3184 nfs4_label_free(olabel);
3185
c6d00e63 3186 nfs4_opendata_put(opendata);
1da177e4 3187 nfs4_put_state_owner(sp);
1da177e4 3188 return 0;
14c43f76
DQ
3189err_free_label:
3190 nfs4_label_free(olabel);
c6d00e63
TM
3191err_opendata_put:
3192 nfs4_opendata_put(opendata);
e56e0b78
TM
3193err_put_state_owner:
3194 nfs4_put_state_owner(sp);
1da177e4 3195out_err:
1da177e4
LT
3196 return status;
3197}
3198
3199
82be417a 3200static struct nfs4_state *nfs4_do_open(struct inode *dir,
4197a055 3201 struct nfs_open_context *ctx,
82be417a
AA
3202 int flags,
3203 struct iattr *sattr,
5bc2afc2
TM
3204 struct nfs4_label *label,
3205 int *opened)
1da177e4 3206{
49f9a0fa 3207 struct nfs_server *server = NFS_SERVER(dir);
0688e64b
TM
3208 struct nfs4_exception exception = {
3209 .interruptible = true,
3210 };
1da177e4 3211 struct nfs4_state *res;
8fd1ab74
TM
3212 struct nfs4_open_createattrs c = {
3213 .label = label,
3214 .sattr = sattr,
3215 .verf = {
3216 [0] = (__u32)jiffies,
3217 [1] = (__u32)current->pid,
3218 },
3219 };
1da177e4
LT
3220 int status;
3221
3222 do {
8fd1ab74 3223 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3efb9722 3224 res = ctx->state;
42113a75 3225 trace_nfs4_open_file(ctx, flags, status);
1da177e4
LT
3226 if (status == 0)
3227 break;
3228 /* NOTE: BAD_SEQID means the server and client disagree about the
3229 * book-keeping w.r.t. state-changing operations
3230 * (OPEN/CLOSE/LOCK/LOCKU...)
3231 * It is actually a sign of a bug on the client or on the server.
3232 *
3233 * If we receive a BAD_SEQID error in the particular case of
cee54fc9 3234 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1da177e4
LT
3235 * have unhashed the old state_owner for us, and that we can
3236 * therefore safely retry using a new one. We should still warn
3237 * the user though...
3238 */
3239 if (status == -NFS4ERR_BAD_SEQID) {
9a3ba432 3240 pr_warn_ratelimited("NFS: v4 server %s "
6f43ddcc
TM
3241 " returned a bad sequence-id error!\n",
3242 NFS_SERVER(dir)->nfs_client->cl_hostname);
1da177e4
LT
3243 exception.retry = 1;
3244 continue;
3245 }
550f5747
TM
3246 /*
3247 * BAD_STATEID on OPEN means that the server cancelled our
3248 * state before it received the OPEN_CONFIRM.
3249 * Recover by retrying the request as per the discussion
3250 * on Page 181 of RFC3530.
3251 */
3252 if (status == -NFS4ERR_BAD_STATEID) {
3253 exception.retry = 1;
3254 continue;
3255 }
924491f2
RM
3256 if (status == -NFS4ERR_EXPIRED) {
3257 nfs4_schedule_lease_recovery(server->nfs_client);
3258 exception.retry = 1;
3259 continue;
3260 }
aac00a8d
TM
3261 if (status == -EAGAIN) {
3262 /* We must have found a delegation */
3263 exception.retry = 1;
3264 continue;
3265 }
49f9a0fa
TM
3266 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3267 continue;
3268 res = ERR_PTR(nfs4_handle_exception(server,
1da177e4
LT
3269 status, &exception));
3270 } while (exception.retry);
3271 return res;
3272}
3273
8487c479
TM
3274static int _nfs4_do_setattr(struct inode *inode,
3275 struct nfs_setattrargs *arg,
3276 struct nfs_setattrres *res,
a52458b4 3277 const struct cred *cred,
29b59f94 3278 struct nfs_open_context *ctx)
1da177e4 3279{
3e4f6290 3280 struct nfs_server *server = NFS_SERVER(inode);
d9b67e1e 3281 struct rpc_message msg = {
659bfcd6 3282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
8487c479
TM
3283 .rpc_argp = arg,
3284 .rpc_resp = res,
659bfcd6 3285 .rpc_cred = cred,
d9b67e1e 3286 };
a52458b4 3287 const struct cred *delegation_cred = NULL;
26e976a8 3288 unsigned long timestamp = jiffies;
ee3ae84e 3289 bool truncate;
65e4308d 3290 int status;
1da177e4 3291
8487c479 3292 nfs_fattr_init(res->fattr);
1da177e4 3293
ee3ae84e 3294 /* Servers should only apply open mode checks for file size changes */
8487c479 3295 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
644c9f40
CL
3296 if (!truncate) {
3297 nfs4_inode_make_writeable(inode);
991eedb1 3298 goto zero_stateid;
644c9f40 3299 }
ee3ae84e 3300
991eedb1 3301 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
ee3ae84e 3302 /* Use that stateid */
09a54f0e 3303 } else if (ctx != NULL && ctx->state) {
17393475 3304 struct nfs_lock_context *l_ctx;
29b59f94 3305 if (!nfs4_valid_open_stateid(ctx->state))
0418dae1 3306 return -EBADF;
17393475
N
3307 l_ctx = nfs_get_lock_context(ctx);
3308 if (IS_ERR(l_ctx))
3309 return PTR_ERR(l_ctx);
7a0566b3
N
3310 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3311 &arg->stateid, &delegation_cred);
3312 nfs_put_lock_context(l_ctx);
3313 if (status == -EIO)
0418dae1 3314 return -EBADF;
d826e5b8
OK
3315 else if (status == -EAGAIN)
3316 goto zero_stateid;
991eedb1
TM
3317 } else {
3318zero_stateid:
8487c479 3319 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
991eedb1 3320 }
abf4e13c
TM
3321 if (delegation_cred)
3322 msg.rpc_cred = delegation_cred;
1da177e4 3323
8487c479 3324 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
abf4e13c 3325
a52458b4 3326 put_cred(delegation_cred);
29b59f94 3327 if (status == 0 && ctx != NULL)
26e976a8 3328 renew_lease(server, timestamp);
8487c479 3329 trace_nfs4_setattr(inode, &arg->stateid, status);
65e4308d 3330 return status;
1da177e4
LT
3331}
3332
a52458b4 3333static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
659bfcd6 3334 struct nfs_fattr *fattr, struct iattr *sattr,
29b59f94 3335 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
1775fd3e 3336 struct nfs4_label *olabel)
1da177e4 3337{
3e4f6290 3338 struct nfs_server *server = NFS_SERVER(inode);
30846df0 3339 __u32 bitmask[NFS4_BITMASK_SZ];
29b59f94 3340 struct nfs4_state *state = ctx ? ctx->state : NULL;
d9b67e1e
AS
3341 struct nfs_setattrargs arg = {
3342 .fh = NFS_FH(inode),
3343 .iap = sattr,
8487c479 3344 .server = server,
30846df0 3345 .bitmask = bitmask,
8487c479 3346 .label = ilabel,
d9b67e1e
AS
3347 };
3348 struct nfs_setattrres res = {
8487c479
TM
3349 .fattr = fattr,
3350 .label = olabel,
3351 .server = server,
d9b67e1e 3352 };
a1d0b5ee
TM
3353 struct nfs4_exception exception = {
3354 .state = state,
3114ea7a 3355 .inode = inode,
8487c479 3356 .stateid = &arg.stateid,
a1d0b5ee 3357 };
1da177e4 3358 int err;
8487c479 3359
1da177e4 3360 do {
30846df0
TM
3361 nfs4_bitmap_copy_adjust_setattr(bitmask,
3362 nfs4_bitmask(server, olabel),
3363 inode);
3364
29b59f94 3365 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
451146be
TM
3366 switch (err) {
3367 case -NFS4ERR_OPENMODE:
721ccfb7
TM
3368 if (!(sattr->ia_valid & ATTR_SIZE)) {
3369 pr_warn_once("NFSv4: server %s is incorrectly "
3370 "applying open mode checks to "
3371 "a SETATTR that is not "
3372 "changing file size.\n",
3373 server->nfs_client->cl_hostname);
3374 }
451146be
TM
3375 if (state && !(state->state & FMODE_WRITE)) {
3376 err = -EBADF;
3377 if (sattr->ia_valid & ATTR_OPEN)
3378 err = -EACCES;
3379 goto out;
3380 }
3381 }
3382 err = nfs4_handle_exception(server, err, &exception);
1da177e4 3383 } while (exception.retry);
451146be 3384out:
1da177e4
LT
3385 return err;
3386}
3387
500d701f
PT
3388static bool
3389nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3390{
3391 if (inode == NULL || !nfs_have_layout(inode))
3392 return false;
3393
3394 return pnfs_wait_on_layoutreturn(inode, task);
3395}
3396
0e0cb35b
TM
3397/*
3398 * Update the seqid of an open stateid
3399 */
3400static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3401 struct nfs4_state *state)
3402{
3403 __be32 seqid_open;
3404 u32 dst_seqid;
3405 int seq;
3406
3407 for (;;) {
3408 if (!nfs4_valid_open_stateid(state))
3409 break;
3410 seq = read_seqbegin(&state->seqlock);
3411 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3412 nfs4_stateid_copy(dst, &state->open_stateid);
3413 if (read_seqretry(&state->seqlock, seq))
3414 continue;
3415 break;
3416 }
3417 seqid_open = state->open_stateid.seqid;
3418 if (read_seqretry(&state->seqlock, seq))
3419 continue;
3420
3421 dst_seqid = be32_to_cpu(dst->seqid);
3422 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3423 dst->seqid = seqid_open;
3424 break;
3425 }
3426}
3427
3428/*
3429 * Update the seqid of an open stateid after receiving
3430 * NFS4ERR_OLD_STATEID
3431 */
3432static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3433 struct nfs4_state *state)
3434{
3435 __be32 seqid_open;
3436 u32 dst_seqid;
3437 bool ret;
b4868b44
BC
3438 int seq, status = -EAGAIN;
3439 DEFINE_WAIT(wait);
0e0cb35b
TM
3440
3441 for (;;) {
3442 ret = false;
3443 if (!nfs4_valid_open_stateid(state))
3444 break;
3445 seq = read_seqbegin(&state->seqlock);
3446 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3447 if (read_seqretry(&state->seqlock, seq))
3448 continue;
3449 break;
3450 }
b4868b44
BC
3451
3452 write_seqlock(&state->seqlock);
0e0cb35b 3453 seqid_open = state->open_stateid.seqid;
0e0cb35b
TM
3454
3455 dst_seqid = be32_to_cpu(dst->seqid);
b4868b44
BC
3456
3457 /* Did another OPEN bump the state's seqid? try again: */
3458 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
0e0cb35b 3459 dst->seqid = seqid_open;
b4868b44
BC
3460 write_sequnlock(&state->seqlock);
3461 ret = true;
3462 break;
3463 }
3464
3465 /* server says we're behind but we haven't seen the update yet */
3466 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3467 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3468 write_sequnlock(&state->seqlock);
3469 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3470
3471 if (signal_pending(current))
3472 status = -EINTR;
3473 else
3474 if (schedule_timeout(5*HZ) != 0)
3475 status = 0;
3476
3477 finish_wait(&state->waitq, &wait);
3478
3479 if (!status)
3480 continue;
3481 if (status == -EINTR)
3482 break;
3483
3484 /* we slept the whole 5 seconds, we must have lost a seqid */
3485 dst->seqid = cpu_to_be32(dst_seqid + 1);
0e0cb35b
TM
3486 ret = true;
3487 break;
3488 }
3489
3490 return ret;
3491}
3492
1da177e4
LT
3493struct nfs4_closedata {
3494 struct inode *inode;
3495 struct nfs4_state *state;
3496 struct nfs_closeargs arg;
3497 struct nfs_closeres res;
cf805165
TM
3498 struct {
3499 struct nfs4_layoutreturn_args arg;
3500 struct nfs4_layoutreturn_res res;
4d796d75 3501 struct nfs4_xdr_opaque_data ld_private;
cf805165
TM
3502 u32 roc_barrier;
3503 bool roc;
3504 } lr;
516a6af6 3505 struct nfs_fattr fattr;
26e976a8 3506 unsigned long timestamp;
1da177e4
LT
3507};
3508
963d8fe5 3509static void nfs4_free_closedata(void *data)
9512135d 3510{
963d8fe5
TM
3511 struct nfs4_closedata *calldata = data;
3512 struct nfs4_state_owner *sp = calldata->state->owner;
643168c2 3513 struct super_block *sb = calldata->state->inode->i_sb;
9512135d 3514
cf805165 3515 if (calldata->lr.roc)
1c5bd76d
TM
3516 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3517 calldata->res.lr_ret);
9512135d
TM
3518 nfs4_put_open_state(calldata->state);
3519 nfs_free_seqid(calldata->arg.seqid);
9512135d 3520 nfs4_put_state_owner(sp);
322b2b90 3521 nfs_sb_deactive(sb);
9512135d
TM
3522 kfree(calldata);
3523}
3524
963d8fe5 3525static void nfs4_close_done(struct rpc_task *task, void *data)
1da177e4 3526{
963d8fe5 3527 struct nfs4_closedata *calldata = data;
1da177e4 3528 struct nfs4_state *state = calldata->state;
1da177e4 3529 struct nfs_server *server = NFS_SERVER(calldata->inode);
412f6c4c 3530 nfs4_stateid *res_stateid = NULL;
b8b8d221
TM
3531 struct nfs4_exception exception = {
3532 .state = state,
3533 .inode = calldata->inode,
3534 .stateid = &calldata->arg.stateid,
3535 };
1da177e4 3536
a3ca5651 3537 dprintk("%s: begin!\n", __func__);
14516c3a
TM
3538 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3539 return;
42113a75 3540 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
cf805165
TM
3541
3542 /* Handle Layoutreturn errors */
078000d0
TM
3543 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3544 &calldata->res.lr_ret) == -EAGAIN)
287a9c55 3545 goto out_restart;
cf805165 3546
d9b67e1e 3547 /* hmm. we are done with the inode, and in the process of freeing
1da177e4
LT
3548 * the state_owner. we keep this around to process errors
3549 */
1da177e4
LT
3550 switch (task->tk_status) {
3551 case 0:
412f6c4c 3552 res_stateid = &calldata->res.stateid;
26e976a8 3553 renew_lease(server, calldata->timestamp);
412f6c4c 3554 break;
f07d4a31
TM
3555 case -NFS4ERR_ACCESS:
3556 if (calldata->arg.bitmask != NULL) {
3557 calldata->arg.bitmask = NULL;
3558 calldata->res.fattr = NULL;
91b30d2e 3559 goto out_restart;
f07d4a31
TM
3560
3561 }
3562 break;
12f275cd
TM
3563 case -NFS4ERR_OLD_STATEID:
3564 /* Did we race with OPEN? */
0e0cb35b 3565 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
91b30d2e
TM
3566 state))
3567 goto out_restart;
12f275cd 3568 goto out_release;
69794ad7 3569 case -NFS4ERR_ADMIN_REVOKED:
1da177e4 3570 case -NFS4ERR_STALE_STATEID:
26d36301
TM
3571 case -NFS4ERR_EXPIRED:
3572 nfs4_free_revoked_stateid(server,
3573 &calldata->arg.stateid,
3574 task->tk_msg.rpc_cred);
df561f66 3575 fallthrough;
9e33bed5 3576 case -NFS4ERR_BAD_STATEID:
e217e825
TM
3577 if (calldata->arg.fmode == 0)
3578 break;
df561f66 3579 fallthrough;
1da177e4 3580 default:
b8b8d221
TM
3581 task->tk_status = nfs4_async_handle_exception(task,
3582 server, task->tk_status, &exception);
3583 if (exception.retry)
91b30d2e 3584 goto out_restart;
1da177e4 3585 }
4a1e2feb
TM
3586 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3587 res_stateid, calldata->arg.fmode);
69794ad7 3588out_release:
91b30d2e 3589 task->tk_status = 0;
72211dbe 3590 nfs_release_seqid(calldata->arg.seqid);
d8d84983 3591 nfs_refresh_inode(calldata->inode, &calldata->fattr);
a3ca5651 3592 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
91b30d2e 3593 return;
91b30d2e
TM
3594out_restart:
3595 task->tk_status = 0;
3596 rpc_restart_call_prepare(task);
3597 goto out_release;
1da177e4
LT
3598}
3599
4ce70ada 3600static void nfs4_close_prepare(struct rpc_task *task, void *data)
1da177e4 3601{
4ce70ada 3602 struct nfs4_closedata *calldata = data;
9512135d 3603 struct nfs4_state *state = calldata->state;
7fdab069 3604 struct inode *inode = calldata->inode;
c8bf7073 3605 struct pnfs_layout_hdr *lo;
aee7af35 3606 bool is_rdonly, is_wronly, is_rdwr;
88069f77 3607 int call_close = 0;
9512135d 3608
a3ca5651 3609 dprintk("%s: begin!\n", __func__);
963d8fe5 3610 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
c8da19b9 3611 goto out_wait;
003707c7 3612
88069f77 3613 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
4cecb76f 3614 spin_lock(&state->owner->so_lock);
aee7af35
TM
3615 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3616 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3617 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
003707c7 3618 /* Calculate the change in open mode */
cd9288ff 3619 calldata->arg.fmode = 0;
e7616923 3620 if (state->n_rdwr == 0) {
cd9288ff
TM
3621 if (state->n_rdonly == 0)
3622 call_close |= is_rdonly;
3623 else if (is_rdonly)
3624 calldata->arg.fmode |= FMODE_READ;
3625 if (state->n_wronly == 0)
3626 call_close |= is_wronly;
3627 else if (is_wronly)
3628 calldata->arg.fmode |= FMODE_WRITE;
e547f262
TM
3629 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3630 call_close |= is_rdwr;
cd9288ff
TM
3631 } else if (is_rdwr)
3632 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3633
0e0cb35b
TM
3634 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3635 if (!nfs4_valid_open_stateid(state))
5d422301 3636 call_close = 0;
4cecb76f 3637 spin_unlock(&state->owner->so_lock);
88069f77
TM
3638
3639 if (!call_close) {
963d8fe5 3640 /* Note: exit _without_ calling nfs4_close_done */
c8da19b9 3641 goto out_no_action;
9512135d 3642 }
88069f77 3643
1c5bd76d 3644 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
500d701f
PT
3645 nfs_release_seqid(calldata->arg.seqid);
3646 goto out_wait;
3647 }
3648
c8bf7073
TM
3649 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3650 if (lo && !pnfs_layout_is_valid(lo)) {
3651 calldata->arg.lr_args = NULL;
3652 calldata->res.lr_res = NULL;
3653 }
3654
9413a1a1 3655 if (calldata->arg.fmode == 0)
88069f77 3656 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3ecefc92 3657
9413a1a1 3658 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3ecefc92 3659 /* Close-to-open cache consistency revalidation */
76bd5c01 3660 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3ecefc92 3661 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
76bd5c01
OK
3662 nfs4_bitmask_adjust(calldata->arg.bitmask, inode, NFS_SERVER(inode), NULL);
3663 } else
3ecefc92
TM
3664 calldata->arg.bitmask = NULL;
3665 }
3c13cb5b 3666
6ae37339
TM
3667 calldata->arg.share_access =
3668 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3669 calldata->arg.fmode, 0);
88069f77 3670
d8d84983
TM
3671 if (calldata->res.fattr == NULL)
3672 calldata->arg.bitmask = NULL;
3673 else if (calldata->arg.bitmask == NULL)
3674 calldata->res.fattr = NULL;
26e976a8 3675 calldata->timestamp = jiffies;
42e1cca7 3676 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
9d12b216
TM
3677 &calldata->arg.seq_args,
3678 &calldata->res.seq_res,
2240a9e2
TM
3679 task) != 0)
3680 nfs_release_seqid(calldata->arg.seqid);
a3ca5651 3681 dprintk("%s: done!\n", __func__);
c8da19b9
TM
3682 return;
3683out_no_action:
3684 task->tk_action = NULL;
3685out_wait:
3686 nfs4_sequence_done(task, &calldata->res.seq_res);
1da177e4
LT
3687}
3688
963d8fe5 3689static const struct rpc_call_ops nfs4_close_ops = {
4ce70ada 3690 .rpc_call_prepare = nfs4_close_prepare,
963d8fe5
TM
3691 .rpc_call_done = nfs4_close_done,
3692 .rpc_release = nfs4_free_closedata,
3693};
3694
1da177e4
LT
3695/*
3696 * It is possible for data to be read/written from a mem-mapped file
3697 * after the sys_close call (which hits the vfs layer as a flush).
3698 * This means that we can't safely call nfsv4 close on a file until
3699 * the inode is cleared. This in turn means that we are not good
3700 * NFSv4 citizens - we do not indicate to the server to update the file's
3701 * share state even when we are done with one of the three share
3702 * stateid's in the inode.
3703 *
3704 * NOTE: Caller must be holding the sp->so_owner semaphore!
3705 */
1f7977c1 3706int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
1da177e4 3707{
4a35bd41 3708 struct nfs_server *server = NFS_SERVER(state->inode);
63f5f796 3709 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1da177e4 3710 struct nfs4_closedata *calldata;
b39e625b
TM
3711 struct nfs4_state_owner *sp = state->owner;
3712 struct rpc_task *task;
5138fde0
TM
3713 struct rpc_message msg = {
3714 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3715 .rpc_cred = state->owner->so_cred,
3716 };
c970aa85
TM
3717 struct rpc_task_setup task_setup_data = {
3718 .rpc_client = server->client,
5138fde0 3719 .rpc_message = &msg,
c970aa85 3720 .callback_ops = &nfs4_close_ops,
101070ca 3721 .workqueue = nfsiod_workqueue,
61296507 3722 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
c970aa85 3723 };
9512135d 3724 int status = -ENOMEM;
1da177e4 3725
fa940720
WAA
3726 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3727 &task_setup_data.rpc_client, &msg);
3728
8535b2be 3729 calldata = kzalloc(sizeof(*calldata), gfp_mask);
1da177e4 3730 if (calldata == NULL)
9512135d 3731 goto out;
fba83f34 3732 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
4a35bd41 3733 calldata->inode = state->inode;
1da177e4 3734 calldata->state = state;
4a35bd41 3735 calldata->arg.fh = NFS_FH(state->inode);
c82bac6f
TM
3736 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3737 goto out_free_calldata;
1da177e4 3738 /* Serialization for the sequence id */
63f5f796
TM
3739 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3740 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
badc76dd 3741 if (IS_ERR(calldata->arg.seqid))
9512135d 3742 goto out_free_calldata;
d8d84983 3743 nfs_fattr_init(&calldata->fattr);
dc0b027d 3744 calldata->arg.fmode = 0;
4d796d75 3745 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
516a6af6 3746 calldata->res.fattr = &calldata->fattr;
c1d51931 3747 calldata->res.seqid = calldata->arg.seqid;
516a6af6 3748 calldata->res.server = server;
cf805165 3749 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
1c5bd76d
TM
3750 calldata->lr.roc = pnfs_roc(state->inode,
3751 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3752 if (calldata->lr.roc) {
3753 calldata->arg.lr_args = &calldata->lr.arg;
3754 calldata->res.lr_res = &calldata->lr.res;
3755 }
643168c2 3756 nfs_sb_active(calldata->inode->i_sb);
9512135d 3757
1174dd1f
TM
3758 msg.rpc_argp = &calldata->arg;
3759 msg.rpc_resp = &calldata->res;
c970aa85
TM
3760 task_setup_data.callback_data = calldata;
3761 task = rpc_run_task(&task_setup_data);
b39e625b
TM
3762 if (IS_ERR(task))
3763 return PTR_ERR(task);
a49c3c77
TM
3764 status = 0;
3765 if (wait)
3766 status = rpc_wait_for_completion_task(task);
b39e625b 3767 rpc_put_task(task);
a49c3c77 3768 return status;
9512135d
TM
3769out_free_calldata:
3770 kfree(calldata);
3771out:
b39e625b
TM
3772 nfs4_put_open_state(state);
3773 nfs4_put_state_owner(sp);
9512135d 3774 return status;
1da177e4
LT
3775}
3776
2b484297 3777static struct inode *
5bc2afc2
TM
3778nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3779 int open_flags, struct iattr *attr, int *opened)
1da177e4 3780{
1da177e4 3781 struct nfs4_state *state;
aa9c2669
DQ
3782 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3783
3784 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
1da177e4 3785
565277f6 3786 /* Protect against concurrent sillydeletes */
5bc2afc2 3787 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
aa9c2669
DQ
3788
3789 nfs4_label_release_security(label);
3790
f46e0bd3
TM
3791 if (IS_ERR(state))
3792 return ERR_CAST(state);
275bb307 3793 return state->inode;
1da177e4
LT
3794}
3795
1185a552 3796static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
7fe5c398
TM
3797{
3798 if (ctx->state == NULL)
3799 return;
3800 if (is_sync)
1bf85d8c 3801 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
7fe5c398 3802 else
1bf85d8c 3803 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
7fe5c398 3804}
1da177e4 3805
b944dba3
TM
3806#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3807#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
b78ef845 3808#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
b944dba3 3809
1da177e4
LT
3810static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3811{
8c61282f 3812 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
43652ad5
BH
3813 struct nfs4_server_caps_arg args = {
3814 .fhandle = fhandle,
8c61282f 3815 .bitmask = bitmask,
43652ad5 3816 };
1da177e4
LT
3817 struct nfs4_server_caps_res res = {};
3818 struct rpc_message msg = {
3819 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
43652ad5 3820 .rpc_argp = &args,
1da177e4
LT
3821 .rpc_resp = &res,
3822 };
3823 int status;
f4b23de3 3824 int i;
1da177e4 3825
8c61282f
KM
3826 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3827 FATTR4_WORD0_FH_EXPIRE_TYPE |
3828 FATTR4_WORD0_LINK_SUPPORT |
3829 FATTR4_WORD0_SYMLINK_SUPPORT |
3830 FATTR4_WORD0_ACLSUPPORT;
3831 if (minorversion)
3832 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3833
7c513058 3834 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4 3835 if (status == 0) {
b944dba3 3836 /* Sanity check the server answers */
8c61282f 3837 switch (minorversion) {
b944dba3
TM
3838 case 0:
3839 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3840 res.attr_bitmask[2] = 0;
3841 break;
3842 case 1:
3843 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3844 break;
3845 case 2:
3846 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3847 }
1da177e4 3848 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
62ab460c
TM
3849 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3850 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3851 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3852 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
b944dba3
TM
3853 NFS_CAP_CTIME|NFS_CAP_MTIME|
3854 NFS_CAP_SECURITY_LABEL);
7dd7d959
MN
3855 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3856 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
1da177e4
LT
3857 server->caps |= NFS_CAP_ACLS;
3858 if (res.has_links != 0)
3859 server->caps |= NFS_CAP_HARDLINKS;
3860 if (res.has_symlinks != 0)
3861 server->caps |= NFS_CAP_SYMLINKS;
62ab460c
TM
3862 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3863 server->caps |= NFS_CAP_FILEID;
3864 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3865 server->caps |= NFS_CAP_MODE;
3866 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3867 server->caps |= NFS_CAP_NLINK;
3868 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3869 server->caps |= NFS_CAP_OWNER;
3870 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3871 server->caps |= NFS_CAP_OWNER_GROUP;
3872 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3873 server->caps |= NFS_CAP_ATIME;
3874 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3875 server->caps |= NFS_CAP_CTIME;
3876 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3877 server->caps |= NFS_CAP_MTIME;
aa9c2669
DQ
3878#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3879 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3880 server->caps |= NFS_CAP_SECURITY_LABEL;
3881#endif
3882 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3883 sizeof(server->attr_bitmask));
b944dba3 3884 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
62ab460c 3885
a65318bf
TM
3886 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3887 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3888 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
b944dba3 3889 server->cache_consistency_bitmask[2] = 0;
f4b23de3
TM
3890
3891 /* Avoid a regression due to buggy server */
3892 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3893 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
8c61282f
KM
3894 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3895 sizeof(server->exclcreat_bitmask));
f4b23de3 3896
1da177e4 3897 server->acl_bitmask = res.acl_bitmask;
264e6351 3898 server->fh_expire_type = res.fh_expire_type;
1da177e4 3899 }
cccef3b9 3900
1da177e4
LT
3901 return status;
3902}
3903
55a97593 3904int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1da177e4 3905{
0688e64b
TM
3906 struct nfs4_exception exception = {
3907 .interruptible = true,
3908 };
1da177e4
LT
3909 int err;
3910 do {
3911 err = nfs4_handle_exception(server,
3912 _nfs4_server_capabilities(server, fhandle),
3913 &exception);
3914 } while (exception.retry);
3915 return err;
3916}
3917
3918static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3919 struct nfs_fsinfo *info)
3920{
aa9c2669 3921 u32 bitmask[3];
1da177e4 3922 struct nfs4_lookup_root_arg args = {
aa9c2669 3923 .bitmask = bitmask,
1da177e4
LT
3924 };
3925 struct nfs4_lookup_res res = {
3926 .server = server,
0e574af1 3927 .fattr = info->fattr,
1da177e4
LT
3928 .fh = fhandle,
3929 };
3930 struct rpc_message msg = {
3931 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3932 .rpc_argp = &args,
3933 .rpc_resp = &res,
3934 };
008f55d0 3935
aa9c2669
DQ
3936 bitmask[0] = nfs4_fattr_bitmap[0];
3937 bitmask[1] = nfs4_fattr_bitmap[1];
3938 /*
3939 * Process the label in the upcoming getfattr
3940 */
3941 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3942
0e574af1 3943 nfs_fattr_init(info->fattr);
7c513058 3944 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
3945}
3946
3947static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3948 struct nfs_fsinfo *info)
3949{
0688e64b
TM
3950 struct nfs4_exception exception = {
3951 .interruptible = true,
3952 };
1da177e4
LT
3953 int err;
3954 do {
fb8a5ba8 3955 err = _nfs4_lookup_root(server, fhandle, info);
b5f875a9 3956 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
fb8a5ba8
BS
3957 switch (err) {
3958 case 0:
3959 case -NFS4ERR_WRONGSEC:
05e9cfb4 3960 goto out;
fb8a5ba8
BS
3961 default:
3962 err = nfs4_handle_exception(server, err, &exception);
3963 }
1da177e4 3964 } while (exception.retry);
05e9cfb4 3965out:
1da177e4
LT
3966 return err;
3967}
3968
8f70e95f
BS
3969static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3970 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3971{
c2190661
TM
3972 struct rpc_auth_create_args auth_args = {
3973 .pseudoflavor = flavor,
3974 };
8f70e95f 3975 struct rpc_auth *auth;
8f70e95f 3976
c2190661 3977 auth = rpcauth_create(&auth_args, server->client);
9df1336c
AS
3978 if (IS_ERR(auth))
3979 return -EACCES;
3980 return nfs4_lookup_root(server, fhandle, info);
8f70e95f
BS
3981}
3982
9a744ba3
CL
3983/*
3984 * Retry pseudoroot lookup with various security flavors. We do this when:
3985 *
3986 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3987 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3988 *
3989 * Returns zero on success, or a negative NFS4ERR value, or a
3990 * negative errno value.
3991 */
801a16dc 3992static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
54ceac45 3993 struct nfs_fsinfo *info)
1da177e4 3994{
9a744ba3
CL
3995 /* Per 3530bis 15.33.5 */
3996 static const rpc_authflavor_t flav_array[] = {
3997 RPC_AUTH_GSS_KRB5P,
3998 RPC_AUTH_GSS_KRB5I,
3999 RPC_AUTH_GSS_KRB5,
c4eafe11 4000 RPC_AUTH_UNIX, /* courtesy */
9a744ba3
CL
4001 RPC_AUTH_NULL,
4002 };
4003 int status = -EPERM;
4004 size_t i;
6a1a1e34 4005
4d4b69dd
WAA
4006 if (server->auth_info.flavor_len > 0) {
4007 /* try each flavor specified by user */
4008 for (i = 0; i < server->auth_info.flavor_len; i++) {
4009 status = nfs4_lookup_root_sec(server, fhandle, info,
4010 server->auth_info.flavors[i]);
4011 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4012 continue;
4013 break;
4014 }
4015 } else {
4016 /* no flavors specified by user, try default list */
4017 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4018 status = nfs4_lookup_root_sec(server, fhandle, info,
4019 flav_array[i]);
4020 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4021 continue;
4022 break;
4023 }
8f70e95f 4024 }
9a744ba3 4025
fb8a5ba8 4026 /*
d3787af2 4027 * -EACCES could mean that the user doesn't have correct permissions
fb8a5ba8
BS
4028 * to access the mount. It could also mean that we tried to mount
4029 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4030 * existing mount programs don't handle -EACCES very well so it should
4031 * be mapped to -EPERM instead.
4032 */
4033 if (status == -EACCES)
4034 status = -EPERM;
801a16dc
BS
4035 return status;
4036}
4037
2ed4b95b
CL
4038/**
4039 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4040 * @server: initialized nfs_server handle
4041 * @fhandle: we fill in the pseudo-fs root file handle
4042 * @info: we fill in an FSINFO struct
5e6b1990 4043 * @auth_probe: probe the auth flavours
2ed4b95b
CL
4044 *
4045 * Returns zero on success, or a negative errno.
801a16dc 4046 */
3028eb2b 4047int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
5e6b1990
TM
4048 struct nfs_fsinfo *info,
4049 bool auth_probe)
801a16dc 4050{
c7757074 4051 int status = 0;
2ed4b95b 4052
c7757074 4053 if (!auth_probe)
5e6b1990 4054 status = nfs4_lookup_root(server, fhandle, info);
c7757074
AP
4055
4056 if (auth_probe || status == NFS4ERR_WRONGSEC)
698c937b
TM
4057 status = server->nfs_client->cl_mvops->find_root_sec(server,
4058 fhandle, info);
2ed4b95b 4059
1da177e4
LT
4060 if (status == 0)
4061 status = nfs4_server_capabilities(server, fhandle);
4062 if (status == 0)
4063 status = nfs4_do_fsinfo(server, fhandle, info);
2ed4b95b 4064
c12e87f4 4065 return nfs4_map_errors(status);
1da177e4
LT
4066}
4067
bae36241
BS
4068static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4069 struct nfs_fsinfo *info)
4070{
4071 int error;
4072 struct nfs_fattr *fattr = info->fattr;
779df6a5 4073 struct nfs4_label *label = fattr->label;
bae36241
BS
4074
4075 error = nfs4_server_capabilities(server, mntfh);
4076 if (error < 0) {
4077 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4078 return error;
4079 }
4080
a841b54d 4081 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
bae36241
BS
4082 if (error < 0) {
4083 dprintk("nfs4_get_root: getattr error = %d\n", -error);
779df6a5 4084 goto out;
bae36241
BS
4085 }
4086
4087 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4088 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4089 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4090
779df6a5 4091out:
bae36241
BS
4092 return error;
4093}
4094
6b97fd3d
MN
4095/*
4096 * Get locations and (maybe) other attributes of a referral.
4097 * Note that we'll actually follow the referral later when
4098 * we detect fsid mismatch in inode revalidation
4099 */
f05d147f
BS
4100static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4101 const struct qstr *name, struct nfs_fattr *fattr,
4102 struct nfs_fh *fhandle)
6b97fd3d
MN
4103{
4104 int status = -ENOMEM;
4105 struct page *page = NULL;
4106 struct nfs4_fs_locations *locations = NULL;
6b97fd3d
MN
4107
4108 page = alloc_page(GFP_KERNEL);
4109 if (page == NULL)
4110 goto out;
4111 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4112 if (locations == NULL)
4113 goto out;
4114
f05d147f 4115 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
6b97fd3d
MN
4116 if (status != 0)
4117 goto out;
519ae255
CL
4118
4119 /*
4120 * If the fsid didn't change, this is a migration event, not a
4121 * referral. Cause us to drop into the exception handler, which
4122 * will kick off migration recovery.
4123 */
6b97fd3d 4124 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
533eb461
AA
4125 dprintk("%s: server did not return a different fsid for"
4126 " a referral at %s\n", __func__, name->name);
519ae255 4127 status = -NFS4ERR_MOVED;
6b97fd3d
MN
4128 goto out;
4129 }
533eb461
AA
4130 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4131 nfs_fixup_referral_attributes(&locations->fattr);
6b97fd3d 4132
533eb461 4133 /* replace the lookup nfs_fattr with the locations nfs_fattr */
6b97fd3d 4134 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
6b97fd3d
MN
4135 memset(fhandle, 0, sizeof(struct nfs_fh));
4136out:
4137 if (page)
4138 __free_page(page);
5d7ca35a 4139 kfree(locations);
6b97fd3d
MN
4140 return status;
4141}
4142
1775fd3e 4143static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
a841b54d
TM
4144 struct nfs_fattr *fattr, struct nfs4_label *label,
4145 struct inode *inode)
1da177e4 4146{
771734f2 4147 __u32 bitmask[NFS4_BITMASK_SZ];
1da177e4
LT
4148 struct nfs4_getattr_arg args = {
4149 .fh = fhandle,
771734f2 4150 .bitmask = bitmask,
1da177e4
LT
4151 };
4152 struct nfs4_getattr_res res = {
4153 .fattr = fattr,
1775fd3e 4154 .label = label,
1da177e4
LT
4155 .server = server,
4156 };
4157 struct rpc_message msg = {
4158 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4159 .rpc_argp = &args,
4160 .rpc_resp = &res,
4161 };
c74dfe97
TM
4162 unsigned short task_flags = 0;
4163
4164 /* Is this is an attribute revalidation, subject to softreval? */
4165 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4166 task_flags |= RPC_TASK_TIMEOUT;
aa9c2669 4167
771734f2 4168 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
aa9c2669 4169
0e574af1 4170 nfs_fattr_init(fattr);
c74dfe97
TM
4171 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4172 return nfs4_do_call_sync(server->client, server, &msg,
4173 &args.seq_args, &res.seq_res, task_flags);
1da177e4
LT
4174}
4175
ec4b0925 4176int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
a841b54d
TM
4177 struct nfs_fattr *fattr, struct nfs4_label *label,
4178 struct inode *inode)
1da177e4 4179{
0688e64b
TM
4180 struct nfs4_exception exception = {
4181 .interruptible = true,
4182 };
1da177e4
LT
4183 int err;
4184 do {
a841b54d 4185 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
b5f875a9
TM
4186 trace_nfs4_getattr(server, fhandle, fattr, err);
4187 err = nfs4_handle_exception(server, err,
1da177e4
LT
4188 &exception);
4189 } while (exception.retry);
4190 return err;
4191}
4192
4193/*
4194 * The file is not closed if it is opened due to the a request to change
4195 * the size of the file. The open call will not be needed once the
4196 * VFS layer lookup-intents are implemented.
4197 *
4198 * Close is called when the inode is destroyed.
4199 * If we haven't opened the file for O_WRONLY, we
4200 * need to in the size_change case to obtain a stateid.
4201 *
4202 * Got race?
4203 * Because OPEN is always done by name in nfsv4, it is
4204 * possible that we opened a different file by the same
4205 * name. We can recognize this race condition, but we
4206 * can't do anything about it besides returning an error.
4207 *
4208 * This will be fixed with VFS changes (lookup-intent).
4209 */
4210static int
4211nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4212 struct iattr *sattr)
4213{
2b0143b5 4214 struct inode *inode = d_inode(dentry);
a52458b4 4215 const struct cred *cred = NULL;
29b59f94 4216 struct nfs_open_context *ctx = NULL;
14c43f76 4217 struct nfs4_label *label = NULL;
1da177e4
LT
4218 int status;
4219
88ac815c
PT
4220 if (pnfs_ld_layoutret_on_setattr(inode) &&
4221 sattr->ia_valid & ATTR_SIZE &&
4222 sattr->ia_size < i_size_read(inode))
24028672 4223 pnfs_commit_and_return_layout(inode);
8a1636c4 4224
0e574af1 4225 nfs_fattr_init(fattr);
1da177e4 4226
2669940d
AA
4227 /* Deal with open(O_TRUNC) */
4228 if (sattr->ia_valid & ATTR_OPEN)
cc7936f9 4229 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
2669940d
AA
4230
4231 /* Optimization: if the end result is no change, don't RPC */
cc7936f9 4232 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
2669940d
AA
4233 return 0;
4234
d530838b 4235 /* Search for an existing open(O_WRITE) file */
659bfcd6 4236 if (sattr->ia_valid & ATTR_FILE) {
659bfcd6
TM
4237
4238 ctx = nfs_file_open_context(sattr->ia_file);
29b59f94 4239 if (ctx)
504e5189 4240 cred = ctx->cred;
659bfcd6 4241 }
08e9eac4 4242
14c43f76
DQ
4243 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4244 if (IS_ERR(label))
4245 return PTR_ERR(label);
4246
199366f0
TM
4247 /* Return any delegations if we're going to change ACLs */
4248 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
c01d3645 4249 nfs4_inode_make_writeable(inode);
199366f0 4250
29b59f94 4251 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
aa9c2669 4252 if (status == 0) {
f044636d 4253 nfs_setattr_update_inode(inode, sattr, fattr);
aa9c2669
DQ
4254 nfs_setsecurity(inode, fattr, label);
4255 }
14c43f76 4256 nfs4_label_free(label);
1da177e4
LT
4257 return status;
4258}
4259
0c2e53f1 4260static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
f7b37b8b 4261 struct dentry *dentry, struct nfs_fh *fhandle,
1775fd3e 4262 struct nfs_fattr *fattr, struct nfs4_label *label)
2b3de441 4263{
0c2e53f1 4264 struct nfs_server *server = NFS_SERVER(dir);
2b3de441
DH
4265 int status;
4266 struct nfs4_lookup_arg args = {
4267 .bitmask = server->attr_bitmask,
0c2e53f1 4268 .dir_fh = NFS_FH(dir),
f7b37b8b 4269 .name = &dentry->d_name,
2b3de441
DH
4270 };
4271 struct nfs4_lookup_res res = {
4272 .server = server,
4273 .fattr = fattr,
aa9c2669 4274 .label = label,
2b3de441
DH
4275 .fh = fhandle,
4276 };
4277 struct rpc_message msg = {
4278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4279 .rpc_argp = &args,
4280 .rpc_resp = &res,
4281 };
f7b37b8b
TM
4282 unsigned short task_flags = 0;
4283
4284 /* Is this is an attribute revalidation, subject to softreval? */
4285 if (nfs_lookup_is_soft_revalidate(dentry))
4286 task_flags |= RPC_TASK_TIMEOUT;
2b3de441 4287
aa9c2669
DQ
4288 args.bitmask = nfs4_bitmask(server, label);
4289
2b3de441
DH
4290 nfs_fattr_init(fattr);
4291
f7b37b8b
TM
4292 dprintk("NFS call lookup %pd2\n", dentry);
4293 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4294 status = nfs4_do_call_sync(clnt, server, &msg,
4295 &args.seq_args, &res.seq_res, task_flags);
1da177e4
LT
4296 dprintk("NFS reply lookup: %d\n", status);
4297 return status;
4298}
4299
72de53ec 4300static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
7ebb9315 4301{
7ebb9315 4302 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
72de53ec 4303 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
7ebb9315
BS
4304 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4305 fattr->nlink = 2;
4306}
4307
72de53ec 4308static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
f7b37b8b 4309 struct dentry *dentry, struct nfs_fh *fhandle,
1775fd3e 4310 struct nfs_fattr *fattr, struct nfs4_label *label)
1da177e4 4311{
0688e64b
TM
4312 struct nfs4_exception exception = {
4313 .interruptible = true,
4314 };
72de53ec 4315 struct rpc_clnt *client = *clnt;
f7b37b8b 4316 const struct qstr *name = &dentry->d_name;
1da177e4
LT
4317 int err;
4318 do {
f7b37b8b 4319 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
078ea3df 4320 trace_nfs4_lookup(dir, name, err);
72de53ec 4321 switch (err) {
08ef7bd3 4322 case -NFS4ERR_BADNAME:
72de53ec
BS
4323 err = -ENOENT;
4324 goto out;
0c2e53f1 4325 case -NFS4ERR_MOVED:
f05d147f 4326 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
c86c90c6
DM
4327 if (err == -NFS4ERR_MOVED)
4328 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
72de53ec 4329 goto out;
0c2e53f1 4330 case -NFS4ERR_WRONGSEC:
72de53ec
BS
4331 err = -EPERM;
4332 if (client != *clnt)
4333 goto out;
66b06860 4334 client = nfs4_negotiate_security(client, dir, name);
72de53ec
BS
4335 if (IS_ERR(client))
4336 return PTR_ERR(client);
4337
4338 exception.retry = 1;
4339 break;
4340 default:
4341 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
0c2e53f1 4342 }
1da177e4 4343 } while (exception.retry);
72de53ec
BS
4344
4345out:
4346 if (err == 0)
4347 *clnt = client;
4348 else if (client != *clnt)
4349 rpc_shutdown_client(client);
4350
1da177e4
LT
4351 return err;
4352}
4353
f7b37b8b 4354static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
1775fd3e
DQ
4355 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4356 struct nfs4_label *label)
72de53ec
BS
4357{
4358 int status;
4359 struct rpc_clnt *client = NFS_CLIENT(dir);
4360
f7b37b8b 4361 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
72de53ec
BS
4362 if (client != NFS_CLIENT(dir)) {
4363 rpc_shutdown_client(client);
4364 nfs_fixup_secinfo_attributes(fattr);
4365 }
4366 return status;
4367}
4368
f05d147f 4369struct rpc_clnt *
f7b37b8b 4370nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
f05d147f
BS
4371 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4372{
b72888cb 4373 struct rpc_clnt *client = NFS_CLIENT(dir);
f05d147f 4374 int status;
f05d147f 4375
f7b37b8b 4376 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
b72888cb 4377 if (status < 0)
f05d147f 4378 return ERR_PTR(status);
b72888cb 4379 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
f05d147f
BS
4380}
4381
5b5faaf6
JL
4382static int _nfs4_proc_lookupp(struct inode *inode,
4383 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4384 struct nfs4_label *label)
4385{
4386 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4387 struct nfs_server *server = NFS_SERVER(inode);
4388 int status;
4389 struct nfs4_lookupp_arg args = {
4390 .bitmask = server->attr_bitmask,
4391 .fh = NFS_FH(inode),
4392 };
4393 struct nfs4_lookupp_res res = {
4394 .server = server,
4395 .fattr = fattr,
4396 .label = label,
4397 .fh = fhandle,
4398 };
4399 struct rpc_message msg = {
4400 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4401 .rpc_argp = &args,
4402 .rpc_resp = &res,
4403 };
76998ebb
TM
4404 unsigned short task_flags = 0;
4405
4406 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4407 task_flags |= RPC_TASK_TIMEOUT;
5b5faaf6
JL
4408
4409 args.bitmask = nfs4_bitmask(server, label);
4410
4411 nfs_fattr_init(fattr);
4412
4413 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4414 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
76998ebb 4415 &res.seq_res, task_flags);
5b5faaf6
JL
4416 dprintk("NFS reply lookupp: %d\n", status);
4417 return status;
4418}
4419
4420static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4421 struct nfs_fattr *fattr, struct nfs4_label *label)
4422{
0688e64b
TM
4423 struct nfs4_exception exception = {
4424 .interruptible = true,
4425 };
5b5faaf6
JL
4426 int err;
4427 do {
4428 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4429 trace_nfs4_lookupp(inode, err);
4430 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4431 &exception);
4432 } while (exception.retry);
4433 return err;
4434}
4435
1da177e4
LT
4436static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4437{
76b32999 4438 struct nfs_server *server = NFS_SERVER(inode);
1da177e4
LT
4439 struct nfs4_accessargs args = {
4440 .fh = NFS_FH(inode),
1750d929 4441 .access = entry->mask,
76b32999
TM
4442 };
4443 struct nfs4_accessres res = {
4444 .server = server,
1da177e4 4445 };
1da177e4
LT
4446 struct rpc_message msg = {
4447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4448 .rpc_argp = &args,
4449 .rpc_resp = &res,
4450 .rpc_cred = entry->cred,
4451 };
aa9c2669 4452 int status = 0;
1da177e4 4453
7c672654 4454 if (!nfs4_have_delegation(inode, FMODE_READ)) {
8bcbe7d9
TM
4455 res.fattr = nfs_alloc_fattr();
4456 if (res.fattr == NULL)
4457 return -ENOMEM;
4458 args.bitmask = server->cache_consistency_bitmask;
4459 }
7c513058 4460 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4 4461 if (!status) {
6168f62c 4462 nfs_access_set_mask(entry, res.access);
8bcbe7d9
TM
4463 if (res.fattr)
4464 nfs_refresh_inode(inode, res.fattr);
1da177e4 4465 }
c407d41a 4466 nfs_free_fattr(res.fattr);
1da177e4
LT
4467 return status;
4468}
4469
4470static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4471{
0688e64b
TM
4472 struct nfs4_exception exception = {
4473 .interruptible = true,
4474 };
1da177e4
LT
4475 int err;
4476 do {
c1578b76
TM
4477 err = _nfs4_proc_access(inode, entry);
4478 trace_nfs4_access(inode, err);
4479 err = nfs4_handle_exception(NFS_SERVER(inode), err,
1da177e4
LT
4480 &exception);
4481 } while (exception.retry);
4482 return err;
4483}
4484
4485/*
4486 * TODO: For the time being, we don't try to get any attributes
4487 * along with any of the zero-copy operations READ, READDIR,
4488 * READLINK, WRITE.
4489 *
4490 * In the case of the first three, we want to put the GETATTR
4491 * after the read-type operation -- this is because it is hard
4492 * to predict the length of a GETATTR response in v4, and thus
4493 * align the READ data correctly. This means that the GETATTR
4494 * may end up partially falling into the page cache, and we should
4495 * shift it into the 'tail' of the xdr_buf before processing.
4496 * To do this efficiently, we need to know the total length
4497 * of data received, which doesn't seem to be available outside
4498 * of the RPC layer.
4499 *
4500 * In the case of WRITE, we also want to put the GETATTR after
4501 * the operation -- in this case because we want to make sure
140150db 4502 * we get the post-operation mtime and size.
1da177e4
LT
4503 *
4504 * Both of these changes to the XDR layer would in fact be quite
4505 * minor, but I decided to leave them for a subsequent patch.
4506 */
4507static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4508 unsigned int pgbase, unsigned int pglen)
4509{
4510 struct nfs4_readlink args = {
4511 .fh = NFS_FH(inode),
4512 .pgbase = pgbase,
4513 .pglen = pglen,
4514 .pages = &page,
4515 };
f50c7000 4516 struct nfs4_readlink_res res;
1da177e4
LT
4517 struct rpc_message msg = {
4518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4519 .rpc_argp = &args,
f50c7000 4520 .rpc_resp = &res,
1da177e4
LT
4521 };
4522
7c513058 4523 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
4524}
4525
4526static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4527 unsigned int pgbase, unsigned int pglen)
4528{
0688e64b
TM
4529 struct nfs4_exception exception = {
4530 .interruptible = true,
4531 };
1da177e4
LT
4532 int err;
4533 do {
c1578b76
TM
4534 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4535 trace_nfs4_readlink(inode, err);
4536 err = nfs4_handle_exception(NFS_SERVER(inode), err,
1da177e4
LT
4537 &exception);
4538 } while (exception.retry);
4539 return err;
4540}
4541
1da177e4 4542/*
8867fe58 4543 * This is just for mknod. open(O_CREAT) will always do ->open_context().
1da177e4 4544 */
1da177e4
LT
4545static int
4546nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
8867fe58 4547 int flags)
1da177e4 4548{
dff25ddb 4549 struct nfs_server *server = NFS_SERVER(dir);
aa9c2669 4550 struct nfs4_label l, *ilabel = NULL;
8867fe58 4551 struct nfs_open_context *ctx;
1da177e4 4552 struct nfs4_state *state;
1da177e4
LT
4553 int status = 0;
4554
532d4def 4555 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
8867fe58
MS
4556 if (IS_ERR(ctx))
4557 return PTR_ERR(ctx);
4558
aa9c2669
DQ
4559 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4560
dff25ddb
AG
4561 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4562 sattr->ia_mode &= ~current_umask();
c5c3fb5f 4563 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
1da177e4
LT
4564 if (IS_ERR(state)) {
4565 status = PTR_ERR(state);
c0204fd2 4566 goto out;
1da177e4 4567 }
1da177e4 4568out:
aa9c2669 4569 nfs4_label_release_security(ilabel);
8867fe58 4570 put_nfs_open_context(ctx);
1da177e4
LT
4571 return status;
4572}
4573
3c591175
TM
4574static int
4575_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
1da177e4 4576{
16e42959 4577 struct nfs_server *server = NFS_SERVER(dir);
4fdc17b2 4578 struct nfs_removeargs args = {
1da177e4 4579 .fh = NFS_FH(dir),
26fe5750 4580 .name = *name,
16e42959 4581 };
4fdc17b2 4582 struct nfs_removeres res = {
16e42959 4583 .server = server,
1da177e4 4584 };
1da177e4 4585 struct rpc_message msg = {
4fdc17b2
TM
4586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4587 .rpc_argp = &args,
4588 .rpc_resp = &res,
1da177e4 4589 };
d3129ef6 4590 unsigned long timestamp = jiffies;
778d2817 4591 int status;
1da177e4 4592
7c513058 4593 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3c591175
TM
4594 if (status == 0) {
4595 spin_lock(&dir->i_lock);
1b523ca9
FL
4596 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4597 NFS_INO_INVALID_DATA);
3c591175
TM
4598 /* Removing a directory decrements nlink in the parent */
4599 if (ftype == NF4DIR && dir->i_nlink > 2)
4600 nfs4_dec_nlink_locked(dir);
4601 spin_unlock(&dir->i_lock);
4602 }
1da177e4
LT
4603 return status;
4604}
4605
912678db 4606static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
1da177e4 4607{
0688e64b
TM
4608 struct nfs4_exception exception = {
4609 .interruptible = true,
4610 };
912678db 4611 struct inode *inode = d_inode(dentry);
1da177e4 4612 int err;
912678db 4613
c01d3645
TM
4614 if (inode) {
4615 if (inode->i_nlink == 1)
4616 nfs4_inode_return_delegation(inode);
4617 else
4618 nfs4_inode_make_writeable(inode);
4619 }
912678db 4620 do {
3c591175 4621 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
912678db
TM
4622 trace_nfs4_remove(dir, &dentry->d_name, err);
4623 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4624 &exception);
4625 } while (exception.retry);
4626 return err;
4627}
4628
4629static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4630{
0688e64b
TM
4631 struct nfs4_exception exception = {
4632 .interruptible = true,
4633 };
912678db
TM
4634 int err;
4635
1da177e4 4636 do {
3c591175 4637 err = _nfs4_proc_remove(dir, name, NF4DIR);
078ea3df
TM
4638 trace_nfs4_remove(dir, name, err);
4639 err = nfs4_handle_exception(NFS_SERVER(dir), err,
1da177e4
LT
4640 &exception);
4641 } while (exception.retry);
4642 return err;
4643}
4644
ed7e9ad0
TM
4645static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4646 struct dentry *dentry,
4647 struct inode *inode)
1da177e4 4648{
e4eff1a6
TM
4649 struct nfs_removeargs *args = msg->rpc_argp;
4650 struct nfs_removeres *res = msg->rpc_resp;
1da177e4 4651
977fcc2b 4652 res->server = NFS_SB(dentry->d_sb);
1da177e4 4653 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
fba83f34 4654 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
aa9c2669
DQ
4655
4656 nfs_fattr_init(res->dir_attr);
977fcc2b
TM
4657
4658 if (inode)
4659 nfs4_inode_return_delegation(inode);
1da177e4
LT
4660}
4661
34e137cc
BS
4662static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4663{
42e1cca7 4664 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
d9afbd1b
TM
4665 &data->args.seq_args,
4666 &data->res.seq_res,
4667 task);
1da177e4
LT
4668}
4669
e4eff1a6 4670static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
1da177e4 4671{
078ea3df
TM
4672 struct nfs_unlinkdata *data = task->tk_calldata;
4673 struct nfs_removeres *res = &data->res;
e4eff1a6 4674
14516c3a
TM
4675 if (!nfs4_sequence_done(task, &res->seq_res))
4676 return 0;
8478eaa1
N
4677 if (nfs4_async_handle_error(task, res->server, NULL,
4678 &data->timeout) == -EAGAIN)
e4eff1a6 4679 return 0;
c40d52fe 4680 if (task->tk_status == 0)
1b523ca9
FL
4681 nfs4_update_changeattr(dir, &res->cinfo,
4682 res->dir_attr->time_start,
4683 NFS_INO_INVALID_DATA);
e4eff1a6 4684 return 1;
1da177e4
LT
4685}
4686
f2c2c552
TM
4687static void nfs4_proc_rename_setup(struct rpc_message *msg,
4688 struct dentry *old_dentry,
4689 struct dentry *new_dentry)
d3d4152a 4690{
d3d4152a
JL
4691 struct nfs_renameargs *arg = msg->rpc_argp;
4692 struct nfs_renameres *res = msg->rpc_resp;
f2c2c552
TM
4693 struct inode *old_inode = d_inode(old_dentry);
4694 struct inode *new_inode = d_inode(new_dentry);
d3d4152a 4695
f2c2c552 4696 if (old_inode)
c01d3645 4697 nfs4_inode_make_writeable(old_inode);
f2c2c552
TM
4698 if (new_inode)
4699 nfs4_inode_return_delegation(new_inode);
d3d4152a 4700 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
f2c2c552 4701 res->server = NFS_SB(old_dentry->d_sb);
fba83f34 4702 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
d3d4152a
JL
4703}
4704
c6bfa1a1
BS
4705static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4706{
42e1cca7 4707 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
d9afbd1b
TM
4708 &data->args.seq_args,
4709 &data->res.seq_res,
4710 task);
d3d4152a
JL
4711}
4712
4713static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4714 struct inode *new_dir)
4715{
fbc6f7c2
TM
4716 struct nfs_renamedata *data = task->tk_calldata;
4717 struct nfs_renameres *res = &data->res;
d3d4152a
JL
4718
4719 if (!nfs4_sequence_done(task, &res->seq_res))
4720 return 0;
8478eaa1 4721 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
d3d4152a
JL
4722 return 0;
4723
c733c49c 4724 if (task->tk_status == 0) {
5636ec4e
TM
4725 if (new_dir != old_dir) {
4726 /* Note: If we moved a directory, nlink will change */
1b523ca9 4727 nfs4_update_changeattr(old_dir, &res->old_cinfo,
5636ec4e 4728 res->old_fattr->time_start,
1b523ca9
FL
4729 NFS_INO_INVALID_OTHER |
4730 NFS_INO_INVALID_DATA);
4731 nfs4_update_changeattr(new_dir, &res->new_cinfo,
5636ec4e 4732 res->new_fattr->time_start,
1b523ca9
FL
4733 NFS_INO_INVALID_OTHER |
4734 NFS_INO_INVALID_DATA);
5636ec4e 4735 } else
1b523ca9 4736 nfs4_update_changeattr(old_dir, &res->old_cinfo,
5636ec4e 4737 res->old_fattr->time_start,
1b523ca9 4738 NFS_INO_INVALID_DATA);
c733c49c 4739 }
d3d4152a
JL
4740 return 1;
4741}
4742
beffb8fe 4743static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
1da177e4 4744{
91ba2eee 4745 struct nfs_server *server = NFS_SERVER(inode);
2f28dc38 4746 __u32 bitmask[NFS4_BITMASK_SZ];
1da177e4
LT
4747 struct nfs4_link_arg arg = {
4748 .fh = NFS_FH(inode),
4749 .dir_fh = NFS_FH(dir),
4750 .name = name,
2f28dc38 4751 .bitmask = bitmask,
91ba2eee 4752 };
91ba2eee
TM
4753 struct nfs4_link_res res = {
4754 .server = server,
1775fd3e 4755 .label = NULL,
1da177e4 4756 };
1da177e4
LT
4757 struct rpc_message msg = {
4758 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4759 .rpc_argp = &arg,
91ba2eee 4760 .rpc_resp = &res,
1da177e4 4761 };
136f2627
TM
4762 int status = -ENOMEM;
4763
4764 res.fattr = nfs_alloc_fattr();
778d2817 4765 if (res.fattr == NULL)
136f2627 4766 goto out;
1da177e4 4767
14c43f76
DQ
4768 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4769 if (IS_ERR(res.label)) {
4770 status = PTR_ERR(res.label);
4771 goto out;
4772 }
4773
c01d3645 4774 nfs4_inode_make_writeable(inode);
2f28dc38 4775 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
9f768272 4776
7c513058 4777 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
91ba2eee 4778 if (!status) {
1b523ca9
FL
4779 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4780 NFS_INO_INVALID_DATA);
aa9c2669
DQ
4781 status = nfs_post_op_update_inode(inode, res.fattr);
4782 if (!status)
4783 nfs_setsecurity(inode, res.fattr, res.label);
91ba2eee 4784 }
14c43f76
DQ
4785
4786
4787 nfs4_label_free(res.label);
4788
136f2627 4789out:
136f2627 4790 nfs_free_fattr(res.fattr);
1da177e4
LT
4791 return status;
4792}
4793
beffb8fe 4794static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
1da177e4 4795{
0688e64b
TM
4796 struct nfs4_exception exception = {
4797 .interruptible = true,
4798 };
1da177e4
LT
4799 int err;
4800 do {
4801 err = nfs4_handle_exception(NFS_SERVER(inode),
4802 _nfs4_proc_link(inode, dir, name),
4803 &exception);
4804 } while (exception.retry);
4805 return err;
4806}
4807
57dc9a57
TM
4808struct nfs4_createdata {
4809 struct rpc_message msg;
4810 struct nfs4_create_arg arg;
4811 struct nfs4_create_res res;
4812 struct nfs_fh fh;
4813 struct nfs_fattr fattr;
1775fd3e 4814 struct nfs4_label *label;
57dc9a57
TM
4815};
4816
4817static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
beffb8fe 4818 const struct qstr *name, struct iattr *sattr, u32 ftype)
57dc9a57
TM
4819{
4820 struct nfs4_createdata *data;
4821
4822 data = kzalloc(sizeof(*data), GFP_KERNEL);
4823 if (data != NULL) {
4824 struct nfs_server *server = NFS_SERVER(dir);
4825
14c43f76
DQ
4826 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4827 if (IS_ERR(data->label))
4828 goto out_free;
4829
57dc9a57
TM
4830 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4831 data->msg.rpc_argp = &data->arg;
4832 data->msg.rpc_resp = &data->res;
4833 data->arg.dir_fh = NFS_FH(dir);
4834 data->arg.server = server;
4835 data->arg.name = name;
4836 data->arg.attrs = sattr;
4837 data->arg.ftype = ftype;
aa9c2669 4838 data->arg.bitmask = nfs4_bitmask(server, data->label);
dff25ddb 4839 data->arg.umask = current_umask();
57dc9a57
TM
4840 data->res.server = server;
4841 data->res.fh = &data->fh;
4842 data->res.fattr = &data->fattr;
1775fd3e 4843 data->res.label = data->label;
57dc9a57 4844 nfs_fattr_init(data->res.fattr);
57dc9a57
TM
4845 }
4846 return data;
14c43f76
DQ
4847out_free:
4848 kfree(data);
4849 return NULL;
57dc9a57
TM
4850}
4851
4852static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4853{
7c513058 4854 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
e73b83f2 4855 &data->arg.seq_args, &data->res.seq_res, 1);
57dc9a57 4856 if (status == 0) {
3c591175 4857 spin_lock(&dir->i_lock);
1b523ca9
FL
4858 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4859 data->res.fattr->time_start,
4860 NFS_INO_INVALID_DATA);
3c591175
TM
4861 /* Creating a directory bumps nlink in the parent */
4862 if (data->arg.ftype == NF4DIR)
4863 nfs4_inc_nlink_locked(dir);
4864 spin_unlock(&dir->i_lock);
1775fd3e 4865 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
57dc9a57
TM
4866 }
4867 return status;
4868}
4869
4870static void nfs4_free_createdata(struct nfs4_createdata *data)
4871{
14c43f76 4872 nfs4_label_free(data->label);
57dc9a57
TM
4873 kfree(data);
4874}
4875
4f390c15 4876static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
1775fd3e
DQ
4877 struct page *page, unsigned int len, struct iattr *sattr,
4878 struct nfs4_label *label)
1da177e4 4879{
57dc9a57
TM
4880 struct nfs4_createdata *data;
4881 int status = -ENAMETOOLONG;
1da177e4 4882
94a6d753 4883 if (len > NFS4_MAXPATHLEN)
57dc9a57 4884 goto out;
4f390c15 4885
57dc9a57
TM
4886 status = -ENOMEM;
4887 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4888 if (data == NULL)
4889 goto out;
4890
4891 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4892 data->arg.u.symlink.pages = &page;
4893 data->arg.u.symlink.len = len;
1775fd3e 4894 data->arg.label = label;
1da177e4 4895
57dc9a57
TM
4896 status = nfs4_do_create(dir, dentry, data);
4897
4898 nfs4_free_createdata(data);
4899out:
1da177e4
LT
4900 return status;
4901}
4902
4f390c15 4903static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
94a6d753 4904 struct page *page, unsigned int len, struct iattr *sattr)
1da177e4 4905{
0688e64b
TM
4906 struct nfs4_exception exception = {
4907 .interruptible = true,
4908 };
aa9c2669 4909 struct nfs4_label l, *label = NULL;
1da177e4 4910 int err;
aa9c2669
DQ
4911
4912 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4913
1da177e4 4914 do {
078ea3df
TM
4915 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4916 trace_nfs4_symlink(dir, &dentry->d_name, err);
4917 err = nfs4_handle_exception(NFS_SERVER(dir), err,
1da177e4
LT
4918 &exception);
4919 } while (exception.retry);
aa9c2669
DQ
4920
4921 nfs4_label_release_security(label);
1da177e4
LT
4922 return err;
4923}
4924
4925static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1775fd3e 4926 struct iattr *sattr, struct nfs4_label *label)
1da177e4 4927{
57dc9a57
TM
4928 struct nfs4_createdata *data;
4929 int status = -ENOMEM;
1da177e4 4930
57dc9a57
TM
4931 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4932 if (data == NULL)
4933 goto out;
4934
1775fd3e 4935 data->arg.label = label;
57dc9a57
TM
4936 status = nfs4_do_create(dir, dentry, data);
4937
4938 nfs4_free_createdata(data);
4939out:
1da177e4
LT
4940 return status;
4941}
4942
4943static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4944 struct iattr *sattr)
4945{
dff25ddb 4946 struct nfs_server *server = NFS_SERVER(dir);
0688e64b
TM
4947 struct nfs4_exception exception = {
4948 .interruptible = true,
4949 };
aa9c2669 4950 struct nfs4_label l, *label = NULL;
1da177e4 4951 int err;
a8a5da99 4952
aa9c2669
DQ
4953 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4954
dff25ddb
AG
4955 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4956 sattr->ia_mode &= ~current_umask();
1da177e4 4957 do {
078ea3df
TM
4958 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4959 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4960 err = nfs4_handle_exception(NFS_SERVER(dir), err,
1da177e4
LT
4961 &exception);
4962 } while (exception.retry);
aa9c2669
DQ
4963 nfs4_label_release_security(label);
4964
1da177e4
LT
4965 return err;
4966}
4967
82e22a5e
TM
4968static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
4969 struct nfs_readdir_res *nr_res)
1da177e4 4970{
82e22a5e 4971 struct inode *dir = d_inode(nr_arg->dentry);
05ad9175 4972 struct nfs_server *server = NFS_SERVER(dir);
1da177e4
LT
4973 struct nfs4_readdir_arg args = {
4974 .fh = NFS_FH(dir),
82e22a5e 4975 .pages = nr_arg->pages,
1da177e4 4976 .pgbase = 0,
82e22a5e
TM
4977 .count = nr_arg->page_len,
4978 .plus = nr_arg->plus,
1da177e4
LT
4979 };
4980 struct nfs4_readdir_res res;
4981 struct rpc_message msg = {
4982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4983 .rpc_argp = &args,
4984 .rpc_resp = &res,
82e22a5e 4985 .rpc_cred = nr_arg->cred,
1da177e4
LT
4986 };
4987 int status;
4988
82e22a5e
TM
4989 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
4990 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
05ad9175
OK
4991 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
4992 args.bitmask = server->attr_bitmask_nl;
4993 else
4994 args.bitmask = server->attr_bitmask;
4995
82e22a5e 4996 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
1da177e4 4997 res.pgbase = args.pgbase;
05ad9175
OK
4998 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
4999 &res.seq_res, 0);
ac396128 5000 if (status >= 0) {
82e22a5e 5001 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
ac396128
TM
5002 status += args.pgbase;
5003 }
c4812998
TM
5004
5005 nfs_invalidate_atime(dir);
5006
3110ff80 5007 dprintk("%s: returns %d\n", __func__, status);
1da177e4
LT
5008 return status;
5009}
5010
82e22a5e
TM
5011static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5012 struct nfs_readdir_res *res)
1da177e4 5013{
0688e64b
TM
5014 struct nfs4_exception exception = {
5015 .interruptible = true,
5016 };
1da177e4
LT
5017 int err;
5018 do {
82e22a5e
TM
5019 err = _nfs4_proc_readdir(arg, res);
5020 trace_nfs4_readdir(d_inode(arg->dentry), err);
5021 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5022 err, &exception);
1da177e4
LT
5023 } while (exception.retry);
5024 return err;
5025}
5026
5027static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
aa9c2669 5028 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
1da177e4 5029{
57dc9a57
TM
5030 struct nfs4_createdata *data;
5031 int mode = sattr->ia_mode;
5032 int status = -ENOMEM;
1da177e4 5033
57dc9a57
TM
5034 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5035 if (data == NULL)
5036 goto out;
5037
1da177e4 5038 if (S_ISFIFO(mode))
57dc9a57 5039 data->arg.ftype = NF4FIFO;
1da177e4 5040 else if (S_ISBLK(mode)) {
57dc9a57
TM
5041 data->arg.ftype = NF4BLK;
5042 data->arg.u.device.specdata1 = MAJOR(rdev);
5043 data->arg.u.device.specdata2 = MINOR(rdev);
1da177e4
LT
5044 }
5045 else if (S_ISCHR(mode)) {
57dc9a57
TM
5046 data->arg.ftype = NF4CHR;
5047 data->arg.u.device.specdata1 = MAJOR(rdev);
5048 data->arg.u.device.specdata2 = MINOR(rdev);
4ea8fed5
TM
5049 } else if (!S_ISSOCK(mode)) {
5050 status = -EINVAL;
5051 goto out_free;
1da177e4 5052 }
1775fd3e 5053
aa9c2669 5054 data->arg.label = label;
57dc9a57 5055 status = nfs4_do_create(dir, dentry, data);
4ea8fed5 5056out_free:
57dc9a57
TM
5057 nfs4_free_createdata(data);
5058out:
1da177e4
LT
5059 return status;
5060}
5061
5062static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5063 struct iattr *sattr, dev_t rdev)
5064{
dff25ddb 5065 struct nfs_server *server = NFS_SERVER(dir);
0688e64b
TM
5066 struct nfs4_exception exception = {
5067 .interruptible = true,
5068 };
aa9c2669 5069 struct nfs4_label l, *label = NULL;
1da177e4 5070 int err;
a8a5da99 5071
aa9c2669
DQ
5072 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5073
dff25ddb
AG
5074 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5075 sattr->ia_mode &= ~current_umask();
1da177e4 5076 do {
078ea3df
TM
5077 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5078 trace_nfs4_mknod(dir, &dentry->d_name, err);
5079 err = nfs4_handle_exception(NFS_SERVER(dir), err,
1da177e4
LT
5080 &exception);
5081 } while (exception.retry);
aa9c2669
DQ
5082
5083 nfs4_label_release_security(label);
5084
1da177e4
LT
5085 return err;
5086}
5087
5088static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5089 struct nfs_fsstat *fsstat)
5090{
5091 struct nfs4_statfs_arg args = {
5092 .fh = fhandle,
5093 .bitmask = server->attr_bitmask,
5094 };
24ad148a
BH
5095 struct nfs4_statfs_res res = {
5096 .fsstat = fsstat,
5097 };
1da177e4
LT
5098 struct rpc_message msg = {
5099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5100 .rpc_argp = &args,
24ad148a 5101 .rpc_resp = &res,
1da177e4
LT
5102 };
5103
0e574af1 5104 nfs_fattr_init(fsstat->fattr);
7c513058 5105 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
5106}
5107
5108static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5109{
0688e64b
TM
5110 struct nfs4_exception exception = {
5111 .interruptible = true,
5112 };
1da177e4
LT
5113 int err;
5114 do {
5115 err = nfs4_handle_exception(server,
5116 _nfs4_proc_statfs(server, fhandle, fsstat),
5117 &exception);
5118 } while (exception.retry);
5119 return err;
5120}
5121
5122static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5123 struct nfs_fsinfo *fsinfo)
5124{
5125 struct nfs4_fsinfo_arg args = {
5126 .fh = fhandle,
5127 .bitmask = server->attr_bitmask,
5128 };
3dda5e43
BH
5129 struct nfs4_fsinfo_res res = {
5130 .fsinfo = fsinfo,
5131 };
1da177e4
LT
5132 struct rpc_message msg = {
5133 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5134 .rpc_argp = &args,
3dda5e43 5135 .rpc_resp = &res,
1da177e4
LT
5136 };
5137
7c513058 5138 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
5139}
5140
5141static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5142{
0688e64b
TM
5143 struct nfs4_exception exception = {
5144 .interruptible = true,
5145 };
1da177e4
LT
5146 int err;
5147
5148 do {
83ca7f5a 5149 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
b5f875a9 5150 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
83ca7f5a 5151 if (err == 0) {
7dc2993a 5152 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
83ca7f5a
CL
5153 break;
5154 }
5155 err = nfs4_handle_exception(server, err, &exception);
1da177e4
LT
5156 } while (exception.retry);
5157 return err;
5158}
5159
5160static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5161{
e38eb650
BS
5162 int error;
5163
0e574af1 5164 nfs_fattr_init(fsinfo->fattr);
e38eb650 5165 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
dc182549
PT
5166 if (error == 0) {
5167 /* block layout checks this! */
5168 server->pnfs_blksize = fsinfo->blksize;
ca440c38 5169 set_pnfs_layoutdriver(server, fhandle, fsinfo);
dc182549 5170 }
e38eb650
BS
5171
5172 return error;
1da177e4
LT
5173}
5174
5175static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5176 struct nfs_pathconf *pathconf)
5177{
5178 struct nfs4_pathconf_arg args = {
5179 .fh = fhandle,
5180 .bitmask = server->attr_bitmask,
5181 };
d45b2989
BH
5182 struct nfs4_pathconf_res res = {
5183 .pathconf = pathconf,
5184 };
1da177e4
LT
5185 struct rpc_message msg = {
5186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5187 .rpc_argp = &args,
d45b2989 5188 .rpc_resp = &res,
1da177e4
LT
5189 };
5190
5191 /* None of the pathconf attributes are mandatory to implement */
5192 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5193 memset(pathconf, 0, sizeof(*pathconf));
5194 return 0;
5195 }
5196
0e574af1 5197 nfs_fattr_init(pathconf->fattr);
7c513058 5198 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
5199}
5200
5201static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5202 struct nfs_pathconf *pathconf)
5203{
0688e64b
TM
5204 struct nfs4_exception exception = {
5205 .interruptible = true,
5206 };
1da177e4
LT
5207 int err;
5208
5209 do {
5210 err = nfs4_handle_exception(server,
5211 _nfs4_proc_pathconf(server, fhandle, pathconf),
5212 &exception);
5213 } while (exception.retry);
5214 return err;
5215}
5216
5521abfd 5217int nfs4_set_rw_stateid(nfs4_stateid *stateid,
9b206149
TM
5218 const struct nfs_open_context *ctx,
5219 const struct nfs_lock_context *l_ctx,
5220 fmode_t fmode)
5221{
17393475 5222 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
9b206149
TM
5223}
5224EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5225
5521abfd
TM
5226static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5227 const struct nfs_open_context *ctx,
5228 const struct nfs_lock_context *l_ctx,
5229 fmode_t fmode)
5230{
d49dd117 5231 nfs4_stateid _current_stateid;
5521abfd 5232
e1253be0 5233 /* If the current stateid represents a lost lock, then exit */
d49dd117 5234 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
e1253be0 5235 return true;
d49dd117 5236 return nfs4_stateid_match(stateid, &_current_stateid);
5521abfd
TM
5237}
5238
5239static bool nfs4_error_stateid_expired(int err)
5240{
5241 switch (err) {
5242 case -NFS4ERR_DELEG_REVOKED:
5243 case -NFS4ERR_ADMIN_REVOKED:
5244 case -NFS4ERR_BAD_STATEID:
5245 case -NFS4ERR_STALE_STATEID:
5246 case -NFS4ERR_OLD_STATEID:
5247 case -NFS4ERR_OPENMODE:
5248 case -NFS4ERR_EXPIRED:
5249 return true;
5250 }
5251 return false;
5252}
5253
d45f60c6 5254static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
1da177e4 5255{
d45f60c6 5256 struct nfs_server *server = NFS_SERVER(hdr->inode);
1da177e4 5257
d45f60c6 5258 trace_nfs4_read(hdr, task->tk_status);
9c27869d
TM
5259 if (task->tk_status < 0) {
5260 struct nfs4_exception exception = {
5261 .inode = hdr->inode,
5262 .state = hdr->args.context->state,
5263 .stateid = &hdr->args.stateid,
5264 };
5265 task->tk_status = nfs4_async_handle_exception(task,
5266 server, task->tk_status, &exception);
5267 if (exception.retry) {
5268 rpc_restart_call_prepare(task);
5269 return -EAGAIN;
5270 }
1da177e4 5271 }
8850df99 5272
1da177e4 5273 if (task->tk_status > 0)
d45f60c6 5274 renew_lease(server, hdr->timestamp);
ec06c096 5275 return 0;
1da177e4
LT
5276}
5277
5521abfd 5278static bool nfs4_read_stateid_changed(struct rpc_task *task,
3c6b899c 5279 struct nfs_pgio_args *args)
5521abfd
TM
5280{
5281
5282 if (!nfs4_error_stateid_expired(task->tk_status) ||
5283 nfs4_stateid_is_current(&args->stateid,
5284 args->context,
5285 args->lock_context,
5286 FMODE_READ))
5287 return false;
5288 rpc_restart_call_prepare(task);
5289 return true;
5290}
5291
c5675526
AS
5292static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5293 struct nfs_pgio_header *hdr)
cbdabc7f 5294{
c5675526
AS
5295 struct nfs_server *server = NFS_SERVER(hdr->inode);
5296 struct rpc_message *msg = &task->tk_msg;
cbdabc7f 5297
c5675526
AS
5298 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5299 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5300 server->caps &= ~NFS_CAP_READ_PLUS;
5301 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5302 rpc_restart_call_prepare(task);
5303 return true;
5304 }
5305 return false;
5306}
5307
5308static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5309{
cbdabc7f
AA
5310 dprintk("--> %s\n", __func__);
5311
d45f60c6 5312 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
cbdabc7f 5313 return -EAGAIN;
d45f60c6 5314 if (nfs4_read_stateid_changed(task, &hdr->args))
5521abfd 5315 return -EAGAIN;
c5675526
AS
5316 if (nfs4_read_plus_not_supported(task, hdr))
5317 return -EAGAIN;
bfc505de
TM
5318 if (task->tk_status > 0)
5319 nfs_invalidate_atime(hdr->inode);
d45f60c6
WAA
5320 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5321 nfs4_read_done_cb(task, hdr);
cbdabc7f
AA
5322}
5323
21e31401 5324#if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5c3485bb
TM
5325static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5326 struct rpc_message *msg)
c5675526 5327{
5c3485bb
TM
5328 /* Note: We don't use READ_PLUS with pNFS yet */
5329 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
c5675526 5330 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
c5675526
AS
5331}
5332#else
5c3485bb
TM
5333static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5334 struct rpc_message *msg)
c5675526 5335{
c5675526
AS
5336}
5337#endif /* CONFIG_NFS_V4_2 */
5338
d45f60c6
WAA
5339static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5340 struct rpc_message *msg)
1da177e4 5341{
d45f60c6 5342 hdr->timestamp = jiffies;
ca857cc1
TM
5343 if (!hdr->pgio_done_cb)
5344 hdr->pgio_done_cb = nfs4_read_done_cb;
5c3485bb
TM
5345 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5346 nfs42_read_plus_support(hdr, msg);
fba83f34 5347 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
1da177e4
LT
5348}
5349
d45f60c6
WAA
5350static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5351 struct nfs_pgio_header *hdr)
ea7c3303 5352{
42e1cca7 5353 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
d45f60c6
WAA
5354 &hdr->args.seq_args,
5355 &hdr->res.seq_res,
9b206149 5356 task))
ef1820f9 5357 return 0;
d45f60c6
WAA
5358 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5359 hdr->args.lock_context,
fbe77c30 5360 hdr->rw_mode) == -EIO)
ef1820f9 5361 return -EIO;
d45f60c6 5362 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
ef1820f9
N
5363 return -EIO;
5364 return 0;
1da177e4
LT
5365}
5366
d45f60c6
WAA
5367static int nfs4_write_done_cb(struct rpc_task *task,
5368 struct nfs_pgio_header *hdr)
1da177e4 5369{
d45f60c6 5370 struct inode *inode = hdr->inode;
8478eaa1 5371
d45f60c6 5372 trace_nfs4_write(hdr, task->tk_status);
9c27869d
TM
5373 if (task->tk_status < 0) {
5374 struct nfs4_exception exception = {
5375 .inode = hdr->inode,
5376 .state = hdr->args.context->state,
5377 .stateid = &hdr->args.stateid,
5378 };
5379 task->tk_status = nfs4_async_handle_exception(task,
5380 NFS_SERVER(inode), task->tk_status,
5381 &exception);
5382 if (exception.retry) {
5383 rpc_restart_call_prepare(task);
5384 return -EAGAIN;
5385 }
1da177e4 5386 }
4f9838c7 5387 if (task->tk_status >= 0) {
d45f60c6 5388 renew_lease(NFS_SERVER(inode), hdr->timestamp);
a08a8cd3 5389 nfs_writeback_update_inode(hdr);
4f9838c7 5390 }
788e7a89 5391 return 0;
1da177e4
LT
5392}
5393
5521abfd 5394static bool nfs4_write_stateid_changed(struct rpc_task *task,
3c6b899c 5395 struct nfs_pgio_args *args)
5521abfd
TM
5396{
5397
5398 if (!nfs4_error_stateid_expired(task->tk_status) ||
5399 nfs4_stateid_is_current(&args->stateid,
5400 args->context,
5401 args->lock_context,
5402 FMODE_WRITE))
5403 return false;
5404 rpc_restart_call_prepare(task);
5405 return true;
5406}
5407
d45f60c6 5408static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
b029bc9b 5409{
d45f60c6 5410 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
b029bc9b 5411 return -EAGAIN;
d45f60c6 5412 if (nfs4_write_stateid_changed(task, &hdr->args))
5521abfd 5413 return -EAGAIN;
d45f60c6
WAA
5414 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5415 nfs4_write_done_cb(task, hdr);
b029bc9b
FI
5416}
5417
5a37f851 5418static
d45f60c6 5419bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
a69aef14 5420{
5a37f851 5421 /* Don't request attributes for pNFS or O_DIRECT writes */
d45f60c6 5422 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5a37f851
TM
5423 return false;
5424 /* Otherwise, request attributes if and only if we don't hold
5425 * a delegation
5426 */
011e2a7f 5427 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
a69aef14 5428}
a69aef14 5429
76bd5c01
OK
5430static void nfs4_bitmask_adjust(__u32 *bitmask, struct inode *inode,
5431 struct nfs_server *server,
5432 struct nfs4_label *label)
5433{
5434
5435 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
5436
5437 if ((cache_validity & NFS_INO_INVALID_DATA) ||
5438 (cache_validity & NFS_INO_REVAL_PAGECACHE) ||
5439 (cache_validity & NFS_INO_REVAL_FORCED) ||
5440 (cache_validity & NFS_INO_INVALID_OTHER))
5441 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
5442
5443 if (cache_validity & NFS_INO_INVALID_ATIME)
5444 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
27c5d3fa
TM
5445 if (cache_validity & NFS_INO_INVALID_OTHER)
5446 bitmask[1] |= FATTR4_WORD1_MODE | FATTR4_WORD1_OWNER |
5447 FATTR4_WORD1_OWNER_GROUP |
5448 FATTR4_WORD1_NUMLINKS;
5449 if (label && label->len && cache_validity & NFS_INO_INVALID_LABEL)
76bd5c01 5450 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
27c5d3fa 5451 if (cache_validity & NFS_INO_INVALID_CHANGE)
76bd5c01 5452 bitmask[0] |= FATTR4_WORD0_CHANGE;
27c5d3fa
TM
5453 if (cache_validity & NFS_INO_INVALID_CTIME)
5454 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
76bd5c01
OK
5455 if (cache_validity & NFS_INO_INVALID_MTIME)
5456 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5457 if (cache_validity & NFS_INO_INVALID_SIZE)
5458 bitmask[0] |= FATTR4_WORD0_SIZE;
5459 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5460 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5461}
5462
d45f60c6 5463static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
fb91fb0e
AS
5464 struct rpc_message *msg,
5465 struct rpc_clnt **clnt)
1da177e4 5466{
d45f60c6 5467 struct nfs_server *server = NFS_SERVER(hdr->inode);
bdc7f021 5468
d45f60c6
WAA
5469 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5470 hdr->args.bitmask = NULL;
5471 hdr->res.fattr = NULL;
76bd5c01 5472 } else {
d45f60c6 5473 hdr->args.bitmask = server->cache_consistency_bitmask;
76bd5c01
OK
5474 nfs4_bitmask_adjust(hdr->args.bitmask, hdr->inode, server, NULL);
5475 }
5a37f851 5476
d45f60c6
WAA
5477 if (!hdr->pgio_done_cb)
5478 hdr->pgio_done_cb = nfs4_write_done_cb;
5479 hdr->res.server = server;
5480 hdr->timestamp = jiffies;
1da177e4 5481
bdc7f021 5482 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
cd1b659d 5483 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
fb91fb0e 5484 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
1da177e4
LT
5485}
5486
0b7c0153 5487static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
c6cb80d0 5488{
42e1cca7 5489 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
d9afbd1b
TM
5490 &data->args.seq_args,
5491 &data->res.seq_res,
5492 task);
1da177e4
LT
5493}
5494
0b7c0153 5495static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
1da177e4 5496{
1da177e4 5497 struct inode *inode = data->inode;
14516c3a 5498
cc668ab3 5499 trace_nfs4_commit(data, task->tk_status);
8478eaa1
N
5500 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5501 NULL, NULL) == -EAGAIN) {
d00c5d43 5502 rpc_restart_call_prepare(task);
788e7a89 5503 return -EAGAIN;
1da177e4 5504 }
788e7a89 5505 return 0;
1da177e4
LT
5506}
5507
0b7c0153 5508static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5f452431
FI
5509{
5510 if (!nfs4_sequence_done(task, &data->res.seq_res))
5511 return -EAGAIN;
0b7c0153 5512 return data->commit_done_cb(task, data);
5f452431
FI
5513}
5514
e9ae1ee2
AS
5515static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5516 struct rpc_clnt **clnt)
1da177e4 5517{
788e7a89 5518 struct nfs_server *server = NFS_SERVER(data->inode);
988b6dce 5519
0b7c0153
FI
5520 if (data->commit_done_cb == NULL)
5521 data->commit_done_cb = nfs4_commit_done_cb;
4f9838c7 5522 data->res.server = server;
bdc7f021 5523 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
fba83f34 5524 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
e9ae1ee2 5525 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
1da177e4
LT
5526}
5527
6b8d84e2
OK
5528static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5529 struct nfs_commitres *res)
5530{
5531 struct inode *dst_inode = file_inode(dst);
5532 struct nfs_server *server = NFS_SERVER(dst_inode);
5533 struct rpc_message msg = {
5534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5535 .rpc_argp = args,
5536 .rpc_resp = res,
5537 };
5538
5539 args->fh = NFS_FH(dst_inode);
5540 return nfs4_call_sync(server->client, server, &msg,
5541 &args->seq_args, &res->seq_res, 1);
5542}
5543
5544int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5545{
5546 struct nfs_commitargs args = {
5547 .offset = offset,
5548 .count = count,
5549 };
5550 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5551 struct nfs4_exception exception = { };
5552 int status;
5553
5554 do {
5555 status = _nfs4_proc_commit(dst, &args, res);
5556 status = nfs4_handle_exception(dst_server, status, &exception);
5557 } while (exception.retry);
5558
5559 return status;
5560}
5561
9bc4e3ca
CL
5562struct nfs4_renewdata {
5563 struct nfs_client *client;
5564 unsigned long timestamp;
5565};
5566
1da177e4
LT
5567/*
5568 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5569 * standalone procedure for queueing an asynchronous RENEW.
5570 */
9bc4e3ca 5571static void nfs4_renew_release(void *calldata)
dc96aef9 5572{
9bc4e3ca
CL
5573 struct nfs4_renewdata *data = calldata;
5574 struct nfs_client *clp = data->client;
dc96aef9 5575
212bf41d 5576 if (refcount_read(&clp->cl_count) > 1)
0851de06
AB
5577 nfs4_schedule_state_renewal(clp);
5578 nfs_put_client(clp);
9bc4e3ca 5579 kfree(data);
dc96aef9
AB
5580}
5581
9bc4e3ca 5582static void nfs4_renew_done(struct rpc_task *task, void *calldata)
1da177e4 5583{
9bc4e3ca
CL
5584 struct nfs4_renewdata *data = calldata;
5585 struct nfs_client *clp = data->client;
5586 unsigned long timestamp = data->timestamp;
1da177e4 5587
c6d01c6f 5588 trace_nfs4_renew_async(clp, task->tk_status);
f8aba1e8
CL
5589 switch (task->tk_status) {
5590 case 0:
5591 break;
5592 case -NFS4ERR_LEASE_MOVED:
5593 nfs4_schedule_lease_moved_recovery(clp);
5594 break;
5595 default:
95baa25c 5596 /* Unless we're shutting down, schedule state recovery! */
042b60be
TM
5597 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5598 return;
5599 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
0400a6b0 5600 nfs4_schedule_lease_recovery(clp);
042b60be
TM
5601 return;
5602 }
5603 nfs4_schedule_path_down_recovery(clp);
1da177e4 5604 }
452e9352 5605 do_renew_lease(clp, timestamp);
1da177e4
LT
5606}
5607
963d8fe5
TM
5608static const struct rpc_call_ops nfs4_renew_ops = {
5609 .rpc_call_done = nfs4_renew_done,
dc96aef9 5610 .rpc_release = nfs4_renew_release,
963d8fe5
TM
5611};
5612
a52458b4 5613static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
1da177e4
LT
5614{
5615 struct rpc_message msg = {
5616 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5617 .rpc_argp = clp,
b4454fe1 5618 .rpc_cred = cred,
1da177e4 5619 };
9bc4e3ca 5620 struct nfs4_renewdata *data;
1da177e4 5621
2f60ea6b
TM
5622 if (renew_flags == 0)
5623 return 0;
212bf41d 5624 if (!refcount_inc_not_zero(&clp->cl_count))
0851de06 5625 return -EIO;
b569ad34 5626 data = kmalloc(sizeof(*data), GFP_NOFS);
5c737cb2
DW
5627 if (data == NULL) {
5628 nfs_put_client(clp);
9bc4e3ca 5629 return -ENOMEM;
5c737cb2 5630 }
9bc4e3ca
CL
5631 data->client = clp;
5632 data->timestamp = jiffies;
bc7a05ca 5633 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
9bc4e3ca 5634 &nfs4_renew_ops, data);
1da177e4
LT
5635}
5636
a52458b4 5637static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
1da177e4
LT
5638{
5639 struct rpc_message msg = {
5640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5641 .rpc_argp = clp,
b4454fe1 5642 .rpc_cred = cred,
1da177e4
LT
5643 };
5644 unsigned long now = jiffies;
5645 int status;
5646
bc7a05ca 5647 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
1da177e4
LT
5648 if (status < 0)
5649 return status;
452e9352 5650 do_renew_lease(clp, now);
1da177e4
LT
5651 return 0;
5652}
5653
aa1870af
BF
5654static inline int nfs4_server_supports_acls(struct nfs_server *server)
5655{
7dd7d959 5656 return server->caps & NFS_CAP_ACLS;
aa1870af
BF
5657}
5658
21f498c2
TM
5659/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5660 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
aa1870af
BF
5661 * the stack.
5662 */
21f498c2 5663#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
aa1870af 5664
ccde1e9c 5665int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
8fbcf237 5666 struct page **pages)
e9e3d724
NH
5667{
5668 struct page *newpage, **spages;
5669 int rc = 0;
5670 size_t len;
5671 spages = pages;
5672
5673 do {
21f498c2 5674 len = min_t(size_t, PAGE_SIZE, buflen);
e9e3d724
NH
5675 newpage = alloc_page(GFP_KERNEL);
5676
5677 if (newpage == NULL)
5678 goto unwind;
5679 memcpy(page_address(newpage), buf, len);
d9b67e1e
AS
5680 buf += len;
5681 buflen -= len;
e9e3d724
NH
5682 *pages++ = newpage;
5683 rc++;
5684 } while (buflen != 0);
5685
5686 return rc;
5687
5688unwind:
5689 for(; rc > 0; rc--)
5690 __free_page(spages[rc-1]);
5691 return -ENOMEM;
5692}
5693
e50a1c2e
BF
5694struct nfs4_cached_acl {
5695 int cached;
5696 size_t len;
5601cda8 5697 char data[];
e50a1c2e
BF
5698};
5699
5700static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5701{
5702 struct nfs_inode *nfsi = NFS_I(inode);
5703
5704 spin_lock(&inode->i_lock);
5705 kfree(nfsi->nfs4_acl);
5706 nfsi->nfs4_acl = acl;
5707 spin_unlock(&inode->i_lock);
5708}
5709
5710static void nfs4_zap_acl_attr(struct inode *inode)
5711{
5712 nfs4_set_cached_acl(inode, NULL);
5713}
5714
5715static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5716{
5717 struct nfs_inode *nfsi = NFS_I(inode);
5718 struct nfs4_cached_acl *acl;
5719 int ret = -ENOENT;
5720
5721 spin_lock(&inode->i_lock);
5722 acl = nfsi->nfs4_acl;
5723 if (acl == NULL)
5724 goto out;
5725 if (buf == NULL) /* user is just asking for length */
5726 goto out_len;
5727 if (acl->cached == 0)
5728 goto out;
5729 ret = -ERANGE; /* see getxattr(2) man page */
5730 if (acl->len > buflen)
5731 goto out;
5732 memcpy(buf, acl->data, acl->len);
5733out_len:
5734 ret = acl->len;
5735out:
5736 spin_unlock(&inode->i_lock);
5737 return ret;
5738}
5739
5794d21e 5740static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
e50a1c2e
BF
5741{
5742 struct nfs4_cached_acl *acl;
b291f1b1 5743 size_t buflen = sizeof(*acl) + acl_len;
e50a1c2e 5744
1f1ea6c2 5745 if (buflen <= PAGE_SIZE) {
b291f1b1 5746 acl = kmalloc(buflen, GFP_KERNEL);
e50a1c2e
BF
5747 if (acl == NULL)
5748 goto out;
5749 acl->cached = 1;
5794d21e 5750 _copy_from_pages(acl->data, pages, pgbase, acl_len);
e50a1c2e
BF
5751 } else {
5752 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5753 if (acl == NULL)
5754 goto out;
5755 acl->cached = 0;
5756 }
5757 acl->len = acl_len;
5758out:
5759 nfs4_set_cached_acl(inode, acl);
5760}
5761
bf118a34
AA
5762/*
5763 * The getxattr API returns the required buffer length when called with a
5764 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5765 * the required buf. On a NULL buf, we send a page of data to the server
5766 * guessing that the ACL request can be serviced by a page. If so, we cache
5767 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5768 * the cache. If not so, we throw away the page, and cache the required
5769 * length. The next getxattr call will then produce another round trip to
5770 * the server, this time with the input buf of the required size.
5771 */
16b4289c 5772static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
aa1870af 5773{
62a1573f 5774 struct page **pages;
aa1870af
BF
5775 struct nfs_getaclargs args = {
5776 .fh = NFS_FH(inode),
aa1870af
BF
5777 .acl_len = buflen,
5778 };
663c79b3
BH
5779 struct nfs_getaclres res = {
5780 .acl_len = buflen,
5781 };
aa1870af
BF
5782 struct rpc_message msg = {
5783 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5784 .rpc_argp = &args,
663c79b3 5785 .rpc_resp = &res,
aa1870af 5786 };
62a1573f 5787 unsigned int npages;
21f498c2 5788 int ret = -ENOMEM, i;
62a1573f
OK
5789 struct nfs_server *server = NFS_SERVER(inode);
5790
5791 if (buflen == 0)
5792 buflen = server->rsize;
5793
5794 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5795 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5796 if (!pages)
5797 return -ENOMEM;
aa1870af 5798
62a1573f 5799 args.acl_pages = pages;
5a006899 5800
bf118a34
AA
5801 for (i = 0; i < npages; i++) {
5802 pages[i] = alloc_page(GFP_KERNEL);
5803 if (!pages[i])
5804 goto out_free;
e50a1c2e 5805 }
5a006899
SP
5806
5807 /* for decoding across pages */
5808 res.acl_scratch = alloc_page(GFP_KERNEL);
5809 if (!res.acl_scratch)
5810 goto out_free;
5811
bf118a34 5812 args.acl_len = npages * PAGE_SIZE;
5a006899 5813
de040bec 5814 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
bf118a34
AA
5815 __func__, buf, buflen, npages, args.acl_len);
5816 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5817 &msg, &args.seq_args, &res.seq_res, 0);
e50a1c2e
BF
5818 if (ret)
5819 goto out_free;
bf118a34 5820
1f1ea6c2
TM
5821 /* Handle the case where the passed-in buffer is too short */
5822 if (res.acl_flags & NFS4_ACL_TRUNC) {
5823 /* Did the user only issue a request for the acl length? */
5824 if (buf == NULL)
5825 goto out_ok;
e50a1c2e 5826 ret = -ERANGE;
1f1ea6c2 5827 goto out_free;
e50a1c2e 5828 }
1f1ea6c2 5829 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
7d3e91a8
SW
5830 if (buf) {
5831 if (res.acl_len > buflen) {
5832 ret = -ERANGE;
5833 goto out_free;
5834 }
1f1ea6c2 5835 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
7d3e91a8 5836 }
1f1ea6c2
TM
5837out_ok:
5838 ret = res.acl_len;
e50a1c2e 5839out_free:
bf118a34
AA
5840 for (i = 0; i < npages; i++)
5841 if (pages[i])
5842 __free_page(pages[i]);
331818f1
TM
5843 if (res.acl_scratch)
5844 __free_page(res.acl_scratch);
62a1573f 5845 kfree(pages);
aa1870af
BF
5846 return ret;
5847}
5848
16b4289c
TM
5849static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5850{
0688e64b
TM
5851 struct nfs4_exception exception = {
5852 .interruptible = true,
5853 };
16b4289c
TM
5854 ssize_t ret;
5855 do {
5856 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
c1578b76 5857 trace_nfs4_get_acl(inode, ret);
16b4289c
TM
5858 if (ret >= 0)
5859 break;
5860 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5861 } while (exception.retry);
5862 return ret;
5863}
5864
e50a1c2e
BF
5865static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5866{
5867 struct nfs_server *server = NFS_SERVER(inode);
5868 int ret;
5869
5870 if (!nfs4_server_supports_acls(server))
5871 return -EOPNOTSUPP;
5872 ret = nfs_revalidate_inode(server, inode);
5873 if (ret < 0)
5874 return ret;
08a22b39
AK
5875 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5876 nfs_zap_acl_cache(inode);
e50a1c2e
BF
5877 ret = nfs4_read_cached_acl(inode, buf, buflen);
5878 if (ret != -ENOENT)
bf118a34
AA
5879 /* -ENOENT is returned if there is no ACL or if there is an ACL
5880 * but no cached acl data, just the acl length */
e50a1c2e
BF
5881 return ret;
5882 return nfs4_get_acl_uncached(inode, buf, buflen);
5883}
5884
16b4289c 5885static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4b580ee3
BF
5886{
5887 struct nfs_server *server = NFS_SERVER(inode);
5888 struct page *pages[NFS4ACL_MAXPAGES];
5889 struct nfs_setaclargs arg = {
5890 .fh = NFS_FH(inode),
5891 .acl_pages = pages,
5892 .acl_len = buflen,
5893 };
73c403a9 5894 struct nfs_setaclres res;
4b580ee3
BF
5895 struct rpc_message msg = {
5896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5897 .rpc_argp = &arg,
73c403a9 5898 .rpc_resp = &res,
4b580ee3 5899 };
21f498c2 5900 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
e9e3d724 5901 int ret, i;
4b580ee3 5902
ef82cd66
BF
5903 /* You can't remove system.nfs4_acl: */
5904 if (buflen == 0)
5905 return -EINVAL;
4b580ee3
BF
5906 if (!nfs4_server_supports_acls(server))
5907 return -EOPNOTSUPP;
21f498c2
TM
5908 if (npages > ARRAY_SIZE(pages))
5909 return -ERANGE;
ccde1e9c 5910 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
e9e3d724
NH
5911 if (i < 0)
5912 return i;
c01d3645 5913 nfs4_inode_make_writeable(inode);
7c513058 5914 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
e9e3d724
NH
5915
5916 /*
5917 * Free each page after tx, so the only ref left is
5918 * held by the network stack
5919 */
5920 for (; i > 0; i--)
5921 put_page(pages[i-1]);
5922
08a22b39
AK
5923 /*
5924 * Acl update can result in inode attribute update.
5925 * so mark the attribute cache invalid.
5926 */
5927 spin_lock(&inode->i_lock);
16e14375 5928 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
472f761e
TM
5929 | NFS_INO_INVALID_CTIME
5930 | NFS_INO_REVAL_FORCED;
08a22b39 5931 spin_unlock(&inode->i_lock);
f41f7418
TM
5932 nfs_access_zap_cache(inode);
5933 nfs_zap_acl_cache(inode);
4b580ee3
BF
5934 return ret;
5935}
5936
16b4289c
TM
5937static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5938{
5939 struct nfs4_exception exception = { };
5940 int err;
5941 do {
c1578b76
TM
5942 err = __nfs4_proc_set_acl(inode, buf, buflen);
5943 trace_nfs4_set_acl(inode, err);
5944 err = nfs4_handle_exception(NFS_SERVER(inode), err,
16b4289c
TM
5945 &exception);
5946 } while (exception.retry);
5947 return err;
5948}
5949
aa9c2669
DQ
5950#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5951static int _nfs4_get_security_label(struct inode *inode, void *buf,
5952 size_t buflen)
5953{
5954 struct nfs_server *server = NFS_SERVER(inode);
5955 struct nfs_fattr fattr;
5956 struct nfs4_label label = {0, 0, buflen, buf};
5957
5958 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
fcb63a9b 5959 struct nfs4_getattr_arg arg = {
aa9c2669
DQ
5960 .fh = NFS_FH(inode),
5961 .bitmask = bitmask,
5962 };
5963 struct nfs4_getattr_res res = {
5964 .fattr = &fattr,
5965 .label = &label,
5966 .server = server,
5967 };
5968 struct rpc_message msg = {
5969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
fcb63a9b 5970 .rpc_argp = &arg,
aa9c2669
DQ
5971 .rpc_resp = &res,
5972 };
5973 int ret;
5974
5975 nfs_fattr_init(&fattr);
5976
fcb63a9b 5977 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
aa9c2669
DQ
5978 if (ret)
5979 return ret;
5980 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5981 return -ENOENT;
a7f9e02f 5982 return label.len;
aa9c2669
DQ
5983}
5984
5985static int nfs4_get_security_label(struct inode *inode, void *buf,
5986 size_t buflen)
5987{
0688e64b
TM
5988 struct nfs4_exception exception = {
5989 .interruptible = true,
5990 };
aa9c2669
DQ
5991 int err;
5992
5993 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5994 return -EOPNOTSUPP;
5995
5996 do {
c1578b76
TM
5997 err = _nfs4_get_security_label(inode, buf, buflen);
5998 trace_nfs4_get_security_label(inode, err);
5999 err = nfs4_handle_exception(NFS_SERVER(inode), err,
aa9c2669
DQ
6000 &exception);
6001 } while (exception.retry);
6002 return err;
6003}
6004
6005static int _nfs4_do_set_security_label(struct inode *inode,
6006 struct nfs4_label *ilabel,
6007 struct nfs_fattr *fattr,
6008 struct nfs4_label *olabel)
6009{
6010
6011 struct iattr sattr = {0};
6012 struct nfs_server *server = NFS_SERVER(inode);
6013 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
12207f69 6014 struct nfs_setattrargs arg = {
d9b67e1e
AS
6015 .fh = NFS_FH(inode),
6016 .iap = &sattr,
aa9c2669
DQ
6017 .server = server,
6018 .bitmask = bitmask,
6019 .label = ilabel,
6020 };
6021 struct nfs_setattrres res = {
6022 .fattr = fattr,
6023 .label = olabel,
6024 .server = server,
6025 };
6026 struct rpc_message msg = {
d9b67e1e
AS
6027 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6028 .rpc_argp = &arg,
6029 .rpc_resp = &res,
aa9c2669
DQ
6030 };
6031 int status;
6032
12207f69 6033 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
aa9c2669 6034
12207f69 6035 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
aa9c2669
DQ
6036 if (status)
6037 dprintk("%s failed: %d\n", __func__, status);
6038
6039 return status;
6040}
6041
6042static int nfs4_do_set_security_label(struct inode *inode,
6043 struct nfs4_label *ilabel,
6044 struct nfs_fattr *fattr,
6045 struct nfs4_label *olabel)
6046{
6047 struct nfs4_exception exception = { };
6048 int err;
6049
6050 do {
c1578b76
TM
6051 err = _nfs4_do_set_security_label(inode, ilabel,
6052 fattr, olabel);
6053 trace_nfs4_set_security_label(inode, err);
6054 err = nfs4_handle_exception(NFS_SERVER(inode), err,
aa9c2669
DQ
6055 &exception);
6056 } while (exception.retry);
6057 return err;
6058}
6059
6060static int
59301226 6061nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
aa9c2669
DQ
6062{
6063 struct nfs4_label ilabel, *olabel = NULL;
6064 struct nfs_fattr fattr;
aa9c2669
DQ
6065 int status;
6066
6067 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6068 return -EOPNOTSUPP;
6069
6070 nfs_fattr_init(&fattr);
6071
6072 ilabel.pi = 0;
6073 ilabel.lfs = 0;
6074 ilabel.label = (char *)buf;
6075 ilabel.len = buflen;
6076
aa9c2669
DQ
6077 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6078 if (IS_ERR(olabel)) {
6079 status = -PTR_ERR(olabel);
6080 goto out;
6081 }
6082
6083 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6084 if (status == 0)
6085 nfs_setsecurity(inode, &fattr, olabel);
6086
6087 nfs4_label_free(olabel);
6088out:
aa9c2669
DQ
6089 return status;
6090}
6091#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6092
6093
f092075d
CL
6094static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6095 nfs4_verifier *bootverf)
cd93710e
CL
6096{
6097 __be32 verf[2];
6098
2c820d9a
CL
6099 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6100 /* An impossible timestamp guarantees this value
6101 * will never match a generated boot time. */
2f86e091
DD
6102 verf[0] = cpu_to_be32(U32_MAX);
6103 verf[1] = cpu_to_be32(U32_MAX);
2c820d9a 6104 } else {
f092075d 6105 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
2f86e091
DD
6106 u64 ns = ktime_to_ns(nn->boot_time);
6107
6108 verf[0] = cpu_to_be32(ns >> 32);
6109 verf[1] = cpu_to_be32(ns);
2c820d9a 6110 }
cd93710e
CL
6111 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6112}
6113
1aee5513 6114static size_t
39d43d16 6115nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
1aee5513 6116{
39d43d16
TM
6117 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6118 struct nfs_netns_client *nn_clp = nn->nfs_client;
6119 const char *id;
6120
1aee5513
TM
6121 buf[0] = '\0';
6122
39d43d16
TM
6123 if (nn_clp) {
6124 rcu_read_lock();
6125 id = rcu_dereference(nn_clp->identifier);
6126 if (id)
6127 strscpy(buf, id, buflen);
6128 rcu_read_unlock();
6129 }
6130
6131 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
1aee5513
TM
6132 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6133
6134 return strlen(buf);
6135}
6136
a3192688
JL
6137static int
6138nfs4_init_nonuniform_client_string(struct nfs_client *clp)
e984a55a 6139{
1aee5513
TM
6140 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6141 size_t buflen;
a3192688
JL
6142 size_t len;
6143 char *str;
e984a55a 6144
ceb3a16c 6145 if (clp->cl_owner_id != NULL)
a3192688 6146 return 0;
4a3e5779 6147
a3192688 6148 rcu_read_lock();
848a4eb2
CL
6149 len = 14 +
6150 strlen(clp->cl_rpcclient->cl_nodename) +
6151 1 +
a3192688 6152 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
a3192688
JL
6153 1;
6154 rcu_read_unlock();
1aee5513 6155
39d43d16 6156 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
1aee5513
TM
6157 if (buflen)
6158 len += buflen + 1;
6159
a3192688
JL
6160 if (len > NFS4_OPAQUE_LIMIT + 1)
6161 return -EINVAL;
6162
6163 /*
6164 * Since this string is allocated at mount time, and held until the
6165 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6166 * about a memory-reclaim deadlock.
6167 */
6168 str = kmalloc(len, GFP_KERNEL);
6169 if (!str)
6170 return -ENOMEM;
ceb3a16c 6171
e984a55a 6172 rcu_read_lock();
1aee5513 6173 if (buflen)
025bb9f8 6174 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
1aee5513 6175 clp->cl_rpcclient->cl_nodename, buf,
848a4eb2 6176 rpc_peeraddr2str(clp->cl_rpcclient,
025bb9f8 6177 RPC_DISPLAY_ADDR));
848a4eb2 6178 else
025bb9f8 6179 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
848a4eb2
CL
6180 clp->cl_rpcclient->cl_nodename,
6181 rpc_peeraddr2str(clp->cl_rpcclient,
025bb9f8 6182 RPC_DISPLAY_ADDR));
e984a55a 6183 rcu_read_unlock();
a3192688 6184
a3192688
JL
6185 clp->cl_owner_id = str;
6186 return 0;
e984a55a
CL
6187}
6188
873e3851
JL
6189static int
6190nfs4_init_uniform_client_string(struct nfs_client *clp)
e984a55a 6191{
1aee5513
TM
6192 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6193 size_t buflen;
873e3851
JL
6194 size_t len;
6195 char *str;
ceb3a16c
TM
6196
6197 if (clp->cl_owner_id != NULL)
873e3851 6198 return 0;
6f2ea7f2 6199
873e3851
JL
6200 len = 10 + 10 + 1 + 10 + 1 +
6201 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6202
39d43d16 6203 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
1aee5513
TM
6204 if (buflen)
6205 len += buflen + 1;
6206
873e3851
JL
6207 if (len > NFS4_OPAQUE_LIMIT + 1)
6208 return -EINVAL;
6209
6210 /*
6211 * Since this string is allocated at mount time, and held until the
6212 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6213 * about a memory-reclaim deadlock.
6214 */
6215 str = kmalloc(len, GFP_KERNEL);
6216 if (!str)
6217 return -ENOMEM;
6218
1aee5513
TM
6219 if (buflen)
6220 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6221 clp->rpc_ops->version, clp->cl_minorversion,
6222 buf, clp->cl_rpcclient->cl_nodename);
6223 else
6224 scnprintf(str, len, "Linux NFSv%u.%u %s",
6225 clp->rpc_ops->version, clp->cl_minorversion,
6226 clp->cl_rpcclient->cl_nodename);
873e3851
JL
6227 clp->cl_owner_id = str;
6228 return 0;
e984a55a
CL
6229}
6230
706cb8db
CL
6231/*
6232 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6233 * services. Advertise one based on the address family of the
6234 * clientaddr.
6235 */
6236static unsigned int
6237nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6238{
6239 if (strchr(clp->cl_ipaddr, ':') != NULL)
6240 return scnprintf(buf, len, "tcp6");
6241 else
6242 return scnprintf(buf, len, "tcp");
6243}
6244
f11b2a1c
JL
6245static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6246{
6247 struct nfs4_setclientid *sc = calldata;
6248
6249 if (task->tk_status == 0)
6250 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6251}
6252
6253static const struct rpc_call_ops nfs4_setclientid_ops = {
6254 .rpc_call_done = nfs4_setclientid_done,
6255};
6256
6bbb4ae8
CL
6257/**
6258 * nfs4_proc_setclientid - Negotiate client ID
6259 * @clp: state data structure
6260 * @program: RPC program for NFSv4 callback service
6261 * @port: IP port number for NFS4 callback service
a52458b4 6262 * @cred: credential to use for this call
6bbb4ae8
CL
6263 * @res: where to place the result
6264 *
6265 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6266 */
bb8b27e5 6267int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
a52458b4 6268 unsigned short port, const struct cred *cred,
bb8b27e5 6269 struct nfs4_setclientid_res *res)
1da177e4
LT
6270{
6271 nfs4_verifier sc_verifier;
6272 struct nfs4_setclientid setclientid = {
6273 .sc_verifier = &sc_verifier,
6274 .sc_prog = program,
3a6bb738 6275 .sc_clnt = clp,
1da177e4
LT
6276 };
6277 struct rpc_message msg = {
6278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6279 .rpc_argp = &setclientid,
bb8b27e5 6280 .rpc_resp = res,
286d7d6a 6281 .rpc_cred = cred,
1da177e4 6282 };
f11b2a1c
JL
6283 struct rpc_task_setup task_setup_data = {
6284 .rpc_client = clp->cl_rpcclient,
6285 .rpc_message = &msg,
6286 .callback_ops = &nfs4_setclientid_ops,
6287 .callback_data = &setclientid,
5a0c257f 6288 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
f11b2a1c 6289 };
7dc2993a 6290 unsigned long now = jiffies;
6bbb4ae8 6291 int status;
1da177e4 6292
de734831 6293 /* nfs_client_id4 */
f092075d 6294 nfs4_init_boot_verifier(clp, &sc_verifier);
873e3851
JL
6295
6296 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6297 status = nfs4_init_uniform_client_string(clp);
6298 else
a3192688 6299 status = nfs4_init_nonuniform_client_string(clp);
873e3851
JL
6300
6301 if (status)
6302 goto out;
3a6bb738 6303
de734831 6304 /* cb_client4 */
706cb8db
CL
6305 setclientid.sc_netid_len =
6306 nfs4_init_callback_netid(clp,
6307 setclientid.sc_netid,
6308 sizeof(setclientid.sc_netid));
de734831 6309 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
d4d3c507 6310 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
1da177e4
LT
6311 clp->cl_ipaddr, port >> 8, port & 255);
6312
3a6bb738 6313 dprintk("NFS call setclientid auth=%s, '%s'\n",
6bbb4ae8 6314 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3a6bb738 6315 clp->cl_owner_id);
dae40965
AS
6316
6317 status = nfs4_call_sync_custom(&task_setup_data);
f11b2a1c 6318 if (setclientid.sc_cred) {
1047ec86 6319 kfree(clp->cl_acceptor);
f11b2a1c
JL
6320 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6321 put_rpccred(setclientid.sc_cred);
6322 }
7dc2993a
RM
6323
6324 if (status == 0)
6325 do_renew_lease(clp, now);
f11b2a1c 6326out:
c6d01c6f 6327 trace_nfs4_setclientid(clp, status);
6bbb4ae8
CL
6328 dprintk("NFS reply setclientid: %d\n", status);
6329 return status;
1da177e4
LT
6330}
6331
6bbb4ae8
CL
6332/**
6333 * nfs4_proc_setclientid_confirm - Confirm client ID
6334 * @clp: state data structure
302fad7b 6335 * @arg: result of a previous SETCLIENTID
a52458b4 6336 * @cred: credential to use for this call
6bbb4ae8
CL
6337 *
6338 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6339 */
fd954ae1 6340int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
bb8b27e5 6341 struct nfs4_setclientid_res *arg,
a52458b4 6342 const struct cred *cred)
1da177e4 6343{
1da177e4
LT
6344 struct rpc_message msg = {
6345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
bb8b27e5 6346 .rpc_argp = arg,
286d7d6a 6347 .rpc_cred = cred,
1da177e4 6348 };
1da177e4
LT
6349 int status;
6350
6bbb4ae8
CL
6351 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6352 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6353 clp->cl_clientid);
5a0c257f
N
6354 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6355 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
c6d01c6f 6356 trace_nfs4_setclientid_confirm(clp, status);
6bbb4ae8 6357 dprintk("NFS reply setclientid_confirm: %d\n", status);
1da177e4
LT
6358 return status;
6359}
6360
fe650407
TM
6361struct nfs4_delegreturndata {
6362 struct nfs4_delegreturnargs args;
fa178f29 6363 struct nfs4_delegreturnres res;
fe650407
TM
6364 struct nfs_fh fh;
6365 nfs4_stateid stateid;
26e976a8 6366 unsigned long timestamp;
586f1c39
TM
6367 struct {
6368 struct nfs4_layoutreturn_args arg;
6369 struct nfs4_layoutreturn_res res;
4d796d75 6370 struct nfs4_xdr_opaque_data ld_private;
586f1c39
TM
6371 u32 roc_barrier;
6372 bool roc;
6373 } lr;
fa178f29 6374 struct nfs_fattr fattr;
fe650407 6375 int rpc_status;
039b756a 6376 struct inode *inode;
fe650407
TM
6377};
6378
fe650407
TM
6379static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6380{
6381 struct nfs4_delegreturndata *data = calldata;
e0dba012
TM
6382 struct nfs4_exception exception = {
6383 .inode = data->inode,
6384 .stateid = &data->stateid,
6385 };
938e1010 6386
14516c3a
TM
6387 if (!nfs4_sequence_done(task, &data->res.seq_res))
6388 return;
938e1010 6389
ca8acf8d 6390 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
586f1c39
TM
6391
6392 /* Handle Layoutreturn errors */
078000d0
TM
6393 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6394 &data->res.lr_ret) == -EAGAIN)
287a9c55 6395 goto out_restart;
586f1c39 6396
79708861 6397 switch (task->tk_status) {
79708861 6398 case 0:
fa178f29 6399 renew_lease(data->res.server, data->timestamp);
23ea44c2 6400 break;
c97cf606
TM
6401 case -NFS4ERR_ADMIN_REVOKED:
6402 case -NFS4ERR_DELEG_REVOKED:
26d36301
TM
6403 case -NFS4ERR_EXPIRED:
6404 nfs4_free_revoked_stateid(data->res.server,
6405 data->args.stateid,
6406 task->tk_msg.rpc_cred);
df561f66 6407 fallthrough;
c97cf606 6408 case -NFS4ERR_BAD_STATEID:
c97cf606 6409 case -NFS4ERR_STALE_STATEID:
244fcd2f 6410 case -ETIMEDOUT:
c97cf606
TM
6411 task->tk_status = 0;
6412 break;
12f275cd 6413 case -NFS4ERR_OLD_STATEID:
246afc0a
TM
6414 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6415 nfs4_stateid_seqid_inc(&data->stateid);
70d136b2
TM
6416 if (data->args.bitmask) {
6417 data->args.bitmask = NULL;
6418 data->res.fattr = NULL;
6419 }
246afc0a 6420 goto out_restart;
8ac2b422
TM
6421 case -NFS4ERR_ACCESS:
6422 if (data->args.bitmask) {
6423 data->args.bitmask = NULL;
6424 data->res.fattr = NULL;
140087fd 6425 goto out_restart;
8ac2b422 6426 }
df561f66 6427 fallthrough;
79708861 6428 default:
e0dba012
TM
6429 task->tk_status = nfs4_async_handle_exception(task,
6430 data->res.server, task->tk_status,
6431 &exception);
6432 if (exception.retry)
140087fd 6433 goto out_restart;
79708861 6434 }
d51f91d2 6435 nfs_delegation_mark_returned(data->inode, data->args.stateid);
79708861 6436 data->rpc_status = task->tk_status;
140087fd 6437 return;
140087fd
TM
6438out_restart:
6439 task->tk_status = 0;
6440 rpc_restart_call_prepare(task);
fe650407
TM
6441}
6442
6443static void nfs4_delegreturn_release(void *calldata)
6444{
039b756a 6445 struct nfs4_delegreturndata *data = calldata;
ea7c38fe 6446 struct inode *inode = data->inode;
039b756a 6447
078000d0
TM
6448 if (data->lr.roc)
6449 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6450 data->res.lr_ret);
ea7c38fe 6451 if (inode) {
0bc2c9b4 6452 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
ea7c38fe
TM
6453 nfs_iput_and_deactive(inode);
6454 }
fe650407
TM
6455 kfree(calldata);
6456}
6457
938e1010
AA
6458static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6459{
6460 struct nfs4_delegreturndata *d_data;
c8bf7073 6461 struct pnfs_layout_hdr *lo;
938e1010
AA
6462
6463 d_data = (struct nfs4_delegreturndata *)data;
6464
5326de9e
TM
6465 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6466 nfs4_sequence_done(task, &d_data->res.seq_res);
500d701f 6467 return;
5326de9e 6468 }
500d701f 6469
c8bf7073
TM
6470 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6471 if (lo && !pnfs_layout_is_valid(lo)) {
6472 d_data->args.lr_args = NULL;
6473 d_data->res.lr_res = NULL;
6474 }
6475
42e1cca7 6476 nfs4_setup_sequence(d_data->res.server->nfs_client,
d9afbd1b
TM
6477 &d_data->args.seq_args,
6478 &d_data->res.seq_res,
6479 task);
938e1010 6480}
938e1010 6481
c8d149f3 6482static const struct rpc_call_ops nfs4_delegreturn_ops = {
938e1010 6483 .rpc_call_prepare = nfs4_delegreturn_prepare,
fe650407
TM
6484 .rpc_call_done = nfs4_delegreturn_done,
6485 .rpc_release = nfs4_delegreturn_release,
6486};
6487
a52458b4 6488static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
fe650407
TM
6489{
6490 struct nfs4_delegreturndata *data;
fa178f29 6491 struct nfs_server *server = NFS_SERVER(inode);
fe650407 6492 struct rpc_task *task;
5138fde0
TM
6493 struct rpc_message msg = {
6494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6495 .rpc_cred = cred,
6496 };
c970aa85
TM
6497 struct rpc_task_setup task_setup_data = {
6498 .rpc_client = server->client,
5138fde0 6499 .rpc_message = &msg,
c970aa85 6500 .callback_ops = &nfs4_delegreturn_ops,
f304a809 6501 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
c970aa85 6502 };
e6f81075 6503 int status = 0;
fe650407 6504
8535b2be 6505 data = kzalloc(sizeof(*data), GFP_NOFS);
fe650407
TM
6506 if (data == NULL)
6507 return -ENOMEM;
fba83f34 6508 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
99ade3c7
AE
6509
6510 nfs4_state_protect(server->nfs_client,
6511 NFS_SP4_MACH_CRED_CLEANUP,
6512 &task_setup_data.rpc_client, &msg);
6513
fe650407
TM
6514 data->args.fhandle = &data->fh;
6515 data->args.stateid = &data->stateid;
9e907fec 6516 data->args.bitmask = server->cache_consistency_bitmask;
76bd5c01 6517 nfs4_bitmask_adjust(data->args.bitmask, inode, server, NULL);
fe650407 6518 nfs_copy_fh(&data->fh, NFS_FH(inode));
f597c537 6519 nfs4_stateid_copy(&data->stateid, stateid);
fa178f29
TM
6520 data->res.fattr = &data->fattr;
6521 data->res.server = server;
586f1c39 6522 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
4d796d75 6523 data->lr.arg.ld_private = &data->lr.ld_private;
5138fde0 6524 nfs_fattr_init(data->res.fattr);
26e976a8 6525 data->timestamp = jiffies;
fe650407 6526 data->rpc_status = 0;
ea7c38fe 6527 data->inode = nfs_igrab_and_active(inode);
078000d0
TM
6528 if (data->inode || issync) {
6529 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6530 cred);
1c5bd76d
TM
6531 if (data->lr.roc) {
6532 data->args.lr_args = &data->lr.arg;
6533 data->res.lr_res = &data->lr.res;
6534 }
6535 }
fe650407 6536
c970aa85 6537 task_setup_data.callback_data = data;
1174dd1f
TM
6538 msg.rpc_argp = &data->args;
6539 msg.rpc_resp = &data->res;
c970aa85 6540 task = rpc_run_task(&task_setup_data);
7a1218a2 6541 if (IS_ERR(task))
fe650407 6542 return PTR_ERR(task);
e6f81075
TM
6543 if (!issync)
6544 goto out;
820bf85c 6545 status = rpc_wait_for_completion_task(task);
e6f81075
TM
6546 if (status != 0)
6547 goto out;
6548 status = data->rpc_status;
e6f81075 6549out:
e6b3c4db 6550 rpc_put_task(task);
fe650407 6551 return status;
1da177e4
LT
6552}
6553
a52458b4 6554int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
1da177e4
LT
6555{
6556 struct nfs_server *server = NFS_SERVER(inode);
6557 struct nfs4_exception exception = { };
6558 int err;
6559 do {
e6f81075 6560 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
48c9579a 6561 trace_nfs4_delegreturn(inode, stateid, err);
1da177e4
LT
6562 switch (err) {
6563 case -NFS4ERR_STALE_STATEID:
6564 case -NFS4ERR_EXPIRED:
1da177e4
LT
6565 case 0:
6566 return 0;
6567 }
6568 err = nfs4_handle_exception(server, err, &exception);
6569 } while (exception.retry);
6570 return err;
6571}
6572
1da177e4
LT
6573static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6574{
6575 struct inode *inode = state->inode;
6576 struct nfs_server *server = NFS_SERVER(inode);
7539bbab 6577 struct nfs_client *clp = server->nfs_client;
911d1aaf 6578 struct nfs_lockt_args arg = {
1da177e4 6579 .fh = NFS_FH(inode),
911d1aaf 6580 .fl = request,
1da177e4 6581 };
911d1aaf
TM
6582 struct nfs_lockt_res res = {
6583 .denied = request,
1da177e4
LT
6584 };
6585 struct rpc_message msg = {
6586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
d9b67e1e
AS
6587 .rpc_argp = &arg,
6588 .rpc_resp = &res,
1da177e4
LT
6589 .rpc_cred = state->owner->so_cred,
6590 };
1da177e4
LT
6591 struct nfs4_lock_state *lsp;
6592 int status;
6593
911d1aaf 6594 arg.lock_owner.clientid = clp->cl_clientid;
8d0a8a9d
TM
6595 status = nfs4_set_lock_state(state, request);
6596 if (status != 0)
6597 goto out;
6598 lsp = request->fl_u.nfs4_fl.owner;
48c22eb2 6599 arg.lock_owner.id = lsp->ls_seqid.owner_id;
d035c36c 6600 arg.lock_owner.s_dev = server->s_dev;
7c513058 6601 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
911d1aaf
TM
6602 switch (status) {
6603 case 0:
6604 request->fl_type = F_UNLCK;
6605 break;
6606 case -NFS4ERR_DENIED:
6607 status = 0;
1da177e4 6608 }
70cc6487 6609 request->fl_ops->fl_release_private(request);
a6f951dd 6610 request->fl_ops = NULL;
8d0a8a9d 6611out:
1da177e4
LT
6612 return status;
6613}
6614
6615static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6616{
0688e64b
TM
6617 struct nfs4_exception exception = {
6618 .interruptible = true,
6619 };
1da177e4
LT
6620 int err;
6621
6622 do {
d1b748a5
TM
6623 err = _nfs4_proc_getlk(state, cmd, request);
6624 trace_nfs4_get_lock(request, state, cmd, err);
6625 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
1da177e4
LT
6626 &exception);
6627 } while (exception.retry);
6628 return err;
6629}
6630
32c6e7ee
TM
6631/*
6632 * Update the seqid of a lock stateid after receiving
6633 * NFS4ERR_OLD_STATEID
6634 */
6635static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6636 struct nfs4_lock_state *lsp)
6637{
6638 struct nfs4_state *state = lsp->ls_state;
6639 bool ret = false;
6640
6641 spin_lock(&state->state_lock);
6642 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6643 goto out;
6644 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6645 nfs4_stateid_seqid_inc(dst);
6646 else
6647 dst->seqid = lsp->ls_stateid.seqid;
6648 ret = true;
6649out:
6650 spin_unlock(&state->state_lock);
6651 return ret;
6652}
6653
6654static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6655 struct nfs4_lock_state *lsp)
6656{
6657 struct nfs4_state *state = lsp->ls_state;
6658 bool ret;
6659
6660 spin_lock(&state->state_lock);
6661 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6662 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6663 spin_unlock(&state->state_lock);
6664 return ret;
6665}
6666
faf5f49c 6667struct nfs4_unlockdata {
911d1aaf
TM
6668 struct nfs_locku_args arg;
6669 struct nfs_locku_res res;
faf5f49c
TM
6670 struct nfs4_lock_state *lsp;
6671 struct nfs_open_context *ctx;
f30cb757 6672 struct nfs_lock_context *l_ctx;
911d1aaf 6673 struct file_lock fl;
516285eb 6674 struct nfs_server *server;
26e976a8 6675 unsigned long timestamp;
faf5f49c
TM
6676};
6677
911d1aaf
TM
6678static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6679 struct nfs_open_context *ctx,
6680 struct nfs4_lock_state *lsp,
6681 struct nfs_seqid *seqid)
6682{
6683 struct nfs4_unlockdata *p;
32c6e7ee
TM
6684 struct nfs4_state *state = lsp->ls_state;
6685 struct inode *inode = state->inode;
911d1aaf 6686
8535b2be 6687 p = kzalloc(sizeof(*p), GFP_NOFS);
911d1aaf
TM
6688 if (p == NULL)
6689 return NULL;
6690 p->arg.fh = NFS_FH(inode);
6691 p->arg.fl = &p->fl;
6692 p->arg.seqid = seqid;
c1d51931 6693 p->res.seqid = seqid;
911d1aaf 6694 p->lsp = lsp;
911d1aaf
TM
6695 /* Ensure we don't close file until we're done freeing locks! */
6696 p->ctx = get_nfs_open_context(ctx);
f30cb757 6697 p->l_ctx = nfs_get_lock_context(ctx);
7b587e1a
N
6698 locks_init_lock(&p->fl);
6699 locks_copy_lock(&p->fl, fl);
911d1aaf 6700 p->server = NFS_SERVER(inode);
32c6e7ee
TM
6701 spin_lock(&state->state_lock);
6702 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6703 spin_unlock(&state->state_lock);
911d1aaf
TM
6704 return p;
6705}
6706
06f814a3 6707static void nfs4_locku_release_calldata(void *data)
faf5f49c 6708{
963d8fe5 6709 struct nfs4_unlockdata *calldata = data;
911d1aaf 6710 nfs_free_seqid(calldata->arg.seqid);
06f814a3 6711 nfs4_put_lock_state(calldata->lsp);
f30cb757 6712 nfs_put_lock_context(calldata->l_ctx);
06f814a3
TM
6713 put_nfs_open_context(calldata->ctx);
6714 kfree(calldata);
faf5f49c
TM
6715}
6716
963d8fe5 6717static void nfs4_locku_done(struct rpc_task *task, void *data)
faf5f49c 6718{
963d8fe5 6719 struct nfs4_unlockdata *calldata = data;
82571552
TM
6720 struct nfs4_exception exception = {
6721 .inode = calldata->lsp->ls_state->inode,
6722 .stateid = &calldata->arg.stateid,
6723 };
faf5f49c 6724
14516c3a
TM
6725 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6726 return;
faf5f49c
TM
6727 switch (task->tk_status) {
6728 case 0:
26e976a8 6729 renew_lease(calldata->server, calldata->timestamp);
75575ddf 6730 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
c69899a1
TM
6731 if (nfs4_update_lock_stateid(calldata->lsp,
6732 &calldata->res.stateid))
6733 break;
df561f66 6734 fallthrough;
26d36301
TM
6735 case -NFS4ERR_ADMIN_REVOKED:
6736 case -NFS4ERR_EXPIRED:
6737 nfs4_free_revoked_stateid(calldata->server,
6738 &calldata->arg.stateid,
6739 task->tk_msg.rpc_cred);
df561f66 6740 fallthrough;
9e33bed5 6741 case -NFS4ERR_BAD_STATEID:
faf5f49c 6742 case -NFS4ERR_STALE_STATEID:
32c6e7ee
TM
6743 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6744 calldata->lsp))
6745 rpc_restart_call_prepare(task);
6746 break;
6747 case -NFS4ERR_OLD_STATEID:
6748 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6749 calldata->lsp))
425c1d4e 6750 rpc_restart_call_prepare(task);
faf5f49c
TM
6751 break;
6752 default:
82571552
TM
6753 task->tk_status = nfs4_async_handle_exception(task,
6754 calldata->server, task->tk_status,
6755 &exception);
6756 if (exception.retry)
d00c5d43 6757 rpc_restart_call_prepare(task);
faf5f49c 6758 }
2b1bc308 6759 nfs_release_seqid(calldata->arg.seqid);
faf5f49c
TM
6760}
6761
4ce70ada 6762static void nfs4_locku_prepare(struct rpc_task *task, void *data)
faf5f49c 6763{
4ce70ada 6764 struct nfs4_unlockdata *calldata = data;
faf5f49c 6765
f30cb757
BC
6766 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6767 nfs_async_iocounter_wait(task, calldata->l_ctx))
6768 return;
6769
911d1aaf 6770 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
c8da19b9 6771 goto out_wait;
795a88c9 6772 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
963d8fe5 6773 /* Note: exit _without_ running nfs4_locku_done */
c8da19b9 6774 goto out_no_action;
faf5f49c 6775 }
26e976a8 6776 calldata->timestamp = jiffies;
42e1cca7 6777 if (nfs4_setup_sequence(calldata->server->nfs_client,
a893693c 6778 &calldata->arg.seq_args,
2240a9e2
TM
6779 &calldata->res.seq_res,
6780 task) != 0)
6781 nfs_release_seqid(calldata->arg.seqid);
c8da19b9
TM
6782 return;
6783out_no_action:
6784 task->tk_action = NULL;
6785out_wait:
6786 nfs4_sequence_done(task, &calldata->res.seq_res);
faf5f49c
TM
6787}
6788
963d8fe5 6789static const struct rpc_call_ops nfs4_locku_ops = {
4ce70ada 6790 .rpc_call_prepare = nfs4_locku_prepare,
963d8fe5 6791 .rpc_call_done = nfs4_locku_done,
06f814a3 6792 .rpc_release = nfs4_locku_release_calldata,
963d8fe5
TM
6793};
6794
a5d16a4d
TM
6795static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6796 struct nfs_open_context *ctx,
6797 struct nfs4_lock_state *lsp,
6798 struct nfs_seqid *seqid)
6799{
6800 struct nfs4_unlockdata *data;
5138fde0
TM
6801 struct rpc_message msg = {
6802 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6803 .rpc_cred = ctx->cred,
6804 };
c970aa85
TM
6805 struct rpc_task_setup task_setup_data = {
6806 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5138fde0 6807 .rpc_message = &msg,
c970aa85 6808 .callback_ops = &nfs4_locku_ops,
101070ca 6809 .workqueue = nfsiod_workqueue,
c970aa85
TM
6810 .flags = RPC_TASK_ASYNC,
6811 };
a5d16a4d 6812
fa940720
WAA
6813 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6814 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6815
137d6aca
FF
6816 /* Ensure this is an unlock - when canceling a lock, the
6817 * canceled lock is passed in, and it won't be an unlock.
6818 */
6819 fl->fl_type = F_UNLCK;
f30cb757
BC
6820 if (fl->fl_flags & FL_CLOSE)
6821 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
137d6aca 6822
a5d16a4d
TM
6823 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6824 if (data == NULL) {
6825 nfs_free_seqid(seqid);
6826 return ERR_PTR(-ENOMEM);
6827 }
6828
fba83f34 6829 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
1174dd1f
TM
6830 msg.rpc_argp = &data->arg;
6831 msg.rpc_resp = &data->res;
c970aa85
TM
6832 task_setup_data.callback_data = data;
6833 return rpc_run_task(&task_setup_data);
a5d16a4d
TM
6834}
6835
faf5f49c
TM
6836static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6837{
65b62a29
TM
6838 struct inode *inode = state->inode;
6839 struct nfs4_state_owner *sp = state->owner;
6840 struct nfs_inode *nfsi = NFS_I(inode);
911d1aaf 6841 struct nfs_seqid *seqid;
1da177e4 6842 struct nfs4_lock_state *lsp;
06f814a3 6843 struct rpc_task *task;
b4019c0e 6844 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
06f814a3 6845 int status = 0;
536ff0f8 6846 unsigned char fl_flags = request->fl_flags;
faf5f49c 6847
8d0a8a9d 6848 status = nfs4_set_lock_state(state, request);
9b073574
TM
6849 /* Unlock _before_ we do the RPC call */
6850 request->fl_flags |= FL_EXISTS;
65b62a29
TM
6851 /* Exclude nfs_delegation_claim_locks() */
6852 mutex_lock(&sp->so_delegreturn_mutex);
6853 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
19e03c57 6854 down_read(&nfsi->rwsem);
75575ddf 6855 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
19e03c57 6856 up_read(&nfsi->rwsem);
65b62a29 6857 mutex_unlock(&sp->so_delegreturn_mutex);
9b073574 6858 goto out;
19e03c57
TM
6859 }
6860 up_read(&nfsi->rwsem);
65b62a29 6861 mutex_unlock(&sp->so_delegreturn_mutex);
8d0a8a9d 6862 if (status != 0)
9b073574
TM
6863 goto out;
6864 /* Is this a delegated lock? */
8d0a8a9d 6865 lsp = request->fl_u.nfs4_fl.owner;
c5a2a15f
TM
6866 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6867 goto out;
b4019c0e
TM
6868 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6869 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
9b073574 6870 status = -ENOMEM;
badc76dd 6871 if (IS_ERR(seqid))
9b073574 6872 goto out;
cd3758e3 6873 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
a5d16a4d
TM
6874 status = PTR_ERR(task);
6875 if (IS_ERR(task))
9b073574 6876 goto out;
820bf85c 6877 status = rpc_wait_for_completion_task(task);
e6b3c4db 6878 rpc_put_task(task);
9b073574 6879out:
536ff0f8 6880 request->fl_flags = fl_flags;
d1b748a5 6881 trace_nfs4_unlock(request, state, F_SETLK, status);
1da177e4
LT
6882 return status;
6883}
6884
a5d16a4d
TM
6885struct nfs4_lockdata {
6886 struct nfs_lock_args arg;
6887 struct nfs_lock_res res;
6888 struct nfs4_lock_state *lsp;
6889 struct nfs_open_context *ctx;
6890 struct file_lock fl;
26e976a8 6891 unsigned long timestamp;
a5d16a4d
TM
6892 int rpc_status;
6893 int cancelled;
66179efe 6894 struct nfs_server *server;
a5d16a4d
TM
6895};
6896
6897static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
8535b2be
TM
6898 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6899 gfp_t gfp_mask)
1da177e4 6900{
a5d16a4d
TM
6901 struct nfs4_lockdata *p;
6902 struct inode *inode = lsp->ls_state->inode;
1da177e4 6903 struct nfs_server *server = NFS_SERVER(inode);
b4019c0e 6904 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
a5d16a4d 6905
8535b2be 6906 p = kzalloc(sizeof(*p), gfp_mask);
a5d16a4d
TM
6907 if (p == NULL)
6908 return NULL;
6909
6910 p->arg.fh = NFS_FH(inode);
6911 p->arg.fl = &p->fl;
8535b2be 6912 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
badc76dd 6913 if (IS_ERR(p->arg.open_seqid))
2f74c0a0 6914 goto out_free;
b4019c0e
TM
6915 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6916 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
badc76dd 6917 if (IS_ERR(p->arg.lock_seqid))
2f74c0a0 6918 goto out_free_seqid;
7539bbab 6919 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
48c22eb2 6920 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
d035c36c 6921 p->arg.lock_owner.s_dev = server->s_dev;
c1d51931 6922 p->res.lock_seqid = p->arg.lock_seqid;
a5d16a4d 6923 p->lsp = lsp;
66179efe 6924 p->server = server;
a5d16a4d 6925 p->ctx = get_nfs_open_context(ctx);
7b587e1a
N
6926 locks_init_lock(&p->fl);
6927 locks_copy_lock(&p->fl, fl);
a5d16a4d 6928 return p;
2f74c0a0
TM
6929out_free_seqid:
6930 nfs_free_seqid(p->arg.open_seqid);
a5d16a4d
TM
6931out_free:
6932 kfree(p);
6933 return NULL;
6934}
6935
6936static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6937{
6938 struct nfs4_lockdata *data = calldata;
6939 struct nfs4_state *state = data->lsp->ls_state;
06735b34 6940
3110ff80 6941 dprintk("%s: begin!\n", __func__);
2f74c0a0 6942 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
c8da19b9 6943 goto out_wait;
a5d16a4d 6944 /* Do we need to do an open_to_lock_owner? */
6b447539 6945 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
8fe72bac 6946 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
2240a9e2 6947 goto out_release_lock_seqid;
8fe72bac 6948 }
425c1d4e
TM
6949 nfs4_stateid_copy(&data->arg.open_stateid,
6950 &state->open_stateid);
a5d16a4d 6951 data->arg.new_lock_owner = 1;
c1d51931 6952 data->res.open_seqid = data->arg.open_seqid;
425c1d4e 6953 } else {
2f74c0a0 6954 data->arg.new_lock_owner = 0;
425c1d4e
TM
6955 nfs4_stateid_copy(&data->arg.lock_stateid,
6956 &data->lsp->ls_stateid);
6957 }
5d422301
TM
6958 if (!nfs4_valid_open_stateid(state)) {
6959 data->rpc_status = -EBADF;
6960 task->tk_action = NULL;
6961 goto out_release_open_seqid;
6962 }
26e976a8 6963 data->timestamp = jiffies;
42e1cca7 6964 if (nfs4_setup_sequence(data->server->nfs_client,
035168ab 6965 &data->arg.seq_args,
2240a9e2 6966 &data->res.seq_res,
d9afbd1b 6967 task) == 0)
66179efe 6968 return;
5d422301 6969out_release_open_seqid:
2240a9e2
TM
6970 nfs_release_seqid(data->arg.open_seqid);
6971out_release_lock_seqid:
6972 nfs_release_seqid(data->arg.lock_seqid);
c8da19b9
TM
6973out_wait:
6974 nfs4_sequence_done(task, &data->res.seq_res);
8fe72bac 6975 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
b257957e
AB
6976}
6977
a5d16a4d
TM
6978static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6979{
6980 struct nfs4_lockdata *data = calldata;
39071e6f 6981 struct nfs4_lock_state *lsp = data->lsp;
a5d16a4d 6982
3110ff80 6983 dprintk("%s: begin!\n", __func__);
a5d16a4d 6984
14516c3a
TM
6985 if (!nfs4_sequence_done(task, &data->res.seq_res))
6986 return;
66179efe 6987
a5d16a4d 6988 data->rpc_status = task->tk_status;
425c1d4e
TM
6989 switch (task->tk_status) {
6990 case 0:
2b0143b5 6991 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
39071e6f 6992 data->timestamp);
a3cf9bca 6993 if (data->arg.new_lock && !data->cancelled) {
c69899a1 6994 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
a3cf9bca 6995 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6ea76bf5 6996 goto out_restart;
c69899a1 6997 }
39071e6f
TM
6998 if (data->arg.new_lock_owner != 0) {
6999 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7000 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7001 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6ea76bf5
TM
7002 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7003 goto out_restart;
425c1d4e
TM
7004 break;
7005 case -NFS4ERR_BAD_STATEID:
7006 case -NFS4ERR_OLD_STATEID:
7007 case -NFS4ERR_STALE_STATEID:
7008 case -NFS4ERR_EXPIRED:
7009 if (data->arg.new_lock_owner != 0) {
6ea76bf5 7010 if (!nfs4_stateid_match(&data->arg.open_stateid,
425c1d4e 7011 &lsp->ls_state->open_stateid))
6ea76bf5
TM
7012 goto out_restart;
7013 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
425c1d4e 7014 &lsp->ls_stateid))
6ea76bf5 7015 goto out_restart;
a5d16a4d 7016 }
a3cf9bca 7017out_done:
3110ff80 7018 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6ea76bf5
TM
7019 return;
7020out_restart:
7021 if (!data->cancelled)
7022 rpc_restart_call_prepare(task);
7023 goto out_done;
a5d16a4d
TM
7024}
7025
7026static void nfs4_lock_release(void *calldata)
7027{
7028 struct nfs4_lockdata *data = calldata;
7029
3110ff80 7030 dprintk("%s: begin!\n", __func__);
2f74c0a0 7031 nfs_free_seqid(data->arg.open_seqid);
6ea76bf5 7032 if (data->cancelled && data->rpc_status == 0) {
a5d16a4d
TM
7033 struct rpc_task *task;
7034 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7035 data->arg.lock_seqid);
7036 if (!IS_ERR(task))
bf294b41 7037 rpc_put_task_async(task);
3110ff80 7038 dprintk("%s: cancelling lock!\n", __func__);
a5d16a4d
TM
7039 } else
7040 nfs_free_seqid(data->arg.lock_seqid);
7041 nfs4_put_lock_state(data->lsp);
7042 put_nfs_open_context(data->ctx);
7043 kfree(data);
3110ff80 7044 dprintk("%s: done!\n", __func__);
a5d16a4d
TM
7045}
7046
7047static const struct rpc_call_ops nfs4_lock_ops = {
7048 .rpc_call_prepare = nfs4_lock_prepare,
7049 .rpc_call_done = nfs4_lock_done,
7050 .rpc_release = nfs4_lock_release,
7051};
7052
2bee72a6
TM
7053static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7054{
2bee72a6
TM
7055 switch (error) {
7056 case -NFS4ERR_ADMIN_REVOKED:
d7f3e4bf 7057 case -NFS4ERR_EXPIRED:
2bee72a6 7058 case -NFS4ERR_BAD_STATEID:
ecac799a 7059 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
2bee72a6 7060 if (new_lock_owner != 0 ||
795a88c9 7061 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
ecac799a 7062 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
a2c0b9e2
TM
7063 break;
7064 case -NFS4ERR_STALE_STATEID:
a2c0b9e2 7065 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
ecac799a 7066 nfs4_schedule_lease_recovery(server->nfs_client);
8b98a532 7067 }
2bee72a6
TM
7068}
7069
afe6c27c 7070static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
a5d16a4d
TM
7071{
7072 struct nfs4_lockdata *data;
7073 struct rpc_task *task;
5138fde0
TM
7074 struct rpc_message msg = {
7075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7076 .rpc_cred = state->owner->so_cred,
7077 };
c970aa85
TM
7078 struct rpc_task_setup task_setup_data = {
7079 .rpc_client = NFS_CLIENT(state->inode),
5138fde0 7080 .rpc_message = &msg,
c970aa85 7081 .callback_ops = &nfs4_lock_ops,
101070ca 7082 .workqueue = nfsiod_workqueue,
61296507 7083 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
c970aa85 7084 };
a5d16a4d
TM
7085 int ret;
7086
3110ff80 7087 dprintk("%s: begin!\n", __func__);
cd3758e3 7088 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
8535b2be
TM
7089 fl->fl_u.nfs4_fl.owner,
7090 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
a5d16a4d
TM
7091 if (data == NULL)
7092 return -ENOMEM;
7093 if (IS_SETLKW(cmd))
7094 data->arg.block = 1;
fba83f34
AS
7095 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7096 recovery_type > NFS_LOCK_NEW);
1174dd1f
TM
7097 msg.rpc_argp = &data->arg;
7098 msg.rpc_resp = &data->res;
c970aa85 7099 task_setup_data.callback_data = data;
8fe72bac
TM
7100 if (recovery_type > NFS_LOCK_NEW) {
7101 if (recovery_type == NFS_LOCK_RECLAIM)
7102 data->arg.reclaim = NFS_LOCK_RECLAIM;
c69899a1
TM
7103 } else
7104 data->arg.new_lock = 1;
c970aa85 7105 task = rpc_run_task(&task_setup_data);
7a1218a2 7106 if (IS_ERR(task))
a5d16a4d 7107 return PTR_ERR(task);
820bf85c 7108 ret = rpc_wait_for_completion_task(task);
a5d16a4d
TM
7109 if (ret == 0) {
7110 ret = data->rpc_status;
2bee72a6
TM
7111 if (ret)
7112 nfs4_handle_setlk_error(data->server, data->lsp,
7113 data->arg.new_lock_owner, ret);
a5d16a4d 7114 } else
a7a3b1e9 7115 data->cancelled = true;
3d1a90ab 7116 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
e6b3c4db 7117 rpc_put_task(task);
3110ff80 7118 dprintk("%s: done, ret = %d!\n", __func__, ret);
a5d16a4d 7119 return ret;
1da177e4
LT
7120}
7121
7122static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7123{
202b50dc 7124 struct nfs_server *server = NFS_SERVER(state->inode);
05ffe24f
TM
7125 struct nfs4_exception exception = {
7126 .inode = state->inode,
7127 };
202b50dc
TM
7128 int err;
7129
7130 do {
42a2d13e
TM
7131 /* Cache the lock if possible... */
7132 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7133 return 0;
afe6c27c 7134 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
168667c4 7135 if (err != -NFS4ERR_DELAY)
202b50dc
TM
7136 break;
7137 nfs4_handle_exception(server, err, &exception);
7138 } while (exception.retry);
7139 return err;
1da177e4
LT
7140}
7141
7142static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7143{
202b50dc 7144 struct nfs_server *server = NFS_SERVER(state->inode);
05ffe24f
TM
7145 struct nfs4_exception exception = {
7146 .inode = state->inode,
7147 };
202b50dc
TM
7148 int err;
7149
6bfc93ef
TM
7150 err = nfs4_set_lock_state(state, request);
7151 if (err != 0)
7152 return err;
f6de7a39 7153 if (!recover_lost_locks) {
ef1820f9
N
7154 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7155 return 0;
7156 }
202b50dc 7157 do {
42a2d13e
TM
7158 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7159 return 0;
afe6c27c 7160 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
a9ed2e25
TM
7161 switch (err) {
7162 default:
7163 goto out;
7164 case -NFS4ERR_GRACE:
7165 case -NFS4ERR_DELAY:
7166 nfs4_handle_exception(server, err, &exception);
7167 err = 0;
7168 }
202b50dc 7169 } while (exception.retry);
a9ed2e25 7170out:
202b50dc 7171 return err;
1da177e4
LT
7172}
7173
f062eb6c 7174#if defined(CONFIG_NFS_V4_1)
b01dd1d8
BS
7175static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7176{
c5896fc8
TM
7177 struct nfs4_lock_state *lsp;
7178 int status;
b01dd1d8 7179
c5896fc8
TM
7180 status = nfs4_set_lock_state(state, request);
7181 if (status != 0)
7182 return status;
7183 lsp = request->fl_u.nfs4_fl.owner;
7184 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7185 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7186 return 0;
81b68de4 7187 return nfs4_lock_expired(state, request);
f062eb6c
BS
7188}
7189#endif
7190
1da177e4
LT
7191static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7192{
19e03c57 7193 struct nfs_inode *nfsi = NFS_I(state->inode);
11476e9d 7194 struct nfs4_state_owner *sp = state->owner;
01c3b861 7195 unsigned char fl_flags = request->fl_flags;
1ea67dbd 7196 int status;
1da177e4 7197
01c3b861 7198 request->fl_flags |= FL_ACCESS;
75575ddf 7199 status = locks_lock_inode_wait(state->inode, request);
01c3b861
TM
7200 if (status < 0)
7201 goto out;
11476e9d 7202 mutex_lock(&sp->so_delegreturn_mutex);
19e03c57 7203 down_read(&nfsi->rwsem);
01c3b861 7204 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
01c3b861 7205 /* Yes: cache locks! */
01c3b861 7206 /* ...but avoid races with delegation recall... */
19e03c57 7207 request->fl_flags = fl_flags & ~FL_SLEEP;
75575ddf 7208 status = locks_lock_inode_wait(state->inode, request);
c69899a1 7209 up_read(&nfsi->rwsem);
11476e9d 7210 mutex_unlock(&sp->so_delegreturn_mutex);
9a99af49 7211 goto out;
9a99af49 7212 }
19e03c57 7213 up_read(&nfsi->rwsem);
11476e9d 7214 mutex_unlock(&sp->so_delegreturn_mutex);
c69899a1 7215 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
01c3b861
TM
7216out:
7217 request->fl_flags = fl_flags;
1da177e4
LT
7218 return status;
7219}
7220
7221static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7222{
a1d0b5ee
TM
7223 struct nfs4_exception exception = {
7224 .state = state,
05ffe24f 7225 .inode = state->inode,
0688e64b 7226 .interruptible = true,
a1d0b5ee 7227 };
1da177e4
LT
7228 int err;
7229
7230 do {
965b5d67
TM
7231 err = _nfs4_proc_setlk(state, cmd, request);
7232 if (err == -NFS4ERR_DENIED)
7233 err = -EAGAIN;
1da177e4 7234 err = nfs4_handle_exception(NFS_SERVER(state->inode),
965b5d67 7235 err, &exception);
1da177e4
LT
7236 } while (exception.retry);
7237 return err;
7238}
7239
d2f3a7f9
JL
7240#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7241#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7242
7243static int
a1d617d8
JL
7244nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7245 struct file_lock *request)
d2f3a7f9
JL
7246{
7247 int status = -ERESTARTSYS;
7248 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7249
7250 while(!signalled()) {
7251 status = nfs4_proc_setlk(state, cmd, request);
7252 if ((status != -EAGAIN) || IS_SETLK(cmd))
7253 break;
7254 freezable_schedule_timeout_interruptible(timeout);
7255 timeout *= 2;
7256 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7257 status = -ERESTARTSYS;
7258 }
7259 return status;
7260}
7261
a1d617d8
JL
7262#ifdef CONFIG_NFS_V4_1
7263struct nfs4_lock_waiter {
7264 struct task_struct *task;
7265 struct inode *inode;
7266 struct nfs_lowner *owner;
a1d617d8
JL
7267};
7268
7269static int
ac6424b9 7270nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
a1d617d8
JL
7271{
7272 int ret;
a1d617d8 7273 struct nfs4_lock_waiter *waiter = wait->private;
a1d617d8 7274
57174593
JL
7275 /* NULL key means to wake up everyone */
7276 if (key) {
7277 struct cb_notify_lock_args *cbnl = key;
7278 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7279 *wowner = waiter->owner;
a1d617d8 7280
57174593
JL
7281 /* Only wake if the callback was for the same owner. */
7282 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7283 return 0;
a1d617d8 7284
57174593
JL
7285 /* Make sure it's for the right inode */
7286 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7287 return 0;
57174593 7288 }
a1d617d8
JL
7289
7290 /* override "private" so we can use default_wake_function */
7291 wait->private = waiter->task;
52b042ab
YW
7292 ret = woken_wake_function(wait, mode, flags, key);
7293 if (ret)
7294 list_del_init(&wait->entry);
a1d617d8
JL
7295 wait->private = waiter;
7296 return ret;
7297}
7298
7299static int
7300nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7301{
7302 int status = -ERESTARTSYS;
a1d617d8
JL
7303 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7304 struct nfs_server *server = NFS_SERVER(state->inode);
7305 struct nfs_client *clp = server->nfs_client;
7306 wait_queue_head_t *q = &clp->cl_lock_waitq;
7307 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7308 .id = lsp->ls_seqid.owner_id,
7309 .s_dev = server->s_dev };
7310 struct nfs4_lock_waiter waiter = { .task = current,
7311 .inode = state->inode,
52b042ab 7312 .owner = &owner};
ac6424b9 7313 wait_queue_entry_t wait;
a1d617d8
JL
7314
7315 /* Don't bother with waitqueue if we don't expect a callback */
7316 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7317 return nfs4_retry_setlk_simple(state, cmd, request);
7318
7319 init_wait(&wait);
7320 wait.private = &waiter;
7321 wait.func = nfs4_wake_lock_waiter;
a1d617d8
JL
7322
7323 while(!signalled()) {
ba851a39 7324 add_wait_queue(q, &wait);
a1d617d8 7325 status = nfs4_proc_setlk(state, cmd, request);
ba851a39
YW
7326 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7327 finish_wait(q, &wait);
a1d617d8 7328 break;
ba851a39 7329 }
a1d617d8
JL
7330
7331 status = -ERESTARTSYS;
52b042ab
YW
7332 freezer_do_not_count();
7333 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7334 freezer_count();
ba851a39 7335 finish_wait(q, &wait);
a1d617d8
JL
7336 }
7337
a1d617d8
JL
7338 return status;
7339}
7340#else /* !CONFIG_NFS_V4_1 */
7341static inline int
7342nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7343{
7344 return nfs4_retry_setlk_simple(state, cmd, request);
7345}
7346#endif
7347
1da177e4
LT
7348static int
7349nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7350{
7351 struct nfs_open_context *ctx;
7352 struct nfs4_state *state;
1da177e4
LT
7353 int status;
7354
7355 /* verify open state */
cd3758e3 7356 ctx = nfs_file_open_context(filp);
1da177e4
LT
7357 state = ctx->state;
7358
d953126a
TM
7359 if (IS_GETLK(cmd)) {
7360 if (state != NULL)
7361 return nfs4_proc_getlk(state, F_GETLK, request);
7362 return 0;
7363 }
1da177e4
LT
7364
7365 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7366 return -EINVAL;
7367
d953126a
TM
7368 if (request->fl_type == F_UNLCK) {
7369 if (state != NULL)
7370 return nfs4_proc_unlck(state, cmd, request);
7371 return 0;
7372 }
1da177e4 7373
d953126a
TM
7374 if (state == NULL)
7375 return -ENOLCK;
1ea67dbd
JL
7376
7377 if ((request->fl_flags & FL_POSIX) &&
7378 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7379 return -ENOLCK;
7380
fcfa4470
BC
7381 /*
7382 * Don't rely on the VFS having checked the file open mode,
7383 * since it won't do this for flock() locks.
7384 */
7385 switch (request->fl_type) {
7386 case F_RDLCK:
7387 if (!(filp->f_mode & FMODE_READ))
7388 return -EBADF;
7389 break;
7390 case F_WRLCK:
7391 if (!(filp->f_mode & FMODE_WRITE))
7392 return -EBADF;
7393 }
7394
1ea67dbd
JL
7395 status = nfs4_set_lock_state(state, request);
7396 if (status != 0)
7397 return status;
7398
d2f3a7f9 7399 return nfs4_retry_setlk(state, cmd, request);
1da177e4
LT
7400}
7401
db4f2e63 7402int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
888e694c
TM
7403{
7404 struct nfs_server *server = NFS_SERVER(state->inode);
888e694c
TM
7405 int err;
7406
7407 err = nfs4_set_lock_state(state, fl);
7408 if (err != 0)
db4f2e63 7409 return err;
3d7a9520
OK
7410 do {
7411 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7412 if (err != -NFS4ERR_DELAY)
7413 break;
7414 ssleep(1);
7415 } while (err == -NFS4ERR_DELAY);
dce2630c 7416 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
888e694c 7417}
6b3b5496 7418
cf470c3e
TM
7419struct nfs_release_lockowner_data {
7420 struct nfs4_lock_state *lsp;
5ae67c4f 7421 struct nfs_server *server;
cf470c3e 7422 struct nfs_release_lockowner_args args;
b7e63a10 7423 struct nfs_release_lockowner_res res;
60ea6812 7424 unsigned long timestamp;
cf470c3e
TM
7425};
7426
fbd4bfd1
CL
7427static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7428{
7429 struct nfs_release_lockowner_data *data = calldata;
5b53dc88 7430 struct nfs_server *server = data->server;
7981c8a6
AS
7431 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7432 &data->res.seq_res, task);
5b53dc88 7433 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
60ea6812 7434 data->timestamp = jiffies;
fbd4bfd1
CL
7435}
7436
7437static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7438{
7439 struct nfs_release_lockowner_data *data = calldata;
60ea6812
CL
7440 struct nfs_server *server = data->server;
7441
b7e63a10 7442 nfs40_sequence_done(task, &data->res.seq_res);
60ea6812
CL
7443
7444 switch (task->tk_status) {
7445 case 0:
7446 renew_lease(server, data->timestamp);
7447 break;
7448 case -NFS4ERR_STALE_CLIENTID:
7449 case -NFS4ERR_EXPIRED:
5b53dc88
KM
7450 nfs4_schedule_lease_recovery(server->nfs_client);
7451 break;
60ea6812
CL
7452 case -NFS4ERR_LEASE_MOVED:
7453 case -NFS4ERR_DELAY:
8478eaa1
N
7454 if (nfs4_async_handle_error(task, server,
7455 NULL, NULL) == -EAGAIN)
60ea6812
CL
7456 rpc_restart_call_prepare(task);
7457 }
fbd4bfd1
CL
7458}
7459
d3c7b7cc
TM
7460static void nfs4_release_lockowner_release(void *calldata)
7461{
cf470c3e 7462 struct nfs_release_lockowner_data *data = calldata;
5ae67c4f 7463 nfs4_free_lock_state(data->server, data->lsp);
d3c7b7cc
TM
7464 kfree(calldata);
7465}
7466
17280175 7467static const struct rpc_call_ops nfs4_release_lockowner_ops = {
fbd4bfd1
CL
7468 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7469 .rpc_call_done = nfs4_release_lockowner_done,
d3c7b7cc
TM
7470 .rpc_release = nfs4_release_lockowner_release,
7471};
7472
f1cdae87
JL
7473static void
7474nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
d3c7b7cc 7475{
cf470c3e 7476 struct nfs_release_lockowner_data *data;
d3c7b7cc
TM
7477 struct rpc_message msg = {
7478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7479 };
7480
7481 if (server->nfs_client->cl_mvops->minor_version != 0)
f1cdae87 7482 return;
fbd4bfd1 7483
cf470c3e
TM
7484 data = kmalloc(sizeof(*data), GFP_NOFS);
7485 if (!data)
f1cdae87 7486 return;
cf470c3e 7487 data->lsp = lsp;
5ae67c4f 7488 data->server = server;
cf470c3e
TM
7489 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7490 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7491 data->args.lock_owner.s_dev = server->s_dev;
fbd4bfd1 7492
cf470c3e 7493 msg.rpc_argp = &data->args;
b7e63a10 7494 msg.rpc_resp = &data->res;
fba83f34 7495 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
cf470c3e 7496 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
d3c7b7cc
TM
7497}
7498
aa1870af
BF
7499#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7500
d9a82a04 7501static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
59301226
AV
7502 struct dentry *unused, struct inode *inode,
7503 const char *key, const void *buf,
7504 size_t buflen, int flags)
6b3b5496 7505{
59301226 7506 return nfs4_proc_set_acl(inode, buf, buflen);
6b3b5496
BF
7507}
7508
d9a82a04 7509static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
b296821a
AV
7510 struct dentry *unused, struct inode *inode,
7511 const char *key, void *buf, size_t buflen)
6b3b5496 7512{
b296821a 7513 return nfs4_proc_get_acl(inode, buf, buflen);
6b3b5496
BF
7514}
7515
764a5c6b 7516static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6b3b5496 7517{
764a5c6b 7518 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6b3b5496
BF
7519}
7520
c9bccef6 7521#ifdef CONFIG_NFS_V4_SECURITY_LABEL
c9bccef6 7522
d9a82a04 7523static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
59301226
AV
7524 struct dentry *unused, struct inode *inode,
7525 const char *key, const void *buf,
7526 size_t buflen, int flags)
c9bccef6
DQ
7527{
7528 if (security_ismaclabel(key))
59301226 7529 return nfs4_set_security_label(inode, buf, buflen);
c9bccef6
DQ
7530
7531 return -EOPNOTSUPP;
7532}
7533
d9a82a04 7534static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
b296821a
AV
7535 struct dentry *unused, struct inode *inode,
7536 const char *key, void *buf, size_t buflen)
c9bccef6
DQ
7537{
7538 if (security_ismaclabel(key))
b296821a 7539 return nfs4_get_security_label(inode, buf, buflen);
c9bccef6
DQ
7540 return -EOPNOTSUPP;
7541}
7542
c4803c49
AG
7543static ssize_t
7544nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
c9bccef6 7545{
c4803c49 7546 int len = 0;
c9bccef6 7547
c4803c49
AG
7548 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7549 len = security_inode_listsecurity(inode, list, list_len);
82c596eb 7550 if (len >= 0 && list_len && len > list_len)
c4803c49 7551 return -ERANGE;
c9bccef6
DQ
7552 }
7553 return len;
7554}
7555
7556static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7557 .prefix = XATTR_SECURITY_PREFIX,
c9bccef6
DQ
7558 .get = nfs4_xattr_get_nfs4_label,
7559 .set = nfs4_xattr_set_nfs4_label,
7560};
c9bccef6 7561
c4803c49
AG
7562#else
7563
7564static ssize_t
7565nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7566{
7567 return 0;
7568}
7569
7570#endif
c9bccef6 7571
012a211a
FL
7572#ifdef CONFIG_NFS_V4_2
7573static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7574 struct dentry *unused, struct inode *inode,
7575 const char *key, const void *buf,
7576 size_t buflen, int flags)
7577{
7578 struct nfs_access_entry cache;
95ad37f9 7579 int ret;
012a211a
FL
7580
7581 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7582 return -EOPNOTSUPP;
7583
7584 /*
7585 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7586 * flags right now. Handling of xattr operations use the normal
7587 * file read/write permissions.
7588 *
7589 * Just in case the server has other ideas (which RFC 8276 allows),
7590 * do a cached access check for the XA* flags to possibly avoid
7591 * doing an RPC and getting EACCES back.
7592 */
7593 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7594 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7595 return -EACCES;
7596 }
7597
95ad37f9
FL
7598 if (buf == NULL) {
7599 ret = nfs42_proc_removexattr(inode, key);
7600 if (!ret)
7601 nfs4_xattr_cache_remove(inode, key);
7602 } else {
7603 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7604 if (!ret)
7605 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7606 }
7607
7608 return ret;
012a211a
FL
7609}
7610
7611static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7612 struct dentry *unused, struct inode *inode,
7613 const char *key, void *buf, size_t buflen)
7614{
7615 struct nfs_access_entry cache;
95ad37f9 7616 ssize_t ret;
012a211a
FL
7617
7618 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7619 return -EOPNOTSUPP;
7620
7621 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7622 if (!(cache.mask & NFS_ACCESS_XAREAD))
7623 return -EACCES;
7624 }
7625
95ad37f9
FL
7626 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7627 if (ret)
7628 return ret;
7629
7630 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7631 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7632 return ret;
7633
7634 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7635
7636 return ret;
012a211a
FL
7637}
7638
7639static ssize_t
7640nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7641{
7642 u64 cookie;
7643 bool eof;
95ad37f9 7644 ssize_t ret, size;
012a211a
FL
7645 char *buf;
7646 size_t buflen;
7647 struct nfs_access_entry cache;
7648
7649 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7650 return 0;
7651
7652 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7653 if (!(cache.mask & NFS_ACCESS_XALIST))
7654 return 0;
7655 }
7656
95ad37f9
FL
7657 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7658 if (ret)
7659 return ret;
7660
7661 ret = nfs4_xattr_cache_list(inode, list, list_len);
7662 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7663 return ret;
7664
012a211a
FL
7665 cookie = 0;
7666 eof = false;
7667 buflen = list_len ? list_len : XATTR_LIST_MAX;
7668 buf = list_len ? list : NULL;
7669 size = 0;
7670
7671 while (!eof) {
7672 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7673 &cookie, &eof);
7674 if (ret < 0)
7675 return ret;
7676
7677 if (list_len) {
7678 buf += ret;
7679 buflen -= ret;
7680 }
7681 size += ret;
7682 }
7683
95ad37f9
FL
7684 if (list_len)
7685 nfs4_xattr_cache_set_list(inode, list, size);
7686
012a211a
FL
7687 return size;
7688}
7689
7690#else
7691
7692static ssize_t
7693nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7694{
7695 return 0;
7696}
7697#endif /* CONFIG_NFS_V4_2 */
7698
533eb461
AA
7699/*
7700 * nfs_fhget will use either the mounted_on_fileid or the fileid
7701 */
69aaaae1
TM
7702static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7703{
533eb461
AA
7704 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7705 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7706 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
81934ddb 7707 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
69aaaae1
TM
7708 return;
7709
7710 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
81934ddb 7711 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
69aaaae1
TM
7712 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7713 fattr->nlink = 2;
7714}
7715
f05d147f
BS
7716static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7717 const struct qstr *name,
7718 struct nfs4_fs_locations *fs_locations,
7719 struct page *page)
683b57b4
TM
7720{
7721 struct nfs_server *server = NFS_SERVER(dir);
c05cefcc 7722 u32 bitmask[3];
683b57b4
TM
7723 struct nfs4_fs_locations_arg args = {
7724 .dir_fh = NFS_FH(dir),
c228fd3a 7725 .name = name,
683b57b4
TM
7726 .page = page,
7727 .bitmask = bitmask,
7728 };
22958463
BH
7729 struct nfs4_fs_locations_res res = {
7730 .fs_locations = fs_locations,
7731 };
683b57b4
TM
7732 struct rpc_message msg = {
7733 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7734 .rpc_argp = &args,
22958463 7735 .rpc_resp = &res,
683b57b4
TM
7736 };
7737 int status;
7738
3110ff80 7739 dprintk("%s: start\n", __func__);
533eb461 7740
c05cefcc
CL
7741 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7742 bitmask[1] = nfs4_fattr_bitmap[1];
7743
533eb461
AA
7744 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7745 * is not supported */
7746 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
c05cefcc 7747 bitmask[0] &= ~FATTR4_WORD0_FILEID;
533eb461 7748 else
c05cefcc 7749 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
533eb461 7750
c228fd3a 7751 nfs_fattr_init(&fs_locations->fattr);
683b57b4 7752 fs_locations->server = server;
830b8e33 7753 fs_locations->nlocations = 0;
f05d147f 7754 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
3110ff80 7755 dprintk("%s: returned status = %d\n", __func__, status);
683b57b4
TM
7756 return status;
7757}
7758
f05d147f
BS
7759int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7760 const struct qstr *name,
7761 struct nfs4_fs_locations *fs_locations,
7762 struct page *page)
db0a9593 7763{
0688e64b
TM
7764 struct nfs4_exception exception = {
7765 .interruptible = true,
7766 };
db0a9593
BS
7767 int err;
7768 do {
078ea3df
TM
7769 err = _nfs4_proc_fs_locations(client, dir, name,
7770 fs_locations, page);
7771 trace_nfs4_get_fs_locations(dir, name, err);
7772 err = nfs4_handle_exception(NFS_SERVER(dir), err,
db0a9593
BS
7773 &exception);
7774 } while (exception.retry);
7775 return err;
7776}
7777
b03d735b
CL
7778/*
7779 * This operation also signals the server that this client is
7780 * performing migration recovery. The server can stop returning
7781 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7782 * appended to this compound to identify the client ID which is
7783 * performing recovery.
7784 */
7785static int _nfs40_proc_get_locations(struct inode *inode,
7786 struct nfs4_fs_locations *locations,
a52458b4 7787 struct page *page, const struct cred *cred)
b03d735b
CL
7788{
7789 struct nfs_server *server = NFS_SERVER(inode);
7790 struct rpc_clnt *clnt = server->client;
7791 u32 bitmask[2] = {
7792 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7793 };
7794 struct nfs4_fs_locations_arg args = {
7795 .clientid = server->nfs_client->cl_clientid,
7796 .fh = NFS_FH(inode),
7797 .page = page,
7798 .bitmask = bitmask,
7799 .migration = 1, /* skip LOOKUP */
7800 .renew = 1, /* append RENEW */
7801 };
7802 struct nfs4_fs_locations_res res = {
7803 .fs_locations = locations,
7804 .migration = 1,
7805 .renew = 1,
7806 };
7807 struct rpc_message msg = {
7808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7809 .rpc_argp = &args,
7810 .rpc_resp = &res,
7811 .rpc_cred = cred,
7812 };
7813 unsigned long now = jiffies;
7814 int status;
7815
7816 nfs_fattr_init(&locations->fattr);
7817 locations->server = server;
7818 locations->nlocations = 0;
7819
fba83f34 7820 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
b03d735b
CL
7821 status = nfs4_call_sync_sequence(clnt, server, &msg,
7822 &args.seq_args, &res.seq_res);
7823 if (status)
7824 return status;
7825
7826 renew_lease(server, now);
7827 return 0;
7828}
7829
7830#ifdef CONFIG_NFS_V4_1
7831
7832/*
7833 * This operation also signals the server that this client is
7834 * performing migration recovery. The server can stop asserting
7835 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7836 * performing this operation is identified in the SEQUENCE
7837 * operation in this compound.
7838 *
7839 * When the client supports GETATTR(fs_locations_info), it can
7840 * be plumbed in here.
7841 */
7842static int _nfs41_proc_get_locations(struct inode *inode,
7843 struct nfs4_fs_locations *locations,
a52458b4 7844 struct page *page, const struct cred *cred)
b03d735b
CL
7845{
7846 struct nfs_server *server = NFS_SERVER(inode);
7847 struct rpc_clnt *clnt = server->client;
7848 u32 bitmask[2] = {
7849 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7850 };
7851 struct nfs4_fs_locations_arg args = {
7852 .fh = NFS_FH(inode),
7853 .page = page,
7854 .bitmask = bitmask,
7855 .migration = 1, /* skip LOOKUP */
7856 };
7857 struct nfs4_fs_locations_res res = {
7858 .fs_locations = locations,
7859 .migration = 1,
7860 };
7861 struct rpc_message msg = {
7862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7863 .rpc_argp = &args,
7864 .rpc_resp = &res,
7865 .rpc_cred = cred,
7866 };
7867 int status;
7868
7869 nfs_fattr_init(&locations->fattr);
7870 locations->server = server;
7871 locations->nlocations = 0;
7872
fba83f34 7873 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
b03d735b
CL
7874 status = nfs4_call_sync_sequence(clnt, server, &msg,
7875 &args.seq_args, &res.seq_res);
7876 if (status == NFS4_OK &&
7877 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7878 status = -NFS4ERR_LEASE_MOVED;
7879 return status;
7880}
7881
7882#endif /* CONFIG_NFS_V4_1 */
7883
7884/**
7885 * nfs4_proc_get_locations - discover locations for a migrated FSID
7886 * @inode: inode on FSID that is migrating
7887 * @locations: result of query
7888 * @page: buffer
7889 * @cred: credential to use for this operation
7890 *
7891 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7892 * operation failed, or a negative errno if a local error occurred.
7893 *
7894 * On success, "locations" is filled in, but if the server has
7895 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7896 * asserted.
7897 *
7898 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7899 * from this client that require migration recovery.
7900 */
7901int nfs4_proc_get_locations(struct inode *inode,
7902 struct nfs4_fs_locations *locations,
a52458b4 7903 struct page *page, const struct cred *cred)
b03d735b
CL
7904{
7905 struct nfs_server *server = NFS_SERVER(inode);
7906 struct nfs_client *clp = server->nfs_client;
7907 const struct nfs4_mig_recovery_ops *ops =
7908 clp->cl_mvops->mig_recovery_ops;
0688e64b
TM
7909 struct nfs4_exception exception = {
7910 .interruptible = true,
7911 };
b03d735b
CL
7912 int status;
7913
7914 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7915 (unsigned long long)server->fsid.major,
7916 (unsigned long long)server->fsid.minor,
7917 clp->cl_hostname);
7918 nfs_display_fhandle(NFS_FH(inode), __func__);
7919
7920 do {
7921 status = ops->get_locations(inode, locations, page, cred);
7922 if (status != -NFS4ERR_DELAY)
7923 break;
7924 nfs4_handle_exception(server, status, &exception);
7925 } while (exception.retry);
7926 return status;
7927}
7928
44c99933
CL
7929/*
7930 * This operation also signals the server that this client is
7931 * performing "lease moved" recovery. The server can stop
7932 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7933 * is appended to this compound to identify the client ID which is
7934 * performing recovery.
7935 */
a52458b4 7936static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
44c99933
CL
7937{
7938 struct nfs_server *server = NFS_SERVER(inode);
7939 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7940 struct rpc_clnt *clnt = server->client;
7941 struct nfs4_fsid_present_arg args = {
7942 .fh = NFS_FH(inode),
7943 .clientid = clp->cl_clientid,
7944 .renew = 1, /* append RENEW */
7945 };
7946 struct nfs4_fsid_present_res res = {
7947 .renew = 1,
7948 };
7949 struct rpc_message msg = {
7950 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7951 .rpc_argp = &args,
7952 .rpc_resp = &res,
7953 .rpc_cred = cred,
7954 };
7955 unsigned long now = jiffies;
7956 int status;
7957
7958 res.fh = nfs_alloc_fhandle();
7959 if (res.fh == NULL)
7960 return -ENOMEM;
7961
fba83f34 7962 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
44c99933
CL
7963 status = nfs4_call_sync_sequence(clnt, server, &msg,
7964 &args.seq_args, &res.seq_res);
7965 nfs_free_fhandle(res.fh);
7966 if (status)
7967 return status;
7968
7969 do_renew_lease(clp, now);
7970 return 0;
7971}
7972
7973#ifdef CONFIG_NFS_V4_1
7974
7975/*
7976 * This operation also signals the server that this client is
7977 * performing "lease moved" recovery. The server can stop asserting
7978 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7979 * this operation is identified in the SEQUENCE operation in this
7980 * compound.
7981 */
a52458b4 7982static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
44c99933
CL
7983{
7984 struct nfs_server *server = NFS_SERVER(inode);
7985 struct rpc_clnt *clnt = server->client;
7986 struct nfs4_fsid_present_arg args = {
7987 .fh = NFS_FH(inode),
7988 };
7989 struct nfs4_fsid_present_res res = {
7990 };
7991 struct rpc_message msg = {
7992 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7993 .rpc_argp = &args,
7994 .rpc_resp = &res,
7995 .rpc_cred = cred,
7996 };
7997 int status;
7998
7999 res.fh = nfs_alloc_fhandle();
8000 if (res.fh == NULL)
8001 return -ENOMEM;
8002
fba83f34 8003 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
44c99933
CL
8004 status = nfs4_call_sync_sequence(clnt, server, &msg,
8005 &args.seq_args, &res.seq_res);
8006 nfs_free_fhandle(res.fh);
8007 if (status == NFS4_OK &&
8008 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8009 status = -NFS4ERR_LEASE_MOVED;
8010 return status;
8011}
8012
8013#endif /* CONFIG_NFS_V4_1 */
8014
8015/**
8016 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8017 * @inode: inode on FSID to check
8018 * @cred: credential to use for this operation
8019 *
8020 * Server indicates whether the FSID is present, moved, or not
8021 * recognized. This operation is necessary to clear a LEASE_MOVED
8022 * condition for this client ID.
8023 *
8024 * Returns NFS4_OK if the FSID is present on this server,
8025 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8026 * NFS4ERR code if some error occurred on the server, or a
8027 * negative errno if a local failure occurred.
8028 */
a52458b4 8029int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
44c99933
CL
8030{
8031 struct nfs_server *server = NFS_SERVER(inode);
8032 struct nfs_client *clp = server->nfs_client;
8033 const struct nfs4_mig_recovery_ops *ops =
8034 clp->cl_mvops->mig_recovery_ops;
0688e64b
TM
8035 struct nfs4_exception exception = {
8036 .interruptible = true,
8037 };
44c99933
CL
8038 int status;
8039
8040 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8041 (unsigned long long)server->fsid.major,
8042 (unsigned long long)server->fsid.minor,
8043 clp->cl_hostname);
8044 nfs_display_fhandle(NFS_FH(inode), __func__);
8045
8046 do {
8047 status = ops->fsid_present(inode, cred);
8048 if (status != -NFS4ERR_DELAY)
8049 break;
8050 nfs4_handle_exception(server, status, &exception);
8051 } while (exception.retry);
8052 return status;
8053}
8054
302fad7b 8055/*
a5250def
WAA
8056 * If 'use_integrity' is true and the state managment nfs_client
8057 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8058 * and the machine credential as per RFC3530bis and RFC5661 Security
8059 * Considerations sections. Otherwise, just use the user cred with the
8060 * filesystem's rpc_client.
5ec16a85 8061 */
a5250def 8062static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
5a5ea0d4
BS
8063{
8064 int status;
50493364
AS
8065 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8066 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
5a5ea0d4
BS
8067 struct nfs4_secinfo_arg args = {
8068 .dir_fh = NFS_FH(dir),
8069 .name = name,
8070 };
8071 struct nfs4_secinfo_res res = {
8072 .flavors = flavors,
8073 };
8074 struct rpc_message msg = {
8075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8076 .rpc_argp = &args,
8077 .rpc_resp = &res,
8078 };
50493364
AS
8079 struct nfs4_call_sync_data data = {
8080 .seq_server = NFS_SERVER(dir),
8081 .seq_args = &args.seq_args,
8082 .seq_res = &res.seq_res,
8083 };
8084 struct rpc_task_setup task_setup = {
8085 .rpc_client = clnt,
8086 .rpc_message = &msg,
8087 .callback_ops = clp->cl_mvops->call_sync_ops,
8088 .callback_data = &data,
8089 .flags = RPC_TASK_NO_ROUND_ROBIN,
8090 };
a52458b4 8091 const struct cred *cred = NULL;
a5250def
WAA
8092
8093 if (use_integrity) {
50493364
AS
8094 clnt = clp->cl_rpcclient;
8095 task_setup.rpc_client = clnt;
8096
8097 cred = nfs4_get_clid_cred(clp);
7cb852df 8098 msg.rpc_cred = cred;
a5250def 8099 }
5a5ea0d4
BS
8100
8101 dprintk("NFS call secinfo %s\n", name->name);
8b5bee2e 8102
50493364
AS
8103 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8104 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8105 status = nfs4_call_sync_custom(&task_setup);
8b5bee2e 8106
5a5ea0d4 8107 dprintk("NFS reply secinfo: %d\n", status);
a5250def 8108
a52458b4 8109 put_cred(cred);
5a5ea0d4
BS
8110 return status;
8111}
8112
72de53ec
BS
8113int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8114 struct nfs4_secinfo_flavors *flavors)
5a5ea0d4 8115{
0688e64b
TM
8116 struct nfs4_exception exception = {
8117 .interruptible = true,
8118 };
5a5ea0d4
BS
8119 int err;
8120 do {
a5250def
WAA
8121 err = -NFS4ERR_WRONGSEC;
8122
8123 /* try to use integrity protection with machine cred */
8124 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8125 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8126
8127 /*
8128 * if unable to use integrity protection, or SECINFO with
8129 * integrity protection returns NFS4ERR_WRONGSEC (which is
8130 * disallowed by spec, but exists in deployed servers) use
8131 * the current filesystem's rpc_client and the user cred.
8132 */
8133 if (err == -NFS4ERR_WRONGSEC)
8134 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8135
078ea3df
TM
8136 trace_nfs4_secinfo(dir, name, err);
8137 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5a5ea0d4
BS
8138 &exception);
8139 } while (exception.retry);
8140 return err;
8141}
8142
557134a3 8143#ifdef CONFIG_NFS_V4_1
357f54d6
AA
8144/*
8145 * Check the exchange flags returned by the server for invalid flags, having
8146 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8147 * DS flags set.
8148 */
8c39076c 8149static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
357f54d6 8150{
8c39076c
OK
8151 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8152 goto out_inval;
8153 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
357f54d6
AA
8154 goto out_inval;
8155 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8156 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8157 goto out_inval;
8158 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8159 goto out_inval;
8160 return NFS_OK;
8161out_inval:
8162 return -NFS4ERR_INVAL;
8163}
8164
78fe0f41 8165static bool
79d4e1f0
CL
8166nfs41_same_server_scope(struct nfs41_server_scope *a,
8167 struct nfs41_server_scope *b)
78fe0f41 8168{
49ad0145
AS
8169 if (a->server_scope_sz != b->server_scope_sz)
8170 return false;
8171 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
78fe0f41
WAA
8172}
8173
02a95dee
AA
8174static void
8175nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8176{
5c441544 8177 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
dff58530 8178 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
5c441544
TM
8179 struct nfs_client *clp = args->client;
8180
8181 switch (task->tk_status) {
8182 case -NFS4ERR_BADSESSION:
8183 case -NFS4ERR_DEADSESSION:
8184 nfs4_schedule_session_recovery(clp->cl_session,
8185 task->tk_status);
8186 }
dff58530
OK
8187 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8188 res->dir != NFS4_CDFS4_BOTH) {
8189 rpc_task_close_connection(task);
8190 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8191 rpc_restart_call(task);
8192 }
02a95dee
AA
8193}
8194
8195static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
1c709b76 8196 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
02a95dee
AA
8197};
8198
7c44f1ae 8199/*
d9ddbf5d 8200 * nfs4_proc_bind_one_conn_to_session()
7c44f1ae
WAA
8201 *
8202 * The 4.1 client currently uses the same TCP connection for the
8203 * fore and backchannel.
8204 */
d9ddbf5d
TM
8205static
8206int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8207 struct rpc_xprt *xprt,
8208 struct nfs_client *clp,
a52458b4 8209 const struct cred *cred)
7c44f1ae
WAA
8210{
8211 int status;
71a097c6
TM
8212 struct nfs41_bind_conn_to_session_args args = {
8213 .client = clp,
8214 .dir = NFS4_CDFC4_FORE_OR_BOTH,
dff58530 8215 .retries = 0,
71a097c6 8216 };
7c44f1ae
WAA
8217 struct nfs41_bind_conn_to_session_res res;
8218 struct rpc_message msg = {
8219 .rpc_proc =
8220 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
71a097c6 8221 .rpc_argp = &args,
7c44f1ae 8222 .rpc_resp = &res,
2cf047c9 8223 .rpc_cred = cred,
7c44f1ae 8224 };
d9ddbf5d
TM
8225 struct rpc_task_setup task_setup_data = {
8226 .rpc_client = clnt,
8227 .rpc_xprt = xprt,
02a95dee 8228 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
d9ddbf5d
TM
8229 .rpc_message = &msg,
8230 .flags = RPC_TASK_TIMEOUT,
8231 };
8232 struct rpc_task *task;
7c44f1ae 8233
71a097c6
TM
8234 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8235 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8236 args.dir = NFS4_CDFC4_FORE;
7c44f1ae 8237
d9ddbf5d
TM
8238 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8239 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8240 args.dir = NFS4_CDFC4_FORE;
8241
8242 task = rpc_run_task(&task_setup_data);
8243 if (!IS_ERR(task)) {
8244 status = task->tk_status;
8245 rpc_put_task(task);
8246 } else
8247 status = PTR_ERR(task);
c6d01c6f 8248 trace_nfs4_bind_conn_to_session(clp, status);
7c44f1ae 8249 if (status == 0) {
71a097c6 8250 if (memcmp(res.sessionid.data,
7c44f1ae
WAA
8251 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8252 dprintk("NFS: %s: Session ID mismatch\n", __func__);
c7ae7639 8253 return -EIO;
7c44f1ae 8254 }
71a097c6 8255 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7c44f1ae
WAA
8256 dprintk("NFS: %s: Unexpected direction from server\n",
8257 __func__);
c7ae7639 8258 return -EIO;
7c44f1ae 8259 }
71a097c6 8260 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7c44f1ae
WAA
8261 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8262 __func__);
c7ae7639 8263 return -EIO;
7c44f1ae
WAA
8264 }
8265 }
c7ae7639 8266
7c44f1ae
WAA
8267 return status;
8268}
8269
d9ddbf5d
TM
8270struct rpc_bind_conn_calldata {
8271 struct nfs_client *clp;
a52458b4 8272 const struct cred *cred;
d9ddbf5d
TM
8273};
8274
8275static int
8276nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8277 struct rpc_xprt *xprt,
8278 void *calldata)
8279{
8280 struct rpc_bind_conn_calldata *p = calldata;
8281
8282 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8283}
8284
a52458b4 8285int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
d9ddbf5d
TM
8286{
8287 struct rpc_bind_conn_calldata data = {
8288 .clp = clp,
8289 .cred = cred,
8290 };
8291 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8292 nfs4_proc_bind_conn_to_session_callback, &data);
8293}
8294
99fe60d0 8295/*
fa940720
WAA
8296 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8297 * and operations we'd like to see to enable certain features in the allow map
2031cd1a
WAA
8298 */
8299static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8300 .how = SP4_MACH_CRED,
8301 .enforce.u.words = {
8302 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8303 1 << (OP_EXCHANGE_ID - 32) |
8304 1 << (OP_CREATE_SESSION - 32) |
8305 1 << (OP_DESTROY_SESSION - 32) |
8306 1 << (OP_DESTROY_CLIENTID - 32)
fa940720
WAA
8307 },
8308 .allow.u.words = {
8309 [0] = 1 << (OP_CLOSE) |
99ade3c7 8310 1 << (OP_OPEN_DOWNGRADE) |
a0279625 8311 1 << (OP_LOCKU) |
99ade3c7 8312 1 << (OP_DELEGRETURN) |
a0279625 8313 1 << (OP_COMMIT),
8b5bee2e 8314 [1] = 1 << (OP_SECINFO - 32) |
3787d506 8315 1 << (OP_SECINFO_NO_NAME - 32) |
99ade3c7 8316 1 << (OP_LAYOUTRETURN - 32) |
3787d506 8317 1 << (OP_TEST_STATEID - 32) |
a0279625
WAA
8318 1 << (OP_FREE_STATEID - 32) |
8319 1 << (OP_WRITE - 32)
2031cd1a
WAA
8320 }
8321};
8322
8323/*
8324 * Select the state protection mode for client `clp' given the server results
8325 * from exchange_id in `sp'.
99fe60d0 8326 *
2031cd1a
WAA
8327 * Returns 0 on success, negative errno otherwise.
8328 */
8329static int nfs4_sp4_select_mode(struct nfs_client *clp,
8330 struct nfs41_state_protection *sp)
8331{
8332 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8333 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8334 1 << (OP_EXCHANGE_ID - 32) |
8335 1 << (OP_CREATE_SESSION - 32) |
8336 1 << (OP_DESTROY_SESSION - 32) |
8337 1 << (OP_DESTROY_CLIENTID - 32)
8338 };
937e3133 8339 unsigned long flags = 0;
2031cd1a 8340 unsigned int i;
937e3133 8341 int ret = 0;
2031cd1a
WAA
8342
8343 if (sp->how == SP4_MACH_CRED) {
8344 /* Print state protect result */
8345 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8346 for (i = 0; i <= LAST_NFS4_OP; i++) {
8347 if (test_bit(i, sp->enforce.u.longs))
8348 dfprintk(MOUNT, " enforce op %d\n", i);
8349 if (test_bit(i, sp->allow.u.longs))
8350 dfprintk(MOUNT, " allow op %d\n", i);
8351 }
8352
8353 /* make sure nothing is on enforce list that isn't supported */
8354 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8355 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8356 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
937e3133
TM
8357 ret = -EINVAL;
8358 goto out;
2031cd1a
WAA
8359 }
8360 }
8361
8362 /*
8363 * Minimal mode - state operations are allowed to use machine
8364 * credential. Note this already happens by default, so the
8365 * client doesn't have to do anything more than the negotiation.
8366 *
8367 * NOTE: we don't care if EXCHANGE_ID is in the list -
8368 * we're already using the machine cred for exchange_id
8369 * and will never use a different cred.
8370 */
8371 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8372 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8373 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8374 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8375 dfprintk(MOUNT, "sp4_mach_cred:\n");
8376 dfprintk(MOUNT, " minimal mode enabled\n");
937e3133 8377 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
2031cd1a
WAA
8378 } else {
8379 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
937e3133
TM
8380 ret = -EINVAL;
8381 goto out;
2031cd1a 8382 }
fa940720
WAA
8383
8384 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
99ade3c7
AE
8385 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8386 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
fa940720
WAA
8387 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8388 dfprintk(MOUNT, " cleanup mode enabled\n");
937e3133 8389 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
fa940720 8390 }
8b5bee2e 8391
99ade3c7
AE
8392 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8393 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
937e3133 8394 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
99ade3c7
AE
8395 }
8396
8b5bee2e
WAA
8397 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8398 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8399 dfprintk(MOUNT, " secinfo mode enabled\n");
937e3133 8400 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8b5bee2e 8401 }
3787d506
WAA
8402
8403 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8404 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8405 dfprintk(MOUNT, " stateid mode enabled\n");
937e3133 8406 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
3787d506 8407 }
8c21c62c
WAA
8408
8409 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8410 dfprintk(MOUNT, " write mode enabled\n");
937e3133 8411 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8c21c62c
WAA
8412 }
8413
8414 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8415 dfprintk(MOUNT, " commit mode enabled\n");
937e3133 8416 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8c21c62c 8417 }
2031cd1a 8418 }
937e3133
TM
8419out:
8420 clp->cl_sp4_flags = flags;
72bf75cf 8421 return ret;
2031cd1a
WAA
8422}
8423
8d89bd70
AA
8424struct nfs41_exchange_id_data {
8425 struct nfs41_exchange_id_res res;
8426 struct nfs41_exchange_id_args args;
8d89bd70
AA
8427};
8428
8d89bd70
AA
8429static void nfs4_exchange_id_release(void *data)
8430{
8431 struct nfs41_exchange_id_data *cdata =
8432 (struct nfs41_exchange_id_data *)data;
8433
63513232 8434 nfs_put_client(cdata->args.client);
8d89bd70
AA
8435 kfree(cdata->res.impl_id);
8436 kfree(cdata->res.server_scope);
8437 kfree(cdata->res.server_owner);
8438 kfree(cdata);
8439}
8440
8441static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8d89bd70
AA
8442 .rpc_release = nfs4_exchange_id_release,
8443};
8444
2031cd1a
WAA
8445/*
8446 * _nfs4_proc_exchange_id()
6bbb4ae8 8447 *
2031cd1a 8448 * Wrapper for EXCHANGE_ID operation.
99fe60d0 8449 */
9c760d1f 8450static struct rpc_task *
a52458b4 8451nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
ad0849a7 8452 u32 sp4_how, struct rpc_xprt *xprt)
99fe60d0 8453{
99fe60d0
BH
8454 struct rpc_message msg = {
8455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
99fe60d0
BH
8456 .rpc_cred = cred,
8457 };
8d89bd70
AA
8458 struct rpc_task_setup task_setup_data = {
8459 .rpc_client = clp->cl_rpcclient,
8460 .callback_ops = &nfs4_exchange_id_call_ops,
8461 .rpc_message = &msg,
5a0c257f 8462 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8d89bd70
AA
8463 };
8464 struct nfs41_exchange_id_data *calldata;
e917f0d1 8465 int status;
8d89bd70 8466
212bf41d 8467 if (!refcount_inc_not_zero(&clp->cl_count))
9c760d1f 8468 return ERR_PTR(-EIO);
8d89bd70 8469
9c760d1f 8470 status = -ENOMEM;
8d89bd70 8471 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9c760d1f
TM
8472 if (!calldata)
8473 goto out;
99fe60d0 8474
fd40559c 8475 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
873e3851
JL
8476
8477 status = nfs4_init_uniform_client_string(clp);
8478 if (status)
8d89bd70 8479 goto out_calldata;
3a6bb738 8480
8d89bd70
AA
8481 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8482 GFP_NOFS);
8483 status = -ENOMEM;
8484 if (unlikely(calldata->res.server_owner == NULL))
8485 goto out_calldata;
78fe0f41 8486
8d89bd70 8487 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
bbafffd2 8488 GFP_NOFS);
8d89bd70 8489 if (unlikely(calldata->res.server_scope == NULL))
acdeb69d 8490 goto out_server_owner;
78fe0f41 8491
8d89bd70
AA
8492 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8493 if (unlikely(calldata->res.impl_id == NULL))
7d2ed9ac 8494 goto out_server_scope;
7d2ed9ac 8495
2031cd1a
WAA
8496 switch (sp4_how) {
8497 case SP4_NONE:
8d89bd70 8498 calldata->args.state_protect.how = SP4_NONE;
2031cd1a
WAA
8499 break;
8500
8501 case SP4_MACH_CRED:
8d89bd70 8502 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
2031cd1a
WAA
8503 break;
8504
8505 default:
8506 /* unsupported! */
8507 WARN_ON_ONCE(1);
8508 status = -EINVAL;
6b55970b 8509 goto out_impl_id;
2031cd1a 8510 }
ad0849a7 8511 if (xprt) {
ad0849a7 8512 task_setup_data.rpc_xprt = xprt;
d9cb7330 8513 task_setup_data.flags |= RPC_TASK_SOFTCONN;
fd40559c
TM
8514 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8515 sizeof(calldata->args.verifier.data));
ad0849a7 8516 }
8d89bd70 8517 calldata->args.client = clp;
8d89bd70 8518 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
bfab2817
TM
8519 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8520#ifdef CONFIG_NFS_V4_1_MIGRATION
8521 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8d89bd70
AA
8522#endif
8523 msg.rpc_argp = &calldata->args;
8524 msg.rpc_resp = &calldata->res;
8525 task_setup_data.callback_data = calldata;
2031cd1a 8526
9c760d1f 8527 return rpc_run_task(&task_setup_data);
8d89bd70
AA
8528
8529out_impl_id:
8530 kfree(calldata->res.impl_id);
8531out_server_scope:
8532 kfree(calldata->res.server_scope);
8533out_server_owner:
8534 kfree(calldata->res.server_owner);
8535out_calldata:
8536 kfree(calldata);
9c760d1f 8537out:
63513232 8538 nfs_put_client(clp);
9c760d1f
TM
8539 return ERR_PTR(status);
8540}
8541
8542/*
8543 * _nfs4_proc_exchange_id()
8544 *
8545 * Wrapper for EXCHANGE_ID operation.
8546 */
a52458b4 8547static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
9c760d1f
TM
8548 u32 sp4_how)
8549{
8550 struct rpc_task *task;
8551 struct nfs41_exchange_id_args *argp;
8552 struct nfs41_exchange_id_res *resp;
7dc2993a 8553 unsigned long now = jiffies;
9c760d1f
TM
8554 int status;
8555
8556 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8557 if (IS_ERR(task))
8558 return PTR_ERR(task);
8559
8560 argp = task->tk_msg.rpc_argp;
8561 resp = task->tk_msg.rpc_resp;
8562 status = task->tk_status;
8563 if (status != 0)
8564 goto out;
8565
8c39076c
OK
8566 status = nfs4_check_cl_exchange_flags(resp->flags,
8567 clp->cl_mvops->minor_version);
9c760d1f
TM
8568 if (status != 0)
8569 goto out;
8570
8571 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8572 if (status != 0)
8573 goto out;
8574
7dc2993a
RM
8575 do_renew_lease(clp, now);
8576
9c760d1f
TM
8577 clp->cl_clientid = resp->clientid;
8578 clp->cl_exchange_flags = resp->flags;
8579 clp->cl_seqid = resp->seqid;
8580 /* Client ID is not confirmed */
8581 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8582 clear_bit(NFS4_SESSION_ESTABLISHED,
8583 &clp->cl_session->session_state);
8584
8585 if (clp->cl_serverscope != NULL &&
8586 !nfs41_same_server_scope(clp->cl_serverscope,
8587 resp->server_scope)) {
8588 dprintk("%s: server_scope mismatch detected\n",
8589 __func__);
8590 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8591 }
8592
8593 swap(clp->cl_serverowner, resp->server_owner);
8594 swap(clp->cl_serverscope, resp->server_scope);
8595 swap(clp->cl_implid, resp->impl_id);
8596
8597 /* Save the EXCHANGE_ID verifier session trunk tests */
8598 memcpy(clp->cl_confirm.data, argp->verifier.data,
8599 sizeof(clp->cl_confirm.data));
8600out:
8601 trace_nfs4_exchange_id(clp, status);
8602 rpc_put_task(task);
8603 return status;
99fe60d0
BH
8604}
8605
2031cd1a
WAA
8606/*
8607 * nfs4_proc_exchange_id()
8608 *
8609 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8610 *
8611 * Since the clientid has expired, all compounds using sessions
8612 * associated with the stale clientid will be returning
8613 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8614 * be in some phase of session reset.
8615 *
8616 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8617 */
a52458b4 8618int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
2031cd1a
WAA
8619{
8620 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8621 int status;
8622
8623 /* try SP4_MACH_CRED if krb5i/p */
8624 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8625 authflavor == RPC_AUTH_GSS_KRB5P) {
9c760d1f 8626 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
2031cd1a
WAA
8627 if (!status)
8628 return 0;
8629 }
8630
8631 /* try SP4_NONE */
9c760d1f 8632 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
2031cd1a
WAA
8633}
8634
04fa2c6b
AA
8635/**
8636 * nfs4_test_session_trunk
8637 *
8638 * This is an add_xprt_test() test function called from
8639 * rpc_clnt_setup_test_and_add_xprt.
8640 *
8641 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8642 * and is dereferrenced in nfs4_exchange_id_release
8643 *
8644 * Upon success, add the new transport to the rpc_clnt
8645 *
8646 * @clnt: struct rpc_clnt to get new transport
8647 * @xprt: the rpc_xprt to test
8648 * @data: call data for _nfs4_proc_exchange_id.
8649 */
10e037d1 8650void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
04fa2c6b
AA
8651 void *data)
8652{
8653 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
9c760d1f
TM
8654 struct rpc_task *task;
8655 int status;
8656
04fa2c6b
AA
8657 u32 sp4_how;
8658
8659 dprintk("--> %s try %s\n", __func__,
8660 xprt->address_strings[RPC_DISPLAY_ADDR]);
8661
8662 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8663
8664 /* Test connection for session trunking. Async exchange_id call */
9c760d1f
TM
8665 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8666 if (IS_ERR(task))
10e037d1 8667 return;
9c760d1f
TM
8668
8669 status = task->tk_status;
8670 if (status == 0)
8671 status = nfs4_detect_session_trunking(adata->clp,
8672 task->tk_msg.rpc_resp, xprt);
8673
10e037d1
S
8674 if (status == 0)
8675 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8676
9c760d1f 8677 rpc_put_task(task);
2031cd1a 8678}
04fa2c6b 8679EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
2031cd1a 8680
66245539 8681static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
a52458b4 8682 const struct cred *cred)
66245539
TM
8683{
8684 struct rpc_message msg = {
8685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8686 .rpc_argp = clp,
8687 .rpc_cred = cred,
8688 };
8689 int status;
8690
5a0c257f
N
8691 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8692 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
c6d01c6f 8693 trace_nfs4_destroy_clientid(clp, status);
66245539 8694 if (status)
02c67525 8695 dprintk("NFS: Got error %d from the server %s on "
66245539
TM
8696 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8697 return status;
8698}
8699
8700static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
a52458b4 8701 const struct cred *cred)
66245539
TM
8702{
8703 unsigned int loop;
8704 int ret;
8705
8706 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8707 ret = _nfs4_proc_destroy_clientid(clp, cred);
8708 switch (ret) {
8709 case -NFS4ERR_DELAY:
8710 case -NFS4ERR_CLIENTID_BUSY:
8711 ssleep(1);
8712 break;
8713 default:
8714 return ret;
8715 }
8716 }
8717 return 0;
8718}
8719
8720int nfs4_destroy_clientid(struct nfs_client *clp)
8721{
a52458b4 8722 const struct cred *cred;
66245539
TM
8723 int ret = 0;
8724
8725 if (clp->cl_mvops->minor_version < 1)
8726 goto out;
8727 if (clp->cl_exchange_flags == 0)
8728 goto out;
05f4c350
CL
8729 if (clp->cl_preserve_clid)
8730 goto out;
73d8bde5 8731 cred = nfs4_get_clid_cred(clp);
66245539 8732 ret = nfs4_proc_destroy_clientid(clp, cred);
a52458b4 8733 put_cred(cred);
66245539
TM
8734 switch (ret) {
8735 case 0:
8736 case -NFS4ERR_STALE_CLIENTID:
8737 clp->cl_exchange_flags = 0;
8738 }
8739out:
8740 return ret;
8741}
8742
0efb01b2
DB
8743#endif /* CONFIG_NFS_V4_1 */
8744
2050f0cc
AA
8745struct nfs4_get_lease_time_data {
8746 struct nfs4_get_lease_time_args *args;
8747 struct nfs4_get_lease_time_res *res;
8748 struct nfs_client *clp;
8749};
8750
8751static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8752 void *calldata)
8753{
2050f0cc
AA
8754 struct nfs4_get_lease_time_data *data =
8755 (struct nfs4_get_lease_time_data *)calldata;
8756
8757 dprintk("--> %s\n", __func__);
8758 /* just setup sequence, do not trigger session recovery
8759 since we're invoked within one */
7981c8a6 8760 nfs4_setup_sequence(data->clp,
d9afbd1b
TM
8761 &data->args->la_seq_args,
8762 &data->res->lr_seq_res,
8763 task);
2050f0cc
AA
8764 dprintk("<-- %s\n", __func__);
8765}
8766
8767/*
8768 * Called from nfs4_state_manager thread for session setup, so don't recover
8769 * from sequence operation or clientid errors.
8770 */
8771static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8772{
8773 struct nfs4_get_lease_time_data *data =
8774 (struct nfs4_get_lease_time_data *)calldata;
8775
8776 dprintk("--> %s\n", __func__);
0efb01b2 8777 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
14516c3a 8778 return;
2050f0cc
AA
8779 switch (task->tk_status) {
8780 case -NFS4ERR_DELAY:
8781 case -NFS4ERR_GRACE:
8782 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8783 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8784 task->tk_status = 0;
df561f66 8785 fallthrough;
a8a4ae3a 8786 case -NFS4ERR_RETRY_UNCACHED_REP:
d00c5d43 8787 rpc_restart_call_prepare(task);
2050f0cc
AA
8788 return;
8789 }
2050f0cc
AA
8790 dprintk("<-- %s\n", __func__);
8791}
8792
17280175 8793static const struct rpc_call_ops nfs4_get_lease_time_ops = {
2050f0cc
AA
8794 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8795 .rpc_call_done = nfs4_get_lease_time_done,
8796};
8797
8798int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8799{
2050f0cc
AA
8800 struct nfs4_get_lease_time_args args;
8801 struct nfs4_get_lease_time_res res = {
8802 .lr_fsinfo = fsinfo,
8803 };
8804 struct nfs4_get_lease_time_data data = {
8805 .args = &args,
8806 .res = &res,
8807 .clp = clp,
8808 };
8809 struct rpc_message msg = {
8810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8811 .rpc_argp = &args,
8812 .rpc_resp = &res,
8813 };
8814 struct rpc_task_setup task_setup = {
8815 .rpc_client = clp->cl_rpcclient,
8816 .rpc_message = &msg,
8817 .callback_ops = &nfs4_get_lease_time_ops,
1bd714f2
TM
8818 .callback_data = &data,
8819 .flags = RPC_TASK_TIMEOUT,
2050f0cc 8820 };
2050f0cc 8821
fba83f34 8822 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
f836b27e 8823 return nfs4_call_sync_custom(&task_setup);
2050f0cc
AA
8824}
8825
0efb01b2
DB
8826#ifdef CONFIG_NFS_V4_1
8827
fc931582
AA
8828/*
8829 * Initialize the values to be used by the client in CREATE_SESSION
8830 * If nfs4_init_session set the fore channel request and response sizes,
8831 * use them.
8832 *
8833 * Set the back channel max_resp_sz_cached to zero to force the client to
8834 * always set csa_cachethis to FALSE because the current implementation
8835 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8836 */
6b26cc8c
CL
8837static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8838 struct rpc_clnt *clnt)
fc931582 8839{
18aad3d5 8840 unsigned int max_rqst_sz, max_resp_sz;
6b26cc8c 8841 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
7402a4fe 8842 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
18aad3d5
AA
8843
8844 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8845 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
fc931582 8846
fc931582 8847 /* Fore channel attributes */
18aad3d5
AA
8848 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8849 args->fc_attrs.max_resp_sz = max_resp_sz;
fc931582 8850 args->fc_attrs.max_ops = NFS4_MAX_OPS;
ef159e91 8851 args->fc_attrs.max_reqs = max_session_slots;
fc931582
AA
8852
8853 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8e0d46e1 8854 "max_ops=%u max_reqs=%u\n",
fc931582
AA
8855 __func__,
8856 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8e0d46e1 8857 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
fc931582
AA
8858
8859 /* Back channel attributes */
6b26cc8c
CL
8860 args->bc_attrs.max_rqst_sz = max_bc_payload;
8861 args->bc_attrs.max_resp_sz = max_bc_payload;
fc931582
AA
8862 args->bc_attrs.max_resp_sz_cached = 0;
8863 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
62421cd9 8864 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
7402a4fe
TM
8865 if (args->bc_attrs.max_reqs > max_bc_slots)
8866 args->bc_attrs.max_reqs = max_bc_slots;
fc931582
AA
8867
8868 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8869 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8870 __func__,
8871 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8872 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8873 args->bc_attrs.max_reqs);
8874}
8875
79969dd1
TM
8876static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8877 struct nfs41_create_session_res *res)
8d35301d 8878{
43c2e885 8879 struct nfs4_channel_attrs *sent = &args->fc_attrs;
79969dd1 8880 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
43c2e885 8881
43c2e885
BF
8882 if (rcvd->max_resp_sz > sent->max_resp_sz)
8883 return -EINVAL;
8884 /*
8885 * Our requested max_ops is the minimum we need; we're not
8886 * prepared to break up compounds into smaller pieces than that.
8887 * So, no point even trying to continue if the server won't
8888 * cooperate:
8889 */
8890 if (rcvd->max_ops < sent->max_ops)
8891 return -EINVAL;
8892 if (rcvd->max_reqs == 0)
8893 return -EINVAL;
b4b9a0c1
VG
8894 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8895 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
43c2e885 8896 return 0;
8d35301d
AA
8897}
8898
79969dd1
TM
8899static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8900 struct nfs41_create_session_res *res)
43c2e885
BF
8901{
8902 struct nfs4_channel_attrs *sent = &args->bc_attrs;
79969dd1 8903 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8d35301d 8904
b1c0df5f
TM
8905 if (!(res->flags & SESSION4_BACK_CHAN))
8906 goto out;
43c2e885
BF
8907 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8908 return -EINVAL;
8909 if (rcvd->max_resp_sz < sent->max_resp_sz)
8910 return -EINVAL;
8911 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8912 return -EINVAL;
5405fc44 8913 if (rcvd->max_ops > sent->max_ops)
43c2e885 8914 return -EINVAL;
5405fc44 8915 if (rcvd->max_reqs > sent->max_reqs)
43c2e885 8916 return -EINVAL;
b1c0df5f 8917out:
43c2e885
BF
8918 return 0;
8919}
8d35301d 8920
8d35301d 8921static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
79969dd1 8922 struct nfs41_create_session_res *res)
8d35301d 8923{
43c2e885 8924 int ret;
8d35301d 8925
79969dd1 8926 ret = nfs4_verify_fore_channel_attrs(args, res);
43c2e885
BF
8927 if (ret)
8928 return ret;
79969dd1
TM
8929 return nfs4_verify_back_channel_attrs(args, res);
8930}
8931
8932static void nfs4_update_session(struct nfs4_session *session,
8933 struct nfs41_create_session_res *res)
8934{
8935 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
e11259f9
TM
8936 /* Mark client id and session as being confirmed */
8937 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8938 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
79969dd1
TM
8939 session->flags = res->flags;
8940 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
b1c0df5f
TM
8941 if (res->flags & SESSION4_BACK_CHAN)
8942 memcpy(&session->bc_attrs, &res->bc_attrs,
8943 sizeof(session->bc_attrs));
8d35301d
AA
8944}
8945
848f5bda 8946static int _nfs4_proc_create_session(struct nfs_client *clp,
a52458b4 8947 const struct cred *cred)
fc931582
AA
8948{
8949 struct nfs4_session *session = clp->cl_session;
8950 struct nfs41_create_session_args args = {
8951 .client = clp,
79969dd1
TM
8952 .clientid = clp->cl_clientid,
8953 .seqid = clp->cl_seqid,
fc931582
AA
8954 .cb_program = NFS4_CALLBACK,
8955 };
79969dd1
TM
8956 struct nfs41_create_session_res res;
8957
fc931582
AA
8958 struct rpc_message msg = {
8959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8960 .rpc_argp = &args,
8961 .rpc_resp = &res,
848f5bda 8962 .rpc_cred = cred,
fc931582
AA
8963 };
8964 int status;
8965
6b26cc8c 8966 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
0f91421e 8967 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
fc931582 8968
5a0c257f
N
8969 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8970 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
c6d01c6f 8971 trace_nfs4_create_session(clp, status);
fc931582 8972
b519d408
TM
8973 switch (status) {
8974 case -NFS4ERR_STALE_CLIENTID:
8975 case -NFS4ERR_DELAY:
8976 case -ETIMEDOUT:
8977 case -EACCES:
8978 case -EAGAIN:
8979 goto out;
8b98a532 8980 }
b519d408
TM
8981
8982 clp->cl_seqid++;
43095d39 8983 if (!status) {
8d35301d 8984 /* Verify the session's negotiated channel_attrs values */
79969dd1 8985 status = nfs4_verify_channel_attrs(&args, &res);
fc931582 8986 /* Increment the clientid slot sequence id */
79969dd1
TM
8987 if (status)
8988 goto out;
8989 nfs4_update_session(session, &res);
fc931582 8990 }
79969dd1 8991out:
fc931582
AA
8992 return status;
8993}
8994
8995/*
8996 * Issues a CREATE_SESSION operation to the server.
8997 * It is the responsibility of the caller to verify the session is
8998 * expired before calling this routine.
8999 */
a52458b4 9000int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
fc931582
AA
9001{
9002 int status;
9003 unsigned *ptr;
fc931582
AA
9004 struct nfs4_session *session = clp->cl_session;
9005
9006 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9007
848f5bda 9008 status = _nfs4_proc_create_session(clp, cred);
fc931582
AA
9009 if (status)
9010 goto out;
9011
aacd5537
AA
9012 /* Init or reset the session slot tables */
9013 status = nfs4_setup_session_slot_tables(session);
9014 dprintk("slot table setup returned %d\n", status);
fc931582
AA
9015 if (status)
9016 goto out;
9017
9018 ptr = (unsigned *)&session->sess_id.data[0];
9019 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9020 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
fc931582
AA
9021out:
9022 dprintk("<-- %s\n", __func__);
9023 return status;
9024}
9025
0f3e66c6
AA
9026/*
9027 * Issue the over-the-wire RPC DESTROY_SESSION.
9028 * The caller must serialize access to this routine.
9029 */
848f5bda 9030int nfs4_proc_destroy_session(struct nfs4_session *session,
a52458b4 9031 const struct cred *cred)
0f3e66c6 9032{
848f5bda
TM
9033 struct rpc_message msg = {
9034 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9035 .rpc_argp = session,
9036 .rpc_cred = cred,
9037 };
0f3e66c6 9038 int status = 0;
0f3e66c6
AA
9039
9040 dprintk("--> nfs4_proc_destroy_session\n");
9041
9042 /* session is still being setup */
e11259f9
TM
9043 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9044 return 0;
0f3e66c6 9045
5a0c257f
N
9046 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9047 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
c6d01c6f 9048 trace_nfs4_destroy_session(session->clp, status);
0f3e66c6
AA
9049
9050 if (status)
08106ac7 9051 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
0f3e66c6
AA
9052 "Session has been destroyed regardless...\n", status);
9053
9054 dprintk("<-- nfs4_proc_destroy_session\n");
9055 return status;
9056}
9057
fc01cea9
AA
9058/*
9059 * Renew the cl_session lease.
9060 */
d5f8d3fe
TM
9061struct nfs4_sequence_data {
9062 struct nfs_client *clp;
9063 struct nfs4_sequence_args args;
9064 struct nfs4_sequence_res res;
9065};
9066
dc96aef9
AB
9067static void nfs41_sequence_release(void *data)
9068{
d5f8d3fe
TM
9069 struct nfs4_sequence_data *calldata = data;
9070 struct nfs_client *clp = calldata->clp;
dc96aef9 9071
212bf41d 9072 if (refcount_read(&clp->cl_count) > 1)
7135840f
AB
9073 nfs4_schedule_state_renewal(clp);
9074 nfs_put_client(clp);
d5f8d3fe 9075 kfree(calldata);
dc96aef9
AB
9076}
9077
aa5190d0
TM
9078static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9079{
9080 switch(task->tk_status) {
9081 case -NFS4ERR_DELAY:
aa5190d0
TM
9082 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9083 return -EAGAIN;
9084 default:
0400a6b0 9085 nfs4_schedule_lease_recovery(clp);
aa5190d0
TM
9086 }
9087 return 0;
9088}
9089
dc96aef9 9090static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
fc01cea9 9091{
d5f8d3fe
TM
9092 struct nfs4_sequence_data *calldata = data;
9093 struct nfs_client *clp = calldata->clp;
fc01cea9 9094
14516c3a
TM
9095 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9096 return;
fc01cea9 9097
c6d01c6f 9098 trace_nfs4_sequence(clp, task->tk_status);
fc01cea9
AA
9099 if (task->tk_status < 0) {
9100 dprintk("%s ERROR %d\n", __func__, task->tk_status);
212bf41d 9101 if (refcount_read(&clp->cl_count) == 1)
7135840f 9102 goto out;
fc01cea9 9103
aa5190d0
TM
9104 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9105 rpc_restart_call_prepare(task);
fc01cea9
AA
9106 return;
9107 }
9108 }
fc01cea9 9109 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7135840f 9110out:
fc01cea9
AA
9111 dprintk("<-- %s\n", __func__);
9112}
9113
9114static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9115{
d5f8d3fe
TM
9116 struct nfs4_sequence_data *calldata = data;
9117 struct nfs_client *clp = calldata->clp;
fc01cea9
AA
9118 struct nfs4_sequence_args *args;
9119 struct nfs4_sequence_res *res;
9120
fc01cea9
AA
9121 args = task->tk_msg.rpc_argp;
9122 res = task->tk_msg.rpc_resp;
9123
7981c8a6 9124 nfs4_setup_sequence(clp, args, res, task);
fc01cea9
AA
9125}
9126
9127static const struct rpc_call_ops nfs41_sequence_ops = {
9128 .rpc_call_done = nfs41_sequence_call_done,
9129 .rpc_call_prepare = nfs41_sequence_prepare,
dc96aef9 9130 .rpc_release = nfs41_sequence_release,
fc01cea9
AA
9131};
9132
8fe72bac 9133static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
a52458b4 9134 const struct cred *cred,
3be0f80b 9135 struct nfs4_slot *slot,
8fe72bac 9136 bool is_privileged)
fc01cea9 9137{
d5f8d3fe 9138 struct nfs4_sequence_data *calldata;
fc01cea9
AA
9139 struct rpc_message msg = {
9140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9141 .rpc_cred = cred,
9142 };
71ac6da9
TM
9143 struct rpc_task_setup task_setup_data = {
9144 .rpc_client = clp->cl_rpcclient,
9145 .rpc_message = &msg,
8fe72bac 9146 .callback_ops = &nfs41_sequence_ops,
bc7a05ca 9147 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
71ac6da9 9148 };
3be0f80b 9149 struct rpc_task *ret;
fc01cea9 9150
3be0f80b 9151 ret = ERR_PTR(-EIO);
212bf41d 9152 if (!refcount_inc_not_zero(&clp->cl_count))
3be0f80b
TM
9153 goto out_err;
9154
9155 ret = ERR_PTR(-ENOMEM);
dfb4f309 9156 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
3be0f80b
TM
9157 if (calldata == NULL)
9158 goto out_put_clp;
fba83f34 9159 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
3be0f80b 9160 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
d5f8d3fe
TM
9161 msg.rpc_argp = &calldata->args;
9162 msg.rpc_resp = &calldata->res;
9163 calldata->clp = clp;
71ac6da9 9164 task_setup_data.callback_data = calldata;
fc01cea9 9165
3be0f80b
TM
9166 ret = rpc_run_task(&task_setup_data);
9167 if (IS_ERR(ret))
9168 goto out_err;
9169 return ret;
9170out_put_clp:
9171 nfs_put_client(clp);
9172out_err:
9173 nfs41_release_slot(slot);
9174 return ret;
71ac6da9
TM
9175}
9176
a52458b4 9177static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
71ac6da9
TM
9178{
9179 struct rpc_task *task;
9180 int ret = 0;
9181
2f60ea6b 9182 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
d1f456b0 9183 return -EAGAIN;
3be0f80b 9184 task = _nfs41_proc_sequence(clp, cred, NULL, false);
71ac6da9
TM
9185 if (IS_ERR(task))
9186 ret = PTR_ERR(task);
9187 else
bf294b41 9188 rpc_put_task_async(task);
71ac6da9
TM
9189 dprintk("<-- %s status=%d\n", __func__, ret);
9190 return ret;
9191}
9192
a52458b4 9193static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
71ac6da9
TM
9194{
9195 struct rpc_task *task;
9196 int ret;
9197
3be0f80b 9198 task = _nfs41_proc_sequence(clp, cred, NULL, true);
71ac6da9
TM
9199 if (IS_ERR(task)) {
9200 ret = PTR_ERR(task);
9201 goto out;
9202 }
9203 ret = rpc_wait_for_completion_task(task);
be824167 9204 if (!ret)
71ac6da9
TM
9205 ret = task->tk_status;
9206 rpc_put_task(task);
9207out:
9208 dprintk("<-- %s status=%d\n", __func__, ret);
9209 return ret;
fc01cea9
AA
9210}
9211
fce5c838
RL
9212struct nfs4_reclaim_complete_data {
9213 struct nfs_client *clp;
9214 struct nfs41_reclaim_complete_args arg;
9215 struct nfs41_reclaim_complete_res res;
9216};
9217
9218static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9219{
9220 struct nfs4_reclaim_complete_data *calldata = data;
9221
7981c8a6 9222 nfs4_setup_sequence(calldata->clp,
d9afbd1b
TM
9223 &calldata->arg.seq_args,
9224 &calldata->res.seq_res,
9225 task);
fce5c838
RL
9226}
9227
aa5190d0
TM
9228static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9229{
9230 switch(task->tk_status) {
9231 case 0:
57174593 9232 wake_up_all(&clp->cl_lock_waitq);
df561f66 9233 fallthrough;
aa5190d0
TM
9234 case -NFS4ERR_COMPLETE_ALREADY:
9235 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9236 break;
9237 case -NFS4ERR_DELAY:
aa5190d0 9238 rpc_delay(task, NFS4_POLL_RETRY_MAX);
df561f66 9239 fallthrough;
a8a4ae3a 9240 case -NFS4ERR_RETRY_UNCACHED_REP:
aa5190d0 9241 return -EAGAIN;
0048fdd0
TM
9242 case -NFS4ERR_BADSESSION:
9243 case -NFS4ERR_DEADSESSION:
9244 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
0048fdd0 9245 break;
aa5190d0 9246 default:
0400a6b0 9247 nfs4_schedule_lease_recovery(clp);
aa5190d0
TM
9248 }
9249 return 0;
9250}
9251
fce5c838
RL
9252static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9253{
9254 struct nfs4_reclaim_complete_data *calldata = data;
9255 struct nfs_client *clp = calldata->clp;
9256 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9257
9258 dprintk("--> %s\n", __func__);
14516c3a
TM
9259 if (!nfs41_sequence_done(task, res))
9260 return;
fce5c838 9261
c6d01c6f 9262 trace_nfs4_reclaim_complete(clp, task->tk_status);
aa5190d0
TM
9263 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9264 rpc_restart_call_prepare(task);
9265 return;
9266 }
fce5c838
RL
9267 dprintk("<-- %s\n", __func__);
9268}
9269
9270static void nfs4_free_reclaim_complete_data(void *data)
9271{
9272 struct nfs4_reclaim_complete_data *calldata = data;
9273
9274 kfree(calldata);
9275}
9276
9277static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9278 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9279 .rpc_call_done = nfs4_reclaim_complete_done,
9280 .rpc_release = nfs4_free_reclaim_complete_data,
9281};
9282
9283/*
9284 * Issue a global reclaim complete.
9285 */
965e9c23 9286static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
a52458b4 9287 const struct cred *cred)
fce5c838
RL
9288{
9289 struct nfs4_reclaim_complete_data *calldata;
fce5c838
RL
9290 struct rpc_message msg = {
9291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
965e9c23 9292 .rpc_cred = cred,
fce5c838
RL
9293 };
9294 struct rpc_task_setup task_setup_data = {
9295 .rpc_client = clp->cl_rpcclient,
9296 .rpc_message = &msg,
9297 .callback_ops = &nfs4_reclaim_complete_call_ops,
4c952e3d 9298 .flags = RPC_TASK_NO_ROUND_ROBIN,
fce5c838
RL
9299 };
9300 int status = -ENOMEM;
9301
9302 dprintk("--> %s\n", __func__);
8535b2be 9303 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
fce5c838
RL
9304 if (calldata == NULL)
9305 goto out;
9306 calldata->clp = clp;
9307 calldata->arg.one_fs = 0;
fce5c838 9308
fba83f34 9309 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
fce5c838
RL
9310 msg.rpc_argp = &calldata->arg;
9311 msg.rpc_resp = &calldata->res;
9312 task_setup_data.callback_data = calldata;
4c952e3d 9313 status = nfs4_call_sync_custom(&task_setup_data);
fce5c838
RL
9314out:
9315 dprintk("<-- %s status=%d\n", __func__, status);
9316 return status;
9317}
b1f69b75
AA
9318
9319static void
9320nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9321{
9322 struct nfs4_layoutget *lgp = calldata;
c31663d4 9323 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
b1f69b75
AA
9324
9325 dprintk("--> %s\n", __func__);
7981c8a6 9326 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
183d9e7b
JL
9327 &lgp->res.seq_res, task);
9328 dprintk("<-- %s\n", __func__);
b1f69b75
AA
9329}
9330
9331static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9332{
9333 struct nfs4_layoutget *lgp = calldata;
183d9e7b
JL
9334
9335 dprintk("--> %s\n", __func__);
2e80dbe7 9336 nfs41_sequence_process(task, &lgp->res.seq_res);
183d9e7b
JL
9337 dprintk("<-- %s\n", __func__);
9338}
9339
9340static int
9341nfs4_layoutget_handle_exception(struct rpc_task *task,
9342 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9343{
ee314c2a
TM
9344 struct inode *inode = lgp->args.inode;
9345 struct nfs_server *server = NFS_SERVER(inode);
9346 struct pnfs_layout_hdr *lo;
e85d7ee4
TM
9347 int nfs4err = task->tk_status;
9348 int err, status = 0;
f7db0b28 9349 LIST_HEAD(head);
b1f69b75 9350
ed7e5423 9351 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
b1f69b75 9352
2dbf8dff
TM
9353 nfs4_sequence_free_slot(&lgp->res.seq_res);
9354
e85d7ee4 9355 switch (nfs4err) {
b1f69b75 9356 case 0:
ee314c2a 9357 goto out;
7c1e6e58
PT
9358
9359 /*
9360 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9361 * on the file. set tk_status to -ENODATA to tell upper layer to
9362 * retry go inband.
9363 */
9364 case -NFS4ERR_LAYOUTUNAVAILABLE:
183d9e7b 9365 status = -ENODATA;
7c1e6e58 9366 goto out;
21b874c8
TM
9367 /*
9368 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9369 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9370 */
9371 case -NFS4ERR_BADLAYOUT:
183d9e7b
JL
9372 status = -EOVERFLOW;
9373 goto out;
ed7e5423
BH
9374 /*
9375 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
21b874c8
TM
9376 * (or clients) writing to the same RAID stripe except when
9377 * the minlength argument is 0 (see RFC5661 section 18.43.3).
183d9e7b
JL
9378 *
9379 * Treat it like we would RECALLCONFLICT -- we retry for a little
9380 * while, and then eventually give up.
ed7e5423 9381 */
b1f69b75 9382 case -NFS4ERR_LAYOUTTRYLATER:
183d9e7b
JL
9383 if (lgp->args.minlength == 0) {
9384 status = -EOVERFLOW;
9385 goto out;
ed7e5423 9386 }
e85d7ee4
TM
9387 status = -EBUSY;
9388 break;
183d9e7b 9389 case -NFS4ERR_RECALLCONFLICT:
183d9e7b 9390 status = -ERECALLCONFLICT;
e85d7ee4 9391 break;
26f47443
TM
9392 case -NFS4ERR_DELEG_REVOKED:
9393 case -NFS4ERR_ADMIN_REVOKED:
ee314c2a
TM
9394 case -NFS4ERR_EXPIRED:
9395 case -NFS4ERR_BAD_STATEID:
183d9e7b 9396 exception->timeout = 0;
ee314c2a 9397 spin_lock(&inode->i_lock);
f7db0b28
TM
9398 lo = NFS_I(inode)->layout;
9399 /* If the open stateid was bad, then recover it. */
9400 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
e8fa33a6 9401 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
ee314c2a 9402 spin_unlock(&inode->i_lock);
183d9e7b 9403 exception->state = lgp->args.ctx->state;
26f47443 9404 exception->stateid = &lgp->args.stateid;
2259f960
TM
9405 break;
9406 }
f7db0b28
TM
9407
9408 /*
9409 * Mark the bad layout state as invalid, then retry
9410 */
668f455d 9411 pnfs_mark_layout_stateid_invalid(lo, &head);
f7db0b28 9412 spin_unlock(&inode->i_lock);
1f18b82c 9413 nfs_commit_inode(inode, 0);
f7db0b28
TM
9414 pnfs_free_lseg_list(&head);
9415 status = -EAGAIN;
9416 goto out;
b1f69b75 9417 }
183d9e7b 9418
e85d7ee4
TM
9419 err = nfs4_handle_exception(server, nfs4err, exception);
9420 if (!status) {
9421 if (exception->retry)
9422 status = -EAGAIN;
9423 else
9424 status = err;
9425 }
ee314c2a 9426out:
b1f69b75 9427 dprintk("<-- %s\n", __func__);
183d9e7b 9428 return status;
b1f69b75
AA
9429}
9430
dacb452d 9431size_t max_response_pages(struct nfs_server *server)
8554116e
IK
9432{
9433 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9434 return nfs_page_array_len(0, max_resp_sz);
9435}
9436
b1f69b75
AA
9437static void nfs4_layoutget_release(void *calldata)
9438{
9439 struct nfs4_layoutget *lgp = calldata;
9440
9441 dprintk("--> %s\n", __func__);
bd171930 9442 nfs4_sequence_free_slot(&lgp->res.seq_res);
29a8bfe5 9443 pnfs_layoutget_free(lgp);
b1f69b75
AA
9444 dprintk("<-- %s\n", __func__);
9445}
9446
9447static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9448 .rpc_call_prepare = nfs4_layoutget_prepare,
9449 .rpc_call_done = nfs4_layoutget_done,
9450 .rpc_release = nfs4_layoutget_release,
9451};
9452
a0b0a6e3 9453struct pnfs_layout_segment *
dacb452d 9454nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
b1f69b75 9455{
a47970ff
WAA
9456 struct inode *inode = lgp->args.inode;
9457 struct nfs_server *server = NFS_SERVER(inode);
b1f69b75
AA
9458 struct rpc_task *task;
9459 struct rpc_message msg = {
9460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9461 .rpc_argp = &lgp->args,
9462 .rpc_resp = &lgp->res,
6ab59344 9463 .rpc_cred = lgp->cred,
b1f69b75
AA
9464 };
9465 struct rpc_task_setup task_setup_data = {
9466 .rpc_client = server->client,
9467 .rpc_message = &msg,
9468 .callback_ops = &nfs4_layoutget_call_ops,
9469 .callback_data = lgp,
63ec2b69 9470 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
b1f69b75 9471 };
a0b0a6e3 9472 struct pnfs_layout_segment *lseg = NULL;
bc23676c
TM
9473 struct nfs4_exception exception = {
9474 .inode = inode,
9475 .timeout = *timeout,
9476 };
b1f69b75
AA
9477 int status = 0;
9478
9479 dprintk("--> %s\n", __func__);
9480
4bd5a980
PT
9481 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9482 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9483
fba83f34 9484 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
a47970ff 9485
b1f69b75 9486 task = rpc_run_task(&task_setup_data);
6e47666e 9487
820bf85c 9488 status = rpc_wait_for_completion_task(task);
2dbf8dff
TM
9489 if (status != 0)
9490 goto out;
9491
18c0778a 9492 if (task->tk_status < 0) {
183d9e7b
JL
9493 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9494 *timeout = exception.timeout;
18c0778a
TM
9495 } else if (lgp->res.layoutp->len == 0) {
9496 status = -EAGAIN;
9497 *timeout = nfs4_update_delay(&exception.timeout);
2dbf8dff
TM
9498 } else
9499 lseg = pnfs_layout_process(lgp);
9500out:
1037e6ea
TM
9501 trace_nfs4_layoutget(lgp->args.ctx,
9502 &lgp->args.range,
9503 &lgp->res.range,
48c9579a 9504 &lgp->res.stateid,
1037e6ea 9505 status);
183d9e7b 9506
b1f69b75
AA
9507 rpc_put_task(task);
9508 dprintk("<-- %s status=%d\n", __func__, status);
a0b0a6e3
TM
9509 if (status)
9510 return ERR_PTR(status);
9511 return lseg;
b1f69b75
AA
9512}
9513
cbe82603
BH
9514static void
9515nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9516{
9517 struct nfs4_layoutreturn *lrp = calldata;
9518
9519 dprintk("--> %s\n", __func__);
7981c8a6 9520 nfs4_setup_sequence(lrp->clp,
d9afbd1b
TM
9521 &lrp->args.seq_args,
9522 &lrp->res.seq_res,
9523 task);
c8bf7073
TM
9524 if (!pnfs_layout_is_valid(lrp->args.layout))
9525 rpc_exit(task, 0);
cbe82603
BH
9526}
9527
9528static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9529{
9530 struct nfs4_layoutreturn *lrp = calldata;
9531 struct nfs_server *server;
9532
9533 dprintk("--> %s\n", __func__);
9534
2e80dbe7 9535 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
cbe82603
BH
9536 return;
9537
6109bcf7
TM
9538 /*
9539 * Was there an RPC level error? Assume the call succeeded,
9540 * and that we need to release the layout
9541 */
9542 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9543 lrp->res.lrs_present = 0;
9544 return;
9545 }
9546
cbe82603 9547 server = NFS_SERVER(lrp->args.inode);
f22e5edd 9548 switch (task->tk_status) {
ff90514e 9549 case -NFS4ERR_OLD_STATEID:
30cb3ee2 9550 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
ecf84026 9551 &lrp->args.range,
ff90514e
TM
9552 lrp->args.inode))
9553 goto out_restart;
df561f66 9554 fallthrough;
f22e5edd
TM
9555 default:
9556 task->tk_status = 0;
df561f66 9557 fallthrough;
f22e5edd
TM
9558 case 0:
9559 break;
9560 case -NFS4ERR_DELAY:
8478eaa1 9561 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
f22e5edd 9562 break;
ff90514e 9563 goto out_restart;
cbe82603 9564 }
cbe82603 9565 dprintk("<-- %s\n", __func__);
ff90514e
TM
9566 return;
9567out_restart:
9568 task->tk_status = 0;
9569 nfs4_sequence_free_slot(&lrp->res.seq_res);
9570 rpc_restart_call_prepare(task);
cbe82603
BH
9571}
9572
9573static void nfs4_layoutreturn_release(void *calldata)
9574{
9575 struct nfs4_layoutreturn *lrp = calldata;
849b286f 9576 struct pnfs_layout_hdr *lo = lrp->args.layout;
cbe82603
BH
9577
9578 dprintk("--> %s\n", __func__);
2a974425 9579 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
68f74479 9580 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
2e80dbe7 9581 nfs4_sequence_free_slot(&lrp->res.seq_res);
4d796d75
TM
9582 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9583 lrp->ld_private.ops->free(&lrp->ld_private);
2f065ddb
TM
9584 pnfs_put_layout_hdr(lrp->args.layout);
9585 nfs_iput_and_deactive(lrp->inode);
44ea8dfc 9586 put_cred(lrp->cred);
cbe82603
BH
9587 kfree(calldata);
9588 dprintk("<-- %s\n", __func__);
9589}
9590
9591static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9592 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9593 .rpc_call_done = nfs4_layoutreturn_done,
9594 .rpc_release = nfs4_layoutreturn_release,
9595};
9596
6c16605d 9597int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
cbe82603
BH
9598{
9599 struct rpc_task *task;
9600 struct rpc_message msg = {
9601 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9602 .rpc_argp = &lrp->args,
9603 .rpc_resp = &lrp->res,
9556000d 9604 .rpc_cred = lrp->cred,
cbe82603
BH
9605 };
9606 struct rpc_task_setup task_setup_data = {
1771c577 9607 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
cbe82603
BH
9608 .rpc_message = &msg,
9609 .callback_ops = &nfs4_layoutreturn_call_ops,
9610 .callback_data = lrp,
9611 };
6c16605d 9612 int status = 0;
cbe82603 9613
99ade3c7
AE
9614 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9615 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9616 &task_setup_data.rpc_client, &msg);
9617
cbe82603 9618 dprintk("--> %s\n", __func__);
5a0ec8ac
TM
9619 if (!sync) {
9620 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9621 if (!lrp->inode) {
9622 nfs4_layoutreturn_release(lrp);
9623 return -EAGAIN;
9624 }
9625 task_setup_data.flags |= RPC_TASK_ASYNC;
9626 }
fba83f34 9627 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
cbe82603
BH
9628 task = rpc_run_task(&task_setup_data);
9629 if (IS_ERR(task))
9630 return PTR_ERR(task);
5a0ec8ac
TM
9631 if (sync)
9632 status = task->tk_status;
48c9579a 9633 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
cbe82603
BH
9634 dprintk("<-- %s status=%d\n", __func__, status);
9635 rpc_put_task(task);
9636 return status;
9637}
9638
b1f69b75 9639static int
cd5875fe
TM
9640_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9641 struct pnfs_device *pdev,
a52458b4 9642 const struct cred *cred)
b1f69b75
AA
9643{
9644 struct nfs4_getdeviceinfo_args args = {
9645 .pdev = pdev,
4e590803
TM
9646 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9647 NOTIFY_DEVICEID4_DELETE,
b1f69b75
AA
9648 };
9649 struct nfs4_getdeviceinfo_res res = {
9650 .pdev = pdev,
9651 };
9652 struct rpc_message msg = {
9653 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9654 .rpc_argp = &args,
9655 .rpc_resp = &res,
cd5875fe 9656 .rpc_cred = cred,
b1f69b75
AA
9657 };
9658 int status;
9659
9660 dprintk("--> %s\n", __func__);
7c513058 9661 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4e590803
TM
9662 if (res.notification & ~args.notify_types)
9663 dprintk("%s: unsupported notification\n", __func__);
df52699e
TM
9664 if (res.notification != args.notify_types)
9665 pdev->nocache = 1;
4e590803 9666
cac1d3a2
TM
9667 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9668
b1f69b75
AA
9669 dprintk("<-- %s status=%d\n", __func__, status);
9670
9671 return status;
9672}
9673
cd5875fe
TM
9674int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9675 struct pnfs_device *pdev,
a52458b4 9676 const struct cred *cred)
b1f69b75
AA
9677{
9678 struct nfs4_exception exception = { };
9679 int err;
9680
9681 do {
9682 err = nfs4_handle_exception(server,
cd5875fe 9683 _nfs4_proc_getdeviceinfo(server, pdev, cred),
b1f69b75
AA
9684 &exception);
9685 } while (exception.retry);
9686 return err;
9687}
9688EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9689
863a3c6c
AA
9690static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9691{
9692 struct nfs4_layoutcommit_data *data = calldata;
9693 struct nfs_server *server = NFS_SERVER(data->args.inode);
9694
7981c8a6 9695 nfs4_setup_sequence(server->nfs_client,
d9afbd1b
TM
9696 &data->args.seq_args,
9697 &data->res.seq_res,
9698 task);
863a3c6c
AA
9699}
9700
9701static void
9702nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9703{
9704 struct nfs4_layoutcommit_data *data = calldata;
9705 struct nfs_server *server = NFS_SERVER(data->args.inode);
9706
6ba7db34 9707 if (!nfs41_sequence_done(task, &data->res.seq_res))
863a3c6c
AA
9708 return;
9709
9710 switch (task->tk_status) { /* Just ignore these failures */
e59d27e0
TM
9711 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9712 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9713 case -NFS4ERR_BADLAYOUT: /* no layout */
9714 case -NFS4ERR_GRACE: /* loca_recalim always false */
863a3c6c 9715 task->tk_status = 0;
e59d27e0 9716 case 0:
e59d27e0
TM
9717 break;
9718 default:
8478eaa1 9719 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
e59d27e0
TM
9720 rpc_restart_call_prepare(task);
9721 return;
9722 }
9723 }
863a3c6c
AA
9724}
9725
9726static void nfs4_layoutcommit_release(void *calldata)
9727{
9728 struct nfs4_layoutcommit_data *data = calldata;
9729
db29c089 9730 pnfs_cleanup_layoutcommit(data);
d8c951c3
TM
9731 nfs_post_op_update_inode_force_wcc(data->args.inode,
9732 data->res.fattr);
a52458b4 9733 put_cred(data->cred);
472e2594 9734 nfs_iput_and_deactive(data->inode);
863a3c6c
AA
9735 kfree(data);
9736}
9737
9738static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9739 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9740 .rpc_call_done = nfs4_layoutcommit_done,
9741 .rpc_release = nfs4_layoutcommit_release,
9742};
9743
9744int
ef311537 9745nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
863a3c6c
AA
9746{
9747 struct rpc_message msg = {
9748 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9749 .rpc_argp = &data->args,
9750 .rpc_resp = &data->res,
9751 .rpc_cred = data->cred,
9752 };
9753 struct rpc_task_setup task_setup_data = {
9754 .task = &data->task,
9755 .rpc_client = NFS_CLIENT(data->args.inode),
9756 .rpc_message = &msg,
9757 .callback_ops = &nfs4_layoutcommit_ops,
9758 .callback_data = data,
863a3c6c
AA
9759 };
9760 struct rpc_task *task;
9761 int status = 0;
9762
b4839ebe
KM
9763 dprintk("NFS: initiating layoutcommit call. sync %d "
9764 "lbw: %llu inode %lu\n", sync,
863a3c6c
AA
9765 data->args.lastbytewritten,
9766 data->args.inode->i_ino);
9767
472e2594
TM
9768 if (!sync) {
9769 data->inode = nfs_igrab_and_active(data->args.inode);
9770 if (data->inode == NULL) {
9771 nfs4_layoutcommit_release(data);
9772 return -EAGAIN;
9773 }
9774 task_setup_data.flags = RPC_TASK_ASYNC;
9775 }
fba83f34 9776 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
863a3c6c
AA
9777 task = rpc_run_task(&task_setup_data);
9778 if (IS_ERR(task))
9779 return PTR_ERR(task);
472e2594
TM
9780 if (sync)
9781 status = task->tk_status;
48c9579a 9782 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
863a3c6c
AA
9783 dprintk("%s: status %d\n", __func__, status);
9784 rpc_put_task(task);
9785 return status;
9786}
fca78d6d 9787
302fad7b 9788/*
97431204
AA
9789 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9790 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9791 */
fca78d6d
BS
9792static int
9793_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
b1b3e136
WAA
9794 struct nfs_fsinfo *info,
9795 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
fca78d6d
BS
9796{
9797 struct nfs41_secinfo_no_name_args args = {
9798 .style = SECINFO_STYLE_CURRENT_FH,
9799 };
9800 struct nfs4_secinfo_res res = {
9801 .flavors = flavors,
9802 };
9803 struct rpc_message msg = {
9804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9805 .rpc_argp = &args,
9806 .rpc_resp = &res,
9807 };
cc15e24a
AS
9808 struct nfs4_call_sync_data data = {
9809 .seq_server = server,
9810 .seq_args = &args.seq_args,
9811 .seq_res = &res.seq_res,
9812 };
9813 struct rpc_task_setup task_setup = {
9814 .rpc_client = server->client,
9815 .rpc_message = &msg,
9816 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9817 .callback_data = &data,
9818 .flags = RPC_TASK_NO_ROUND_ROBIN,
9819 };
a52458b4 9820 const struct cred *cred = NULL;
b1b3e136
WAA
9821 int status;
9822
9823 if (use_integrity) {
48bb6ec1 9824 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
cc15e24a 9825
7cb852df
WAA
9826 cred = nfs4_get_clid_cred(server->nfs_client);
9827 msg.rpc_cred = cred;
b1b3e136
WAA
9828 }
9829
9830 dprintk("--> %s\n", __func__);
cc15e24a
AS
9831 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9832 status = nfs4_call_sync_custom(&task_setup);
b1b3e136
WAA
9833 dprintk("<-- %s status=%d\n", __func__, status);
9834
a52458b4 9835 put_cred(cred);
b1b3e136
WAA
9836
9837 return status;
fca78d6d
BS
9838}
9839
9840static int
9841nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9842 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9843{
0688e64b
TM
9844 struct nfs4_exception exception = {
9845 .interruptible = true,
9846 };
fca78d6d
BS
9847 int err;
9848 do {
b1b3e136
WAA
9849 /* first try using integrity protection */
9850 err = -NFS4ERR_WRONGSEC;
9851
9852 /* try to use integrity protection with machine cred */
9853 if (_nfs4_is_integrity_protected(server->nfs_client))
9854 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9855 flavors, true);
9856
9857 /*
9858 * if unable to use integrity protection, or SECINFO with
9859 * integrity protection returns NFS4ERR_WRONGSEC (which is
9860 * disallowed by spec, but exists in deployed servers) use
9861 * the current filesystem's rpc_client and the user cred.
9862 */
9863 if (err == -NFS4ERR_WRONGSEC)
9864 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9865 flavors, false);
9866
fca78d6d
BS
9867 switch (err) {
9868 case 0:
9869 case -NFS4ERR_WRONGSEC:
78b19bae 9870 case -ENOTSUPP:
05e9cfb4 9871 goto out;
fca78d6d
BS
9872 default:
9873 err = nfs4_handle_exception(server, err, &exception);
9874 }
9875 } while (exception.retry);
05e9cfb4 9876out:
fca78d6d
BS
9877 return err;
9878}
9879
9880static int
9881nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9882 struct nfs_fsinfo *info)
9883{
9884 int err;
9885 struct page *page;
367156d9 9886 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
fca78d6d 9887 struct nfs4_secinfo_flavors *flavors;
58a8cf12
WAA
9888 struct nfs4_secinfo4 *secinfo;
9889 int i;
fca78d6d
BS
9890
9891 page = alloc_page(GFP_KERNEL);
9892 if (!page) {
9893 err = -ENOMEM;
9894 goto out;
9895 }
9896
9897 flavors = page_address(page);
9898 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9899
9900 /*
9901 * Fall back on "guess and check" method if
9902 * the server doesn't support SECINFO_NO_NAME
9903 */
78b19bae 9904 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
fca78d6d
BS
9905 err = nfs4_find_root_sec(server, fhandle, info);
9906 goto out_freepage;
9907 }
9908 if (err)
9909 goto out_freepage;
9910
58a8cf12
WAA
9911 for (i = 0; i < flavors->num_flavors; i++) {
9912 secinfo = &flavors->flavors[i];
9913
9914 switch (secinfo->flavor) {
9915 case RPC_AUTH_NULL:
9916 case RPC_AUTH_UNIX:
9917 case RPC_AUTH_GSS:
9918 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9919 &secinfo->flavor_info);
9920 break;
9921 default:
9922 flavor = RPC_AUTH_MAXFLAVOR;
9923 break;
9924 }
9925
4d4b69dd
WAA
9926 if (!nfs_auth_info_match(&server->auth_info, flavor))
9927 flavor = RPC_AUTH_MAXFLAVOR;
9928
58a8cf12
WAA
9929 if (flavor != RPC_AUTH_MAXFLAVOR) {
9930 err = nfs4_lookup_root_sec(server, fhandle,
9931 info, flavor);
9932 if (!err)
9933 break;
9934 }
9935 }
9936
9937 if (flavor == RPC_AUTH_MAXFLAVOR)
9938 err = -EPERM;
fca78d6d
BS
9939
9940out_freepage:
9941 put_page(page);
9942 if (err == -EACCES)
9943 return -EPERM;
9944out:
9945 return err;
9946}
1cab0652 9947
ab7cb0df
TM
9948static int _nfs41_test_stateid(struct nfs_server *server,
9949 nfs4_stateid *stateid,
a52458b4 9950 const struct cred *cred)
7d974794
BS
9951{
9952 int status;
9953 struct nfs41_test_stateid_args args = {
1cab0652 9954 .stateid = stateid,
7d974794
BS
9955 };
9956 struct nfs41_test_stateid_res res;
9957 struct rpc_message msg = {
9958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9959 .rpc_argp = &args,
9960 .rpc_resp = &res,
ab7cb0df 9961 .rpc_cred = cred,
7d974794 9962 };
3787d506
WAA
9963 struct rpc_clnt *rpc_client = server->client;
9964
9965 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9966 &rpc_client, &msg);
1cab0652 9967
38527b15 9968 dprintk("NFS call test_stateid %p\n", stateid);
fba83f34 9969 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
3787d506 9970 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8fe72bac 9971 &args.seq_args, &res.seq_res);
38527b15
CL
9972 if (status != NFS_OK) {
9973 dprintk("NFS reply test_stateid: failed, %d\n", status);
377e507d 9974 return status;
38527b15
CL
9975 }
9976 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
377e507d 9977 return -res.status;
7d974794
BS
9978}
9979
43912bbb
TM
9980static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9981 int err, struct nfs4_exception *exception)
9982{
9983 exception->retry = 0;
9984 switch(err) {
9985 case -NFS4ERR_DELAY:
76e8a1bd 9986 case -NFS4ERR_RETRY_UNCACHED_REP:
43912bbb
TM
9987 nfs4_handle_exception(server, err, exception);
9988 break;
9989 case -NFS4ERR_BADSESSION:
9990 case -NFS4ERR_BADSLOT:
9991 case -NFS4ERR_BAD_HIGH_SLOT:
9992 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9993 case -NFS4ERR_DEADSESSION:
9994 nfs4_do_handle_exception(server, err, exception);
9995 }
9996}
9997
38527b15
CL
9998/**
9999 * nfs41_test_stateid - perform a TEST_STATEID operation
10000 *
10001 * @server: server / transport on which to perform the operation
10002 * @stateid: state ID to test
ab7cb0df 10003 * @cred: credential
38527b15
CL
10004 *
10005 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10006 * Otherwise a negative NFS4ERR value is returned if the operation
10007 * failed or the state ID is not currently valid.
10008 */
ab7cb0df
TM
10009static int nfs41_test_stateid(struct nfs_server *server,
10010 nfs4_stateid *stateid,
a52458b4 10011 const struct cred *cred)
7d974794 10012{
0688e64b
TM
10013 struct nfs4_exception exception = {
10014 .interruptible = true,
10015 };
7d974794
BS
10016 int err;
10017 do {
ab7cb0df 10018 err = _nfs41_test_stateid(server, stateid, cred);
43912bbb 10019 nfs4_handle_delay_or_session_error(server, err, &exception);
7d974794
BS
10020 } while (exception.retry);
10021 return err;
10022}
9aeda35f 10023
7c1d5fae
TM
10024struct nfs_free_stateid_data {
10025 struct nfs_server *server;
10026 struct nfs41_free_stateid_args args;
9aeda35f 10027 struct nfs41_free_stateid_res res;
7c1d5fae
TM
10028};
10029
10030static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10031{
10032 struct nfs_free_stateid_data *data = calldata;
7981c8a6 10033 nfs4_setup_sequence(data->server->nfs_client,
7c1d5fae
TM
10034 &data->args.seq_args,
10035 &data->res.seq_res,
10036 task);
10037}
10038
10039static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10040{
10041 struct nfs_free_stateid_data *data = calldata;
10042
10043 nfs41_sequence_done(task, &data->res.seq_res);
10044
10045 switch (task->tk_status) {
10046 case -NFS4ERR_DELAY:
8478eaa1 10047 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
7c1d5fae
TM
10048 rpc_restart_call_prepare(task);
10049 }
10050}
10051
10052static void nfs41_free_stateid_release(void *calldata)
10053{
10054 kfree(calldata);
10055}
10056
17f26b12 10057static const struct rpc_call_ops nfs41_free_stateid_ops = {
7c1d5fae
TM
10058 .rpc_call_prepare = nfs41_free_stateid_prepare,
10059 .rpc_call_done = nfs41_free_stateid_done,
10060 .rpc_release = nfs41_free_stateid_release,
10061};
10062
2f261020
AS
10063/**
10064 * nfs41_free_stateid - perform a FREE_STATEID operation
10065 *
10066 * @server: server / transport on which to perform the operation
10067 * @stateid: state ID to release
10068 * @cred: credential
302fad7b 10069 * @privileged: set to true if this call needs to be privileged
2f261020
AS
10070 *
10071 * Note: this function is always asynchronous.
10072 */
10073static int nfs41_free_stateid(struct nfs_server *server,
f0b0bf88 10074 const nfs4_stateid *stateid,
a52458b4 10075 const struct cred *cred,
7c1d5fae
TM
10076 bool privileged)
10077{
9aeda35f
BS
10078 struct rpc_message msg = {
10079 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
ab7cb0df 10080 .rpc_cred = cred,
9aeda35f 10081 };
7c1d5fae
TM
10082 struct rpc_task_setup task_setup = {
10083 .rpc_client = server->client,
10084 .rpc_message = &msg,
10085 .callback_ops = &nfs41_free_stateid_ops,
10086 .flags = RPC_TASK_ASYNC,
10087 };
10088 struct nfs_free_stateid_data *data;
2f261020 10089 struct rpc_task *task;
9aeda35f 10090
3787d506
WAA
10091 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10092 &task_setup.rpc_client, &msg);
10093
38527b15 10094 dprintk("NFS call free_stateid %p\n", stateid);
7c1d5fae
TM
10095 data = kmalloc(sizeof(*data), GFP_NOFS);
10096 if (!data)
2f261020 10097 return -ENOMEM;
7c1d5fae
TM
10098 data->server = server;
10099 nfs4_stateid_copy(&data->args.stateid, stateid);
10100
10101 task_setup.callback_data = data;
10102
10103 msg.rpc_argp = &data->args;
10104 msg.rpc_resp = &data->res;
fba83f34 10105 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
2f261020 10106 task = rpc_run_task(&task_setup);
7c1d5fae
TM
10107 if (IS_ERR(task))
10108 return PTR_ERR(task);
7c1d5fae 10109 rpc_put_task(task);
f0b0bf88 10110 return 0;
9aeda35f 10111}
36281caa 10112
f1cdae87
JL
10113static void
10114nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
c8b2d0bf 10115{
a52458b4 10116 const struct cred *cred = lsp->ls_state->owner->so_cred;
c8b2d0bf 10117
f0b0bf88 10118 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
c8b2d0bf 10119 nfs4_free_lock_state(server, lsp);
c8b2d0bf
TM
10120}
10121
36281caa
TM
10122static bool nfs41_match_stateid(const nfs4_stateid *s1,
10123 const nfs4_stateid *s2)
10124{
93b717fd
TM
10125 if (s1->type != s2->type)
10126 return false;
10127
2d2f24ad 10128 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
36281caa
TM
10129 return false;
10130
2d2f24ad 10131 if (s1->seqid == s2->seqid)
36281caa 10132 return true;
36281caa 10133
045c5519 10134 return s1->seqid == 0 || s2->seqid == 0;
36281caa
TM
10135}
10136
557134a3
AA
10137#endif /* CONFIG_NFS_V4_1 */
10138
36281caa
TM
10139static bool nfs4_match_stateid(const nfs4_stateid *s1,
10140 const nfs4_stateid *s2)
10141{
f597c537 10142 return nfs4_stateid_match(s1, s2);
36281caa
TM
10143}
10144
10145
17280175 10146static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
7eff03ae 10147 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
b79a4a1b 10148 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
1da177e4
LT
10149 .recover_open = nfs4_open_reclaim,
10150 .recover_lock = nfs4_lock_reclaim,
591d71cb 10151 .establish_clid = nfs4_init_clientid,
05f4c350 10152 .detect_trunking = nfs40_discover_server_trunking,
1da177e4
LT
10153};
10154
591d71cb 10155#if defined(CONFIG_NFS_V4_1)
17280175 10156static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
591d71cb
AA
10157 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10158 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10159 .recover_open = nfs4_open_reclaim,
10160 .recover_lock = nfs4_lock_reclaim,
4d643d1d 10161 .establish_clid = nfs41_init_clientid,
fce5c838 10162 .reclaim_complete = nfs41_proc_reclaim_complete,
05f4c350 10163 .detect_trunking = nfs41_discover_server_trunking,
591d71cb
AA
10164};
10165#endif /* CONFIG_NFS_V4_1 */
10166
17280175 10167static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
591d71cb
AA
10168 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10169 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
4dfd4f7a 10170 .recover_open = nfs40_open_expired,
591d71cb
AA
10171 .recover_lock = nfs4_lock_expired,
10172 .establish_clid = nfs4_init_clientid,
10173};
10174
10175#if defined(CONFIG_NFS_V4_1)
17280175 10176static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
7eff03ae 10177 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
b79a4a1b 10178 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
f062eb6c
BS
10179 .recover_open = nfs41_open_expired,
10180 .recover_lock = nfs41_lock_expired,
4d643d1d 10181 .establish_clid = nfs41_init_clientid,
1da177e4 10182};
591d71cb 10183#endif /* CONFIG_NFS_V4_1 */
1da177e4 10184
17280175 10185static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
29fba38b 10186 .sched_state_renewal = nfs4_proc_async_renew,
f15e1e8b 10187 .get_state_renewal_cred = nfs4_get_renew_cred,
8e69514f 10188 .renew_lease = nfs4_proc_renew,
29fba38b
BH
10189};
10190
10191#if defined(CONFIG_NFS_V4_1)
17280175 10192static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
29fba38b 10193 .sched_state_renewal = nfs41_proc_async_sequence,
f15e1e8b 10194 .get_state_renewal_cred = nfs4_get_machine_cred,
8e69514f 10195 .renew_lease = nfs4_proc_sequence,
29fba38b
BH
10196};
10197#endif
10198
ec011fe8 10199static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
b03d735b 10200 .get_locations = _nfs40_proc_get_locations,
44c99933 10201 .fsid_present = _nfs40_proc_fsid_present,
ec011fe8
CL
10202};
10203
10204#if defined(CONFIG_NFS_V4_1)
10205static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
b03d735b 10206 .get_locations = _nfs41_proc_get_locations,
44c99933 10207 .fsid_present = _nfs41_proc_fsid_present,
ec011fe8
CL
10208};
10209#endif /* CONFIG_NFS_V4_1 */
10210
97dc1359
TM
10211static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10212 .minor_version = 0,
39c6daae
TM
10213 .init_caps = NFS_CAP_READDIRPLUS
10214 | NFS_CAP_ATOMIC_OPEN
39c6daae 10215 | NFS_CAP_POSIX_LOCK,
abf79bb3
CL
10216 .init_client = nfs40_init_client,
10217 .shutdown_client = nfs40_shutdown_client,
36281caa 10218 .match_stateid = nfs4_match_stateid,
fca78d6d 10219 .find_root_sec = nfs4_find_root_sec,
c8b2d0bf 10220 .free_lock_state = nfs4_release_lockowner,
45870d69 10221 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
63f5f796 10222 .alloc_seqid = nfs_alloc_seqid,
9915ea7e 10223 .call_sync_ops = &nfs40_call_sync_ops,
c48f4f35
TM
10224 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10225 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10226 .state_renewal_ops = &nfs40_state_renewal_ops,
ec011fe8 10227 .mig_recovery_ops = &nfs40_mig_recovery_ops,
97dc1359
TM
10228};
10229
10230#if defined(CONFIG_NFS_V4_1)
63f5f796
TM
10231static struct nfs_seqid *
10232nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10233{
10234 return NULL;
10235}
10236
97dc1359
TM
10237static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10238 .minor_version = 1,
39c6daae
TM
10239 .init_caps = NFS_CAP_READDIRPLUS
10240 | NFS_CAP_ATOMIC_OPEN
3b66486c 10241 | NFS_CAP_POSIX_LOCK
49f9a0fa 10242 | NFS_CAP_STATEID_NFSV41
6e01260c
FI
10243 | NFS_CAP_ATOMIC_OPEN_V1
10244 | NFS_CAP_LGOPEN,
abf79bb3
CL
10245 .init_client = nfs41_init_client,
10246 .shutdown_client = nfs41_shutdown_client,
36281caa 10247 .match_stateid = nfs41_match_stateid,
fca78d6d 10248 .find_root_sec = nfs41_find_root_sec,
c8b2d0bf 10249 .free_lock_state = nfs41_free_lock_state,
45870d69 10250 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
63f5f796 10251 .alloc_seqid = nfs_alloc_no_seqid,
04fa2c6b 10252 .session_trunk = nfs4_test_session_trunk,
9915ea7e 10253 .call_sync_ops = &nfs41_call_sync_ops,
c48f4f35
TM
10254 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10255 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10256 .state_renewal_ops = &nfs41_state_renewal_ops,
ec011fe8 10257 .mig_recovery_ops = &nfs41_mig_recovery_ops,
97dc1359
TM
10258};
10259#endif
10260
42c2c424
SD
10261#if defined(CONFIG_NFS_V4_2)
10262static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10263 .minor_version = 2,
7017310a
BS
10264 .init_caps = NFS_CAP_READDIRPLUS
10265 | NFS_CAP_ATOMIC_OPEN
7017310a
BS
10266 | NFS_CAP_POSIX_LOCK
10267 | NFS_CAP_STATEID_NFSV41
e983120e 10268 | NFS_CAP_ATOMIC_OPEN_V1
6e01260c 10269 | NFS_CAP_LGOPEN
f4ac1674 10270 | NFS_CAP_ALLOCATE
2e72448b 10271 | NFS_CAP_COPY
cb95deea 10272 | NFS_CAP_OFFLOAD_CANCEL
0491567b 10273 | NFS_CAP_COPY_NOTIFY
624bd5b7 10274 | NFS_CAP_DEALLOCATE
6c5a0d89 10275 | NFS_CAP_SEEK
e5341f3a 10276 | NFS_CAP_LAYOUTSTATS
3eb86093 10277 | NFS_CAP_CLONE
c5675526
AS
10278 | NFS_CAP_LAYOUTERROR
10279 | NFS_CAP_READ_PLUS,
abf79bb3
CL
10280 .init_client = nfs41_init_client,
10281 .shutdown_client = nfs41_shutdown_client,
42c2c424
SD
10282 .match_stateid = nfs41_match_stateid,
10283 .find_root_sec = nfs41_find_root_sec,
7017310a 10284 .free_lock_state = nfs41_free_lock_state,
9915ea7e 10285 .call_sync_ops = &nfs41_call_sync_ops,
45870d69 10286 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
63f5f796 10287 .alloc_seqid = nfs_alloc_no_seqid,
04fa2c6b 10288 .session_trunk = nfs4_test_session_trunk,
42c2c424
SD
10289 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10290 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10291 .state_renewal_ops = &nfs41_state_renewal_ops,
18e3b739 10292 .mig_recovery_ops = &nfs41_mig_recovery_ops,
42c2c424
SD
10293};
10294#endif
10295
97dc1359
TM
10296const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10297 [0] = &nfs_v4_0_minor_ops,
10298#if defined(CONFIG_NFS_V4_1)
10299 [1] = &nfs_v4_1_minor_ops,
10300#endif
42c2c424
SD
10301#if defined(CONFIG_NFS_V4_2)
10302 [2] = &nfs_v4_2_minor_ops,
10303#endif
97dc1359
TM
10304};
10305
13997823 10306static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
c4803c49 10307{
012a211a 10308 ssize_t error, error2, error3;
c4803c49
AG
10309
10310 error = generic_listxattr(dentry, list, size);
10311 if (error < 0)
10312 return error;
10313 if (list) {
10314 list += error;
10315 size -= error;
10316 }
10317
10318 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10319 if (error2 < 0)
10320 return error2;
012a211a
FL
10321
10322 if (list) {
10323 list += error2;
10324 size -= error2;
10325 }
10326
10327 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10328 if (error3 < 0)
10329 return error3;
10330
10331 return error + error2 + error3;
c4803c49
AG
10332}
10333
17f26b12 10334static const struct inode_operations nfs4_dir_inode_operations = {
73a79706
BS
10335 .create = nfs_create,
10336 .lookup = nfs_lookup,
10337 .atomic_open = nfs_atomic_open,
10338 .link = nfs_link,
10339 .unlink = nfs_unlink,
10340 .symlink = nfs_symlink,
10341 .mkdir = nfs_mkdir,
10342 .rmdir = nfs_rmdir,
10343 .mknod = nfs_mknod,
10344 .rename = nfs_rename,
10345 .permission = nfs_permission,
10346 .getattr = nfs_getattr,
10347 .setattr = nfs_setattr,
c4803c49 10348 .listxattr = nfs4_listxattr,
73a79706
BS
10349};
10350
92e1d5be 10351static const struct inode_operations nfs4_file_inode_operations = {
6b3b5496
BF
10352 .permission = nfs_permission,
10353 .getattr = nfs_getattr,
10354 .setattr = nfs_setattr,
c4803c49 10355 .listxattr = nfs4_listxattr,
6b3b5496
BF
10356};
10357
509de811 10358const struct nfs_rpc_ops nfs_v4_clientops = {
1da177e4
LT
10359 .version = 4, /* protocol version */
10360 .dentry_ops = &nfs4_dentry_operations,
10361 .dir_inode_ops = &nfs4_dir_inode_operations,
6b3b5496 10362 .file_inode_ops = &nfs4_file_inode_operations,
1788ea6e 10363 .file_ops = &nfs4_file_operations,
1da177e4 10364 .getroot = nfs4_proc_get_root,
281cad46 10365 .submount = nfs4_submount,
f2aedb71 10366 .try_get_tree = nfs4_try_get_tree,
1da177e4
LT
10367 .getattr = nfs4_proc_getattr,
10368 .setattr = nfs4_proc_setattr,
10369 .lookup = nfs4_proc_lookup,
5b5faaf6 10370 .lookupp = nfs4_proc_lookupp,
1da177e4
LT
10371 .access = nfs4_proc_access,
10372 .readlink = nfs4_proc_readlink,
1da177e4
LT
10373 .create = nfs4_proc_create,
10374 .remove = nfs4_proc_remove,
10375 .unlink_setup = nfs4_proc_unlink_setup,
34e137cc 10376 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
1da177e4 10377 .unlink_done = nfs4_proc_unlink_done,
d3d4152a 10378 .rename_setup = nfs4_proc_rename_setup,
c6bfa1a1 10379 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
d3d4152a 10380 .rename_done = nfs4_proc_rename_done,
1da177e4
LT
10381 .link = nfs4_proc_link,
10382 .symlink = nfs4_proc_symlink,
10383 .mkdir = nfs4_proc_mkdir,
912678db 10384 .rmdir = nfs4_proc_rmdir,
1da177e4
LT
10385 .readdir = nfs4_proc_readdir,
10386 .mknod = nfs4_proc_mknod,
10387 .statfs = nfs4_proc_statfs,
10388 .fsinfo = nfs4_proc_fsinfo,
10389 .pathconf = nfs4_proc_pathconf,
e9326dca 10390 .set_capabilities = nfs4_server_capabilities,
1da177e4 10391 .decode_dirent = nfs4_decode_dirent,
a4cdda59 10392 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
1da177e4 10393 .read_setup = nfs4_proc_read_setup,
ec06c096 10394 .read_done = nfs4_read_done,
1da177e4 10395 .write_setup = nfs4_proc_write_setup,
788e7a89 10396 .write_done = nfs4_write_done,
1da177e4 10397 .commit_setup = nfs4_proc_commit_setup,
0b7c0153 10398 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
788e7a89 10399 .commit_done = nfs4_commit_done,
1da177e4 10400 .lock = nfs4_proc_lock,
e50a1c2e 10401 .clear_acl_cache = nfs4_zap_acl_attr,
7fe5c398 10402 .close_context = nfs4_close_context,
2b484297 10403 .open_context = nfs4_atomic_open,
011e2a7f 10404 .have_delegation = nfs4_have_delegation,
6663ee7f 10405 .alloc_client = nfs4_alloc_client,
45a52a02 10406 .init_client = nfs4_init_client,
cdb7eced 10407 .free_client = nfs4_free_client,
1179acc6
BS
10408 .create_server = nfs4_create_server,
10409 .clone_server = nfs_clone_server,
1da177e4
LT
10410};
10411
64c2ce8b 10412static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
98e9cb57 10413 .name = XATTR_NAME_NFSV4_ACL,
64c2ce8b
AK
10414 .list = nfs4_xattr_list_nfs4_acl,
10415 .get = nfs4_xattr_get_nfs4_acl,
10416 .set = nfs4_xattr_set_nfs4_acl,
10417};
10418
012a211a
FL
10419#ifdef CONFIG_NFS_V4_2
10420static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10421 .prefix = XATTR_USER_PREFIX,
10422 .get = nfs4_xattr_get_nfs4_user,
10423 .set = nfs4_xattr_set_nfs4_user,
10424};
10425#endif
10426
64c2ce8b
AK
10427const struct xattr_handler *nfs4_xattr_handlers[] = {
10428 &nfs4_xattr_nfs4_acl_handler,
c9bccef6
DQ
10429#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10430 &nfs4_xattr_nfs4_label_handler,
012a211a
FL
10431#endif
10432#ifdef CONFIG_NFS_V4_2
10433 &nfs4_xattr_nfs4_user_handler,
c9bccef6 10434#endif
64c2ce8b
AK
10435 NULL
10436};
10437
1da177e4
LT
10438/*
10439 * Local variables:
10440 * c-basic-offset: 8
10441 * End:
10442 */