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