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