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