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