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