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