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