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