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