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