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