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