]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/nfs/nfs4proc.c
nfs4: fix missing-braces warning
[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 1453static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
4a1e2feb 1454 nfs4_stateid *arg_stateid,
226056c5 1455 nfs4_stateid *stateid, fmode_t fmode)
003707c7 1456{
226056c5
TM
1457 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1458 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1459 case FMODE_WRITE:
1460 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1461 break;
1462 case FMODE_READ:
1463 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1464 break;
1465 case 0:
1466 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1467 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1468 clear_bit(NFS_OPEN_STATE, &state->flags);
1469 }
1470 if (stateid == NULL)
1471 return;
f95549cf 1472 /* Handle races with OPEN */
4a1e2feb
TM
1473 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1474 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1475 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
f95549cf 1476 nfs_resync_open_stateid_locked(state);
226056c5 1477 return;
f95549cf 1478 }
003707c7 1479 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
f597c537
TM
1480 nfs4_stateid_copy(&state->stateid, stateid);
1481 nfs4_stateid_copy(&state->open_stateid, stateid);
226056c5
TM
1482}
1483
4a1e2feb
TM
1484static void nfs_clear_open_stateid(struct nfs4_state *state,
1485 nfs4_stateid *arg_stateid,
1486 nfs4_stateid *stateid, fmode_t fmode)
226056c5
TM
1487{
1488 write_seqlock(&state->seqlock);
4a1e2feb 1489 nfs_clear_open_stateid_locked(state, arg_stateid, 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
5d422301
TM
3125 if (!nfs4_valid_open_stateid(state))
3126 call_close = 0;
4cecb76f 3127 spin_unlock(&state->owner->so_lock);
88069f77
TM
3128
3129 if (!call_close) {
963d8fe5 3130 /* Note: exit _without_ calling nfs4_close_done */
c8da19b9 3131 goto out_no_action;
9512135d 3132 }
88069f77 3133
500d701f
PT
3134 if (nfs4_wait_on_layoutreturn(inode, task)) {
3135 nfs_release_seqid(calldata->arg.seqid);
3136 goto out_wait;
3137 }
3138
3c13cb5b 3139 if (calldata->arg.fmode == 0)
88069f77 3140 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3c13cb5b
TM
3141 if (calldata->roc)
3142 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3143
6ae37339
TM
3144 calldata->arg.share_access =
3145 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3146 calldata->arg.fmode, 0);
88069f77 3147
516a6af6 3148 nfs_fattr_init(calldata->res.fattr);
26e976a8 3149 calldata->timestamp = jiffies;
7fdab069 3150 if (nfs4_setup_sequence(NFS_SERVER(inode),
9d12b216
TM
3151 &calldata->arg.seq_args,
3152 &calldata->res.seq_res,
2240a9e2
TM
3153 task) != 0)
3154 nfs_release_seqid(calldata->arg.seqid);
a3ca5651 3155 dprintk("%s: done!\n", __func__);
c8da19b9
TM
3156 return;
3157out_no_action:
3158 task->tk_action = NULL;
3159out_wait:
3160 nfs4_sequence_done(task, &calldata->res.seq_res);
1da177e4
LT
3161}
3162
963d8fe5 3163static const struct rpc_call_ops nfs4_close_ops = {
4ce70ada 3164 .rpc_call_prepare = nfs4_close_prepare,
963d8fe5
TM
3165 .rpc_call_done = nfs4_close_done,
3166 .rpc_release = nfs4_free_closedata,
3167};
3168
fe08c546
PT
3169static bool nfs4_roc(struct inode *inode)
3170{
40dd4b7a 3171 if (!nfs_have_layout(inode))
fe08c546 3172 return false;
fe08c546
PT
3173 return pnfs_roc(inode);
3174}
3175
1da177e4
LT
3176/*
3177 * It is possible for data to be read/written from a mem-mapped file
3178 * after the sys_close call (which hits the vfs layer as a flush).
3179 * This means that we can't safely call nfsv4 close on a file until
3180 * the inode is cleared. This in turn means that we are not good
3181 * NFSv4 citizens - we do not indicate to the server to update the file's
3182 * share state even when we are done with one of the three share
3183 * stateid's in the inode.
3184 *
3185 * NOTE: Caller must be holding the sp->so_owner semaphore!
3186 */
1f7977c1 3187int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
1da177e4 3188{
4a35bd41 3189 struct nfs_server *server = NFS_SERVER(state->inode);
63f5f796 3190 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1da177e4 3191 struct nfs4_closedata *calldata;
b39e625b
TM
3192 struct nfs4_state_owner *sp = state->owner;
3193 struct rpc_task *task;
5138fde0
TM
3194 struct rpc_message msg = {
3195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3196 .rpc_cred = state->owner->so_cred,
3197 };
c970aa85
TM
3198 struct rpc_task_setup task_setup_data = {
3199 .rpc_client = server->client,
5138fde0 3200 .rpc_message = &msg,
c970aa85 3201 .callback_ops = &nfs4_close_ops,
101070ca 3202 .workqueue = nfsiod_workqueue,
c970aa85
TM
3203 .flags = RPC_TASK_ASYNC,
3204 };
9512135d 3205 int status = -ENOMEM;
1da177e4 3206
fa940720
WAA
3207 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3208 &task_setup_data.rpc_client, &msg);
3209
8535b2be 3210 calldata = kzalloc(sizeof(*calldata), gfp_mask);
1da177e4 3211 if (calldata == NULL)
9512135d 3212 goto out;
a9c92d6b 3213 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
4a35bd41 3214 calldata->inode = state->inode;
1da177e4 3215 calldata->state = state;
4a35bd41 3216 calldata->arg.fh = NFS_FH(state->inode);
1da177e4 3217 /* Serialization for the sequence id */
63f5f796
TM
3218 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3219 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
badc76dd 3220 if (IS_ERR(calldata->arg.seqid))
9512135d 3221 goto out_free_calldata;
dc0b027d 3222 calldata->arg.fmode = 0;
a65318bf 3223 calldata->arg.bitmask = server->cache_consistency_bitmask;
516a6af6 3224 calldata->res.fattr = &calldata->fattr;
c1d51931 3225 calldata->res.seqid = calldata->arg.seqid;
516a6af6 3226 calldata->res.server = server;
fe08c546 3227 calldata->roc = nfs4_roc(state->inode);
643168c2 3228 nfs_sb_active(calldata->inode->i_sb);
9512135d 3229
1174dd1f
TM
3230 msg.rpc_argp = &calldata->arg;
3231 msg.rpc_resp = &calldata->res;
c970aa85
TM
3232 task_setup_data.callback_data = calldata;
3233 task = rpc_run_task(&task_setup_data);
b39e625b
TM
3234 if (IS_ERR(task))
3235 return PTR_ERR(task);
a49c3c77
TM
3236 status = 0;
3237 if (wait)
3238 status = rpc_wait_for_completion_task(task);
b39e625b 3239 rpc_put_task(task);
a49c3c77 3240 return status;
9512135d
TM
3241out_free_calldata:
3242 kfree(calldata);
3243out:
b39e625b
TM
3244 nfs4_put_open_state(state);
3245 nfs4_put_state_owner(sp);
9512135d 3246 return status;
1da177e4
LT
3247}
3248
2b484297 3249static struct inode *
5bc2afc2
TM
3250nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3251 int open_flags, struct iattr *attr, int *opened)
1da177e4 3252{
1da177e4 3253 struct nfs4_state *state;
aa9c2669
DQ
3254 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3255
3256 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
1da177e4 3257
565277f6 3258 /* Protect against concurrent sillydeletes */
5bc2afc2 3259 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
aa9c2669
DQ
3260
3261 nfs4_label_release_security(label);
3262
f46e0bd3
TM
3263 if (IS_ERR(state))
3264 return ERR_CAST(state);
275bb307 3265 return state->inode;
1da177e4
LT
3266}
3267
1185a552 3268static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
7fe5c398
TM
3269{
3270 if (ctx->state == NULL)
3271 return;
3272 if (is_sync)
643168c2 3273 nfs4_close_sync(ctx->state, ctx->mode);
7fe5c398 3274 else
643168c2 3275 nfs4_close_state(ctx->state, ctx->mode);
7fe5c398 3276}
1da177e4 3277
b944dba3
TM
3278#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3279#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
999e5683 3280#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
b944dba3 3281
1da177e4
LT
3282static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3283{
8c61282f 3284 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
43652ad5
BH
3285 struct nfs4_server_caps_arg args = {
3286 .fhandle = fhandle,
8c61282f 3287 .bitmask = bitmask,
43652ad5 3288 };
1da177e4
LT
3289 struct nfs4_server_caps_res res = {};
3290 struct rpc_message msg = {
3291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
43652ad5 3292 .rpc_argp = &args,
1da177e4
LT
3293 .rpc_resp = &res,
3294 };
3295 int status;
3296
8c61282f
KM
3297 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3298 FATTR4_WORD0_FH_EXPIRE_TYPE |
3299 FATTR4_WORD0_LINK_SUPPORT |
3300 FATTR4_WORD0_SYMLINK_SUPPORT |
3301 FATTR4_WORD0_ACLSUPPORT;
3302 if (minorversion)
3303 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3304
7c513058 3305 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4 3306 if (status == 0) {
b944dba3 3307 /* Sanity check the server answers */
8c61282f 3308 switch (minorversion) {
b944dba3
TM
3309 case 0:
3310 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3311 res.attr_bitmask[2] = 0;
3312 break;
3313 case 1:
3314 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3315 break;
3316 case 2:
3317 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3318 }
1da177e4 3319 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
62ab460c
TM
3320 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3321 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3322 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3323 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
b944dba3
TM
3324 NFS_CAP_CTIME|NFS_CAP_MTIME|
3325 NFS_CAP_SECURITY_LABEL);
7dd7d959
MN
3326 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3327 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
1da177e4
LT
3328 server->caps |= NFS_CAP_ACLS;
3329 if (res.has_links != 0)
3330 server->caps |= NFS_CAP_HARDLINKS;
3331 if (res.has_symlinks != 0)
3332 server->caps |= NFS_CAP_SYMLINKS;
62ab460c
TM
3333 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3334 server->caps |= NFS_CAP_FILEID;
3335 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3336 server->caps |= NFS_CAP_MODE;
3337 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3338 server->caps |= NFS_CAP_NLINK;
3339 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3340 server->caps |= NFS_CAP_OWNER;
3341 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3342 server->caps |= NFS_CAP_OWNER_GROUP;
3343 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3344 server->caps |= NFS_CAP_ATIME;
3345 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3346 server->caps |= NFS_CAP_CTIME;
3347 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3348 server->caps |= NFS_CAP_MTIME;
aa9c2669
DQ
3349#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3350 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3351 server->caps |= NFS_CAP_SECURITY_LABEL;
3352#endif
3353 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3354 sizeof(server->attr_bitmask));
b944dba3 3355 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
62ab460c 3356
a65318bf
TM
3357 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3358 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3359 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
b944dba3 3360 server->cache_consistency_bitmask[2] = 0;
8c61282f
KM
3361 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3362 sizeof(server->exclcreat_bitmask));
1da177e4 3363 server->acl_bitmask = res.acl_bitmask;
264e6351 3364 server->fh_expire_type = res.fh_expire_type;
1da177e4 3365 }
cccef3b9 3366
1da177e4
LT
3367 return status;
3368}
3369
55a97593 3370int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1da177e4
LT
3371{
3372 struct nfs4_exception exception = { };
3373 int err;
3374 do {
3375 err = nfs4_handle_exception(server,
3376 _nfs4_server_capabilities(server, fhandle),
3377 &exception);
3378 } while (exception.retry);
3379 return err;
3380}
3381
3382static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3383 struct nfs_fsinfo *info)
3384{
aa9c2669 3385 u32 bitmask[3];
1da177e4 3386 struct nfs4_lookup_root_arg args = {
aa9c2669 3387 .bitmask = bitmask,
1da177e4
LT
3388 };
3389 struct nfs4_lookup_res res = {
3390 .server = server,
0e574af1 3391 .fattr = info->fattr,
1da177e4
LT
3392 .fh = fhandle,
3393 };
3394 struct rpc_message msg = {
3395 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3396 .rpc_argp = &args,
3397 .rpc_resp = &res,
3398 };
008f55d0 3399
aa9c2669
DQ
3400 bitmask[0] = nfs4_fattr_bitmap[0];
3401 bitmask[1] = nfs4_fattr_bitmap[1];
3402 /*
3403 * Process the label in the upcoming getfattr
3404 */
3405 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3406
0e574af1 3407 nfs_fattr_init(info->fattr);
7c513058 3408 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
3409}
3410
3411static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3412 struct nfs_fsinfo *info)
3413{
3414 struct nfs4_exception exception = { };
3415 int err;
3416 do {
fb8a5ba8 3417 err = _nfs4_lookup_root(server, fhandle, info);
b5f875a9 3418 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
fb8a5ba8
BS
3419 switch (err) {
3420 case 0:
3421 case -NFS4ERR_WRONGSEC:
05e9cfb4 3422 goto out;
fb8a5ba8
BS
3423 default:
3424 err = nfs4_handle_exception(server, err, &exception);
3425 }
1da177e4 3426 } while (exception.retry);
05e9cfb4 3427out:
1da177e4
LT
3428 return err;
3429}
3430
8f70e95f
BS
3431static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3432 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3433{
c2190661
TM
3434 struct rpc_auth_create_args auth_args = {
3435 .pseudoflavor = flavor,
3436 };
8f70e95f
BS
3437 struct rpc_auth *auth;
3438 int ret;
3439
c2190661 3440 auth = rpcauth_create(&auth_args, server->client);
e8d920c5 3441 if (IS_ERR(auth)) {
75bc8821 3442 ret = -EACCES;
8f70e95f
BS
3443 goto out;
3444 }
3445 ret = nfs4_lookup_root(server, fhandle, info);
8f70e95f
BS
3446out:
3447 return ret;
3448}
3449
9a744ba3
CL
3450/*
3451 * Retry pseudoroot lookup with various security flavors. We do this when:
3452 *
3453 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3454 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3455 *
3456 * Returns zero on success, or a negative NFS4ERR value, or a
3457 * negative errno value.
3458 */
801a16dc 3459static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
54ceac45 3460 struct nfs_fsinfo *info)
1da177e4 3461{
9a744ba3
CL
3462 /* Per 3530bis 15.33.5 */
3463 static const rpc_authflavor_t flav_array[] = {
3464 RPC_AUTH_GSS_KRB5P,
3465 RPC_AUTH_GSS_KRB5I,
3466 RPC_AUTH_GSS_KRB5,
c4eafe11 3467 RPC_AUTH_UNIX, /* courtesy */
9a744ba3
CL
3468 RPC_AUTH_NULL,
3469 };
3470 int status = -EPERM;
3471 size_t i;
6a1a1e34 3472
4d4b69dd
WAA
3473 if (server->auth_info.flavor_len > 0) {
3474 /* try each flavor specified by user */
3475 for (i = 0; i < server->auth_info.flavor_len; i++) {
3476 status = nfs4_lookup_root_sec(server, fhandle, info,
3477 server->auth_info.flavors[i]);
3478 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3479 continue;
3480 break;
3481 }
3482 } else {
3483 /* no flavors specified by user, try default list */
3484 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3485 status = nfs4_lookup_root_sec(server, fhandle, info,
3486 flav_array[i]);
3487 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3488 continue;
3489 break;
3490 }
8f70e95f 3491 }
9a744ba3 3492
fb8a5ba8
BS
3493 /*
3494 * -EACCESS could mean that the user doesn't have correct permissions
3495 * to access the mount. It could also mean that we tried to mount
3496 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3497 * existing mount programs don't handle -EACCES very well so it should
3498 * be mapped to -EPERM instead.
3499 */
3500 if (status == -EACCES)
3501 status = -EPERM;
801a16dc
BS
3502 return status;
3503}
3504
2ed4b95b
CL
3505/**
3506 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3507 * @server: initialized nfs_server handle
3508 * @fhandle: we fill in the pseudo-fs root file handle
3509 * @info: we fill in an FSINFO struct
5e6b1990 3510 * @auth_probe: probe the auth flavours
2ed4b95b
CL
3511 *
3512 * Returns zero on success, or a negative errno.
801a16dc 3513 */
3028eb2b 3514int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
5e6b1990
TM
3515 struct nfs_fsinfo *info,
3516 bool auth_probe)
801a16dc 3517{
c7757074 3518 int status = 0;
2ed4b95b 3519
c7757074 3520 if (!auth_probe)
5e6b1990 3521 status = nfs4_lookup_root(server, fhandle, info);
c7757074
AP
3522
3523 if (auth_probe || status == NFS4ERR_WRONGSEC)
698c937b
TM
3524 status = server->nfs_client->cl_mvops->find_root_sec(server,
3525 fhandle, info);
2ed4b95b 3526
1da177e4
LT
3527 if (status == 0)
3528 status = nfs4_server_capabilities(server, fhandle);
3529 if (status == 0)
3530 status = nfs4_do_fsinfo(server, fhandle, info);
2ed4b95b 3531
c12e87f4 3532 return nfs4_map_errors(status);
1da177e4
LT
3533}
3534
bae36241
BS
3535static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3536 struct nfs_fsinfo *info)
3537{
3538 int error;
3539 struct nfs_fattr *fattr = info->fattr;
1775fd3e 3540 struct nfs4_label *label = NULL;
bae36241
BS
3541
3542 error = nfs4_server_capabilities(server, mntfh);
3543 if (error < 0) {
3544 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3545 return error;
3546 }
3547
14c43f76
DQ
3548 label = nfs4_label_alloc(server, GFP_KERNEL);
3549 if (IS_ERR(label))
3550 return PTR_ERR(label);
3551
1775fd3e 3552 error = nfs4_proc_getattr(server, mntfh, fattr, label);
bae36241
BS
3553 if (error < 0) {
3554 dprintk("nfs4_get_root: getattr error = %d\n", -error);
14c43f76 3555 goto err_free_label;
bae36241
BS
3556 }
3557
3558 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3559 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3560 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3561
14c43f76
DQ
3562err_free_label:
3563 nfs4_label_free(label);
3564
bae36241
BS
3565 return error;
3566}
3567
6b97fd3d
MN
3568/*
3569 * Get locations and (maybe) other attributes of a referral.
3570 * Note that we'll actually follow the referral later when
3571 * we detect fsid mismatch in inode revalidation
3572 */
f05d147f
BS
3573static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3574 const struct qstr *name, struct nfs_fattr *fattr,
3575 struct nfs_fh *fhandle)
6b97fd3d
MN
3576{
3577 int status = -ENOMEM;
3578 struct page *page = NULL;
3579 struct nfs4_fs_locations *locations = NULL;
6b97fd3d
MN
3580
3581 page = alloc_page(GFP_KERNEL);
3582 if (page == NULL)
3583 goto out;
3584 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3585 if (locations == NULL)
3586 goto out;
3587
f05d147f 3588 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
6b97fd3d
MN
3589 if (status != 0)
3590 goto out;
519ae255
CL
3591
3592 /*
3593 * If the fsid didn't change, this is a migration event, not a
3594 * referral. Cause us to drop into the exception handler, which
3595 * will kick off migration recovery.
3596 */
6b97fd3d 3597 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
533eb461
AA
3598 dprintk("%s: server did not return a different fsid for"
3599 " a referral at %s\n", __func__, name->name);
519ae255 3600 status = -NFS4ERR_MOVED;
6b97fd3d
MN
3601 goto out;
3602 }
533eb461
AA
3603 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3604 nfs_fixup_referral_attributes(&locations->fattr);
6b97fd3d 3605
533eb461 3606 /* replace the lookup nfs_fattr with the locations nfs_fattr */
6b97fd3d 3607 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
6b97fd3d
MN
3608 memset(fhandle, 0, sizeof(struct nfs_fh));
3609out:
3610 if (page)
3611 __free_page(page);
5d7ca35a 3612 kfree(locations);
6b97fd3d
MN
3613 return status;
3614}
3615
1775fd3e
DQ
3616static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3617 struct nfs_fattr *fattr, struct nfs4_label *label)
1da177e4
LT
3618{
3619 struct nfs4_getattr_arg args = {
3620 .fh = fhandle,
3621 .bitmask = server->attr_bitmask,
3622 };
3623 struct nfs4_getattr_res res = {
3624 .fattr = fattr,
1775fd3e 3625 .label = label,
1da177e4
LT
3626 .server = server,
3627 };
3628 struct rpc_message msg = {
3629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3630 .rpc_argp = &args,
3631 .rpc_resp = &res,
3632 };
aa9c2669
DQ
3633
3634 args.bitmask = nfs4_bitmask(server, label);
3635
0e574af1 3636 nfs_fattr_init(fattr);
7c513058 3637 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
3638}
3639
1775fd3e
DQ
3640static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3641 struct nfs_fattr *fattr, struct nfs4_label *label)
1da177e4
LT
3642{
3643 struct nfs4_exception exception = { };
3644 int err;
3645 do {
b5f875a9
TM
3646 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3647 trace_nfs4_getattr(server, fhandle, fattr, err);
3648 err = nfs4_handle_exception(server, err,
1da177e4
LT
3649 &exception);
3650 } while (exception.retry);
3651 return err;
3652}
3653
3654/*
3655 * The file is not closed if it is opened due to the a request to change
3656 * the size of the file. The open call will not be needed once the
3657 * VFS layer lookup-intents are implemented.
3658 *
3659 * Close is called when the inode is destroyed.
3660 * If we haven't opened the file for O_WRONLY, we
3661 * need to in the size_change case to obtain a stateid.
3662 *
3663 * Got race?
3664 * Because OPEN is always done by name in nfsv4, it is
3665 * possible that we opened a different file by the same
3666 * name. We can recognize this race condition, but we
3667 * can't do anything about it besides returning an error.
3668 *
3669 * This will be fixed with VFS changes (lookup-intent).
3670 */
3671static int
3672nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3673 struct iattr *sattr)
3674{
2b0143b5 3675 struct inode *inode = d_inode(dentry);
659bfcd6 3676 struct rpc_cred *cred = NULL;
d530838b 3677 struct nfs4_state *state = NULL;
14c43f76 3678 struct nfs4_label *label = NULL;
1da177e4
LT
3679 int status;
3680
88ac815c
PT
3681 if (pnfs_ld_layoutret_on_setattr(inode) &&
3682 sattr->ia_valid & ATTR_SIZE &&
3683 sattr->ia_size < i_size_read(inode))
24028672 3684 pnfs_commit_and_return_layout(inode);
8a1636c4 3685
0e574af1 3686 nfs_fattr_init(fattr);
1da177e4 3687
2669940d
AA
3688 /* Deal with open(O_TRUNC) */
3689 if (sattr->ia_valid & ATTR_OPEN)
cc7936f9 3690 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
2669940d
AA
3691
3692 /* Optimization: if the end result is no change, don't RPC */
cc7936f9 3693 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
2669940d
AA
3694 return 0;
3695
d530838b 3696 /* Search for an existing open(O_WRITE) file */
659bfcd6
TM
3697 if (sattr->ia_valid & ATTR_FILE) {
3698 struct nfs_open_context *ctx;
3699
3700 ctx = nfs_file_open_context(sattr->ia_file);
504e5189
NB
3701 if (ctx) {
3702 cred = ctx->cred;
3703 state = ctx->state;
3704 }
659bfcd6 3705 }
08e9eac4 3706
14c43f76
DQ
3707 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3708 if (IS_ERR(label))
3709 return PTR_ERR(label);
3710
3711 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
aa9c2669 3712 if (status == 0) {
f044636d 3713 nfs_setattr_update_inode(inode, sattr, fattr);
aa9c2669
DQ
3714 nfs_setsecurity(inode, fattr, label);
3715 }
14c43f76 3716 nfs4_label_free(label);
1da177e4
LT
3717 return status;
3718}
3719
0c2e53f1
TM
3720static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3721 const struct qstr *name, struct nfs_fh *fhandle,
1775fd3e 3722 struct nfs_fattr *fattr, struct nfs4_label *label)
2b3de441 3723{
0c2e53f1 3724 struct nfs_server *server = NFS_SERVER(dir);
2b3de441
DH
3725 int status;
3726 struct nfs4_lookup_arg args = {
3727 .bitmask = server->attr_bitmask,
0c2e53f1 3728 .dir_fh = NFS_FH(dir),
2b3de441
DH
3729 .name = name,
3730 };
3731 struct nfs4_lookup_res res = {
3732 .server = server,
3733 .fattr = fattr,
aa9c2669 3734 .label = label,
2b3de441
DH
3735 .fh = fhandle,
3736 };
3737 struct rpc_message msg = {
3738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3739 .rpc_argp = &args,
3740 .rpc_resp = &res,
3741 };
3742
aa9c2669
DQ
3743 args.bitmask = nfs4_bitmask(server, label);
3744
2b3de441
DH
3745 nfs_fattr_init(fattr);
3746
1da177e4 3747 dprintk("NFS call lookup %s\n", name->name);
0c2e53f1 3748 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
3749 dprintk("NFS reply lookup: %d\n", status);
3750 return status;
3751}
3752
72de53ec 3753static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
7ebb9315 3754{
7ebb9315 3755 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
72de53ec 3756 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
7ebb9315
BS
3757 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3758 fattr->nlink = 2;
3759}
3760
72de53ec 3761static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
beffb8fe 3762 const struct qstr *name, struct nfs_fh *fhandle,
1775fd3e 3763 struct nfs_fattr *fattr, struct nfs4_label *label)
1da177e4
LT
3764{
3765 struct nfs4_exception exception = { };
72de53ec 3766 struct rpc_clnt *client = *clnt;
1da177e4
LT
3767 int err;
3768 do {
1775fd3e 3769 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
078ea3df 3770 trace_nfs4_lookup(dir, name, err);
72de53ec 3771 switch (err) {
08ef7bd3 3772 case -NFS4ERR_BADNAME:
72de53ec
BS
3773 err = -ENOENT;
3774 goto out;
0c2e53f1 3775 case -NFS4ERR_MOVED:
f05d147f 3776 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
c86c90c6
DM
3777 if (err == -NFS4ERR_MOVED)
3778 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
72de53ec 3779 goto out;
0c2e53f1 3780 case -NFS4ERR_WRONGSEC:
72de53ec
BS
3781 err = -EPERM;
3782 if (client != *clnt)
3783 goto out;
66b06860 3784 client = nfs4_negotiate_security(client, dir, name);
72de53ec
BS
3785 if (IS_ERR(client))
3786 return PTR_ERR(client);
3787
3788 exception.retry = 1;
3789 break;
3790 default:
3791 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
0c2e53f1 3792 }
1da177e4 3793 } while (exception.retry);
72de53ec
BS
3794
3795out:
3796 if (err == 0)
3797 *clnt = client;
3798 else if (client != *clnt)
3799 rpc_shutdown_client(client);
3800
1da177e4
LT
3801 return err;
3802}
3803
beffb8fe 3804static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
1775fd3e
DQ
3805 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3806 struct nfs4_label *label)
72de53ec
BS
3807{
3808 int status;
3809 struct rpc_clnt *client = NFS_CLIENT(dir);
3810
1775fd3e 3811 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
72de53ec
BS
3812 if (client != NFS_CLIENT(dir)) {
3813 rpc_shutdown_client(client);
3814 nfs_fixup_secinfo_attributes(fattr);
3815 }
3816 return status;
3817}
3818
f05d147f 3819struct rpc_clnt *
beffb8fe 3820nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
f05d147f
BS
3821 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3822{
b72888cb 3823 struct rpc_clnt *client = NFS_CLIENT(dir);
f05d147f 3824 int status;
f05d147f 3825
1775fd3e 3826 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
b72888cb 3827 if (status < 0)
f05d147f 3828 return ERR_PTR(status);
b72888cb 3829 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
f05d147f
BS
3830}
3831
1da177e4
LT
3832static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3833{
76b32999 3834 struct nfs_server *server = NFS_SERVER(inode);
1da177e4
LT
3835 struct nfs4_accessargs args = {
3836 .fh = NFS_FH(inode),
a4980e78 3837 .bitmask = server->cache_consistency_bitmask,
76b32999
TM
3838 };
3839 struct nfs4_accessres res = {
3840 .server = server,
1da177e4 3841 };
1da177e4
LT
3842 struct rpc_message msg = {
3843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3844 .rpc_argp = &args,
3845 .rpc_resp = &res,
3846 .rpc_cred = entry->cred,
3847 };
3848 int mode = entry->mask;
aa9c2669 3849 int status = 0;
1da177e4
LT
3850
3851 /*
3852 * Determine which access bits we want to ask for...
3853 */
3854 if (mode & MAY_READ)
3855 args.access |= NFS4_ACCESS_READ;
3856 if (S_ISDIR(inode->i_mode)) {
3857 if (mode & MAY_WRITE)
3858 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3859 if (mode & MAY_EXEC)
3860 args.access |= NFS4_ACCESS_LOOKUP;
3861 } else {
3862 if (mode & MAY_WRITE)
3863 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3864 if (mode & MAY_EXEC)
3865 args.access |= NFS4_ACCESS_EXECUTE;
3866 }
c407d41a
TM
3867
3868 res.fattr = nfs_alloc_fattr();
3869 if (res.fattr == NULL)
3870 return -ENOMEM;
3871
7c513058 3872 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4 3873 if (!status) {
6168f62c 3874 nfs_access_set_mask(entry, res.access);
c407d41a 3875 nfs_refresh_inode(inode, res.fattr);
1da177e4 3876 }
c407d41a 3877 nfs_free_fattr(res.fattr);
1da177e4
LT
3878 return status;
3879}
3880
3881static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3882{
3883 struct nfs4_exception exception = { };
3884 int err;
3885 do {
c1578b76
TM
3886 err = _nfs4_proc_access(inode, entry);
3887 trace_nfs4_access(inode, err);
3888 err = nfs4_handle_exception(NFS_SERVER(inode), err,
1da177e4
LT
3889 &exception);
3890 } while (exception.retry);
3891 return err;
3892}
3893
3894/*
3895 * TODO: For the time being, we don't try to get any attributes
3896 * along with any of the zero-copy operations READ, READDIR,
3897 * READLINK, WRITE.
3898 *
3899 * In the case of the first three, we want to put the GETATTR
3900 * after the read-type operation -- this is because it is hard
3901 * to predict the length of a GETATTR response in v4, and thus
3902 * align the READ data correctly. This means that the GETATTR
3903 * may end up partially falling into the page cache, and we should
3904 * shift it into the 'tail' of the xdr_buf before processing.
3905 * To do this efficiently, we need to know the total length
3906 * of data received, which doesn't seem to be available outside
3907 * of the RPC layer.
3908 *
3909 * In the case of WRITE, we also want to put the GETATTR after
3910 * the operation -- in this case because we want to make sure
140150db 3911 * we get the post-operation mtime and size.
1da177e4
LT
3912 *
3913 * Both of these changes to the XDR layer would in fact be quite
3914 * minor, but I decided to leave them for a subsequent patch.
3915 */
3916static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3917 unsigned int pgbase, unsigned int pglen)
3918{
3919 struct nfs4_readlink args = {
3920 .fh = NFS_FH(inode),
3921 .pgbase = pgbase,
3922 .pglen = pglen,
3923 .pages = &page,
3924 };
f50c7000 3925 struct nfs4_readlink_res res;
1da177e4
LT
3926 struct rpc_message msg = {
3927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3928 .rpc_argp = &args,
f50c7000 3929 .rpc_resp = &res,
1da177e4
LT
3930 };
3931
7c513058 3932 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
3933}
3934
3935static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3936 unsigned int pgbase, unsigned int pglen)
3937{
3938 struct nfs4_exception exception = { };
3939 int err;
3940 do {
c1578b76
TM
3941 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3942 trace_nfs4_readlink(inode, err);
3943 err = nfs4_handle_exception(NFS_SERVER(inode), err,
1da177e4
LT
3944 &exception);
3945 } while (exception.retry);
3946 return err;
3947}
3948
1da177e4 3949/*
8867fe58 3950 * This is just for mknod. open(O_CREAT) will always do ->open_context().
1da177e4 3951 */
1da177e4
LT
3952static int
3953nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
8867fe58 3954 int flags)
1da177e4 3955{
aa9c2669 3956 struct nfs4_label l, *ilabel = NULL;
8867fe58 3957 struct nfs_open_context *ctx;
1da177e4 3958 struct nfs4_state *state;
1da177e4
LT
3959 int status = 0;
3960
8867fe58
MS
3961 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3962 if (IS_ERR(ctx))
3963 return PTR_ERR(ctx);
3964
aa9c2669
DQ
3965 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3966
a8a5da99 3967 sattr->ia_mode &= ~current_umask();
c5c3fb5f 3968 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
1da177e4
LT
3969 if (IS_ERR(state)) {
3970 status = PTR_ERR(state);
c0204fd2 3971 goto out;
1da177e4 3972 }
1da177e4 3973out:
aa9c2669 3974 nfs4_label_release_security(ilabel);
8867fe58 3975 put_nfs_open_context(ctx);
1da177e4
LT
3976 return status;
3977}
3978
beffb8fe 3979static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
1da177e4 3980{
16e42959 3981 struct nfs_server *server = NFS_SERVER(dir);
4fdc17b2 3982 struct nfs_removeargs args = {
1da177e4 3983 .fh = NFS_FH(dir),
26fe5750 3984 .name = *name,
16e42959 3985 };
4fdc17b2 3986 struct nfs_removeres res = {
16e42959 3987 .server = server,
1da177e4 3988 };
1da177e4 3989 struct rpc_message msg = {
4fdc17b2
TM
3990 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3991 .rpc_argp = &args,
3992 .rpc_resp = &res,
1da177e4 3993 };
778d2817 3994 int status;
1da177e4 3995
7c513058 3996 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
778d2817 3997 if (status == 0)
16e42959 3998 update_changeattr(dir, &res.cinfo);
1da177e4
LT
3999 return status;
4000}
4001
beffb8fe 4002static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
1da177e4
LT
4003{
4004 struct nfs4_exception exception = { };
4005 int err;
4006 do {
078ea3df
TM
4007 err = _nfs4_proc_remove(dir, name);
4008 trace_nfs4_remove(dir, name, err);
4009 err = nfs4_handle_exception(NFS_SERVER(dir), err,
1da177e4
LT
4010 &exception);
4011 } while (exception.retry);
4012 return err;
4013}
4014
e4eff1a6 4015static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
1da177e4 4016{
e4eff1a6
TM
4017 struct nfs_server *server = NFS_SERVER(dir);
4018 struct nfs_removeargs *args = msg->rpc_argp;
4019 struct nfs_removeres *res = msg->rpc_resp;
1da177e4 4020
e4eff1a6 4021 res->server = server;
1da177e4 4022 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
a9c92d6b 4023 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
aa9c2669
DQ
4024
4025 nfs_fattr_init(res->dir_attr);
1da177e4
LT
4026}
4027
34e137cc
BS
4028static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4029{
884be175 4030 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
d9afbd1b
TM
4031 &data->args.seq_args,
4032 &data->res.seq_res,
4033 task);
1da177e4
LT
4034}
4035
e4eff1a6 4036static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
1da177e4 4037{
078ea3df
TM
4038 struct nfs_unlinkdata *data = task->tk_calldata;
4039 struct nfs_removeres *res = &data->res;
e4eff1a6 4040
14516c3a
TM
4041 if (!nfs4_sequence_done(task, &res->seq_res))
4042 return 0;
8478eaa1
N
4043 if (nfs4_async_handle_error(task, res->server, NULL,
4044 &data->timeout) == -EAGAIN)
e4eff1a6
TM
4045 return 0;
4046 update_changeattr(dir, &res->cinfo);
e4eff1a6 4047 return 1;
1da177e4
LT
4048}
4049
d3d4152a
JL
4050static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4051{
4052 struct nfs_server *server = NFS_SERVER(dir);
4053 struct nfs_renameargs *arg = msg->rpc_argp;
4054 struct nfs_renameres *res = msg->rpc_resp;
4055
4056 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
d3d4152a 4057 res->server = server;
a9c92d6b 4058 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
d3d4152a
JL
4059}
4060
c6bfa1a1
BS
4061static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4062{
d9afbd1b
TM
4063 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4064 &data->args.seq_args,
4065 &data->res.seq_res,
4066 task);
d3d4152a
JL
4067}
4068
4069static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4070 struct inode *new_dir)
4071{
fbc6f7c2
TM
4072 struct nfs_renamedata *data = task->tk_calldata;
4073 struct nfs_renameres *res = &data->res;
d3d4152a
JL
4074
4075 if (!nfs4_sequence_done(task, &res->seq_res))
4076 return 0;
8478eaa1 4077 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
d3d4152a
JL
4078 return 0;
4079
4080 update_changeattr(old_dir, &res->old_cinfo);
d3d4152a 4081 update_changeattr(new_dir, &res->new_cinfo);
d3d4152a
JL
4082 return 1;
4083}
4084
beffb8fe 4085static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
1da177e4 4086{
91ba2eee 4087 struct nfs_server *server = NFS_SERVER(inode);
1da177e4
LT
4088 struct nfs4_link_arg arg = {
4089 .fh = NFS_FH(inode),
4090 .dir_fh = NFS_FH(dir),
4091 .name = name,
91ba2eee
TM
4092 .bitmask = server->attr_bitmask,
4093 };
91ba2eee
TM
4094 struct nfs4_link_res res = {
4095 .server = server,
1775fd3e 4096 .label = NULL,
1da177e4 4097 };
1da177e4
LT
4098 struct rpc_message msg = {
4099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4100 .rpc_argp = &arg,
91ba2eee 4101 .rpc_resp = &res,
1da177e4 4102 };
136f2627
TM
4103 int status = -ENOMEM;
4104
4105 res.fattr = nfs_alloc_fattr();
778d2817 4106 if (res.fattr == NULL)
136f2627 4107 goto out;
1da177e4 4108
14c43f76
DQ
4109 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4110 if (IS_ERR(res.label)) {
4111 status = PTR_ERR(res.label);
4112 goto out;
4113 }
aa9c2669 4114 arg.bitmask = nfs4_bitmask(server, res.label);
14c43f76 4115
7c513058 4116 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
91ba2eee
TM
4117 if (!status) {
4118 update_changeattr(dir, &res.cinfo);
aa9c2669
DQ
4119 status = nfs_post_op_update_inode(inode, res.fattr);
4120 if (!status)
4121 nfs_setsecurity(inode, res.fattr, res.label);
91ba2eee 4122 }
14c43f76
DQ
4123
4124
4125 nfs4_label_free(res.label);
4126
136f2627 4127out:
136f2627 4128 nfs_free_fattr(res.fattr);
1da177e4
LT
4129 return status;
4130}
4131
beffb8fe 4132static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
1da177e4
LT
4133{
4134 struct nfs4_exception exception = { };
4135 int err;
4136 do {
4137 err = nfs4_handle_exception(NFS_SERVER(inode),
4138 _nfs4_proc_link(inode, dir, name),
4139 &exception);
4140 } while (exception.retry);
4141 return err;
4142}
4143
57dc9a57
TM
4144struct nfs4_createdata {
4145 struct rpc_message msg;
4146 struct nfs4_create_arg arg;
4147 struct nfs4_create_res res;
4148 struct nfs_fh fh;
4149 struct nfs_fattr fattr;
1775fd3e 4150 struct nfs4_label *label;
57dc9a57
TM
4151};
4152
4153static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
beffb8fe 4154 const struct qstr *name, struct iattr *sattr, u32 ftype)
57dc9a57
TM
4155{
4156 struct nfs4_createdata *data;
4157
4158 data = kzalloc(sizeof(*data), GFP_KERNEL);
4159 if (data != NULL) {
4160 struct nfs_server *server = NFS_SERVER(dir);
4161
14c43f76
DQ
4162 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4163 if (IS_ERR(data->label))
4164 goto out_free;
4165
57dc9a57
TM
4166 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4167 data->msg.rpc_argp = &data->arg;
4168 data->msg.rpc_resp = &data->res;
4169 data->arg.dir_fh = NFS_FH(dir);
4170 data->arg.server = server;
4171 data->arg.name = name;
4172 data->arg.attrs = sattr;
4173 data->arg.ftype = ftype;
aa9c2669 4174 data->arg.bitmask = nfs4_bitmask(server, data->label);
57dc9a57
TM
4175 data->res.server = server;
4176 data->res.fh = &data->fh;
4177 data->res.fattr = &data->fattr;
1775fd3e 4178 data->res.label = data->label;
57dc9a57 4179 nfs_fattr_init(data->res.fattr);
57dc9a57
TM
4180 }
4181 return data;
14c43f76
DQ
4182out_free:
4183 kfree(data);
4184 return NULL;
57dc9a57
TM
4185}
4186
4187static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4188{
7c513058 4189 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
e73b83f2 4190 &data->arg.seq_args, &data->res.seq_res, 1);
57dc9a57
TM
4191 if (status == 0) {
4192 update_changeattr(dir, &data->res.dir_cinfo);
1775fd3e 4193 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
57dc9a57
TM
4194 }
4195 return status;
4196}
4197
4198static void nfs4_free_createdata(struct nfs4_createdata *data)
4199{
14c43f76 4200 nfs4_label_free(data->label);
57dc9a57
TM
4201 kfree(data);
4202}
4203
4f390c15 4204static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
1775fd3e
DQ
4205 struct page *page, unsigned int len, struct iattr *sattr,
4206 struct nfs4_label *label)
1da177e4 4207{
57dc9a57
TM
4208 struct nfs4_createdata *data;
4209 int status = -ENAMETOOLONG;
1da177e4 4210
94a6d753 4211 if (len > NFS4_MAXPATHLEN)
57dc9a57 4212 goto out;
4f390c15 4213
57dc9a57
TM
4214 status = -ENOMEM;
4215 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4216 if (data == NULL)
4217 goto out;
4218
4219 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4220 data->arg.u.symlink.pages = &page;
4221 data->arg.u.symlink.len = len;
1775fd3e 4222 data->arg.label = label;
1da177e4 4223
57dc9a57
TM
4224 status = nfs4_do_create(dir, dentry, data);
4225
4226 nfs4_free_createdata(data);
4227out:
1da177e4
LT
4228 return status;
4229}
4230
4f390c15 4231static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
94a6d753 4232 struct page *page, unsigned int len, struct iattr *sattr)
1da177e4
LT
4233{
4234 struct nfs4_exception exception = { };
aa9c2669 4235 struct nfs4_label l, *label = NULL;
1da177e4 4236 int err;
aa9c2669
DQ
4237
4238 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4239
1da177e4 4240 do {
078ea3df
TM
4241 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4242 trace_nfs4_symlink(dir, &dentry->d_name, err);
4243 err = nfs4_handle_exception(NFS_SERVER(dir), err,
1da177e4
LT
4244 &exception);
4245 } while (exception.retry);
aa9c2669
DQ
4246
4247 nfs4_label_release_security(label);
1da177e4
LT
4248 return err;
4249}
4250
4251static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1775fd3e 4252 struct iattr *sattr, struct nfs4_label *label)
1da177e4 4253{
57dc9a57
TM
4254 struct nfs4_createdata *data;
4255 int status = -ENOMEM;
1da177e4 4256
57dc9a57
TM
4257 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4258 if (data == NULL)
4259 goto out;
4260
1775fd3e 4261 data->arg.label = label;
57dc9a57
TM
4262 status = nfs4_do_create(dir, dentry, data);
4263
4264 nfs4_free_createdata(data);
4265out:
1da177e4
LT
4266 return status;
4267}
4268
4269static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4270 struct iattr *sattr)
4271{
4272 struct nfs4_exception exception = { };
aa9c2669 4273 struct nfs4_label l, *label = NULL;
1da177e4 4274 int err;
a8a5da99 4275
aa9c2669
DQ
4276 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4277
a8a5da99 4278 sattr->ia_mode &= ~current_umask();
1da177e4 4279 do {
078ea3df
TM
4280 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4281 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4282 err = nfs4_handle_exception(NFS_SERVER(dir), err,
1da177e4
LT
4283 &exception);
4284 } while (exception.retry);
aa9c2669
DQ
4285 nfs4_label_release_security(label);
4286
1da177e4
LT
4287 return err;
4288}
4289
4290static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
56e4ebf8 4291 u64 cookie, struct page **pages, unsigned int count, int plus)
1da177e4 4292{
2b0143b5 4293 struct inode *dir = d_inode(dentry);
1da177e4
LT
4294 struct nfs4_readdir_arg args = {
4295 .fh = NFS_FH(dir),
56e4ebf8 4296 .pages = pages,
1da177e4
LT
4297 .pgbase = 0,
4298 .count = count,
2b0143b5 4299 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
82f2e547 4300 .plus = plus,
1da177e4
LT
4301 };
4302 struct nfs4_readdir_res res;
4303 struct rpc_message msg = {
4304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4305 .rpc_argp = &args,
4306 .rpc_resp = &res,
4307 .rpc_cred = cred,
4308 };
4309 int status;
4310
6de1472f
AV
4311 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4312 dentry,
eadf4598 4313 (unsigned long long)cookie);
c3f52af3 4314 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
1da177e4 4315 res.pgbase = args.pgbase;
7c513058 4316 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
ac396128 4317 if (status >= 0) {
c3f52af3 4318 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
ac396128
TM
4319 status += args.pgbase;
4320 }
c4812998
TM
4321
4322 nfs_invalidate_atime(dir);
4323
3110ff80 4324 dprintk("%s: returns %d\n", __func__, status);
1da177e4
LT
4325 return status;
4326}
4327
4328static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
56e4ebf8 4329 u64 cookie, struct page **pages, unsigned int count, int plus)
1da177e4
LT
4330{
4331 struct nfs4_exception exception = { };
4332 int err;
4333 do {
c1578b76
TM
4334 err = _nfs4_proc_readdir(dentry, cred, cookie,
4335 pages, count, plus);
2b0143b5
DH
4336 trace_nfs4_readdir(d_inode(dentry), err);
4337 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
1da177e4
LT
4338 &exception);
4339 } while (exception.retry);
4340 return err;
4341}
4342
4343static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
aa9c2669 4344 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
1da177e4 4345{
57dc9a57
TM
4346 struct nfs4_createdata *data;
4347 int mode = sattr->ia_mode;
4348 int status = -ENOMEM;
1da177e4 4349
57dc9a57
TM
4350 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4351 if (data == NULL)
4352 goto out;
4353
1da177e4 4354 if (S_ISFIFO(mode))
57dc9a57 4355 data->arg.ftype = NF4FIFO;
1da177e4 4356 else if (S_ISBLK(mode)) {
57dc9a57
TM
4357 data->arg.ftype = NF4BLK;
4358 data->arg.u.device.specdata1 = MAJOR(rdev);
4359 data->arg.u.device.specdata2 = MINOR(rdev);
1da177e4
LT
4360 }
4361 else if (S_ISCHR(mode)) {
57dc9a57
TM
4362 data->arg.ftype = NF4CHR;
4363 data->arg.u.device.specdata1 = MAJOR(rdev);
4364 data->arg.u.device.specdata2 = MINOR(rdev);
4ea8fed5
TM
4365 } else if (!S_ISSOCK(mode)) {
4366 status = -EINVAL;
4367 goto out_free;
1da177e4 4368 }
1775fd3e 4369
aa9c2669 4370 data->arg.label = label;
57dc9a57 4371 status = nfs4_do_create(dir, dentry, data);
4ea8fed5 4372out_free:
57dc9a57
TM
4373 nfs4_free_createdata(data);
4374out:
1da177e4
LT
4375 return status;
4376}
4377
4378static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4379 struct iattr *sattr, dev_t rdev)
4380{
4381 struct nfs4_exception exception = { };
aa9c2669 4382 struct nfs4_label l, *label = NULL;
1da177e4 4383 int err;
a8a5da99 4384
aa9c2669
DQ
4385 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4386
a8a5da99 4387 sattr->ia_mode &= ~current_umask();
1da177e4 4388 do {
078ea3df
TM
4389 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4390 trace_nfs4_mknod(dir, &dentry->d_name, err);
4391 err = nfs4_handle_exception(NFS_SERVER(dir), err,
1da177e4
LT
4392 &exception);
4393 } while (exception.retry);
aa9c2669
DQ
4394
4395 nfs4_label_release_security(label);
4396
1da177e4
LT
4397 return err;
4398}
4399
4400static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4401 struct nfs_fsstat *fsstat)
4402{
4403 struct nfs4_statfs_arg args = {
4404 .fh = fhandle,
4405 .bitmask = server->attr_bitmask,
4406 };
24ad148a
BH
4407 struct nfs4_statfs_res res = {
4408 .fsstat = fsstat,
4409 };
1da177e4
LT
4410 struct rpc_message msg = {
4411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4412 .rpc_argp = &args,
24ad148a 4413 .rpc_resp = &res,
1da177e4
LT
4414 };
4415
0e574af1 4416 nfs_fattr_init(fsstat->fattr);
7c513058 4417 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
4418}
4419
4420static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4421{
4422 struct nfs4_exception exception = { };
4423 int err;
4424 do {
4425 err = nfs4_handle_exception(server,
4426 _nfs4_proc_statfs(server, fhandle, fsstat),
4427 &exception);
4428 } while (exception.retry);
4429 return err;
4430}
4431
4432static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4433 struct nfs_fsinfo *fsinfo)
4434{
4435 struct nfs4_fsinfo_arg args = {
4436 .fh = fhandle,
4437 .bitmask = server->attr_bitmask,
4438 };
3dda5e43
BH
4439 struct nfs4_fsinfo_res res = {
4440 .fsinfo = fsinfo,
4441 };
1da177e4
LT
4442 struct rpc_message msg = {
4443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4444 .rpc_argp = &args,
3dda5e43 4445 .rpc_resp = &res,
1da177e4
LT
4446 };
4447
7c513058 4448 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
4449}
4450
4451static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4452{
4453 struct nfs4_exception exception = { };
83ca7f5a 4454 unsigned long now = jiffies;
1da177e4
LT
4455 int err;
4456
4457 do {
83ca7f5a 4458 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
b5f875a9 4459 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
83ca7f5a 4460 if (err == 0) {
fb10fb67
TM
4461 nfs4_set_lease_period(server->nfs_client,
4462 fsinfo->lease_time * HZ,
4463 now);
83ca7f5a
CL
4464 break;
4465 }
4466 err = nfs4_handle_exception(server, err, &exception);
1da177e4
LT
4467 } while (exception.retry);
4468 return err;
4469}
4470
4471static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4472{
e38eb650
BS
4473 int error;
4474
0e574af1 4475 nfs_fattr_init(fsinfo->fattr);
e38eb650 4476 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
dc182549
PT
4477 if (error == 0) {
4478 /* block layout checks this! */
4479 server->pnfs_blksize = fsinfo->blksize;
ca440c38 4480 set_pnfs_layoutdriver(server, fhandle, fsinfo);
dc182549 4481 }
e38eb650
BS
4482
4483 return error;
1da177e4
LT
4484}
4485
4486static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4487 struct nfs_pathconf *pathconf)
4488{
4489 struct nfs4_pathconf_arg args = {
4490 .fh = fhandle,
4491 .bitmask = server->attr_bitmask,
4492 };
d45b2989
BH
4493 struct nfs4_pathconf_res res = {
4494 .pathconf = pathconf,
4495 };
1da177e4
LT
4496 struct rpc_message msg = {
4497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4498 .rpc_argp = &args,
d45b2989 4499 .rpc_resp = &res,
1da177e4
LT
4500 };
4501
4502 /* None of the pathconf attributes are mandatory to implement */
4503 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4504 memset(pathconf, 0, sizeof(*pathconf));
4505 return 0;
4506 }
4507
0e574af1 4508 nfs_fattr_init(pathconf->fattr);
7c513058 4509 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
1da177e4
LT
4510}
4511
4512static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4513 struct nfs_pathconf *pathconf)
4514{
4515 struct nfs4_exception exception = { };
4516 int err;
4517
4518 do {
4519 err = nfs4_handle_exception(server,
4520 _nfs4_proc_pathconf(server, fhandle, pathconf),
4521 &exception);
4522 } while (exception.retry);
4523 return err;
4524}
4525
5521abfd 4526int nfs4_set_rw_stateid(nfs4_stateid *stateid,
9b206149
TM
4527 const struct nfs_open_context *ctx,
4528 const struct nfs_lock_context *l_ctx,
4529 fmode_t fmode)
4530{
4531 const struct nfs_lockowner *lockowner = NULL;
4532
4533 if (l_ctx != NULL)
4534 lockowner = &l_ctx->lockowner;
abf4e13c 4535 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
9b206149
TM
4536}
4537EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4538
5521abfd
TM
4539static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4540 const struct nfs_open_context *ctx,
4541 const struct nfs_lock_context *l_ctx,
4542 fmode_t fmode)
4543{
4544 nfs4_stateid current_stateid;
4545
e1253be0
TM
4546 /* If the current stateid represents a lost lock, then exit */
4547 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4548 return true;
5521abfd
TM
4549 return nfs4_stateid_match(stateid, &current_stateid);
4550}
4551
4552static bool nfs4_error_stateid_expired(int err)
4553{
4554 switch (err) {
4555 case -NFS4ERR_DELEG_REVOKED:
4556 case -NFS4ERR_ADMIN_REVOKED:
4557 case -NFS4ERR_BAD_STATEID:
4558 case -NFS4ERR_STALE_STATEID:
4559 case -NFS4ERR_OLD_STATEID:
4560 case -NFS4ERR_OPENMODE:
4561 case -NFS4ERR_EXPIRED:
4562 return true;
4563 }
4564 return false;
4565}
4566
d45f60c6 4567static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
1da177e4 4568{
d45f60c6 4569 struct nfs_server *server = NFS_SERVER(hdr->inode);
1da177e4 4570
d45f60c6 4571 trace_nfs4_read(hdr, task->tk_status);
9c27869d
TM
4572 if (task->tk_status < 0) {
4573 struct nfs4_exception exception = {
4574 .inode = hdr->inode,
4575 .state = hdr->args.context->state,
4576 .stateid = &hdr->args.stateid,
4577 };
4578 task->tk_status = nfs4_async_handle_exception(task,
4579 server, task->tk_status, &exception);
4580 if (exception.retry) {
4581 rpc_restart_call_prepare(task);
4582 return -EAGAIN;
4583 }
1da177e4 4584 }
8850df99 4585
1da177e4 4586 if (task->tk_status > 0)
d45f60c6 4587 renew_lease(server, hdr->timestamp);
ec06c096 4588 return 0;
1da177e4
LT
4589}
4590
5521abfd 4591static bool nfs4_read_stateid_changed(struct rpc_task *task,
3c6b899c 4592 struct nfs_pgio_args *args)
5521abfd
TM
4593{
4594
4595 if (!nfs4_error_stateid_expired(task->tk_status) ||
4596 nfs4_stateid_is_current(&args->stateid,
4597 args->context,
4598 args->lock_context,
4599 FMODE_READ))
4600 return false;
4601 rpc_restart_call_prepare(task);
4602 return true;
4603}
4604
d45f60c6 4605static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
cbdabc7f
AA
4606{
4607
4608 dprintk("--> %s\n", __func__);
4609
d45f60c6 4610 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
cbdabc7f 4611 return -EAGAIN;
d45f60c6 4612 if (nfs4_read_stateid_changed(task, &hdr->args))
5521abfd 4613 return -EAGAIN;
bfc505de
TM
4614 if (task->tk_status > 0)
4615 nfs_invalidate_atime(hdr->inode);
d45f60c6
WAA
4616 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4617 nfs4_read_done_cb(task, hdr);
cbdabc7f
AA
4618}
4619
d45f60c6
WAA
4620static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4621 struct rpc_message *msg)
1da177e4 4622{
d45f60c6 4623 hdr->timestamp = jiffies;
ca857cc1
TM
4624 if (!hdr->pgio_done_cb)
4625 hdr->pgio_done_cb = nfs4_read_done_cb;
bdc7f021 4626 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
d45f60c6 4627 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
1da177e4
LT
4628}
4629
d45f60c6
WAA
4630static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4631 struct nfs_pgio_header *hdr)
ea7c3303 4632{
d45f60c6
WAA
4633 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4634 &hdr->args.seq_args,
4635 &hdr->res.seq_res,
9b206149 4636 task))
ef1820f9 4637 return 0;
d45f60c6
WAA
4638 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4639 hdr->args.lock_context,
4640 hdr->rw_ops->rw_mode) == -EIO)
ef1820f9 4641 return -EIO;
d45f60c6 4642 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
ef1820f9
N
4643 return -EIO;
4644 return 0;
1da177e4
LT
4645}
4646
d45f60c6
WAA
4647static int nfs4_write_done_cb(struct rpc_task *task,
4648 struct nfs_pgio_header *hdr)
1da177e4 4649{
d45f60c6 4650 struct inode *inode = hdr->inode;
8478eaa1 4651
d45f60c6 4652 trace_nfs4_write(hdr, task->tk_status);
9c27869d
TM
4653 if (task->tk_status < 0) {
4654 struct nfs4_exception exception = {
4655 .inode = hdr->inode,
4656 .state = hdr->args.context->state,
4657 .stateid = &hdr->args.stateid,
4658 };
4659 task->tk_status = nfs4_async_handle_exception(task,
4660 NFS_SERVER(inode), task->tk_status,
4661 &exception);
4662 if (exception.retry) {
4663 rpc_restart_call_prepare(task);
4664 return -EAGAIN;
4665 }
1da177e4 4666 }
4f9838c7 4667 if (task->tk_status >= 0) {
d45f60c6 4668 renew_lease(NFS_SERVER(inode), hdr->timestamp);
a08a8cd3 4669 nfs_writeback_update_inode(hdr);
4f9838c7 4670 }
788e7a89 4671 return 0;
1da177e4
LT
4672}
4673
5521abfd 4674static bool nfs4_write_stateid_changed(struct rpc_task *task,
3c6b899c 4675 struct nfs_pgio_args *args)
5521abfd
TM
4676{
4677
4678 if (!nfs4_error_stateid_expired(task->tk_status) ||
4679 nfs4_stateid_is_current(&args->stateid,
4680 args->context,
4681 args->lock_context,
4682 FMODE_WRITE))
4683 return false;
4684 rpc_restart_call_prepare(task);
4685 return true;
4686}
4687
d45f60c6 4688static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
b029bc9b 4689{
d45f60c6 4690 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
b029bc9b 4691 return -EAGAIN;
d45f60c6 4692 if (nfs4_write_stateid_changed(task, &hdr->args))
5521abfd 4693 return -EAGAIN;
d45f60c6
WAA
4694 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4695 nfs4_write_done_cb(task, hdr);
b029bc9b
FI
4696}
4697
5a37f851 4698static
d45f60c6 4699bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
a69aef14 4700{
5a37f851 4701 /* Don't request attributes for pNFS or O_DIRECT writes */
d45f60c6 4702 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5a37f851
TM
4703 return false;
4704 /* Otherwise, request attributes if and only if we don't hold
4705 * a delegation
4706 */
011e2a7f 4707 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
a69aef14 4708}
a69aef14 4709
d45f60c6
WAA
4710static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4711 struct rpc_message *msg)
1da177e4 4712{
d45f60c6 4713 struct nfs_server *server = NFS_SERVER(hdr->inode);
bdc7f021 4714
d45f60c6
WAA
4715 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4716 hdr->args.bitmask = NULL;
4717 hdr->res.fattr = NULL;
7ffd1064 4718 } else
d45f60c6 4719 hdr->args.bitmask = server->cache_consistency_bitmask;
5a37f851 4720
d45f60c6
WAA
4721 if (!hdr->pgio_done_cb)
4722 hdr->pgio_done_cb = nfs4_write_done_cb;
4723 hdr->res.server = server;
4724 hdr->timestamp = jiffies;
1da177e4 4725
bdc7f021 4726 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
d45f60c6 4727 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
1da177e4
LT
4728}
4729
0b7c0153 4730static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
c6cb80d0 4731{
d9afbd1b
TM
4732 nfs4_setup_sequence(NFS_SERVER(data->inode),
4733 &data->args.seq_args,
4734 &data->res.seq_res,
4735 task);
1da177e4
LT
4736}
4737
0b7c0153 4738static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
1da177e4 4739{
1da177e4 4740 struct inode *inode = data->inode;
14516c3a 4741
cc668ab3 4742 trace_nfs4_commit(data, task->tk_status);
8478eaa1
N
4743 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4744 NULL, NULL) == -EAGAIN) {
d00c5d43 4745 rpc_restart_call_prepare(task);
788e7a89 4746 return -EAGAIN;
1da177e4 4747 }
788e7a89 4748 return 0;
1da177e4
LT
4749}
4750
0b7c0153 4751static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5f452431
FI
4752{
4753 if (!nfs4_sequence_done(task, &data->res.seq_res))
4754 return -EAGAIN;
0b7c0153 4755 return data->commit_done_cb(task, data);
5f452431
FI
4756}
4757
0b7c0153 4758static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
1da177e4 4759{
788e7a89 4760 struct nfs_server *server = NFS_SERVER(data->inode);
988b6dce 4761
0b7c0153
FI
4762 if (data->commit_done_cb == NULL)
4763 data->commit_done_cb = nfs4_commit_done_cb;
4f9838c7 4764 data->res.server = server;
bdc7f021 4765 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
a9c92d6b 4766 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
1da177e4
LT
4767}
4768
9bc4e3ca
CL
4769struct nfs4_renewdata {
4770 struct nfs_client *client;
4771 unsigned long timestamp;
4772};
4773
1da177e4
LT
4774/*
4775 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4776 * standalone procedure for queueing an asynchronous RENEW.
4777 */
9bc4e3ca 4778static void nfs4_renew_release(void *calldata)
dc96aef9 4779{
9bc4e3ca
CL
4780 struct nfs4_renewdata *data = calldata;
4781 struct nfs_client *clp = data->client;
dc96aef9 4782
0851de06
AB
4783 if (atomic_read(&clp->cl_count) > 1)
4784 nfs4_schedule_state_renewal(clp);
4785 nfs_put_client(clp);
9bc4e3ca 4786 kfree(data);
dc96aef9
AB
4787}
4788
9bc4e3ca 4789static void nfs4_renew_done(struct rpc_task *task, void *calldata)
1da177e4 4790{
9bc4e3ca
CL
4791 struct nfs4_renewdata *data = calldata;
4792 struct nfs_client *clp = data->client;
4793 unsigned long timestamp = data->timestamp;
1da177e4 4794
c6d01c6f 4795 trace_nfs4_renew_async(clp, task->tk_status);
f8aba1e8
CL
4796 switch (task->tk_status) {
4797 case 0:
4798 break;
4799 case -NFS4ERR_LEASE_MOVED:
4800 nfs4_schedule_lease_moved_recovery(clp);
4801 break;
4802 default:
95baa25c 4803 /* Unless we're shutting down, schedule state recovery! */
042b60be
TM
4804 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4805 return;
4806 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
0400a6b0 4807 nfs4_schedule_lease_recovery(clp);
042b60be
TM
4808 return;
4809 }
4810 nfs4_schedule_path_down_recovery(clp);
1da177e4 4811 }
452e9352 4812 do_renew_lease(clp, timestamp);
1da177e4
LT
4813}
4814
963d8fe5
TM
4815static const struct rpc_call_ops nfs4_renew_ops = {
4816 .rpc_call_done = nfs4_renew_done,
dc96aef9 4817 .rpc_release = nfs4_renew_release,
963d8fe5
TM
4818};
4819
2f60ea6b 4820static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
1da177e4
LT
4821{
4822 struct rpc_message msg = {
4823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4824 .rpc_argp = clp,
b4454fe1 4825 .rpc_cred = cred,
1da177e4 4826 };
9bc4e3ca 4827 struct nfs4_renewdata *data;
1da177e4 4828
2f60ea6b
TM
4829 if (renew_flags == 0)
4830 return 0;
0851de06
AB
4831 if (!atomic_inc_not_zero(&clp->cl_count))
4832 return -EIO;
b569ad34 4833 data = kmalloc(sizeof(*data), GFP_NOFS);
9bc4e3ca
CL
4834 if (data == NULL)
4835 return -ENOMEM;
4836 data->client = clp;
4837 data->timestamp = jiffies;
bc7a05ca 4838 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
9bc4e3ca 4839 &nfs4_renew_ops, data);
1da177e4
LT
4840}
4841
8534d4ec 4842static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
1da177e4
LT
4843{
4844 struct rpc_message msg = {
4845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4846 .rpc_argp = clp,
b4454fe1 4847 .rpc_cred = cred,
1da177e4
LT
4848 };
4849 unsigned long now = jiffies;
4850 int status;
4851
bc7a05ca 4852 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
1da177e4
LT
4853 if (status < 0)
4854 return status;
452e9352 4855 do_renew_lease(clp, now);
1da177e4
LT
4856 return 0;
4857}
4858
aa1870af
BF
4859static inline int nfs4_server_supports_acls(struct nfs_server *server)
4860{
7dd7d959 4861 return server->caps & NFS_CAP_ACLS;
aa1870af
BF
4862}
4863
21f498c2
TM
4864/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4865 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
aa1870af
BF
4866 * the stack.
4867 */
21f498c2 4868#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
aa1870af 4869
e9e3d724 4870static int buf_to_pages_noslab(const void *buf, size_t buflen,
8fbcf237 4871 struct page **pages)
e9e3d724
NH
4872{
4873 struct page *newpage, **spages;
4874 int rc = 0;
4875 size_t len;
4876 spages = pages;
4877
4878 do {
21f498c2 4879 len = min_t(size_t, PAGE_SIZE, buflen);
e9e3d724
NH
4880 newpage = alloc_page(GFP_KERNEL);
4881
4882 if (newpage == NULL)
4883 goto unwind;
4884 memcpy(page_address(newpage), buf, len);
4885 buf += len;
4886 buflen -= len;
4887 *pages++ = newpage;
4888 rc++;
4889 } while (buflen != 0);
4890
4891 return rc;
4892
4893unwind:
4894 for(; rc > 0; rc--)
4895 __free_page(spages[rc-1]);
4896 return -ENOMEM;
4897}
4898
e50a1c2e
BF
4899struct nfs4_cached_acl {
4900 int cached;
4901 size_t len;
3e9d4154 4902 char data[0];
e50a1c2e
BF
4903};
4904
4905static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4906{
4907 struct nfs_inode *nfsi = NFS_I(inode);
4908
4909 spin_lock(&inode->i_lock);
4910 kfree(nfsi->nfs4_acl);
4911 nfsi->nfs4_acl = acl;
4912 spin_unlock(&inode->i_lock);
4913}
4914
4915static void nfs4_zap_acl_attr(struct inode *inode)
4916{
4917 nfs4_set_cached_acl(inode, NULL);
4918}
4919
4920static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4921{
4922 struct nfs_inode *nfsi = NFS_I(inode);
4923 struct nfs4_cached_acl *acl;
4924 int ret = -ENOENT;
4925
4926 spin_lock(&inode->i_lock);
4927 acl = nfsi->nfs4_acl;
4928 if (acl == NULL)
4929 goto out;
4930 if (buf == NULL) /* user is just asking for length */
4931 goto out_len;
4932 if (acl->cached == 0)
4933 goto out;
4934 ret = -ERANGE; /* see getxattr(2) man page */
4935 if (acl->len > buflen)
4936 goto out;
4937 memcpy(buf, acl->data, acl->len);
4938out_len:
4939 ret = acl->len;
4940out:
4941 spin_unlock(&inode->i_lock);
4942 return ret;
4943}
4944
5794d21e 4945static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
e50a1c2e
BF
4946{
4947 struct nfs4_cached_acl *acl;
b291f1b1 4948 size_t buflen = sizeof(*acl) + acl_len;
e50a1c2e 4949
1f1ea6c2 4950 if (buflen <= PAGE_SIZE) {
b291f1b1 4951 acl = kmalloc(buflen, GFP_KERNEL);
e50a1c2e
BF
4952 if (acl == NULL)
4953 goto out;
4954 acl->cached = 1;
5794d21e 4955 _copy_from_pages(acl->data, pages, pgbase, acl_len);
e50a1c2e
BF
4956 } else {
4957 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4958 if (acl == NULL)
4959 goto out;
4960 acl->cached = 0;
4961 }
4962 acl->len = acl_len;
4963out:
4964 nfs4_set_cached_acl(inode, acl);
4965}
4966
bf118a34
AA
4967/*
4968 * The getxattr API returns the required buffer length when called with a
4969 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4970 * the required buf. On a NULL buf, we send a page of data to the server
4971 * guessing that the ACL request can be serviced by a page. If so, we cache
4972 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4973 * the cache. If not so, we throw away the page, and cache the required
4974 * length. The next getxattr call will then produce another round trip to
4975 * the server, this time with the input buf of the required size.
4976 */
16b4289c 4977static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
aa1870af 4978{
bf118a34 4979 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
aa1870af
BF
4980 struct nfs_getaclargs args = {
4981 .fh = NFS_FH(inode),
4982 .acl_pages = pages,
4983 .acl_len = buflen,
4984 };
663c79b3
BH
4985 struct nfs_getaclres res = {
4986 .acl_len = buflen,
4987 };
aa1870af
BF
4988 struct rpc_message msg = {
4989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4990 .rpc_argp = &args,
663c79b3 4991 .rpc_resp = &res,
aa1870af 4992 };
21f498c2
TM
4993 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4994 int ret = -ENOMEM, i;
aa1870af 4995
bf118a34
AA
4996 /* As long as we're doing a round trip to the server anyway,
4997 * let's be prepared for a page of acl data. */
4998 if (npages == 0)
4999 npages = 1;
21f498c2
TM
5000 if (npages > ARRAY_SIZE(pages))
5001 return -ERANGE;
5a006899 5002
bf118a34
AA
5003 for (i = 0; i < npages; i++) {
5004 pages[i] = alloc_page(GFP_KERNEL);
5005 if (!pages[i])
5006 goto out_free;
e50a1c2e 5007 }
5a006899
SP
5008
5009 /* for decoding across pages */
5010 res.acl_scratch = alloc_page(GFP_KERNEL);
5011 if (!res.acl_scratch)
5012 goto out_free;
5013
bf118a34 5014 args.acl_len = npages * PAGE_SIZE;
5a006899 5015
de040bec 5016 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
bf118a34
AA
5017 __func__, buf, buflen, npages, args.acl_len);
5018 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5019 &msg, &args.seq_args, &res.seq_res, 0);
e50a1c2e
BF
5020 if (ret)
5021 goto out_free;
bf118a34 5022
1f1ea6c2
TM
5023 /* Handle the case where the passed-in buffer is too short */
5024 if (res.acl_flags & NFS4_ACL_TRUNC) {
5025 /* Did the user only issue a request for the acl length? */
5026 if (buf == NULL)
5027 goto out_ok;
e50a1c2e 5028 ret = -ERANGE;
1f1ea6c2 5029 goto out_free;
e50a1c2e 5030 }
1f1ea6c2 5031 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
7d3e91a8
SW
5032 if (buf) {
5033 if (res.acl_len > buflen) {
5034 ret = -ERANGE;
5035 goto out_free;
5036 }
1f1ea6c2 5037 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
7d3e91a8 5038 }
1f1ea6c2
TM
5039out_ok:
5040 ret = res.acl_len;
e50a1c2e 5041out_free:
bf118a34
AA
5042 for (i = 0; i < npages; i++)
5043 if (pages[i])
5044 __free_page(pages[i]);
331818f1
TM
5045 if (res.acl_scratch)
5046 __free_page(res.acl_scratch);
aa1870af
BF
5047 return ret;
5048}
5049
16b4289c
TM
5050static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5051{
5052 struct nfs4_exception exception = { };
5053 ssize_t ret;
5054 do {
5055 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
c1578b76 5056 trace_nfs4_get_acl(inode, ret);
16b4289c
TM
5057 if (ret >= 0)
5058 break;
5059 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5060 } while (exception.retry);
5061 return ret;
5062}
5063
e50a1c2e
BF
5064static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5065{
5066 struct nfs_server *server = NFS_SERVER(inode);
5067 int ret;
5068
5069 if (!nfs4_server_supports_acls(server))
5070 return -EOPNOTSUPP;
5071 ret = nfs_revalidate_inode(server, inode);
5072 if (ret < 0)
5073 return ret;
08a22b39
AK
5074 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5075 nfs_zap_acl_cache(inode);
e50a1c2e
BF
5076 ret = nfs4_read_cached_acl(inode, buf, buflen);
5077 if (ret != -ENOENT)
bf118a34
AA
5078 /* -ENOENT is returned if there is no ACL or if there is an ACL
5079 * but no cached acl data, just the acl length */
e50a1c2e
BF
5080 return ret;
5081 return nfs4_get_acl_uncached(inode, buf, buflen);
5082}
5083
16b4289c 5084static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4b580ee3
BF
5085{
5086 struct nfs_server *server = NFS_SERVER(inode);
5087 struct page *pages[NFS4ACL_MAXPAGES];
5088 struct nfs_setaclargs arg = {
5089 .fh = NFS_FH(inode),
5090 .acl_pages = pages,
5091 .acl_len = buflen,
5092 };
73c403a9 5093 struct nfs_setaclres res;
4b580ee3
BF
5094 struct rpc_message msg = {
5095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5096 .rpc_argp = &arg,
73c403a9 5097 .rpc_resp = &res,
4b580ee3 5098 };
21f498c2 5099 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
e9e3d724 5100 int ret, i;
4b580ee3
BF
5101
5102 if (!nfs4_server_supports_acls(server))
5103 return -EOPNOTSUPP;
21f498c2
TM
5104 if (npages > ARRAY_SIZE(pages))
5105 return -ERANGE;
8fbcf237 5106 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
e9e3d724
NH
5107 if (i < 0)
5108 return i;
57ec14c5 5109 nfs4_inode_return_delegation(inode);
7c513058 5110 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
e9e3d724
NH
5111
5112 /*
5113 * Free each page after tx, so the only ref left is
5114 * held by the network stack
5115 */
5116 for (; i > 0; i--)
5117 put_page(pages[i-1]);
5118
08a22b39
AK
5119 /*
5120 * Acl update can result in inode attribute update.
5121 * so mark the attribute cache invalid.
5122 */
5123 spin_lock(&inode->i_lock);
5124 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5125 spin_unlock(&inode->i_lock);
f41f7418
TM
5126 nfs_access_zap_cache(inode);
5127 nfs_zap_acl_cache(inode);
4b580ee3
BF
5128 return ret;
5129}
5130
16b4289c
TM
5131static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5132{
5133 struct nfs4_exception exception = { };
5134 int err;
5135 do {
c1578b76
TM
5136 err = __nfs4_proc_set_acl(inode, buf, buflen);
5137 trace_nfs4_set_acl(inode, err);
5138 err = nfs4_handle_exception(NFS_SERVER(inode), err,
16b4289c
TM
5139 &exception);
5140 } while (exception.retry);
5141 return err;
5142}
5143
aa9c2669
DQ
5144#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5145static int _nfs4_get_security_label(struct inode *inode, void *buf,
5146 size_t buflen)
5147{
5148 struct nfs_server *server = NFS_SERVER(inode);
5149 struct nfs_fattr fattr;
5150 struct nfs4_label label = {0, 0, buflen, buf};
5151
5152 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
fcb63a9b 5153 struct nfs4_getattr_arg arg = {
aa9c2669
DQ
5154 .fh = NFS_FH(inode),
5155 .bitmask = bitmask,
5156 };
5157 struct nfs4_getattr_res res = {
5158 .fattr = &fattr,
5159 .label = &label,
5160 .server = server,
5161 };
5162 struct rpc_message msg = {
5163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
fcb63a9b 5164 .rpc_argp = &arg,
aa9c2669
DQ
5165 .rpc_resp = &res,
5166 };
5167 int ret;
5168
5169 nfs_fattr_init(&fattr);
5170
fcb63a9b 5171 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
aa9c2669
DQ
5172 if (ret)
5173 return ret;
5174 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5175 return -ENOENT;
5176 if (buflen < label.len)
5177 return -ERANGE;
5178 return 0;
5179}
5180
5181static int nfs4_get_security_label(struct inode *inode, void *buf,
5182 size_t buflen)
5183{
5184 struct nfs4_exception exception = { };
5185 int err;
5186
5187 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5188 return -EOPNOTSUPP;
5189
5190 do {
c1578b76
TM
5191 err = _nfs4_get_security_label(inode, buf, buflen);
5192 trace_nfs4_get_security_label(inode, err);
5193 err = nfs4_handle_exception(NFS_SERVER(inode), err,
aa9c2669
DQ
5194 &exception);
5195 } while (exception.retry);
5196 return err;
5197}
5198
5199static int _nfs4_do_set_security_label(struct inode *inode,
5200 struct nfs4_label *ilabel,
5201 struct nfs_fattr *fattr,
5202 struct nfs4_label *olabel)
5203{
5204
5205 struct iattr sattr = {0};
5206 struct nfs_server *server = NFS_SERVER(inode);
5207 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
12207f69 5208 struct nfs_setattrargs arg = {
aa9c2669
DQ
5209 .fh = NFS_FH(inode),
5210 .iap = &sattr,
5211 .server = server,
5212 .bitmask = bitmask,
5213 .label = ilabel,
5214 };
5215 struct nfs_setattrres res = {
5216 .fattr = fattr,
5217 .label = olabel,
5218 .server = server,
5219 };
5220 struct rpc_message msg = {
5221 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
12207f69 5222 .rpc_argp = &arg,
aa9c2669
DQ
5223 .rpc_resp = &res,
5224 };
5225 int status;
5226
12207f69 5227 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
aa9c2669 5228
12207f69 5229 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
aa9c2669
DQ
5230 if (status)
5231 dprintk("%s failed: %d\n", __func__, status);
5232
5233 return status;
5234}
5235
5236static int nfs4_do_set_security_label(struct inode *inode,
5237 struct nfs4_label *ilabel,
5238 struct nfs_fattr *fattr,
5239 struct nfs4_label *olabel)
5240{
5241 struct nfs4_exception exception = { };
5242 int err;
5243
5244 do {
c1578b76
TM
5245 err = _nfs4_do_set_security_label(inode, ilabel,
5246 fattr, olabel);
5247 trace_nfs4_set_security_label(inode, err);
5248 err = nfs4_handle_exception(NFS_SERVER(inode), err,
aa9c2669
DQ
5249 &exception);
5250 } while (exception.retry);
5251 return err;
5252}
5253
5254static int
59301226 5255nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
aa9c2669
DQ
5256{
5257 struct nfs4_label ilabel, *olabel = NULL;
5258 struct nfs_fattr fattr;
5259 struct rpc_cred *cred;
aa9c2669
DQ
5260 int status;
5261
5262 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5263 return -EOPNOTSUPP;
5264
5265 nfs_fattr_init(&fattr);
5266
5267 ilabel.pi = 0;
5268 ilabel.lfs = 0;
5269 ilabel.label = (char *)buf;
5270 ilabel.len = buflen;
5271
5272 cred = rpc_lookup_cred();
5273 if (IS_ERR(cred))
5274 return PTR_ERR(cred);
5275
5276 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5277 if (IS_ERR(olabel)) {
5278 status = -PTR_ERR(olabel);
5279 goto out;
5280 }
5281
5282 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5283 if (status == 0)
5284 nfs_setsecurity(inode, &fattr, olabel);
5285
5286 nfs4_label_free(olabel);
5287out:
5288 put_rpccred(cred);
5289 return status;
5290}
5291#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5292
5293
f092075d
CL
5294static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5295 nfs4_verifier *bootverf)
cd93710e
CL
5296{
5297 __be32 verf[2];
5298
2c820d9a
CL
5299 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5300 /* An impossible timestamp guarantees this value
5301 * will never match a generated boot time. */
2f86e091
DD
5302 verf[0] = cpu_to_be32(U32_MAX);
5303 verf[1] = cpu_to_be32(U32_MAX);
2c820d9a 5304 } else {
f092075d 5305 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
2f86e091
DD
5306 u64 ns = ktime_to_ns(nn->boot_time);
5307
5308 verf[0] = cpu_to_be32(ns >> 32);
5309 verf[1] = cpu_to_be32(ns);
2c820d9a 5310 }
cd93710e
CL
5311 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5312}
5313
a3192688
JL
5314static int
5315nfs4_init_nonuniform_client_string(struct nfs_client *clp)
e984a55a 5316{
a3192688
JL
5317 size_t len;
5318 char *str;
e984a55a 5319
ceb3a16c 5320 if (clp->cl_owner_id != NULL)
a3192688 5321 return 0;
4a3e5779 5322
a3192688 5323 rcu_read_lock();
4a70316c 5324 len = 14 + strlen(clp->cl_ipaddr) + 1 +
a3192688
JL
5325 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5326 1 +
5327 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5328 1;
5329 rcu_read_unlock();
5330
5331 if (len > NFS4_OPAQUE_LIMIT + 1)
5332 return -EINVAL;
5333
5334 /*
5335 * Since this string is allocated at mount time, and held until the
5336 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5337 * about a memory-reclaim deadlock.
5338 */
5339 str = kmalloc(len, GFP_KERNEL);
5340 if (!str)
5341 return -ENOMEM;
ceb3a16c 5342
e984a55a 5343 rcu_read_lock();
f2dd436e 5344 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
a3192688
JL
5345 clp->cl_ipaddr,
5346 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5347 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
e984a55a 5348 rcu_read_unlock();
a3192688 5349
a3192688
JL
5350 clp->cl_owner_id = str;
5351 return 0;
e984a55a
CL
5352}
5353
873e3851
JL
5354static int
5355nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5356{
873e3851
JL
5357 size_t len;
5358 char *str;
5359
5360 len = 10 + 10 + 1 + 10 + 1 +
5361 strlen(nfs4_client_id_uniquifier) + 1 +
5362 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5363
5364 if (len > NFS4_OPAQUE_LIMIT + 1)
5365 return -EINVAL;
5366
5367 /*
5368 * Since this string is allocated at mount time, and held until the
5369 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5370 * about a memory-reclaim deadlock.
5371 */
5372 str = kmalloc(len, GFP_KERNEL);
5373 if (!str)
5374 return -ENOMEM;
5375
f2dd436e 5376 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
873e3851
JL
5377 clp->rpc_ops->version, clp->cl_minorversion,
5378 nfs4_client_id_uniquifier,
5379 clp->cl_rpcclient->cl_nodename);
873e3851
JL
5380 clp->cl_owner_id = str;
5381 return 0;
5382}
5383
5384static int
5385nfs4_init_uniform_client_string(struct nfs_client *clp)
e984a55a 5386{
873e3851
JL
5387 size_t len;
5388 char *str;
ceb3a16c
TM
5389
5390 if (clp->cl_owner_id != NULL)
873e3851 5391 return 0;
6f2ea7f2
CL
5392
5393 if (nfs4_client_id_uniquifier[0] != '\0')
873e3851
JL
5394 return nfs4_init_uniquifier_client_string(clp);
5395
5396 len = 10 + 10 + 1 + 10 + 1 +
5397 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5398
5399 if (len > NFS4_OPAQUE_LIMIT + 1)
5400 return -EINVAL;
5401
5402 /*
5403 * Since this string is allocated at mount time, and held until the
5404 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5405 * about a memory-reclaim deadlock.
5406 */
5407 str = kmalloc(len, GFP_KERNEL);
5408 if (!str)
5409 return -ENOMEM;
5410
f2dd436e 5411 scnprintf(str, len, "Linux NFSv%u.%u %s",
873e3851
JL
5412 clp->rpc_ops->version, clp->cl_minorversion,
5413 clp->cl_rpcclient->cl_nodename);
873e3851
JL
5414 clp->cl_owner_id = str;
5415 return 0;
e984a55a
CL
5416}
5417
706cb8db
CL
5418/*
5419 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5420 * services. Advertise one based on the address family of the
5421 * clientaddr.
5422 */
5423static unsigned int
5424nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5425{
5426 if (strchr(clp->cl_ipaddr, ':') != NULL)
5427 return scnprintf(buf, len, "tcp6");
5428 else
5429 return scnprintf(buf, len, "tcp");
5430}
5431
f11b2a1c
JL
5432static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5433{
5434 struct nfs4_setclientid *sc = calldata;
5435
5436 if (task->tk_status == 0)
5437 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5438}
5439
5440static const struct rpc_call_ops nfs4_setclientid_ops = {
5441 .rpc_call_done = nfs4_setclientid_done,
5442};
5443
6bbb4ae8
CL
5444/**
5445 * nfs4_proc_setclientid - Negotiate client ID
5446 * @clp: state data structure
5447 * @program: RPC program for NFSv4 callback service
5448 * @port: IP port number for NFS4 callback service
5449 * @cred: RPC credential to use for this call
5450 * @res: where to place the result
5451 *
5452 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5453 */
bb8b27e5
TM
5454int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5455 unsigned short port, struct rpc_cred *cred,
5456 struct nfs4_setclientid_res *res)
1da177e4
LT
5457{
5458 nfs4_verifier sc_verifier;
5459 struct nfs4_setclientid setclientid = {
5460 .sc_verifier = &sc_verifier,
5461 .sc_prog = program,
3a6bb738 5462 .sc_clnt = clp,
1da177e4
LT
5463 };
5464 struct rpc_message msg = {
5465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5466 .rpc_argp = &setclientid,
bb8b27e5 5467 .rpc_resp = res,
286d7d6a 5468 .rpc_cred = cred,
1da177e4 5469 };
f11b2a1c
JL
5470 struct rpc_task *task;
5471 struct rpc_task_setup task_setup_data = {
5472 .rpc_client = clp->cl_rpcclient,
5473 .rpc_message = &msg,
5474 .callback_ops = &nfs4_setclientid_ops,
5475 .callback_data = &setclientid,
5476 .flags = RPC_TASK_TIMEOUT,
5477 };
6bbb4ae8 5478 int status;
1da177e4 5479
de734831 5480 /* nfs_client_id4 */
f092075d 5481 nfs4_init_boot_verifier(clp, &sc_verifier);
873e3851
JL
5482
5483 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5484 status = nfs4_init_uniform_client_string(clp);
5485 else
a3192688 5486 status = nfs4_init_nonuniform_client_string(clp);
873e3851
JL
5487
5488 if (status)
5489 goto out;
3a6bb738 5490
de734831 5491 /* cb_client4 */
706cb8db
CL
5492 setclientid.sc_netid_len =
5493 nfs4_init_callback_netid(clp,
5494 setclientid.sc_netid,
5495 sizeof(setclientid.sc_netid));
de734831 5496 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
d4d3c507 5497 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
1da177e4
LT
5498 clp->cl_ipaddr, port >> 8, port & 255);
5499
3a6bb738 5500 dprintk("NFS call setclientid auth=%s, '%s'\n",
6bbb4ae8 5501 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3a6bb738 5502 clp->cl_owner_id);
f11b2a1c
JL
5503 task = rpc_run_task(&task_setup_data);
5504 if (IS_ERR(task)) {
5505 status = PTR_ERR(task);
5506 goto out;
5507 }
5508 status = task->tk_status;
5509 if (setclientid.sc_cred) {
5510 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5511 put_rpccred(setclientid.sc_cred);
5512 }
5513 rpc_put_task(task);
5514out:
c6d01c6f 5515 trace_nfs4_setclientid(clp, status);
6bbb4ae8
CL
5516 dprintk("NFS reply setclientid: %d\n", status);
5517 return status;
1da177e4
LT
5518}
5519
6bbb4ae8
CL
5520/**
5521 * nfs4_proc_setclientid_confirm - Confirm client ID
5522 * @clp: state data structure
5523 * @res: result of a previous SETCLIENTID
5524 * @cred: RPC credential to use for this call
5525 *
5526 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5527 */
fd954ae1 5528int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
bb8b27e5
TM
5529 struct nfs4_setclientid_res *arg,
5530 struct rpc_cred *cred)
1da177e4 5531{
1da177e4
LT
5532 struct rpc_message msg = {
5533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
bb8b27e5 5534 .rpc_argp = arg,
286d7d6a 5535 .rpc_cred = cred,
1da177e4 5536 };
1da177e4
LT
5537 int status;
5538
6bbb4ae8
CL
5539 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5540 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5541 clp->cl_clientid);
1bd714f2 5542 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
c6d01c6f 5543 trace_nfs4_setclientid_confirm(clp, status);
6bbb4ae8 5544 dprintk("NFS reply setclientid_confirm: %d\n", status);
1da177e4
LT
5545 return status;
5546}
5547
fe650407
TM
5548struct nfs4_delegreturndata {
5549 struct nfs4_delegreturnargs args;
fa178f29 5550 struct nfs4_delegreturnres res;
fe650407
TM
5551 struct nfs_fh fh;
5552 nfs4_stateid stateid;
26e976a8 5553 unsigned long timestamp;
fa178f29 5554 struct nfs_fattr fattr;
fe650407 5555 int rpc_status;
039b756a
PT
5556 struct inode *inode;
5557 bool roc;
5558 u32 roc_barrier;
fe650407
TM
5559};
5560
fe650407
TM
5561static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5562{
5563 struct nfs4_delegreturndata *data = calldata;
938e1010 5564
14516c3a
TM
5565 if (!nfs4_sequence_done(task, &data->res.seq_res))
5566 return;
938e1010 5567
ca8acf8d 5568 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
79708861 5569 switch (task->tk_status) {
79708861 5570 case 0:
fa178f29 5571 renew_lease(data->res.server, data->timestamp);
c97cf606
TM
5572 case -NFS4ERR_ADMIN_REVOKED:
5573 case -NFS4ERR_DELEG_REVOKED:
26d36301
TM
5574 case -NFS4ERR_EXPIRED:
5575 nfs4_free_revoked_stateid(data->res.server,
5576 data->args.stateid,
5577 task->tk_msg.rpc_cred);
c97cf606
TM
5578 case -NFS4ERR_BAD_STATEID:
5579 case -NFS4ERR_OLD_STATEID:
5580 case -NFS4ERR_STALE_STATEID:
c97cf606 5581 task->tk_status = 0;
039b756a
PT
5582 if (data->roc)
5583 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
c97cf606 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;
fe650407
TM
5593}
5594
5595static void nfs4_delegreturn_release(void *calldata)
5596{
039b756a 5597 struct nfs4_delegreturndata *data = calldata;
ea7c38fe 5598 struct inode *inode = data->inode;
039b756a 5599
ea7c38fe
TM
5600 if (inode) {
5601 if (data->roc)
5602 pnfs_roc_release(inode);
5603 nfs_iput_and_deactive(inode);
5604 }
fe650407
TM
5605 kfree(calldata);
5606}
5607
938e1010
AA
5608static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5609{
5610 struct nfs4_delegreturndata *d_data;
5611
5612 d_data = (struct nfs4_delegreturndata *)data;
5613
500d701f
PT
5614 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5615 return;
5616
4ff376fe
TM
5617 if (d_data->roc)
5618 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
039b756a 5619
d9afbd1b
TM
5620 nfs4_setup_sequence(d_data->res.server,
5621 &d_data->args.seq_args,
5622 &d_data->res.seq_res,
5623 task);
938e1010 5624}
938e1010 5625
c8d149f3 5626static const struct rpc_call_ops nfs4_delegreturn_ops = {
938e1010 5627 .rpc_call_prepare = nfs4_delegreturn_prepare,
fe650407
TM
5628 .rpc_call_done = nfs4_delegreturn_done,
5629 .rpc_release = nfs4_delegreturn_release,
5630};
5631
e6f81075 5632static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
fe650407
TM
5633{
5634 struct nfs4_delegreturndata *data;
fa178f29 5635 struct nfs_server *server = NFS_SERVER(inode);
fe650407 5636 struct rpc_task *task;
5138fde0
TM
5637 struct rpc_message msg = {
5638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5639 .rpc_cred = cred,
5640 };
c970aa85
TM
5641 struct rpc_task_setup task_setup_data = {
5642 .rpc_client = server->client,
5138fde0 5643 .rpc_message = &msg,
c970aa85
TM
5644 .callback_ops = &nfs4_delegreturn_ops,
5645 .flags = RPC_TASK_ASYNC,
5646 };
e6f81075 5647 int status = 0;
fe650407 5648
8535b2be 5649 data = kzalloc(sizeof(*data), GFP_NOFS);
fe650407
TM
5650 if (data == NULL)
5651 return -ENOMEM;
a9c92d6b 5652 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
99ade3c7
AE
5653
5654 nfs4_state_protect(server->nfs_client,
5655 NFS_SP4_MACH_CRED_CLEANUP,
5656 &task_setup_data.rpc_client, &msg);
5657
fe650407
TM
5658 data->args.fhandle = &data->fh;
5659 data->args.stateid = &data->stateid;
9e907fec 5660 data->args.bitmask = server->cache_consistency_bitmask;
fe650407 5661 nfs_copy_fh(&data->fh, NFS_FH(inode));
f597c537 5662 nfs4_stateid_copy(&data->stateid, stateid);
fa178f29
TM
5663 data->res.fattr = &data->fattr;
5664 data->res.server = server;
5138fde0 5665 nfs_fattr_init(data->res.fattr);
26e976a8 5666 data->timestamp = jiffies;
fe650407 5667 data->rpc_status = 0;
ea7c38fe
TM
5668 data->inode = nfs_igrab_and_active(inode);
5669 if (data->inode)
5670 data->roc = nfs4_roc(inode);
fe650407 5671
c970aa85 5672 task_setup_data.callback_data = data;
1174dd1f
TM
5673 msg.rpc_argp = &data->args;
5674 msg.rpc_resp = &data->res;
c970aa85 5675 task = rpc_run_task(&task_setup_data);
7a1218a2 5676 if (IS_ERR(task))
fe650407 5677 return PTR_ERR(task);
e6f81075
TM
5678 if (!issync)
5679 goto out;
fe650407 5680 status = nfs4_wait_for_completion_rpc_task(task);
e6f81075
TM
5681 if (status != 0)
5682 goto out;
5683 status = data->rpc_status;
e144cbcc
TM
5684 if (status == 0)
5685 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5686 else
5687 nfs_refresh_inode(inode, &data->fattr);
e6f81075 5688out:
e6b3c4db 5689 rpc_put_task(task);
fe650407 5690 return status;
1da177e4
LT
5691}
5692
e6f81075 5693int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
1da177e4
LT
5694{
5695 struct nfs_server *server = NFS_SERVER(inode);
5696 struct nfs4_exception exception = { };
5697 int err;
5698 do {
e6f81075 5699 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
48c9579a 5700 trace_nfs4_delegreturn(inode, stateid, err);
1da177e4
LT
5701 switch (err) {
5702 case -NFS4ERR_STALE_STATEID:
5703 case -NFS4ERR_EXPIRED:
1da177e4
LT
5704 case 0:
5705 return 0;
5706 }
5707 err = nfs4_handle_exception(server, err, &exception);
5708 } while (exception.retry);
5709 return err;
5710}
5711
1da177e4
LT
5712static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5713{
5714 struct inode *inode = state->inode;
5715 struct nfs_server *server = NFS_SERVER(inode);
7539bbab 5716 struct nfs_client *clp = server->nfs_client;
911d1aaf 5717 struct nfs_lockt_args arg = {
1da177e4 5718 .fh = NFS_FH(inode),
911d1aaf 5719 .fl = request,
1da177e4 5720 };
911d1aaf
TM
5721 struct nfs_lockt_res res = {
5722 .denied = request,
1da177e4
LT
5723 };
5724 struct rpc_message msg = {
5725 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5726 .rpc_argp = &arg,
5727 .rpc_resp = &res,
5728 .rpc_cred = state->owner->so_cred,
5729 };
1da177e4
LT
5730 struct nfs4_lock_state *lsp;
5731 int status;
5732
911d1aaf 5733 arg.lock_owner.clientid = clp->cl_clientid;
8d0a8a9d
TM
5734 status = nfs4_set_lock_state(state, request);
5735 if (status != 0)
5736 goto out;
5737 lsp = request->fl_u.nfs4_fl.owner;
48c22eb2 5738 arg.lock_owner.id = lsp->ls_seqid.owner_id;
d035c36c 5739 arg.lock_owner.s_dev = server->s_dev;
7c513058 5740 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
911d1aaf
TM
5741 switch (status) {
5742 case 0:
5743 request->fl_type = F_UNLCK;
5744 break;
5745 case -NFS4ERR_DENIED:
5746 status = 0;
1da177e4 5747 }
70cc6487 5748 request->fl_ops->fl_release_private(request);
a6f951dd 5749 request->fl_ops = NULL;
8d0a8a9d 5750out:
1da177e4
LT
5751 return status;
5752}
5753
5754static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5755{
5756 struct nfs4_exception exception = { };
5757 int err;
5758
5759 do {
d1b748a5
TM
5760 err = _nfs4_proc_getlk(state, cmd, request);
5761 trace_nfs4_get_lock(request, state, cmd, err);
5762 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
1da177e4
LT
5763 &exception);
5764 } while (exception.retry);
5765 return err;
5766}
5767
faf5f49c 5768struct nfs4_unlockdata {
911d1aaf
TM
5769 struct nfs_locku_args arg;
5770 struct nfs_locku_res res;
faf5f49c
TM
5771 struct nfs4_lock_state *lsp;
5772 struct nfs_open_context *ctx;
911d1aaf 5773 struct file_lock fl;
516285eb 5774 struct nfs_server *server;
26e976a8 5775 unsigned long timestamp;
faf5f49c
TM
5776};
5777
911d1aaf
TM
5778static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5779 struct nfs_open_context *ctx,
5780 struct nfs4_lock_state *lsp,
5781 struct nfs_seqid *seqid)
5782{
5783 struct nfs4_unlockdata *p;
5784 struct inode *inode = lsp->ls_state->inode;
5785
8535b2be 5786 p = kzalloc(sizeof(*p), GFP_NOFS);
911d1aaf
TM
5787 if (p == NULL)
5788 return NULL;
5789 p->arg.fh = NFS_FH(inode);
5790 p->arg.fl = &p->fl;
5791 p->arg.seqid = seqid;
c1d51931 5792 p->res.seqid = seqid;
911d1aaf
TM
5793 p->lsp = lsp;
5794 atomic_inc(&lsp->ls_count);
5795 /* Ensure we don't close file until we're done freeing locks! */
5796 p->ctx = get_nfs_open_context(ctx);
5797 memcpy(&p->fl, fl, sizeof(p->fl));
5798 p->server = NFS_SERVER(inode);
5799 return p;
5800}
5801
06f814a3 5802static void nfs4_locku_release_calldata(void *data)
faf5f49c 5803{
963d8fe5 5804 struct nfs4_unlockdata *calldata = data;
911d1aaf 5805 nfs_free_seqid(calldata->arg.seqid);
06f814a3
TM
5806 nfs4_put_lock_state(calldata->lsp);
5807 put_nfs_open_context(calldata->ctx);
5808 kfree(calldata);
faf5f49c
TM
5809}
5810
963d8fe5 5811static void nfs4_locku_done(struct rpc_task *task, void *data)
faf5f49c 5812{
963d8fe5 5813 struct nfs4_unlockdata *calldata = data;
faf5f49c 5814
14516c3a
TM
5815 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5816 return;
faf5f49c
TM
5817 switch (task->tk_status) {
5818 case 0:
26e976a8 5819 renew_lease(calldata->server, calldata->timestamp);
75575ddf 5820 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
c69899a1
TM
5821 if (nfs4_update_lock_stateid(calldata->lsp,
5822 &calldata->res.stateid))
5823 break;
26d36301
TM
5824 case -NFS4ERR_ADMIN_REVOKED:
5825 case -NFS4ERR_EXPIRED:
5826 nfs4_free_revoked_stateid(calldata->server,
5827 &calldata->arg.stateid,
5828 task->tk_msg.rpc_cred);
9e33bed5
TM
5829 case -NFS4ERR_BAD_STATEID:
5830 case -NFS4ERR_OLD_STATEID:
faf5f49c 5831 case -NFS4ERR_STALE_STATEID:
425c1d4e
TM
5832 if (!nfs4_stateid_match(&calldata->arg.stateid,
5833 &calldata->lsp->ls_stateid))
5834 rpc_restart_call_prepare(task);
faf5f49c
TM
5835 break;
5836 default:
8478eaa1
N
5837 if (nfs4_async_handle_error(task, calldata->server,
5838 NULL, NULL) == -EAGAIN)
d00c5d43 5839 rpc_restart_call_prepare(task);
faf5f49c 5840 }
2b1bc308 5841 nfs_release_seqid(calldata->arg.seqid);
faf5f49c
TM
5842}
5843
4ce70ada 5844static void nfs4_locku_prepare(struct rpc_task *task, void *data)
faf5f49c 5845{
4ce70ada 5846 struct nfs4_unlockdata *calldata = data;
faf5f49c 5847
911d1aaf 5848 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
c8da19b9 5849 goto out_wait;
425c1d4e 5850 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
795a88c9 5851 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
963d8fe5 5852 /* Note: exit _without_ running nfs4_locku_done */
c8da19b9 5853 goto out_no_action;
faf5f49c 5854 }
26e976a8 5855 calldata->timestamp = jiffies;
035168ab 5856 if (nfs4_setup_sequence(calldata->server,
a893693c 5857 &calldata->arg.seq_args,
2240a9e2
TM
5858 &calldata->res.seq_res,
5859 task) != 0)
5860 nfs_release_seqid(calldata->arg.seqid);
c8da19b9
TM
5861 return;
5862out_no_action:
5863 task->tk_action = NULL;
5864out_wait:
5865 nfs4_sequence_done(task, &calldata->res.seq_res);
faf5f49c
TM
5866}
5867
963d8fe5 5868static const struct rpc_call_ops nfs4_locku_ops = {
4ce70ada 5869 .rpc_call_prepare = nfs4_locku_prepare,
963d8fe5 5870 .rpc_call_done = nfs4_locku_done,
06f814a3 5871 .rpc_release = nfs4_locku_release_calldata,
963d8fe5
TM
5872};
5873
a5d16a4d
TM
5874static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5875 struct nfs_open_context *ctx,
5876 struct nfs4_lock_state *lsp,
5877 struct nfs_seqid *seqid)
5878{
5879 struct nfs4_unlockdata *data;
5138fde0
TM
5880 struct rpc_message msg = {
5881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5882 .rpc_cred = ctx->cred,
5883 };
c970aa85
TM
5884 struct rpc_task_setup task_setup_data = {
5885 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5138fde0 5886 .rpc_message = &msg,
c970aa85 5887 .callback_ops = &nfs4_locku_ops,
101070ca 5888 .workqueue = nfsiod_workqueue,
c970aa85
TM
5889 .flags = RPC_TASK_ASYNC,
5890 };
a5d16a4d 5891
fa940720
WAA
5892 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5893 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5894
137d6aca
FF
5895 /* Ensure this is an unlock - when canceling a lock, the
5896 * canceled lock is passed in, and it won't be an unlock.
5897 */
5898 fl->fl_type = F_UNLCK;
5899
a5d16a4d
TM
5900 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5901 if (data == NULL) {
5902 nfs_free_seqid(seqid);
5903 return ERR_PTR(-ENOMEM);
5904 }
5905
a9c92d6b 5906 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
1174dd1f
TM
5907 msg.rpc_argp = &data->arg;
5908 msg.rpc_resp = &data->res;
c970aa85
TM
5909 task_setup_data.callback_data = data;
5910 return rpc_run_task(&task_setup_data);
a5d16a4d
TM
5911}
5912
faf5f49c
TM
5913static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5914{
65b62a29
TM
5915 struct inode *inode = state->inode;
5916 struct nfs4_state_owner *sp = state->owner;
5917 struct nfs_inode *nfsi = NFS_I(inode);
911d1aaf 5918 struct nfs_seqid *seqid;
1da177e4 5919 struct nfs4_lock_state *lsp;
06f814a3 5920 struct rpc_task *task;
b4019c0e 5921 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
06f814a3 5922 int status = 0;
536ff0f8 5923 unsigned char fl_flags = request->fl_flags;
faf5f49c 5924
8d0a8a9d 5925 status = nfs4_set_lock_state(state, request);
9b073574
TM
5926 /* Unlock _before_ we do the RPC call */
5927 request->fl_flags |= FL_EXISTS;
65b62a29
TM
5928 /* Exclude nfs_delegation_claim_locks() */
5929 mutex_lock(&sp->so_delegreturn_mutex);
5930 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
19e03c57 5931 down_read(&nfsi->rwsem);
75575ddf 5932 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
19e03c57 5933 up_read(&nfsi->rwsem);
65b62a29 5934 mutex_unlock(&sp->so_delegreturn_mutex);
9b073574 5935 goto out;
19e03c57
TM
5936 }
5937 up_read(&nfsi->rwsem);
65b62a29 5938 mutex_unlock(&sp->so_delegreturn_mutex);
8d0a8a9d 5939 if (status != 0)
9b073574
TM
5940 goto out;
5941 /* Is this a delegated lock? */
8d0a8a9d 5942 lsp = request->fl_u.nfs4_fl.owner;
c5a2a15f
TM
5943 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5944 goto out;
b4019c0e
TM
5945 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5946 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
9b073574 5947 status = -ENOMEM;
badc76dd 5948 if (IS_ERR(seqid))
9b073574 5949 goto out;
cd3758e3 5950 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
a5d16a4d
TM
5951 status = PTR_ERR(task);
5952 if (IS_ERR(task))
9b073574 5953 goto out;
a5d16a4d 5954 status = nfs4_wait_for_completion_rpc_task(task);
e6b3c4db 5955 rpc_put_task(task);
9b073574 5956out:
536ff0f8 5957 request->fl_flags = fl_flags;
d1b748a5 5958 trace_nfs4_unlock(request, state, F_SETLK, status);
1da177e4
LT
5959 return status;
5960}
5961
a5d16a4d
TM
5962struct nfs4_lockdata {
5963 struct nfs_lock_args arg;
5964 struct nfs_lock_res res;
5965 struct nfs4_lock_state *lsp;
5966 struct nfs_open_context *ctx;
5967 struct file_lock fl;
26e976a8 5968 unsigned long timestamp;
a5d16a4d
TM
5969 int rpc_status;
5970 int cancelled;
66179efe 5971 struct nfs_server *server;
a5d16a4d
TM
5972};
5973
5974static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
8535b2be
TM
5975 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5976 gfp_t gfp_mask)
1da177e4 5977{
a5d16a4d
TM
5978 struct nfs4_lockdata *p;
5979 struct inode *inode = lsp->ls_state->inode;
1da177e4 5980 struct nfs_server *server = NFS_SERVER(inode);
b4019c0e 5981 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
a5d16a4d 5982
8535b2be 5983 p = kzalloc(sizeof(*p), gfp_mask);
a5d16a4d
TM
5984 if (p == NULL)
5985 return NULL;
5986
5987 p->arg.fh = NFS_FH(inode);
5988 p->arg.fl = &p->fl;
8535b2be 5989 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
badc76dd 5990 if (IS_ERR(p->arg.open_seqid))
2f74c0a0 5991 goto out_free;
b4019c0e
TM
5992 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5993 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
badc76dd 5994 if (IS_ERR(p->arg.lock_seqid))
2f74c0a0 5995 goto out_free_seqid;
7539bbab 5996 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
48c22eb2 5997 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
d035c36c 5998 p->arg.lock_owner.s_dev = server->s_dev;
c1d51931 5999 p->res.lock_seqid = p->arg.lock_seqid;
a5d16a4d 6000 p->lsp = lsp;
66179efe 6001 p->server = server;
a5d16a4d
TM
6002 atomic_inc(&lsp->ls_count);
6003 p->ctx = get_nfs_open_context(ctx);
feaff8e5 6004 get_file(fl->fl_file);
a5d16a4d
TM
6005 memcpy(&p->fl, fl, sizeof(p->fl));
6006 return p;
2f74c0a0
TM
6007out_free_seqid:
6008 nfs_free_seqid(p->arg.open_seqid);
a5d16a4d
TM
6009out_free:
6010 kfree(p);
6011 return NULL;
6012}
6013
6014static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6015{
6016 struct nfs4_lockdata *data = calldata;
6017 struct nfs4_state *state = data->lsp->ls_state;
06735b34 6018
3110ff80 6019 dprintk("%s: begin!\n", __func__);
2f74c0a0 6020 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
c8da19b9 6021 goto out_wait;
a5d16a4d 6022 /* Do we need to do an open_to_lock_owner? */
6b447539 6023 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
8fe72bac 6024 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
2240a9e2 6025 goto out_release_lock_seqid;
8fe72bac 6026 }
425c1d4e
TM
6027 nfs4_stateid_copy(&data->arg.open_stateid,
6028 &state->open_stateid);
a5d16a4d 6029 data->arg.new_lock_owner = 1;
c1d51931 6030 data->res.open_seqid = data->arg.open_seqid;
425c1d4e 6031 } else {
2f74c0a0 6032 data->arg.new_lock_owner = 0;
425c1d4e
TM
6033 nfs4_stateid_copy(&data->arg.lock_stateid,
6034 &data->lsp->ls_stateid);
6035 }
5d422301
TM
6036 if (!nfs4_valid_open_stateid(state)) {
6037 data->rpc_status = -EBADF;
6038 task->tk_action = NULL;
6039 goto out_release_open_seqid;
6040 }
26e976a8 6041 data->timestamp = jiffies;
035168ab
TM
6042 if (nfs4_setup_sequence(data->server,
6043 &data->arg.seq_args,
2240a9e2 6044 &data->res.seq_res,
d9afbd1b 6045 task) == 0)
66179efe 6046 return;
5d422301 6047out_release_open_seqid:
2240a9e2
TM
6048 nfs_release_seqid(data->arg.open_seqid);
6049out_release_lock_seqid:
6050 nfs_release_seqid(data->arg.lock_seqid);
c8da19b9
TM
6051out_wait:
6052 nfs4_sequence_done(task, &data->res.seq_res);
8fe72bac 6053 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
b257957e
AB
6054}
6055
a5d16a4d
TM
6056static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6057{
6058 struct nfs4_lockdata *data = calldata;
39071e6f 6059 struct nfs4_lock_state *lsp = data->lsp;
a5d16a4d 6060
3110ff80 6061 dprintk("%s: begin!\n", __func__);
a5d16a4d 6062
14516c3a
TM
6063 if (!nfs4_sequence_done(task, &data->res.seq_res))
6064 return;
66179efe 6065
a5d16a4d 6066 data->rpc_status = task->tk_status;
425c1d4e
TM
6067 switch (task->tk_status) {
6068 case 0:
2b0143b5 6069 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
39071e6f 6070 data->timestamp);
c69899a1
TM
6071 if (data->arg.new_lock) {
6072 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
75575ddf 6073 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
c69899a1
TM
6074 rpc_restart_call_prepare(task);
6075 break;
6076 }
6077 }
39071e6f
TM
6078 if (data->arg.new_lock_owner != 0) {
6079 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6080 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6081 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6082 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6083 rpc_restart_call_prepare(task);
425c1d4e
TM
6084 break;
6085 case -NFS4ERR_BAD_STATEID:
6086 case -NFS4ERR_OLD_STATEID:
6087 case -NFS4ERR_STALE_STATEID:
6088 case -NFS4ERR_EXPIRED:
6089 if (data->arg.new_lock_owner != 0) {
6090 if (!nfs4_stateid_match(&data->arg.open_stateid,
6091 &lsp->ls_state->open_stateid))
6092 rpc_restart_call_prepare(task);
6093 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6094 &lsp->ls_stateid))
6095 rpc_restart_call_prepare(task);
a5d16a4d 6096 }
3110ff80 6097 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
a5d16a4d
TM
6098}
6099
6100static void nfs4_lock_release(void *calldata)
6101{
6102 struct nfs4_lockdata *data = calldata;
6103
3110ff80 6104 dprintk("%s: begin!\n", __func__);
2f74c0a0 6105 nfs_free_seqid(data->arg.open_seqid);
a5d16a4d
TM
6106 if (data->cancelled != 0) {
6107 struct rpc_task *task;
6108 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6109 data->arg.lock_seqid);
6110 if (!IS_ERR(task))
bf294b41 6111 rpc_put_task_async(task);
3110ff80 6112 dprintk("%s: cancelling lock!\n", __func__);
a5d16a4d
TM
6113 } else
6114 nfs_free_seqid(data->arg.lock_seqid);
6115 nfs4_put_lock_state(data->lsp);
6116 put_nfs_open_context(data->ctx);
feaff8e5 6117 fput(data->fl.fl_file);
a5d16a4d 6118 kfree(data);
3110ff80 6119 dprintk("%s: done!\n", __func__);
a5d16a4d
TM
6120}
6121
6122static const struct rpc_call_ops nfs4_lock_ops = {
6123 .rpc_call_prepare = nfs4_lock_prepare,
6124 .rpc_call_done = nfs4_lock_done,
6125 .rpc_release = nfs4_lock_release,
6126};
6127
2bee72a6
TM
6128static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6129{
2bee72a6
TM
6130 switch (error) {
6131 case -NFS4ERR_ADMIN_REVOKED:
d7f3e4bf 6132 case -NFS4ERR_EXPIRED:
2bee72a6 6133 case -NFS4ERR_BAD_STATEID:
ecac799a 6134 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
2bee72a6 6135 if (new_lock_owner != 0 ||
795a88c9 6136 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
ecac799a 6137 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
a2c0b9e2
TM
6138 break;
6139 case -NFS4ERR_STALE_STATEID:
a2c0b9e2 6140 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
ecac799a 6141 nfs4_schedule_lease_recovery(server->nfs_client);
2bee72a6
TM
6142 };
6143}
6144
afe6c27c 6145static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
a5d16a4d
TM
6146{
6147 struct nfs4_lockdata *data;
6148 struct rpc_task *task;
5138fde0
TM
6149 struct rpc_message msg = {
6150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6151 .rpc_cred = state->owner->so_cred,
6152 };
c970aa85
TM
6153 struct rpc_task_setup task_setup_data = {
6154 .rpc_client = NFS_CLIENT(state->inode),
5138fde0 6155 .rpc_message = &msg,
c970aa85 6156 .callback_ops = &nfs4_lock_ops,
101070ca 6157 .workqueue = nfsiod_workqueue,
c970aa85
TM
6158 .flags = RPC_TASK_ASYNC,
6159 };
a5d16a4d
TM
6160 int ret;
6161
3110ff80 6162 dprintk("%s: begin!\n", __func__);
cd3758e3 6163 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
8535b2be
TM
6164 fl->fl_u.nfs4_fl.owner,
6165 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
a5d16a4d
TM
6166 if (data == NULL)
6167 return -ENOMEM;
6168 if (IS_SETLKW(cmd))
6169 data->arg.block = 1;
a9c92d6b 6170 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
1174dd1f
TM
6171 msg.rpc_argp = &data->arg;
6172 msg.rpc_resp = &data->res;
c970aa85 6173 task_setup_data.callback_data = data;
8fe72bac
TM
6174 if (recovery_type > NFS_LOCK_NEW) {
6175 if (recovery_type == NFS_LOCK_RECLAIM)
6176 data->arg.reclaim = NFS_LOCK_RECLAIM;
6177 nfs4_set_sequence_privileged(&data->arg.seq_args);
c69899a1
TM
6178 } else
6179 data->arg.new_lock = 1;
c970aa85 6180 task = rpc_run_task(&task_setup_data);
7a1218a2 6181 if (IS_ERR(task))
a5d16a4d 6182 return PTR_ERR(task);
a5d16a4d
TM
6183 ret = nfs4_wait_for_completion_rpc_task(task);
6184 if (ret == 0) {
6185 ret = data->rpc_status;
2bee72a6
TM
6186 if (ret)
6187 nfs4_handle_setlk_error(data->server, data->lsp,
6188 data->arg.new_lock_owner, ret);
a5d16a4d
TM
6189 } else
6190 data->cancelled = 1;
e6b3c4db 6191 rpc_put_task(task);
3110ff80 6192 dprintk("%s: done, ret = %d!\n", __func__, ret);
48c9579a 6193 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
a5d16a4d 6194 return ret;
1da177e4
LT
6195}
6196
6197static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6198{
202b50dc 6199 struct nfs_server *server = NFS_SERVER(state->inode);
05ffe24f
TM
6200 struct nfs4_exception exception = {
6201 .inode = state->inode,
6202 };
202b50dc
TM
6203 int err;
6204
6205 do {
42a2d13e
TM
6206 /* Cache the lock if possible... */
6207 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6208 return 0;
afe6c27c 6209 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
168667c4 6210 if (err != -NFS4ERR_DELAY)
202b50dc
TM
6211 break;
6212 nfs4_handle_exception(server, err, &exception);
6213 } while (exception.retry);
6214 return err;
1da177e4
LT
6215}
6216
6217static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6218{
202b50dc 6219 struct nfs_server *server = NFS_SERVER(state->inode);
05ffe24f
TM
6220 struct nfs4_exception exception = {
6221 .inode = state->inode,
6222 };
202b50dc
TM
6223 int err;
6224
6bfc93ef
TM
6225 err = nfs4_set_lock_state(state, request);
6226 if (err != 0)
6227 return err;
f6de7a39 6228 if (!recover_lost_locks) {
ef1820f9
N
6229 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6230 return 0;
6231 }
202b50dc 6232 do {
42a2d13e
TM
6233 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6234 return 0;
afe6c27c 6235 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
a9ed2e25
TM
6236 switch (err) {
6237 default:
6238 goto out;
6239 case -NFS4ERR_GRACE:
6240 case -NFS4ERR_DELAY:
6241 nfs4_handle_exception(server, err, &exception);
6242 err = 0;
6243 }
202b50dc 6244 } while (exception.retry);
a9ed2e25 6245out:
202b50dc 6246 return err;
1da177e4
LT
6247}
6248
f062eb6c 6249#if defined(CONFIG_NFS_V4_1)
b01dd1d8
BS
6250static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6251{
c5896fc8
TM
6252 struct nfs4_lock_state *lsp;
6253 int status;
b01dd1d8 6254
c5896fc8
TM
6255 status = nfs4_set_lock_state(state, request);
6256 if (status != 0)
6257 return status;
6258 lsp = request->fl_u.nfs4_fl.owner;
6259 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6260 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6261 return 0;
6262 status = nfs4_lock_expired(state, request);
eb64cf96 6263 return status;
f062eb6c
BS
6264}
6265#endif
6266
1da177e4
LT
6267static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6268{
19e03c57 6269 struct nfs_inode *nfsi = NFS_I(state->inode);
11476e9d 6270 struct nfs4_state_owner *sp = state->owner;
01c3b861 6271 unsigned char fl_flags = request->fl_flags;
1ea67dbd 6272 int status;
1da177e4 6273
01c3b861 6274 request->fl_flags |= FL_ACCESS;
75575ddf 6275 status = locks_lock_inode_wait(state->inode, request);
01c3b861
TM
6276 if (status < 0)
6277 goto out;
11476e9d 6278 mutex_lock(&sp->so_delegreturn_mutex);
19e03c57 6279 down_read(&nfsi->rwsem);
01c3b861 6280 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
01c3b861 6281 /* Yes: cache locks! */
01c3b861 6282 /* ...but avoid races with delegation recall... */
19e03c57 6283 request->fl_flags = fl_flags & ~FL_SLEEP;
75575ddf 6284 status = locks_lock_inode_wait(state->inode, request);
c69899a1 6285 up_read(&nfsi->rwsem);
11476e9d 6286 mutex_unlock(&sp->so_delegreturn_mutex);
9a99af49 6287 goto out;
9a99af49 6288 }
19e03c57 6289 up_read(&nfsi->rwsem);
11476e9d 6290 mutex_unlock(&sp->so_delegreturn_mutex);
c69899a1 6291 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
01c3b861
TM
6292out:
6293 request->fl_flags = fl_flags;
1da177e4
LT
6294 return status;
6295}
6296
6297static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6298{
a1d0b5ee
TM
6299 struct nfs4_exception exception = {
6300 .state = state,
05ffe24f 6301 .inode = state->inode,
a1d0b5ee 6302 };
1da177e4
LT
6303 int err;
6304
6305 do {
965b5d67
TM
6306 err = _nfs4_proc_setlk(state, cmd, request);
6307 if (err == -NFS4ERR_DENIED)
6308 err = -EAGAIN;
1da177e4 6309 err = nfs4_handle_exception(NFS_SERVER(state->inode),
965b5d67 6310 err, &exception);
1da177e4
LT
6311 } while (exception.retry);
6312 return err;
6313}
6314
d2f3a7f9
JL
6315#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6316#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6317
6318static int
a1d617d8
JL
6319nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6320 struct file_lock *request)
d2f3a7f9
JL
6321{
6322 int status = -ERESTARTSYS;
6323 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6324
6325 while(!signalled()) {
6326 status = nfs4_proc_setlk(state, cmd, request);
6327 if ((status != -EAGAIN) || IS_SETLK(cmd))
6328 break;
6329 freezable_schedule_timeout_interruptible(timeout);
6330 timeout *= 2;
6331 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6332 status = -ERESTARTSYS;
6333 }
6334 return status;
6335}
6336
a1d617d8
JL
6337#ifdef CONFIG_NFS_V4_1
6338struct nfs4_lock_waiter {
6339 struct task_struct *task;
6340 struct inode *inode;
6341 struct nfs_lowner *owner;
6342 bool notified;
6343};
6344
6345static int
6346nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6347{
6348 int ret;
6349 struct cb_notify_lock_args *cbnl = key;
6350 struct nfs4_lock_waiter *waiter = wait->private;
6351 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6352 *wowner = waiter->owner;
6353
6354 /* Only wake if the callback was for the same owner */
6355 if (lowner->clientid != wowner->clientid ||
6356 lowner->id != wowner->id ||
6357 lowner->s_dev != wowner->s_dev)
6358 return 0;
6359
6360 /* Make sure it's for the right inode */
6361 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6362 return 0;
6363
6364 waiter->notified = true;
6365
6366 /* override "private" so we can use default_wake_function */
6367 wait->private = waiter->task;
6368 ret = autoremove_wake_function(wait, mode, flags, key);
6369 wait->private = waiter;
6370 return ret;
6371}
6372
6373static int
6374nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6375{
6376 int status = -ERESTARTSYS;
6377 unsigned long flags;
6378 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6379 struct nfs_server *server = NFS_SERVER(state->inode);
6380 struct nfs_client *clp = server->nfs_client;
6381 wait_queue_head_t *q = &clp->cl_lock_waitq;
6382 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6383 .id = lsp->ls_seqid.owner_id,
6384 .s_dev = server->s_dev };
6385 struct nfs4_lock_waiter waiter = { .task = current,
6386 .inode = state->inode,
6387 .owner = &owner,
6388 .notified = false };
6389 wait_queue_t wait;
6390
6391 /* Don't bother with waitqueue if we don't expect a callback */
6392 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6393 return nfs4_retry_setlk_simple(state, cmd, request);
6394
6395 init_wait(&wait);
6396 wait.private = &waiter;
6397 wait.func = nfs4_wake_lock_waiter;
6398 add_wait_queue(q, &wait);
6399
6400 while(!signalled()) {
6401 status = nfs4_proc_setlk(state, cmd, request);
6402 if ((status != -EAGAIN) || IS_SETLK(cmd))
6403 break;
6404
6405 status = -ERESTARTSYS;
6406 spin_lock_irqsave(&q->lock, flags);
6407 if (waiter.notified) {
6408 spin_unlock_irqrestore(&q->lock, flags);
6409 continue;
6410 }
6411 set_current_state(TASK_INTERRUPTIBLE);
6412 spin_unlock_irqrestore(&q->lock, flags);
6413
6414 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6415 }
6416
6417 finish_wait(q, &wait);
6418 return status;
6419}
6420#else /* !CONFIG_NFS_V4_1 */
6421static inline int
6422nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6423{
6424 return nfs4_retry_setlk_simple(state, cmd, request);
6425}
6426#endif
6427
1da177e4
LT
6428static int
6429nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6430{
6431 struct nfs_open_context *ctx;
6432 struct nfs4_state *state;
1da177e4
LT
6433 int status;
6434
6435 /* verify open state */
cd3758e3 6436 ctx = nfs_file_open_context(filp);
1da177e4
LT
6437 state = ctx->state;
6438
6439 if (request->fl_start < 0 || request->fl_end < 0)
6440 return -EINVAL;
6441
d953126a
TM
6442 if (IS_GETLK(cmd)) {
6443 if (state != NULL)
6444 return nfs4_proc_getlk(state, F_GETLK, request);
6445 return 0;
6446 }
1da177e4
LT
6447
6448 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6449 return -EINVAL;
6450
d953126a
TM
6451 if (request->fl_type == F_UNLCK) {
6452 if (state != NULL)
6453 return nfs4_proc_unlck(state, cmd, request);
6454 return 0;
6455 }
1da177e4 6456
d953126a
TM
6457 if (state == NULL)
6458 return -ENOLCK;
1ea67dbd
JL
6459
6460 if ((request->fl_flags & FL_POSIX) &&
6461 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6462 return -ENOLCK;
6463
55725513
TM
6464 /*
6465 * Don't rely on the VFS having checked the file open mode,
6466 * since it won't do this for flock() locks.
6467 */
f44106e2 6468 switch (request->fl_type) {
55725513
TM
6469 case F_RDLCK:
6470 if (!(filp->f_mode & FMODE_READ))
6471 return -EBADF;
6472 break;
6473 case F_WRLCK:
6474 if (!(filp->f_mode & FMODE_WRITE))
6475 return -EBADF;
6476 }
6477
1ea67dbd
JL
6478 status = nfs4_set_lock_state(state, request);
6479 if (status != 0)
6480 return status;
6481
d2f3a7f9 6482 return nfs4_retry_setlk(state, cmd, request);
1da177e4
LT
6483}
6484
db4f2e63 6485int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
888e694c
TM
6486{
6487 struct nfs_server *server = NFS_SERVER(state->inode);
888e694c
TM
6488 int err;
6489
6490 err = nfs4_set_lock_state(state, fl);
6491 if (err != 0)
db4f2e63 6492 return err;
4a706fa0 6493 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
db4f2e63 6494 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
888e694c 6495}
6b3b5496 6496
cf470c3e
TM
6497struct nfs_release_lockowner_data {
6498 struct nfs4_lock_state *lsp;
5ae67c4f 6499 struct nfs_server *server;
cf470c3e 6500 struct nfs_release_lockowner_args args;
b7e63a10 6501 struct nfs_release_lockowner_res res;
60ea6812 6502 unsigned long timestamp;
cf470c3e
TM
6503};
6504
fbd4bfd1
CL
6505static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6506{
6507 struct nfs_release_lockowner_data *data = calldata;
5b53dc88 6508 struct nfs_server *server = data->server;
cb04ad2a
PT
6509 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6510 &data->args.seq_args, &data->res.seq_res, task);
5b53dc88 6511 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
60ea6812 6512 data->timestamp = jiffies;
fbd4bfd1
CL
6513}
6514
6515static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6516{
6517 struct nfs_release_lockowner_data *data = calldata;
60ea6812
CL
6518 struct nfs_server *server = data->server;
6519
b7e63a10 6520 nfs40_sequence_done(task, &data->res.seq_res);
60ea6812
CL
6521
6522 switch (task->tk_status) {
6523 case 0:
6524 renew_lease(server, data->timestamp);
6525 break;
6526 case -NFS4ERR_STALE_CLIENTID:
6527 case -NFS4ERR_EXPIRED:
5b53dc88
KM
6528 nfs4_schedule_lease_recovery(server->nfs_client);
6529 break;
60ea6812
CL
6530 case -NFS4ERR_LEASE_MOVED:
6531 case -NFS4ERR_DELAY:
8478eaa1
N
6532 if (nfs4_async_handle_error(task, server,
6533 NULL, NULL) == -EAGAIN)
60ea6812
CL
6534 rpc_restart_call_prepare(task);
6535 }
fbd4bfd1
CL
6536}
6537
d3c7b7cc
TM
6538static void nfs4_release_lockowner_release(void *calldata)
6539{
cf470c3e 6540 struct nfs_release_lockowner_data *data = calldata;
5ae67c4f 6541 nfs4_free_lock_state(data->server, data->lsp);
d3c7b7cc
TM
6542 kfree(calldata);
6543}
6544
17280175 6545static const struct rpc_call_ops nfs4_release_lockowner_ops = {
fbd4bfd1
CL
6546 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6547 .rpc_call_done = nfs4_release_lockowner_done,
d3c7b7cc
TM
6548 .rpc_release = nfs4_release_lockowner_release,
6549};
6550
f1cdae87
JL
6551static void
6552nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
d3c7b7cc 6553{
cf470c3e 6554 struct nfs_release_lockowner_data *data;
d3c7b7cc
TM
6555 struct rpc_message msg = {
6556 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6557 };
6558
6559 if (server->nfs_client->cl_mvops->minor_version != 0)
f1cdae87 6560 return;
fbd4bfd1 6561
cf470c3e
TM
6562 data = kmalloc(sizeof(*data), GFP_NOFS);
6563 if (!data)
f1cdae87 6564 return;
cf470c3e 6565 data->lsp = lsp;
5ae67c4f 6566 data->server = server;
cf470c3e
TM
6567 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6568 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6569 data->args.lock_owner.s_dev = server->s_dev;
fbd4bfd1 6570
cf470c3e 6571 msg.rpc_argp = &data->args;
b7e63a10
TM
6572 msg.rpc_resp = &data->res;
6573 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
cf470c3e 6574 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
d3c7b7cc
TM
6575}
6576
aa1870af
BF
6577#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6578
d9a82a04 6579static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
59301226
AV
6580 struct dentry *unused, struct inode *inode,
6581 const char *key, const void *buf,
6582 size_t buflen, int flags)
6b3b5496 6583{
59301226 6584 return nfs4_proc_set_acl(inode, buf, buflen);
6b3b5496
BF
6585}
6586
d9a82a04 6587static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
b296821a
AV
6588 struct dentry *unused, struct inode *inode,
6589 const char *key, void *buf, size_t buflen)
6b3b5496 6590{
b296821a 6591 return nfs4_proc_get_acl(inode, buf, buflen);
6b3b5496
BF
6592}
6593
764a5c6b 6594static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6b3b5496 6595{
764a5c6b 6596 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6b3b5496
BF
6597}
6598
c9bccef6 6599#ifdef CONFIG_NFS_V4_SECURITY_LABEL
c9bccef6 6600
d9a82a04 6601static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
59301226
AV
6602 struct dentry *unused, struct inode *inode,
6603 const char *key, const void *buf,
6604 size_t buflen, int flags)
c9bccef6
DQ
6605{
6606 if (security_ismaclabel(key))
59301226 6607 return nfs4_set_security_label(inode, buf, buflen);
c9bccef6
DQ
6608
6609 return -EOPNOTSUPP;
6610}
6611
d9a82a04 6612static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
b296821a
AV
6613 struct dentry *unused, struct inode *inode,
6614 const char *key, void *buf, size_t buflen)
c9bccef6
DQ
6615{
6616 if (security_ismaclabel(key))
b296821a 6617 return nfs4_get_security_label(inode, buf, buflen);
c9bccef6
DQ
6618 return -EOPNOTSUPP;
6619}
6620
c4803c49
AG
6621static ssize_t
6622nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
c9bccef6 6623{
c4803c49 6624 int len = 0;
c9bccef6 6625
c4803c49
AG
6626 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6627 len = security_inode_listsecurity(inode, list, list_len);
6628 if (list_len && len > list_len)
6629 return -ERANGE;
c9bccef6
DQ
6630 }
6631 return len;
6632}
6633
6634static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6635 .prefix = XATTR_SECURITY_PREFIX,
c9bccef6
DQ
6636 .get = nfs4_xattr_get_nfs4_label,
6637 .set = nfs4_xattr_set_nfs4_label,
6638};
c9bccef6 6639
c4803c49
AG
6640#else
6641
6642static ssize_t
6643nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6644{
6645 return 0;
6646}
6647
6648#endif
c9bccef6 6649
533eb461
AA
6650/*
6651 * nfs_fhget will use either the mounted_on_fileid or the fileid
6652 */
69aaaae1
TM
6653static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6654{
533eb461
AA
6655 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6656 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6657 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
81934ddb 6658 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
69aaaae1
TM
6659 return;
6660
6661 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
81934ddb 6662 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
69aaaae1
TM
6663 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6664 fattr->nlink = 2;
6665}
6666
f05d147f
BS
6667static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6668 const struct qstr *name,
6669 struct nfs4_fs_locations *fs_locations,
6670 struct page *page)
683b57b4
TM
6671{
6672 struct nfs_server *server = NFS_SERVER(dir);
a09df2ca 6673 u32 bitmask[3] = {
361e624f 6674 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
683b57b4
TM
6675 };
6676 struct nfs4_fs_locations_arg args = {
6677 .dir_fh = NFS_FH(dir),
c228fd3a 6678 .name = name,
683b57b4
TM
6679 .page = page,
6680 .bitmask = bitmask,
6681 };
22958463
BH
6682 struct nfs4_fs_locations_res res = {
6683 .fs_locations = fs_locations,
6684 };
683b57b4
TM
6685 struct rpc_message msg = {
6686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6687 .rpc_argp = &args,
22958463 6688 .rpc_resp = &res,
683b57b4
TM
6689 };
6690 int status;
6691
3110ff80 6692 dprintk("%s: start\n", __func__);
533eb461
AA
6693
6694 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6695 * is not supported */
6696 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6697 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6698 else
6699 bitmask[0] |= FATTR4_WORD0_FILEID;
6700
c228fd3a 6701 nfs_fattr_init(&fs_locations->fattr);
683b57b4 6702 fs_locations->server = server;
830b8e33 6703 fs_locations->nlocations = 0;
f05d147f 6704 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
3110ff80 6705 dprintk("%s: returned status = %d\n", __func__, status);
683b57b4
TM
6706 return status;
6707}
6708
f05d147f
BS
6709int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6710 const struct qstr *name,
6711 struct nfs4_fs_locations *fs_locations,
6712 struct page *page)
db0a9593
BS
6713{
6714 struct nfs4_exception exception = { };
6715 int err;
6716 do {
078ea3df
TM
6717 err = _nfs4_proc_fs_locations(client, dir, name,
6718 fs_locations, page);
6719 trace_nfs4_get_fs_locations(dir, name, err);
6720 err = nfs4_handle_exception(NFS_SERVER(dir), err,
db0a9593
BS
6721 &exception);
6722 } while (exception.retry);
6723 return err;
6724}
6725
b03d735b
CL
6726/*
6727 * This operation also signals the server that this client is
6728 * performing migration recovery. The server can stop returning
6729 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6730 * appended to this compound to identify the client ID which is
6731 * performing recovery.
6732 */
6733static int _nfs40_proc_get_locations(struct inode *inode,
6734 struct nfs4_fs_locations *locations,
6735 struct page *page, struct rpc_cred *cred)
6736{
6737 struct nfs_server *server = NFS_SERVER(inode);
6738 struct rpc_clnt *clnt = server->client;
6739 u32 bitmask[2] = {
6740 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6741 };
6742 struct nfs4_fs_locations_arg args = {
6743 .clientid = server->nfs_client->cl_clientid,
6744 .fh = NFS_FH(inode),
6745 .page = page,
6746 .bitmask = bitmask,
6747 .migration = 1, /* skip LOOKUP */
6748 .renew = 1, /* append RENEW */
6749 };
6750 struct nfs4_fs_locations_res res = {
6751 .fs_locations = locations,
6752 .migration = 1,
6753 .renew = 1,
6754 };
6755 struct rpc_message msg = {
6756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6757 .rpc_argp = &args,
6758 .rpc_resp = &res,
6759 .rpc_cred = cred,
6760 };
6761 unsigned long now = jiffies;
6762 int status;
6763
6764 nfs_fattr_init(&locations->fattr);
6765 locations->server = server;
6766 locations->nlocations = 0;
6767
6768 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6769 nfs4_set_sequence_privileged(&args.seq_args);
6770 status = nfs4_call_sync_sequence(clnt, server, &msg,
6771 &args.seq_args, &res.seq_res);
6772 if (status)
6773 return status;
6774
6775 renew_lease(server, now);
6776 return 0;
6777}
6778
6779#ifdef CONFIG_NFS_V4_1
6780
6781/*
6782 * This operation also signals the server that this client is
6783 * performing migration recovery. The server can stop asserting
6784 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6785 * performing this operation is identified in the SEQUENCE
6786 * operation in this compound.
6787 *
6788 * When the client supports GETATTR(fs_locations_info), it can
6789 * be plumbed in here.
6790 */
6791static int _nfs41_proc_get_locations(struct inode *inode,
6792 struct nfs4_fs_locations *locations,
6793 struct page *page, struct rpc_cred *cred)
6794{
6795 struct nfs_server *server = NFS_SERVER(inode);
6796 struct rpc_clnt *clnt = server->client;
6797 u32 bitmask[2] = {
6798 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6799 };
6800 struct nfs4_fs_locations_arg args = {
6801 .fh = NFS_FH(inode),
6802 .page = page,
6803 .bitmask = bitmask,
6804 .migration = 1, /* skip LOOKUP */
6805 };
6806 struct nfs4_fs_locations_res res = {
6807 .fs_locations = locations,
6808 .migration = 1,
6809 };
6810 struct rpc_message msg = {
6811 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6812 .rpc_argp = &args,
6813 .rpc_resp = &res,
6814 .rpc_cred = cred,
6815 };
6816 int status;
6817
6818 nfs_fattr_init(&locations->fattr);
6819 locations->server = server;
6820 locations->nlocations = 0;
6821
6822 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6823 nfs4_set_sequence_privileged(&args.seq_args);
6824 status = nfs4_call_sync_sequence(clnt, server, &msg,
6825 &args.seq_args, &res.seq_res);
6826 if (status == NFS4_OK &&
6827 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6828 status = -NFS4ERR_LEASE_MOVED;
6829 return status;
6830}
6831
6832#endif /* CONFIG_NFS_V4_1 */
6833
6834/**
6835 * nfs4_proc_get_locations - discover locations for a migrated FSID
6836 * @inode: inode on FSID that is migrating
6837 * @locations: result of query
6838 * @page: buffer
6839 * @cred: credential to use for this operation
6840 *
6841 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6842 * operation failed, or a negative errno if a local error occurred.
6843 *
6844 * On success, "locations" is filled in, but if the server has
6845 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6846 * asserted.
6847 *
6848 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6849 * from this client that require migration recovery.
6850 */
6851int nfs4_proc_get_locations(struct inode *inode,
6852 struct nfs4_fs_locations *locations,
6853 struct page *page, struct rpc_cred *cred)
6854{
6855 struct nfs_server *server = NFS_SERVER(inode);
6856 struct nfs_client *clp = server->nfs_client;
6857 const struct nfs4_mig_recovery_ops *ops =
6858 clp->cl_mvops->mig_recovery_ops;
6859 struct nfs4_exception exception = { };
6860 int status;
6861
6862 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6863 (unsigned long long)server->fsid.major,
6864 (unsigned long long)server->fsid.minor,
6865 clp->cl_hostname);
6866 nfs_display_fhandle(NFS_FH(inode), __func__);
6867
6868 do {
6869 status = ops->get_locations(inode, locations, page, cred);
6870 if (status != -NFS4ERR_DELAY)
6871 break;
6872 nfs4_handle_exception(server, status, &exception);
6873 } while (exception.retry);
6874 return status;
6875}
6876
44c99933
CL
6877/*
6878 * This operation also signals the server that this client is
6879 * performing "lease moved" recovery. The server can stop
6880 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6881 * is appended to this compound to identify the client ID which is
6882 * performing recovery.
6883 */
6884static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6885{
6886 struct nfs_server *server = NFS_SERVER(inode);
6887 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6888 struct rpc_clnt *clnt = server->client;
6889 struct nfs4_fsid_present_arg args = {
6890 .fh = NFS_FH(inode),
6891 .clientid = clp->cl_clientid,
6892 .renew = 1, /* append RENEW */
6893 };
6894 struct nfs4_fsid_present_res res = {
6895 .renew = 1,
6896 };
6897 struct rpc_message msg = {
6898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6899 .rpc_argp = &args,
6900 .rpc_resp = &res,
6901 .rpc_cred = cred,
6902 };
6903 unsigned long now = jiffies;
6904 int status;
6905
6906 res.fh = nfs_alloc_fhandle();
6907 if (res.fh == NULL)
6908 return -ENOMEM;
6909
6910 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6911 nfs4_set_sequence_privileged(&args.seq_args);
6912 status = nfs4_call_sync_sequence(clnt, server, &msg,
6913 &args.seq_args, &res.seq_res);
6914 nfs_free_fhandle(res.fh);
6915 if (status)
6916 return status;
6917
6918 do_renew_lease(clp, now);
6919 return 0;
6920}
6921
6922#ifdef CONFIG_NFS_V4_1
6923
6924/*
6925 * This operation also signals the server that this client is
6926 * performing "lease moved" recovery. The server can stop asserting
6927 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6928 * this operation is identified in the SEQUENCE operation in this
6929 * compound.
6930 */
6931static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6932{
6933 struct nfs_server *server = NFS_SERVER(inode);
6934 struct rpc_clnt *clnt = server->client;
6935 struct nfs4_fsid_present_arg args = {
6936 .fh = NFS_FH(inode),
6937 };
6938 struct nfs4_fsid_present_res res = {
6939 };
6940 struct rpc_message msg = {
6941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6942 .rpc_argp = &args,
6943 .rpc_resp = &res,
6944 .rpc_cred = cred,
6945 };
6946 int status;
6947
6948 res.fh = nfs_alloc_fhandle();
6949 if (res.fh == NULL)
6950 return -ENOMEM;
6951
6952 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6953 nfs4_set_sequence_privileged(&args.seq_args);
6954 status = nfs4_call_sync_sequence(clnt, server, &msg,
6955 &args.seq_args, &res.seq_res);
6956 nfs_free_fhandle(res.fh);
6957 if (status == NFS4_OK &&
6958 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6959 status = -NFS4ERR_LEASE_MOVED;
6960 return status;
6961}
6962
6963#endif /* CONFIG_NFS_V4_1 */
6964
6965/**
6966 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6967 * @inode: inode on FSID to check
6968 * @cred: credential to use for this operation
6969 *
6970 * Server indicates whether the FSID is present, moved, or not
6971 * recognized. This operation is necessary to clear a LEASE_MOVED
6972 * condition for this client ID.
6973 *
6974 * Returns NFS4_OK if the FSID is present on this server,
6975 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6976 * NFS4ERR code if some error occurred on the server, or a
6977 * negative errno if a local failure occurred.
6978 */
6979int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6980{
6981 struct nfs_server *server = NFS_SERVER(inode);
6982 struct nfs_client *clp = server->nfs_client;
6983 const struct nfs4_mig_recovery_ops *ops =
6984 clp->cl_mvops->mig_recovery_ops;
6985 struct nfs4_exception exception = { };
6986 int status;
6987
6988 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6989 (unsigned long long)server->fsid.major,
6990 (unsigned long long)server->fsid.minor,
6991 clp->cl_hostname);
6992 nfs_display_fhandle(NFS_FH(inode), __func__);
6993
6994 do {
6995 status = ops->fsid_present(inode, cred);
6996 if (status != -NFS4ERR_DELAY)
6997 break;
6998 nfs4_handle_exception(server, status, &exception);
6999 } while (exception.retry);
7000 return status;
7001}
7002
5ec16a85 7003/**
a5250def
WAA
7004 * If 'use_integrity' is true and the state managment nfs_client
7005 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7006 * and the machine credential as per RFC3530bis and RFC5661 Security
7007 * Considerations sections. Otherwise, just use the user cred with the
7008 * filesystem's rpc_client.
5ec16a85 7009 */
a5250def 7010static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
5a5ea0d4
BS
7011{
7012 int status;
7013 struct nfs4_secinfo_arg args = {
7014 .dir_fh = NFS_FH(dir),
7015 .name = name,
7016 };
7017 struct nfs4_secinfo_res res = {
7018 .flavors = flavors,
7019 };
7020 struct rpc_message msg = {
7021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7022 .rpc_argp = &args,
7023 .rpc_resp = &res,
7024 };
a5250def 7025 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7cb852df 7026 struct rpc_cred *cred = NULL;
a5250def
WAA
7027
7028 if (use_integrity) {
7029 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7cb852df
WAA
7030 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7031 msg.rpc_cred = cred;
a5250def 7032 }
5a5ea0d4
BS
7033
7034 dprintk("NFS call secinfo %s\n", name->name);
8b5bee2e
WAA
7035
7036 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7037 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7038
a5250def
WAA
7039 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7040 &res.seq_res, 0);
5a5ea0d4 7041 dprintk("NFS reply secinfo: %d\n", status);
a5250def 7042
7cb852df
WAA
7043 if (cred)
7044 put_rpccred(cred);
a5250def 7045
5a5ea0d4
BS
7046 return status;
7047}
7048
72de53ec
BS
7049int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7050 struct nfs4_secinfo_flavors *flavors)
5a5ea0d4
BS
7051{
7052 struct nfs4_exception exception = { };
7053 int err;
7054 do {
a5250def
WAA
7055 err = -NFS4ERR_WRONGSEC;
7056
7057 /* try to use integrity protection with machine cred */
7058 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7059 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7060
7061 /*
7062 * if unable to use integrity protection, or SECINFO with
7063 * integrity protection returns NFS4ERR_WRONGSEC (which is
7064 * disallowed by spec, but exists in deployed servers) use
7065 * the current filesystem's rpc_client and the user cred.
7066 */
7067 if (err == -NFS4ERR_WRONGSEC)
7068 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7069
078ea3df
TM
7070 trace_nfs4_secinfo(dir, name, err);
7071 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5a5ea0d4
BS
7072 &exception);
7073 } while (exception.retry);
7074 return err;
7075}
7076
557134a3 7077#ifdef CONFIG_NFS_V4_1
357f54d6
AA
7078/*
7079 * Check the exchange flags returned by the server for invalid flags, having
7080 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7081 * DS flags set.
7082 */
7083static int nfs4_check_cl_exchange_flags(u32 flags)
7084{
7085 if (flags & ~EXCHGID4_FLAG_MASK_R)
7086 goto out_inval;
7087 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7088 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7089 goto out_inval;
7090 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7091 goto out_inval;
7092 return NFS_OK;
7093out_inval:
7094 return -NFS4ERR_INVAL;
7095}
7096
78fe0f41 7097static bool
79d4e1f0
CL
7098nfs41_same_server_scope(struct nfs41_server_scope *a,
7099 struct nfs41_server_scope *b)
78fe0f41
WAA
7100{
7101 if (a->server_scope_sz == b->server_scope_sz &&
7102 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7103 return true;
7104
7105 return false;
7106}
7107
02a95dee
AA
7108static void
7109nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7110{
7111}
7112
7113static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7114 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7115};
7116
7c44f1ae 7117/*
d9ddbf5d 7118 * nfs4_proc_bind_one_conn_to_session()
7c44f1ae
WAA
7119 *
7120 * The 4.1 client currently uses the same TCP connection for the
7121 * fore and backchannel.
7122 */
d9ddbf5d
TM
7123static
7124int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7125 struct rpc_xprt *xprt,
7126 struct nfs_client *clp,
7127 struct rpc_cred *cred)
7c44f1ae
WAA
7128{
7129 int status;
71a097c6
TM
7130 struct nfs41_bind_conn_to_session_args args = {
7131 .client = clp,
7132 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7133 };
7c44f1ae
WAA
7134 struct nfs41_bind_conn_to_session_res res;
7135 struct rpc_message msg = {
7136 .rpc_proc =
7137 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
71a097c6 7138 .rpc_argp = &args,
7c44f1ae 7139 .rpc_resp = &res,
2cf047c9 7140 .rpc_cred = cred,
7c44f1ae 7141 };
d9ddbf5d
TM
7142 struct rpc_task_setup task_setup_data = {
7143 .rpc_client = clnt,
7144 .rpc_xprt = xprt,
02a95dee 7145 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
d9ddbf5d
TM
7146 .rpc_message = &msg,
7147 .flags = RPC_TASK_TIMEOUT,
7148 };
7149 struct rpc_task *task;
7c44f1ae
WAA
7150
7151 dprintk("--> %s\n", __func__);
7c44f1ae 7152
71a097c6
TM
7153 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7154 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7155 args.dir = NFS4_CDFC4_FORE;
7c44f1ae 7156
d9ddbf5d
TM
7157 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7158 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7159 args.dir = NFS4_CDFC4_FORE;
7160
7161 task = rpc_run_task(&task_setup_data);
7162 if (!IS_ERR(task)) {
7163 status = task->tk_status;
7164 rpc_put_task(task);
7165 } else
7166 status = PTR_ERR(task);
c6d01c6f 7167 trace_nfs4_bind_conn_to_session(clp, status);
7c44f1ae 7168 if (status == 0) {
71a097c6 7169 if (memcmp(res.sessionid.data,
7c44f1ae
WAA
7170 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7171 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7172 status = -EIO;
71a097c6 7173 goto out;
7c44f1ae 7174 }
71a097c6 7175 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7c44f1ae
WAA
7176 dprintk("NFS: %s: Unexpected direction from server\n",
7177 __func__);
7178 status = -EIO;
71a097c6 7179 goto out;
7c44f1ae 7180 }
71a097c6 7181 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7c44f1ae
WAA
7182 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7183 __func__);
7184 status = -EIO;
71a097c6 7185 goto out;
7c44f1ae
WAA
7186 }
7187 }
7c44f1ae
WAA
7188out:
7189 dprintk("<-- %s status= %d\n", __func__, status);
7190 return status;
7191}
7192
d9ddbf5d
TM
7193struct rpc_bind_conn_calldata {
7194 struct nfs_client *clp;
7195 struct rpc_cred *cred;
7196};
7197
7198static int
7199nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7200 struct rpc_xprt *xprt,
7201 void *calldata)
7202{
7203 struct rpc_bind_conn_calldata *p = calldata;
7204
7205 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7206}
7207
7208int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7209{
7210 struct rpc_bind_conn_calldata data = {
7211 .clp = clp,
7212 .cred = cred,
7213 };
7214 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7215 nfs4_proc_bind_conn_to_session_callback, &data);
7216}
7217
99fe60d0 7218/*
fa940720
WAA
7219 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7220 * and operations we'd like to see to enable certain features in the allow map
2031cd1a
WAA
7221 */
7222static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7223 .how = SP4_MACH_CRED,
7224 .enforce.u.words = {
7225 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7226 1 << (OP_EXCHANGE_ID - 32) |
7227 1 << (OP_CREATE_SESSION - 32) |
7228 1 << (OP_DESTROY_SESSION - 32) |
7229 1 << (OP_DESTROY_CLIENTID - 32)
fa940720
WAA
7230 },
7231 .allow.u.words = {
7232 [0] = 1 << (OP_CLOSE) |
99ade3c7 7233 1 << (OP_OPEN_DOWNGRADE) |
a0279625 7234 1 << (OP_LOCKU) |
99ade3c7 7235 1 << (OP_DELEGRETURN) |
a0279625 7236 1 << (OP_COMMIT),
8b5bee2e 7237 [1] = 1 << (OP_SECINFO - 32) |
3787d506 7238 1 << (OP_SECINFO_NO_NAME - 32) |
99ade3c7 7239 1 << (OP_LAYOUTRETURN - 32) |
3787d506 7240 1 << (OP_TEST_STATEID - 32) |
a0279625
WAA
7241 1 << (OP_FREE_STATEID - 32) |
7242 1 << (OP_WRITE - 32)
2031cd1a
WAA
7243 }
7244};
7245
7246/*
7247 * Select the state protection mode for client `clp' given the server results
7248 * from exchange_id in `sp'.
99fe60d0 7249 *
2031cd1a
WAA
7250 * Returns 0 on success, negative errno otherwise.
7251 */
7252static int nfs4_sp4_select_mode(struct nfs_client *clp,
7253 struct nfs41_state_protection *sp)
7254{
7255 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7256 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7257 1 << (OP_EXCHANGE_ID - 32) |
7258 1 << (OP_CREATE_SESSION - 32) |
7259 1 << (OP_DESTROY_SESSION - 32) |
7260 1 << (OP_DESTROY_CLIENTID - 32)
7261 };
7262 unsigned int i;
7263
7264 if (sp->how == SP4_MACH_CRED) {
7265 /* Print state protect result */
7266 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7267 for (i = 0; i <= LAST_NFS4_OP; i++) {
7268 if (test_bit(i, sp->enforce.u.longs))
7269 dfprintk(MOUNT, " enforce op %d\n", i);
7270 if (test_bit(i, sp->allow.u.longs))
7271 dfprintk(MOUNT, " allow op %d\n", i);
7272 }
7273
7274 /* make sure nothing is on enforce list that isn't supported */
7275 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7276 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7277 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7278 return -EINVAL;
7279 }
7280 }
7281
7282 /*
7283 * Minimal mode - state operations are allowed to use machine
7284 * credential. Note this already happens by default, so the
7285 * client doesn't have to do anything more than the negotiation.
7286 *
7287 * NOTE: we don't care if EXCHANGE_ID is in the list -
7288 * we're already using the machine cred for exchange_id
7289 * and will never use a different cred.
7290 */
7291 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7292 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7293 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7294 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7295 dfprintk(MOUNT, "sp4_mach_cred:\n");
7296 dfprintk(MOUNT, " minimal mode enabled\n");
7297 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7298 } else {
7299 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7300 return -EINVAL;
7301 }
fa940720
WAA
7302
7303 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
99ade3c7
AE
7304 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7305 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
fa940720
WAA
7306 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7307 dfprintk(MOUNT, " cleanup mode enabled\n");
7308 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7309 }
8b5bee2e 7310
99ade3c7
AE
7311 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7312 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7313 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7314 &clp->cl_sp4_flags);
7315 }
7316
8b5bee2e
WAA
7317 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7318 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7319 dfprintk(MOUNT, " secinfo mode enabled\n");
7320 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7321 }
3787d506
WAA
7322
7323 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7324 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7325 dfprintk(MOUNT, " stateid mode enabled\n");
7326 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7327 }
8c21c62c
WAA
7328
7329 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7330 dfprintk(MOUNT, " write mode enabled\n");
7331 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7332 }
7333
7334 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7335 dfprintk(MOUNT, " commit mode enabled\n");
7336 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7337 }
2031cd1a
WAA
7338 }
7339
7340 return 0;
7341}
7342
8d89bd70
AA
7343struct nfs41_exchange_id_data {
7344 struct nfs41_exchange_id_res res;
7345 struct nfs41_exchange_id_args args;
ad0849a7 7346 struct rpc_xprt *xprt;
8d89bd70
AA
7347 int rpc_status;
7348};
7349
7350static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7351{
7352 struct nfs41_exchange_id_data *cdata =
7353 (struct nfs41_exchange_id_data *)data;
7354 struct nfs_client *clp = cdata->args.client;
7355 int status = task->tk_status;
7356
7357 trace_nfs4_exchange_id(clp, status);
7358
7359 if (status == 0)
7360 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
ad0849a7
AA
7361
7362 if (cdata->xprt && status == 0) {
7363 status = nfs4_detect_session_trunking(clp, &cdata->res,
7364 cdata->xprt);
7365 goto out;
7366 }
7367
8d89bd70
AA
7368 if (status == 0)
7369 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7370
7371 if (status == 0) {
7372 clp->cl_clientid = cdata->res.clientid;
7373 clp->cl_exchange_flags = cdata->res.flags;
7374 /* Client ID is not confirmed */
7375 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7376 clear_bit(NFS4_SESSION_ESTABLISHED,
7377 &clp->cl_session->session_state);
7378 clp->cl_seqid = cdata->res.seqid;
7379 }
7380
7381 kfree(clp->cl_serverowner);
7382 clp->cl_serverowner = cdata->res.server_owner;
7383 cdata->res.server_owner = NULL;
7384
7385 /* use the most recent implementation id */
7386 kfree(clp->cl_implid);
7387 clp->cl_implid = cdata->res.impl_id;
7388 cdata->res.impl_id = NULL;
7389
7390 if (clp->cl_serverscope != NULL &&
7391 !nfs41_same_server_scope(clp->cl_serverscope,
7392 cdata->res.server_scope)) {
7393 dprintk("%s: server_scope mismatch detected\n",
7394 __func__);
7395 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7396 kfree(clp->cl_serverscope);
7397 clp->cl_serverscope = NULL;
7398 }
7399
7400 if (clp->cl_serverscope == NULL) {
7401 clp->cl_serverscope = cdata->res.server_scope;
7402 cdata->res.server_scope = NULL;
7403 }
ad0849a7
AA
7404 /* Save the EXCHANGE_ID verifier session trunk tests */
7405 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7406 sizeof(clp->cl_confirm.data));
8d89bd70 7407 }
ad0849a7 7408out:
8d89bd70 7409 cdata->rpc_status = status;
ad0849a7 7410 return;
8d89bd70
AA
7411}
7412
7413static void nfs4_exchange_id_release(void *data)
7414{
7415 struct nfs41_exchange_id_data *cdata =
7416 (struct nfs41_exchange_id_data *)data;
7417
7418 nfs_put_client(cdata->args.client);
ad0849a7
AA
7419 if (cdata->xprt) {
7420 xprt_put(cdata->xprt);
7421 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7422 }
8d89bd70
AA
7423 kfree(cdata->res.impl_id);
7424 kfree(cdata->res.server_scope);
7425 kfree(cdata->res.server_owner);
7426 kfree(cdata);
7427}
7428
7429static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7430 .rpc_call_done = nfs4_exchange_id_done,
7431 .rpc_release = nfs4_exchange_id_release,
7432};
7433
2031cd1a
WAA
7434/*
7435 * _nfs4_proc_exchange_id()
6bbb4ae8 7436 *
2031cd1a 7437 * Wrapper for EXCHANGE_ID operation.
99fe60d0 7438 */
2031cd1a 7439static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
ad0849a7 7440 u32 sp4_how, struct rpc_xprt *xprt)
99fe60d0
BH
7441{
7442 nfs4_verifier verifier;
99fe60d0
BH
7443 struct rpc_message msg = {
7444 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
99fe60d0
BH
7445 .rpc_cred = cred,
7446 };
8d89bd70
AA
7447 struct rpc_task_setup task_setup_data = {
7448 .rpc_client = clp->cl_rpcclient,
7449 .callback_ops = &nfs4_exchange_id_call_ops,
7450 .rpc_message = &msg,
7451 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7452 };
7453 struct nfs41_exchange_id_data *calldata;
7454 struct rpc_task *task;
7455 int status = -EIO;
7456
7457 if (!atomic_inc_not_zero(&clp->cl_count))
7458 goto out;
7459
7460 status = -ENOMEM;
7461 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7462 if (!calldata)
7463 goto out;
99fe60d0 7464
ad0849a7
AA
7465 if (!xprt)
7466 nfs4_init_boot_verifier(clp, &verifier);
873e3851
JL
7467
7468 status = nfs4_init_uniform_client_string(clp);
7469 if (status)
8d89bd70 7470 goto out_calldata;
3a6bb738
JL
7471
7472 dprintk("NFS call exchange_id auth=%s, '%s'\n",
6bbb4ae8 7473 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3a6bb738 7474 clp->cl_owner_id);
99fe60d0 7475
8d89bd70
AA
7476 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7477 GFP_NOFS);
7478 status = -ENOMEM;
7479 if (unlikely(calldata->res.server_owner == NULL))
7480 goto out_calldata;
78fe0f41 7481
8d89bd70 7482 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
bbafffd2 7483 GFP_NOFS);
8d89bd70 7484 if (unlikely(calldata->res.server_scope == NULL))
acdeb69d 7485 goto out_server_owner;
78fe0f41 7486
8d89bd70
AA
7487 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7488 if (unlikely(calldata->res.impl_id == NULL))
7d2ed9ac 7489 goto out_server_scope;
7d2ed9ac 7490
2031cd1a
WAA
7491 switch (sp4_how) {
7492 case SP4_NONE:
8d89bd70 7493 calldata->args.state_protect.how = SP4_NONE;
2031cd1a
WAA
7494 break;
7495
7496 case SP4_MACH_CRED:
8d89bd70 7497 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
2031cd1a
WAA
7498 break;
7499
7500 default:
7501 /* unsupported! */
7502 WARN_ON_ONCE(1);
7503 status = -EINVAL;
6b55970b 7504 goto out_impl_id;
2031cd1a 7505 }
ad0849a7
AA
7506 if (xprt) {
7507 calldata->xprt = xprt;
7508 task_setup_data.rpc_xprt = xprt;
7509 task_setup_data.flags =
7510 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7511 calldata->args.verifier = &clp->cl_confirm;
7512 } else {
7513 calldata->args.verifier = &verifier;
7514 }
8d89bd70
AA
7515 calldata->args.client = clp;
7516#ifdef CONFIG_NFS_V4_1_MIGRATION
7517 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7518 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7519 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7520#else
7521 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7522 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7523#endif
7524 msg.rpc_argp = &calldata->args;
7525 msg.rpc_resp = &calldata->res;
7526 task_setup_data.callback_data = calldata;
78fe0f41 7527
8d89bd70
AA
7528 task = rpc_run_task(&task_setup_data);
7529 if (IS_ERR(task)) {
7530 status = PTR_ERR(task);
7531 goto out_impl_id;
6b55970b 7532 }
7d2ed9ac 7533
ad0849a7
AA
7534 if (!xprt) {
7535 status = rpc_wait_for_completion_task(task);
7536 if (!status)
7537 status = calldata->rpc_status;
7538 } else /* session trunking test */
8d89bd70 7539 status = calldata->rpc_status;
ad0849a7 7540
8d89bd70 7541 rpc_put_task(task);
abe9a6d5 7542out:
177313f1 7543 if (clp->cl_implid != NULL)
6bbb4ae8 7544 dprintk("NFS reply exchange_id: Server Implementation ID: "
7d2ed9ac 7545 "domain: %s, name: %s, date: %llu,%u\n",
6bbb4ae8 7546 clp->cl_implid->domain, clp->cl_implid->name,
59155546
CL
7547 clp->cl_implid->date.seconds,
7548 clp->cl_implid->date.nseconds);
6bbb4ae8 7549 dprintk("NFS reply exchange_id: %d\n", status);
99fe60d0 7550 return status;
8d89bd70
AA
7551
7552out_impl_id:
7553 kfree(calldata->res.impl_id);
7554out_server_scope:
7555 kfree(calldata->res.server_scope);
7556out_server_owner:
7557 kfree(calldata->res.server_owner);
7558out_calldata:
7559 kfree(calldata);
7560 goto out;
99fe60d0
BH
7561}
7562
2031cd1a
WAA
7563/*
7564 * nfs4_proc_exchange_id()
7565 *
7566 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7567 *
7568 * Since the clientid has expired, all compounds using sessions
7569 * associated with the stale clientid will be returning
7570 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7571 * be in some phase of session reset.
7572 *
7573 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7574 */
7575int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7576{
7577 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7578 int status;
7579
7580 /* try SP4_MACH_CRED if krb5i/p */
7581 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7582 authflavor == RPC_AUTH_GSS_KRB5P) {
ad0849a7 7583 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
2031cd1a
WAA
7584 if (!status)
7585 return 0;
7586 }
7587
7588 /* try SP4_NONE */
ad0849a7 7589 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
2031cd1a
WAA
7590}
7591
04fa2c6b
AA
7592/**
7593 * nfs4_test_session_trunk
7594 *
7595 * This is an add_xprt_test() test function called from
7596 * rpc_clnt_setup_test_and_add_xprt.
7597 *
7598 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7599 * and is dereferrenced in nfs4_exchange_id_release
7600 *
7601 * Upon success, add the new transport to the rpc_clnt
7602 *
7603 * @clnt: struct rpc_clnt to get new transport
7604 * @xprt: the rpc_xprt to test
7605 * @data: call data for _nfs4_proc_exchange_id.
7606 */
7607int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7608 void *data)
7609{
7610 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7611 u32 sp4_how;
7612
7613 dprintk("--> %s try %s\n", __func__,
7614 xprt->address_strings[RPC_DISPLAY_ADDR]);
7615
7616 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7617
7618 /* Test connection for session trunking. Async exchange_id call */
7619 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7620}
7621EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7622
66245539
TM
7623static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7624 struct rpc_cred *cred)
7625{
7626 struct rpc_message msg = {
7627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7628 .rpc_argp = clp,
7629 .rpc_cred = cred,
7630 };
7631 int status;
7632
7633 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
c6d01c6f 7634 trace_nfs4_destroy_clientid(clp, status);
66245539 7635 if (status)
02c67525 7636 dprintk("NFS: Got error %d from the server %s on "
66245539
TM
7637 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7638 return status;
7639}
7640
7641static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7642 struct rpc_cred *cred)
7643{
7644 unsigned int loop;
7645 int ret;
7646
7647 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7648 ret = _nfs4_proc_destroy_clientid(clp, cred);
7649 switch (ret) {
7650 case -NFS4ERR_DELAY:
7651 case -NFS4ERR_CLIENTID_BUSY:
7652 ssleep(1);
7653 break;
7654 default:
7655 return ret;
7656 }
7657 }
7658 return 0;
7659}
7660
7661int nfs4_destroy_clientid(struct nfs_client *clp)
7662{
7663 struct rpc_cred *cred;
7664 int ret = 0;
7665
7666 if (clp->cl_mvops->minor_version < 1)
7667 goto out;
7668 if (clp->cl_exchange_flags == 0)
7669 goto out;
05f4c350
CL
7670 if (clp->cl_preserve_clid)
7671 goto out;
73d8bde5 7672 cred = nfs4_get_clid_cred(clp);
66245539
TM
7673 ret = nfs4_proc_destroy_clientid(clp, cred);
7674 if (cred)
7675 put_rpccred(cred);
7676 switch (ret) {
7677 case 0:
7678 case -NFS4ERR_STALE_CLIENTID:
7679 clp->cl_exchange_flags = 0;
7680 }
7681out:
7682 return ret;
7683}
7684
2050f0cc
AA
7685struct nfs4_get_lease_time_data {
7686 struct nfs4_get_lease_time_args *args;
7687 struct nfs4_get_lease_time_res *res;
7688 struct nfs_client *clp;
7689};
7690
7691static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7692 void *calldata)
7693{
2050f0cc
AA
7694 struct nfs4_get_lease_time_data *data =
7695 (struct nfs4_get_lease_time_data *)calldata;
7696
7697 dprintk("--> %s\n", __func__);
7698 /* just setup sequence, do not trigger session recovery
7699 since we're invoked within one */
d9afbd1b
TM
7700 nfs41_setup_sequence(data->clp->cl_session,
7701 &data->args->la_seq_args,
7702 &data->res->lr_seq_res,
7703 task);
2050f0cc
AA
7704 dprintk("<-- %s\n", __func__);
7705}
7706
7707/*
7708 * Called from nfs4_state_manager thread for session setup, so don't recover
7709 * from sequence operation or clientid errors.
7710 */
7711static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7712{
7713 struct nfs4_get_lease_time_data *data =
7714 (struct nfs4_get_lease_time_data *)calldata;
7715
7716 dprintk("--> %s\n", __func__);
14516c3a
TM
7717 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7718 return;
2050f0cc
AA
7719 switch (task->tk_status) {
7720 case -NFS4ERR_DELAY:
7721 case -NFS4ERR_GRACE:
7722 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7723 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7724 task->tk_status = 0;
a8a4ae3a
AA
7725 /* fall through */
7726 case -NFS4ERR_RETRY_UNCACHED_REP:
d00c5d43 7727 rpc_restart_call_prepare(task);
2050f0cc
AA
7728 return;
7729 }
2050f0cc
AA
7730 dprintk("<-- %s\n", __func__);
7731}
7732
17280175 7733static const struct rpc_call_ops nfs4_get_lease_time_ops = {
2050f0cc
AA
7734 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7735 .rpc_call_done = nfs4_get_lease_time_done,
7736};
7737
7738int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7739{
7740 struct rpc_task *task;
7741 struct nfs4_get_lease_time_args args;
7742 struct nfs4_get_lease_time_res res = {
7743 .lr_fsinfo = fsinfo,
7744 };
7745 struct nfs4_get_lease_time_data data = {
7746 .args = &args,
7747 .res = &res,
7748 .clp = clp,
7749 };
7750 struct rpc_message msg = {
7751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7752 .rpc_argp = &args,
7753 .rpc_resp = &res,
7754 };
7755 struct rpc_task_setup task_setup = {
7756 .rpc_client = clp->cl_rpcclient,
7757 .rpc_message = &msg,
7758 .callback_ops = &nfs4_get_lease_time_ops,
1bd714f2
TM
7759 .callback_data = &data,
7760 .flags = RPC_TASK_TIMEOUT,
2050f0cc
AA
7761 };
7762 int status;
7763
a9c92d6b 7764 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
8fe72bac 7765 nfs4_set_sequence_privileged(&args.la_seq_args);
2050f0cc
AA
7766 dprintk("--> %s\n", __func__);
7767 task = rpc_run_task(&task_setup);
7768
7769 if (IS_ERR(task))
7770 status = PTR_ERR(task);
7771 else {
7772 status = task->tk_status;
7773 rpc_put_task(task);
7774 }
7775 dprintk("<-- %s return %d\n", __func__, status);
7776
7777 return status;
7778}
7779
fc931582
AA
7780/*
7781 * Initialize the values to be used by the client in CREATE_SESSION
7782 * If nfs4_init_session set the fore channel request and response sizes,
7783 * use them.
7784 *
7785 * Set the back channel max_resp_sz_cached to zero to force the client to
7786 * always set csa_cachethis to FALSE because the current implementation
7787 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7788 */
6b26cc8c
CL
7789static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7790 struct rpc_clnt *clnt)
fc931582 7791{
18aad3d5 7792 unsigned int max_rqst_sz, max_resp_sz;
6b26cc8c 7793 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
18aad3d5
AA
7794
7795 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7796 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
fc931582 7797
fc931582 7798 /* Fore channel attributes */
18aad3d5
AA
7799 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7800 args->fc_attrs.max_resp_sz = max_resp_sz;
fc931582 7801 args->fc_attrs.max_ops = NFS4_MAX_OPS;
ef159e91 7802 args->fc_attrs.max_reqs = max_session_slots;
fc931582
AA
7803
7804 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8e0d46e1 7805 "max_ops=%u max_reqs=%u\n",
fc931582
AA
7806 __func__,
7807 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8e0d46e1 7808 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
fc931582
AA
7809
7810 /* Back channel attributes */
6b26cc8c
CL
7811 args->bc_attrs.max_rqst_sz = max_bc_payload;
7812 args->bc_attrs.max_resp_sz = max_bc_payload;
fc931582
AA
7813 args->bc_attrs.max_resp_sz_cached = 0;
7814 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5405fc44 7815 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
fc931582
AA
7816
7817 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7818 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7819 __func__,
7820 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7821 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7822 args->bc_attrs.max_reqs);
7823}
7824
79969dd1
TM
7825static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7826 struct nfs41_create_session_res *res)
8d35301d 7827{
43c2e885 7828 struct nfs4_channel_attrs *sent = &args->fc_attrs;
79969dd1 7829 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
43c2e885 7830
43c2e885
BF
7831 if (rcvd->max_resp_sz > sent->max_resp_sz)
7832 return -EINVAL;
7833 /*
7834 * Our requested max_ops is the minimum we need; we're not
7835 * prepared to break up compounds into smaller pieces than that.
7836 * So, no point even trying to continue if the server won't
7837 * cooperate:
7838 */
7839 if (rcvd->max_ops < sent->max_ops)
7840 return -EINVAL;
7841 if (rcvd->max_reqs == 0)
7842 return -EINVAL;
b4b9a0c1
VG
7843 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7844 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
43c2e885 7845 return 0;
8d35301d
AA
7846}
7847
79969dd1
TM
7848static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7849 struct nfs41_create_session_res *res)
43c2e885
BF
7850{
7851 struct nfs4_channel_attrs *sent = &args->bc_attrs;
79969dd1 7852 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8d35301d 7853
b1c0df5f
TM
7854 if (!(res->flags & SESSION4_BACK_CHAN))
7855 goto out;
43c2e885
BF
7856 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7857 return -EINVAL;
7858 if (rcvd->max_resp_sz < sent->max_resp_sz)
7859 return -EINVAL;
7860 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7861 return -EINVAL;
5405fc44 7862 if (rcvd->max_ops > sent->max_ops)
43c2e885 7863 return -EINVAL;
5405fc44 7864 if (rcvd->max_reqs > sent->max_reqs)
43c2e885 7865 return -EINVAL;
b1c0df5f 7866out:
43c2e885
BF
7867 return 0;
7868}
8d35301d 7869
8d35301d 7870static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
79969dd1 7871 struct nfs41_create_session_res *res)
8d35301d 7872{
43c2e885 7873 int ret;
8d35301d 7874
79969dd1 7875 ret = nfs4_verify_fore_channel_attrs(args, res);
43c2e885
BF
7876 if (ret)
7877 return ret;
79969dd1
TM
7878 return nfs4_verify_back_channel_attrs(args, res);
7879}
7880
7881static void nfs4_update_session(struct nfs4_session *session,
7882 struct nfs41_create_session_res *res)
7883{
7884 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
e11259f9
TM
7885 /* Mark client id and session as being confirmed */
7886 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7887 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
79969dd1
TM
7888 session->flags = res->flags;
7889 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
b1c0df5f
TM
7890 if (res->flags & SESSION4_BACK_CHAN)
7891 memcpy(&session->bc_attrs, &res->bc_attrs,
7892 sizeof(session->bc_attrs));
8d35301d
AA
7893}
7894
848f5bda
TM
7895static int _nfs4_proc_create_session(struct nfs_client *clp,
7896 struct rpc_cred *cred)
fc931582
AA
7897{
7898 struct nfs4_session *session = clp->cl_session;
7899 struct nfs41_create_session_args args = {
7900 .client = clp,
79969dd1
TM
7901 .clientid = clp->cl_clientid,
7902 .seqid = clp->cl_seqid,
fc931582
AA
7903 .cb_program = NFS4_CALLBACK,
7904 };
79969dd1
TM
7905 struct nfs41_create_session_res res;
7906
fc931582
AA
7907 struct rpc_message msg = {
7908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7909 .rpc_argp = &args,
7910 .rpc_resp = &res,
848f5bda 7911 .rpc_cred = cred,
fc931582
AA
7912 };
7913 int status;
7914
6b26cc8c 7915 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
0f91421e 7916 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
fc931582 7917
1bd714f2 7918 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
c6d01c6f 7919 trace_nfs4_create_session(clp, status);
fc931582 7920
b519d408
TM
7921 switch (status) {
7922 case -NFS4ERR_STALE_CLIENTID:
7923 case -NFS4ERR_DELAY:
7924 case -ETIMEDOUT:
7925 case -EACCES:
7926 case -EAGAIN:
7927 goto out;
7928 };
7929
7930 clp->cl_seqid++;
43095d39 7931 if (!status) {
8d35301d 7932 /* Verify the session's negotiated channel_attrs values */
79969dd1 7933 status = nfs4_verify_channel_attrs(&args, &res);
fc931582 7934 /* Increment the clientid slot sequence id */
79969dd1
TM
7935 if (status)
7936 goto out;
7937 nfs4_update_session(session, &res);
fc931582 7938 }
79969dd1 7939out:
fc931582
AA
7940 return status;
7941}
7942
7943/*
7944 * Issues a CREATE_SESSION operation to the server.
7945 * It is the responsibility of the caller to verify the session is
7946 * expired before calling this routine.
7947 */
848f5bda 7948int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
fc931582
AA
7949{
7950 int status;
7951 unsigned *ptr;
fc931582
AA
7952 struct nfs4_session *session = clp->cl_session;
7953
7954 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7955
848f5bda 7956 status = _nfs4_proc_create_session(clp, cred);
fc931582
AA
7957 if (status)
7958 goto out;
7959
aacd5537
AA
7960 /* Init or reset the session slot tables */
7961 status = nfs4_setup_session_slot_tables(session);
7962 dprintk("slot table setup returned %d\n", status);
fc931582
AA
7963 if (status)
7964 goto out;
7965
7966 ptr = (unsigned *)&session->sess_id.data[0];
7967 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7968 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
fc931582
AA
7969out:
7970 dprintk("<-- %s\n", __func__);
7971 return status;
7972}
7973
0f3e66c6
AA
7974/*
7975 * Issue the over-the-wire RPC DESTROY_SESSION.
7976 * The caller must serialize access to this routine.
7977 */
848f5bda
TM
7978int nfs4_proc_destroy_session(struct nfs4_session *session,
7979 struct rpc_cred *cred)
0f3e66c6 7980{
848f5bda
TM
7981 struct rpc_message msg = {
7982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7983 .rpc_argp = session,
7984 .rpc_cred = cred,
7985 };
0f3e66c6 7986 int status = 0;
0f3e66c6
AA
7987
7988 dprintk("--> nfs4_proc_destroy_session\n");
7989
7990 /* session is still being setup */
e11259f9
TM
7991 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7992 return 0;
0f3e66c6 7993
1bd714f2 7994 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
c6d01c6f 7995 trace_nfs4_destroy_session(session->clp, status);
0f3e66c6
AA
7996
7997 if (status)
08106ac7 7998 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
0f3e66c6
AA
7999 "Session has been destroyed regardless...\n", status);
8000
8001 dprintk("<-- nfs4_proc_destroy_session\n");
8002 return status;
8003}
8004
fc01cea9
AA
8005/*
8006 * Renew the cl_session lease.
8007 */
d5f8d3fe
TM
8008struct nfs4_sequence_data {
8009 struct nfs_client *clp;
8010 struct nfs4_sequence_args args;
8011 struct nfs4_sequence_res res;
8012};
8013
dc96aef9
AB
8014static void nfs41_sequence_release(void *data)
8015{
d5f8d3fe
TM
8016 struct nfs4_sequence_data *calldata = data;
8017 struct nfs_client *clp = calldata->clp;
dc96aef9 8018
7135840f
AB
8019 if (atomic_read(&clp->cl_count) > 1)
8020 nfs4_schedule_state_renewal(clp);
8021 nfs_put_client(clp);
d5f8d3fe 8022 kfree(calldata);
dc96aef9
AB
8023}
8024
aa5190d0
TM
8025static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8026{
8027 switch(task->tk_status) {
8028 case -NFS4ERR_DELAY:
aa5190d0
TM
8029 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8030 return -EAGAIN;
8031 default:
0400a6b0 8032 nfs4_schedule_lease_recovery(clp);
aa5190d0
TM
8033 }
8034 return 0;
8035}
8036
dc96aef9 8037static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
fc01cea9 8038{
d5f8d3fe
TM
8039 struct nfs4_sequence_data *calldata = data;
8040 struct nfs_client *clp = calldata->clp;
fc01cea9 8041
14516c3a
TM
8042 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8043 return;
fc01cea9 8044
c6d01c6f 8045 trace_nfs4_sequence(clp, task->tk_status);
fc01cea9
AA
8046 if (task->tk_status < 0) {
8047 dprintk("%s ERROR %d\n", __func__, task->tk_status);
7135840f
AB
8048 if (atomic_read(&clp->cl_count) == 1)
8049 goto out;
fc01cea9 8050
aa5190d0
TM
8051 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8052 rpc_restart_call_prepare(task);
fc01cea9
AA
8053 return;
8054 }
8055 }
fc01cea9 8056 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7135840f 8057out:
fc01cea9
AA
8058 dprintk("<-- %s\n", __func__);
8059}
8060
8061static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8062{
d5f8d3fe
TM
8063 struct nfs4_sequence_data *calldata = data;
8064 struct nfs_client *clp = calldata->clp;
fc01cea9
AA
8065 struct nfs4_sequence_args *args;
8066 struct nfs4_sequence_res *res;
8067
fc01cea9
AA
8068 args = task->tk_msg.rpc_argp;
8069 res = task->tk_msg.rpc_resp;
8070
d9afbd1b 8071 nfs41_setup_sequence(clp->cl_session, args, res, task);
fc01cea9
AA
8072}
8073
8074static const struct rpc_call_ops nfs41_sequence_ops = {
8075 .rpc_call_done = nfs41_sequence_call_done,
8076 .rpc_call_prepare = nfs41_sequence_prepare,
dc96aef9 8077 .rpc_release = nfs41_sequence_release,
fc01cea9
AA
8078};
8079
8fe72bac
TM
8080static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8081 struct rpc_cred *cred,
8082 bool is_privileged)
fc01cea9 8083{
d5f8d3fe 8084 struct nfs4_sequence_data *calldata;
fc01cea9
AA
8085 struct rpc_message msg = {
8086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8087 .rpc_cred = cred,
8088 };
71ac6da9
TM
8089 struct rpc_task_setup task_setup_data = {
8090 .rpc_client = clp->cl_rpcclient,
8091 .rpc_message = &msg,
8fe72bac 8092 .callback_ops = &nfs41_sequence_ops,
bc7a05ca 8093 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
71ac6da9 8094 };
fc01cea9 8095
7135840f 8096 if (!atomic_inc_not_zero(&clp->cl_count))
71ac6da9 8097 return ERR_PTR(-EIO);
dfb4f309 8098 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
d5f8d3fe 8099 if (calldata == NULL) {
7135840f 8100 nfs_put_client(clp);
71ac6da9 8101 return ERR_PTR(-ENOMEM);
fc01cea9 8102 }
a9c92d6b 8103 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
8fe72bac
TM
8104 if (is_privileged)
8105 nfs4_set_sequence_privileged(&calldata->args);
d5f8d3fe
TM
8106 msg.rpc_argp = &calldata->args;
8107 msg.rpc_resp = &calldata->res;
8108 calldata->clp = clp;
71ac6da9 8109 task_setup_data.callback_data = calldata;
fc01cea9 8110
71ac6da9
TM
8111 return rpc_run_task(&task_setup_data);
8112}
8113
2f60ea6b 8114static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
71ac6da9
TM
8115{
8116 struct rpc_task *task;
8117 int ret = 0;
8118
2f60ea6b 8119 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
d1f456b0 8120 return -EAGAIN;
8fe72bac 8121 task = _nfs41_proc_sequence(clp, cred, false);
71ac6da9
TM
8122 if (IS_ERR(task))
8123 ret = PTR_ERR(task);
8124 else
bf294b41 8125 rpc_put_task_async(task);
71ac6da9
TM
8126 dprintk("<-- %s status=%d\n", __func__, ret);
8127 return ret;
8128}
8129
8130static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8131{
8132 struct rpc_task *task;
8133 int ret;
8134
8fe72bac 8135 task = _nfs41_proc_sequence(clp, cred, true);
71ac6da9
TM
8136 if (IS_ERR(task)) {
8137 ret = PTR_ERR(task);
8138 goto out;
8139 }
8140 ret = rpc_wait_for_completion_task(task);
be824167 8141 if (!ret)
71ac6da9
TM
8142 ret = task->tk_status;
8143 rpc_put_task(task);
8144out:
8145 dprintk("<-- %s status=%d\n", __func__, ret);
8146 return ret;
fc01cea9
AA
8147}
8148
fce5c838
RL
8149struct nfs4_reclaim_complete_data {
8150 struct nfs_client *clp;
8151 struct nfs41_reclaim_complete_args arg;
8152 struct nfs41_reclaim_complete_res res;
8153};
8154
8155static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8156{
8157 struct nfs4_reclaim_complete_data *calldata = data;
8158
d9afbd1b
TM
8159 nfs41_setup_sequence(calldata->clp->cl_session,
8160 &calldata->arg.seq_args,
8161 &calldata->res.seq_res,
8162 task);
fce5c838
RL
8163}
8164
aa5190d0
TM
8165static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8166{
8167 switch(task->tk_status) {
8168 case 0:
8169 case -NFS4ERR_COMPLETE_ALREADY:
8170 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8171 break;
8172 case -NFS4ERR_DELAY:
aa5190d0 8173 rpc_delay(task, NFS4_POLL_RETRY_MAX);
a8a4ae3a
AA
8174 /* fall through */
8175 case -NFS4ERR_RETRY_UNCACHED_REP:
aa5190d0
TM
8176 return -EAGAIN;
8177 default:
0400a6b0 8178 nfs4_schedule_lease_recovery(clp);
aa5190d0
TM
8179 }
8180 return 0;
8181}
8182
fce5c838
RL
8183static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8184{
8185 struct nfs4_reclaim_complete_data *calldata = data;
8186 struct nfs_client *clp = calldata->clp;
8187 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8188
8189 dprintk("--> %s\n", __func__);
14516c3a
TM
8190 if (!nfs41_sequence_done(task, res))
8191 return;
fce5c838 8192
c6d01c6f 8193 trace_nfs4_reclaim_complete(clp, task->tk_status);
aa5190d0
TM
8194 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8195 rpc_restart_call_prepare(task);
8196 return;
8197 }
fce5c838
RL
8198 dprintk("<-- %s\n", __func__);
8199}
8200
8201static void nfs4_free_reclaim_complete_data(void *data)
8202{
8203 struct nfs4_reclaim_complete_data *calldata = data;
8204
8205 kfree(calldata);
8206}
8207
8208static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8209 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8210 .rpc_call_done = nfs4_reclaim_complete_done,
8211 .rpc_release = nfs4_free_reclaim_complete_data,
8212};
8213
8214/*
8215 * Issue a global reclaim complete.
8216 */
965e9c23
TM
8217static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8218 struct rpc_cred *cred)
fce5c838
RL
8219{
8220 struct nfs4_reclaim_complete_data *calldata;
8221 struct rpc_task *task;
8222 struct rpc_message msg = {
8223 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
965e9c23 8224 .rpc_cred = cred,
fce5c838
RL
8225 };
8226 struct rpc_task_setup task_setup_data = {
8227 .rpc_client = clp->cl_rpcclient,
8228 .rpc_message = &msg,
8229 .callback_ops = &nfs4_reclaim_complete_call_ops,
8230 .flags = RPC_TASK_ASYNC,
8231 };
8232 int status = -ENOMEM;
8233
8234 dprintk("--> %s\n", __func__);
8535b2be 8235 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
fce5c838
RL
8236 if (calldata == NULL)
8237 goto out;
8238 calldata->clp = clp;
8239 calldata->arg.one_fs = 0;
fce5c838 8240
a9c92d6b 8241 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
8fe72bac 8242 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
fce5c838
RL
8243 msg.rpc_argp = &calldata->arg;
8244 msg.rpc_resp = &calldata->res;
8245 task_setup_data.callback_data = calldata;
8246 task = rpc_run_task(&task_setup_data);
acf82b85 8247 if (IS_ERR(task)) {
fce5c838 8248 status = PTR_ERR(task);
acf82b85
DC
8249 goto out;
8250 }
c34c32ea
AA
8251 status = nfs4_wait_for_completion_rpc_task(task);
8252 if (status == 0)
8253 status = task->tk_status;
fce5c838 8254 rpc_put_task(task);
acf82b85 8255 return 0;
fce5c838
RL
8256out:
8257 dprintk("<-- %s status=%d\n", __func__, status);
8258 return status;
8259}
b1f69b75
AA
8260
8261static void
8262nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8263{
8264 struct nfs4_layoutget *lgp = calldata;
c31663d4 8265 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6ba7db34 8266 struct nfs4_session *session = nfs4_get_session(server);
b1f69b75
AA
8267
8268 dprintk("--> %s\n", __func__);
183d9e7b
JL
8269 nfs41_setup_sequence(session, &lgp->args.seq_args,
8270 &lgp->res.seq_res, task);
8271 dprintk("<-- %s\n", __func__);
b1f69b75
AA
8272}
8273
8274static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8275{
8276 struct nfs4_layoutget *lgp = calldata;
183d9e7b
JL
8277
8278 dprintk("--> %s\n", __func__);
2e80dbe7 8279 nfs41_sequence_process(task, &lgp->res.seq_res);
183d9e7b
JL
8280 dprintk("<-- %s\n", __func__);
8281}
8282
8283static int
8284nfs4_layoutget_handle_exception(struct rpc_task *task,
8285 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8286{
ee314c2a
TM
8287 struct inode *inode = lgp->args.inode;
8288 struct nfs_server *server = NFS_SERVER(inode);
8289 struct pnfs_layout_hdr *lo;
e85d7ee4
TM
8290 int nfs4err = task->tk_status;
8291 int err, status = 0;
f7db0b28 8292 LIST_HEAD(head);
b1f69b75 8293
ed7e5423 8294 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
b1f69b75 8295
e85d7ee4 8296 switch (nfs4err) {
b1f69b75 8297 case 0:
ee314c2a 8298 goto out;
7c1e6e58
PT
8299
8300 /*
8301 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8302 * on the file. set tk_status to -ENODATA to tell upper layer to
8303 * retry go inband.
8304 */
8305 case -NFS4ERR_LAYOUTUNAVAILABLE:
183d9e7b 8306 status = -ENODATA;
7c1e6e58 8307 goto out;
21b874c8
TM
8308 /*
8309 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8310 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8311 */
8312 case -NFS4ERR_BADLAYOUT:
183d9e7b
JL
8313 status = -EOVERFLOW;
8314 goto out;
ed7e5423
BH
8315 /*
8316 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
21b874c8
TM
8317 * (or clients) writing to the same RAID stripe except when
8318 * the minlength argument is 0 (see RFC5661 section 18.43.3).
183d9e7b
JL
8319 *
8320 * Treat it like we would RECALLCONFLICT -- we retry for a little
8321 * while, and then eventually give up.
ed7e5423 8322 */
b1f69b75 8323 case -NFS4ERR_LAYOUTTRYLATER:
183d9e7b
JL
8324 if (lgp->args.minlength == 0) {
8325 status = -EOVERFLOW;
8326 goto out;
ed7e5423 8327 }
e85d7ee4
TM
8328 status = -EBUSY;
8329 break;
183d9e7b 8330 case -NFS4ERR_RECALLCONFLICT:
183d9e7b 8331 status = -ERECALLCONFLICT;
e85d7ee4 8332 break;
26f47443
TM
8333 case -NFS4ERR_DELEG_REVOKED:
8334 case -NFS4ERR_ADMIN_REVOKED:
ee314c2a
TM
8335 case -NFS4ERR_EXPIRED:
8336 case -NFS4ERR_BAD_STATEID:
183d9e7b 8337 exception->timeout = 0;
ee314c2a 8338 spin_lock(&inode->i_lock);
f7db0b28
TM
8339 lo = NFS_I(inode)->layout;
8340 /* If the open stateid was bad, then recover it. */
8341 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8342 nfs4_stateid_match_other(&lgp->args.stateid,
2259f960 8343 &lgp->args.ctx->state->stateid)) {
ee314c2a 8344 spin_unlock(&inode->i_lock);
183d9e7b 8345 exception->state = lgp->args.ctx->state;
26f47443 8346 exception->stateid = &lgp->args.stateid;
2259f960
TM
8347 break;
8348 }
f7db0b28
TM
8349
8350 /*
8351 * Mark the bad layout state as invalid, then retry
8352 */
668f455d 8353 pnfs_mark_layout_stateid_invalid(lo, &head);
f7db0b28
TM
8354 spin_unlock(&inode->i_lock);
8355 pnfs_free_lseg_list(&head);
8356 status = -EAGAIN;
8357 goto out;
b1f69b75 8358 }
183d9e7b 8359
e85d7ee4
TM
8360 err = nfs4_handle_exception(server, nfs4err, exception);
8361 if (!status) {
8362 if (exception->retry)
8363 status = -EAGAIN;
8364 else
8365 status = err;
8366 }
ee314c2a 8367out:
b1f69b75 8368 dprintk("<-- %s\n", __func__);
183d9e7b 8369 return status;
b1f69b75
AA
8370}
8371
8554116e
IK
8372static size_t max_response_pages(struct nfs_server *server)
8373{
8374 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8375 return nfs_page_array_len(0, max_resp_sz);
8376}
8377
8378static void nfs4_free_pages(struct page **pages, size_t size)
8379{
8380 int i;
8381
8382 if (!pages)
8383 return;
8384
8385 for (i = 0; i < size; i++) {
8386 if (!pages[i])
8387 break;
8388 __free_page(pages[i]);
8389 }
8390 kfree(pages);
8391}
8392
8393static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8394{
8395 struct page **pages;
8396 int i;
8397
8398 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8399 if (!pages) {
8400 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8401 return NULL;
8402 }
8403
8404 for (i = 0; i < size; i++) {
8405 pages[i] = alloc_page(gfp_flags);
8406 if (!pages[i]) {
8407 dprintk("%s: failed to allocate page\n", __func__);
8408 nfs4_free_pages(pages, size);
8409 return NULL;
8410 }
8411 }
8412
8413 return pages;
8414}
8415
b1f69b75
AA
8416static void nfs4_layoutget_release(void *calldata)
8417{
8418 struct nfs4_layoutget *lgp = calldata;
a47970ff
WAA
8419 struct inode *inode = lgp->args.inode;
8420 struct nfs_server *server = NFS_SERVER(inode);
8554116e 8421 size_t max_pages = max_response_pages(server);
b1f69b75
AA
8422
8423 dprintk("--> %s\n", __func__);
8554116e 8424 nfs4_free_pages(lgp->args.layout.pages, max_pages);
a47970ff 8425 pnfs_put_layout_hdr(NFS_I(inode)->layout);
b1f69b75
AA
8426 put_nfs_open_context(lgp->args.ctx);
8427 kfree(calldata);
8428 dprintk("<-- %s\n", __func__);
8429}
8430
8431static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8432 .rpc_call_prepare = nfs4_layoutget_prepare,
8433 .rpc_call_done = nfs4_layoutget_done,
8434 .rpc_release = nfs4_layoutget_release,
8435};
8436
a0b0a6e3 8437struct pnfs_layout_segment *
183d9e7b 8438nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
b1f69b75 8439{
a47970ff
WAA
8440 struct inode *inode = lgp->args.inode;
8441 struct nfs_server *server = NFS_SERVER(inode);
8554116e 8442 size_t max_pages = max_response_pages(server);
b1f69b75
AA
8443 struct rpc_task *task;
8444 struct rpc_message msg = {
8445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8446 .rpc_argp = &lgp->args,
8447 .rpc_resp = &lgp->res,
6ab59344 8448 .rpc_cred = lgp->cred,
b1f69b75
AA
8449 };
8450 struct rpc_task_setup task_setup_data = {
8451 .rpc_client = server->client,
8452 .rpc_message = &msg,
8453 .callback_ops = &nfs4_layoutget_call_ops,
8454 .callback_data = lgp,
8455 .flags = RPC_TASK_ASYNC,
8456 };
a0b0a6e3 8457 struct pnfs_layout_segment *lseg = NULL;
bc23676c
TM
8458 struct nfs4_exception exception = {
8459 .inode = inode,
8460 .timeout = *timeout,
8461 };
b1f69b75
AA
8462 int status = 0;
8463
8464 dprintk("--> %s\n", __func__);
8465
4bd5a980
PT
8466 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8467 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8468
8554116e
IK
8469 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8470 if (!lgp->args.layout.pages) {
8471 nfs4_layoutget_release(lgp);
a0b0a6e3 8472 return ERR_PTR(-ENOMEM);
8554116e
IK
8473 }
8474 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8475
35124a09 8476 lgp->res.layoutp = &lgp->args.layout;
b1f69b75 8477 lgp->res.seq_res.sr_slot = NULL;
a9c92d6b 8478 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
a47970ff 8479
b1f69b75
AA
8480 task = rpc_run_task(&task_setup_data);
8481 if (IS_ERR(task))
a0b0a6e3 8482 return ERR_CAST(task);
b1f69b75 8483 status = nfs4_wait_for_completion_rpc_task(task);
183d9e7b
JL
8484 if (status == 0) {
8485 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8486 *timeout = exception.timeout;
8487 }
8488
1037e6ea
TM
8489 trace_nfs4_layoutget(lgp->args.ctx,
8490 &lgp->args.range,
8491 &lgp->res.range,
48c9579a 8492 &lgp->res.stateid,
1037e6ea 8493 status);
183d9e7b 8494
085b7a45
WAA
8495 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8496 if (status == 0 && lgp->res.layoutp->len)
a0b0a6e3 8497 lseg = pnfs_layout_process(lgp);
2e80dbe7 8498 nfs4_sequence_free_slot(&lgp->res.seq_res);
b1f69b75
AA
8499 rpc_put_task(task);
8500 dprintk("<-- %s status=%d\n", __func__, status);
a0b0a6e3
TM
8501 if (status)
8502 return ERR_PTR(status);
8503 return lseg;
b1f69b75
AA
8504}
8505
cbe82603
BH
8506static void
8507nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8508{
8509 struct nfs4_layoutreturn *lrp = calldata;
8510
8511 dprintk("--> %s\n", __func__);
d9afbd1b
TM
8512 nfs41_setup_sequence(lrp->clp->cl_session,
8513 &lrp->args.seq_args,
8514 &lrp->res.seq_res,
8515 task);
cbe82603
BH
8516}
8517
8518static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8519{
8520 struct nfs4_layoutreturn *lrp = calldata;
8521 struct nfs_server *server;
8522
8523 dprintk("--> %s\n", __func__);
8524
2e80dbe7 8525 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
cbe82603
BH
8526 return;
8527
8528 server = NFS_SERVER(lrp->args.inode);
f22e5edd
TM
8529 switch (task->tk_status) {
8530 default:
8531 task->tk_status = 0;
8532 case 0:
8533 break;
8534 case -NFS4ERR_DELAY:
8478eaa1 8535 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
f22e5edd 8536 break;
2e80dbe7 8537 nfs4_sequence_free_slot(&lrp->res.seq_res);
d00c5d43 8538 rpc_restart_call_prepare(task);
cbe82603
BH
8539 return;
8540 }
cbe82603
BH
8541 dprintk("<-- %s\n", __func__);
8542}
8543
8544static void nfs4_layoutreturn_release(void *calldata)
8545{
8546 struct nfs4_layoutreturn *lrp = calldata;
849b286f 8547 struct pnfs_layout_hdr *lo = lrp->args.layout;
c5d73716 8548 LIST_HEAD(freeme);
cbe82603
BH
8549
8550 dprintk("--> %s\n", __func__);
849b286f 8551 spin_lock(&lo->plh_inode->i_lock);
52ec7be2
TM
8552 if (lrp->res.lrs_present) {
8553 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8554 &lrp->args.range,
8555 be32_to_cpu(lrp->args.stateid.seqid));
849b286f 8556 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
52ec7be2
TM
8557 } else
8558 pnfs_mark_layout_stateid_invalid(lo, &freeme);
d67ae825 8559 pnfs_clear_layoutreturn_waitbit(lo);
849b286f 8560 spin_unlock(&lo->plh_inode->i_lock);
2e80dbe7 8561 nfs4_sequence_free_slot(&lrp->res.seq_res);
c5d73716 8562 pnfs_free_lseg_list(&freeme);
70c3bd2b 8563 pnfs_put_layout_hdr(lrp->args.layout);
5a0ec8ac 8564 nfs_iput_and_deactive(lrp->inode);
cbe82603
BH
8565 kfree(calldata);
8566 dprintk("<-- %s\n", __func__);
8567}
8568
8569static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8570 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8571 .rpc_call_done = nfs4_layoutreturn_done,
8572 .rpc_release = nfs4_layoutreturn_release,
8573};
8574
6c16605d 8575int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
cbe82603
BH
8576{
8577 struct rpc_task *task;
8578 struct rpc_message msg = {
8579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8580 .rpc_argp = &lrp->args,
8581 .rpc_resp = &lrp->res,
9556000d 8582 .rpc_cred = lrp->cred,
cbe82603
BH
8583 };
8584 struct rpc_task_setup task_setup_data = {
1771c577 8585 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
cbe82603
BH
8586 .rpc_message = &msg,
8587 .callback_ops = &nfs4_layoutreturn_call_ops,
8588 .callback_data = lrp,
8589 };
6c16605d 8590 int status = 0;
cbe82603 8591
99ade3c7
AE
8592 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8593 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8594 &task_setup_data.rpc_client, &msg);
8595
cbe82603 8596 dprintk("--> %s\n", __func__);
5a0ec8ac
TM
8597 if (!sync) {
8598 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8599 if (!lrp->inode) {
8600 nfs4_layoutreturn_release(lrp);
8601 return -EAGAIN;
8602 }
8603 task_setup_data.flags |= RPC_TASK_ASYNC;
8604 }
a9c92d6b 8605 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
cbe82603
BH
8606 task = rpc_run_task(&task_setup_data);
8607 if (IS_ERR(task))
8608 return PTR_ERR(task);
5a0ec8ac
TM
8609 if (sync)
8610 status = task->tk_status;
48c9579a 8611 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
cbe82603
BH
8612 dprintk("<-- %s status=%d\n", __func__, status);
8613 rpc_put_task(task);
8614 return status;
8615}
8616
b1f69b75 8617static int
cd5875fe
TM
8618_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8619 struct pnfs_device *pdev,
8620 struct rpc_cred *cred)
b1f69b75
AA
8621{
8622 struct nfs4_getdeviceinfo_args args = {
8623 .pdev = pdev,
4e590803
TM
8624 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8625 NOTIFY_DEVICEID4_DELETE,
b1f69b75
AA
8626 };
8627 struct nfs4_getdeviceinfo_res res = {
8628 .pdev = pdev,
8629 };
8630 struct rpc_message msg = {
8631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8632 .rpc_argp = &args,
8633 .rpc_resp = &res,
cd5875fe 8634 .rpc_cred = cred,
b1f69b75
AA
8635 };
8636 int status;
8637
8638 dprintk("--> %s\n", __func__);
7c513058 8639 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4e590803
TM
8640 if (res.notification & ~args.notify_types)
8641 dprintk("%s: unsupported notification\n", __func__);
df52699e
TM
8642 if (res.notification != args.notify_types)
8643 pdev->nocache = 1;
4e590803 8644
b1f69b75
AA
8645 dprintk("<-- %s status=%d\n", __func__, status);
8646
8647 return status;
8648}
8649
cd5875fe
TM
8650int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8651 struct pnfs_device *pdev,
8652 struct rpc_cred *cred)
b1f69b75
AA
8653{
8654 struct nfs4_exception exception = { };
8655 int err;
8656
8657 do {
8658 err = nfs4_handle_exception(server,
cd5875fe 8659 _nfs4_proc_getdeviceinfo(server, pdev, cred),
b1f69b75
AA
8660 &exception);
8661 } while (exception.retry);
8662 return err;
8663}
8664EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8665
863a3c6c
AA
8666static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8667{
8668 struct nfs4_layoutcommit_data *data = calldata;
8669 struct nfs_server *server = NFS_SERVER(data->args.inode);
6ba7db34 8670 struct nfs4_session *session = nfs4_get_session(server);
863a3c6c 8671
d9afbd1b
TM
8672 nfs41_setup_sequence(session,
8673 &data->args.seq_args,
8674 &data->res.seq_res,
8675 task);
863a3c6c
AA
8676}
8677
8678static void
8679nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8680{
8681 struct nfs4_layoutcommit_data *data = calldata;
8682 struct nfs_server *server = NFS_SERVER(data->args.inode);
8683
6ba7db34 8684 if (!nfs41_sequence_done(task, &data->res.seq_res))
863a3c6c
AA
8685 return;
8686
8687 switch (task->tk_status) { /* Just ignore these failures */
e59d27e0
TM
8688 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8689 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8690 case -NFS4ERR_BADLAYOUT: /* no layout */
8691 case -NFS4ERR_GRACE: /* loca_recalim always false */
863a3c6c 8692 task->tk_status = 0;
e59d27e0 8693 case 0:
e59d27e0
TM
8694 break;
8695 default:
8478eaa1 8696 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
e59d27e0
TM
8697 rpc_restart_call_prepare(task);
8698 return;
8699 }
8700 }
863a3c6c
AA
8701}
8702
8703static void nfs4_layoutcommit_release(void *calldata)
8704{
8705 struct nfs4_layoutcommit_data *data = calldata;
8706
db29c089 8707 pnfs_cleanup_layoutcommit(data);
d8c951c3
TM
8708 nfs_post_op_update_inode_force_wcc(data->args.inode,
8709 data->res.fattr);
863a3c6c 8710 put_rpccred(data->cred);
472e2594 8711 nfs_iput_and_deactive(data->inode);
863a3c6c
AA
8712 kfree(data);
8713}
8714
8715static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8716 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8717 .rpc_call_done = nfs4_layoutcommit_done,
8718 .rpc_release = nfs4_layoutcommit_release,
8719};
8720
8721int
ef311537 8722nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
863a3c6c
AA
8723{
8724 struct rpc_message msg = {
8725 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8726 .rpc_argp = &data->args,
8727 .rpc_resp = &data->res,
8728 .rpc_cred = data->cred,
8729 };
8730 struct rpc_task_setup task_setup_data = {
8731 .task = &data->task,
8732 .rpc_client = NFS_CLIENT(data->args.inode),
8733 .rpc_message = &msg,
8734 .callback_ops = &nfs4_layoutcommit_ops,
8735 .callback_data = data,
863a3c6c
AA
8736 };
8737 struct rpc_task *task;
8738 int status = 0;
8739
b4839ebe
KM
8740 dprintk("NFS: initiating layoutcommit call. sync %d "
8741 "lbw: %llu inode %lu\n", sync,
863a3c6c
AA
8742 data->args.lastbytewritten,
8743 data->args.inode->i_ino);
8744
472e2594
TM
8745 if (!sync) {
8746 data->inode = nfs_igrab_and_active(data->args.inode);
8747 if (data->inode == NULL) {
8748 nfs4_layoutcommit_release(data);
8749 return -EAGAIN;
8750 }
8751 task_setup_data.flags = RPC_TASK_ASYNC;
8752 }
a9c92d6b 8753 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
863a3c6c
AA
8754 task = rpc_run_task(&task_setup_data);
8755 if (IS_ERR(task))
8756 return PTR_ERR(task);
472e2594
TM
8757 if (sync)
8758 status = task->tk_status;
48c9579a 8759 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
863a3c6c
AA
8760 dprintk("%s: status %d\n", __func__, status);
8761 rpc_put_task(task);
8762 return status;
8763}
fca78d6d 8764
97431204
AA
8765/**
8766 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8767 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8768 */
fca78d6d
BS
8769static int
8770_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
b1b3e136
WAA
8771 struct nfs_fsinfo *info,
8772 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
fca78d6d
BS
8773{
8774 struct nfs41_secinfo_no_name_args args = {
8775 .style = SECINFO_STYLE_CURRENT_FH,
8776 };
8777 struct nfs4_secinfo_res res = {
8778 .flavors = flavors,
8779 };
8780 struct rpc_message msg = {
8781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8782 .rpc_argp = &args,
8783 .rpc_resp = &res,
8784 };
b1b3e136 8785 struct rpc_clnt *clnt = server->client;
7cb852df 8786 struct rpc_cred *cred = NULL;
b1b3e136
WAA
8787 int status;
8788
8789 if (use_integrity) {
8790 clnt = server->nfs_client->cl_rpcclient;
7cb852df
WAA
8791 cred = nfs4_get_clid_cred(server->nfs_client);
8792 msg.rpc_cred = cred;
b1b3e136
WAA
8793 }
8794
8795 dprintk("--> %s\n", __func__);
8796 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8797 &res.seq_res, 0);
8798 dprintk("<-- %s status=%d\n", __func__, status);
8799
7cb852df
WAA
8800 if (cred)
8801 put_rpccred(cred);
b1b3e136
WAA
8802
8803 return status;
fca78d6d
BS
8804}
8805
8806static int
8807nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8808 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8809{
8810 struct nfs4_exception exception = { };
8811 int err;
8812 do {
b1b3e136
WAA
8813 /* first try using integrity protection */
8814 err = -NFS4ERR_WRONGSEC;
8815
8816 /* try to use integrity protection with machine cred */
8817 if (_nfs4_is_integrity_protected(server->nfs_client))
8818 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8819 flavors, true);
8820
8821 /*
8822 * if unable to use integrity protection, or SECINFO with
8823 * integrity protection returns NFS4ERR_WRONGSEC (which is
8824 * disallowed by spec, but exists in deployed servers) use
8825 * the current filesystem's rpc_client and the user cred.
8826 */
8827 if (err == -NFS4ERR_WRONGSEC)
8828 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8829 flavors, false);
8830
fca78d6d
BS
8831 switch (err) {
8832 case 0:
8833 case -NFS4ERR_WRONGSEC:
78b19bae 8834 case -ENOTSUPP:
05e9cfb4 8835 goto out;
fca78d6d
BS
8836 default:
8837 err = nfs4_handle_exception(server, err, &exception);
8838 }
8839 } while (exception.retry);
05e9cfb4 8840out:
fca78d6d
BS
8841 return err;
8842}
8843
8844static int
8845nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8846 struct nfs_fsinfo *info)
8847{
8848 int err;
8849 struct page *page;
367156d9 8850 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
fca78d6d 8851 struct nfs4_secinfo_flavors *flavors;
58a8cf12
WAA
8852 struct nfs4_secinfo4 *secinfo;
8853 int i;
fca78d6d
BS
8854
8855 page = alloc_page(GFP_KERNEL);
8856 if (!page) {
8857 err = -ENOMEM;
8858 goto out;
8859 }
8860
8861 flavors = page_address(page);
8862 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8863
8864 /*
8865 * Fall back on "guess and check" method if
8866 * the server doesn't support SECINFO_NO_NAME
8867 */
78b19bae 8868 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
fca78d6d
BS
8869 err = nfs4_find_root_sec(server, fhandle, info);
8870 goto out_freepage;
8871 }
8872 if (err)
8873 goto out_freepage;
8874
58a8cf12
WAA
8875 for (i = 0; i < flavors->num_flavors; i++) {
8876 secinfo = &flavors->flavors[i];
8877
8878 switch (secinfo->flavor) {
8879 case RPC_AUTH_NULL:
8880 case RPC_AUTH_UNIX:
8881 case RPC_AUTH_GSS:
8882 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8883 &secinfo->flavor_info);
8884 break;
8885 default:
8886 flavor = RPC_AUTH_MAXFLAVOR;
8887 break;
8888 }
8889
4d4b69dd
WAA
8890 if (!nfs_auth_info_match(&server->auth_info, flavor))
8891 flavor = RPC_AUTH_MAXFLAVOR;
8892
58a8cf12
WAA
8893 if (flavor != RPC_AUTH_MAXFLAVOR) {
8894 err = nfs4_lookup_root_sec(server, fhandle,
8895 info, flavor);
8896 if (!err)
8897 break;
8898 }
8899 }
8900
8901 if (flavor == RPC_AUTH_MAXFLAVOR)
8902 err = -EPERM;
fca78d6d
BS
8903
8904out_freepage:
8905 put_page(page);
8906 if (err == -EACCES)
8907 return -EPERM;
8908out:
8909 return err;
8910}
1cab0652 8911
ab7cb0df
TM
8912static int _nfs41_test_stateid(struct nfs_server *server,
8913 nfs4_stateid *stateid,
8914 struct rpc_cred *cred)
7d974794
BS
8915{
8916 int status;
8917 struct nfs41_test_stateid_args args = {
1cab0652 8918 .stateid = stateid,
7d974794
BS
8919 };
8920 struct nfs41_test_stateid_res res;
8921 struct rpc_message msg = {
8922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8923 .rpc_argp = &args,
8924 .rpc_resp = &res,
ab7cb0df 8925 .rpc_cred = cred,
7d974794 8926 };
3787d506
WAA
8927 struct rpc_clnt *rpc_client = server->client;
8928
8929 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8930 &rpc_client, &msg);
1cab0652 8931
38527b15 8932 dprintk("NFS call test_stateid %p\n", stateid);
a9c92d6b 8933 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8fe72bac 8934 nfs4_set_sequence_privileged(&args.seq_args);
3787d506 8935 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8fe72bac 8936 &args.seq_args, &res.seq_res);
38527b15
CL
8937 if (status != NFS_OK) {
8938 dprintk("NFS reply test_stateid: failed, %d\n", status);
377e507d 8939 return status;
38527b15
CL
8940 }
8941 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
377e507d 8942 return -res.status;
7d974794
BS
8943}
8944
43912bbb
TM
8945static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8946 int err, struct nfs4_exception *exception)
8947{
8948 exception->retry = 0;
8949 switch(err) {
8950 case -NFS4ERR_DELAY:
76e8a1bd 8951 case -NFS4ERR_RETRY_UNCACHED_REP:
43912bbb
TM
8952 nfs4_handle_exception(server, err, exception);
8953 break;
8954 case -NFS4ERR_BADSESSION:
8955 case -NFS4ERR_BADSLOT:
8956 case -NFS4ERR_BAD_HIGH_SLOT:
8957 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8958 case -NFS4ERR_DEADSESSION:
8959 nfs4_do_handle_exception(server, err, exception);
8960 }
8961}
8962
38527b15
CL
8963/**
8964 * nfs41_test_stateid - perform a TEST_STATEID operation
8965 *
8966 * @server: server / transport on which to perform the operation
8967 * @stateid: state ID to test
ab7cb0df 8968 * @cred: credential
38527b15
CL
8969 *
8970 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8971 * Otherwise a negative NFS4ERR value is returned if the operation
8972 * failed or the state ID is not currently valid.
8973 */
ab7cb0df
TM
8974static int nfs41_test_stateid(struct nfs_server *server,
8975 nfs4_stateid *stateid,
8976 struct rpc_cred *cred)
7d974794
BS
8977{
8978 struct nfs4_exception exception = { };
8979 int err;
8980 do {
ab7cb0df 8981 err = _nfs41_test_stateid(server, stateid, cred);
43912bbb 8982 nfs4_handle_delay_or_session_error(server, err, &exception);
7d974794
BS
8983 } while (exception.retry);
8984 return err;
8985}
9aeda35f 8986
7c1d5fae
TM
8987struct nfs_free_stateid_data {
8988 struct nfs_server *server;
8989 struct nfs41_free_stateid_args args;
9aeda35f 8990 struct nfs41_free_stateid_res res;
7c1d5fae
TM
8991};
8992
8993static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8994{
8995 struct nfs_free_stateid_data *data = calldata;
8996 nfs41_setup_sequence(nfs4_get_session(data->server),
8997 &data->args.seq_args,
8998 &data->res.seq_res,
8999 task);
9000}
9001
9002static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9003{
9004 struct nfs_free_stateid_data *data = calldata;
9005
9006 nfs41_sequence_done(task, &data->res.seq_res);
9007
9008 switch (task->tk_status) {
9009 case -NFS4ERR_DELAY:
8478eaa1 9010 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
7c1d5fae
TM
9011 rpc_restart_call_prepare(task);
9012 }
9013}
9014
9015static void nfs41_free_stateid_release(void *calldata)
9016{
9017 kfree(calldata);
9018}
9019
17f26b12 9020static const struct rpc_call_ops nfs41_free_stateid_ops = {
7c1d5fae
TM
9021 .rpc_call_prepare = nfs41_free_stateid_prepare,
9022 .rpc_call_done = nfs41_free_stateid_done,
9023 .rpc_release = nfs41_free_stateid_release,
9024};
9025
9026static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
f0b0bf88 9027 const nfs4_stateid *stateid,
ab7cb0df 9028 struct rpc_cred *cred,
7c1d5fae
TM
9029 bool privileged)
9030{
9aeda35f
BS
9031 struct rpc_message msg = {
9032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
ab7cb0df 9033 .rpc_cred = cred,
9aeda35f 9034 };
7c1d5fae
TM
9035 struct rpc_task_setup task_setup = {
9036 .rpc_client = server->client,
9037 .rpc_message = &msg,
9038 .callback_ops = &nfs41_free_stateid_ops,
9039 .flags = RPC_TASK_ASYNC,
9040 };
9041 struct nfs_free_stateid_data *data;
9aeda35f 9042
3787d506
WAA
9043 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9044 &task_setup.rpc_client, &msg);
9045
38527b15 9046 dprintk("NFS call free_stateid %p\n", stateid);
7c1d5fae
TM
9047 data = kmalloc(sizeof(*data), GFP_NOFS);
9048 if (!data)
9049 return ERR_PTR(-ENOMEM);
9050 data->server = server;
9051 nfs4_stateid_copy(&data->args.stateid, stateid);
9052
9053 task_setup.callback_data = data;
9054
9055 msg.rpc_argp = &data->args;
9056 msg.rpc_resp = &data->res;
76e8a1bd 9057 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
7c1d5fae
TM
9058 if (privileged)
9059 nfs4_set_sequence_privileged(&data->args.seq_args);
9060
9061 return rpc_run_task(&task_setup);
9aeda35f
BS
9062}
9063
38527b15
CL
9064/**
9065 * nfs41_free_stateid - perform a FREE_STATEID operation
9066 *
9067 * @server: server / transport on which to perform the operation
9068 * @stateid: state ID to release
ab7cb0df 9069 * @cred: credential
f0b0bf88 9070 * @is_recovery: set to true if this call needs to be privileged
38527b15 9071 *
f0b0bf88 9072 * Note: this function is always asynchronous.
38527b15 9073 */
ab7cb0df 9074static int nfs41_free_stateid(struct nfs_server *server,
f0b0bf88
TM
9075 const nfs4_stateid *stateid,
9076 struct rpc_cred *cred,
9077 bool is_recovery)
9aeda35f 9078{
7c1d5fae 9079 struct rpc_task *task;
7c1d5fae 9080
f0b0bf88 9081 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
7c1d5fae
TM
9082 if (IS_ERR(task))
9083 return PTR_ERR(task);
7c1d5fae 9084 rpc_put_task(task);
f0b0bf88 9085 return 0;
9aeda35f 9086}
36281caa 9087
f1cdae87
JL
9088static void
9089nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
c8b2d0bf 9090{
ab7cb0df 9091 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
c8b2d0bf 9092
f0b0bf88 9093 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
c8b2d0bf 9094 nfs4_free_lock_state(server, lsp);
c8b2d0bf
TM
9095}
9096
36281caa
TM
9097static bool nfs41_match_stateid(const nfs4_stateid *s1,
9098 const nfs4_stateid *s2)
9099{
93b717fd
TM
9100 if (s1->type != s2->type)
9101 return false;
9102
2d2f24ad 9103 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
36281caa
TM
9104 return false;
9105
2d2f24ad 9106 if (s1->seqid == s2->seqid)
36281caa 9107 return true;
2d2f24ad 9108 if (s1->seqid == 0 || s2->seqid == 0)
36281caa
TM
9109 return true;
9110
9111 return false;
9112}
9113
557134a3
AA
9114#endif /* CONFIG_NFS_V4_1 */
9115
36281caa
TM
9116static bool nfs4_match_stateid(const nfs4_stateid *s1,
9117 const nfs4_stateid *s2)
9118{
f597c537 9119 return nfs4_stateid_match(s1, s2);
36281caa
TM
9120}
9121
9122
17280175 9123static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
7eff03ae 9124 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
b79a4a1b 9125 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
1da177e4
LT
9126 .recover_open = nfs4_open_reclaim,
9127 .recover_lock = nfs4_lock_reclaim,
591d71cb 9128 .establish_clid = nfs4_init_clientid,
05f4c350 9129 .detect_trunking = nfs40_discover_server_trunking,
1da177e4
LT
9130};
9131
591d71cb 9132#if defined(CONFIG_NFS_V4_1)
17280175 9133static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
591d71cb
AA
9134 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9135 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9136 .recover_open = nfs4_open_reclaim,
9137 .recover_lock = nfs4_lock_reclaim,
4d643d1d 9138 .establish_clid = nfs41_init_clientid,
fce5c838 9139 .reclaim_complete = nfs41_proc_reclaim_complete,
05f4c350 9140 .detect_trunking = nfs41_discover_server_trunking,
591d71cb
AA
9141};
9142#endif /* CONFIG_NFS_V4_1 */
9143
17280175 9144static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
591d71cb
AA
9145 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9146 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
4dfd4f7a 9147 .recover_open = nfs40_open_expired,
591d71cb
AA
9148 .recover_lock = nfs4_lock_expired,
9149 .establish_clid = nfs4_init_clientid,
9150};
9151
9152#if defined(CONFIG_NFS_V4_1)
17280175 9153static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
7eff03ae 9154 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
b79a4a1b 9155 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
f062eb6c
BS
9156 .recover_open = nfs41_open_expired,
9157 .recover_lock = nfs41_lock_expired,
4d643d1d 9158 .establish_clid = nfs41_init_clientid,
1da177e4 9159};
591d71cb 9160#endif /* CONFIG_NFS_V4_1 */
1da177e4 9161
17280175 9162static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
29fba38b 9163 .sched_state_renewal = nfs4_proc_async_renew,
a7b72103 9164 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
8e69514f 9165 .renew_lease = nfs4_proc_renew,
29fba38b
BH
9166};
9167
9168#if defined(CONFIG_NFS_V4_1)
17280175 9169static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
29fba38b 9170 .sched_state_renewal = nfs41_proc_async_sequence,
a7b72103 9171 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
8e69514f 9172 .renew_lease = nfs4_proc_sequence,
29fba38b
BH
9173};
9174#endif
9175
ec011fe8 9176static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
b03d735b 9177 .get_locations = _nfs40_proc_get_locations,
44c99933 9178 .fsid_present = _nfs40_proc_fsid_present,
ec011fe8
CL
9179};
9180
9181#if defined(CONFIG_NFS_V4_1)
9182static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
b03d735b 9183 .get_locations = _nfs41_proc_get_locations,
44c99933 9184 .fsid_present = _nfs41_proc_fsid_present,
ec011fe8
CL
9185};
9186#endif /* CONFIG_NFS_V4_1 */
9187
97dc1359
TM
9188static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9189 .minor_version = 0,
39c6daae
TM
9190 .init_caps = NFS_CAP_READDIRPLUS
9191 | NFS_CAP_ATOMIC_OPEN
39c6daae 9192 | NFS_CAP_POSIX_LOCK,
abf79bb3
CL
9193 .init_client = nfs40_init_client,
9194 .shutdown_client = nfs40_shutdown_client,
36281caa 9195 .match_stateid = nfs4_match_stateid,
fca78d6d 9196 .find_root_sec = nfs4_find_root_sec,
c8b2d0bf 9197 .free_lock_state = nfs4_release_lockowner,
45870d69 9198 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
63f5f796 9199 .alloc_seqid = nfs_alloc_seqid,
9915ea7e 9200 .call_sync_ops = &nfs40_call_sync_ops,
c48f4f35
TM
9201 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9202 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9203 .state_renewal_ops = &nfs40_state_renewal_ops,
ec011fe8 9204 .mig_recovery_ops = &nfs40_mig_recovery_ops,
97dc1359
TM
9205};
9206
9207#if defined(CONFIG_NFS_V4_1)
63f5f796
TM
9208static struct nfs_seqid *
9209nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9210{
9211 return NULL;
9212}
9213
97dc1359
TM
9214static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9215 .minor_version = 1,
39c6daae
TM
9216 .init_caps = NFS_CAP_READDIRPLUS
9217 | NFS_CAP_ATOMIC_OPEN
3b66486c 9218 | NFS_CAP_POSIX_LOCK
49f9a0fa 9219 | NFS_CAP_STATEID_NFSV41
e983120e 9220 | NFS_CAP_ATOMIC_OPEN_V1,
abf79bb3
CL
9221 .init_client = nfs41_init_client,
9222 .shutdown_client = nfs41_shutdown_client,
36281caa 9223 .match_stateid = nfs41_match_stateid,
fca78d6d 9224 .find_root_sec = nfs41_find_root_sec,
c8b2d0bf 9225 .free_lock_state = nfs41_free_lock_state,
45870d69 9226 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
63f5f796 9227 .alloc_seqid = nfs_alloc_no_seqid,
04fa2c6b 9228 .session_trunk = nfs4_test_session_trunk,
9915ea7e 9229 .call_sync_ops = &nfs41_call_sync_ops,
c48f4f35
TM
9230 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9231 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9232 .state_renewal_ops = &nfs41_state_renewal_ops,
ec011fe8 9233 .mig_recovery_ops = &nfs41_mig_recovery_ops,
97dc1359
TM
9234};
9235#endif
9236
42c2c424
SD
9237#if defined(CONFIG_NFS_V4_2)
9238static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9239 .minor_version = 2,
7017310a
BS
9240 .init_caps = NFS_CAP_READDIRPLUS
9241 | NFS_CAP_ATOMIC_OPEN
7017310a
BS
9242 | NFS_CAP_POSIX_LOCK
9243 | NFS_CAP_STATEID_NFSV41
e983120e 9244 | NFS_CAP_ATOMIC_OPEN_V1
f4ac1674 9245 | NFS_CAP_ALLOCATE
2e72448b 9246 | NFS_CAP_COPY
624bd5b7 9247 | NFS_CAP_DEALLOCATE
6c5a0d89 9248 | NFS_CAP_SEEK
e5341f3a
PT
9249 | NFS_CAP_LAYOUTSTATS
9250 | NFS_CAP_CLONE,
abf79bb3
CL
9251 .init_client = nfs41_init_client,
9252 .shutdown_client = nfs41_shutdown_client,
42c2c424
SD
9253 .match_stateid = nfs41_match_stateid,
9254 .find_root_sec = nfs41_find_root_sec,
7017310a 9255 .free_lock_state = nfs41_free_lock_state,
9915ea7e 9256 .call_sync_ops = &nfs41_call_sync_ops,
45870d69 9257 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
63f5f796 9258 .alloc_seqid = nfs_alloc_no_seqid,
04fa2c6b 9259 .session_trunk = nfs4_test_session_trunk,
42c2c424
SD
9260 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9261 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9262 .state_renewal_ops = &nfs41_state_renewal_ops,
18e3b739 9263 .mig_recovery_ops = &nfs41_mig_recovery_ops,
42c2c424
SD
9264};
9265#endif
9266
97dc1359
TM
9267const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9268 [0] = &nfs_v4_0_minor_ops,
9269#if defined(CONFIG_NFS_V4_1)
9270 [1] = &nfs_v4_1_minor_ops,
9271#endif
42c2c424
SD
9272#if defined(CONFIG_NFS_V4_2)
9273 [2] = &nfs_v4_2_minor_ops,
9274#endif
97dc1359
TM
9275};
9276
13997823 9277static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
c4803c49
AG
9278{
9279 ssize_t error, error2;
9280
9281 error = generic_listxattr(dentry, list, size);
9282 if (error < 0)
9283 return error;
9284 if (list) {
9285 list += error;
9286 size -= error;
9287 }
9288
9289 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9290 if (error2 < 0)
9291 return error2;
9292 return error + error2;
9293}
9294
17f26b12 9295static const struct inode_operations nfs4_dir_inode_operations = {
73a79706
BS
9296 .create = nfs_create,
9297 .lookup = nfs_lookup,
9298 .atomic_open = nfs_atomic_open,
9299 .link = nfs_link,
9300 .unlink = nfs_unlink,
9301 .symlink = nfs_symlink,
9302 .mkdir = nfs_mkdir,
9303 .rmdir = nfs_rmdir,
9304 .mknod = nfs_mknod,
9305 .rename = nfs_rename,
9306 .permission = nfs_permission,
9307 .getattr = nfs_getattr,
9308 .setattr = nfs_setattr,
9309 .getxattr = generic_getxattr,
9310 .setxattr = generic_setxattr,
c4803c49 9311 .listxattr = nfs4_listxattr,
73a79706
BS
9312 .removexattr = generic_removexattr,
9313};
9314
92e1d5be 9315static const struct inode_operations nfs4_file_inode_operations = {
6b3b5496
BF
9316 .permission = nfs_permission,
9317 .getattr = nfs_getattr,
9318 .setattr = nfs_setattr,
64c2ce8b
AK
9319 .getxattr = generic_getxattr,
9320 .setxattr = generic_setxattr,
c4803c49 9321 .listxattr = nfs4_listxattr,
64c2ce8b 9322 .removexattr = generic_removexattr,
6b3b5496
BF
9323};
9324
509de811 9325const struct nfs_rpc_ops nfs_v4_clientops = {
1da177e4
LT
9326 .version = 4, /* protocol version */
9327 .dentry_ops = &nfs4_dentry_operations,
9328 .dir_inode_ops = &nfs4_dir_inode_operations,
6b3b5496 9329 .file_inode_ops = &nfs4_file_inode_operations,
1788ea6e 9330 .file_ops = &nfs4_file_operations,
1da177e4 9331 .getroot = nfs4_proc_get_root,
281cad46 9332 .submount = nfs4_submount,
ff9099f2 9333 .try_mount = nfs4_try_mount,
1da177e4
LT
9334 .getattr = nfs4_proc_getattr,
9335 .setattr = nfs4_proc_setattr,
9336 .lookup = nfs4_proc_lookup,
9337 .access = nfs4_proc_access,
9338 .readlink = nfs4_proc_readlink,
1da177e4
LT
9339 .create = nfs4_proc_create,
9340 .remove = nfs4_proc_remove,
9341 .unlink_setup = nfs4_proc_unlink_setup,
34e137cc 9342 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
1da177e4 9343 .unlink_done = nfs4_proc_unlink_done,
d3d4152a 9344 .rename_setup = nfs4_proc_rename_setup,
c6bfa1a1 9345 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
d3d4152a 9346 .rename_done = nfs4_proc_rename_done,
1da177e4
LT
9347 .link = nfs4_proc_link,
9348 .symlink = nfs4_proc_symlink,
9349 .mkdir = nfs4_proc_mkdir,
9350 .rmdir = nfs4_proc_remove,
9351 .readdir = nfs4_proc_readdir,
9352 .mknod = nfs4_proc_mknod,
9353 .statfs = nfs4_proc_statfs,
9354 .fsinfo = nfs4_proc_fsinfo,
9355 .pathconf = nfs4_proc_pathconf,
e9326dca 9356 .set_capabilities = nfs4_server_capabilities,
1da177e4 9357 .decode_dirent = nfs4_decode_dirent,
a4cdda59 9358 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
1da177e4 9359 .read_setup = nfs4_proc_read_setup,
ec06c096 9360 .read_done = nfs4_read_done,
1da177e4 9361 .write_setup = nfs4_proc_write_setup,
788e7a89 9362 .write_done = nfs4_write_done,
1da177e4 9363 .commit_setup = nfs4_proc_commit_setup,
0b7c0153 9364 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
788e7a89 9365 .commit_done = nfs4_commit_done,
1da177e4 9366 .lock = nfs4_proc_lock,
e50a1c2e 9367 .clear_acl_cache = nfs4_zap_acl_attr,
7fe5c398 9368 .close_context = nfs4_close_context,
2b484297 9369 .open_context = nfs4_atomic_open,
011e2a7f 9370 .have_delegation = nfs4_have_delegation,
57ec14c5 9371 .return_delegation = nfs4_inode_return_delegation,
6663ee7f 9372 .alloc_client = nfs4_alloc_client,
45a52a02 9373 .init_client = nfs4_init_client,
cdb7eced 9374 .free_client = nfs4_free_client,
1179acc6
BS
9375 .create_server = nfs4_create_server,
9376 .clone_server = nfs_clone_server,
1da177e4
LT
9377};
9378
64c2ce8b 9379static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
98e9cb57 9380 .name = XATTR_NAME_NFSV4_ACL,
64c2ce8b
AK
9381 .list = nfs4_xattr_list_nfs4_acl,
9382 .get = nfs4_xattr_get_nfs4_acl,
9383 .set = nfs4_xattr_set_nfs4_acl,
9384};
9385
9386const struct xattr_handler *nfs4_xattr_handlers[] = {
9387 &nfs4_xattr_nfs4_acl_handler,
c9bccef6
DQ
9388#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9389 &nfs4_xattr_nfs4_label_handler,
9390#endif
64c2ce8b
AK
9391 NULL
9392};
9393
1da177e4
LT
9394/*
9395 * Local variables:
9396 * c-basic-offset: 8
9397 * End:
9398 */