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