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