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