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