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