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