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