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