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