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