4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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.
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>
60 #include "delegation.h"
67 #include "nfs4idmap.h"
68 #include "nfs4session.h"
72 #include "nfs4trace.h"
74 #define NFSDBG_FACILITY NFSDBG_PROC
76 #define NFS4_BITMASK_SZ 3
78 #define NFS4_POLL_RETRY_MIN (HZ/10)
79 #define NFS4_POLL_RETRY_MAX (15*HZ)
81 /* file attributes which can be mapped to nfs attributes */
82 #define NFS4_VALID_ATTRS (ATTR_MODE \
93 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
94 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
95 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
96 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
97 struct nfs_fattr *fattr, struct inode *inode);
98 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
99 struct nfs_fattr *fattr, struct iattr *sattr,
100 struct nfs_open_context *ctx, struct nfs4_label *ilabel);
101 #ifdef CONFIG_NFS_V4_1
102 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
103 const struct cred *cred,
104 struct nfs4_slot *slot,
106 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
107 const struct cred *);
108 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
109 const struct cred *, bool);
112 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
113 static inline struct nfs4_label *
114 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
115 struct iattr *sattr, struct nfs4_label *label)
122 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
125 err = security_dentry_init_security(dentry, sattr->ia_mode,
126 &dentry->d_name, NULL,
127 (void **)&label->label, &label->len);
134 nfs4_label_release_security(struct nfs4_label *label)
137 security_release_secctx(label->label, label->len);
139 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
142 return server->attr_bitmask;
144 return server->attr_bitmask_nl;
147 static inline struct nfs4_label *
148 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
149 struct iattr *sattr, struct nfs4_label *l)
152 nfs4_label_release_security(struct nfs4_label *label)
155 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
156 { return server->attr_bitmask; }
159 /* Prevent leaks of NFSv4 errors into userland */
160 static int nfs4_map_errors(int err)
165 case -NFS4ERR_RESOURCE:
166 case -NFS4ERR_LAYOUTTRYLATER:
167 case -NFS4ERR_RECALLCONFLICT:
169 case -NFS4ERR_WRONGSEC:
170 case -NFS4ERR_WRONG_CRED:
172 case -NFS4ERR_BADOWNER:
173 case -NFS4ERR_BADNAME:
175 case -NFS4ERR_SHARE_DENIED:
177 case -NFS4ERR_MINOR_VERS_MISMATCH:
178 return -EPROTONOSUPPORT;
179 case -NFS4ERR_FILE_OPEN:
181 case -NFS4ERR_NOT_SAME:
184 dprintk("%s could not handle NFSv4 error %d\n",
192 * This is our standard bitmap for GETATTR requests.
194 const u32 nfs4_fattr_bitmap[3] = {
196 | FATTR4_WORD0_CHANGE
199 | FATTR4_WORD0_FILEID,
201 | FATTR4_WORD1_NUMLINKS
203 | FATTR4_WORD1_OWNER_GROUP
204 | FATTR4_WORD1_RAWDEV
205 | FATTR4_WORD1_SPACE_USED
206 | FATTR4_WORD1_TIME_ACCESS
207 | FATTR4_WORD1_TIME_METADATA
208 | FATTR4_WORD1_TIME_MODIFY
209 | FATTR4_WORD1_MOUNTED_ON_FILEID,
210 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
211 FATTR4_WORD2_SECURITY_LABEL
215 static const u32 nfs4_pnfs_open_bitmap[3] = {
217 | FATTR4_WORD0_CHANGE
220 | FATTR4_WORD0_FILEID,
222 | FATTR4_WORD1_NUMLINKS
224 | FATTR4_WORD1_OWNER_GROUP
225 | FATTR4_WORD1_RAWDEV
226 | FATTR4_WORD1_SPACE_USED
227 | FATTR4_WORD1_TIME_ACCESS
228 | FATTR4_WORD1_TIME_METADATA
229 | FATTR4_WORD1_TIME_MODIFY,
230 FATTR4_WORD2_MDSTHRESHOLD
231 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
232 | FATTR4_WORD2_SECURITY_LABEL
236 static const u32 nfs4_open_noattr_bitmap[3] = {
238 | FATTR4_WORD0_FILEID,
241 const u32 nfs4_statfs_bitmap[3] = {
242 FATTR4_WORD0_FILES_AVAIL
243 | FATTR4_WORD0_FILES_FREE
244 | FATTR4_WORD0_FILES_TOTAL,
245 FATTR4_WORD1_SPACE_AVAIL
246 | FATTR4_WORD1_SPACE_FREE
247 | FATTR4_WORD1_SPACE_TOTAL
250 const u32 nfs4_pathconf_bitmap[3] = {
252 | FATTR4_WORD0_MAXNAME,
256 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
257 | FATTR4_WORD0_MAXREAD
258 | FATTR4_WORD0_MAXWRITE
259 | FATTR4_WORD0_LEASE_TIME,
260 FATTR4_WORD1_TIME_DELTA
261 | FATTR4_WORD1_FS_LAYOUT_TYPES,
262 FATTR4_WORD2_LAYOUT_BLKSIZE
263 | FATTR4_WORD2_CLONE_BLKSIZE
264 | FATTR4_WORD2_CHANGE_ATTR_TYPE
265 | FATTR4_WORD2_XATTR_SUPPORT
268 const u32 nfs4_fs_locations_bitmap[3] = {
272 | FATTR4_WORD0_FILEID
273 | FATTR4_WORD0_FS_LOCATIONS,
275 | FATTR4_WORD1_OWNER_GROUP
276 | FATTR4_WORD1_RAWDEV
277 | FATTR4_WORD1_SPACE_USED
278 | FATTR4_WORD1_TIME_ACCESS
279 | FATTR4_WORD1_TIME_METADATA
280 | FATTR4_WORD1_TIME_MODIFY
281 | FATTR4_WORD1_MOUNTED_ON_FILEID,
284 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
285 struct inode *inode, unsigned long flags)
287 unsigned long cache_validity;
289 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
290 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
293 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags;
295 /* Remove the attributes over which we have full control */
296 dst[1] &= ~FATTR4_WORD1_RAWDEV;
297 if (!(cache_validity & NFS_INO_INVALID_SIZE))
298 dst[0] &= ~FATTR4_WORD0_SIZE;
300 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
301 dst[0] &= ~FATTR4_WORD0_CHANGE;
303 if (!(cache_validity & NFS_INO_INVALID_MODE))
304 dst[1] &= ~FATTR4_WORD1_MODE;
305 if (!(cache_validity & NFS_INO_INVALID_OTHER))
306 dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP);
309 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
310 struct nfs4_readdir_arg *readdir)
312 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
316 readdir->cookie = cookie;
317 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
322 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
327 * NFSv4 servers do not return entries for '.' and '..'
328 * Therefore, we fake these entries here. We let '.'
329 * have cookie 0 and '..' have cookie 1. Note that
330 * when talking to the server, we always send cookie 0
333 start = p = kmap_atomic(*readdir->pages);
336 *p++ = xdr_one; /* next */
337 *p++ = xdr_zero; /* cookie, first word */
338 *p++ = xdr_one; /* cookie, second word */
339 *p++ = xdr_one; /* entry len */
340 memcpy(p, ".\0\0\0", 4); /* entry */
342 *p++ = xdr_one; /* bitmap length */
343 *p++ = htonl(attrs); /* bitmap */
344 *p++ = htonl(12); /* attribute buffer length */
345 *p++ = htonl(NF4DIR);
346 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
349 *p++ = xdr_one; /* next */
350 *p++ = xdr_zero; /* cookie, first word */
351 *p++ = xdr_two; /* cookie, second word */
352 *p++ = xdr_two; /* entry len */
353 memcpy(p, "..\0\0", 4); /* entry */
355 *p++ = xdr_one; /* bitmap length */
356 *p++ = htonl(attrs); /* bitmap */
357 *p++ = htonl(12); /* attribute buffer length */
358 *p++ = htonl(NF4DIR);
359 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
361 readdir->pgbase = (char *)p - (char *)start;
362 readdir->count -= readdir->pgbase;
363 kunmap_atomic(start);
366 static void nfs4_fattr_set_prechange(struct nfs_fattr *fattr, u64 version)
368 if (!(fattr->valid & NFS_ATTR_FATTR_PRECHANGE)) {
369 fattr->pre_change_attr = version;
370 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
374 static void nfs4_test_and_free_stateid(struct nfs_server *server,
375 nfs4_stateid *stateid,
376 const struct cred *cred)
378 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
380 ops->test_and_free_expired(server, stateid, cred);
383 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
384 nfs4_stateid *stateid,
385 const struct cred *cred)
387 stateid->type = NFS4_REVOKED_STATEID_TYPE;
388 nfs4_test_and_free_stateid(server, stateid, cred);
391 static void nfs4_free_revoked_stateid(struct nfs_server *server,
392 const nfs4_stateid *stateid,
393 const struct cred *cred)
397 nfs4_stateid_copy(&tmp, stateid);
398 __nfs4_free_revoked_stateid(server, &tmp, cred);
401 static long nfs4_update_delay(long *timeout)
405 return NFS4_POLL_RETRY_MAX;
407 *timeout = NFS4_POLL_RETRY_MIN;
408 if (*timeout > NFS4_POLL_RETRY_MAX)
409 *timeout = NFS4_POLL_RETRY_MAX;
415 static int nfs4_delay_killable(long *timeout)
419 freezable_schedule_timeout_killable_unsafe(
420 nfs4_update_delay(timeout));
421 if (!__fatal_signal_pending(current))
426 static int nfs4_delay_interruptible(long *timeout)
430 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
431 if (!signal_pending(current))
433 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
436 static int nfs4_delay(long *timeout, bool interruptible)
439 return nfs4_delay_interruptible(timeout);
440 return nfs4_delay_killable(timeout);
443 static const nfs4_stateid *
444 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
448 switch (stateid->type) {
449 case NFS4_OPEN_STATEID_TYPE:
450 case NFS4_LOCK_STATEID_TYPE:
451 case NFS4_DELEGATION_STATEID_TYPE:
459 /* This is the error handling routine for processes that are allowed
462 static int nfs4_do_handle_exception(struct nfs_server *server,
463 int errorcode, struct nfs4_exception *exception)
465 struct nfs_client *clp = server->nfs_client;
466 struct nfs4_state *state = exception->state;
467 const nfs4_stateid *stateid;
468 struct inode *inode = exception->inode;
471 exception->delay = 0;
472 exception->recovering = 0;
473 exception->retry = 0;
475 stateid = nfs4_recoverable_stateid(exception->stateid);
476 if (stateid == NULL && state != NULL)
477 stateid = nfs4_recoverable_stateid(&state->stateid);
482 case -NFS4ERR_BADHANDLE:
484 if (inode != NULL && S_ISREG(inode->i_mode))
485 pnfs_destroy_layout(NFS_I(inode));
487 case -NFS4ERR_DELEG_REVOKED:
488 case -NFS4ERR_ADMIN_REVOKED:
489 case -NFS4ERR_EXPIRED:
490 case -NFS4ERR_BAD_STATEID:
491 case -NFS4ERR_PARTNER_NO_AUTH:
492 if (inode != NULL && stateid != NULL) {
493 nfs_inode_find_state_and_recover(inode,
495 goto wait_on_recovery;
498 case -NFS4ERR_OPENMODE:
502 err = nfs_async_inode_return_delegation(inode,
505 goto wait_on_recovery;
506 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
507 exception->retry = 1;
513 ret = nfs4_schedule_stateid_recovery(server, state);
516 goto wait_on_recovery;
517 case -NFS4ERR_STALE_STATEID:
518 case -NFS4ERR_STALE_CLIENTID:
519 nfs4_schedule_lease_recovery(clp);
520 goto wait_on_recovery;
522 ret = nfs4_schedule_migration_recovery(server);
525 goto wait_on_recovery;
526 case -NFS4ERR_LEASE_MOVED:
527 nfs4_schedule_lease_moved_recovery(clp);
528 goto wait_on_recovery;
529 #if defined(CONFIG_NFS_V4_1)
530 case -NFS4ERR_BADSESSION:
531 case -NFS4ERR_BADSLOT:
532 case -NFS4ERR_BAD_HIGH_SLOT:
533 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
534 case -NFS4ERR_DEADSESSION:
535 case -NFS4ERR_SEQ_FALSE_RETRY:
536 case -NFS4ERR_SEQ_MISORDERED:
537 /* Handled in nfs41_sequence_process() */
538 goto wait_on_recovery;
539 #endif /* defined(CONFIG_NFS_V4_1) */
540 case -NFS4ERR_FILE_OPEN:
541 if (exception->timeout > HZ) {
542 /* We have retried a decent amount, time to
550 nfs_inc_server_stats(server, NFSIOS_DELAY);
553 case -NFS4ERR_LAYOUTTRYLATER:
554 case -NFS4ERR_RECALLCONFLICT:
555 exception->delay = 1;
558 case -NFS4ERR_RETRY_UNCACHED_REP:
559 case -NFS4ERR_OLD_STATEID:
560 exception->retry = 1;
562 case -NFS4ERR_BADOWNER:
563 /* The following works around a Linux server bug! */
564 case -NFS4ERR_BADNAME:
565 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
566 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
567 exception->retry = 1;
568 printk(KERN_WARNING "NFS: v4 server %s "
569 "does not accept raw "
571 "Reenabling the idmapper.\n",
572 server->nfs_client->cl_hostname);
575 /* We failed to handle the error */
576 return nfs4_map_errors(ret);
578 exception->recovering = 1;
582 /* This is the error handling routine for processes that are allowed
585 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
587 struct nfs_client *clp = server->nfs_client;
590 ret = nfs4_do_handle_exception(server, errorcode, exception);
591 if (exception->delay) {
592 ret = nfs4_delay(&exception->timeout,
593 exception->interruptible);
596 if (exception->recovering) {
597 if (exception->task_is_privileged)
599 ret = nfs4_wait_clnt_recover(clp);
600 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
607 exception->retry = 1;
612 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
613 int errorcode, struct nfs4_exception *exception)
615 struct nfs_client *clp = server->nfs_client;
618 ret = nfs4_do_handle_exception(server, errorcode, exception);
619 if (exception->delay) {
620 rpc_delay(task, nfs4_update_delay(&exception->timeout));
623 if (exception->recovering) {
624 if (exception->task_is_privileged)
626 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
627 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
628 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
631 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
636 exception->retry = 1;
638 * For NFS4ERR_MOVED, the client transport will need to
639 * be recomputed after migration recovery has completed.
641 if (errorcode == -NFS4ERR_MOVED)
642 rpc_task_release_transport(task);
648 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
649 struct nfs4_state *state, long *timeout)
651 struct nfs4_exception exception = {
655 if (task->tk_status >= 0)
658 exception.timeout = *timeout;
659 task->tk_status = nfs4_async_handle_exception(task, server,
662 if (exception.delay && timeout)
663 *timeout = exception.timeout;
670 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
671 * or 'false' otherwise.
673 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
675 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
676 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
679 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
681 spin_lock(&clp->cl_lock);
682 if (time_before(clp->cl_last_renewal,timestamp))
683 clp->cl_last_renewal = timestamp;
684 spin_unlock(&clp->cl_lock);
687 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
689 struct nfs_client *clp = server->nfs_client;
691 if (!nfs4_has_session(clp))
692 do_renew_lease(clp, timestamp);
695 struct nfs4_call_sync_data {
696 const struct nfs_server *seq_server;
697 struct nfs4_sequence_args *seq_args;
698 struct nfs4_sequence_res *seq_res;
701 void nfs4_init_sequence(struct nfs4_sequence_args *args,
702 struct nfs4_sequence_res *res, int cache_reply,
705 args->sa_slot = NULL;
706 args->sa_cache_this = cache_reply;
707 args->sa_privileged = privileged;
712 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
714 struct nfs4_slot *slot = res->sr_slot;
715 struct nfs4_slot_table *tbl;
718 spin_lock(&tbl->slot_tbl_lock);
719 if (!nfs41_wake_and_assign_slot(tbl, slot))
720 nfs4_free_slot(tbl, slot);
721 spin_unlock(&tbl->slot_tbl_lock);
726 static int nfs40_sequence_done(struct rpc_task *task,
727 struct nfs4_sequence_res *res)
729 if (res->sr_slot != NULL)
730 nfs40_sequence_free_slot(res);
734 #if defined(CONFIG_NFS_V4_1)
736 static void nfs41_release_slot(struct nfs4_slot *slot)
738 struct nfs4_session *session;
739 struct nfs4_slot_table *tbl;
740 bool send_new_highest_used_slotid = false;
745 session = tbl->session;
747 /* Bump the slot sequence number */
752 spin_lock(&tbl->slot_tbl_lock);
753 /* Be nice to the server: try to ensure that the last transmitted
754 * value for highest_user_slotid <= target_highest_slotid
756 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
757 send_new_highest_used_slotid = true;
759 if (nfs41_wake_and_assign_slot(tbl, slot)) {
760 send_new_highest_used_slotid = false;
763 nfs4_free_slot(tbl, slot);
765 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
766 send_new_highest_used_slotid = false;
768 spin_unlock(&tbl->slot_tbl_lock);
769 if (send_new_highest_used_slotid)
770 nfs41_notify_server(session->clp);
771 if (waitqueue_active(&tbl->slot_waitq))
772 wake_up_all(&tbl->slot_waitq);
775 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
777 nfs41_release_slot(res->sr_slot);
781 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
784 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
785 slot->seq_nr_highest_sent = seqnr;
787 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot, u32 seqnr)
789 nfs4_slot_sequence_record_sent(slot, seqnr);
790 slot->seq_nr_last_acked = seqnr;
793 static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
794 struct nfs4_slot *slot)
796 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
798 rpc_put_task_async(task);
801 static int nfs41_sequence_process(struct rpc_task *task,
802 struct nfs4_sequence_res *res)
804 struct nfs4_session *session;
805 struct nfs4_slot *slot = res->sr_slot;
806 struct nfs_client *clp;
812 /* don't increment the sequence number if the task wasn't sent */
813 if (!RPC_WAS_SENT(task) || slot->seq_done)
816 session = slot->table->session;
819 trace_nfs4_sequence_done(session, res);
821 status = res->sr_status;
822 if (task->tk_status == -NFS4ERR_DEADSESSION)
823 status = -NFS4ERR_DEADSESSION;
825 /* Check the SEQUENCE operation status */
828 /* Mark this sequence number as having been acked */
829 nfs4_slot_sequence_acked(slot, slot->seq_nr);
830 /* Update the slot's sequence and clientid lease timer */
832 do_renew_lease(clp, res->sr_timestamp);
833 /* Check sequence flags */
834 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
836 nfs41_update_target_slotid(slot->table, slot, res);
840 * sr_status remains 1 if an RPC level error occurred.
841 * The server may or may not have processed the sequence
844 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
848 /* The server detected a resend of the RPC call and
849 * returned NFS4ERR_DELAY as per Section 2.10.6.2
852 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
857 case -NFS4ERR_RETRY_UNCACHED_REP:
858 case -NFS4ERR_SEQ_FALSE_RETRY:
860 * The server thinks we tried to replay a request.
861 * Retry the call after bumping the sequence ID.
863 nfs4_slot_sequence_acked(slot, slot->seq_nr);
865 case -NFS4ERR_BADSLOT:
867 * The slot id we used was probably retired. Try again
868 * using a different slot id.
870 if (slot->slot_nr < slot->table->target_highest_slotid)
871 goto session_recover;
873 case -NFS4ERR_SEQ_MISORDERED:
874 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
876 * Were one or more calls using this slot interrupted?
877 * If the server never received the request, then our
878 * transmitted slot sequence number may be too high. However,
879 * if the server did receive the request then it might
880 * accidentally give us a reply with a mismatched operation.
881 * We can sort this out by sending a lone sequence operation
882 * to the server on the same slot.
884 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
886 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
887 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
894 * A retry might be sent while the original request is
895 * still in progress on the replier. The replier SHOULD
896 * deal with the issue by returning NFS4ERR_DELAY as the
897 * reply to SEQUENCE or CB_SEQUENCE operation, but
898 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
900 * Restart the search after a delay.
902 slot->seq_nr = slot->seq_nr_highest_sent;
904 case -NFS4ERR_BADSESSION:
905 case -NFS4ERR_DEADSESSION:
906 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
907 goto session_recover;
909 /* Just update the slot sequence no. */
913 /* The session may be reset by one of the error handlers. */
914 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
918 nfs4_schedule_session_recovery(session, status);
919 dprintk("%s ERROR: %d Reset session\n", __func__, status);
920 nfs41_sequence_free_slot(res);
925 if (rpc_restart_call_prepare(task)) {
926 nfs41_sequence_free_slot(res);
932 if (!rpc_restart_call(task))
934 rpc_delay(task, NFS4_POLL_RETRY_MAX);
938 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
940 if (!nfs41_sequence_process(task, res))
942 if (res->sr_slot != NULL)
943 nfs41_sequence_free_slot(res);
947 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
949 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
951 if (res->sr_slot == NULL)
953 if (res->sr_slot->table->session != NULL)
954 return nfs41_sequence_process(task, res);
955 return nfs40_sequence_done(task, res);
958 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
960 if (res->sr_slot != NULL) {
961 if (res->sr_slot->table->session != NULL)
962 nfs41_sequence_free_slot(res);
964 nfs40_sequence_free_slot(res);
968 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
970 if (res->sr_slot == NULL)
972 if (!res->sr_slot->table->session)
973 return nfs40_sequence_done(task, res);
974 return nfs41_sequence_done(task, res);
976 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
978 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
980 struct nfs4_call_sync_data *data = calldata;
982 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
984 nfs4_setup_sequence(data->seq_server->nfs_client,
985 data->seq_args, data->seq_res, task);
988 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
990 struct nfs4_call_sync_data *data = calldata;
992 nfs41_sequence_done(task, data->seq_res);
995 static const struct rpc_call_ops nfs41_call_sync_ops = {
996 .rpc_call_prepare = nfs41_call_sync_prepare,
997 .rpc_call_done = nfs41_call_sync_done,
1000 #else /* !CONFIG_NFS_V4_1 */
1002 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1004 return nfs40_sequence_done(task, res);
1007 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1009 if (res->sr_slot != NULL)
1010 nfs40_sequence_free_slot(res);
1013 int nfs4_sequence_done(struct rpc_task *task,
1014 struct nfs4_sequence_res *res)
1016 return nfs40_sequence_done(task, res);
1018 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1020 #endif /* !CONFIG_NFS_V4_1 */
1022 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1024 res->sr_timestamp = jiffies;
1025 res->sr_status_flags = 0;
1030 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1031 struct nfs4_sequence_res *res,
1032 struct nfs4_slot *slot)
1036 slot->privileged = args->sa_privileged ? 1 : 0;
1037 args->sa_slot = slot;
1039 res->sr_slot = slot;
1042 int nfs4_setup_sequence(struct nfs_client *client,
1043 struct nfs4_sequence_args *args,
1044 struct nfs4_sequence_res *res,
1045 struct rpc_task *task)
1047 struct nfs4_session *session = nfs4_get_session(client);
1048 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1049 struct nfs4_slot *slot;
1051 /* slot already allocated? */
1052 if (res->sr_slot != NULL)
1056 tbl = &session->fc_slot_table;
1058 spin_lock(&tbl->slot_tbl_lock);
1059 /* The state manager will wait until the slot table is empty */
1060 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1063 slot = nfs4_alloc_slot(tbl);
1065 if (slot == ERR_PTR(-ENOMEM))
1066 goto out_sleep_timeout;
1069 spin_unlock(&tbl->slot_tbl_lock);
1071 nfs4_sequence_attach_slot(args, res, slot);
1073 trace_nfs4_setup_sequence(session, args);
1075 nfs41_sequence_res_init(res);
1076 rpc_call_start(task);
1079 /* Try again in 1/4 second */
1080 if (args->sa_privileged)
1081 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1082 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1084 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1085 NULL, jiffies + (HZ >> 2));
1086 spin_unlock(&tbl->slot_tbl_lock);
1089 if (args->sa_privileged)
1090 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1091 RPC_PRIORITY_PRIVILEGED);
1093 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1094 spin_unlock(&tbl->slot_tbl_lock);
1097 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1099 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1101 struct nfs4_call_sync_data *data = calldata;
1102 nfs4_setup_sequence(data->seq_server->nfs_client,
1103 data->seq_args, data->seq_res, task);
1106 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1108 struct nfs4_call_sync_data *data = calldata;
1109 nfs4_sequence_done(task, data->seq_res);
1112 static const struct rpc_call_ops nfs40_call_sync_ops = {
1113 .rpc_call_prepare = nfs40_call_sync_prepare,
1114 .rpc_call_done = nfs40_call_sync_done,
1117 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1120 struct rpc_task *task;
1122 task = rpc_run_task(task_setup);
1124 return PTR_ERR(task);
1126 ret = task->tk_status;
1131 static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1132 struct nfs_server *server,
1133 struct rpc_message *msg,
1134 struct nfs4_sequence_args *args,
1135 struct nfs4_sequence_res *res,
1136 unsigned short task_flags)
1138 struct nfs_client *clp = server->nfs_client;
1139 struct nfs4_call_sync_data data = {
1140 .seq_server = server,
1144 struct rpc_task_setup task_setup = {
1147 .callback_ops = clp->cl_mvops->call_sync_ops,
1148 .callback_data = &data,
1149 .flags = task_flags,
1152 return nfs4_call_sync_custom(&task_setup);
1155 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1156 struct nfs_server *server,
1157 struct rpc_message *msg,
1158 struct nfs4_sequence_args *args,
1159 struct nfs4_sequence_res *res)
1161 unsigned short task_flags = 0;
1163 if (server->caps & NFS_CAP_MOVEABLE)
1164 task_flags = RPC_TASK_MOVEABLE;
1165 return nfs4_do_call_sync(clnt, server, msg, args, res, task_flags);
1169 int nfs4_call_sync(struct rpc_clnt *clnt,
1170 struct nfs_server *server,
1171 struct rpc_message *msg,
1172 struct nfs4_sequence_args *args,
1173 struct nfs4_sequence_res *res,
1176 nfs4_init_sequence(args, res, cache_reply, 0);
1177 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1181 nfs4_inc_nlink_locked(struct inode *inode)
1183 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1184 NFS_INO_INVALID_CTIME |
1185 NFS_INO_INVALID_NLINK);
1190 nfs4_inc_nlink(struct inode *inode)
1192 spin_lock(&inode->i_lock);
1193 nfs4_inc_nlink_locked(inode);
1194 spin_unlock(&inode->i_lock);
1198 nfs4_dec_nlink_locked(struct inode *inode)
1200 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1201 NFS_INO_INVALID_CTIME |
1202 NFS_INO_INVALID_NLINK);
1207 nfs4_update_changeattr_locked(struct inode *inode,
1208 struct nfs4_change_info *cinfo,
1209 unsigned long timestamp, unsigned long cache_validity)
1211 struct nfs_inode *nfsi = NFS_I(inode);
1212 u64 change_attr = inode_peek_iversion_raw(inode);
1214 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
1215 if (S_ISDIR(inode->i_mode))
1216 cache_validity |= NFS_INO_INVALID_DATA;
1218 switch (NFS_SERVER(inode)->change_attr_type) {
1219 case NFS4_CHANGE_TYPE_IS_UNDEFINED:
1220 if (cinfo->after == change_attr)
1224 if ((s64)(change_attr - cinfo->after) >= 0)
1228 inode_set_iversion_raw(inode, cinfo->after);
1229 if (!cinfo->atomic || cinfo->before != change_attr) {
1230 if (S_ISDIR(inode->i_mode))
1231 nfs_force_lookup_revalidate(inode);
1233 if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1235 NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
1236 NFS_INO_INVALID_SIZE | NFS_INO_INVALID_OTHER |
1237 NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
1238 NFS_INO_INVALID_MODE | NFS_INO_INVALID_XATTR;
1239 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1241 nfsi->attrtimeo_timestamp = jiffies;
1242 nfsi->read_cache_jiffies = timestamp;
1243 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1244 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1246 nfs_set_cache_invalid(inode, cache_validity);
1250 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1251 unsigned long timestamp, unsigned long cache_validity)
1253 spin_lock(&dir->i_lock);
1254 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1255 spin_unlock(&dir->i_lock);
1258 struct nfs4_open_createattrs {
1259 struct nfs4_label *label;
1260 struct iattr *sattr;
1261 const __u32 verf[2];
1264 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1265 int err, struct nfs4_exception *exception)
1269 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1271 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1272 exception->retry = 1;
1276 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1278 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1281 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1283 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1285 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1289 nfs4_map_atomic_open_share(struct nfs_server *server,
1290 fmode_t fmode, int openflags)
1294 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1296 res = NFS4_SHARE_ACCESS_READ;
1299 res = NFS4_SHARE_ACCESS_WRITE;
1301 case FMODE_READ|FMODE_WRITE:
1302 res = NFS4_SHARE_ACCESS_BOTH;
1304 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1306 /* Want no delegation if we're using O_DIRECT */
1307 if (openflags & O_DIRECT)
1308 res |= NFS4_SHARE_WANT_NO_DELEG;
1313 static enum open_claim_type4
1314 nfs4_map_atomic_open_claim(struct nfs_server *server,
1315 enum open_claim_type4 claim)
1317 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1322 case NFS4_OPEN_CLAIM_FH:
1323 return NFS4_OPEN_CLAIM_NULL;
1324 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1325 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1326 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1327 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1331 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1333 p->o_res.f_attr = &p->f_attr;
1334 p->o_res.seqid = p->o_arg.seqid;
1335 p->c_res.seqid = p->c_arg.seqid;
1336 p->o_res.server = p->o_arg.server;
1337 p->o_res.access_request = p->o_arg.access;
1338 nfs_fattr_init(&p->f_attr);
1339 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1342 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1343 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1344 const struct nfs4_open_createattrs *c,
1345 enum open_claim_type4 claim,
1348 struct dentry *parent = dget_parent(dentry);
1349 struct inode *dir = d_inode(parent);
1350 struct nfs_server *server = NFS_SERVER(dir);
1351 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1352 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1353 struct nfs4_opendata *p;
1355 p = kzalloc(sizeof(*p), gfp_mask);
1359 p->f_attr.label = nfs4_label_alloc(server, gfp_mask);
1360 if (IS_ERR(p->f_attr.label))
1363 p->a_label = nfs4_label_alloc(server, gfp_mask);
1364 if (IS_ERR(p->a_label))
1367 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1368 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1369 if (IS_ERR(p->o_arg.seqid))
1370 goto err_free_label;
1371 nfs_sb_active(dentry->d_sb);
1372 p->dentry = dget(dentry);
1375 atomic_inc(&sp->so_count);
1376 p->o_arg.open_flags = flags;
1377 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1378 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1379 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1381 if (flags & O_CREAT) {
1382 p->o_arg.umask = current_umask();
1383 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1384 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1385 p->o_arg.u.attrs = &p->attrs;
1386 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1388 memcpy(p->o_arg.u.verifier.data, c->verf,
1389 sizeof(p->o_arg.u.verifier.data));
1392 /* ask server to check for all possible rights as results
1394 switch (p->o_arg.claim) {
1397 case NFS4_OPEN_CLAIM_NULL:
1398 case NFS4_OPEN_CLAIM_FH:
1399 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1400 NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE |
1401 NFS4_ACCESS_EXECUTE |
1402 nfs_access_xattr_mask(server);
1404 p->o_arg.clientid = server->nfs_client->cl_clientid;
1405 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1406 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1407 p->o_arg.name = &dentry->d_name;
1408 p->o_arg.server = server;
1409 p->o_arg.bitmask = nfs4_bitmask(server, label);
1410 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1411 switch (p->o_arg.claim) {
1412 case NFS4_OPEN_CLAIM_NULL:
1413 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1414 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1415 p->o_arg.fh = NFS_FH(dir);
1417 case NFS4_OPEN_CLAIM_PREVIOUS:
1418 case NFS4_OPEN_CLAIM_FH:
1419 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1420 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1421 p->o_arg.fh = NFS_FH(d_inode(dentry));
1423 p->c_arg.fh = &p->o_res.fh;
1424 p->c_arg.stateid = &p->o_res.stateid;
1425 p->c_arg.seqid = p->o_arg.seqid;
1426 nfs4_init_opendata_res(p);
1427 kref_init(&p->kref);
1431 nfs4_label_free(p->a_label);
1433 nfs4_label_free(p->f_attr.label);
1441 static void nfs4_opendata_free(struct kref *kref)
1443 struct nfs4_opendata *p = container_of(kref,
1444 struct nfs4_opendata, kref);
1445 struct super_block *sb = p->dentry->d_sb;
1447 nfs4_lgopen_release(p->lgp);
1448 nfs_free_seqid(p->o_arg.seqid);
1449 nfs4_sequence_free_slot(&p->o_res.seq_res);
1450 if (p->state != NULL)
1451 nfs4_put_open_state(p->state);
1452 nfs4_put_state_owner(p->owner);
1454 nfs4_label_free(p->a_label);
1455 nfs4_label_free(p->f_attr.label);
1459 nfs_sb_deactive(sb);
1460 nfs_fattr_free_names(&p->f_attr);
1461 kfree(p->f_attr.mdsthreshold);
1465 static void nfs4_opendata_put(struct nfs4_opendata *p)
1468 kref_put(&p->kref, nfs4_opendata_free);
1471 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1474 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1475 case FMODE_READ|FMODE_WRITE:
1476 return state->n_rdwr != 0;
1478 return state->n_wronly != 0;
1480 return state->n_rdonly != 0;
1486 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1487 int open_mode, enum open_claim_type4 claim)
1491 if (open_mode & (O_EXCL|O_TRUNC))
1494 case NFS4_OPEN_CLAIM_NULL:
1495 case NFS4_OPEN_CLAIM_FH:
1500 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1502 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1503 && state->n_rdonly != 0;
1506 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1507 && state->n_wronly != 0;
1509 case FMODE_READ|FMODE_WRITE:
1510 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1511 && state->n_rdwr != 0;
1517 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1518 enum open_claim_type4 claim)
1520 if (delegation == NULL)
1522 if ((delegation->type & fmode) != fmode)
1525 case NFS4_OPEN_CLAIM_NULL:
1526 case NFS4_OPEN_CLAIM_FH:
1528 case NFS4_OPEN_CLAIM_PREVIOUS:
1529 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1535 nfs_mark_delegation_referenced(delegation);
1539 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1548 case FMODE_READ|FMODE_WRITE:
1551 nfs4_state_set_mode_locked(state, state->state | fmode);
1554 #ifdef CONFIG_NFS_V4_1
1555 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1557 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1559 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1561 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1565 #endif /* CONFIG_NFS_V4_1 */
1567 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1569 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1570 wake_up_all(&state->waitq);
1573 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1575 struct nfs_client *clp = state->owner->so_server->nfs_client;
1576 bool need_recover = false;
1578 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1579 need_recover = true;
1580 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1581 need_recover = true;
1582 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1583 need_recover = true;
1585 nfs4_state_mark_reclaim_nograce(clp, state);
1589 * Check for whether or not the caller may update the open stateid
1590 * to the value passed in by stateid.
1592 * Note: This function relies heavily on the server implementing
1593 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1595 * i.e. The stateid seqids have to be initialised to 1, and
1596 * are then incremented on every state transition.
1598 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1599 const nfs4_stateid *stateid)
1601 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1602 /* The common case - we're updating to a new sequence number */
1603 if (nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1604 if (nfs4_stateid_is_next(&state->open_stateid, stateid))
1608 /* The server returned a new stateid */
1610 /* This is the first OPEN in this generation */
1611 if (stateid->seqid == cpu_to_be32(1))
1616 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1618 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1620 if (state->n_wronly)
1621 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1622 if (state->n_rdonly)
1623 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1625 set_bit(NFS_O_RDWR_STATE, &state->flags);
1626 set_bit(NFS_OPEN_STATE, &state->flags);
1629 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1630 nfs4_stateid *stateid, fmode_t fmode)
1632 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1633 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1635 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1638 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1641 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1642 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1643 clear_bit(NFS_OPEN_STATE, &state->flags);
1645 if (stateid == NULL)
1647 /* Handle OPEN+OPEN_DOWNGRADE races */
1648 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1649 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1650 nfs_resync_open_stateid_locked(state);
1653 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1654 nfs4_stateid_copy(&state->stateid, stateid);
1655 nfs4_stateid_copy(&state->open_stateid, stateid);
1656 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1658 nfs_state_log_update_open_stateid(state);
1661 static void nfs_clear_open_stateid(struct nfs4_state *state,
1662 nfs4_stateid *arg_stateid,
1663 nfs4_stateid *stateid, fmode_t fmode)
1665 write_seqlock(&state->seqlock);
1666 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1667 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1668 nfs_clear_open_stateid_locked(state, stateid, fmode);
1669 write_sequnlock(&state->seqlock);
1670 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1671 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1674 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1675 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1676 __must_hold(&state->owner->so_lock)
1677 __must_hold(&state->seqlock)
1685 if (nfs_stateid_is_sequential(state, stateid))
1690 /* Rely on seqids for serialisation with NFSv4.0 */
1691 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1694 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1695 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1697 * Ensure we process the state changes in the same order
1698 * in which the server processed them by delaying the
1699 * update of the stateid until we are in sequence.
1701 write_sequnlock(&state->seqlock);
1702 spin_unlock(&state->owner->so_lock);
1704 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1706 if (!fatal_signal_pending(current)) {
1707 if (schedule_timeout(5*HZ) == 0)
1713 finish_wait(&state->waitq, &wait);
1715 spin_lock(&state->owner->so_lock);
1716 write_seqlock(&state->seqlock);
1719 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1720 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1721 nfs4_stateid_copy(freeme, &state->open_stateid);
1722 nfs_test_and_clear_all_open_stateid(state);
1725 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1726 nfs4_stateid_copy(&state->stateid, stateid);
1727 nfs4_stateid_copy(&state->open_stateid, stateid);
1728 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1729 nfs_state_log_update_open_stateid(state);
1732 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1733 const nfs4_stateid *open_stateid,
1735 nfs4_stateid *freeme)
1738 * Protect the call to nfs4_state_set_mode_locked and
1739 * serialise the stateid update
1741 write_seqlock(&state->seqlock);
1742 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1745 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1748 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1750 case FMODE_READ|FMODE_WRITE:
1751 set_bit(NFS_O_RDWR_STATE, &state->flags);
1753 set_bit(NFS_OPEN_STATE, &state->flags);
1754 write_sequnlock(&state->seqlock);
1757 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1759 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1760 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1761 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1762 clear_bit(NFS_OPEN_STATE, &state->flags);
1765 static void nfs_state_set_delegation(struct nfs4_state *state,
1766 const nfs4_stateid *deleg_stateid,
1770 * Protect the call to nfs4_state_set_mode_locked and
1771 * serialise the stateid update
1773 write_seqlock(&state->seqlock);
1774 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1775 set_bit(NFS_DELEGATED_STATE, &state->flags);
1776 write_sequnlock(&state->seqlock);
1779 static void nfs_state_clear_delegation(struct nfs4_state *state)
1781 write_seqlock(&state->seqlock);
1782 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1783 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1784 write_sequnlock(&state->seqlock);
1787 int update_open_stateid(struct nfs4_state *state,
1788 const nfs4_stateid *open_stateid,
1789 const nfs4_stateid *delegation,
1792 struct nfs_server *server = NFS_SERVER(state->inode);
1793 struct nfs_client *clp = server->nfs_client;
1794 struct nfs_inode *nfsi = NFS_I(state->inode);
1795 struct nfs_delegation *deleg_cur;
1796 nfs4_stateid freeme = { };
1799 fmode &= (FMODE_READ|FMODE_WRITE);
1802 spin_lock(&state->owner->so_lock);
1803 if (open_stateid != NULL) {
1804 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1808 deleg_cur = nfs4_get_valid_delegation(state->inode);
1809 if (deleg_cur == NULL)
1812 spin_lock(&deleg_cur->lock);
1813 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1814 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1815 (deleg_cur->type & fmode) != fmode)
1816 goto no_delegation_unlock;
1818 if (delegation == NULL)
1819 delegation = &deleg_cur->stateid;
1820 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1821 goto no_delegation_unlock;
1823 nfs_mark_delegation_referenced(deleg_cur);
1824 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1826 no_delegation_unlock:
1827 spin_unlock(&deleg_cur->lock);
1830 update_open_stateflags(state, fmode);
1831 spin_unlock(&state->owner->so_lock);
1834 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1835 nfs4_schedule_state_manager(clp);
1836 if (freeme.type != 0)
1837 nfs4_test_and_free_stateid(server, &freeme,
1838 state->owner->so_cred);
1843 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1844 const nfs4_stateid *stateid)
1846 struct nfs4_state *state = lsp->ls_state;
1849 spin_lock(&state->state_lock);
1850 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1852 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1854 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1857 spin_unlock(&state->state_lock);
1861 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1863 struct nfs_delegation *delegation;
1865 fmode &= FMODE_READ|FMODE_WRITE;
1867 delegation = nfs4_get_valid_delegation(inode);
1868 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1873 nfs4_inode_return_delegation(inode);
1876 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1878 struct nfs4_state *state = opendata->state;
1879 struct nfs_delegation *delegation;
1880 int open_mode = opendata->o_arg.open_flags;
1881 fmode_t fmode = opendata->o_arg.fmode;
1882 enum open_claim_type4 claim = opendata->o_arg.claim;
1883 nfs4_stateid stateid;
1887 spin_lock(&state->owner->so_lock);
1888 if (can_open_cached(state, fmode, open_mode, claim)) {
1889 update_open_stateflags(state, fmode);
1890 spin_unlock(&state->owner->so_lock);
1891 goto out_return_state;
1893 spin_unlock(&state->owner->so_lock);
1895 delegation = nfs4_get_valid_delegation(state->inode);
1896 if (!can_open_delegated(delegation, fmode, claim)) {
1900 /* Save the delegation */
1901 nfs4_stateid_copy(&stateid, &delegation->stateid);
1903 nfs_release_seqid(opendata->o_arg.seqid);
1904 if (!opendata->is_recover) {
1905 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1911 /* Try to update the stateid using the delegation */
1912 if (update_open_stateid(state, NULL, &stateid, fmode))
1913 goto out_return_state;
1916 return ERR_PTR(ret);
1918 refcount_inc(&state->count);
1923 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1925 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1926 struct nfs_delegation *delegation;
1927 int delegation_flags = 0;
1930 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1932 delegation_flags = delegation->flags;
1934 switch (data->o_arg.claim) {
1937 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1938 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1939 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1940 "returning a delegation for "
1941 "OPEN(CLAIM_DELEGATE_CUR)\n",
1945 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1946 nfs_inode_set_delegation(state->inode,
1947 data->owner->so_cred,
1948 data->o_res.delegation_type,
1949 &data->o_res.delegation,
1950 data->o_res.pagemod_limit);
1952 nfs_inode_reclaim_delegation(state->inode,
1953 data->owner->so_cred,
1954 data->o_res.delegation_type,
1955 &data->o_res.delegation,
1956 data->o_res.pagemod_limit);
1958 if (data->o_res.do_recall)
1959 nfs_async_inode_return_delegation(state->inode,
1960 &data->o_res.delegation);
1964 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1965 * and update the nfs4_state.
1967 static struct nfs4_state *
1968 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1970 struct inode *inode = data->state->inode;
1971 struct nfs4_state *state = data->state;
1974 if (!data->rpc_done) {
1975 if (data->rpc_status)
1976 return ERR_PTR(data->rpc_status);
1977 /* cached opens have already been processed */
1981 ret = nfs_refresh_inode(inode, &data->f_attr);
1983 return ERR_PTR(ret);
1985 if (data->o_res.delegation_type != 0)
1986 nfs4_opendata_check_deleg(data, state);
1988 if (!update_open_stateid(state, &data->o_res.stateid,
1989 NULL, data->o_arg.fmode))
1990 return ERR_PTR(-EAGAIN);
1991 refcount_inc(&state->count);
1996 static struct inode *
1997 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1999 struct inode *inode;
2001 switch (data->o_arg.claim) {
2002 case NFS4_OPEN_CLAIM_NULL:
2003 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2004 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2005 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2006 return ERR_PTR(-EAGAIN);
2007 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2011 inode = d_inode(data->dentry);
2013 nfs_refresh_inode(inode, &data->f_attr);
2018 static struct nfs4_state *
2019 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2021 struct nfs4_state *state;
2022 struct inode *inode;
2024 inode = nfs4_opendata_get_inode(data);
2026 return ERR_CAST(inode);
2027 if (data->state != NULL && data->state->inode == inode) {
2028 state = data->state;
2029 refcount_inc(&state->count);
2031 state = nfs4_get_open_state(inode, data->owner);
2034 state = ERR_PTR(-ENOMEM);
2038 static struct nfs4_state *
2039 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2041 struct nfs4_state *state;
2043 if (!data->rpc_done) {
2044 state = nfs4_try_open_cached(data);
2045 trace_nfs4_cached_open(data->state);
2049 state = nfs4_opendata_find_nfs4_state(data);
2053 if (data->o_res.delegation_type != 0)
2054 nfs4_opendata_check_deleg(data, state);
2055 if (!update_open_stateid(state, &data->o_res.stateid,
2056 NULL, data->o_arg.fmode)) {
2057 nfs4_put_open_state(state);
2058 state = ERR_PTR(-EAGAIN);
2061 nfs_release_seqid(data->o_arg.seqid);
2065 static struct nfs4_state *
2066 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2068 struct nfs4_state *ret;
2070 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2071 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2073 ret = _nfs4_opendata_to_nfs4_state(data);
2074 nfs4_sequence_free_slot(&data->o_res.seq_res);
2078 static struct nfs_open_context *
2079 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2081 struct nfs_inode *nfsi = NFS_I(state->inode);
2082 struct nfs_open_context *ctx;
2085 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2086 if (ctx->state != state)
2088 if ((ctx->mode & mode) != mode)
2090 if (!get_nfs_open_context(ctx))
2096 return ERR_PTR(-ENOENT);
2099 static struct nfs_open_context *
2100 nfs4_state_find_open_context(struct nfs4_state *state)
2102 struct nfs_open_context *ctx;
2104 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2107 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2110 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2113 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2114 struct nfs4_state *state, enum open_claim_type4 claim)
2116 struct nfs4_opendata *opendata;
2118 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2119 NULL, claim, GFP_NOFS);
2120 if (opendata == NULL)
2121 return ERR_PTR(-ENOMEM);
2122 opendata->state = state;
2123 refcount_inc(&state->count);
2127 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2130 struct nfs4_state *newstate;
2133 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2135 opendata->o_arg.open_flags = 0;
2136 opendata->o_arg.fmode = fmode;
2137 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2138 NFS_SB(opendata->dentry->d_sb),
2140 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2141 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2142 nfs4_init_opendata_res(opendata);
2143 ret = _nfs4_recover_proc_open(opendata);
2146 newstate = nfs4_opendata_to_nfs4_state(opendata);
2147 if (IS_ERR(newstate))
2148 return PTR_ERR(newstate);
2149 if (newstate != opendata->state)
2151 nfs4_close_state(newstate, fmode);
2155 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2159 /* memory barrier prior to reading state->n_* */
2161 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2164 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2167 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2171 * We may have performed cached opens for all three recoveries.
2172 * Check if we need to update the current stateid.
2174 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2175 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2176 write_seqlock(&state->seqlock);
2177 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2178 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2179 write_sequnlock(&state->seqlock);
2186 * reclaim state on the server after a reboot.
2188 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2190 struct nfs_delegation *delegation;
2191 struct nfs4_opendata *opendata;
2192 fmode_t delegation_type = 0;
2195 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2196 NFS4_OPEN_CLAIM_PREVIOUS);
2197 if (IS_ERR(opendata))
2198 return PTR_ERR(opendata);
2200 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2201 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2202 delegation_type = delegation->type;
2204 opendata->o_arg.u.delegation_type = delegation_type;
2205 status = nfs4_open_recover(opendata, state);
2206 nfs4_opendata_put(opendata);
2210 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2212 struct nfs_server *server = NFS_SERVER(state->inode);
2213 struct nfs4_exception exception = { };
2216 err = _nfs4_do_open_reclaim(ctx, state);
2217 trace_nfs4_open_reclaim(ctx, 0, err);
2218 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2220 if (err != -NFS4ERR_DELAY)
2222 nfs4_handle_exception(server, err, &exception);
2223 } while (exception.retry);
2227 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2229 struct nfs_open_context *ctx;
2232 ctx = nfs4_state_find_open_context(state);
2235 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2236 nfs_state_clear_open_state_flags(state);
2237 ret = nfs4_do_open_reclaim(ctx, state);
2238 put_nfs_open_context(ctx);
2242 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
2246 printk(KERN_ERR "NFS: %s: unhandled error "
2247 "%d.\n", __func__, err);
2255 case -NFS4ERR_BADSESSION:
2256 case -NFS4ERR_BADSLOT:
2257 case -NFS4ERR_BAD_HIGH_SLOT:
2258 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2259 case -NFS4ERR_DEADSESSION:
2261 case -NFS4ERR_STALE_CLIENTID:
2262 case -NFS4ERR_STALE_STATEID:
2263 /* Don't recall a delegation if it was lost */
2264 nfs4_schedule_lease_recovery(server->nfs_client);
2266 case -NFS4ERR_MOVED:
2267 nfs4_schedule_migration_recovery(server);
2269 case -NFS4ERR_LEASE_MOVED:
2270 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2272 case -NFS4ERR_DELEG_REVOKED:
2273 case -NFS4ERR_ADMIN_REVOKED:
2274 case -NFS4ERR_EXPIRED:
2275 case -NFS4ERR_BAD_STATEID:
2276 case -NFS4ERR_OPENMODE:
2277 nfs_inode_find_state_and_recover(state->inode,
2279 nfs4_schedule_stateid_recovery(server, state);
2281 case -NFS4ERR_DELAY:
2282 case -NFS4ERR_GRACE:
2286 case -NFS4ERR_DENIED:
2288 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2290 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2297 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2298 struct nfs4_state *state, const nfs4_stateid *stateid)
2300 struct nfs_server *server = NFS_SERVER(state->inode);
2301 struct nfs4_opendata *opendata;
2304 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2305 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2306 if (IS_ERR(opendata))
2307 return PTR_ERR(opendata);
2308 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2309 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2310 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2314 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2315 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2319 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2320 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2324 nfs_state_clear_delegation(state);
2326 nfs4_opendata_put(opendata);
2327 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2330 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2332 struct nfs4_opendata *data = calldata;
2334 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2335 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2338 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2340 struct nfs4_opendata *data = calldata;
2342 nfs40_sequence_done(task, &data->c_res.seq_res);
2344 data->rpc_status = task->tk_status;
2345 if (data->rpc_status == 0) {
2346 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2347 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2348 renew_lease(data->o_res.server, data->timestamp);
2349 data->rpc_done = true;
2353 static void nfs4_open_confirm_release(void *calldata)
2355 struct nfs4_opendata *data = calldata;
2356 struct nfs4_state *state = NULL;
2358 /* If this request hasn't been cancelled, do nothing */
2359 if (!data->cancelled)
2361 /* In case of error, no cleanup! */
2362 if (!data->rpc_done)
2364 state = nfs4_opendata_to_nfs4_state(data);
2366 nfs4_close_state(state, data->o_arg.fmode);
2368 nfs4_opendata_put(data);
2371 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2372 .rpc_call_prepare = nfs4_open_confirm_prepare,
2373 .rpc_call_done = nfs4_open_confirm_done,
2374 .rpc_release = nfs4_open_confirm_release,
2378 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2380 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2382 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2383 struct rpc_task *task;
2384 struct rpc_message msg = {
2385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2386 .rpc_argp = &data->c_arg,
2387 .rpc_resp = &data->c_res,
2388 .rpc_cred = data->owner->so_cred,
2390 struct rpc_task_setup task_setup_data = {
2391 .rpc_client = server->client,
2392 .rpc_message = &msg,
2393 .callback_ops = &nfs4_open_confirm_ops,
2394 .callback_data = data,
2395 .workqueue = nfsiod_workqueue,
2396 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2400 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2402 kref_get(&data->kref);
2403 data->rpc_done = false;
2404 data->rpc_status = 0;
2405 data->timestamp = jiffies;
2406 task = rpc_run_task(&task_setup_data);
2408 return PTR_ERR(task);
2409 status = rpc_wait_for_completion_task(task);
2411 data->cancelled = true;
2414 status = data->rpc_status;
2419 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2421 struct nfs4_opendata *data = calldata;
2422 struct nfs4_state_owner *sp = data->owner;
2423 struct nfs_client *clp = sp->so_server->nfs_client;
2424 enum open_claim_type4 claim = data->o_arg.claim;
2426 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2429 * Check if we still need to send an OPEN call, or if we can use
2430 * a delegation instead.
2432 if (data->state != NULL) {
2433 struct nfs_delegation *delegation;
2435 if (can_open_cached(data->state, data->o_arg.fmode,
2436 data->o_arg.open_flags, claim))
2439 delegation = nfs4_get_valid_delegation(data->state->inode);
2440 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2441 goto unlock_no_action;
2444 /* Update client id. */
2445 data->o_arg.clientid = clp->cl_clientid;
2449 case NFS4_OPEN_CLAIM_PREVIOUS:
2450 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2451 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2452 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2454 case NFS4_OPEN_CLAIM_FH:
2455 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2457 data->timestamp = jiffies;
2458 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2459 &data->o_arg.seq_args,
2460 &data->o_res.seq_res,
2462 nfs_release_seqid(data->o_arg.seqid);
2464 /* Set the create mode (note dependency on the session type) */
2465 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2466 if (data->o_arg.open_flags & O_EXCL) {
2467 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2468 if (clp->cl_mvops->minor_version == 0) {
2469 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2470 /* don't put an ACCESS op in OPEN compound if O_EXCL,
2471 * because ACCESS will return permission denied for
2472 * all bits until close */
2473 data->o_res.access_request = data->o_arg.access = 0;
2474 } else if (nfs4_has_persistent_session(clp))
2475 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2479 trace_nfs4_cached_open(data->state);
2482 task->tk_action = NULL;
2484 nfs4_sequence_done(task, &data->o_res.seq_res);
2487 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2489 struct nfs4_opendata *data = calldata;
2491 data->rpc_status = task->tk_status;
2493 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2496 if (task->tk_status == 0) {
2497 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2498 switch (data->o_res.f_attr->mode & S_IFMT) {
2502 data->rpc_status = -ELOOP;
2505 data->rpc_status = -EISDIR;
2508 data->rpc_status = -ENOTDIR;
2511 renew_lease(data->o_res.server, data->timestamp);
2512 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2513 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2515 data->rpc_done = true;
2518 static void nfs4_open_release(void *calldata)
2520 struct nfs4_opendata *data = calldata;
2521 struct nfs4_state *state = NULL;
2523 /* If this request hasn't been cancelled, do nothing */
2524 if (!data->cancelled)
2526 /* In case of error, no cleanup! */
2527 if (data->rpc_status != 0 || !data->rpc_done)
2529 /* In case we need an open_confirm, no cleanup! */
2530 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2532 state = nfs4_opendata_to_nfs4_state(data);
2534 nfs4_close_state(state, data->o_arg.fmode);
2536 nfs4_opendata_put(data);
2539 static const struct rpc_call_ops nfs4_open_ops = {
2540 .rpc_call_prepare = nfs4_open_prepare,
2541 .rpc_call_done = nfs4_open_done,
2542 .rpc_release = nfs4_open_release,
2545 static int nfs4_run_open_task(struct nfs4_opendata *data,
2546 struct nfs_open_context *ctx)
2548 struct inode *dir = d_inode(data->dir);
2549 struct nfs_server *server = NFS_SERVER(dir);
2550 struct nfs_openargs *o_arg = &data->o_arg;
2551 struct nfs_openres *o_res = &data->o_res;
2552 struct rpc_task *task;
2553 struct rpc_message msg = {
2554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2557 .rpc_cred = data->owner->so_cred,
2559 struct rpc_task_setup task_setup_data = {
2560 .rpc_client = server->client,
2561 .rpc_message = &msg,
2562 .callback_ops = &nfs4_open_ops,
2563 .callback_data = data,
2564 .workqueue = nfsiod_workqueue,
2565 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2569 if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
2570 task_setup_data.flags |= RPC_TASK_MOVEABLE;
2572 kref_get(&data->kref);
2573 data->rpc_done = false;
2574 data->rpc_status = 0;
2575 data->cancelled = false;
2576 data->is_recover = false;
2578 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2579 data->is_recover = true;
2580 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2582 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2583 pnfs_lgopen_prepare(data, ctx);
2585 task = rpc_run_task(&task_setup_data);
2587 return PTR_ERR(task);
2588 status = rpc_wait_for_completion_task(task);
2590 data->cancelled = true;
2593 status = data->rpc_status;
2599 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2601 struct inode *dir = d_inode(data->dir);
2602 struct nfs_openres *o_res = &data->o_res;
2605 status = nfs4_run_open_task(data, NULL);
2606 if (status != 0 || !data->rpc_done)
2609 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2611 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2612 status = _nfs4_proc_open_confirm(data);
2618 * Additional permission checks in order to distinguish between an
2619 * open for read, and an open for execute. This works around the
2620 * fact that NFSv4 OPEN treats read and execute permissions as being
2622 * Note that in the non-execute case, we want to turn off permission
2623 * checking if we just created a new file (POSIX open() semantics).
2625 static int nfs4_opendata_access(const struct cred *cred,
2626 struct nfs4_opendata *opendata,
2627 struct nfs4_state *state, fmode_t fmode,
2630 struct nfs_access_entry cache;
2633 /* access call failed or for some reason the server doesn't
2634 * support any access modes -- defer access call until later */
2635 if (opendata->o_res.access_supported == 0)
2640 * Use openflags to check for exec, because fmode won't
2641 * always have FMODE_EXEC set when file open for exec.
2643 if (openflags & __FMODE_EXEC) {
2644 /* ONLY check for exec rights */
2645 if (S_ISDIR(state->inode->i_mode))
2646 mask = NFS4_ACCESS_LOOKUP;
2648 mask = NFS4_ACCESS_EXECUTE;
2649 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2650 mask = NFS4_ACCESS_READ;
2652 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2653 nfs_access_add_cache(state->inode, &cache, cred);
2655 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2656 if ((mask & ~cache.mask & flags) == 0)
2663 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2665 static int _nfs4_proc_open(struct nfs4_opendata *data,
2666 struct nfs_open_context *ctx)
2668 struct inode *dir = d_inode(data->dir);
2669 struct nfs_server *server = NFS_SERVER(dir);
2670 struct nfs_openargs *o_arg = &data->o_arg;
2671 struct nfs_openres *o_res = &data->o_res;
2674 status = nfs4_run_open_task(data, ctx);
2675 if (!data->rpc_done)
2678 if (status == -NFS4ERR_BADNAME &&
2679 !(o_arg->open_flags & O_CREAT))
2684 nfs_fattr_map_and_free_names(server, &data->f_attr);
2686 if (o_arg->open_flags & O_CREAT) {
2687 if (o_arg->open_flags & O_EXCL)
2688 data->file_created = true;
2689 else if (o_res->cinfo.before != o_res->cinfo.after)
2690 data->file_created = true;
2691 if (data->file_created ||
2692 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2693 nfs4_update_changeattr(dir, &o_res->cinfo,
2694 o_res->f_attr->time_start,
2695 NFS_INO_INVALID_DATA);
2697 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2698 server->caps &= ~NFS_CAP_POSIX_LOCK;
2699 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2700 status = _nfs4_proc_open_confirm(data);
2704 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2705 nfs4_sequence_free_slot(&o_res->seq_res);
2706 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, NULL);
2713 * reclaim state on the server after a network partition.
2714 * Assumes caller holds the appropriate lock
2716 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2718 struct nfs4_opendata *opendata;
2721 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2722 NFS4_OPEN_CLAIM_FH);
2723 if (IS_ERR(opendata))
2724 return PTR_ERR(opendata);
2725 ret = nfs4_open_recover(opendata, state);
2727 d_drop(ctx->dentry);
2728 nfs4_opendata_put(opendata);
2732 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2734 struct nfs_server *server = NFS_SERVER(state->inode);
2735 struct nfs4_exception exception = { };
2739 err = _nfs4_open_expired(ctx, state);
2740 trace_nfs4_open_expired(ctx, 0, err);
2741 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2746 case -NFS4ERR_GRACE:
2747 case -NFS4ERR_DELAY:
2748 nfs4_handle_exception(server, err, &exception);
2751 } while (exception.retry);
2756 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2758 struct nfs_open_context *ctx;
2761 ctx = nfs4_state_find_open_context(state);
2764 ret = nfs4_do_open_expired(ctx, state);
2765 put_nfs_open_context(ctx);
2769 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2770 const nfs4_stateid *stateid)
2772 nfs_remove_bad_delegation(state->inode, stateid);
2773 nfs_state_clear_delegation(state);
2776 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2778 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2779 nfs_finish_clear_delegation_stateid(state, NULL);
2782 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2784 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2785 nfs40_clear_delegation_stateid(state);
2786 nfs_state_clear_open_state_flags(state);
2787 return nfs4_open_expired(sp, state);
2790 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2791 nfs4_stateid *stateid,
2792 const struct cred *cred)
2794 return -NFS4ERR_BAD_STATEID;
2797 #if defined(CONFIG_NFS_V4_1)
2798 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2799 nfs4_stateid *stateid,
2800 const struct cred *cred)
2804 switch (stateid->type) {
2807 case NFS4_INVALID_STATEID_TYPE:
2808 case NFS4_SPECIAL_STATEID_TYPE:
2809 return -NFS4ERR_BAD_STATEID;
2810 case NFS4_REVOKED_STATEID_TYPE:
2814 status = nfs41_test_stateid(server, stateid, cred);
2816 case -NFS4ERR_EXPIRED:
2817 case -NFS4ERR_ADMIN_REVOKED:
2818 case -NFS4ERR_DELEG_REVOKED:
2824 /* Ack the revoked state to the server */
2825 nfs41_free_stateid(server, stateid, cred, true);
2826 return -NFS4ERR_EXPIRED;
2829 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2831 struct nfs_server *server = NFS_SERVER(state->inode);
2832 nfs4_stateid stateid;
2833 struct nfs_delegation *delegation;
2834 const struct cred *cred = NULL;
2835 int status, ret = NFS_OK;
2837 /* Get the delegation credential for use by test/free_stateid */
2839 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2840 if (delegation == NULL) {
2842 nfs_state_clear_delegation(state);
2846 spin_lock(&delegation->lock);
2847 nfs4_stateid_copy(&stateid, &delegation->stateid);
2849 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2850 &delegation->flags)) {
2851 spin_unlock(&delegation->lock);
2856 if (delegation->cred)
2857 cred = get_cred(delegation->cred);
2858 spin_unlock(&delegation->lock);
2860 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2861 trace_nfs4_test_delegation_stateid(state, NULL, status);
2862 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2863 nfs_finish_clear_delegation_stateid(state, &stateid);
2871 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2875 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2876 nfs4_copy_delegation_stateid(state->inode, state->state,
2878 nfs4_stateid_match_other(&state->stateid, &tmp))
2879 nfs_state_set_delegation(state, &tmp, state->state);
2881 nfs_state_clear_delegation(state);
2885 * nfs41_check_expired_locks - possibly free a lock stateid
2887 * @state: NFSv4 state for an inode
2889 * Returns NFS_OK if recovery for this stateid is now finished.
2890 * Otherwise a negative NFS4ERR value is returned.
2892 static int nfs41_check_expired_locks(struct nfs4_state *state)
2894 int status, ret = NFS_OK;
2895 struct nfs4_lock_state *lsp, *prev = NULL;
2896 struct nfs_server *server = NFS_SERVER(state->inode);
2898 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2901 spin_lock(&state->state_lock);
2902 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2903 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2904 const struct cred *cred = lsp->ls_state->owner->so_cred;
2906 refcount_inc(&lsp->ls_count);
2907 spin_unlock(&state->state_lock);
2909 nfs4_put_lock_state(prev);
2912 status = nfs41_test_and_free_expired_stateid(server,
2915 trace_nfs4_test_lock_stateid(state, lsp, status);
2916 if (status == -NFS4ERR_EXPIRED ||
2917 status == -NFS4ERR_BAD_STATEID) {
2918 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2919 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2920 if (!recover_lost_locks)
2921 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2922 } else if (status != NFS_OK) {
2924 nfs4_put_lock_state(prev);
2927 spin_lock(&state->state_lock);
2930 spin_unlock(&state->state_lock);
2931 nfs4_put_lock_state(prev);
2937 * nfs41_check_open_stateid - possibly free an open stateid
2939 * @state: NFSv4 state for an inode
2941 * Returns NFS_OK if recovery for this stateid is now finished.
2942 * Otherwise a negative NFS4ERR value is returned.
2944 static int nfs41_check_open_stateid(struct nfs4_state *state)
2946 struct nfs_server *server = NFS_SERVER(state->inode);
2947 nfs4_stateid *stateid = &state->open_stateid;
2948 const struct cred *cred = state->owner->so_cred;
2951 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2952 return -NFS4ERR_BAD_STATEID;
2953 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2954 trace_nfs4_test_open_stateid(state, NULL, status);
2955 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2956 nfs_state_clear_open_state_flags(state);
2957 stateid->type = NFS4_INVALID_STATEID_TYPE;
2960 if (nfs_open_stateid_recover_openmode(state))
2961 return -NFS4ERR_OPENMODE;
2965 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2969 status = nfs41_check_delegation_stateid(state);
2970 if (status != NFS_OK)
2972 nfs41_delegation_recover_stateid(state);
2974 status = nfs41_check_expired_locks(state);
2975 if (status != NFS_OK)
2977 status = nfs41_check_open_stateid(state);
2978 if (status != NFS_OK)
2979 status = nfs4_open_expired(sp, state);
2985 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2986 * fields corresponding to attributes that were used to store the verifier.
2987 * Make sure we clobber those fields in the later setattr call
2989 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2990 struct iattr *sattr, struct nfs4_label **label)
2992 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2997 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2998 attrset[i] = opendata->o_res.attrset[i];
2999 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3000 attrset[i] &= ~bitmask[i];
3003 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3004 sattr->ia_valid : 0;
3006 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3007 if (sattr->ia_valid & ATTR_ATIME_SET)
3008 ret |= ATTR_ATIME_SET;
3013 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3014 if (sattr->ia_valid & ATTR_MTIME_SET)
3015 ret |= ATTR_MTIME_SET;
3020 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3025 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3026 int flags, struct nfs_open_context *ctx)
3028 struct nfs4_state_owner *sp = opendata->owner;
3029 struct nfs_server *server = sp->so_server;
3030 struct dentry *dentry;
3031 struct nfs4_state *state;
3032 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3033 struct inode *dir = d_inode(opendata->dir);
3034 unsigned long dir_verifier;
3038 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3039 dir_verifier = nfs_save_change_attribute(dir);
3041 ret = _nfs4_proc_open(opendata, ctx);
3045 state = _nfs4_opendata_to_nfs4_state(opendata);
3046 ret = PTR_ERR(state);
3050 if (server->caps & NFS_CAP_POSIX_LOCK)
3051 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3052 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3053 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3054 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_PRESERVE_UNLINKED)
3055 set_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(state->inode)->flags);
3057 dentry = opendata->dentry;
3058 if (d_really_is_negative(dentry)) {
3059 struct dentry *alias;
3061 alias = d_exact_alias(dentry, state->inode);
3063 alias = d_splice_alias(igrab(state->inode), dentry);
3064 /* d_splice_alias() can't fail here - it's a non-directory */
3067 ctx->dentry = dentry = alias;
3071 switch(opendata->o_arg.claim) {
3074 case NFS4_OPEN_CLAIM_NULL:
3075 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3076 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3077 if (!opendata->rpc_done)
3079 if (opendata->o_res.delegation_type != 0)
3080 dir_verifier = nfs_save_change_attribute(dir);
3081 nfs_set_verifier(dentry, dir_verifier);
3084 /* Parse layoutget results before we check for access */
3085 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3087 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3092 if (d_inode(dentry) == state->inode) {
3093 nfs_inode_attach_open_context(ctx);
3094 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3095 nfs4_schedule_stateid_recovery(server, state);
3099 if (!opendata->cancelled) {
3100 if (opendata->lgp) {
3101 nfs4_lgopen_release(opendata->lgp);
3102 opendata->lgp = NULL;
3104 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3110 * Returns a referenced nfs4_state
3112 static int _nfs4_do_open(struct inode *dir,
3113 struct nfs_open_context *ctx,
3115 const struct nfs4_open_createattrs *c,
3118 struct nfs4_state_owner *sp;
3119 struct nfs4_state *state = NULL;
3120 struct nfs_server *server = NFS_SERVER(dir);
3121 struct nfs4_opendata *opendata;
3122 struct dentry *dentry = ctx->dentry;
3123 const struct cred *cred = ctx->cred;
3124 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3125 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3126 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3127 struct iattr *sattr = c->sattr;
3128 struct nfs4_label *label = c->label;
3131 /* Protect against reboot recovery conflicts */
3133 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3135 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3138 status = nfs4_client_recover_expired_lease(server->nfs_client);
3140 goto err_put_state_owner;
3141 if (d_really_is_positive(dentry))
3142 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3144 if (d_really_is_positive(dentry))
3145 claim = NFS4_OPEN_CLAIM_FH;
3146 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3147 c, claim, GFP_KERNEL);
3148 if (opendata == NULL)
3149 goto err_put_state_owner;
3151 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3152 if (!opendata->f_attr.mdsthreshold) {
3153 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3154 if (!opendata->f_attr.mdsthreshold)
3155 goto err_opendata_put;
3157 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3159 if (d_really_is_positive(dentry))
3160 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3162 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3164 goto err_opendata_put;
3167 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3168 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3169 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3171 * send create attributes which was not set by open
3172 * with an extra setattr.
3174 if (attrs || label) {
3175 unsigned ia_old = sattr->ia_valid;
3177 sattr->ia_valid = attrs;
3178 nfs_fattr_init(opendata->o_res.f_attr);
3179 status = nfs4_do_setattr(state->inode, cred,
3180 opendata->o_res.f_attr, sattr,
3183 nfs_setattr_update_inode(state->inode, sattr,
3184 opendata->o_res.f_attr);
3185 nfs_setsecurity(state->inode, opendata->o_res.f_attr);
3187 sattr->ia_valid = ia_old;
3190 if (opened && opendata->file_created)
3193 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3194 *ctx_th = opendata->f_attr.mdsthreshold;
3195 opendata->f_attr.mdsthreshold = NULL;
3198 nfs4_opendata_put(opendata);
3199 nfs4_put_state_owner(sp);
3202 nfs4_opendata_put(opendata);
3203 err_put_state_owner:
3204 nfs4_put_state_owner(sp);
3210 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3211 struct nfs_open_context *ctx,
3213 struct iattr *sattr,
3214 struct nfs4_label *label,
3217 struct nfs_server *server = NFS_SERVER(dir);
3218 struct nfs4_exception exception = {
3219 .interruptible = true,
3221 struct nfs4_state *res;
3222 struct nfs4_open_createattrs c = {
3226 [0] = (__u32)jiffies,
3227 [1] = (__u32)current->pid,
3233 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3235 trace_nfs4_open_file(ctx, flags, status);
3238 /* NOTE: BAD_SEQID means the server and client disagree about the
3239 * book-keeping w.r.t. state-changing operations
3240 * (OPEN/CLOSE/LOCK/LOCKU...)
3241 * It is actually a sign of a bug on the client or on the server.
3243 * If we receive a BAD_SEQID error in the particular case of
3244 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3245 * have unhashed the old state_owner for us, and that we can
3246 * therefore safely retry using a new one. We should still warn
3247 * the user though...
3249 if (status == -NFS4ERR_BAD_SEQID) {
3250 pr_warn_ratelimited("NFS: v4 server %s "
3251 " returned a bad sequence-id error!\n",
3252 NFS_SERVER(dir)->nfs_client->cl_hostname);
3253 exception.retry = 1;
3257 * BAD_STATEID on OPEN means that the server cancelled our
3258 * state before it received the OPEN_CONFIRM.
3259 * Recover by retrying the request as per the discussion
3260 * on Page 181 of RFC3530.
3262 if (status == -NFS4ERR_BAD_STATEID) {
3263 exception.retry = 1;
3266 if (status == -NFS4ERR_EXPIRED) {
3267 nfs4_schedule_lease_recovery(server->nfs_client);
3268 exception.retry = 1;
3271 if (status == -EAGAIN) {
3272 /* We must have found a delegation */
3273 exception.retry = 1;
3276 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3278 res = ERR_PTR(nfs4_handle_exception(server,
3279 status, &exception));
3280 } while (exception.retry);
3284 static int _nfs4_do_setattr(struct inode *inode,
3285 struct nfs_setattrargs *arg,
3286 struct nfs_setattrres *res,
3287 const struct cred *cred,
3288 struct nfs_open_context *ctx)
3290 struct nfs_server *server = NFS_SERVER(inode);
3291 struct rpc_message msg = {
3292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3297 const struct cred *delegation_cred = NULL;
3298 unsigned long timestamp = jiffies;
3302 nfs_fattr_init(res->fattr);
3304 /* Servers should only apply open mode checks for file size changes */
3305 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3307 nfs4_inode_make_writeable(inode);
3311 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3312 /* Use that stateid */
3313 } else if (ctx != NULL && ctx->state) {
3314 struct nfs_lock_context *l_ctx;
3315 if (!nfs4_valid_open_stateid(ctx->state))
3317 l_ctx = nfs_get_lock_context(ctx);
3319 return PTR_ERR(l_ctx);
3320 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3321 &arg->stateid, &delegation_cred);
3322 nfs_put_lock_context(l_ctx);
3325 else if (status == -EAGAIN)
3329 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3331 if (delegation_cred)
3332 msg.rpc_cred = delegation_cred;
3334 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3336 put_cred(delegation_cred);
3337 if (status == 0 && ctx != NULL)
3338 renew_lease(server, timestamp);
3339 trace_nfs4_setattr(inode, &arg->stateid, status);
3343 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3344 struct nfs_fattr *fattr, struct iattr *sattr,
3345 struct nfs_open_context *ctx, struct nfs4_label *ilabel)
3347 struct nfs_server *server = NFS_SERVER(inode);
3348 __u32 bitmask[NFS4_BITMASK_SZ];
3349 struct nfs4_state *state = ctx ? ctx->state : NULL;
3350 struct nfs_setattrargs arg = {
3351 .fh = NFS_FH(inode),
3357 struct nfs_setattrres res = {
3361 struct nfs4_exception exception = {
3364 .stateid = &arg.stateid,
3366 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
3369 if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3370 adjust_flags |= NFS_INO_INVALID_MODE;
3371 if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
3372 adjust_flags |= NFS_INO_INVALID_OTHER;
3375 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label),
3376 inode, adjust_flags);
3378 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3380 case -NFS4ERR_OPENMODE:
3381 if (!(sattr->ia_valid & ATTR_SIZE)) {
3382 pr_warn_once("NFSv4: server %s is incorrectly "
3383 "applying open mode checks to "
3384 "a SETATTR that is not "
3385 "changing file size.\n",
3386 server->nfs_client->cl_hostname);
3388 if (state && !(state->state & FMODE_WRITE)) {
3390 if (sattr->ia_valid & ATTR_OPEN)
3395 err = nfs4_handle_exception(server, err, &exception);
3396 } while (exception.retry);
3402 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3404 if (inode == NULL || !nfs_have_layout(inode))
3407 return pnfs_wait_on_layoutreturn(inode, task);
3411 * Update the seqid of an open stateid
3413 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3414 struct nfs4_state *state)
3421 if (!nfs4_valid_open_stateid(state))
3423 seq = read_seqbegin(&state->seqlock);
3424 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3425 nfs4_stateid_copy(dst, &state->open_stateid);
3426 if (read_seqretry(&state->seqlock, seq))
3430 seqid_open = state->open_stateid.seqid;
3431 if (read_seqretry(&state->seqlock, seq))
3434 dst_seqid = be32_to_cpu(dst->seqid);
3435 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3436 dst->seqid = seqid_open;
3442 * Update the seqid of an open stateid after receiving
3443 * NFS4ERR_OLD_STATEID
3445 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3446 struct nfs4_state *state)
3451 int seq, status = -EAGAIN;
3456 if (!nfs4_valid_open_stateid(state))
3458 seq = read_seqbegin(&state->seqlock);
3459 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3460 if (read_seqretry(&state->seqlock, seq))
3465 write_seqlock(&state->seqlock);
3466 seqid_open = state->open_stateid.seqid;
3468 dst_seqid = be32_to_cpu(dst->seqid);
3470 /* Did another OPEN bump the state's seqid? try again: */
3471 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3472 dst->seqid = seqid_open;
3473 write_sequnlock(&state->seqlock);
3478 /* server says we're behind but we haven't seen the update yet */
3479 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3480 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3481 write_sequnlock(&state->seqlock);
3482 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3484 if (fatal_signal_pending(current))
3487 if (schedule_timeout(5*HZ) != 0)
3490 finish_wait(&state->waitq, &wait);
3494 if (status == -EINTR)
3497 /* we slept the whole 5 seconds, we must have lost a seqid */
3498 dst->seqid = cpu_to_be32(dst_seqid + 1);
3506 struct nfs4_closedata {
3507 struct inode *inode;
3508 struct nfs4_state *state;
3509 struct nfs_closeargs arg;
3510 struct nfs_closeres res;
3512 struct nfs4_layoutreturn_args arg;
3513 struct nfs4_layoutreturn_res res;
3514 struct nfs4_xdr_opaque_data ld_private;
3518 struct nfs_fattr fattr;
3519 unsigned long timestamp;
3522 static void nfs4_free_closedata(void *data)
3524 struct nfs4_closedata *calldata = data;
3525 struct nfs4_state_owner *sp = calldata->state->owner;
3526 struct super_block *sb = calldata->state->inode->i_sb;
3528 if (calldata->lr.roc)
3529 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3530 calldata->res.lr_ret);
3531 nfs4_put_open_state(calldata->state);
3532 nfs_free_seqid(calldata->arg.seqid);
3533 nfs4_put_state_owner(sp);
3534 nfs_sb_deactive(sb);
3538 static void nfs4_close_done(struct rpc_task *task, void *data)
3540 struct nfs4_closedata *calldata = data;
3541 struct nfs4_state *state = calldata->state;
3542 struct nfs_server *server = NFS_SERVER(calldata->inode);
3543 nfs4_stateid *res_stateid = NULL;
3544 struct nfs4_exception exception = {
3546 .inode = calldata->inode,
3547 .stateid = &calldata->arg.stateid,
3550 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3552 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3554 /* Handle Layoutreturn errors */
3555 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3556 &calldata->res.lr_ret) == -EAGAIN)
3559 /* hmm. we are done with the inode, and in the process of freeing
3560 * the state_owner. we keep this around to process errors
3562 switch (task->tk_status) {
3564 res_stateid = &calldata->res.stateid;
3565 renew_lease(server, calldata->timestamp);
3567 case -NFS4ERR_ACCESS:
3568 if (calldata->arg.bitmask != NULL) {
3569 calldata->arg.bitmask = NULL;
3570 calldata->res.fattr = NULL;
3575 case -NFS4ERR_OLD_STATEID:
3576 /* Did we race with OPEN? */
3577 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3581 case -NFS4ERR_ADMIN_REVOKED:
3582 case -NFS4ERR_STALE_STATEID:
3583 case -NFS4ERR_EXPIRED:
3584 nfs4_free_revoked_stateid(server,
3585 &calldata->arg.stateid,
3586 task->tk_msg.rpc_cred);
3588 case -NFS4ERR_BAD_STATEID:
3589 if (calldata->arg.fmode == 0)
3593 task->tk_status = nfs4_async_handle_exception(task,
3594 server, task->tk_status, &exception);
3595 if (exception.retry)
3598 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3599 res_stateid, calldata->arg.fmode);
3601 task->tk_status = 0;
3602 nfs_release_seqid(calldata->arg.seqid);
3603 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3604 dprintk("%s: ret = %d\n", __func__, task->tk_status);
3607 task->tk_status = 0;
3608 rpc_restart_call_prepare(task);
3612 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3614 struct nfs4_closedata *calldata = data;
3615 struct nfs4_state *state = calldata->state;
3616 struct inode *inode = calldata->inode;
3617 struct nfs_server *server = NFS_SERVER(inode);
3618 struct pnfs_layout_hdr *lo;
3619 bool is_rdonly, is_wronly, is_rdwr;
3622 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3625 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3626 spin_lock(&state->owner->so_lock);
3627 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3628 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3629 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3630 /* Calculate the change in open mode */
3631 calldata->arg.fmode = 0;
3632 if (state->n_rdwr == 0) {
3633 if (state->n_rdonly == 0)
3634 call_close |= is_rdonly;
3636 calldata->arg.fmode |= FMODE_READ;
3637 if (state->n_wronly == 0)
3638 call_close |= is_wronly;
3640 calldata->arg.fmode |= FMODE_WRITE;
3641 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3642 call_close |= is_rdwr;
3644 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3646 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3647 if (!nfs4_valid_open_stateid(state))
3649 spin_unlock(&state->owner->so_lock);
3652 /* Note: exit _without_ calling nfs4_close_done */
3656 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3657 nfs_release_seqid(calldata->arg.seqid);
3661 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3662 if (lo && !pnfs_layout_is_valid(lo)) {
3663 calldata->arg.lr_args = NULL;
3664 calldata->res.lr_res = NULL;
3667 if (calldata->arg.fmode == 0)
3668 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3670 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3671 /* Close-to-open cache consistency revalidation */
3672 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3673 nfs4_bitmask_set(calldata->arg.bitmask_store,
3674 server->cache_consistency_bitmask,
3676 calldata->arg.bitmask = calldata->arg.bitmask_store;
3678 calldata->arg.bitmask = NULL;
3681 calldata->arg.share_access =
3682 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3683 calldata->arg.fmode, 0);
3685 if (calldata->res.fattr == NULL)
3686 calldata->arg.bitmask = NULL;
3687 else if (calldata->arg.bitmask == NULL)
3688 calldata->res.fattr = NULL;
3689 calldata->timestamp = jiffies;
3690 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3691 &calldata->arg.seq_args,
3692 &calldata->res.seq_res,
3694 nfs_release_seqid(calldata->arg.seqid);
3697 task->tk_action = NULL;
3699 nfs4_sequence_done(task, &calldata->res.seq_res);
3702 static const struct rpc_call_ops nfs4_close_ops = {
3703 .rpc_call_prepare = nfs4_close_prepare,
3704 .rpc_call_done = nfs4_close_done,
3705 .rpc_release = nfs4_free_closedata,
3709 * It is possible for data to be read/written from a mem-mapped file
3710 * after the sys_close call (which hits the vfs layer as a flush).
3711 * This means that we can't safely call nfsv4 close on a file until
3712 * the inode is cleared. This in turn means that we are not good
3713 * NFSv4 citizens - we do not indicate to the server to update the file's
3714 * share state even when we are done with one of the three share
3715 * stateid's in the inode.
3717 * NOTE: Caller must be holding the sp->so_owner semaphore!
3719 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3721 struct nfs_server *server = NFS_SERVER(state->inode);
3722 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3723 struct nfs4_closedata *calldata;
3724 struct nfs4_state_owner *sp = state->owner;
3725 struct rpc_task *task;
3726 struct rpc_message msg = {
3727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3728 .rpc_cred = state->owner->so_cred,
3730 struct rpc_task_setup task_setup_data = {
3731 .rpc_client = server->client,
3732 .rpc_message = &msg,
3733 .callback_ops = &nfs4_close_ops,
3734 .workqueue = nfsiod_workqueue,
3735 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3737 int status = -ENOMEM;
3739 if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
3740 task_setup_data.flags |= RPC_TASK_MOVEABLE;
3742 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3743 &task_setup_data.rpc_client, &msg);
3745 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3746 if (calldata == NULL)
3748 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3749 calldata->inode = state->inode;
3750 calldata->state = state;
3751 calldata->arg.fh = NFS_FH(state->inode);
3752 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3753 goto out_free_calldata;
3754 /* Serialization for the sequence id */
3755 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3756 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3757 if (IS_ERR(calldata->arg.seqid))
3758 goto out_free_calldata;
3759 nfs_fattr_init(&calldata->fattr);
3760 calldata->arg.fmode = 0;
3761 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3762 calldata->res.fattr = &calldata->fattr;
3763 calldata->res.seqid = calldata->arg.seqid;
3764 calldata->res.server = server;
3765 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3766 calldata->lr.roc = pnfs_roc(state->inode,
3767 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3768 if (calldata->lr.roc) {
3769 calldata->arg.lr_args = &calldata->lr.arg;
3770 calldata->res.lr_res = &calldata->lr.res;
3772 nfs_sb_active(calldata->inode->i_sb);
3774 msg.rpc_argp = &calldata->arg;
3775 msg.rpc_resp = &calldata->res;
3776 task_setup_data.callback_data = calldata;
3777 task = rpc_run_task(&task_setup_data);
3779 return PTR_ERR(task);
3782 status = rpc_wait_for_completion_task(task);
3788 nfs4_put_open_state(state);
3789 nfs4_put_state_owner(sp);
3793 static struct inode *
3794 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3795 int open_flags, struct iattr *attr, int *opened)
3797 struct nfs4_state *state;
3798 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3800 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3802 /* Protect against concurrent sillydeletes */
3803 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3805 nfs4_label_release_security(label);
3808 return ERR_CAST(state);
3809 return state->inode;
3812 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3814 if (ctx->state == NULL)
3817 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3819 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3822 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3823 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3824 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3826 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3828 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3829 struct nfs4_server_caps_arg args = {
3833 struct nfs4_server_caps_res res = {};
3834 struct rpc_message msg = {
3835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3842 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3843 FATTR4_WORD0_FH_EXPIRE_TYPE |
3844 FATTR4_WORD0_LINK_SUPPORT |
3845 FATTR4_WORD0_SYMLINK_SUPPORT |
3846 FATTR4_WORD0_ACLSUPPORT |
3847 FATTR4_WORD0_CASE_INSENSITIVE |
3848 FATTR4_WORD0_CASE_PRESERVING;
3850 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3852 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3854 /* Sanity check the server answers */
3855 switch (minorversion) {
3857 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3858 res.attr_bitmask[2] = 0;
3861 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3864 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3866 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3867 server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3868 NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3869 server->fattr_valid = NFS_ATTR_FATTR_V4;
3870 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3871 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3872 server->caps |= NFS_CAP_ACLS;
3873 if (res.has_links != 0)
3874 server->caps |= NFS_CAP_HARDLINKS;
3875 if (res.has_symlinks != 0)
3876 server->caps |= NFS_CAP_SYMLINKS;
3877 if (res.case_insensitive)
3878 server->caps |= NFS_CAP_CASE_INSENSITIVE;
3879 if (res.case_preserving)
3880 server->caps |= NFS_CAP_CASE_PRESERVING;
3881 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3882 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3883 server->caps |= NFS_CAP_SECURITY_LABEL;
3885 if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS)
3886 server->caps |= NFS_CAP_FS_LOCATIONS;
3887 if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3888 server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3889 if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3890 server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3891 if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3892 server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3893 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3894 server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3895 NFS_ATTR_FATTR_OWNER_NAME);
3896 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3897 server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3898 NFS_ATTR_FATTR_GROUP_NAME);
3899 if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3900 server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3901 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3902 server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3903 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3904 server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3905 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3906 server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
3907 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3908 sizeof(server->attr_bitmask));
3909 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3911 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3912 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3913 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3914 server->cache_consistency_bitmask[2] = 0;
3916 /* Avoid a regression due to buggy server */
3917 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3918 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3919 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3920 sizeof(server->exclcreat_bitmask));
3922 server->acl_bitmask = res.acl_bitmask;
3923 server->fh_expire_type = res.fh_expire_type;
3929 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3931 struct nfs4_exception exception = {
3932 .interruptible = true,
3936 nfs4_server_set_init_caps(server);
3938 err = nfs4_handle_exception(server,
3939 _nfs4_server_capabilities(server, fhandle),
3941 } while (exception.retry);
3945 static void test_fs_location_for_trunking(struct nfs4_fs_location *location,
3946 struct nfs_client *clp,
3947 struct nfs_server *server)
3951 for (i = 0; i < location->nservers; i++) {
3952 struct nfs4_string *srv_loc = &location->servers[i];
3953 struct sockaddr addr;
3955 struct xprt_create xprt_args = {
3959 struct nfs4_add_xprt_data xprtdata = {
3962 struct rpc_add_xprt_test rpcdata = {
3963 .add_xprt_test = clp->cl_mvops->session_trunk,
3966 char *servername = NULL;
3971 addrlen = nfs_parse_server_name(srv_loc->data, srv_loc->len,
3972 &addr, sizeof(addr),
3973 clp->cl_net, server->port);
3976 xprt_args.dstaddr = &addr;
3977 xprt_args.addrlen = addrlen;
3978 servername = kmalloc(srv_loc->len + 1, GFP_KERNEL);
3981 memcpy(servername, srv_loc->data, srv_loc->len);
3982 servername[srv_loc->len] = '\0';
3983 xprt_args.servername = servername;
3985 xprtdata.cred = nfs4_get_clid_cred(clp);
3986 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
3987 rpc_clnt_setup_test_and_add_xprt,
3990 put_cred(xprtdata.cred);
3995 static int _nfs4_discover_trunking(struct nfs_server *server,
3996 struct nfs_fh *fhandle)
3998 struct nfs4_fs_locations *locations = NULL;
4000 const struct cred *cred;
4001 struct nfs_client *clp = server->nfs_client;
4002 const struct nfs4_state_maintenance_ops *ops =
4003 clp->cl_mvops->state_renewal_ops;
4004 int status = -ENOMEM, i;
4006 cred = ops->get_state_renewal_cred(clp);
4008 cred = nfs4_get_clid_cred(clp);
4013 page = alloc_page(GFP_KERNEL);
4016 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4019 locations->fattr = nfs_alloc_fattr();
4020 if (!locations->fattr)
4023 status = nfs4_proc_get_locations(server, fhandle, locations, page,
4028 for (i = 0; i < locations->nlocations; i++)
4029 test_fs_location_for_trunking(&locations->locations[i], clp,
4032 kfree(locations->fattr);
4040 static int nfs4_discover_trunking(struct nfs_server *server,
4041 struct nfs_fh *fhandle)
4043 struct nfs4_exception exception = {
4044 .interruptible = true,
4046 struct nfs_client *clp = server->nfs_client;
4049 if (!nfs4_has_session(clp))
4052 err = nfs4_handle_exception(server,
4053 _nfs4_discover_trunking(server, fhandle),
4055 } while (exception.retry);
4060 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4061 struct nfs_fsinfo *info)
4064 struct nfs4_lookup_root_arg args = {
4067 struct nfs4_lookup_res res = {
4069 .fattr = info->fattr,
4072 struct rpc_message msg = {
4073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
4078 bitmask[0] = nfs4_fattr_bitmap[0];
4079 bitmask[1] = nfs4_fattr_bitmap[1];
4081 * Process the label in the upcoming getfattr
4083 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
4085 nfs_fattr_init(info->fattr);
4086 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4089 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4090 struct nfs_fsinfo *info)
4092 struct nfs4_exception exception = {
4093 .interruptible = true,
4097 err = _nfs4_lookup_root(server, fhandle, info);
4098 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
4101 case -NFS4ERR_WRONGSEC:
4104 err = nfs4_handle_exception(server, err, &exception);
4106 } while (exception.retry);
4111 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4112 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4114 struct rpc_auth_create_args auth_args = {
4115 .pseudoflavor = flavor,
4117 struct rpc_auth *auth;
4119 auth = rpcauth_create(&auth_args, server->client);
4122 return nfs4_lookup_root(server, fhandle, info);
4126 * Retry pseudoroot lookup with various security flavors. We do this when:
4128 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4129 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4131 * Returns zero on success, or a negative NFS4ERR value, or a
4132 * negative errno value.
4134 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4135 struct nfs_fsinfo *info)
4137 /* Per 3530bis 15.33.5 */
4138 static const rpc_authflavor_t flav_array[] = {
4142 RPC_AUTH_UNIX, /* courtesy */
4145 int status = -EPERM;
4148 if (server->auth_info.flavor_len > 0) {
4149 /* try each flavor specified by user */
4150 for (i = 0; i < server->auth_info.flavor_len; i++) {
4151 status = nfs4_lookup_root_sec(server, fhandle, info,
4152 server->auth_info.flavors[i]);
4153 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4158 /* no flavors specified by user, try default list */
4159 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4160 status = nfs4_lookup_root_sec(server, fhandle, info,
4162 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4169 * -EACCES could mean that the user doesn't have correct permissions
4170 * to access the mount. It could also mean that we tried to mount
4171 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4172 * existing mount programs don't handle -EACCES very well so it should
4173 * be mapped to -EPERM instead.
4175 if (status == -EACCES)
4181 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4182 * @server: initialized nfs_server handle
4183 * @fhandle: we fill in the pseudo-fs root file handle
4184 * @info: we fill in an FSINFO struct
4185 * @auth_probe: probe the auth flavours
4187 * Returns zero on success, or a negative errno.
4189 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4190 struct nfs_fsinfo *info,
4196 status = nfs4_lookup_root(server, fhandle, info);
4198 if (auth_probe || status == NFS4ERR_WRONGSEC)
4199 status = server->nfs_client->cl_mvops->find_root_sec(server,
4203 status = nfs4_server_capabilities(server, fhandle);
4205 status = nfs4_do_fsinfo(server, fhandle, info);
4207 return nfs4_map_errors(status);
4210 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4211 struct nfs_fsinfo *info)
4214 struct nfs_fattr *fattr = info->fattr;
4216 error = nfs4_server_capabilities(server, mntfh);
4218 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4222 error = nfs4_proc_getattr(server, mntfh, fattr, NULL);
4224 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4228 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4229 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4230 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4237 * Get locations and (maybe) other attributes of a referral.
4238 * Note that we'll actually follow the referral later when
4239 * we detect fsid mismatch in inode revalidation
4241 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4242 const struct qstr *name, struct nfs_fattr *fattr,
4243 struct nfs_fh *fhandle)
4245 int status = -ENOMEM;
4246 struct page *page = NULL;
4247 struct nfs4_fs_locations *locations = NULL;
4249 page = alloc_page(GFP_KERNEL);
4252 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4253 if (locations == NULL)
4256 locations->fattr = fattr;
4258 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4263 * If the fsid didn't change, this is a migration event, not a
4264 * referral. Cause us to drop into the exception handler, which
4265 * will kick off migration recovery.
4267 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &fattr->fsid)) {
4268 dprintk("%s: server did not return a different fsid for"
4269 " a referral at %s\n", __func__, name->name);
4270 status = -NFS4ERR_MOVED;
4273 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4274 nfs_fixup_referral_attributes(fattr);
4275 memset(fhandle, 0, sizeof(struct nfs_fh));
4283 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4284 struct nfs_fattr *fattr, struct inode *inode)
4286 __u32 bitmask[NFS4_BITMASK_SZ];
4287 struct nfs4_getattr_arg args = {
4291 struct nfs4_getattr_res res = {
4295 struct rpc_message msg = {
4296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4300 unsigned short task_flags = 0;
4302 if (nfs4_has_session(server->nfs_client))
4303 task_flags = RPC_TASK_MOVEABLE;
4305 /* Is this is an attribute revalidation, subject to softreval? */
4306 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4307 task_flags |= RPC_TASK_TIMEOUT;
4309 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0);
4310 nfs_fattr_init(fattr);
4311 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4312 return nfs4_do_call_sync(server->client, server, &msg,
4313 &args.seq_args, &res.seq_res, task_flags);
4316 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4317 struct nfs_fattr *fattr, struct inode *inode)
4319 struct nfs4_exception exception = {
4320 .interruptible = true,
4324 err = _nfs4_proc_getattr(server, fhandle, fattr, inode);
4325 trace_nfs4_getattr(server, fhandle, fattr, err);
4326 err = nfs4_handle_exception(server, err,
4328 } while (exception.retry);
4333 * The file is not closed if it is opened due to the a request to change
4334 * the size of the file. The open call will not be needed once the
4335 * VFS layer lookup-intents are implemented.
4337 * Close is called when the inode is destroyed.
4338 * If we haven't opened the file for O_WRONLY, we
4339 * need to in the size_change case to obtain a stateid.
4342 * Because OPEN is always done by name in nfsv4, it is
4343 * possible that we opened a different file by the same
4344 * name. We can recognize this race condition, but we
4345 * can't do anything about it besides returning an error.
4347 * This will be fixed with VFS changes (lookup-intent).
4350 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4351 struct iattr *sattr)
4353 struct inode *inode = d_inode(dentry);
4354 const struct cred *cred = NULL;
4355 struct nfs_open_context *ctx = NULL;
4358 if (pnfs_ld_layoutret_on_setattr(inode) &&
4359 sattr->ia_valid & ATTR_SIZE &&
4360 sattr->ia_size < i_size_read(inode))
4361 pnfs_commit_and_return_layout(inode);
4363 nfs_fattr_init(fattr);
4365 /* Deal with open(O_TRUNC) */
4366 if (sattr->ia_valid & ATTR_OPEN)
4367 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4369 /* Optimization: if the end result is no change, don't RPC */
4370 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4373 /* Search for an existing open(O_WRITE) file */
4374 if (sattr->ia_valid & ATTR_FILE) {
4376 ctx = nfs_file_open_context(sattr->ia_file);
4381 /* Return any delegations if we're going to change ACLs */
4382 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4383 nfs4_inode_make_writeable(inode);
4385 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL);
4387 nfs_setattr_update_inode(inode, sattr, fattr);
4388 nfs_setsecurity(inode, fattr);
4393 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4394 struct dentry *dentry, struct nfs_fh *fhandle,
4395 struct nfs_fattr *fattr)
4397 struct nfs_server *server = NFS_SERVER(dir);
4399 struct nfs4_lookup_arg args = {
4400 .bitmask = server->attr_bitmask,
4401 .dir_fh = NFS_FH(dir),
4402 .name = &dentry->d_name,
4404 struct nfs4_lookup_res res = {
4409 struct rpc_message msg = {
4410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4414 unsigned short task_flags = 0;
4416 if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
4417 task_flags = RPC_TASK_MOVEABLE;
4419 /* Is this is an attribute revalidation, subject to softreval? */
4420 if (nfs_lookup_is_soft_revalidate(dentry))
4421 task_flags |= RPC_TASK_TIMEOUT;
4423 args.bitmask = nfs4_bitmask(server, fattr->label);
4425 nfs_fattr_init(fattr);
4427 dprintk("NFS call lookup %pd2\n", dentry);
4428 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4429 status = nfs4_do_call_sync(clnt, server, &msg,
4430 &args.seq_args, &res.seq_res, task_flags);
4431 dprintk("NFS reply lookup: %d\n", status);
4435 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4437 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4438 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4439 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4443 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4444 struct dentry *dentry, struct nfs_fh *fhandle,
4445 struct nfs_fattr *fattr)
4447 struct nfs4_exception exception = {
4448 .interruptible = true,
4450 struct rpc_clnt *client = *clnt;
4451 const struct qstr *name = &dentry->d_name;
4454 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr);
4455 trace_nfs4_lookup(dir, name, err);
4457 case -NFS4ERR_BADNAME:
4460 case -NFS4ERR_MOVED:
4461 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4462 if (err == -NFS4ERR_MOVED)
4463 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4465 case -NFS4ERR_WRONGSEC:
4467 if (client != *clnt)
4469 client = nfs4_negotiate_security(client, dir, name);
4471 return PTR_ERR(client);
4473 exception.retry = 1;
4476 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4478 } while (exception.retry);
4483 else if (client != *clnt)
4484 rpc_shutdown_client(client);
4489 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4490 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4493 struct rpc_clnt *client = NFS_CLIENT(dir);
4495 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4496 if (client != NFS_CLIENT(dir)) {
4497 rpc_shutdown_client(client);
4498 nfs_fixup_secinfo_attributes(fattr);
4504 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4505 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4507 struct rpc_clnt *client = NFS_CLIENT(dir);
4510 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4512 return ERR_PTR(status);
4513 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4516 static int _nfs4_proc_lookupp(struct inode *inode,
4517 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4519 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4520 struct nfs_server *server = NFS_SERVER(inode);
4522 struct nfs4_lookupp_arg args = {
4523 .bitmask = server->attr_bitmask,
4524 .fh = NFS_FH(inode),
4526 struct nfs4_lookupp_res res = {
4531 struct rpc_message msg = {
4532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4536 unsigned short task_flags = 0;
4538 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4539 task_flags |= RPC_TASK_TIMEOUT;
4541 args.bitmask = nfs4_bitmask(server, fattr->label);
4543 nfs_fattr_init(fattr);
4545 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4546 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4547 &res.seq_res, task_flags);
4548 dprintk("NFS reply lookupp: %d\n", status);
4552 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4553 struct nfs_fattr *fattr)
4555 struct nfs4_exception exception = {
4556 .interruptible = true,
4560 err = _nfs4_proc_lookupp(inode, fhandle, fattr);
4561 trace_nfs4_lookupp(inode, err);
4562 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4564 } while (exception.retry);
4568 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4569 const struct cred *cred)
4571 struct nfs_server *server = NFS_SERVER(inode);
4572 struct nfs4_accessargs args = {
4573 .fh = NFS_FH(inode),
4574 .access = entry->mask,
4576 struct nfs4_accessres res = {
4579 struct rpc_message msg = {
4580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4587 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4588 res.fattr = nfs_alloc_fattr();
4589 if (res.fattr == NULL)
4591 args.bitmask = server->cache_consistency_bitmask;
4593 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4595 nfs_access_set_mask(entry, res.access);
4597 nfs_refresh_inode(inode, res.fattr);
4599 nfs_free_fattr(res.fattr);
4603 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4604 const struct cred *cred)
4606 struct nfs4_exception exception = {
4607 .interruptible = true,
4611 err = _nfs4_proc_access(inode, entry, cred);
4612 trace_nfs4_access(inode, err);
4613 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4615 } while (exception.retry);
4620 * TODO: For the time being, we don't try to get any attributes
4621 * along with any of the zero-copy operations READ, READDIR,
4624 * In the case of the first three, we want to put the GETATTR
4625 * after the read-type operation -- this is because it is hard
4626 * to predict the length of a GETATTR response in v4, and thus
4627 * align the READ data correctly. This means that the GETATTR
4628 * may end up partially falling into the page cache, and we should
4629 * shift it into the 'tail' of the xdr_buf before processing.
4630 * To do this efficiently, we need to know the total length
4631 * of data received, which doesn't seem to be available outside
4634 * In the case of WRITE, we also want to put the GETATTR after
4635 * the operation -- in this case because we want to make sure
4636 * we get the post-operation mtime and size.
4638 * Both of these changes to the XDR layer would in fact be quite
4639 * minor, but I decided to leave them for a subsequent patch.
4641 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4642 unsigned int pgbase, unsigned int pglen)
4644 struct nfs4_readlink args = {
4645 .fh = NFS_FH(inode),
4650 struct nfs4_readlink_res res;
4651 struct rpc_message msg = {
4652 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4657 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4660 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4661 unsigned int pgbase, unsigned int pglen)
4663 struct nfs4_exception exception = {
4664 .interruptible = true,
4668 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4669 trace_nfs4_readlink(inode, err);
4670 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4672 } while (exception.retry);
4677 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4680 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4683 struct nfs_server *server = NFS_SERVER(dir);
4684 struct nfs4_label l, *ilabel = NULL;
4685 struct nfs_open_context *ctx;
4686 struct nfs4_state *state;
4689 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4691 return PTR_ERR(ctx);
4693 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4695 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4696 sattr->ia_mode &= ~current_umask();
4697 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4698 if (IS_ERR(state)) {
4699 status = PTR_ERR(state);
4703 nfs4_label_release_security(ilabel);
4704 put_nfs_open_context(ctx);
4709 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4711 struct nfs_server *server = NFS_SERVER(dir);
4712 struct nfs_removeargs args = {
4716 struct nfs_removeres res = {
4719 struct rpc_message msg = {
4720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4724 unsigned long timestamp = jiffies;
4727 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4729 spin_lock(&dir->i_lock);
4730 /* Removing a directory decrements nlink in the parent */
4731 if (ftype == NF4DIR && dir->i_nlink > 2)
4732 nfs4_dec_nlink_locked(dir);
4733 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4734 NFS_INO_INVALID_DATA);
4735 spin_unlock(&dir->i_lock);
4740 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4742 struct nfs4_exception exception = {
4743 .interruptible = true,
4745 struct inode *inode = d_inode(dentry);
4749 if (inode->i_nlink == 1)
4750 nfs4_inode_return_delegation(inode);
4752 nfs4_inode_make_writeable(inode);
4755 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4756 trace_nfs4_remove(dir, &dentry->d_name, err);
4757 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4759 } while (exception.retry);
4763 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4765 struct nfs4_exception exception = {
4766 .interruptible = true,
4771 err = _nfs4_proc_remove(dir, name, NF4DIR);
4772 trace_nfs4_remove(dir, name, err);
4773 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4775 } while (exception.retry);
4779 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4780 struct dentry *dentry,
4781 struct inode *inode)
4783 struct nfs_removeargs *args = msg->rpc_argp;
4784 struct nfs_removeres *res = msg->rpc_resp;
4786 res->server = NFS_SB(dentry->d_sb);
4787 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4788 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4790 nfs_fattr_init(res->dir_attr);
4793 nfs4_inode_return_delegation(inode);
4794 nfs_d_prune_case_insensitive_aliases(inode);
4798 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4800 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4801 &data->args.seq_args,
4806 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4808 struct nfs_unlinkdata *data = task->tk_calldata;
4809 struct nfs_removeres *res = &data->res;
4811 if (!nfs4_sequence_done(task, &res->seq_res))
4813 if (nfs4_async_handle_error(task, res->server, NULL,
4814 &data->timeout) == -EAGAIN)
4816 if (task->tk_status == 0)
4817 nfs4_update_changeattr(dir, &res->cinfo,
4818 res->dir_attr->time_start,
4819 NFS_INO_INVALID_DATA);
4823 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4824 struct dentry *old_dentry,
4825 struct dentry *new_dentry)
4827 struct nfs_renameargs *arg = msg->rpc_argp;
4828 struct nfs_renameres *res = msg->rpc_resp;
4829 struct inode *old_inode = d_inode(old_dentry);
4830 struct inode *new_inode = d_inode(new_dentry);
4833 nfs4_inode_make_writeable(old_inode);
4835 nfs4_inode_return_delegation(new_inode);
4836 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4837 res->server = NFS_SB(old_dentry->d_sb);
4838 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4841 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4843 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4844 &data->args.seq_args,
4849 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4850 struct inode *new_dir)
4852 struct nfs_renamedata *data = task->tk_calldata;
4853 struct nfs_renameres *res = &data->res;
4855 if (!nfs4_sequence_done(task, &res->seq_res))
4857 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4860 if (task->tk_status == 0) {
4861 nfs_d_prune_case_insensitive_aliases(d_inode(data->old_dentry));
4862 if (new_dir != old_dir) {
4863 /* Note: If we moved a directory, nlink will change */
4864 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4865 res->old_fattr->time_start,
4866 NFS_INO_INVALID_NLINK |
4867 NFS_INO_INVALID_DATA);
4868 nfs4_update_changeattr(new_dir, &res->new_cinfo,
4869 res->new_fattr->time_start,
4870 NFS_INO_INVALID_NLINK |
4871 NFS_INO_INVALID_DATA);
4873 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4874 res->old_fattr->time_start,
4875 NFS_INO_INVALID_DATA);
4880 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4882 struct nfs_server *server = NFS_SERVER(inode);
4883 __u32 bitmask[NFS4_BITMASK_SZ];
4884 struct nfs4_link_arg arg = {
4885 .fh = NFS_FH(inode),
4886 .dir_fh = NFS_FH(dir),
4890 struct nfs4_link_res res = {
4893 struct rpc_message msg = {
4894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4898 int status = -ENOMEM;
4900 res.fattr = nfs_alloc_fattr_with_label(server);
4901 if (res.fattr == NULL)
4904 nfs4_inode_make_writeable(inode);
4905 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label), inode,
4906 NFS_INO_INVALID_CHANGE);
4907 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4909 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4910 NFS_INO_INVALID_DATA);
4911 nfs4_inc_nlink(inode);
4912 status = nfs_post_op_update_inode(inode, res.fattr);
4914 nfs_setsecurity(inode, res.fattr);
4918 nfs_free_fattr(res.fattr);
4922 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4924 struct nfs4_exception exception = {
4925 .interruptible = true,
4929 err = nfs4_handle_exception(NFS_SERVER(inode),
4930 _nfs4_proc_link(inode, dir, name),
4932 } while (exception.retry);
4936 struct nfs4_createdata {
4937 struct rpc_message msg;
4938 struct nfs4_create_arg arg;
4939 struct nfs4_create_res res;
4941 struct nfs_fattr fattr;
4944 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4945 const struct qstr *name, struct iattr *sattr, u32 ftype)
4947 struct nfs4_createdata *data;
4949 data = kzalloc(sizeof(*data), GFP_KERNEL);
4951 struct nfs_server *server = NFS_SERVER(dir);
4953 data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL);
4954 if (IS_ERR(data->fattr.label))
4957 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4958 data->msg.rpc_argp = &data->arg;
4959 data->msg.rpc_resp = &data->res;
4960 data->arg.dir_fh = NFS_FH(dir);
4961 data->arg.server = server;
4962 data->arg.name = name;
4963 data->arg.attrs = sattr;
4964 data->arg.ftype = ftype;
4965 data->arg.bitmask = nfs4_bitmask(server, data->fattr.label);
4966 data->arg.umask = current_umask();
4967 data->res.server = server;
4968 data->res.fh = &data->fh;
4969 data->res.fattr = &data->fattr;
4970 nfs_fattr_init(data->res.fattr);
4978 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4980 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4981 &data->arg.seq_args, &data->res.seq_res, 1);
4983 spin_lock(&dir->i_lock);
4984 /* Creating a directory bumps nlink in the parent */
4985 if (data->arg.ftype == NF4DIR)
4986 nfs4_inc_nlink_locked(dir);
4987 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4988 data->res.fattr->time_start,
4989 NFS_INO_INVALID_DATA);
4990 spin_unlock(&dir->i_lock);
4991 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
4996 static void nfs4_free_createdata(struct nfs4_createdata *data)
4998 nfs4_label_free(data->fattr.label);
5002 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5003 struct page *page, unsigned int len, struct iattr *sattr,
5004 struct nfs4_label *label)
5006 struct nfs4_createdata *data;
5007 int status = -ENAMETOOLONG;
5009 if (len > NFS4_MAXPATHLEN)
5013 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
5017 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
5018 data->arg.u.symlink.pages = &page;
5019 data->arg.u.symlink.len = len;
5020 data->arg.label = label;
5022 status = nfs4_do_create(dir, dentry, data);
5024 nfs4_free_createdata(data);
5029 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5030 struct page *page, unsigned int len, struct iattr *sattr)
5032 struct nfs4_exception exception = {
5033 .interruptible = true,
5035 struct nfs4_label l, *label = NULL;
5038 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5041 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
5042 trace_nfs4_symlink(dir, &dentry->d_name, err);
5043 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5045 } while (exception.retry);
5047 nfs4_label_release_security(label);
5051 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5052 struct iattr *sattr, struct nfs4_label *label)
5054 struct nfs4_createdata *data;
5055 int status = -ENOMEM;
5057 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
5061 data->arg.label = label;
5062 status = nfs4_do_create(dir, dentry, data);
5064 nfs4_free_createdata(data);
5069 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5070 struct iattr *sattr)
5072 struct nfs_server *server = NFS_SERVER(dir);
5073 struct nfs4_exception exception = {
5074 .interruptible = true,
5076 struct nfs4_label l, *label = NULL;
5079 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5081 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5082 sattr->ia_mode &= ~current_umask();
5084 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
5085 trace_nfs4_mkdir(dir, &dentry->d_name, err);
5086 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5088 } while (exception.retry);
5089 nfs4_label_release_security(label);
5094 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5095 struct nfs_readdir_res *nr_res)
5097 struct inode *dir = d_inode(nr_arg->dentry);
5098 struct nfs_server *server = NFS_SERVER(dir);
5099 struct nfs4_readdir_arg args = {
5101 .pages = nr_arg->pages,
5103 .count = nr_arg->page_len,
5104 .plus = nr_arg->plus,
5106 struct nfs4_readdir_res res;
5107 struct rpc_message msg = {
5108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5111 .rpc_cred = nr_arg->cred,
5115 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5116 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5117 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5118 args.bitmask = server->attr_bitmask_nl;
5120 args.bitmask = server->attr_bitmask;
5122 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5123 res.pgbase = args.pgbase;
5124 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5127 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5128 status += args.pgbase;
5131 nfs_invalidate_atime(dir);
5133 dprintk("%s: returns %d\n", __func__, status);
5137 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5138 struct nfs_readdir_res *res)
5140 struct nfs4_exception exception = {
5141 .interruptible = true,
5145 err = _nfs4_proc_readdir(arg, res);
5146 trace_nfs4_readdir(d_inode(arg->dentry), err);
5147 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5149 } while (exception.retry);
5153 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5154 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5156 struct nfs4_createdata *data;
5157 int mode = sattr->ia_mode;
5158 int status = -ENOMEM;
5160 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5165 data->arg.ftype = NF4FIFO;
5166 else if (S_ISBLK(mode)) {
5167 data->arg.ftype = NF4BLK;
5168 data->arg.u.device.specdata1 = MAJOR(rdev);
5169 data->arg.u.device.specdata2 = MINOR(rdev);
5171 else if (S_ISCHR(mode)) {
5172 data->arg.ftype = NF4CHR;
5173 data->arg.u.device.specdata1 = MAJOR(rdev);
5174 data->arg.u.device.specdata2 = MINOR(rdev);
5175 } else if (!S_ISSOCK(mode)) {
5180 data->arg.label = label;
5181 status = nfs4_do_create(dir, dentry, data);
5183 nfs4_free_createdata(data);
5188 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5189 struct iattr *sattr, dev_t rdev)
5191 struct nfs_server *server = NFS_SERVER(dir);
5192 struct nfs4_exception exception = {
5193 .interruptible = true,
5195 struct nfs4_label l, *label = NULL;
5198 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5200 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5201 sattr->ia_mode &= ~current_umask();
5203 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5204 trace_nfs4_mknod(dir, &dentry->d_name, err);
5205 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5207 } while (exception.retry);
5209 nfs4_label_release_security(label);
5214 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5215 struct nfs_fsstat *fsstat)
5217 struct nfs4_statfs_arg args = {
5219 .bitmask = server->attr_bitmask,
5221 struct nfs4_statfs_res res = {
5224 struct rpc_message msg = {
5225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5230 nfs_fattr_init(fsstat->fattr);
5231 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5234 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5236 struct nfs4_exception exception = {
5237 .interruptible = true,
5241 err = nfs4_handle_exception(server,
5242 _nfs4_proc_statfs(server, fhandle, fsstat),
5244 } while (exception.retry);
5248 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5249 struct nfs_fsinfo *fsinfo)
5251 struct nfs4_fsinfo_arg args = {
5253 .bitmask = server->attr_bitmask,
5255 struct nfs4_fsinfo_res res = {
5258 struct rpc_message msg = {
5259 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5264 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5267 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5269 struct nfs4_exception exception = {
5270 .interruptible = true,
5275 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5276 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5278 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5281 err = nfs4_handle_exception(server, err, &exception);
5282 } while (exception.retry);
5286 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5290 nfs_fattr_init(fsinfo->fattr);
5291 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5293 /* block layout checks this! */
5294 server->pnfs_blksize = fsinfo->blksize;
5295 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5301 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5302 struct nfs_pathconf *pathconf)
5304 struct nfs4_pathconf_arg args = {
5306 .bitmask = server->attr_bitmask,
5308 struct nfs4_pathconf_res res = {
5309 .pathconf = pathconf,
5311 struct rpc_message msg = {
5312 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5317 /* None of the pathconf attributes are mandatory to implement */
5318 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5319 memset(pathconf, 0, sizeof(*pathconf));
5323 nfs_fattr_init(pathconf->fattr);
5324 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5327 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5328 struct nfs_pathconf *pathconf)
5330 struct nfs4_exception exception = {
5331 .interruptible = true,
5336 err = nfs4_handle_exception(server,
5337 _nfs4_proc_pathconf(server, fhandle, pathconf),
5339 } while (exception.retry);
5343 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5344 const struct nfs_open_context *ctx,
5345 const struct nfs_lock_context *l_ctx,
5348 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5350 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5352 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5353 const struct nfs_open_context *ctx,
5354 const struct nfs_lock_context *l_ctx,
5357 nfs4_stateid _current_stateid;
5359 /* If the current stateid represents a lost lock, then exit */
5360 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5362 return nfs4_stateid_match(stateid, &_current_stateid);
5365 static bool nfs4_error_stateid_expired(int err)
5368 case -NFS4ERR_DELEG_REVOKED:
5369 case -NFS4ERR_ADMIN_REVOKED:
5370 case -NFS4ERR_BAD_STATEID:
5371 case -NFS4ERR_STALE_STATEID:
5372 case -NFS4ERR_OLD_STATEID:
5373 case -NFS4ERR_OPENMODE:
5374 case -NFS4ERR_EXPIRED:
5380 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5382 struct nfs_server *server = NFS_SERVER(hdr->inode);
5384 trace_nfs4_read(hdr, task->tk_status);
5385 if (task->tk_status < 0) {
5386 struct nfs4_exception exception = {
5387 .inode = hdr->inode,
5388 .state = hdr->args.context->state,
5389 .stateid = &hdr->args.stateid,
5391 task->tk_status = nfs4_async_handle_exception(task,
5392 server, task->tk_status, &exception);
5393 if (exception.retry) {
5394 rpc_restart_call_prepare(task);
5399 if (task->tk_status > 0)
5400 renew_lease(server, hdr->timestamp);
5404 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5405 struct nfs_pgio_args *args)
5408 if (!nfs4_error_stateid_expired(task->tk_status) ||
5409 nfs4_stateid_is_current(&args->stateid,
5414 rpc_restart_call_prepare(task);
5418 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5419 struct nfs_pgio_header *hdr)
5421 struct nfs_server *server = NFS_SERVER(hdr->inode);
5422 struct rpc_message *msg = &task->tk_msg;
5424 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5425 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5426 server->caps &= ~NFS_CAP_READ_PLUS;
5427 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5428 rpc_restart_call_prepare(task);
5434 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5436 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5438 if (nfs4_read_stateid_changed(task, &hdr->args))
5440 if (nfs4_read_plus_not_supported(task, hdr))
5442 if (task->tk_status > 0)
5443 nfs_invalidate_atime(hdr->inode);
5444 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5445 nfs4_read_done_cb(task, hdr);
5448 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5449 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5450 struct rpc_message *msg)
5452 /* Note: We don't use READ_PLUS with pNFS yet */
5453 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
5454 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5457 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5458 struct rpc_message *msg)
5461 #endif /* CONFIG_NFS_V4_2 */
5463 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5464 struct rpc_message *msg)
5466 hdr->timestamp = jiffies;
5467 if (!hdr->pgio_done_cb)
5468 hdr->pgio_done_cb = nfs4_read_done_cb;
5469 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5470 nfs42_read_plus_support(hdr, msg);
5471 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5474 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5475 struct nfs_pgio_header *hdr)
5477 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5478 &hdr->args.seq_args,
5482 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5483 hdr->args.lock_context,
5484 hdr->rw_mode) == -EIO)
5486 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5491 static int nfs4_write_done_cb(struct rpc_task *task,
5492 struct nfs_pgio_header *hdr)
5494 struct inode *inode = hdr->inode;
5496 trace_nfs4_write(hdr, task->tk_status);
5497 if (task->tk_status < 0) {
5498 struct nfs4_exception exception = {
5499 .inode = hdr->inode,
5500 .state = hdr->args.context->state,
5501 .stateid = &hdr->args.stateid,
5503 task->tk_status = nfs4_async_handle_exception(task,
5504 NFS_SERVER(inode), task->tk_status,
5506 if (exception.retry) {
5507 rpc_restart_call_prepare(task);
5511 if (task->tk_status >= 0) {
5512 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5513 nfs_writeback_update_inode(hdr);
5518 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5519 struct nfs_pgio_args *args)
5522 if (!nfs4_error_stateid_expired(task->tk_status) ||
5523 nfs4_stateid_is_current(&args->stateid,
5528 rpc_restart_call_prepare(task);
5532 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5534 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5536 if (nfs4_write_stateid_changed(task, &hdr->args))
5538 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5539 nfs4_write_done_cb(task, hdr);
5543 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5545 /* Don't request attributes for pNFS or O_DIRECT writes */
5546 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5548 /* Otherwise, request attributes if and only if we don't hold
5551 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5554 void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[],
5555 struct inode *inode, unsigned long cache_validity)
5557 struct nfs_server *server = NFS_SERVER(inode);
5560 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5561 cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity);
5563 if (cache_validity & NFS_INO_INVALID_CHANGE)
5564 bitmask[0] |= FATTR4_WORD0_CHANGE;
5565 if (cache_validity & NFS_INO_INVALID_ATIME)
5566 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5567 if (cache_validity & NFS_INO_INVALID_MODE)
5568 bitmask[1] |= FATTR4_WORD1_MODE;
5569 if (cache_validity & NFS_INO_INVALID_OTHER)
5570 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5571 if (cache_validity & NFS_INO_INVALID_NLINK)
5572 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5573 if (cache_validity & NFS_INO_INVALID_CTIME)
5574 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5575 if (cache_validity & NFS_INO_INVALID_MTIME)
5576 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5577 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5578 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5580 if (cache_validity & NFS_INO_INVALID_SIZE)
5581 bitmask[0] |= FATTR4_WORD0_SIZE;
5583 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5584 bitmask[i] &= server->attr_bitmask[i];
5587 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5588 struct rpc_message *msg,
5589 struct rpc_clnt **clnt)
5591 struct nfs_server *server = NFS_SERVER(hdr->inode);
5593 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5594 hdr->args.bitmask = NULL;
5595 hdr->res.fattr = NULL;
5597 nfs4_bitmask_set(hdr->args.bitmask_store,
5598 server->cache_consistency_bitmask,
5599 hdr->inode, NFS_INO_INVALID_BLOCKS);
5600 hdr->args.bitmask = hdr->args.bitmask_store;
5603 if (!hdr->pgio_done_cb)
5604 hdr->pgio_done_cb = nfs4_write_done_cb;
5605 hdr->res.server = server;
5606 hdr->timestamp = jiffies;
5608 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5609 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5610 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5613 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5615 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5616 &data->args.seq_args,
5621 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5623 struct inode *inode = data->inode;
5625 trace_nfs4_commit(data, task->tk_status);
5626 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5627 NULL, NULL) == -EAGAIN) {
5628 rpc_restart_call_prepare(task);
5634 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5636 if (!nfs4_sequence_done(task, &data->res.seq_res))
5638 return data->commit_done_cb(task, data);
5641 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5642 struct rpc_clnt **clnt)
5644 struct nfs_server *server = NFS_SERVER(data->inode);
5646 if (data->commit_done_cb == NULL)
5647 data->commit_done_cb = nfs4_commit_done_cb;
5648 data->res.server = server;
5649 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5650 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5651 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5654 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5655 struct nfs_commitres *res)
5657 struct inode *dst_inode = file_inode(dst);
5658 struct nfs_server *server = NFS_SERVER(dst_inode);
5659 struct rpc_message msg = {
5660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5665 args->fh = NFS_FH(dst_inode);
5666 return nfs4_call_sync(server->client, server, &msg,
5667 &args->seq_args, &res->seq_res, 1);
5670 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5672 struct nfs_commitargs args = {
5676 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5677 struct nfs4_exception exception = { };
5681 status = _nfs4_proc_commit(dst, &args, res);
5682 status = nfs4_handle_exception(dst_server, status, &exception);
5683 } while (exception.retry);
5688 struct nfs4_renewdata {
5689 struct nfs_client *client;
5690 unsigned long timestamp;
5694 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5695 * standalone procedure for queueing an asynchronous RENEW.
5697 static void nfs4_renew_release(void *calldata)
5699 struct nfs4_renewdata *data = calldata;
5700 struct nfs_client *clp = data->client;
5702 if (refcount_read(&clp->cl_count) > 1)
5703 nfs4_schedule_state_renewal(clp);
5704 nfs_put_client(clp);
5708 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5710 struct nfs4_renewdata *data = calldata;
5711 struct nfs_client *clp = data->client;
5712 unsigned long timestamp = data->timestamp;
5714 trace_nfs4_renew_async(clp, task->tk_status);
5715 switch (task->tk_status) {
5718 case -NFS4ERR_LEASE_MOVED:
5719 nfs4_schedule_lease_moved_recovery(clp);
5722 /* Unless we're shutting down, schedule state recovery! */
5723 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5725 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5726 nfs4_schedule_lease_recovery(clp);
5729 nfs4_schedule_path_down_recovery(clp);
5731 do_renew_lease(clp, timestamp);
5734 static const struct rpc_call_ops nfs4_renew_ops = {
5735 .rpc_call_done = nfs4_renew_done,
5736 .rpc_release = nfs4_renew_release,
5739 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5741 struct rpc_message msg = {
5742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5746 struct nfs4_renewdata *data;
5748 if (renew_flags == 0)
5750 if (!refcount_inc_not_zero(&clp->cl_count))
5752 data = kmalloc(sizeof(*data), GFP_NOFS);
5754 nfs_put_client(clp);
5758 data->timestamp = jiffies;
5759 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5760 &nfs4_renew_ops, data);
5763 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5765 struct rpc_message msg = {
5766 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5770 unsigned long now = jiffies;
5773 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5776 do_renew_lease(clp, now);
5780 static bool nfs4_server_supports_acls(const struct nfs_server *server,
5781 enum nfs4_acl_type type)
5785 return server->attr_bitmask[0] & FATTR4_WORD0_ACL;
5787 return server->attr_bitmask[1] & FATTR4_WORD1_DACL;
5789 return server->attr_bitmask[1] & FATTR4_WORD1_SACL;
5793 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5794 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5797 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5799 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5800 struct page **pages)
5802 struct page *newpage, **spages;
5808 len = min_t(size_t, PAGE_SIZE, buflen);
5809 newpage = alloc_page(GFP_KERNEL);
5811 if (newpage == NULL)
5813 memcpy(page_address(newpage), buf, len);
5818 } while (buflen != 0);
5824 __free_page(spages[rc-1]);
5828 struct nfs4_cached_acl {
5829 enum nfs4_acl_type type;
5835 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5837 struct nfs_inode *nfsi = NFS_I(inode);
5839 spin_lock(&inode->i_lock);
5840 kfree(nfsi->nfs4_acl);
5841 nfsi->nfs4_acl = acl;
5842 spin_unlock(&inode->i_lock);
5845 static void nfs4_zap_acl_attr(struct inode *inode)
5847 nfs4_set_cached_acl(inode, NULL);
5850 static ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf,
5851 size_t buflen, enum nfs4_acl_type type)
5853 struct nfs_inode *nfsi = NFS_I(inode);
5854 struct nfs4_cached_acl *acl;
5857 spin_lock(&inode->i_lock);
5858 acl = nfsi->nfs4_acl;
5861 if (acl->type != type)
5863 if (buf == NULL) /* user is just asking for length */
5865 if (acl->cached == 0)
5867 ret = -ERANGE; /* see getxattr(2) man page */
5868 if (acl->len > buflen)
5870 memcpy(buf, acl->data, acl->len);
5874 spin_unlock(&inode->i_lock);
5878 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages,
5879 size_t pgbase, size_t acl_len,
5880 enum nfs4_acl_type type)
5882 struct nfs4_cached_acl *acl;
5883 size_t buflen = sizeof(*acl) + acl_len;
5885 if (buflen <= PAGE_SIZE) {
5886 acl = kmalloc(buflen, GFP_KERNEL);
5890 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5892 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5900 nfs4_set_cached_acl(inode, acl);
5904 * The getxattr API returns the required buffer length when called with a
5905 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5906 * the required buf. On a NULL buf, we send a page of data to the server
5907 * guessing that the ACL request can be serviced by a page. If so, we cache
5908 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5909 * the cache. If not so, we throw away the page, and cache the required
5910 * length. The next getxattr call will then produce another round trip to
5911 * the server, this time with the input buf of the required size.
5913 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf,
5914 size_t buflen, enum nfs4_acl_type type)
5916 struct page **pages;
5917 struct nfs_getaclargs args = {
5918 .fh = NFS_FH(inode),
5922 struct nfs_getaclres res = {
5926 struct rpc_message msg = {
5927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5931 unsigned int npages;
5932 int ret = -ENOMEM, i;
5933 struct nfs_server *server = NFS_SERVER(inode);
5936 buflen = server->rsize;
5938 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5939 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
5943 args.acl_pages = pages;
5945 for (i = 0; i < npages; i++) {
5946 pages[i] = alloc_page(GFP_KERNEL);
5951 /* for decoding across pages */
5952 res.acl_scratch = alloc_page(GFP_KERNEL);
5953 if (!res.acl_scratch)
5956 args.acl_len = npages * PAGE_SIZE;
5958 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5959 __func__, buf, buflen, npages, args.acl_len);
5960 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5961 &msg, &args.seq_args, &res.seq_res, 0);
5965 /* Handle the case where the passed-in buffer is too short */
5966 if (res.acl_flags & NFS4_ACL_TRUNC) {
5967 /* Did the user only issue a request for the acl length? */
5973 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len,
5976 if (res.acl_len > buflen) {
5980 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5985 for (i = 0; i < npages; i++)
5987 __free_page(pages[i]);
5988 if (res.acl_scratch)
5989 __free_page(res.acl_scratch);
5994 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf,
5995 size_t buflen, enum nfs4_acl_type type)
5997 struct nfs4_exception exception = {
5998 .interruptible = true,
6002 ret = __nfs4_get_acl_uncached(inode, buf, buflen, type);
6003 trace_nfs4_get_acl(inode, ret);
6006 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
6007 } while (exception.retry);
6011 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen,
6012 enum nfs4_acl_type type)
6014 struct nfs_server *server = NFS_SERVER(inode);
6017 if (!nfs4_server_supports_acls(server, type))
6019 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
6022 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
6023 nfs_zap_acl_cache(inode);
6024 ret = nfs4_read_cached_acl(inode, buf, buflen, type);
6026 /* -ENOENT is returned if there is no ACL or if there is an ACL
6027 * but no cached acl data, just the acl length */
6029 return nfs4_get_acl_uncached(inode, buf, buflen, type);
6032 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf,
6033 size_t buflen, enum nfs4_acl_type type)
6035 struct nfs_server *server = NFS_SERVER(inode);
6036 struct page *pages[NFS4ACL_MAXPAGES];
6037 struct nfs_setaclargs arg = {
6038 .fh = NFS_FH(inode),
6043 struct nfs_setaclres res;
6044 struct rpc_message msg = {
6045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
6049 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
6052 /* You can't remove system.nfs4_acl: */
6055 if (!nfs4_server_supports_acls(server, type))
6057 if (npages > ARRAY_SIZE(pages))
6059 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
6062 nfs4_inode_make_writeable(inode);
6063 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6066 * Free each page after tx, so the only ref left is
6067 * held by the network stack
6070 put_page(pages[i-1]);
6073 * Acl update can result in inode attribute update.
6074 * so mark the attribute cache invalid.
6076 spin_lock(&inode->i_lock);
6077 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
6078 NFS_INO_INVALID_CTIME |
6079 NFS_INO_REVAL_FORCED);
6080 spin_unlock(&inode->i_lock);
6081 nfs_access_zap_cache(inode);
6082 nfs_zap_acl_cache(inode);
6086 static int nfs4_proc_set_acl(struct inode *inode, const void *buf,
6087 size_t buflen, enum nfs4_acl_type type)
6089 struct nfs4_exception exception = { };
6092 err = __nfs4_proc_set_acl(inode, buf, buflen, type);
6093 trace_nfs4_set_acl(inode, err);
6094 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
6096 * no need to retry since the kernel
6097 * isn't involved in encoding the ACEs.
6102 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6104 } while (exception.retry);
6108 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6109 static int _nfs4_get_security_label(struct inode *inode, void *buf,
6112 struct nfs_server *server = NFS_SERVER(inode);
6113 struct nfs4_label label = {0, 0, buflen, buf};
6115 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6116 struct nfs_fattr fattr = {
6119 struct nfs4_getattr_arg arg = {
6120 .fh = NFS_FH(inode),
6123 struct nfs4_getattr_res res = {
6127 struct rpc_message msg = {
6128 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6134 nfs_fattr_init(&fattr);
6136 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6139 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6144 static int nfs4_get_security_label(struct inode *inode, void *buf,
6147 struct nfs4_exception exception = {
6148 .interruptible = true,
6152 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6156 err = _nfs4_get_security_label(inode, buf, buflen);
6157 trace_nfs4_get_security_label(inode, err);
6158 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6160 } while (exception.retry);
6164 static int _nfs4_do_set_security_label(struct inode *inode,
6165 struct nfs4_label *ilabel,
6166 struct nfs_fattr *fattr)
6169 struct iattr sattr = {0};
6170 struct nfs_server *server = NFS_SERVER(inode);
6171 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6172 struct nfs_setattrargs arg = {
6173 .fh = NFS_FH(inode),
6179 struct nfs_setattrres res = {
6183 struct rpc_message msg = {
6184 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6190 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6192 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6194 dprintk("%s failed: %d\n", __func__, status);
6199 static int nfs4_do_set_security_label(struct inode *inode,
6200 struct nfs4_label *ilabel,
6201 struct nfs_fattr *fattr)
6203 struct nfs4_exception exception = { };
6207 err = _nfs4_do_set_security_label(inode, ilabel, fattr);
6208 trace_nfs4_set_security_label(inode, err);
6209 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6211 } while (exception.retry);
6216 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6218 struct nfs4_label ilabel = {0, 0, buflen, (char *)buf };
6219 struct nfs_fattr *fattr;
6222 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6225 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
6229 status = nfs4_do_set_security_label(inode, &ilabel, fattr);
6231 nfs_setsecurity(inode, fattr);
6235 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6238 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6239 nfs4_verifier *bootverf)
6243 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6244 /* An impossible timestamp guarantees this value
6245 * will never match a generated boot time. */
6246 verf[0] = cpu_to_be32(U32_MAX);
6247 verf[1] = cpu_to_be32(U32_MAX);
6249 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6250 u64 ns = ktime_to_ns(nn->boot_time);
6252 verf[0] = cpu_to_be32(ns >> 32);
6253 verf[1] = cpu_to_be32(ns);
6255 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6259 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6261 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6262 struct nfs_netns_client *nn_clp = nn->nfs_client;
6269 id = rcu_dereference(nn_clp->identifier);
6271 strscpy(buf, id, buflen);
6275 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6276 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6282 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6284 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6289 if (clp->cl_owner_id != NULL)
6294 strlen(clp->cl_rpcclient->cl_nodename) +
6296 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6300 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6304 if (len > NFS4_OPAQUE_LIMIT + 1)
6308 * Since this string is allocated at mount time, and held until the
6309 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6310 * about a memory-reclaim deadlock.
6312 str = kmalloc(len, GFP_KERNEL);
6318 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6319 clp->cl_rpcclient->cl_nodename, buf,
6320 rpc_peeraddr2str(clp->cl_rpcclient,
6323 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6324 clp->cl_rpcclient->cl_nodename,
6325 rpc_peeraddr2str(clp->cl_rpcclient,
6329 clp->cl_owner_id = str;
6334 nfs4_init_uniform_client_string(struct nfs_client *clp)
6336 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6341 if (clp->cl_owner_id != NULL)
6344 len = 10 + 10 + 1 + 10 + 1 +
6345 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6347 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6351 if (len > NFS4_OPAQUE_LIMIT + 1)
6355 * Since this string is allocated at mount time, and held until the
6356 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6357 * about a memory-reclaim deadlock.
6359 str = kmalloc(len, GFP_KERNEL);
6364 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6365 clp->rpc_ops->version, clp->cl_minorversion,
6366 buf, clp->cl_rpcclient->cl_nodename);
6368 scnprintf(str, len, "Linux NFSv%u.%u %s",
6369 clp->rpc_ops->version, clp->cl_minorversion,
6370 clp->cl_rpcclient->cl_nodename);
6371 clp->cl_owner_id = str;
6376 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6377 * services. Advertise one based on the address family of the
6381 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6383 if (strchr(clp->cl_ipaddr, ':') != NULL)
6384 return scnprintf(buf, len, "tcp6");
6386 return scnprintf(buf, len, "tcp");
6389 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6391 struct nfs4_setclientid *sc = calldata;
6393 if (task->tk_status == 0)
6394 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6397 static const struct rpc_call_ops nfs4_setclientid_ops = {
6398 .rpc_call_done = nfs4_setclientid_done,
6402 * nfs4_proc_setclientid - Negotiate client ID
6403 * @clp: state data structure
6404 * @program: RPC program for NFSv4 callback service
6405 * @port: IP port number for NFS4 callback service
6406 * @cred: credential to use for this call
6407 * @res: where to place the result
6409 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6411 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6412 unsigned short port, const struct cred *cred,
6413 struct nfs4_setclientid_res *res)
6415 nfs4_verifier sc_verifier;
6416 struct nfs4_setclientid setclientid = {
6417 .sc_verifier = &sc_verifier,
6421 struct rpc_message msg = {
6422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6423 .rpc_argp = &setclientid,
6427 struct rpc_task_setup task_setup_data = {
6428 .rpc_client = clp->cl_rpcclient,
6429 .rpc_message = &msg,
6430 .callback_ops = &nfs4_setclientid_ops,
6431 .callback_data = &setclientid,
6432 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6434 unsigned long now = jiffies;
6437 /* nfs_client_id4 */
6438 nfs4_init_boot_verifier(clp, &sc_verifier);
6440 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6441 status = nfs4_init_uniform_client_string(clp);
6443 status = nfs4_init_nonuniform_client_string(clp);
6449 setclientid.sc_netid_len =
6450 nfs4_init_callback_netid(clp,
6451 setclientid.sc_netid,
6452 sizeof(setclientid.sc_netid));
6453 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6454 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6455 clp->cl_ipaddr, port >> 8, port & 255);
6457 dprintk("NFS call setclientid auth=%s, '%s'\n",
6458 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6461 status = nfs4_call_sync_custom(&task_setup_data);
6462 if (setclientid.sc_cred) {
6463 kfree(clp->cl_acceptor);
6464 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6465 put_rpccred(setclientid.sc_cred);
6469 do_renew_lease(clp, now);
6471 trace_nfs4_setclientid(clp, status);
6472 dprintk("NFS reply setclientid: %d\n", status);
6477 * nfs4_proc_setclientid_confirm - Confirm client ID
6478 * @clp: state data structure
6479 * @arg: result of a previous SETCLIENTID
6480 * @cred: credential to use for this call
6482 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6484 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6485 struct nfs4_setclientid_res *arg,
6486 const struct cred *cred)
6488 struct rpc_message msg = {
6489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6495 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6496 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6498 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6499 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6500 trace_nfs4_setclientid_confirm(clp, status);
6501 dprintk("NFS reply setclientid_confirm: %d\n", status);
6505 struct nfs4_delegreturndata {
6506 struct nfs4_delegreturnargs args;
6507 struct nfs4_delegreturnres res;
6509 nfs4_stateid stateid;
6510 unsigned long timestamp;
6512 struct nfs4_layoutreturn_args arg;
6513 struct nfs4_layoutreturn_res res;
6514 struct nfs4_xdr_opaque_data ld_private;
6518 struct nfs_fattr fattr;
6520 struct inode *inode;
6523 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6525 struct nfs4_delegreturndata *data = calldata;
6526 struct nfs4_exception exception = {
6527 .inode = data->inode,
6528 .stateid = &data->stateid,
6529 .task_is_privileged = data->args.seq_args.sa_privileged,
6532 if (!nfs4_sequence_done(task, &data->res.seq_res))
6535 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6537 /* Handle Layoutreturn errors */
6538 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6539 &data->res.lr_ret) == -EAGAIN)
6542 switch (task->tk_status) {
6544 renew_lease(data->res.server, data->timestamp);
6546 case -NFS4ERR_ADMIN_REVOKED:
6547 case -NFS4ERR_DELEG_REVOKED:
6548 case -NFS4ERR_EXPIRED:
6549 nfs4_free_revoked_stateid(data->res.server,
6551 task->tk_msg.rpc_cred);
6553 case -NFS4ERR_BAD_STATEID:
6554 case -NFS4ERR_STALE_STATEID:
6556 task->tk_status = 0;
6558 case -NFS4ERR_OLD_STATEID:
6559 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6560 nfs4_stateid_seqid_inc(&data->stateid);
6561 if (data->args.bitmask) {
6562 data->args.bitmask = NULL;
6563 data->res.fattr = NULL;
6566 case -NFS4ERR_ACCESS:
6567 if (data->args.bitmask) {
6568 data->args.bitmask = NULL;
6569 data->res.fattr = NULL;
6574 task->tk_status = nfs4_async_handle_exception(task,
6575 data->res.server, task->tk_status,
6577 if (exception.retry)
6580 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6581 data->rpc_status = task->tk_status;
6584 task->tk_status = 0;
6585 rpc_restart_call_prepare(task);
6588 static void nfs4_delegreturn_release(void *calldata)
6590 struct nfs4_delegreturndata *data = calldata;
6591 struct inode *inode = data->inode;
6594 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6597 nfs4_fattr_set_prechange(&data->fattr,
6598 inode_peek_iversion_raw(inode));
6599 nfs_refresh_inode(inode, &data->fattr);
6600 nfs_iput_and_deactive(inode);
6605 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6607 struct nfs4_delegreturndata *d_data;
6608 struct pnfs_layout_hdr *lo;
6610 d_data = (struct nfs4_delegreturndata *)data;
6612 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6613 nfs4_sequence_done(task, &d_data->res.seq_res);
6617 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6618 if (lo && !pnfs_layout_is_valid(lo)) {
6619 d_data->args.lr_args = NULL;
6620 d_data->res.lr_res = NULL;
6623 nfs4_setup_sequence(d_data->res.server->nfs_client,
6624 &d_data->args.seq_args,
6625 &d_data->res.seq_res,
6629 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6630 .rpc_call_prepare = nfs4_delegreturn_prepare,
6631 .rpc_call_done = nfs4_delegreturn_done,
6632 .rpc_release = nfs4_delegreturn_release,
6635 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6637 struct nfs4_delegreturndata *data;
6638 struct nfs_server *server = NFS_SERVER(inode);
6639 struct rpc_task *task;
6640 struct rpc_message msg = {
6641 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6644 struct rpc_task_setup task_setup_data = {
6645 .rpc_client = server->client,
6646 .rpc_message = &msg,
6647 .callback_ops = &nfs4_delegreturn_ops,
6648 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6652 if (nfs_server_capable(inode, NFS_CAP_MOVEABLE))
6653 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6655 data = kzalloc(sizeof(*data), GFP_KERNEL);
6659 nfs4_state_protect(server->nfs_client,
6660 NFS_SP4_MACH_CRED_CLEANUP,
6661 &task_setup_data.rpc_client, &msg);
6663 data->args.fhandle = &data->fh;
6664 data->args.stateid = &data->stateid;
6665 nfs4_bitmask_set(data->args.bitmask_store,
6666 server->cache_consistency_bitmask, inode, 0);
6667 data->args.bitmask = data->args.bitmask_store;
6668 nfs_copy_fh(&data->fh, NFS_FH(inode));
6669 nfs4_stateid_copy(&data->stateid, stateid);
6670 data->res.fattr = &data->fattr;
6671 data->res.server = server;
6672 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6673 data->lr.arg.ld_private = &data->lr.ld_private;
6674 nfs_fattr_init(data->res.fattr);
6675 data->timestamp = jiffies;
6676 data->rpc_status = 0;
6677 data->inode = nfs_igrab_and_active(inode);
6678 if (data->inode || issync) {
6679 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6682 data->args.lr_args = &data->lr.arg;
6683 data->res.lr_res = &data->lr.res;
6688 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6691 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6693 task_setup_data.callback_data = data;
6694 msg.rpc_argp = &data->args;
6695 msg.rpc_resp = &data->res;
6696 task = rpc_run_task(&task_setup_data);
6698 return PTR_ERR(task);
6701 status = rpc_wait_for_completion_task(task);
6704 status = data->rpc_status;
6710 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6712 struct nfs_server *server = NFS_SERVER(inode);
6713 struct nfs4_exception exception = { };
6716 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6717 trace_nfs4_delegreturn(inode, stateid, err);
6719 case -NFS4ERR_STALE_STATEID:
6720 case -NFS4ERR_EXPIRED:
6724 err = nfs4_handle_exception(server, err, &exception);
6725 } while (exception.retry);
6729 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6731 struct inode *inode = state->inode;
6732 struct nfs_server *server = NFS_SERVER(inode);
6733 struct nfs_client *clp = server->nfs_client;
6734 struct nfs_lockt_args arg = {
6735 .fh = NFS_FH(inode),
6738 struct nfs_lockt_res res = {
6741 struct rpc_message msg = {
6742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6745 .rpc_cred = state->owner->so_cred,
6747 struct nfs4_lock_state *lsp;
6750 arg.lock_owner.clientid = clp->cl_clientid;
6751 status = nfs4_set_lock_state(state, request);
6754 lsp = request->fl_u.nfs4_fl.owner;
6755 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6756 arg.lock_owner.s_dev = server->s_dev;
6757 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6760 request->fl_type = F_UNLCK;
6762 case -NFS4ERR_DENIED:
6765 request->fl_ops->fl_release_private(request);
6766 request->fl_ops = NULL;
6771 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6773 struct nfs4_exception exception = {
6774 .interruptible = true,
6779 err = _nfs4_proc_getlk(state, cmd, request);
6780 trace_nfs4_get_lock(request, state, cmd, err);
6781 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6783 } while (exception.retry);
6788 * Update the seqid of a lock stateid after receiving
6789 * NFS4ERR_OLD_STATEID
6791 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6792 struct nfs4_lock_state *lsp)
6794 struct nfs4_state *state = lsp->ls_state;
6797 spin_lock(&state->state_lock);
6798 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6800 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6801 nfs4_stateid_seqid_inc(dst);
6803 dst->seqid = lsp->ls_stateid.seqid;
6806 spin_unlock(&state->state_lock);
6810 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6811 struct nfs4_lock_state *lsp)
6813 struct nfs4_state *state = lsp->ls_state;
6816 spin_lock(&state->state_lock);
6817 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6818 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6819 spin_unlock(&state->state_lock);
6823 struct nfs4_unlockdata {
6824 struct nfs_locku_args arg;
6825 struct nfs_locku_res res;
6826 struct nfs4_lock_state *lsp;
6827 struct nfs_open_context *ctx;
6828 struct nfs_lock_context *l_ctx;
6829 struct file_lock fl;
6830 struct nfs_server *server;
6831 unsigned long timestamp;
6834 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6835 struct nfs_open_context *ctx,
6836 struct nfs4_lock_state *lsp,
6837 struct nfs_seqid *seqid)
6839 struct nfs4_unlockdata *p;
6840 struct nfs4_state *state = lsp->ls_state;
6841 struct inode *inode = state->inode;
6843 p = kzalloc(sizeof(*p), GFP_KERNEL);
6846 p->arg.fh = NFS_FH(inode);
6848 p->arg.seqid = seqid;
6849 p->res.seqid = seqid;
6851 /* Ensure we don't close file until we're done freeing locks! */
6852 p->ctx = get_nfs_open_context(ctx);
6853 p->l_ctx = nfs_get_lock_context(ctx);
6854 locks_init_lock(&p->fl);
6855 locks_copy_lock(&p->fl, fl);
6856 p->server = NFS_SERVER(inode);
6857 spin_lock(&state->state_lock);
6858 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6859 spin_unlock(&state->state_lock);
6863 static void nfs4_locku_release_calldata(void *data)
6865 struct nfs4_unlockdata *calldata = data;
6866 nfs_free_seqid(calldata->arg.seqid);
6867 nfs4_put_lock_state(calldata->lsp);
6868 nfs_put_lock_context(calldata->l_ctx);
6869 put_nfs_open_context(calldata->ctx);
6873 static void nfs4_locku_done(struct rpc_task *task, void *data)
6875 struct nfs4_unlockdata *calldata = data;
6876 struct nfs4_exception exception = {
6877 .inode = calldata->lsp->ls_state->inode,
6878 .stateid = &calldata->arg.stateid,
6881 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6883 switch (task->tk_status) {
6885 renew_lease(calldata->server, calldata->timestamp);
6886 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6887 if (nfs4_update_lock_stateid(calldata->lsp,
6888 &calldata->res.stateid))
6891 case -NFS4ERR_ADMIN_REVOKED:
6892 case -NFS4ERR_EXPIRED:
6893 nfs4_free_revoked_stateid(calldata->server,
6894 &calldata->arg.stateid,
6895 task->tk_msg.rpc_cred);
6897 case -NFS4ERR_BAD_STATEID:
6898 case -NFS4ERR_STALE_STATEID:
6899 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6901 rpc_restart_call_prepare(task);
6903 case -NFS4ERR_OLD_STATEID:
6904 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6906 rpc_restart_call_prepare(task);
6909 task->tk_status = nfs4_async_handle_exception(task,
6910 calldata->server, task->tk_status,
6912 if (exception.retry)
6913 rpc_restart_call_prepare(task);
6915 nfs_release_seqid(calldata->arg.seqid);
6918 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6920 struct nfs4_unlockdata *calldata = data;
6922 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6923 nfs_async_iocounter_wait(task, calldata->l_ctx))
6926 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6928 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6929 /* Note: exit _without_ running nfs4_locku_done */
6932 calldata->timestamp = jiffies;
6933 if (nfs4_setup_sequence(calldata->server->nfs_client,
6934 &calldata->arg.seq_args,
6935 &calldata->res.seq_res,
6937 nfs_release_seqid(calldata->arg.seqid);
6940 task->tk_action = NULL;
6942 nfs4_sequence_done(task, &calldata->res.seq_res);
6945 static const struct rpc_call_ops nfs4_locku_ops = {
6946 .rpc_call_prepare = nfs4_locku_prepare,
6947 .rpc_call_done = nfs4_locku_done,
6948 .rpc_release = nfs4_locku_release_calldata,
6951 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6952 struct nfs_open_context *ctx,
6953 struct nfs4_lock_state *lsp,
6954 struct nfs_seqid *seqid)
6956 struct nfs4_unlockdata *data;
6957 struct rpc_message msg = {
6958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6959 .rpc_cred = ctx->cred,
6961 struct rpc_task_setup task_setup_data = {
6962 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6963 .rpc_message = &msg,
6964 .callback_ops = &nfs4_locku_ops,
6965 .workqueue = nfsiod_workqueue,
6966 .flags = RPC_TASK_ASYNC,
6969 if (nfs_server_capable(lsp->ls_state->inode, NFS_CAP_MOVEABLE))
6970 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6972 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6973 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6975 /* Ensure this is an unlock - when canceling a lock, the
6976 * canceled lock is passed in, and it won't be an unlock.
6978 fl->fl_type = F_UNLCK;
6979 if (fl->fl_flags & FL_CLOSE)
6980 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6982 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6984 nfs_free_seqid(seqid);
6985 return ERR_PTR(-ENOMEM);
6988 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6989 msg.rpc_argp = &data->arg;
6990 msg.rpc_resp = &data->res;
6991 task_setup_data.callback_data = data;
6992 return rpc_run_task(&task_setup_data);
6995 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6997 struct inode *inode = state->inode;
6998 struct nfs4_state_owner *sp = state->owner;
6999 struct nfs_inode *nfsi = NFS_I(inode);
7000 struct nfs_seqid *seqid;
7001 struct nfs4_lock_state *lsp;
7002 struct rpc_task *task;
7003 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7005 unsigned char fl_flags = request->fl_flags;
7007 status = nfs4_set_lock_state(state, request);
7008 /* Unlock _before_ we do the RPC call */
7009 request->fl_flags |= FL_EXISTS;
7010 /* Exclude nfs_delegation_claim_locks() */
7011 mutex_lock(&sp->so_delegreturn_mutex);
7012 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
7013 down_read(&nfsi->rwsem);
7014 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
7015 up_read(&nfsi->rwsem);
7016 mutex_unlock(&sp->so_delegreturn_mutex);
7019 up_read(&nfsi->rwsem);
7020 mutex_unlock(&sp->so_delegreturn_mutex);
7023 /* Is this a delegated lock? */
7024 lsp = request->fl_u.nfs4_fl.owner;
7025 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
7027 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
7028 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
7032 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
7033 status = PTR_ERR(task);
7036 status = rpc_wait_for_completion_task(task);
7039 request->fl_flags = fl_flags;
7040 trace_nfs4_unlock(request, state, F_SETLK, status);
7044 struct nfs4_lockdata {
7045 struct nfs_lock_args arg;
7046 struct nfs_lock_res res;
7047 struct nfs4_lock_state *lsp;
7048 struct nfs_open_context *ctx;
7049 struct file_lock fl;
7050 unsigned long timestamp;
7053 struct nfs_server *server;
7056 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
7057 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
7060 struct nfs4_lockdata *p;
7061 struct inode *inode = lsp->ls_state->inode;
7062 struct nfs_server *server = NFS_SERVER(inode);
7063 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7065 p = kzalloc(sizeof(*p), gfp_mask);
7069 p->arg.fh = NFS_FH(inode);
7071 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
7072 if (IS_ERR(p->arg.open_seqid))
7074 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
7075 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
7076 if (IS_ERR(p->arg.lock_seqid))
7077 goto out_free_seqid;
7078 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
7079 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
7080 p->arg.lock_owner.s_dev = server->s_dev;
7081 p->res.lock_seqid = p->arg.lock_seqid;
7084 p->ctx = get_nfs_open_context(ctx);
7085 locks_init_lock(&p->fl);
7086 locks_copy_lock(&p->fl, fl);
7089 nfs_free_seqid(p->arg.open_seqid);
7095 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7097 struct nfs4_lockdata *data = calldata;
7098 struct nfs4_state *state = data->lsp->ls_state;
7100 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
7102 /* Do we need to do an open_to_lock_owner? */
7103 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
7104 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
7105 goto out_release_lock_seqid;
7107 nfs4_stateid_copy(&data->arg.open_stateid,
7108 &state->open_stateid);
7109 data->arg.new_lock_owner = 1;
7110 data->res.open_seqid = data->arg.open_seqid;
7112 data->arg.new_lock_owner = 0;
7113 nfs4_stateid_copy(&data->arg.lock_stateid,
7114 &data->lsp->ls_stateid);
7116 if (!nfs4_valid_open_stateid(state)) {
7117 data->rpc_status = -EBADF;
7118 task->tk_action = NULL;
7119 goto out_release_open_seqid;
7121 data->timestamp = jiffies;
7122 if (nfs4_setup_sequence(data->server->nfs_client,
7123 &data->arg.seq_args,
7127 out_release_open_seqid:
7128 nfs_release_seqid(data->arg.open_seqid);
7129 out_release_lock_seqid:
7130 nfs_release_seqid(data->arg.lock_seqid);
7132 nfs4_sequence_done(task, &data->res.seq_res);
7133 dprintk("%s: ret = %d\n", __func__, data->rpc_status);
7136 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7138 struct nfs4_lockdata *data = calldata;
7139 struct nfs4_lock_state *lsp = data->lsp;
7141 if (!nfs4_sequence_done(task, &data->res.seq_res))
7144 data->rpc_status = task->tk_status;
7145 switch (task->tk_status) {
7147 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
7149 if (data->arg.new_lock && !data->cancelled) {
7150 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
7151 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7154 if (data->arg.new_lock_owner != 0) {
7155 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7156 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7157 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7158 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7161 case -NFS4ERR_BAD_STATEID:
7162 case -NFS4ERR_OLD_STATEID:
7163 case -NFS4ERR_STALE_STATEID:
7164 case -NFS4ERR_EXPIRED:
7165 if (data->arg.new_lock_owner != 0) {
7166 if (!nfs4_stateid_match(&data->arg.open_stateid,
7167 &lsp->ls_state->open_stateid))
7169 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7174 dprintk("%s: ret = %d!\n", __func__, data->rpc_status);
7177 if (!data->cancelled)
7178 rpc_restart_call_prepare(task);
7182 static void nfs4_lock_release(void *calldata)
7184 struct nfs4_lockdata *data = calldata;
7186 nfs_free_seqid(data->arg.open_seqid);
7187 if (data->cancelled && data->rpc_status == 0) {
7188 struct rpc_task *task;
7189 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7190 data->arg.lock_seqid);
7192 rpc_put_task_async(task);
7193 dprintk("%s: cancelling lock!\n", __func__);
7195 nfs_free_seqid(data->arg.lock_seqid);
7196 nfs4_put_lock_state(data->lsp);
7197 put_nfs_open_context(data->ctx);
7201 static const struct rpc_call_ops nfs4_lock_ops = {
7202 .rpc_call_prepare = nfs4_lock_prepare,
7203 .rpc_call_done = nfs4_lock_done,
7204 .rpc_release = nfs4_lock_release,
7207 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7210 case -NFS4ERR_ADMIN_REVOKED:
7211 case -NFS4ERR_EXPIRED:
7212 case -NFS4ERR_BAD_STATEID:
7213 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7214 if (new_lock_owner != 0 ||
7215 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7216 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7218 case -NFS4ERR_STALE_STATEID:
7219 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7220 nfs4_schedule_lease_recovery(server->nfs_client);
7224 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7226 struct nfs4_lockdata *data;
7227 struct rpc_task *task;
7228 struct rpc_message msg = {
7229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7230 .rpc_cred = state->owner->so_cred,
7232 struct rpc_task_setup task_setup_data = {
7233 .rpc_client = NFS_CLIENT(state->inode),
7234 .rpc_message = &msg,
7235 .callback_ops = &nfs4_lock_ops,
7236 .workqueue = nfsiod_workqueue,
7237 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7241 if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
7242 task_setup_data.flags |= RPC_TASK_MOVEABLE;
7244 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7245 fl->fl_u.nfs4_fl.owner, GFP_KERNEL);
7249 data->arg.block = 1;
7250 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7251 recovery_type > NFS_LOCK_NEW);
7252 msg.rpc_argp = &data->arg;
7253 msg.rpc_resp = &data->res;
7254 task_setup_data.callback_data = data;
7255 if (recovery_type > NFS_LOCK_NEW) {
7256 if (recovery_type == NFS_LOCK_RECLAIM)
7257 data->arg.reclaim = NFS_LOCK_RECLAIM;
7259 data->arg.new_lock = 1;
7260 task = rpc_run_task(&task_setup_data);
7262 return PTR_ERR(task);
7263 ret = rpc_wait_for_completion_task(task);
7265 ret = data->rpc_status;
7267 nfs4_handle_setlk_error(data->server, data->lsp,
7268 data->arg.new_lock_owner, ret);
7270 data->cancelled = true;
7271 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7273 dprintk("%s: ret = %d\n", __func__, ret);
7277 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7279 struct nfs_server *server = NFS_SERVER(state->inode);
7280 struct nfs4_exception exception = {
7281 .inode = state->inode,
7286 /* Cache the lock if possible... */
7287 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7289 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7290 if (err != -NFS4ERR_DELAY)
7292 nfs4_handle_exception(server, err, &exception);
7293 } while (exception.retry);
7297 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7299 struct nfs_server *server = NFS_SERVER(state->inode);
7300 struct nfs4_exception exception = {
7301 .inode = state->inode,
7305 err = nfs4_set_lock_state(state, request);
7308 if (!recover_lost_locks) {
7309 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7313 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7315 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7319 case -NFS4ERR_GRACE:
7320 case -NFS4ERR_DELAY:
7321 nfs4_handle_exception(server, err, &exception);
7324 } while (exception.retry);
7329 #if defined(CONFIG_NFS_V4_1)
7330 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7332 struct nfs4_lock_state *lsp;
7335 status = nfs4_set_lock_state(state, request);
7338 lsp = request->fl_u.nfs4_fl.owner;
7339 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7340 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7342 return nfs4_lock_expired(state, request);
7346 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7348 struct nfs_inode *nfsi = NFS_I(state->inode);
7349 struct nfs4_state_owner *sp = state->owner;
7350 unsigned char fl_flags = request->fl_flags;
7353 request->fl_flags |= FL_ACCESS;
7354 status = locks_lock_inode_wait(state->inode, request);
7357 mutex_lock(&sp->so_delegreturn_mutex);
7358 down_read(&nfsi->rwsem);
7359 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7360 /* Yes: cache locks! */
7361 /* ...but avoid races with delegation recall... */
7362 request->fl_flags = fl_flags & ~FL_SLEEP;
7363 status = locks_lock_inode_wait(state->inode, request);
7364 up_read(&nfsi->rwsem);
7365 mutex_unlock(&sp->so_delegreturn_mutex);
7368 up_read(&nfsi->rwsem);
7369 mutex_unlock(&sp->so_delegreturn_mutex);
7370 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7372 request->fl_flags = fl_flags;
7376 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7378 struct nfs4_exception exception = {
7380 .inode = state->inode,
7381 .interruptible = true,
7386 err = _nfs4_proc_setlk(state, cmd, request);
7387 if (err == -NFS4ERR_DENIED)
7389 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7391 } while (exception.retry);
7395 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7396 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7399 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7400 struct file_lock *request)
7402 int status = -ERESTARTSYS;
7403 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7405 while(!signalled()) {
7406 status = nfs4_proc_setlk(state, cmd, request);
7407 if ((status != -EAGAIN) || IS_SETLK(cmd))
7409 freezable_schedule_timeout_interruptible(timeout);
7411 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7412 status = -ERESTARTSYS;
7417 #ifdef CONFIG_NFS_V4_1
7418 struct nfs4_lock_waiter {
7419 struct inode *inode;
7420 struct nfs_lowner owner;
7421 wait_queue_entry_t wait;
7425 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7427 struct nfs4_lock_waiter *waiter =
7428 container_of(wait, struct nfs4_lock_waiter, wait);
7430 /* NULL key means to wake up everyone */
7432 struct cb_notify_lock_args *cbnl = key;
7433 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7434 *wowner = &waiter->owner;
7436 /* Only wake if the callback was for the same owner. */
7437 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7440 /* Make sure it's for the right inode */
7441 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7445 return woken_wake_function(wait, mode, flags, key);
7449 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7451 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7452 struct nfs_server *server = NFS_SERVER(state->inode);
7453 struct nfs_client *clp = server->nfs_client;
7454 wait_queue_head_t *q = &clp->cl_lock_waitq;
7455 struct nfs4_lock_waiter waiter = {
7456 .inode = state->inode,
7457 .owner = { .clientid = clp->cl_clientid,
7458 .id = lsp->ls_seqid.owner_id,
7459 .s_dev = server->s_dev },
7463 /* Don't bother with waitqueue if we don't expect a callback */
7464 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7465 return nfs4_retry_setlk_simple(state, cmd, request);
7467 init_wait(&waiter.wait);
7468 waiter.wait.func = nfs4_wake_lock_waiter;
7469 add_wait_queue(q, &waiter.wait);
7472 status = nfs4_proc_setlk(state, cmd, request);
7473 if (status != -EAGAIN || IS_SETLK(cmd))
7476 status = -ERESTARTSYS;
7477 freezer_do_not_count();
7478 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE,
7479 NFS4_LOCK_MAXTIMEOUT);
7481 } while (!signalled());
7483 remove_wait_queue(q, &waiter.wait);
7487 #else /* !CONFIG_NFS_V4_1 */
7489 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7491 return nfs4_retry_setlk_simple(state, cmd, request);
7496 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7498 struct nfs_open_context *ctx;
7499 struct nfs4_state *state;
7502 /* verify open state */
7503 ctx = nfs_file_open_context(filp);
7506 if (IS_GETLK(cmd)) {
7508 return nfs4_proc_getlk(state, F_GETLK, request);
7512 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7515 if (request->fl_type == F_UNLCK) {
7517 return nfs4_proc_unlck(state, cmd, request);
7524 if ((request->fl_flags & FL_POSIX) &&
7525 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7529 * Don't rely on the VFS having checked the file open mode,
7530 * since it won't do this for flock() locks.
7532 switch (request->fl_type) {
7534 if (!(filp->f_mode & FMODE_READ))
7538 if (!(filp->f_mode & FMODE_WRITE))
7542 status = nfs4_set_lock_state(state, request);
7546 return nfs4_retry_setlk(state, cmd, request);
7549 static int nfs4_delete_lease(struct file *file, void **priv)
7551 return generic_setlease(file, F_UNLCK, NULL, priv);
7554 static int nfs4_add_lease(struct file *file, long arg, struct file_lock **lease,
7557 struct inode *inode = file_inode(file);
7558 fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7561 /* No delegation, no lease */
7562 if (!nfs4_have_delegation(inode, type))
7564 ret = generic_setlease(file, arg, lease, priv);
7565 if (ret || nfs4_have_delegation(inode, type))
7567 /* We raced with a delegation return */
7568 nfs4_delete_lease(file, priv);
7572 int nfs4_proc_setlease(struct file *file, long arg, struct file_lock **lease,
7578 return nfs4_add_lease(file, arg, lease, priv);
7580 return nfs4_delete_lease(file, priv);
7586 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7588 struct nfs_server *server = NFS_SERVER(state->inode);
7591 err = nfs4_set_lock_state(state, fl);
7595 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7596 if (err != -NFS4ERR_DELAY)
7599 } while (err == -NFS4ERR_DELAY);
7600 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7603 struct nfs_release_lockowner_data {
7604 struct nfs4_lock_state *lsp;
7605 struct nfs_server *server;
7606 struct nfs_release_lockowner_args args;
7607 struct nfs_release_lockowner_res res;
7608 unsigned long timestamp;
7611 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7613 struct nfs_release_lockowner_data *data = calldata;
7614 struct nfs_server *server = data->server;
7615 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7616 &data->res.seq_res, task);
7617 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7618 data->timestamp = jiffies;
7621 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7623 struct nfs_release_lockowner_data *data = calldata;
7624 struct nfs_server *server = data->server;
7626 nfs40_sequence_done(task, &data->res.seq_res);
7628 switch (task->tk_status) {
7630 renew_lease(server, data->timestamp);
7632 case -NFS4ERR_STALE_CLIENTID:
7633 case -NFS4ERR_EXPIRED:
7634 nfs4_schedule_lease_recovery(server->nfs_client);
7636 case -NFS4ERR_LEASE_MOVED:
7637 case -NFS4ERR_DELAY:
7638 if (nfs4_async_handle_error(task, server,
7639 NULL, NULL) == -EAGAIN)
7640 rpc_restart_call_prepare(task);
7644 static void nfs4_release_lockowner_release(void *calldata)
7646 struct nfs_release_lockowner_data *data = calldata;
7647 nfs4_free_lock_state(data->server, data->lsp);
7651 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7652 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7653 .rpc_call_done = nfs4_release_lockowner_done,
7654 .rpc_release = nfs4_release_lockowner_release,
7658 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7660 struct nfs_release_lockowner_data *data;
7661 struct rpc_message msg = {
7662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7665 if (server->nfs_client->cl_mvops->minor_version != 0)
7668 data = kmalloc(sizeof(*data), GFP_KERNEL);
7672 data->server = server;
7673 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7674 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7675 data->args.lock_owner.s_dev = server->s_dev;
7677 msg.rpc_argp = &data->args;
7678 msg.rpc_resp = &data->res;
7679 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7680 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7683 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7685 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7686 struct user_namespace *mnt_userns,
7687 struct dentry *unused, struct inode *inode,
7688 const char *key, const void *buf,
7689 size_t buflen, int flags)
7691 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_ACL);
7694 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7695 struct dentry *unused, struct inode *inode,
7696 const char *key, void *buf, size_t buflen)
7698 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_ACL);
7701 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7703 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_ACL);
7706 #if defined(CONFIG_NFS_V4_1)
7707 #define XATTR_NAME_NFSV4_DACL "system.nfs4_dacl"
7709 static int nfs4_xattr_set_nfs4_dacl(const struct xattr_handler *handler,
7710 struct user_namespace *mnt_userns,
7711 struct dentry *unused, struct inode *inode,
7712 const char *key, const void *buf,
7713 size_t buflen, int flags)
7715 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_DACL);
7718 static int nfs4_xattr_get_nfs4_dacl(const struct xattr_handler *handler,
7719 struct dentry *unused, struct inode *inode,
7720 const char *key, void *buf, size_t buflen)
7722 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_DACL);
7725 static bool nfs4_xattr_list_nfs4_dacl(struct dentry *dentry)
7727 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_DACL);
7730 #define XATTR_NAME_NFSV4_SACL "system.nfs4_sacl"
7732 static int nfs4_xattr_set_nfs4_sacl(const struct xattr_handler *handler,
7733 struct user_namespace *mnt_userns,
7734 struct dentry *unused, struct inode *inode,
7735 const char *key, const void *buf,
7736 size_t buflen, int flags)
7738 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_SACL);
7741 static int nfs4_xattr_get_nfs4_sacl(const struct xattr_handler *handler,
7742 struct dentry *unused, struct inode *inode,
7743 const char *key, void *buf, size_t buflen)
7745 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_SACL);
7748 static bool nfs4_xattr_list_nfs4_sacl(struct dentry *dentry)
7750 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_SACL);
7755 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7757 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7758 struct user_namespace *mnt_userns,
7759 struct dentry *unused, struct inode *inode,
7760 const char *key, const void *buf,
7761 size_t buflen, int flags)
7763 if (security_ismaclabel(key))
7764 return nfs4_set_security_label(inode, buf, buflen);
7769 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7770 struct dentry *unused, struct inode *inode,
7771 const char *key, void *buf, size_t buflen)
7773 if (security_ismaclabel(key))
7774 return nfs4_get_security_label(inode, buf, buflen);
7779 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7783 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7784 len = security_inode_listsecurity(inode, list, list_len);
7785 if (len >= 0 && list_len && len > list_len)
7791 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7792 .prefix = XATTR_SECURITY_PREFIX,
7793 .get = nfs4_xattr_get_nfs4_label,
7794 .set = nfs4_xattr_set_nfs4_label,
7800 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7807 #ifdef CONFIG_NFS_V4_2
7808 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7809 struct user_namespace *mnt_userns,
7810 struct dentry *unused, struct inode *inode,
7811 const char *key, const void *buf,
7812 size_t buflen, int flags)
7817 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7821 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7822 * flags right now. Handling of xattr operations use the normal
7823 * file read/write permissions.
7825 * Just in case the server has other ideas (which RFC 8276 allows),
7826 * do a cached access check for the XA* flags to possibly avoid
7827 * doing an RPC and getting EACCES back.
7829 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7830 if (!(mask & NFS_ACCESS_XAWRITE))
7835 ret = nfs42_proc_removexattr(inode, key);
7837 nfs4_xattr_cache_remove(inode, key);
7839 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7841 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7847 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7848 struct dentry *unused, struct inode *inode,
7849 const char *key, void *buf, size_t buflen)
7854 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7857 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7858 if (!(mask & NFS_ACCESS_XAREAD))
7862 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7866 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7867 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7870 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7876 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7885 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7888 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7889 if (!(mask & NFS_ACCESS_XALIST))
7893 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7897 ret = nfs4_xattr_cache_list(inode, list, list_len);
7898 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7903 buflen = list_len ? list_len : XATTR_LIST_MAX;
7904 buf = list_len ? list : NULL;
7908 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7921 nfs4_xattr_cache_set_list(inode, list, size);
7929 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7933 #endif /* CONFIG_NFS_V4_2 */
7936 * nfs_fhget will use either the mounted_on_fileid or the fileid
7938 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7940 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7941 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7942 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7943 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7946 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7947 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7948 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7952 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7953 const struct qstr *name,
7954 struct nfs4_fs_locations *fs_locations,
7957 struct nfs_server *server = NFS_SERVER(dir);
7959 struct nfs4_fs_locations_arg args = {
7960 .dir_fh = NFS_FH(dir),
7965 struct nfs4_fs_locations_res res = {
7966 .fs_locations = fs_locations,
7968 struct rpc_message msg = {
7969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7975 dprintk("%s: start\n", __func__);
7977 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7978 bitmask[1] = nfs4_fattr_bitmap[1];
7980 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7981 * is not supported */
7982 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7983 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7985 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7987 nfs_fattr_init(fs_locations->fattr);
7988 fs_locations->server = server;
7989 fs_locations->nlocations = 0;
7990 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7991 dprintk("%s: returned status = %d\n", __func__, status);
7995 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7996 const struct qstr *name,
7997 struct nfs4_fs_locations *fs_locations,
8000 struct nfs4_exception exception = {
8001 .interruptible = true,
8005 err = _nfs4_proc_fs_locations(client, dir, name,
8006 fs_locations, page);
8007 trace_nfs4_get_fs_locations(dir, name, err);
8008 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8010 } while (exception.retry);
8015 * This operation also signals the server that this client is
8016 * performing migration recovery. The server can stop returning
8017 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
8018 * appended to this compound to identify the client ID which is
8019 * performing recovery.
8021 static int _nfs40_proc_get_locations(struct nfs_server *server,
8022 struct nfs_fh *fhandle,
8023 struct nfs4_fs_locations *locations,
8024 struct page *page, const struct cred *cred)
8026 struct rpc_clnt *clnt = server->client;
8028 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8030 struct nfs4_fs_locations_arg args = {
8031 .clientid = server->nfs_client->cl_clientid,
8035 .migration = 1, /* skip LOOKUP */
8036 .renew = 1, /* append RENEW */
8038 struct nfs4_fs_locations_res res = {
8039 .fs_locations = locations,
8043 struct rpc_message msg = {
8044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8049 unsigned long now = jiffies;
8052 nfs_fattr_init(locations->fattr);
8053 locations->server = server;
8054 locations->nlocations = 0;
8056 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8057 status = nfs4_call_sync_sequence(clnt, server, &msg,
8058 &args.seq_args, &res.seq_res);
8062 renew_lease(server, now);
8066 #ifdef CONFIG_NFS_V4_1
8069 * This operation also signals the server that this client is
8070 * performing migration recovery. The server can stop asserting
8071 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
8072 * performing this operation is identified in the SEQUENCE
8073 * operation in this compound.
8075 * When the client supports GETATTR(fs_locations_info), it can
8076 * be plumbed in here.
8078 static int _nfs41_proc_get_locations(struct nfs_server *server,
8079 struct nfs_fh *fhandle,
8080 struct nfs4_fs_locations *locations,
8081 struct page *page, const struct cred *cred)
8083 struct rpc_clnt *clnt = server->client;
8085 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8087 struct nfs4_fs_locations_arg args = {
8091 .migration = 1, /* skip LOOKUP */
8093 struct nfs4_fs_locations_res res = {
8094 .fs_locations = locations,
8097 struct rpc_message msg = {
8098 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8103 struct nfs4_call_sync_data data = {
8104 .seq_server = server,
8105 .seq_args = &args.seq_args,
8106 .seq_res = &res.seq_res,
8108 struct rpc_task_setup task_setup_data = {
8110 .rpc_message = &msg,
8111 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
8112 .callback_data = &data,
8113 .flags = RPC_TASK_NO_ROUND_ROBIN,
8117 nfs_fattr_init(locations->fattr);
8118 locations->server = server;
8119 locations->nlocations = 0;
8121 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8122 status = nfs4_call_sync_custom(&task_setup_data);
8123 if (status == NFS4_OK &&
8124 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8125 status = -NFS4ERR_LEASE_MOVED;
8129 #endif /* CONFIG_NFS_V4_1 */
8132 * nfs4_proc_get_locations - discover locations for a migrated FSID
8133 * @server: pointer to nfs_server to process
8134 * @fhandle: pointer to the kernel NFS client file handle
8135 * @locations: result of query
8137 * @cred: credential to use for this operation
8139 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
8140 * operation failed, or a negative errno if a local error occurred.
8142 * On success, "locations" is filled in, but if the server has
8143 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
8146 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
8147 * from this client that require migration recovery.
8149 int nfs4_proc_get_locations(struct nfs_server *server,
8150 struct nfs_fh *fhandle,
8151 struct nfs4_fs_locations *locations,
8152 struct page *page, const struct cred *cred)
8154 struct nfs_client *clp = server->nfs_client;
8155 const struct nfs4_mig_recovery_ops *ops =
8156 clp->cl_mvops->mig_recovery_ops;
8157 struct nfs4_exception exception = {
8158 .interruptible = true,
8162 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8163 (unsigned long long)server->fsid.major,
8164 (unsigned long long)server->fsid.minor,
8166 nfs_display_fhandle(fhandle, __func__);
8169 status = ops->get_locations(server, fhandle, locations, page,
8171 if (status != -NFS4ERR_DELAY)
8173 nfs4_handle_exception(server, status, &exception);
8174 } while (exception.retry);
8179 * This operation also signals the server that this client is
8180 * performing "lease moved" recovery. The server can stop
8181 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
8182 * is appended to this compound to identify the client ID which is
8183 * performing recovery.
8185 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
8187 struct nfs_server *server = NFS_SERVER(inode);
8188 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8189 struct rpc_clnt *clnt = server->client;
8190 struct nfs4_fsid_present_arg args = {
8191 .fh = NFS_FH(inode),
8192 .clientid = clp->cl_clientid,
8193 .renew = 1, /* append RENEW */
8195 struct nfs4_fsid_present_res res = {
8198 struct rpc_message msg = {
8199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8204 unsigned long now = jiffies;
8207 res.fh = nfs_alloc_fhandle();
8211 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8212 status = nfs4_call_sync_sequence(clnt, server, &msg,
8213 &args.seq_args, &res.seq_res);
8214 nfs_free_fhandle(res.fh);
8218 do_renew_lease(clp, now);
8222 #ifdef CONFIG_NFS_V4_1
8225 * This operation also signals the server that this client is
8226 * performing "lease moved" recovery. The server can stop asserting
8227 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8228 * this operation is identified in the SEQUENCE operation in this
8231 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8233 struct nfs_server *server = NFS_SERVER(inode);
8234 struct rpc_clnt *clnt = server->client;
8235 struct nfs4_fsid_present_arg args = {
8236 .fh = NFS_FH(inode),
8238 struct nfs4_fsid_present_res res = {
8240 struct rpc_message msg = {
8241 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8248 res.fh = nfs_alloc_fhandle();
8252 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8253 status = nfs4_call_sync_sequence(clnt, server, &msg,
8254 &args.seq_args, &res.seq_res);
8255 nfs_free_fhandle(res.fh);
8256 if (status == NFS4_OK &&
8257 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8258 status = -NFS4ERR_LEASE_MOVED;
8262 #endif /* CONFIG_NFS_V4_1 */
8265 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8266 * @inode: inode on FSID to check
8267 * @cred: credential to use for this operation
8269 * Server indicates whether the FSID is present, moved, or not
8270 * recognized. This operation is necessary to clear a LEASE_MOVED
8271 * condition for this client ID.
8273 * Returns NFS4_OK if the FSID is present on this server,
8274 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8275 * NFS4ERR code if some error occurred on the server, or a
8276 * negative errno if a local failure occurred.
8278 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8280 struct nfs_server *server = NFS_SERVER(inode);
8281 struct nfs_client *clp = server->nfs_client;
8282 const struct nfs4_mig_recovery_ops *ops =
8283 clp->cl_mvops->mig_recovery_ops;
8284 struct nfs4_exception exception = {
8285 .interruptible = true,
8289 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8290 (unsigned long long)server->fsid.major,
8291 (unsigned long long)server->fsid.minor,
8293 nfs_display_fhandle(NFS_FH(inode), __func__);
8296 status = ops->fsid_present(inode, cred);
8297 if (status != -NFS4ERR_DELAY)
8299 nfs4_handle_exception(server, status, &exception);
8300 } while (exception.retry);
8305 * If 'use_integrity' is true and the state managment nfs_client
8306 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8307 * and the machine credential as per RFC3530bis and RFC5661 Security
8308 * Considerations sections. Otherwise, just use the user cred with the
8309 * filesystem's rpc_client.
8311 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8314 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8315 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8316 struct nfs4_secinfo_arg args = {
8317 .dir_fh = NFS_FH(dir),
8320 struct nfs4_secinfo_res res = {
8323 struct rpc_message msg = {
8324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8328 struct nfs4_call_sync_data data = {
8329 .seq_server = NFS_SERVER(dir),
8330 .seq_args = &args.seq_args,
8331 .seq_res = &res.seq_res,
8333 struct rpc_task_setup task_setup = {
8335 .rpc_message = &msg,
8336 .callback_ops = clp->cl_mvops->call_sync_ops,
8337 .callback_data = &data,
8338 .flags = RPC_TASK_NO_ROUND_ROBIN,
8340 const struct cred *cred = NULL;
8342 if (use_integrity) {
8343 clnt = clp->cl_rpcclient;
8344 task_setup.rpc_client = clnt;
8346 cred = nfs4_get_clid_cred(clp);
8347 msg.rpc_cred = cred;
8350 dprintk("NFS call secinfo %s\n", name->name);
8352 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8353 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8354 status = nfs4_call_sync_custom(&task_setup);
8356 dprintk("NFS reply secinfo: %d\n", status);
8362 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8363 struct nfs4_secinfo_flavors *flavors)
8365 struct nfs4_exception exception = {
8366 .interruptible = true,
8370 err = -NFS4ERR_WRONGSEC;
8372 /* try to use integrity protection with machine cred */
8373 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8374 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8377 * if unable to use integrity protection, or SECINFO with
8378 * integrity protection returns NFS4ERR_WRONGSEC (which is
8379 * disallowed by spec, but exists in deployed servers) use
8380 * the current filesystem's rpc_client and the user cred.
8382 if (err == -NFS4ERR_WRONGSEC)
8383 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8385 trace_nfs4_secinfo(dir, name, err);
8386 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8388 } while (exception.retry);
8392 #ifdef CONFIG_NFS_V4_1
8394 * Check the exchange flags returned by the server for invalid flags, having
8395 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8398 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8400 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8402 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8404 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8405 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8407 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8411 return -NFS4ERR_INVAL;
8415 nfs41_same_server_scope(struct nfs41_server_scope *a,
8416 struct nfs41_server_scope *b)
8418 if (a->server_scope_sz != b->server_scope_sz)
8420 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8424 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8426 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8427 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8428 struct nfs_client *clp = args->client;
8430 switch (task->tk_status) {
8431 case -NFS4ERR_BADSESSION:
8432 case -NFS4ERR_DEADSESSION:
8433 nfs4_schedule_session_recovery(clp->cl_session,
8437 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8438 res->dir != NFS4_CDFS4_BOTH) {
8439 rpc_task_close_connection(task);
8440 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8441 rpc_restart_call(task);
8445 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8446 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8450 * nfs4_proc_bind_one_conn_to_session()
8452 * The 4.1 client currently uses the same TCP connection for the
8453 * fore and backchannel.
8456 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8457 struct rpc_xprt *xprt,
8458 struct nfs_client *clp,
8459 const struct cred *cred)
8462 struct nfs41_bind_conn_to_session_args args = {
8464 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8467 struct nfs41_bind_conn_to_session_res res;
8468 struct rpc_message msg = {
8470 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8475 struct rpc_task_setup task_setup_data = {
8478 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8479 .rpc_message = &msg,
8480 .flags = RPC_TASK_TIMEOUT,
8482 struct rpc_task *task;
8484 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8485 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8486 args.dir = NFS4_CDFC4_FORE;
8488 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8489 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8490 args.dir = NFS4_CDFC4_FORE;
8492 task = rpc_run_task(&task_setup_data);
8493 if (!IS_ERR(task)) {
8494 status = task->tk_status;
8497 status = PTR_ERR(task);
8498 trace_nfs4_bind_conn_to_session(clp, status);
8500 if (memcmp(res.sessionid.data,
8501 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8502 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8505 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8506 dprintk("NFS: %s: Unexpected direction from server\n",
8510 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8511 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8520 struct rpc_bind_conn_calldata {
8521 struct nfs_client *clp;
8522 const struct cred *cred;
8526 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8527 struct rpc_xprt *xprt,
8530 struct rpc_bind_conn_calldata *p = calldata;
8532 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8535 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8537 struct rpc_bind_conn_calldata data = {
8541 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8542 nfs4_proc_bind_conn_to_session_callback, &data);
8546 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8547 * and operations we'd like to see to enable certain features in the allow map
8549 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8550 .how = SP4_MACH_CRED,
8551 .enforce.u.words = {
8552 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8553 1 << (OP_EXCHANGE_ID - 32) |
8554 1 << (OP_CREATE_SESSION - 32) |
8555 1 << (OP_DESTROY_SESSION - 32) |
8556 1 << (OP_DESTROY_CLIENTID - 32)
8559 [0] = 1 << (OP_CLOSE) |
8560 1 << (OP_OPEN_DOWNGRADE) |
8562 1 << (OP_DELEGRETURN) |
8564 [1] = 1 << (OP_SECINFO - 32) |
8565 1 << (OP_SECINFO_NO_NAME - 32) |
8566 1 << (OP_LAYOUTRETURN - 32) |
8567 1 << (OP_TEST_STATEID - 32) |
8568 1 << (OP_FREE_STATEID - 32) |
8569 1 << (OP_WRITE - 32)
8574 * Select the state protection mode for client `clp' given the server results
8575 * from exchange_id in `sp'.
8577 * Returns 0 on success, negative errno otherwise.
8579 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8580 struct nfs41_state_protection *sp)
8582 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8583 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8584 1 << (OP_EXCHANGE_ID - 32) |
8585 1 << (OP_CREATE_SESSION - 32) |
8586 1 << (OP_DESTROY_SESSION - 32) |
8587 1 << (OP_DESTROY_CLIENTID - 32)
8589 unsigned long flags = 0;
8593 if (sp->how == SP4_MACH_CRED) {
8594 /* Print state protect result */
8595 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8596 for (i = 0; i <= LAST_NFS4_OP; i++) {
8597 if (test_bit(i, sp->enforce.u.longs))
8598 dfprintk(MOUNT, " enforce op %d\n", i);
8599 if (test_bit(i, sp->allow.u.longs))
8600 dfprintk(MOUNT, " allow op %d\n", i);
8603 /* make sure nothing is on enforce list that isn't supported */
8604 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8605 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8606 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8613 * Minimal mode - state operations are allowed to use machine
8614 * credential. Note this already happens by default, so the
8615 * client doesn't have to do anything more than the negotiation.
8617 * NOTE: we don't care if EXCHANGE_ID is in the list -
8618 * we're already using the machine cred for exchange_id
8619 * and will never use a different cred.
8621 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8622 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8623 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8624 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8625 dfprintk(MOUNT, "sp4_mach_cred:\n");
8626 dfprintk(MOUNT, " minimal mode enabled\n");
8627 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8629 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8634 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8635 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8636 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8637 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8638 dfprintk(MOUNT, " cleanup mode enabled\n");
8639 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8642 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8643 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8644 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8647 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8648 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8649 dfprintk(MOUNT, " secinfo mode enabled\n");
8650 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8653 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8654 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8655 dfprintk(MOUNT, " stateid mode enabled\n");
8656 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8659 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8660 dfprintk(MOUNT, " write mode enabled\n");
8661 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8664 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8665 dfprintk(MOUNT, " commit mode enabled\n");
8666 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8670 clp->cl_sp4_flags = flags;
8674 struct nfs41_exchange_id_data {
8675 struct nfs41_exchange_id_res res;
8676 struct nfs41_exchange_id_args args;
8679 static void nfs4_exchange_id_release(void *data)
8681 struct nfs41_exchange_id_data *cdata =
8682 (struct nfs41_exchange_id_data *)data;
8684 nfs_put_client(cdata->args.client);
8685 kfree(cdata->res.impl_id);
8686 kfree(cdata->res.server_scope);
8687 kfree(cdata->res.server_owner);
8691 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8692 .rpc_release = nfs4_exchange_id_release,
8696 * _nfs4_proc_exchange_id()
8698 * Wrapper for EXCHANGE_ID operation.
8700 static struct rpc_task *
8701 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8702 u32 sp4_how, struct rpc_xprt *xprt)
8704 struct rpc_message msg = {
8705 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8708 struct rpc_task_setup task_setup_data = {
8709 .rpc_client = clp->cl_rpcclient,
8710 .callback_ops = &nfs4_exchange_id_call_ops,
8711 .rpc_message = &msg,
8712 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8714 struct nfs41_exchange_id_data *calldata;
8717 if (!refcount_inc_not_zero(&clp->cl_count))
8718 return ERR_PTR(-EIO);
8721 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8725 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8727 status = nfs4_init_uniform_client_string(clp);
8731 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8734 if (unlikely(calldata->res.server_owner == NULL))
8737 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8739 if (unlikely(calldata->res.server_scope == NULL))
8740 goto out_server_owner;
8742 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8743 if (unlikely(calldata->res.impl_id == NULL))
8744 goto out_server_scope;
8748 calldata->args.state_protect.how = SP4_NONE;
8752 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8762 task_setup_data.rpc_xprt = xprt;
8763 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8764 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8765 sizeof(calldata->args.verifier.data));
8767 calldata->args.client = clp;
8768 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8769 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8770 #ifdef CONFIG_NFS_V4_1_MIGRATION
8771 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8773 msg.rpc_argp = &calldata->args;
8774 msg.rpc_resp = &calldata->res;
8775 task_setup_data.callback_data = calldata;
8777 return rpc_run_task(&task_setup_data);
8780 kfree(calldata->res.impl_id);
8782 kfree(calldata->res.server_scope);
8784 kfree(calldata->res.server_owner);
8788 nfs_put_client(clp);
8789 return ERR_PTR(status);
8793 * _nfs4_proc_exchange_id()
8795 * Wrapper for EXCHANGE_ID operation.
8797 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8800 struct rpc_task *task;
8801 struct nfs41_exchange_id_args *argp;
8802 struct nfs41_exchange_id_res *resp;
8803 unsigned long now = jiffies;
8806 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8808 return PTR_ERR(task);
8810 argp = task->tk_msg.rpc_argp;
8811 resp = task->tk_msg.rpc_resp;
8812 status = task->tk_status;
8816 status = nfs4_check_cl_exchange_flags(resp->flags,
8817 clp->cl_mvops->minor_version);
8821 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8825 do_renew_lease(clp, now);
8827 clp->cl_clientid = resp->clientid;
8828 clp->cl_exchange_flags = resp->flags;
8829 clp->cl_seqid = resp->seqid;
8830 /* Client ID is not confirmed */
8831 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8832 clear_bit(NFS4_SESSION_ESTABLISHED,
8833 &clp->cl_session->session_state);
8835 if (clp->cl_serverscope != NULL &&
8836 !nfs41_same_server_scope(clp->cl_serverscope,
8837 resp->server_scope)) {
8838 dprintk("%s: server_scope mismatch detected\n",
8840 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8843 swap(clp->cl_serverowner, resp->server_owner);
8844 swap(clp->cl_serverscope, resp->server_scope);
8845 swap(clp->cl_implid, resp->impl_id);
8847 /* Save the EXCHANGE_ID verifier session trunk tests */
8848 memcpy(clp->cl_confirm.data, argp->verifier.data,
8849 sizeof(clp->cl_confirm.data));
8851 trace_nfs4_exchange_id(clp, status);
8857 * nfs4_proc_exchange_id()
8859 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8861 * Since the clientid has expired, all compounds using sessions
8862 * associated with the stale clientid will be returning
8863 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8864 * be in some phase of session reset.
8866 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8868 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8870 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8873 /* try SP4_MACH_CRED if krb5i/p */
8874 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8875 authflavor == RPC_AUTH_GSS_KRB5P) {
8876 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8882 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8886 * nfs4_test_session_trunk
8888 * This is an add_xprt_test() test function called from
8889 * rpc_clnt_setup_test_and_add_xprt.
8891 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8892 * and is dereferrenced in nfs4_exchange_id_release
8894 * Upon success, add the new transport to the rpc_clnt
8896 * @clnt: struct rpc_clnt to get new transport
8897 * @xprt: the rpc_xprt to test
8898 * @data: call data for _nfs4_proc_exchange_id.
8900 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8903 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8904 struct rpc_task *task;
8909 dprintk("--> %s try %s\n", __func__,
8910 xprt->address_strings[RPC_DISPLAY_ADDR]);
8912 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8914 /* Test connection for session trunking. Async exchange_id call */
8915 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8919 status = task->tk_status;
8921 status = nfs4_detect_session_trunking(adata->clp,
8922 task->tk_msg.rpc_resp, xprt);
8925 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8926 else if (rpc_clnt_xprt_switch_has_addr(clnt,
8927 (struct sockaddr *)&xprt->addr))
8928 rpc_clnt_xprt_switch_remove_xprt(clnt, xprt);
8932 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8934 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8935 const struct cred *cred)
8937 struct rpc_message msg = {
8938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8944 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8945 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8946 trace_nfs4_destroy_clientid(clp, status);
8948 dprintk("NFS: Got error %d from the server %s on "
8949 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8953 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8954 const struct cred *cred)
8959 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8960 ret = _nfs4_proc_destroy_clientid(clp, cred);
8962 case -NFS4ERR_DELAY:
8963 case -NFS4ERR_CLIENTID_BUSY:
8973 int nfs4_destroy_clientid(struct nfs_client *clp)
8975 const struct cred *cred;
8978 if (clp->cl_mvops->minor_version < 1)
8980 if (clp->cl_exchange_flags == 0)
8982 if (clp->cl_preserve_clid)
8984 cred = nfs4_get_clid_cred(clp);
8985 ret = nfs4_proc_destroy_clientid(clp, cred);
8989 case -NFS4ERR_STALE_CLIENTID:
8990 clp->cl_exchange_flags = 0;
8996 #endif /* CONFIG_NFS_V4_1 */
8998 struct nfs4_get_lease_time_data {
8999 struct nfs4_get_lease_time_args *args;
9000 struct nfs4_get_lease_time_res *res;
9001 struct nfs_client *clp;
9004 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
9007 struct nfs4_get_lease_time_data *data =
9008 (struct nfs4_get_lease_time_data *)calldata;
9010 /* just setup sequence, do not trigger session recovery
9011 since we're invoked within one */
9012 nfs4_setup_sequence(data->clp,
9013 &data->args->la_seq_args,
9014 &data->res->lr_seq_res,
9019 * Called from nfs4_state_manager thread for session setup, so don't recover
9020 * from sequence operation or clientid errors.
9022 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
9024 struct nfs4_get_lease_time_data *data =
9025 (struct nfs4_get_lease_time_data *)calldata;
9027 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
9029 switch (task->tk_status) {
9030 case -NFS4ERR_DELAY:
9031 case -NFS4ERR_GRACE:
9032 rpc_delay(task, NFS4_POLL_RETRY_MIN);
9033 task->tk_status = 0;
9035 case -NFS4ERR_RETRY_UNCACHED_REP:
9036 rpc_restart_call_prepare(task);
9041 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
9042 .rpc_call_prepare = nfs4_get_lease_time_prepare,
9043 .rpc_call_done = nfs4_get_lease_time_done,
9046 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
9048 struct nfs4_get_lease_time_args args;
9049 struct nfs4_get_lease_time_res res = {
9050 .lr_fsinfo = fsinfo,
9052 struct nfs4_get_lease_time_data data = {
9057 struct rpc_message msg = {
9058 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
9062 struct rpc_task_setup task_setup = {
9063 .rpc_client = clp->cl_rpcclient,
9064 .rpc_message = &msg,
9065 .callback_ops = &nfs4_get_lease_time_ops,
9066 .callback_data = &data,
9067 .flags = RPC_TASK_TIMEOUT,
9070 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
9071 return nfs4_call_sync_custom(&task_setup);
9074 #ifdef CONFIG_NFS_V4_1
9077 * Initialize the values to be used by the client in CREATE_SESSION
9078 * If nfs4_init_session set the fore channel request and response sizes,
9081 * Set the back channel max_resp_sz_cached to zero to force the client to
9082 * always set csa_cachethis to FALSE because the current implementation
9083 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
9085 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
9086 struct rpc_clnt *clnt)
9088 unsigned int max_rqst_sz, max_resp_sz;
9089 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
9090 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
9092 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
9093 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
9095 /* Fore channel attributes */
9096 args->fc_attrs.max_rqst_sz = max_rqst_sz;
9097 args->fc_attrs.max_resp_sz = max_resp_sz;
9098 args->fc_attrs.max_ops = NFS4_MAX_OPS;
9099 args->fc_attrs.max_reqs = max_session_slots;
9101 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
9102 "max_ops=%u max_reqs=%u\n",
9104 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
9105 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
9107 /* Back channel attributes */
9108 args->bc_attrs.max_rqst_sz = max_bc_payload;
9109 args->bc_attrs.max_resp_sz = max_bc_payload;
9110 args->bc_attrs.max_resp_sz_cached = 0;
9111 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
9112 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
9113 if (args->bc_attrs.max_reqs > max_bc_slots)
9114 args->bc_attrs.max_reqs = max_bc_slots;
9116 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
9117 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
9119 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
9120 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
9121 args->bc_attrs.max_reqs);
9124 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
9125 struct nfs41_create_session_res *res)
9127 struct nfs4_channel_attrs *sent = &args->fc_attrs;
9128 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
9130 if (rcvd->max_resp_sz > sent->max_resp_sz)
9133 * Our requested max_ops is the minimum we need; we're not
9134 * prepared to break up compounds into smaller pieces than that.
9135 * So, no point even trying to continue if the server won't
9138 if (rcvd->max_ops < sent->max_ops)
9140 if (rcvd->max_reqs == 0)
9142 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
9143 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
9147 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
9148 struct nfs41_create_session_res *res)
9150 struct nfs4_channel_attrs *sent = &args->bc_attrs;
9151 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
9153 if (!(res->flags & SESSION4_BACK_CHAN))
9155 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
9157 if (rcvd->max_resp_sz < sent->max_resp_sz)
9159 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
9161 if (rcvd->max_ops > sent->max_ops)
9163 if (rcvd->max_reqs > sent->max_reqs)
9169 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
9170 struct nfs41_create_session_res *res)
9174 ret = nfs4_verify_fore_channel_attrs(args, res);
9177 return nfs4_verify_back_channel_attrs(args, res);
9180 static void nfs4_update_session(struct nfs4_session *session,
9181 struct nfs41_create_session_res *res)
9183 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
9184 /* Mark client id and session as being confirmed */
9185 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
9186 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
9187 session->flags = res->flags;
9188 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
9189 if (res->flags & SESSION4_BACK_CHAN)
9190 memcpy(&session->bc_attrs, &res->bc_attrs,
9191 sizeof(session->bc_attrs));
9194 static int _nfs4_proc_create_session(struct nfs_client *clp,
9195 const struct cred *cred)
9197 struct nfs4_session *session = clp->cl_session;
9198 struct nfs41_create_session_args args = {
9200 .clientid = clp->cl_clientid,
9201 .seqid = clp->cl_seqid,
9202 .cb_program = NFS4_CALLBACK,
9204 struct nfs41_create_session_res res;
9206 struct rpc_message msg = {
9207 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9214 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
9215 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
9217 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9218 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9219 trace_nfs4_create_session(clp, status);
9222 case -NFS4ERR_STALE_CLIENTID:
9223 case -NFS4ERR_DELAY:
9232 /* Verify the session's negotiated channel_attrs values */
9233 status = nfs4_verify_channel_attrs(&args, &res);
9234 /* Increment the clientid slot sequence id */
9237 nfs4_update_session(session, &res);
9244 * Issues a CREATE_SESSION operation to the server.
9245 * It is the responsibility of the caller to verify the session is
9246 * expired before calling this routine.
9248 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9252 struct nfs4_session *session = clp->cl_session;
9253 struct nfs4_add_xprt_data xprtdata = {
9256 struct rpc_add_xprt_test rpcdata = {
9257 .add_xprt_test = clp->cl_mvops->session_trunk,
9261 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9263 status = _nfs4_proc_create_session(clp, cred);
9267 /* Init or reset the session slot tables */
9268 status = nfs4_setup_session_slot_tables(session);
9269 dprintk("slot table setup returned %d\n", status);
9273 ptr = (unsigned *)&session->sess_id.data[0];
9274 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9275 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9276 rpc_clnt_probe_trunked_xprts(clp->cl_rpcclient, &rpcdata);
9282 * Issue the over-the-wire RPC DESTROY_SESSION.
9283 * The caller must serialize access to this routine.
9285 int nfs4_proc_destroy_session(struct nfs4_session *session,
9286 const struct cred *cred)
9288 struct rpc_message msg = {
9289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9290 .rpc_argp = session,
9295 /* session is still being setup */
9296 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9299 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9300 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9301 trace_nfs4_destroy_session(session->clp, status);
9304 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9305 "Session has been destroyed regardless...\n", status);
9306 rpc_clnt_manage_trunked_xprts(session->clp->cl_rpcclient);
9311 * Renew the cl_session lease.
9313 struct nfs4_sequence_data {
9314 struct nfs_client *clp;
9315 struct nfs4_sequence_args args;
9316 struct nfs4_sequence_res res;
9319 static void nfs41_sequence_release(void *data)
9321 struct nfs4_sequence_data *calldata = data;
9322 struct nfs_client *clp = calldata->clp;
9324 if (refcount_read(&clp->cl_count) > 1)
9325 nfs4_schedule_state_renewal(clp);
9326 nfs_put_client(clp);
9330 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9332 switch(task->tk_status) {
9333 case -NFS4ERR_DELAY:
9334 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9337 nfs4_schedule_lease_recovery(clp);
9342 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9344 struct nfs4_sequence_data *calldata = data;
9345 struct nfs_client *clp = calldata->clp;
9347 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9350 trace_nfs4_sequence(clp, task->tk_status);
9351 if (task->tk_status < 0) {
9352 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9353 if (refcount_read(&clp->cl_count) == 1)
9356 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9357 rpc_restart_call_prepare(task);
9361 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9364 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9366 struct nfs4_sequence_data *calldata = data;
9367 struct nfs_client *clp = calldata->clp;
9368 struct nfs4_sequence_args *args;
9369 struct nfs4_sequence_res *res;
9371 args = task->tk_msg.rpc_argp;
9372 res = task->tk_msg.rpc_resp;
9374 nfs4_setup_sequence(clp, args, res, task);
9377 static const struct rpc_call_ops nfs41_sequence_ops = {
9378 .rpc_call_done = nfs41_sequence_call_done,
9379 .rpc_call_prepare = nfs41_sequence_prepare,
9380 .rpc_release = nfs41_sequence_release,
9383 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9384 const struct cred *cred,
9385 struct nfs4_slot *slot,
9388 struct nfs4_sequence_data *calldata;
9389 struct rpc_message msg = {
9390 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9393 struct rpc_task_setup task_setup_data = {
9394 .rpc_client = clp->cl_rpcclient,
9395 .rpc_message = &msg,
9396 .callback_ops = &nfs41_sequence_ops,
9397 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
9399 struct rpc_task *ret;
9401 ret = ERR_PTR(-EIO);
9402 if (!refcount_inc_not_zero(&clp->cl_count))
9405 ret = ERR_PTR(-ENOMEM);
9406 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
9407 if (calldata == NULL)
9409 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9410 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9411 msg.rpc_argp = &calldata->args;
9412 msg.rpc_resp = &calldata->res;
9413 calldata->clp = clp;
9414 task_setup_data.callback_data = calldata;
9416 ret = rpc_run_task(&task_setup_data);
9421 nfs_put_client(clp);
9423 nfs41_release_slot(slot);
9427 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9429 struct rpc_task *task;
9432 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9434 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9436 ret = PTR_ERR(task);
9438 rpc_put_task_async(task);
9439 dprintk("<-- %s status=%d\n", __func__, ret);
9443 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9445 struct rpc_task *task;
9448 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9450 ret = PTR_ERR(task);
9453 ret = rpc_wait_for_completion_task(task);
9455 ret = task->tk_status;
9458 dprintk("<-- %s status=%d\n", __func__, ret);
9462 struct nfs4_reclaim_complete_data {
9463 struct nfs_client *clp;
9464 struct nfs41_reclaim_complete_args arg;
9465 struct nfs41_reclaim_complete_res res;
9468 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9470 struct nfs4_reclaim_complete_data *calldata = data;
9472 nfs4_setup_sequence(calldata->clp,
9473 &calldata->arg.seq_args,
9474 &calldata->res.seq_res,
9478 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9480 switch(task->tk_status) {
9482 wake_up_all(&clp->cl_lock_waitq);
9484 case -NFS4ERR_COMPLETE_ALREADY:
9485 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9487 case -NFS4ERR_DELAY:
9488 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9490 case -NFS4ERR_RETRY_UNCACHED_REP:
9492 dprintk("%s: failed to reclaim complete error %d for server %s, retrying\n",
9493 __func__, task->tk_status, clp->cl_hostname);
9495 case -NFS4ERR_BADSESSION:
9496 case -NFS4ERR_DEADSESSION:
9497 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9500 nfs4_schedule_lease_recovery(clp);
9505 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9507 struct nfs4_reclaim_complete_data *calldata = data;
9508 struct nfs_client *clp = calldata->clp;
9509 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9511 if (!nfs41_sequence_done(task, res))
9514 trace_nfs4_reclaim_complete(clp, task->tk_status);
9515 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9516 rpc_restart_call_prepare(task);
9521 static void nfs4_free_reclaim_complete_data(void *data)
9523 struct nfs4_reclaim_complete_data *calldata = data;
9528 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9529 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9530 .rpc_call_done = nfs4_reclaim_complete_done,
9531 .rpc_release = nfs4_free_reclaim_complete_data,
9535 * Issue a global reclaim complete.
9537 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9538 const struct cred *cred)
9540 struct nfs4_reclaim_complete_data *calldata;
9541 struct rpc_message msg = {
9542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9545 struct rpc_task_setup task_setup_data = {
9546 .rpc_client = clp->cl_rpcclient,
9547 .rpc_message = &msg,
9548 .callback_ops = &nfs4_reclaim_complete_call_ops,
9549 .flags = RPC_TASK_NO_ROUND_ROBIN,
9551 int status = -ENOMEM;
9553 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9554 if (calldata == NULL)
9556 calldata->clp = clp;
9557 calldata->arg.one_fs = 0;
9559 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9560 msg.rpc_argp = &calldata->arg;
9561 msg.rpc_resp = &calldata->res;
9562 task_setup_data.callback_data = calldata;
9563 status = nfs4_call_sync_custom(&task_setup_data);
9565 dprintk("<-- %s status=%d\n", __func__, status);
9570 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9572 struct nfs4_layoutget *lgp = calldata;
9573 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9575 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9576 &lgp->res.seq_res, task);
9579 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9581 struct nfs4_layoutget *lgp = calldata;
9583 nfs41_sequence_process(task, &lgp->res.seq_res);
9587 nfs4_layoutget_handle_exception(struct rpc_task *task,
9588 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9590 struct inode *inode = lgp->args.inode;
9591 struct nfs_server *server = NFS_SERVER(inode);
9592 struct pnfs_layout_hdr *lo = lgp->lo;
9593 int nfs4err = task->tk_status;
9594 int err, status = 0;
9597 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9599 nfs4_sequence_free_slot(&lgp->res.seq_res);
9606 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9607 * on the file. set tk_status to -ENODATA to tell upper layer to
9610 case -NFS4ERR_LAYOUTUNAVAILABLE:
9614 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9615 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9617 case -NFS4ERR_BADLAYOUT:
9618 status = -EOVERFLOW;
9621 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9622 * (or clients) writing to the same RAID stripe except when
9623 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9625 * Treat it like we would RECALLCONFLICT -- we retry for a little
9626 * while, and then eventually give up.
9628 case -NFS4ERR_LAYOUTTRYLATER:
9629 if (lgp->args.minlength == 0) {
9630 status = -EOVERFLOW;
9635 case -NFS4ERR_RECALLCONFLICT:
9636 status = -ERECALLCONFLICT;
9638 case -NFS4ERR_DELEG_REVOKED:
9639 case -NFS4ERR_ADMIN_REVOKED:
9640 case -NFS4ERR_EXPIRED:
9641 case -NFS4ERR_BAD_STATEID:
9642 exception->timeout = 0;
9643 spin_lock(&inode->i_lock);
9644 /* If the open stateid was bad, then recover it. */
9645 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9646 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9647 spin_unlock(&inode->i_lock);
9648 exception->state = lgp->args.ctx->state;
9649 exception->stateid = &lgp->args.stateid;
9654 * Mark the bad layout state as invalid, then retry
9656 pnfs_mark_layout_stateid_invalid(lo, &head);
9657 spin_unlock(&inode->i_lock);
9658 nfs_commit_inode(inode, 0);
9659 pnfs_free_lseg_list(&head);
9664 err = nfs4_handle_exception(server, nfs4err, exception);
9666 if (exception->retry)
9675 size_t max_response_pages(struct nfs_server *server)
9677 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9678 return nfs_page_array_len(0, max_resp_sz);
9681 static void nfs4_layoutget_release(void *calldata)
9683 struct nfs4_layoutget *lgp = calldata;
9685 nfs4_sequence_free_slot(&lgp->res.seq_res);
9686 pnfs_layoutget_free(lgp);
9689 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9690 .rpc_call_prepare = nfs4_layoutget_prepare,
9691 .rpc_call_done = nfs4_layoutget_done,
9692 .rpc_release = nfs4_layoutget_release,
9695 struct pnfs_layout_segment *
9696 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9698 struct inode *inode = lgp->args.inode;
9699 struct nfs_server *server = NFS_SERVER(inode);
9700 struct rpc_task *task;
9701 struct rpc_message msg = {
9702 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9703 .rpc_argp = &lgp->args,
9704 .rpc_resp = &lgp->res,
9705 .rpc_cred = lgp->cred,
9707 struct rpc_task_setup task_setup_data = {
9708 .rpc_client = server->client,
9709 .rpc_message = &msg,
9710 .callback_ops = &nfs4_layoutget_call_ops,
9711 .callback_data = lgp,
9712 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
9715 struct pnfs_layout_segment *lseg = NULL;
9716 struct nfs4_exception exception = {
9718 .timeout = *timeout,
9722 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9724 task = rpc_run_task(&task_setup_data);
9726 return ERR_CAST(task);
9728 status = rpc_wait_for_completion_task(task);
9732 if (task->tk_status < 0) {
9733 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9734 *timeout = exception.timeout;
9735 } else if (lgp->res.layoutp->len == 0) {
9737 *timeout = nfs4_update_delay(&exception.timeout);
9739 lseg = pnfs_layout_process(lgp);
9741 trace_nfs4_layoutget(lgp->args.ctx,
9748 dprintk("<-- %s status=%d\n", __func__, status);
9750 return ERR_PTR(status);
9755 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9757 struct nfs4_layoutreturn *lrp = calldata;
9759 nfs4_setup_sequence(lrp->clp,
9760 &lrp->args.seq_args,
9763 if (!pnfs_layout_is_valid(lrp->args.layout))
9767 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9769 struct nfs4_layoutreturn *lrp = calldata;
9770 struct nfs_server *server;
9772 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9776 * Was there an RPC level error? Assume the call succeeded,
9777 * and that we need to release the layout
9779 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9780 lrp->res.lrs_present = 0;
9784 server = NFS_SERVER(lrp->args.inode);
9785 switch (task->tk_status) {
9786 case -NFS4ERR_OLD_STATEID:
9787 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9793 task->tk_status = 0;
9797 case -NFS4ERR_DELAY:
9798 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9804 task->tk_status = 0;
9805 nfs4_sequence_free_slot(&lrp->res.seq_res);
9806 rpc_restart_call_prepare(task);
9809 static void nfs4_layoutreturn_release(void *calldata)
9811 struct nfs4_layoutreturn *lrp = calldata;
9812 struct pnfs_layout_hdr *lo = lrp->args.layout;
9814 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9815 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9816 nfs4_sequence_free_slot(&lrp->res.seq_res);
9817 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9818 lrp->ld_private.ops->free(&lrp->ld_private);
9819 pnfs_put_layout_hdr(lrp->args.layout);
9820 nfs_iput_and_deactive(lrp->inode);
9821 put_cred(lrp->cred);
9825 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9826 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9827 .rpc_call_done = nfs4_layoutreturn_done,
9828 .rpc_release = nfs4_layoutreturn_release,
9831 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9833 struct rpc_task *task;
9834 struct rpc_message msg = {
9835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9836 .rpc_argp = &lrp->args,
9837 .rpc_resp = &lrp->res,
9838 .rpc_cred = lrp->cred,
9840 struct rpc_task_setup task_setup_data = {
9841 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9842 .rpc_message = &msg,
9843 .callback_ops = &nfs4_layoutreturn_call_ops,
9844 .callback_data = lrp,
9845 .flags = RPC_TASK_MOVEABLE,
9849 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9850 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9851 &task_setup_data.rpc_client, &msg);
9853 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9856 nfs4_layoutreturn_release(lrp);
9859 task_setup_data.flags |= RPC_TASK_ASYNC;
9862 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9865 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9867 task = rpc_run_task(&task_setup_data);
9869 return PTR_ERR(task);
9871 status = task->tk_status;
9872 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9873 dprintk("<-- %s status=%d\n", __func__, status);
9879 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9880 struct pnfs_device *pdev,
9881 const struct cred *cred)
9883 struct nfs4_getdeviceinfo_args args = {
9885 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9886 NOTIFY_DEVICEID4_DELETE,
9888 struct nfs4_getdeviceinfo_res res = {
9891 struct rpc_message msg = {
9892 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9899 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9900 if (res.notification & ~args.notify_types)
9901 dprintk("%s: unsupported notification\n", __func__);
9902 if (res.notification != args.notify_types)
9905 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9907 dprintk("<-- %s status=%d\n", __func__, status);
9912 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9913 struct pnfs_device *pdev,
9914 const struct cred *cred)
9916 struct nfs4_exception exception = { };
9920 err = nfs4_handle_exception(server,
9921 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9923 } while (exception.retry);
9926 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9928 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9930 struct nfs4_layoutcommit_data *data = calldata;
9931 struct nfs_server *server = NFS_SERVER(data->args.inode);
9933 nfs4_setup_sequence(server->nfs_client,
9934 &data->args.seq_args,
9940 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9942 struct nfs4_layoutcommit_data *data = calldata;
9943 struct nfs_server *server = NFS_SERVER(data->args.inode);
9945 if (!nfs41_sequence_done(task, &data->res.seq_res))
9948 switch (task->tk_status) { /* Just ignore these failures */
9949 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9950 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9951 case -NFS4ERR_BADLAYOUT: /* no layout */
9952 case -NFS4ERR_GRACE: /* loca_recalim always false */
9953 task->tk_status = 0;
9958 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9959 rpc_restart_call_prepare(task);
9965 static void nfs4_layoutcommit_release(void *calldata)
9967 struct nfs4_layoutcommit_data *data = calldata;
9969 pnfs_cleanup_layoutcommit(data);
9970 nfs_post_op_update_inode_force_wcc(data->args.inode,
9972 put_cred(data->cred);
9973 nfs_iput_and_deactive(data->inode);
9977 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9978 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9979 .rpc_call_done = nfs4_layoutcommit_done,
9980 .rpc_release = nfs4_layoutcommit_release,
9984 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9986 struct rpc_message msg = {
9987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9988 .rpc_argp = &data->args,
9989 .rpc_resp = &data->res,
9990 .rpc_cred = data->cred,
9992 struct rpc_task_setup task_setup_data = {
9993 .task = &data->task,
9994 .rpc_client = NFS_CLIENT(data->args.inode),
9995 .rpc_message = &msg,
9996 .callback_ops = &nfs4_layoutcommit_ops,
9997 .callback_data = data,
9998 .flags = RPC_TASK_MOVEABLE,
10000 struct rpc_task *task;
10003 dprintk("NFS: initiating layoutcommit call. sync %d "
10004 "lbw: %llu inode %lu\n", sync,
10005 data->args.lastbytewritten,
10006 data->args.inode->i_ino);
10009 data->inode = nfs_igrab_and_active(data->args.inode);
10010 if (data->inode == NULL) {
10011 nfs4_layoutcommit_release(data);
10014 task_setup_data.flags = RPC_TASK_ASYNC;
10016 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
10017 task = rpc_run_task(&task_setup_data);
10019 return PTR_ERR(task);
10021 status = task->tk_status;
10022 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
10023 dprintk("%s: status %d\n", __func__, status);
10024 rpc_put_task(task);
10029 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
10030 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
10033 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10034 struct nfs_fsinfo *info,
10035 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
10037 struct nfs41_secinfo_no_name_args args = {
10038 .style = SECINFO_STYLE_CURRENT_FH,
10040 struct nfs4_secinfo_res res = {
10041 .flavors = flavors,
10043 struct rpc_message msg = {
10044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
10048 struct nfs4_call_sync_data data = {
10049 .seq_server = server,
10050 .seq_args = &args.seq_args,
10051 .seq_res = &res.seq_res,
10053 struct rpc_task_setup task_setup = {
10054 .rpc_client = server->client,
10055 .rpc_message = &msg,
10056 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
10057 .callback_data = &data,
10058 .flags = RPC_TASK_NO_ROUND_ROBIN,
10060 const struct cred *cred = NULL;
10063 if (use_integrity) {
10064 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
10066 cred = nfs4_get_clid_cred(server->nfs_client);
10067 msg.rpc_cred = cred;
10070 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
10071 status = nfs4_call_sync_custom(&task_setup);
10072 dprintk("<-- %s status=%d\n", __func__, status);
10080 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10081 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
10083 struct nfs4_exception exception = {
10084 .interruptible = true,
10088 /* first try using integrity protection */
10089 err = -NFS4ERR_WRONGSEC;
10091 /* try to use integrity protection with machine cred */
10092 if (_nfs4_is_integrity_protected(server->nfs_client))
10093 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10097 * if unable to use integrity protection, or SECINFO with
10098 * integrity protection returns NFS4ERR_WRONGSEC (which is
10099 * disallowed by spec, but exists in deployed servers) use
10100 * the current filesystem's rpc_client and the user cred.
10102 if (err == -NFS4ERR_WRONGSEC)
10103 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10108 case -NFS4ERR_WRONGSEC:
10112 err = nfs4_handle_exception(server, err, &exception);
10114 } while (exception.retry);
10120 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
10121 struct nfs_fsinfo *info)
10125 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
10126 struct nfs4_secinfo_flavors *flavors;
10127 struct nfs4_secinfo4 *secinfo;
10130 page = alloc_page(GFP_KERNEL);
10136 flavors = page_address(page);
10137 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
10140 * Fall back on "guess and check" method if
10141 * the server doesn't support SECINFO_NO_NAME
10143 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
10144 err = nfs4_find_root_sec(server, fhandle, info);
10150 for (i = 0; i < flavors->num_flavors; i++) {
10151 secinfo = &flavors->flavors[i];
10153 switch (secinfo->flavor) {
10154 case RPC_AUTH_NULL:
10155 case RPC_AUTH_UNIX:
10157 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
10158 &secinfo->flavor_info);
10161 flavor = RPC_AUTH_MAXFLAVOR;
10165 if (!nfs_auth_info_match(&server->auth_info, flavor))
10166 flavor = RPC_AUTH_MAXFLAVOR;
10168 if (flavor != RPC_AUTH_MAXFLAVOR) {
10169 err = nfs4_lookup_root_sec(server, fhandle,
10176 if (flavor == RPC_AUTH_MAXFLAVOR)
10181 if (err == -EACCES)
10187 static int _nfs41_test_stateid(struct nfs_server *server,
10188 nfs4_stateid *stateid,
10189 const struct cred *cred)
10192 struct nfs41_test_stateid_args args = {
10193 .stateid = stateid,
10195 struct nfs41_test_stateid_res res;
10196 struct rpc_message msg = {
10197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10202 struct rpc_clnt *rpc_client = server->client;
10204 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10205 &rpc_client, &msg);
10207 dprintk("NFS call test_stateid %p\n", stateid);
10208 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
10209 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
10210 &args.seq_args, &res.seq_res);
10211 if (status != NFS_OK) {
10212 dprintk("NFS reply test_stateid: failed, %d\n", status);
10215 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10216 return -res.status;
10219 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10220 int err, struct nfs4_exception *exception)
10222 exception->retry = 0;
10224 case -NFS4ERR_DELAY:
10225 case -NFS4ERR_RETRY_UNCACHED_REP:
10226 nfs4_handle_exception(server, err, exception);
10228 case -NFS4ERR_BADSESSION:
10229 case -NFS4ERR_BADSLOT:
10230 case -NFS4ERR_BAD_HIGH_SLOT:
10231 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10232 case -NFS4ERR_DEADSESSION:
10233 nfs4_do_handle_exception(server, err, exception);
10238 * nfs41_test_stateid - perform a TEST_STATEID operation
10240 * @server: server / transport on which to perform the operation
10241 * @stateid: state ID to test
10242 * @cred: credential
10244 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10245 * Otherwise a negative NFS4ERR value is returned if the operation
10246 * failed or the state ID is not currently valid.
10248 static int nfs41_test_stateid(struct nfs_server *server,
10249 nfs4_stateid *stateid,
10250 const struct cred *cred)
10252 struct nfs4_exception exception = {
10253 .interruptible = true,
10257 err = _nfs41_test_stateid(server, stateid, cred);
10258 nfs4_handle_delay_or_session_error(server, err, &exception);
10259 } while (exception.retry);
10263 struct nfs_free_stateid_data {
10264 struct nfs_server *server;
10265 struct nfs41_free_stateid_args args;
10266 struct nfs41_free_stateid_res res;
10269 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10271 struct nfs_free_stateid_data *data = calldata;
10272 nfs4_setup_sequence(data->server->nfs_client,
10273 &data->args.seq_args,
10274 &data->res.seq_res,
10278 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10280 struct nfs_free_stateid_data *data = calldata;
10282 nfs41_sequence_done(task, &data->res.seq_res);
10284 switch (task->tk_status) {
10285 case -NFS4ERR_DELAY:
10286 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10287 rpc_restart_call_prepare(task);
10291 static void nfs41_free_stateid_release(void *calldata)
10293 struct nfs_free_stateid_data *data = calldata;
10294 struct nfs_client *clp = data->server->nfs_client;
10296 nfs_put_client(clp);
10300 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10301 .rpc_call_prepare = nfs41_free_stateid_prepare,
10302 .rpc_call_done = nfs41_free_stateid_done,
10303 .rpc_release = nfs41_free_stateid_release,
10307 * nfs41_free_stateid - perform a FREE_STATEID operation
10309 * @server: server / transport on which to perform the operation
10310 * @stateid: state ID to release
10311 * @cred: credential
10312 * @privileged: set to true if this call needs to be privileged
10314 * Note: this function is always asynchronous.
10316 static int nfs41_free_stateid(struct nfs_server *server,
10317 const nfs4_stateid *stateid,
10318 const struct cred *cred,
10321 struct rpc_message msg = {
10322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10325 struct rpc_task_setup task_setup = {
10326 .rpc_client = server->client,
10327 .rpc_message = &msg,
10328 .callback_ops = &nfs41_free_stateid_ops,
10329 .flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
10331 struct nfs_free_stateid_data *data;
10332 struct rpc_task *task;
10333 struct nfs_client *clp = server->nfs_client;
10335 if (!refcount_inc_not_zero(&clp->cl_count))
10338 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10339 &task_setup.rpc_client, &msg);
10341 dprintk("NFS call free_stateid %p\n", stateid);
10342 data = kmalloc(sizeof(*data), GFP_KERNEL);
10345 data->server = server;
10346 nfs4_stateid_copy(&data->args.stateid, stateid);
10348 task_setup.callback_data = data;
10350 msg.rpc_argp = &data->args;
10351 msg.rpc_resp = &data->res;
10352 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10353 task = rpc_run_task(&task_setup);
10355 return PTR_ERR(task);
10356 rpc_put_task(task);
10361 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10363 const struct cred *cred = lsp->ls_state->owner->so_cred;
10365 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10366 nfs4_free_lock_state(server, lsp);
10369 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10370 const nfs4_stateid *s2)
10372 if (s1->type != s2->type)
10375 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10378 if (s1->seqid == s2->seqid)
10381 return s1->seqid == 0 || s2->seqid == 0;
10384 #endif /* CONFIG_NFS_V4_1 */
10386 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10387 const nfs4_stateid *s2)
10389 return nfs4_stateid_match(s1, s2);
10393 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10394 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10395 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10396 .recover_open = nfs4_open_reclaim,
10397 .recover_lock = nfs4_lock_reclaim,
10398 .establish_clid = nfs4_init_clientid,
10399 .detect_trunking = nfs40_discover_server_trunking,
10402 #if defined(CONFIG_NFS_V4_1)
10403 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10404 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10405 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10406 .recover_open = nfs4_open_reclaim,
10407 .recover_lock = nfs4_lock_reclaim,
10408 .establish_clid = nfs41_init_clientid,
10409 .reclaim_complete = nfs41_proc_reclaim_complete,
10410 .detect_trunking = nfs41_discover_server_trunking,
10412 #endif /* CONFIG_NFS_V4_1 */
10414 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10415 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10416 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10417 .recover_open = nfs40_open_expired,
10418 .recover_lock = nfs4_lock_expired,
10419 .establish_clid = nfs4_init_clientid,
10422 #if defined(CONFIG_NFS_V4_1)
10423 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10424 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10425 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10426 .recover_open = nfs41_open_expired,
10427 .recover_lock = nfs41_lock_expired,
10428 .establish_clid = nfs41_init_clientid,
10430 #endif /* CONFIG_NFS_V4_1 */
10432 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10433 .sched_state_renewal = nfs4_proc_async_renew,
10434 .get_state_renewal_cred = nfs4_get_renew_cred,
10435 .renew_lease = nfs4_proc_renew,
10438 #if defined(CONFIG_NFS_V4_1)
10439 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10440 .sched_state_renewal = nfs41_proc_async_sequence,
10441 .get_state_renewal_cred = nfs4_get_machine_cred,
10442 .renew_lease = nfs4_proc_sequence,
10446 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10447 .get_locations = _nfs40_proc_get_locations,
10448 .fsid_present = _nfs40_proc_fsid_present,
10451 #if defined(CONFIG_NFS_V4_1)
10452 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10453 .get_locations = _nfs41_proc_get_locations,
10454 .fsid_present = _nfs41_proc_fsid_present,
10456 #endif /* CONFIG_NFS_V4_1 */
10458 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10459 .minor_version = 0,
10460 .init_caps = NFS_CAP_READDIRPLUS
10461 | NFS_CAP_ATOMIC_OPEN
10462 | NFS_CAP_POSIX_LOCK,
10463 .init_client = nfs40_init_client,
10464 .shutdown_client = nfs40_shutdown_client,
10465 .match_stateid = nfs4_match_stateid,
10466 .find_root_sec = nfs4_find_root_sec,
10467 .free_lock_state = nfs4_release_lockowner,
10468 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10469 .alloc_seqid = nfs_alloc_seqid,
10470 .call_sync_ops = &nfs40_call_sync_ops,
10471 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10472 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10473 .state_renewal_ops = &nfs40_state_renewal_ops,
10474 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10477 #if defined(CONFIG_NFS_V4_1)
10478 static struct nfs_seqid *
10479 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10484 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10485 .minor_version = 1,
10486 .init_caps = NFS_CAP_READDIRPLUS
10487 | NFS_CAP_ATOMIC_OPEN
10488 | NFS_CAP_POSIX_LOCK
10489 | NFS_CAP_STATEID_NFSV41
10490 | NFS_CAP_ATOMIC_OPEN_V1
10492 | NFS_CAP_MOVEABLE,
10493 .init_client = nfs41_init_client,
10494 .shutdown_client = nfs41_shutdown_client,
10495 .match_stateid = nfs41_match_stateid,
10496 .find_root_sec = nfs41_find_root_sec,
10497 .free_lock_state = nfs41_free_lock_state,
10498 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10499 .alloc_seqid = nfs_alloc_no_seqid,
10500 .session_trunk = nfs4_test_session_trunk,
10501 .call_sync_ops = &nfs41_call_sync_ops,
10502 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10503 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10504 .state_renewal_ops = &nfs41_state_renewal_ops,
10505 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10509 #if defined(CONFIG_NFS_V4_2)
10510 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10511 .minor_version = 2,
10512 .init_caps = NFS_CAP_READDIRPLUS
10513 | NFS_CAP_ATOMIC_OPEN
10514 | NFS_CAP_POSIX_LOCK
10515 | NFS_CAP_STATEID_NFSV41
10516 | NFS_CAP_ATOMIC_OPEN_V1
10520 | NFS_CAP_OFFLOAD_CANCEL
10521 | NFS_CAP_COPY_NOTIFY
10522 | NFS_CAP_DEALLOCATE
10524 | NFS_CAP_LAYOUTSTATS
10526 | NFS_CAP_LAYOUTERROR
10527 | NFS_CAP_READ_PLUS
10528 | NFS_CAP_MOVEABLE,
10529 .init_client = nfs41_init_client,
10530 .shutdown_client = nfs41_shutdown_client,
10531 .match_stateid = nfs41_match_stateid,
10532 .find_root_sec = nfs41_find_root_sec,
10533 .free_lock_state = nfs41_free_lock_state,
10534 .call_sync_ops = &nfs41_call_sync_ops,
10535 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10536 .alloc_seqid = nfs_alloc_no_seqid,
10537 .session_trunk = nfs4_test_session_trunk,
10538 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10539 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10540 .state_renewal_ops = &nfs41_state_renewal_ops,
10541 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10545 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10546 [0] = &nfs_v4_0_minor_ops,
10547 #if defined(CONFIG_NFS_V4_1)
10548 [1] = &nfs_v4_1_minor_ops,
10550 #if defined(CONFIG_NFS_V4_2)
10551 [2] = &nfs_v4_2_minor_ops,
10555 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10557 ssize_t error, error2, error3;
10559 error = generic_listxattr(dentry, list, size);
10567 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10576 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10580 return error + error2 + error3;
10583 static void nfs4_enable_swap(struct inode *inode)
10585 /* The state manager thread must always be running.
10586 * It will notice the client is a swapper, and stay put.
10588 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10590 nfs4_schedule_state_manager(clp);
10593 static void nfs4_disable_swap(struct inode *inode)
10595 /* The state manager thread will now exit once it is
10598 wake_up_var(&NFS_SERVER(inode)->nfs_client->cl_state);
10601 static const struct inode_operations nfs4_dir_inode_operations = {
10602 .create = nfs_create,
10603 .lookup = nfs_lookup,
10604 .atomic_open = nfs_atomic_open,
10606 .unlink = nfs_unlink,
10607 .symlink = nfs_symlink,
10608 .mkdir = nfs_mkdir,
10609 .rmdir = nfs_rmdir,
10610 .mknod = nfs_mknod,
10611 .rename = nfs_rename,
10612 .permission = nfs_permission,
10613 .getattr = nfs_getattr,
10614 .setattr = nfs_setattr,
10615 .listxattr = nfs4_listxattr,
10618 static const struct inode_operations nfs4_file_inode_operations = {
10619 .permission = nfs_permission,
10620 .getattr = nfs_getattr,
10621 .setattr = nfs_setattr,
10622 .listxattr = nfs4_listxattr,
10625 const struct nfs_rpc_ops nfs_v4_clientops = {
10626 .version = 4, /* protocol version */
10627 .dentry_ops = &nfs4_dentry_operations,
10628 .dir_inode_ops = &nfs4_dir_inode_operations,
10629 .file_inode_ops = &nfs4_file_inode_operations,
10630 .file_ops = &nfs4_file_operations,
10631 .getroot = nfs4_proc_get_root,
10632 .submount = nfs4_submount,
10633 .try_get_tree = nfs4_try_get_tree,
10634 .getattr = nfs4_proc_getattr,
10635 .setattr = nfs4_proc_setattr,
10636 .lookup = nfs4_proc_lookup,
10637 .lookupp = nfs4_proc_lookupp,
10638 .access = nfs4_proc_access,
10639 .readlink = nfs4_proc_readlink,
10640 .create = nfs4_proc_create,
10641 .remove = nfs4_proc_remove,
10642 .unlink_setup = nfs4_proc_unlink_setup,
10643 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10644 .unlink_done = nfs4_proc_unlink_done,
10645 .rename_setup = nfs4_proc_rename_setup,
10646 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10647 .rename_done = nfs4_proc_rename_done,
10648 .link = nfs4_proc_link,
10649 .symlink = nfs4_proc_symlink,
10650 .mkdir = nfs4_proc_mkdir,
10651 .rmdir = nfs4_proc_rmdir,
10652 .readdir = nfs4_proc_readdir,
10653 .mknod = nfs4_proc_mknod,
10654 .statfs = nfs4_proc_statfs,
10655 .fsinfo = nfs4_proc_fsinfo,
10656 .pathconf = nfs4_proc_pathconf,
10657 .set_capabilities = nfs4_server_capabilities,
10658 .decode_dirent = nfs4_decode_dirent,
10659 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10660 .read_setup = nfs4_proc_read_setup,
10661 .read_done = nfs4_read_done,
10662 .write_setup = nfs4_proc_write_setup,
10663 .write_done = nfs4_write_done,
10664 .commit_setup = nfs4_proc_commit_setup,
10665 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10666 .commit_done = nfs4_commit_done,
10667 .lock = nfs4_proc_lock,
10668 .clear_acl_cache = nfs4_zap_acl_attr,
10669 .close_context = nfs4_close_context,
10670 .open_context = nfs4_atomic_open,
10671 .have_delegation = nfs4_have_delegation,
10672 .alloc_client = nfs4_alloc_client,
10673 .init_client = nfs4_init_client,
10674 .free_client = nfs4_free_client,
10675 .create_server = nfs4_create_server,
10676 .clone_server = nfs_clone_server,
10677 .discover_trunking = nfs4_discover_trunking,
10678 .enable_swap = nfs4_enable_swap,
10679 .disable_swap = nfs4_disable_swap,
10682 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10683 .name = XATTR_NAME_NFSV4_ACL,
10684 .list = nfs4_xattr_list_nfs4_acl,
10685 .get = nfs4_xattr_get_nfs4_acl,
10686 .set = nfs4_xattr_set_nfs4_acl,
10689 #if defined(CONFIG_NFS_V4_1)
10690 static const struct xattr_handler nfs4_xattr_nfs4_dacl_handler = {
10691 .name = XATTR_NAME_NFSV4_DACL,
10692 .list = nfs4_xattr_list_nfs4_dacl,
10693 .get = nfs4_xattr_get_nfs4_dacl,
10694 .set = nfs4_xattr_set_nfs4_dacl,
10697 static const struct xattr_handler nfs4_xattr_nfs4_sacl_handler = {
10698 .name = XATTR_NAME_NFSV4_SACL,
10699 .list = nfs4_xattr_list_nfs4_sacl,
10700 .get = nfs4_xattr_get_nfs4_sacl,
10701 .set = nfs4_xattr_set_nfs4_sacl,
10705 #ifdef CONFIG_NFS_V4_2
10706 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10707 .prefix = XATTR_USER_PREFIX,
10708 .get = nfs4_xattr_get_nfs4_user,
10709 .set = nfs4_xattr_set_nfs4_user,
10713 const struct xattr_handler *nfs4_xattr_handlers[] = {
10714 &nfs4_xattr_nfs4_acl_handler,
10715 #if defined(CONFIG_NFS_V4_1)
10716 &nfs4_xattr_nfs4_dacl_handler,
10717 &nfs4_xattr_nfs4_sacl_handler,
10719 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10720 &nfs4_xattr_nfs4_label_handler,
10722 #ifdef CONFIG_NFS_V4_2
10723 &nfs4_xattr_nfs4_user_handler,