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