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