4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
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 #ifdef CONFIG_NFS_V4_2
76 #endif /* CONFIG_NFS_V4_2 */
78 #define NFSDBG_FACILITY NFSDBG_PROC
80 #define NFS4_BITMASK_SZ 3
82 #define NFS4_POLL_RETRY_MIN (HZ/10)
83 #define NFS4_POLL_RETRY_MAX (15*HZ)
85 /* file attributes which can be mapped to nfs attributes */
86 #define NFS4_VALID_ATTRS (ATTR_MODE \
97 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
98 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
99 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
100 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
101 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
102 struct nfs_fattr *fattr, struct iattr *sattr,
103 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
104 struct nfs4_label *olabel);
105 #ifdef CONFIG_NFS_V4_1
106 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
107 const struct cred *cred,
108 struct nfs4_slot *slot,
110 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
111 const struct cred *);
112 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
113 const struct cred *, bool);
115 static void nfs4_bitmask_adjust(__u32 *bitmask, struct inode *inode,
116 struct nfs_server *server,
117 struct nfs4_label *label);
119 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
120 static inline struct nfs4_label *
121 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
122 struct iattr *sattr, struct nfs4_label *label)
129 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
132 err = security_dentry_init_security(dentry, sattr->ia_mode,
133 &dentry->d_name, (void **)&label->label, &label->len);
140 nfs4_label_release_security(struct nfs4_label *label)
143 security_release_secctx(label->label, label->len);
145 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
148 return server->attr_bitmask;
150 return server->attr_bitmask_nl;
153 static inline struct nfs4_label *
154 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
155 struct iattr *sattr, struct nfs4_label *l)
158 nfs4_label_release_security(struct nfs4_label *label)
161 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
162 { return server->attr_bitmask; }
165 /* Prevent leaks of NFSv4 errors into userland */
166 static int nfs4_map_errors(int err)
171 case -NFS4ERR_RESOURCE:
172 case -NFS4ERR_LAYOUTTRYLATER:
173 case -NFS4ERR_RECALLCONFLICT:
175 case -NFS4ERR_WRONGSEC:
176 case -NFS4ERR_WRONG_CRED:
178 case -NFS4ERR_BADOWNER:
179 case -NFS4ERR_BADNAME:
181 case -NFS4ERR_SHARE_DENIED:
183 case -NFS4ERR_MINOR_VERS_MISMATCH:
184 return -EPROTONOSUPPORT;
185 case -NFS4ERR_FILE_OPEN:
187 case -NFS4ERR_NOT_SAME:
190 dprintk("%s could not handle NFSv4 error %d\n",
198 * This is our standard bitmap for GETATTR requests.
200 const u32 nfs4_fattr_bitmap[3] = {
202 | FATTR4_WORD0_CHANGE
205 | FATTR4_WORD0_FILEID,
207 | FATTR4_WORD1_NUMLINKS
209 | FATTR4_WORD1_OWNER_GROUP
210 | FATTR4_WORD1_RAWDEV
211 | FATTR4_WORD1_SPACE_USED
212 | FATTR4_WORD1_TIME_ACCESS
213 | FATTR4_WORD1_TIME_METADATA
214 | FATTR4_WORD1_TIME_MODIFY
215 | FATTR4_WORD1_MOUNTED_ON_FILEID,
216 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
217 FATTR4_WORD2_SECURITY_LABEL
221 static const u32 nfs4_pnfs_open_bitmap[3] = {
223 | FATTR4_WORD0_CHANGE
226 | FATTR4_WORD0_FILEID,
228 | FATTR4_WORD1_NUMLINKS
230 | FATTR4_WORD1_OWNER_GROUP
231 | FATTR4_WORD1_RAWDEV
232 | FATTR4_WORD1_SPACE_USED
233 | FATTR4_WORD1_TIME_ACCESS
234 | FATTR4_WORD1_TIME_METADATA
235 | FATTR4_WORD1_TIME_MODIFY,
236 FATTR4_WORD2_MDSTHRESHOLD
237 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
238 | FATTR4_WORD2_SECURITY_LABEL
242 static const u32 nfs4_open_noattr_bitmap[3] = {
244 | FATTR4_WORD0_FILEID,
247 const u32 nfs4_statfs_bitmap[3] = {
248 FATTR4_WORD0_FILES_AVAIL
249 | FATTR4_WORD0_FILES_FREE
250 | FATTR4_WORD0_FILES_TOTAL,
251 FATTR4_WORD1_SPACE_AVAIL
252 | FATTR4_WORD1_SPACE_FREE
253 | FATTR4_WORD1_SPACE_TOTAL
256 const u32 nfs4_pathconf_bitmap[3] = {
258 | FATTR4_WORD0_MAXNAME,
262 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
263 | FATTR4_WORD0_MAXREAD
264 | FATTR4_WORD0_MAXWRITE
265 | FATTR4_WORD0_LEASE_TIME,
266 FATTR4_WORD1_TIME_DELTA
267 | FATTR4_WORD1_FS_LAYOUT_TYPES,
268 FATTR4_WORD2_LAYOUT_BLKSIZE
269 | FATTR4_WORD2_CLONE_BLKSIZE
270 | FATTR4_WORD2_XATTR_SUPPORT
273 const u32 nfs4_fs_locations_bitmap[3] = {
277 | FATTR4_WORD0_FILEID
278 | FATTR4_WORD0_FS_LOCATIONS,
280 | FATTR4_WORD1_OWNER_GROUP
281 | FATTR4_WORD1_RAWDEV
282 | FATTR4_WORD1_SPACE_USED
283 | FATTR4_WORD1_TIME_ACCESS
284 | FATTR4_WORD1_TIME_METADATA
285 | FATTR4_WORD1_TIME_MODIFY
286 | FATTR4_WORD1_MOUNTED_ON_FILEID,
289 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
292 unsigned long cache_validity;
294 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
295 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
298 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
299 if (!(cache_validity & NFS_INO_REVAL_FORCED))
300 cache_validity &= ~(NFS_INO_INVALID_CHANGE
301 | NFS_INO_INVALID_SIZE);
303 if (!(cache_validity & NFS_INO_INVALID_SIZE))
304 dst[0] &= ~FATTR4_WORD0_SIZE;
306 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
307 dst[0] &= ~FATTR4_WORD0_CHANGE;
310 static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
311 const __u32 *src, struct inode *inode)
313 nfs4_bitmap_copy_adjust(dst, src, inode);
316 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
317 struct nfs4_readdir_arg *readdir)
319 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
323 readdir->cookie = cookie;
324 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
329 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
334 * NFSv4 servers do not return entries for '.' and '..'
335 * Therefore, we fake these entries here. We let '.'
336 * have cookie 0 and '..' have cookie 1. Note that
337 * when talking to the server, we always send cookie 0
340 start = p = kmap_atomic(*readdir->pages);
343 *p++ = xdr_one; /* next */
344 *p++ = xdr_zero; /* cookie, first word */
345 *p++ = xdr_one; /* cookie, second word */
346 *p++ = xdr_one; /* entry len */
347 memcpy(p, ".\0\0\0", 4); /* entry */
349 *p++ = xdr_one; /* bitmap length */
350 *p++ = htonl(attrs); /* bitmap */
351 *p++ = htonl(12); /* attribute buffer length */
352 *p++ = htonl(NF4DIR);
353 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
356 *p++ = xdr_one; /* next */
357 *p++ = xdr_zero; /* cookie, first word */
358 *p++ = xdr_two; /* cookie, second word */
359 *p++ = xdr_two; /* entry len */
360 memcpy(p, "..\0\0", 4); /* entry */
362 *p++ = xdr_one; /* bitmap length */
363 *p++ = htonl(attrs); /* bitmap */
364 *p++ = htonl(12); /* attribute buffer length */
365 *p++ = htonl(NF4DIR);
366 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
368 readdir->pgbase = (char *)p - (char *)start;
369 readdir->count -= readdir->pgbase;
370 kunmap_atomic(start);
373 static void nfs4_test_and_free_stateid(struct nfs_server *server,
374 nfs4_stateid *stateid,
375 const struct cred *cred)
377 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
379 ops->test_and_free_expired(server, stateid, cred);
382 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
383 nfs4_stateid *stateid,
384 const struct cred *cred)
386 stateid->type = NFS4_REVOKED_STATEID_TYPE;
387 nfs4_test_and_free_stateid(server, stateid, cred);
390 static void nfs4_free_revoked_stateid(struct nfs_server *server,
391 const nfs4_stateid *stateid,
392 const struct cred *cred)
396 nfs4_stateid_copy(&tmp, stateid);
397 __nfs4_free_revoked_stateid(server, &tmp, cred);
400 static long nfs4_update_delay(long *timeout)
404 return NFS4_POLL_RETRY_MAX;
406 *timeout = NFS4_POLL_RETRY_MIN;
407 if (*timeout > NFS4_POLL_RETRY_MAX)
408 *timeout = NFS4_POLL_RETRY_MAX;
414 static int nfs4_delay_killable(long *timeout)
418 freezable_schedule_timeout_killable_unsafe(
419 nfs4_update_delay(timeout));
420 if (!__fatal_signal_pending(current))
425 static int nfs4_delay_interruptible(long *timeout)
429 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
430 if (!signal_pending(current))
432 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
435 static int nfs4_delay(long *timeout, bool interruptible)
438 return nfs4_delay_interruptible(timeout);
439 return nfs4_delay_killable(timeout);
442 static const nfs4_stateid *
443 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
447 switch (stateid->type) {
448 case NFS4_OPEN_STATEID_TYPE:
449 case NFS4_LOCK_STATEID_TYPE:
450 case NFS4_DELEGATION_STATEID_TYPE:
458 /* This is the error handling routine for processes that are allowed
461 static int nfs4_do_handle_exception(struct nfs_server *server,
462 int errorcode, struct nfs4_exception *exception)
464 struct nfs_client *clp = server->nfs_client;
465 struct nfs4_state *state = exception->state;
466 const nfs4_stateid *stateid;
467 struct inode *inode = exception->inode;
470 exception->delay = 0;
471 exception->recovering = 0;
472 exception->retry = 0;
474 stateid = nfs4_recoverable_stateid(exception->stateid);
475 if (stateid == NULL && state != NULL)
476 stateid = nfs4_recoverable_stateid(&state->stateid);
481 case -NFS4ERR_BADHANDLE:
483 if (inode != NULL && S_ISREG(inode->i_mode))
484 pnfs_destroy_layout(NFS_I(inode));
486 case -NFS4ERR_DELEG_REVOKED:
487 case -NFS4ERR_ADMIN_REVOKED:
488 case -NFS4ERR_EXPIRED:
489 case -NFS4ERR_BAD_STATEID:
490 case -NFS4ERR_PARTNER_NO_AUTH:
491 if (inode != NULL && stateid != NULL) {
492 nfs_inode_find_state_and_recover(inode,
494 goto wait_on_recovery;
497 case -NFS4ERR_OPENMODE:
501 err = nfs_async_inode_return_delegation(inode,
504 goto wait_on_recovery;
505 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
506 exception->retry = 1;
512 ret = nfs4_schedule_stateid_recovery(server, state);
515 goto wait_on_recovery;
516 case -NFS4ERR_STALE_STATEID:
517 case -NFS4ERR_STALE_CLIENTID:
518 nfs4_schedule_lease_recovery(clp);
519 goto wait_on_recovery;
521 ret = nfs4_schedule_migration_recovery(server);
524 goto wait_on_recovery;
525 case -NFS4ERR_LEASE_MOVED:
526 nfs4_schedule_lease_moved_recovery(clp);
527 goto wait_on_recovery;
528 #if defined(CONFIG_NFS_V4_1)
529 case -NFS4ERR_BADSESSION:
530 case -NFS4ERR_BADSLOT:
531 case -NFS4ERR_BAD_HIGH_SLOT:
532 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
533 case -NFS4ERR_DEADSESSION:
534 case -NFS4ERR_SEQ_FALSE_RETRY:
535 case -NFS4ERR_SEQ_MISORDERED:
536 /* Handled in nfs41_sequence_process() */
537 goto wait_on_recovery;
538 #endif /* defined(CONFIG_NFS_V4_1) */
539 case -NFS4ERR_FILE_OPEN:
540 if (exception->timeout > HZ) {
541 /* We have retried a decent amount, time to
549 nfs_inc_server_stats(server, NFSIOS_DELAY);
552 case -NFS4ERR_LAYOUTTRYLATER:
553 case -NFS4ERR_RECALLCONFLICT:
554 exception->delay = 1;
557 case -NFS4ERR_RETRY_UNCACHED_REP:
558 case -NFS4ERR_OLD_STATEID:
559 exception->retry = 1;
561 case -NFS4ERR_BADOWNER:
562 /* The following works around a Linux server bug! */
563 case -NFS4ERR_BADNAME:
564 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
565 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
566 exception->retry = 1;
567 printk(KERN_WARNING "NFS: v4 server %s "
568 "does not accept raw "
570 "Reenabling the idmapper.\n",
571 server->nfs_client->cl_hostname);
574 /* We failed to handle the error */
575 return nfs4_map_errors(ret);
577 exception->recovering = 1;
581 /* This is the error handling routine for processes that are allowed
584 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
586 struct nfs_client *clp = server->nfs_client;
589 ret = nfs4_do_handle_exception(server, errorcode, exception);
590 if (exception->delay) {
591 ret = nfs4_delay(&exception->timeout,
592 exception->interruptible);
595 if (exception->recovering) {
596 ret = nfs4_wait_clnt_recover(clp);
597 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
604 exception->retry = 1;
609 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
610 int errorcode, struct nfs4_exception *exception)
612 struct nfs_client *clp = server->nfs_client;
615 ret = nfs4_do_handle_exception(server, errorcode, exception);
616 if (exception->delay) {
617 rpc_delay(task, nfs4_update_delay(&exception->timeout));
620 if (exception->recovering) {
621 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
622 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
623 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
626 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
631 exception->retry = 1;
633 * For NFS4ERR_MOVED, the client transport will need to
634 * be recomputed after migration recovery has completed.
636 if (errorcode == -NFS4ERR_MOVED)
637 rpc_task_release_transport(task);
643 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
644 struct nfs4_state *state, long *timeout)
646 struct nfs4_exception exception = {
650 if (task->tk_status >= 0)
653 exception.timeout = *timeout;
654 task->tk_status = nfs4_async_handle_exception(task, server,
657 if (exception.delay && timeout)
658 *timeout = exception.timeout;
665 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
666 * or 'false' otherwise.
668 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
670 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
671 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
674 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
676 spin_lock(&clp->cl_lock);
677 if (time_before(clp->cl_last_renewal,timestamp))
678 clp->cl_last_renewal = timestamp;
679 spin_unlock(&clp->cl_lock);
682 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
684 struct nfs_client *clp = server->nfs_client;
686 if (!nfs4_has_session(clp))
687 do_renew_lease(clp, timestamp);
690 struct nfs4_call_sync_data {
691 const struct nfs_server *seq_server;
692 struct nfs4_sequence_args *seq_args;
693 struct nfs4_sequence_res *seq_res;
696 void nfs4_init_sequence(struct nfs4_sequence_args *args,
697 struct nfs4_sequence_res *res, int cache_reply,
700 args->sa_slot = NULL;
701 args->sa_cache_this = cache_reply;
702 args->sa_privileged = privileged;
707 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
709 struct nfs4_slot *slot = res->sr_slot;
710 struct nfs4_slot_table *tbl;
713 spin_lock(&tbl->slot_tbl_lock);
714 if (!nfs41_wake_and_assign_slot(tbl, slot))
715 nfs4_free_slot(tbl, slot);
716 spin_unlock(&tbl->slot_tbl_lock);
721 static int nfs40_sequence_done(struct rpc_task *task,
722 struct nfs4_sequence_res *res)
724 if (res->sr_slot != NULL)
725 nfs40_sequence_free_slot(res);
729 #if defined(CONFIG_NFS_V4_1)
731 static void nfs41_release_slot(struct nfs4_slot *slot)
733 struct nfs4_session *session;
734 struct nfs4_slot_table *tbl;
735 bool send_new_highest_used_slotid = false;
740 session = tbl->session;
742 /* Bump the slot sequence number */
747 spin_lock(&tbl->slot_tbl_lock);
748 /* Be nice to the server: try to ensure that the last transmitted
749 * value for highest_user_slotid <= target_highest_slotid
751 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
752 send_new_highest_used_slotid = true;
754 if (nfs41_wake_and_assign_slot(tbl, slot)) {
755 send_new_highest_used_slotid = false;
758 nfs4_free_slot(tbl, slot);
760 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
761 send_new_highest_used_slotid = false;
763 spin_unlock(&tbl->slot_tbl_lock);
764 if (send_new_highest_used_slotid)
765 nfs41_notify_server(session->clp);
766 if (waitqueue_active(&tbl->slot_waitq))
767 wake_up_all(&tbl->slot_waitq);
770 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
772 nfs41_release_slot(res->sr_slot);
776 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
779 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
780 slot->seq_nr_highest_sent = seqnr;
782 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
785 slot->seq_nr_highest_sent = seqnr;
786 slot->seq_nr_last_acked = seqnr;
789 static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
790 struct nfs4_slot *slot)
792 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
794 rpc_put_task_async(task);
797 static int nfs41_sequence_process(struct rpc_task *task,
798 struct nfs4_sequence_res *res)
800 struct nfs4_session *session;
801 struct nfs4_slot *slot = res->sr_slot;
802 struct nfs_client *clp;
808 /* don't increment the sequence number if the task wasn't sent */
809 if (!RPC_WAS_SENT(task) || slot->seq_done)
812 session = slot->table->session;
815 trace_nfs4_sequence_done(session, res);
817 status = res->sr_status;
818 if (task->tk_status == -NFS4ERR_DEADSESSION)
819 status = -NFS4ERR_DEADSESSION;
821 /* Check the SEQUENCE operation status */
824 /* Mark this sequence number as having been acked */
825 nfs4_slot_sequence_acked(slot, slot->seq_nr);
826 /* Update the slot's sequence and clientid lease timer */
828 do_renew_lease(clp, res->sr_timestamp);
829 /* Check sequence flags */
830 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
832 nfs41_update_target_slotid(slot->table, slot, res);
836 * sr_status remains 1 if an RPC level error occurred.
837 * The server may or may not have processed the sequence
840 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
844 /* The server detected a resend of the RPC call and
845 * returned NFS4ERR_DELAY as per Section 2.10.6.2
848 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
852 nfs4_slot_sequence_acked(slot, slot->seq_nr);
854 case -NFS4ERR_RETRY_UNCACHED_REP:
855 case -NFS4ERR_SEQ_FALSE_RETRY:
857 * The server thinks we tried to replay a request.
858 * Retry the call after bumping the sequence ID.
860 nfs4_slot_sequence_acked(slot, slot->seq_nr);
862 case -NFS4ERR_BADSLOT:
864 * The slot id we used was probably retired. Try again
865 * using a different slot id.
867 if (slot->slot_nr < slot->table->target_highest_slotid)
868 goto session_recover;
870 case -NFS4ERR_SEQ_MISORDERED:
871 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
873 * Were one or more calls using this slot interrupted?
874 * If the server never received the request, then our
875 * transmitted slot sequence number may be too high. However,
876 * if the server did receive the request then it might
877 * accidentally give us a reply with a mismatched operation.
878 * We can sort this out by sending a lone sequence operation
879 * to the server on the same slot.
881 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
883 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
884 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
891 * A retry might be sent while the original request is
892 * still in progress on the replier. The replier SHOULD
893 * deal with the issue by returning NFS4ERR_DELAY as the
894 * reply to SEQUENCE or CB_SEQUENCE operation, but
895 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
897 * Restart the search after a delay.
899 slot->seq_nr = slot->seq_nr_highest_sent;
901 case -NFS4ERR_BADSESSION:
902 case -NFS4ERR_DEADSESSION:
903 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
904 goto session_recover;
906 /* Just update the slot sequence no. */
910 /* The session may be reset by one of the error handlers. */
911 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
915 nfs4_schedule_session_recovery(session, status);
916 dprintk("%s ERROR: %d Reset session\n", __func__, status);
917 nfs41_sequence_free_slot(res);
922 if (rpc_restart_call_prepare(task)) {
923 nfs41_sequence_free_slot(res);
929 if (!rpc_restart_call(task))
931 rpc_delay(task, NFS4_POLL_RETRY_MAX);
935 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
937 if (!nfs41_sequence_process(task, res))
939 if (res->sr_slot != NULL)
940 nfs41_sequence_free_slot(res);
944 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
946 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
948 if (res->sr_slot == NULL)
950 if (res->sr_slot->table->session != NULL)
951 return nfs41_sequence_process(task, res);
952 return nfs40_sequence_done(task, res);
955 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
957 if (res->sr_slot != NULL) {
958 if (res->sr_slot->table->session != NULL)
959 nfs41_sequence_free_slot(res);
961 nfs40_sequence_free_slot(res);
965 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
967 if (res->sr_slot == NULL)
969 if (!res->sr_slot->table->session)
970 return nfs40_sequence_done(task, res);
971 return nfs41_sequence_done(task, res);
973 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
975 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
977 struct nfs4_call_sync_data *data = calldata;
979 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
981 nfs4_setup_sequence(data->seq_server->nfs_client,
982 data->seq_args, data->seq_res, task);
985 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
987 struct nfs4_call_sync_data *data = calldata;
989 nfs41_sequence_done(task, data->seq_res);
992 static const struct rpc_call_ops nfs41_call_sync_ops = {
993 .rpc_call_prepare = nfs41_call_sync_prepare,
994 .rpc_call_done = nfs41_call_sync_done,
997 #else /* !CONFIG_NFS_V4_1 */
999 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1001 return nfs40_sequence_done(task, res);
1004 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1006 if (res->sr_slot != NULL)
1007 nfs40_sequence_free_slot(res);
1010 int nfs4_sequence_done(struct rpc_task *task,
1011 struct nfs4_sequence_res *res)
1013 return nfs40_sequence_done(task, res);
1015 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1017 #endif /* !CONFIG_NFS_V4_1 */
1019 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1021 res->sr_timestamp = jiffies;
1022 res->sr_status_flags = 0;
1027 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1028 struct nfs4_sequence_res *res,
1029 struct nfs4_slot *slot)
1033 slot->privileged = args->sa_privileged ? 1 : 0;
1034 args->sa_slot = slot;
1036 res->sr_slot = slot;
1039 int nfs4_setup_sequence(struct nfs_client *client,
1040 struct nfs4_sequence_args *args,
1041 struct nfs4_sequence_res *res,
1042 struct rpc_task *task)
1044 struct nfs4_session *session = nfs4_get_session(client);
1045 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1046 struct nfs4_slot *slot;
1048 /* slot already allocated? */
1049 if (res->sr_slot != NULL)
1053 tbl = &session->fc_slot_table;
1055 spin_lock(&tbl->slot_tbl_lock);
1056 /* The state manager will wait until the slot table is empty */
1057 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1060 slot = nfs4_alloc_slot(tbl);
1062 if (slot == ERR_PTR(-ENOMEM))
1063 goto out_sleep_timeout;
1066 spin_unlock(&tbl->slot_tbl_lock);
1068 nfs4_sequence_attach_slot(args, res, slot);
1070 trace_nfs4_setup_sequence(session, args);
1072 nfs41_sequence_res_init(res);
1073 rpc_call_start(task);
1076 /* Try again in 1/4 second */
1077 if (args->sa_privileged)
1078 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1079 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1081 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1082 NULL, jiffies + (HZ >> 2));
1083 spin_unlock(&tbl->slot_tbl_lock);
1086 if (args->sa_privileged)
1087 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1088 RPC_PRIORITY_PRIVILEGED);
1090 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1091 spin_unlock(&tbl->slot_tbl_lock);
1094 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1096 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1098 struct nfs4_call_sync_data *data = calldata;
1099 nfs4_setup_sequence(data->seq_server->nfs_client,
1100 data->seq_args, data->seq_res, task);
1103 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1105 struct nfs4_call_sync_data *data = calldata;
1106 nfs4_sequence_done(task, data->seq_res);
1109 static const struct rpc_call_ops nfs40_call_sync_ops = {
1110 .rpc_call_prepare = nfs40_call_sync_prepare,
1111 .rpc_call_done = nfs40_call_sync_done,
1114 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1117 struct rpc_task *task;
1119 task = rpc_run_task(task_setup);
1121 return PTR_ERR(task);
1123 ret = task->tk_status;
1128 static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1129 struct nfs_server *server,
1130 struct rpc_message *msg,
1131 struct nfs4_sequence_args *args,
1132 struct nfs4_sequence_res *res,
1133 unsigned short task_flags)
1135 struct nfs_client *clp = server->nfs_client;
1136 struct nfs4_call_sync_data data = {
1137 .seq_server = server,
1141 struct rpc_task_setup task_setup = {
1144 .callback_ops = clp->cl_mvops->call_sync_ops,
1145 .callback_data = &data,
1146 .flags = task_flags,
1149 return nfs4_call_sync_custom(&task_setup);
1152 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1153 struct nfs_server *server,
1154 struct rpc_message *msg,
1155 struct nfs4_sequence_args *args,
1156 struct nfs4_sequence_res *res)
1158 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1162 int nfs4_call_sync(struct rpc_clnt *clnt,
1163 struct nfs_server *server,
1164 struct rpc_message *msg,
1165 struct nfs4_sequence_args *args,
1166 struct nfs4_sequence_res *res,
1169 nfs4_init_sequence(args, res, cache_reply, 0);
1170 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1174 nfs4_inc_nlink_locked(struct inode *inode)
1176 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1181 nfs4_dec_nlink_locked(struct inode *inode)
1183 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1188 nfs4_update_changeattr_locked(struct inode *inode,
1189 struct nfs4_change_info *cinfo,
1190 unsigned long timestamp, unsigned long cache_validity)
1192 struct nfs_inode *nfsi = NFS_I(inode);
1194 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1195 | NFS_INO_INVALID_MTIME
1198 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(inode)) {
1199 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1200 nfsi->attrtimeo_timestamp = jiffies;
1202 if (S_ISDIR(inode->i_mode)) {
1203 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
1204 nfs_force_lookup_revalidate(inode);
1206 if (!NFS_PROTO(inode)->have_delegation(inode,
1208 nfsi->cache_validity |= NFS_INO_REVAL_PAGECACHE;
1211 if (cinfo->before != inode_peek_iversion_raw(inode))
1212 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1213 NFS_INO_INVALID_ACL |
1214 NFS_INO_INVALID_XATTR;
1216 inode_set_iversion_raw(inode, cinfo->after);
1217 nfsi->read_cache_jiffies = timestamp;
1218 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1219 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1221 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1222 nfs_fscache_invalidate(inode);
1226 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1227 unsigned long timestamp, unsigned long cache_validity)
1229 spin_lock(&dir->i_lock);
1230 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1231 spin_unlock(&dir->i_lock);
1234 struct nfs4_open_createattrs {
1235 struct nfs4_label *label;
1236 struct iattr *sattr;
1237 const __u32 verf[2];
1240 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1241 int err, struct nfs4_exception *exception)
1245 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1247 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1248 exception->retry = 1;
1252 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1254 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1257 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1259 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1261 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1265 nfs4_map_atomic_open_share(struct nfs_server *server,
1266 fmode_t fmode, int openflags)
1270 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1272 res = NFS4_SHARE_ACCESS_READ;
1275 res = NFS4_SHARE_ACCESS_WRITE;
1277 case FMODE_READ|FMODE_WRITE:
1278 res = NFS4_SHARE_ACCESS_BOTH;
1280 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1282 /* Want no delegation if we're using O_DIRECT */
1283 if (openflags & O_DIRECT)
1284 res |= NFS4_SHARE_WANT_NO_DELEG;
1289 static enum open_claim_type4
1290 nfs4_map_atomic_open_claim(struct nfs_server *server,
1291 enum open_claim_type4 claim)
1293 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1298 case NFS4_OPEN_CLAIM_FH:
1299 return NFS4_OPEN_CLAIM_NULL;
1300 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1301 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1302 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1303 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1307 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1309 p->o_res.f_attr = &p->f_attr;
1310 p->o_res.f_label = p->f_label;
1311 p->o_res.seqid = p->o_arg.seqid;
1312 p->c_res.seqid = p->c_arg.seqid;
1313 p->o_res.server = p->o_arg.server;
1314 p->o_res.access_request = p->o_arg.access;
1315 nfs_fattr_init(&p->f_attr);
1316 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1319 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1320 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1321 const struct nfs4_open_createattrs *c,
1322 enum open_claim_type4 claim,
1325 struct dentry *parent = dget_parent(dentry);
1326 struct inode *dir = d_inode(parent);
1327 struct nfs_server *server = NFS_SERVER(dir);
1328 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1329 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1330 struct nfs4_opendata *p;
1332 p = kzalloc(sizeof(*p), gfp_mask);
1336 p->f_label = nfs4_label_alloc(server, gfp_mask);
1337 if (IS_ERR(p->f_label))
1340 p->a_label = nfs4_label_alloc(server, gfp_mask);
1341 if (IS_ERR(p->a_label))
1344 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1345 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1346 if (IS_ERR(p->o_arg.seqid))
1347 goto err_free_label;
1348 nfs_sb_active(dentry->d_sb);
1349 p->dentry = dget(dentry);
1352 atomic_inc(&sp->so_count);
1353 p->o_arg.open_flags = flags;
1354 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1355 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1356 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1358 if (flags & O_CREAT) {
1359 p->o_arg.umask = current_umask();
1360 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1361 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1362 p->o_arg.u.attrs = &p->attrs;
1363 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1365 memcpy(p->o_arg.u.verifier.data, c->verf,
1366 sizeof(p->o_arg.u.verifier.data));
1369 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1370 * will return permission denied for all bits until close */
1371 if (!(flags & O_EXCL)) {
1372 /* ask server to check for all possible rights as results
1374 switch (p->o_arg.claim) {
1377 case NFS4_OPEN_CLAIM_NULL:
1378 case NFS4_OPEN_CLAIM_FH:
1379 p->o_arg.access = NFS4_ACCESS_READ |
1380 NFS4_ACCESS_MODIFY |
1381 NFS4_ACCESS_EXTEND |
1382 NFS4_ACCESS_EXECUTE;
1383 #ifdef CONFIG_NFS_V4_2
1384 if (server->caps & NFS_CAP_XATTR)
1385 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1386 NFS4_ACCESS_XAWRITE |
1391 p->o_arg.clientid = server->nfs_client->cl_clientid;
1392 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1393 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1394 p->o_arg.name = &dentry->d_name;
1395 p->o_arg.server = server;
1396 p->o_arg.bitmask = nfs4_bitmask(server, label);
1397 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1398 switch (p->o_arg.claim) {
1399 case NFS4_OPEN_CLAIM_NULL:
1400 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1401 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1402 p->o_arg.fh = NFS_FH(dir);
1404 case NFS4_OPEN_CLAIM_PREVIOUS:
1405 case NFS4_OPEN_CLAIM_FH:
1406 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1407 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1408 p->o_arg.fh = NFS_FH(d_inode(dentry));
1410 p->c_arg.fh = &p->o_res.fh;
1411 p->c_arg.stateid = &p->o_res.stateid;
1412 p->c_arg.seqid = p->o_arg.seqid;
1413 nfs4_init_opendata_res(p);
1414 kref_init(&p->kref);
1418 nfs4_label_free(p->a_label);
1420 nfs4_label_free(p->f_label);
1428 static void nfs4_opendata_free(struct kref *kref)
1430 struct nfs4_opendata *p = container_of(kref,
1431 struct nfs4_opendata, kref);
1432 struct super_block *sb = p->dentry->d_sb;
1434 nfs4_lgopen_release(p->lgp);
1435 nfs_free_seqid(p->o_arg.seqid);
1436 nfs4_sequence_free_slot(&p->o_res.seq_res);
1437 if (p->state != NULL)
1438 nfs4_put_open_state(p->state);
1439 nfs4_put_state_owner(p->owner);
1441 nfs4_label_free(p->a_label);
1442 nfs4_label_free(p->f_label);
1446 nfs_sb_deactive(sb);
1447 nfs_fattr_free_names(&p->f_attr);
1448 kfree(p->f_attr.mdsthreshold);
1452 static void nfs4_opendata_put(struct nfs4_opendata *p)
1455 kref_put(&p->kref, nfs4_opendata_free);
1458 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1461 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1462 case FMODE_READ|FMODE_WRITE:
1463 return state->n_rdwr != 0;
1465 return state->n_wronly != 0;
1467 return state->n_rdonly != 0;
1473 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1474 int open_mode, enum open_claim_type4 claim)
1478 if (open_mode & (O_EXCL|O_TRUNC))
1481 case NFS4_OPEN_CLAIM_NULL:
1482 case NFS4_OPEN_CLAIM_FH:
1487 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1489 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1490 && state->n_rdonly != 0;
1493 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1494 && state->n_wronly != 0;
1496 case FMODE_READ|FMODE_WRITE:
1497 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1498 && state->n_rdwr != 0;
1504 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1505 enum open_claim_type4 claim)
1507 if (delegation == NULL)
1509 if ((delegation->type & fmode) != fmode)
1512 case NFS4_OPEN_CLAIM_NULL:
1513 case NFS4_OPEN_CLAIM_FH:
1515 case NFS4_OPEN_CLAIM_PREVIOUS:
1516 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1522 nfs_mark_delegation_referenced(delegation);
1526 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1535 case FMODE_READ|FMODE_WRITE:
1538 nfs4_state_set_mode_locked(state, state->state | fmode);
1541 #ifdef CONFIG_NFS_V4_1
1542 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1544 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1546 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1548 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1552 #endif /* CONFIG_NFS_V4_1 */
1554 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1556 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1557 wake_up_all(&state->waitq);
1560 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1562 struct nfs_client *clp = state->owner->so_server->nfs_client;
1563 bool need_recover = false;
1565 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1566 need_recover = true;
1567 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1568 need_recover = true;
1569 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1570 need_recover = true;
1572 nfs4_state_mark_reclaim_nograce(clp, state);
1576 * Check for whether or not the caller may update the open stateid
1577 * to the value passed in by stateid.
1579 * Note: This function relies heavily on the server implementing
1580 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1582 * i.e. The stateid seqids have to be initialised to 1, and
1583 * are then incremented on every state transition.
1585 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1586 const nfs4_stateid *stateid)
1588 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1589 /* The common case - we're updating to a new sequence number */
1590 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1591 nfs4_stateid_is_next(&state->open_stateid, stateid)) {
1595 /* This is the first OPEN in this generation */
1596 if (stateid->seqid == cpu_to_be32(1))
1602 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1604 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1606 if (state->n_wronly)
1607 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1608 if (state->n_rdonly)
1609 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1611 set_bit(NFS_O_RDWR_STATE, &state->flags);
1612 set_bit(NFS_OPEN_STATE, &state->flags);
1615 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1616 nfs4_stateid *stateid, fmode_t fmode)
1618 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1619 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1621 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1624 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1627 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1628 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1629 clear_bit(NFS_OPEN_STATE, &state->flags);
1631 if (stateid == NULL)
1633 /* Handle OPEN+OPEN_DOWNGRADE races */
1634 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1635 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1636 nfs_resync_open_stateid_locked(state);
1639 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1640 nfs4_stateid_copy(&state->stateid, stateid);
1641 nfs4_stateid_copy(&state->open_stateid, stateid);
1642 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1644 nfs_state_log_update_open_stateid(state);
1647 static void nfs_clear_open_stateid(struct nfs4_state *state,
1648 nfs4_stateid *arg_stateid,
1649 nfs4_stateid *stateid, fmode_t fmode)
1651 write_seqlock(&state->seqlock);
1652 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1653 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1654 nfs_clear_open_stateid_locked(state, stateid, fmode);
1655 write_sequnlock(&state->seqlock);
1656 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1657 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1660 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1661 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1662 __must_hold(&state->owner->so_lock)
1663 __must_hold(&state->seqlock)
1671 if (nfs_stateid_is_sequential(state, stateid))
1676 /* Rely on seqids for serialisation with NFSv4.0 */
1677 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1680 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1681 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1683 * Ensure we process the state changes in the same order
1684 * in which the server processed them by delaying the
1685 * update of the stateid until we are in sequence.
1687 write_sequnlock(&state->seqlock);
1688 spin_unlock(&state->owner->so_lock);
1690 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1692 if (!signal_pending(current)) {
1693 if (schedule_timeout(5*HZ) == 0)
1699 finish_wait(&state->waitq, &wait);
1701 spin_lock(&state->owner->so_lock);
1702 write_seqlock(&state->seqlock);
1705 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1706 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1707 nfs4_stateid_copy(freeme, &state->open_stateid);
1708 nfs_test_and_clear_all_open_stateid(state);
1711 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1712 nfs4_stateid_copy(&state->stateid, stateid);
1713 nfs4_stateid_copy(&state->open_stateid, stateid);
1714 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1715 nfs_state_log_update_open_stateid(state);
1718 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1719 const nfs4_stateid *open_stateid,
1721 nfs4_stateid *freeme)
1724 * Protect the call to nfs4_state_set_mode_locked and
1725 * serialise the stateid update
1727 write_seqlock(&state->seqlock);
1728 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1731 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1734 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1736 case FMODE_READ|FMODE_WRITE:
1737 set_bit(NFS_O_RDWR_STATE, &state->flags);
1739 set_bit(NFS_OPEN_STATE, &state->flags);
1740 write_sequnlock(&state->seqlock);
1743 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1745 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1746 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1747 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1748 clear_bit(NFS_OPEN_STATE, &state->flags);
1751 static void nfs_state_set_delegation(struct nfs4_state *state,
1752 const nfs4_stateid *deleg_stateid,
1756 * Protect the call to nfs4_state_set_mode_locked and
1757 * serialise the stateid update
1759 write_seqlock(&state->seqlock);
1760 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1761 set_bit(NFS_DELEGATED_STATE, &state->flags);
1762 write_sequnlock(&state->seqlock);
1765 static void nfs_state_clear_delegation(struct nfs4_state *state)
1767 write_seqlock(&state->seqlock);
1768 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1769 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1770 write_sequnlock(&state->seqlock);
1773 int update_open_stateid(struct nfs4_state *state,
1774 const nfs4_stateid *open_stateid,
1775 const nfs4_stateid *delegation,
1778 struct nfs_server *server = NFS_SERVER(state->inode);
1779 struct nfs_client *clp = server->nfs_client;
1780 struct nfs_inode *nfsi = NFS_I(state->inode);
1781 struct nfs_delegation *deleg_cur;
1782 nfs4_stateid freeme = { };
1785 fmode &= (FMODE_READ|FMODE_WRITE);
1788 spin_lock(&state->owner->so_lock);
1789 if (open_stateid != NULL) {
1790 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1794 deleg_cur = nfs4_get_valid_delegation(state->inode);
1795 if (deleg_cur == NULL)
1798 spin_lock(&deleg_cur->lock);
1799 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1800 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1801 (deleg_cur->type & fmode) != fmode)
1802 goto no_delegation_unlock;
1804 if (delegation == NULL)
1805 delegation = &deleg_cur->stateid;
1806 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1807 goto no_delegation_unlock;
1809 nfs_mark_delegation_referenced(deleg_cur);
1810 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1812 no_delegation_unlock:
1813 spin_unlock(&deleg_cur->lock);
1816 update_open_stateflags(state, fmode);
1817 spin_unlock(&state->owner->so_lock);
1820 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1821 nfs4_schedule_state_manager(clp);
1822 if (freeme.type != 0)
1823 nfs4_test_and_free_stateid(server, &freeme,
1824 state->owner->so_cred);
1829 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1830 const nfs4_stateid *stateid)
1832 struct nfs4_state *state = lsp->ls_state;
1835 spin_lock(&state->state_lock);
1836 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1838 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1840 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1843 spin_unlock(&state->state_lock);
1847 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1849 struct nfs_delegation *delegation;
1851 fmode &= FMODE_READ|FMODE_WRITE;
1853 delegation = nfs4_get_valid_delegation(inode);
1854 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1859 nfs4_inode_return_delegation(inode);
1862 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1864 struct nfs4_state *state = opendata->state;
1865 struct nfs_delegation *delegation;
1866 int open_mode = opendata->o_arg.open_flags;
1867 fmode_t fmode = opendata->o_arg.fmode;
1868 enum open_claim_type4 claim = opendata->o_arg.claim;
1869 nfs4_stateid stateid;
1873 spin_lock(&state->owner->so_lock);
1874 if (can_open_cached(state, fmode, open_mode, claim)) {
1875 update_open_stateflags(state, fmode);
1876 spin_unlock(&state->owner->so_lock);
1877 goto out_return_state;
1879 spin_unlock(&state->owner->so_lock);
1881 delegation = nfs4_get_valid_delegation(state->inode);
1882 if (!can_open_delegated(delegation, fmode, claim)) {
1886 /* Save the delegation */
1887 nfs4_stateid_copy(&stateid, &delegation->stateid);
1889 nfs_release_seqid(opendata->o_arg.seqid);
1890 if (!opendata->is_recover) {
1891 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1897 /* Try to update the stateid using the delegation */
1898 if (update_open_stateid(state, NULL, &stateid, fmode))
1899 goto out_return_state;
1902 return ERR_PTR(ret);
1904 refcount_inc(&state->count);
1909 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1911 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1912 struct nfs_delegation *delegation;
1913 int delegation_flags = 0;
1916 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1918 delegation_flags = delegation->flags;
1920 switch (data->o_arg.claim) {
1923 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1924 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1925 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1926 "returning a delegation for "
1927 "OPEN(CLAIM_DELEGATE_CUR)\n",
1931 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1932 nfs_inode_set_delegation(state->inode,
1933 data->owner->so_cred,
1934 data->o_res.delegation_type,
1935 &data->o_res.delegation,
1936 data->o_res.pagemod_limit);
1938 nfs_inode_reclaim_delegation(state->inode,
1939 data->owner->so_cred,
1940 data->o_res.delegation_type,
1941 &data->o_res.delegation,
1942 data->o_res.pagemod_limit);
1944 if (data->o_res.do_recall)
1945 nfs_async_inode_return_delegation(state->inode,
1946 &data->o_res.delegation);
1950 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1951 * and update the nfs4_state.
1953 static struct nfs4_state *
1954 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1956 struct inode *inode = data->state->inode;
1957 struct nfs4_state *state = data->state;
1960 if (!data->rpc_done) {
1961 if (data->rpc_status)
1962 return ERR_PTR(data->rpc_status);
1963 /* cached opens have already been processed */
1967 ret = nfs_refresh_inode(inode, &data->f_attr);
1969 return ERR_PTR(ret);
1971 if (data->o_res.delegation_type != 0)
1972 nfs4_opendata_check_deleg(data, state);
1974 if (!update_open_stateid(state, &data->o_res.stateid,
1975 NULL, data->o_arg.fmode))
1976 return ERR_PTR(-EAGAIN);
1977 refcount_inc(&state->count);
1982 static struct inode *
1983 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1985 struct inode *inode;
1987 switch (data->o_arg.claim) {
1988 case NFS4_OPEN_CLAIM_NULL:
1989 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1990 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1991 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1992 return ERR_PTR(-EAGAIN);
1993 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1994 &data->f_attr, data->f_label);
1997 inode = d_inode(data->dentry);
1999 nfs_refresh_inode(inode, &data->f_attr);
2004 static struct nfs4_state *
2005 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2007 struct nfs4_state *state;
2008 struct inode *inode;
2010 inode = nfs4_opendata_get_inode(data);
2012 return ERR_CAST(inode);
2013 if (data->state != NULL && data->state->inode == inode) {
2014 state = data->state;
2015 refcount_inc(&state->count);
2017 state = nfs4_get_open_state(inode, data->owner);
2020 state = ERR_PTR(-ENOMEM);
2024 static struct nfs4_state *
2025 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2027 struct nfs4_state *state;
2029 if (!data->rpc_done) {
2030 state = nfs4_try_open_cached(data);
2031 trace_nfs4_cached_open(data->state);
2035 state = nfs4_opendata_find_nfs4_state(data);
2039 if (data->o_res.delegation_type != 0)
2040 nfs4_opendata_check_deleg(data, state);
2041 if (!update_open_stateid(state, &data->o_res.stateid,
2042 NULL, data->o_arg.fmode)) {
2043 nfs4_put_open_state(state);
2044 state = ERR_PTR(-EAGAIN);
2047 nfs_release_seqid(data->o_arg.seqid);
2051 static struct nfs4_state *
2052 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2054 struct nfs4_state *ret;
2056 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2057 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2059 ret = _nfs4_opendata_to_nfs4_state(data);
2060 nfs4_sequence_free_slot(&data->o_res.seq_res);
2064 static struct nfs_open_context *
2065 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2067 struct nfs_inode *nfsi = NFS_I(state->inode);
2068 struct nfs_open_context *ctx;
2071 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2072 if (ctx->state != state)
2074 if ((ctx->mode & mode) != mode)
2076 if (!get_nfs_open_context(ctx))
2082 return ERR_PTR(-ENOENT);
2085 static struct nfs_open_context *
2086 nfs4_state_find_open_context(struct nfs4_state *state)
2088 struct nfs_open_context *ctx;
2090 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2093 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2096 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2099 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2100 struct nfs4_state *state, enum open_claim_type4 claim)
2102 struct nfs4_opendata *opendata;
2104 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2105 NULL, claim, GFP_NOFS);
2106 if (opendata == NULL)
2107 return ERR_PTR(-ENOMEM);
2108 opendata->state = state;
2109 refcount_inc(&state->count);
2113 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2116 struct nfs4_state *newstate;
2119 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2121 opendata->o_arg.open_flags = 0;
2122 opendata->o_arg.fmode = fmode;
2123 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2124 NFS_SB(opendata->dentry->d_sb),
2126 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2127 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2128 nfs4_init_opendata_res(opendata);
2129 ret = _nfs4_recover_proc_open(opendata);
2132 newstate = nfs4_opendata_to_nfs4_state(opendata);
2133 if (IS_ERR(newstate))
2134 return PTR_ERR(newstate);
2135 if (newstate != opendata->state)
2137 nfs4_close_state(newstate, fmode);
2141 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2145 /* memory barrier prior to reading state->n_* */
2147 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2150 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2153 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2157 * We may have performed cached opens for all three recoveries.
2158 * Check if we need to update the current stateid.
2160 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2161 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2162 write_seqlock(&state->seqlock);
2163 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2164 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2165 write_sequnlock(&state->seqlock);
2172 * reclaim state on the server after a reboot.
2174 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2176 struct nfs_delegation *delegation;
2177 struct nfs4_opendata *opendata;
2178 fmode_t delegation_type = 0;
2181 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2182 NFS4_OPEN_CLAIM_PREVIOUS);
2183 if (IS_ERR(opendata))
2184 return PTR_ERR(opendata);
2186 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2187 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2188 delegation_type = delegation->type;
2190 opendata->o_arg.u.delegation_type = delegation_type;
2191 status = nfs4_open_recover(opendata, state);
2192 nfs4_opendata_put(opendata);
2196 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2198 struct nfs_server *server = NFS_SERVER(state->inode);
2199 struct nfs4_exception exception = { };
2202 err = _nfs4_do_open_reclaim(ctx, state);
2203 trace_nfs4_open_reclaim(ctx, 0, err);
2204 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2206 if (err != -NFS4ERR_DELAY)
2208 nfs4_handle_exception(server, err, &exception);
2209 } while (exception.retry);
2213 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2215 struct nfs_open_context *ctx;
2218 ctx = nfs4_state_find_open_context(state);
2221 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2222 nfs_state_clear_open_state_flags(state);
2223 ret = nfs4_do_open_reclaim(ctx, state);
2224 put_nfs_open_context(ctx);
2228 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)
2232 printk(KERN_ERR "NFS: %s: unhandled error "
2233 "%d.\n", __func__, err);
2240 case -NFS4ERR_BADSESSION:
2241 case -NFS4ERR_BADSLOT:
2242 case -NFS4ERR_BAD_HIGH_SLOT:
2243 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2244 case -NFS4ERR_DEADSESSION:
2246 case -NFS4ERR_STALE_CLIENTID:
2247 case -NFS4ERR_STALE_STATEID:
2248 /* Don't recall a delegation if it was lost */
2249 nfs4_schedule_lease_recovery(server->nfs_client);
2251 case -NFS4ERR_MOVED:
2252 nfs4_schedule_migration_recovery(server);
2254 case -NFS4ERR_LEASE_MOVED:
2255 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2257 case -NFS4ERR_DELEG_REVOKED:
2258 case -NFS4ERR_ADMIN_REVOKED:
2259 case -NFS4ERR_EXPIRED:
2260 case -NFS4ERR_BAD_STATEID:
2261 case -NFS4ERR_OPENMODE:
2262 nfs_inode_find_state_and_recover(state->inode,
2264 nfs4_schedule_stateid_recovery(server, state);
2266 case -NFS4ERR_DELAY:
2267 case -NFS4ERR_GRACE:
2271 case -NFS4ERR_DENIED:
2273 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2275 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2282 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2283 struct nfs4_state *state, const nfs4_stateid *stateid)
2285 struct nfs_server *server = NFS_SERVER(state->inode);
2286 struct nfs4_opendata *opendata;
2289 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2290 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2291 if (IS_ERR(opendata))
2292 return PTR_ERR(opendata);
2293 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2294 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2295 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2299 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2300 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2304 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2305 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2309 nfs_state_clear_delegation(state);
2311 nfs4_opendata_put(opendata);
2312 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2315 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2317 struct nfs4_opendata *data = calldata;
2319 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2320 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2323 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2325 struct nfs4_opendata *data = calldata;
2327 nfs40_sequence_done(task, &data->c_res.seq_res);
2329 data->rpc_status = task->tk_status;
2330 if (data->rpc_status == 0) {
2331 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2332 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2333 renew_lease(data->o_res.server, data->timestamp);
2334 data->rpc_done = true;
2338 static void nfs4_open_confirm_release(void *calldata)
2340 struct nfs4_opendata *data = calldata;
2341 struct nfs4_state *state = NULL;
2343 /* If this request hasn't been cancelled, do nothing */
2344 if (!data->cancelled)
2346 /* In case of error, no cleanup! */
2347 if (!data->rpc_done)
2349 state = nfs4_opendata_to_nfs4_state(data);
2351 nfs4_close_state(state, data->o_arg.fmode);
2353 nfs4_opendata_put(data);
2356 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2357 .rpc_call_prepare = nfs4_open_confirm_prepare,
2358 .rpc_call_done = nfs4_open_confirm_done,
2359 .rpc_release = nfs4_open_confirm_release,
2363 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2365 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2367 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2368 struct rpc_task *task;
2369 struct rpc_message msg = {
2370 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2371 .rpc_argp = &data->c_arg,
2372 .rpc_resp = &data->c_res,
2373 .rpc_cred = data->owner->so_cred,
2375 struct rpc_task_setup task_setup_data = {
2376 .rpc_client = server->client,
2377 .rpc_message = &msg,
2378 .callback_ops = &nfs4_open_confirm_ops,
2379 .callback_data = data,
2380 .workqueue = nfsiod_workqueue,
2381 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2385 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2387 kref_get(&data->kref);
2388 data->rpc_done = false;
2389 data->rpc_status = 0;
2390 data->timestamp = jiffies;
2391 task = rpc_run_task(&task_setup_data);
2393 return PTR_ERR(task);
2394 status = rpc_wait_for_completion_task(task);
2396 data->cancelled = true;
2399 status = data->rpc_status;
2404 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2406 struct nfs4_opendata *data = calldata;
2407 struct nfs4_state_owner *sp = data->owner;
2408 struct nfs_client *clp = sp->so_server->nfs_client;
2409 enum open_claim_type4 claim = data->o_arg.claim;
2411 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2414 * Check if we still need to send an OPEN call, or if we can use
2415 * a delegation instead.
2417 if (data->state != NULL) {
2418 struct nfs_delegation *delegation;
2420 if (can_open_cached(data->state, data->o_arg.fmode,
2421 data->o_arg.open_flags, claim))
2424 delegation = nfs4_get_valid_delegation(data->state->inode);
2425 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2426 goto unlock_no_action;
2429 /* Update client id. */
2430 data->o_arg.clientid = clp->cl_clientid;
2434 case NFS4_OPEN_CLAIM_PREVIOUS:
2435 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2436 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2437 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2439 case NFS4_OPEN_CLAIM_FH:
2440 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2442 data->timestamp = jiffies;
2443 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2444 &data->o_arg.seq_args,
2445 &data->o_res.seq_res,
2447 nfs_release_seqid(data->o_arg.seqid);
2449 /* Set the create mode (note dependency on the session type) */
2450 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2451 if (data->o_arg.open_flags & O_EXCL) {
2452 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2453 if (nfs4_has_persistent_session(clp))
2454 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2455 else if (clp->cl_mvops->minor_version > 0)
2456 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2460 trace_nfs4_cached_open(data->state);
2463 task->tk_action = NULL;
2465 nfs4_sequence_done(task, &data->o_res.seq_res);
2468 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2470 struct nfs4_opendata *data = calldata;
2472 data->rpc_status = task->tk_status;
2474 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2477 if (task->tk_status == 0) {
2478 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2479 switch (data->o_res.f_attr->mode & S_IFMT) {
2483 data->rpc_status = -ELOOP;
2486 data->rpc_status = -EISDIR;
2489 data->rpc_status = -ENOTDIR;
2492 renew_lease(data->o_res.server, data->timestamp);
2493 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2494 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2496 data->rpc_done = true;
2499 static void nfs4_open_release(void *calldata)
2501 struct nfs4_opendata *data = calldata;
2502 struct nfs4_state *state = NULL;
2504 /* If this request hasn't been cancelled, do nothing */
2505 if (!data->cancelled)
2507 /* In case of error, no cleanup! */
2508 if (data->rpc_status != 0 || !data->rpc_done)
2510 /* In case we need an open_confirm, no cleanup! */
2511 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2513 state = nfs4_opendata_to_nfs4_state(data);
2515 nfs4_close_state(state, data->o_arg.fmode);
2517 nfs4_opendata_put(data);
2520 static const struct rpc_call_ops nfs4_open_ops = {
2521 .rpc_call_prepare = nfs4_open_prepare,
2522 .rpc_call_done = nfs4_open_done,
2523 .rpc_release = nfs4_open_release,
2526 static int nfs4_run_open_task(struct nfs4_opendata *data,
2527 struct nfs_open_context *ctx)
2529 struct inode *dir = d_inode(data->dir);
2530 struct nfs_server *server = NFS_SERVER(dir);
2531 struct nfs_openargs *o_arg = &data->o_arg;
2532 struct nfs_openres *o_res = &data->o_res;
2533 struct rpc_task *task;
2534 struct rpc_message msg = {
2535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2538 .rpc_cred = data->owner->so_cred,
2540 struct rpc_task_setup task_setup_data = {
2541 .rpc_client = server->client,
2542 .rpc_message = &msg,
2543 .callback_ops = &nfs4_open_ops,
2544 .callback_data = data,
2545 .workqueue = nfsiod_workqueue,
2546 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2550 kref_get(&data->kref);
2551 data->rpc_done = false;
2552 data->rpc_status = 0;
2553 data->cancelled = false;
2554 data->is_recover = false;
2556 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2557 data->is_recover = true;
2558 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2560 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2561 pnfs_lgopen_prepare(data, ctx);
2563 task = rpc_run_task(&task_setup_data);
2565 return PTR_ERR(task);
2566 status = rpc_wait_for_completion_task(task);
2568 data->cancelled = true;
2571 status = data->rpc_status;
2577 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2579 struct inode *dir = d_inode(data->dir);
2580 struct nfs_openres *o_res = &data->o_res;
2583 status = nfs4_run_open_task(data, NULL);
2584 if (status != 0 || !data->rpc_done)
2587 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2589 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2590 status = _nfs4_proc_open_confirm(data);
2596 * Additional permission checks in order to distinguish between an
2597 * open for read, and an open for execute. This works around the
2598 * fact that NFSv4 OPEN treats read and execute permissions as being
2600 * Note that in the non-execute case, we want to turn off permission
2601 * checking if we just created a new file (POSIX open() semantics).
2603 static int nfs4_opendata_access(const struct cred *cred,
2604 struct nfs4_opendata *opendata,
2605 struct nfs4_state *state, fmode_t fmode,
2608 struct nfs_access_entry cache;
2611 /* access call failed or for some reason the server doesn't
2612 * support any access modes -- defer access call until later */
2613 if (opendata->o_res.access_supported == 0)
2618 * Use openflags to check for exec, because fmode won't
2619 * always have FMODE_EXEC set when file open for exec.
2621 if (openflags & __FMODE_EXEC) {
2622 /* ONLY check for exec rights */
2623 if (S_ISDIR(state->inode->i_mode))
2624 mask = NFS4_ACCESS_LOOKUP;
2626 mask = NFS4_ACCESS_EXECUTE;
2627 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2628 mask = NFS4_ACCESS_READ;
2631 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2632 nfs_access_add_cache(state->inode, &cache);
2634 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2635 if ((mask & ~cache.mask & flags) == 0)
2642 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2644 static int _nfs4_proc_open(struct nfs4_opendata *data,
2645 struct nfs_open_context *ctx)
2647 struct inode *dir = d_inode(data->dir);
2648 struct nfs_server *server = NFS_SERVER(dir);
2649 struct nfs_openargs *o_arg = &data->o_arg;
2650 struct nfs_openres *o_res = &data->o_res;
2653 status = nfs4_run_open_task(data, ctx);
2654 if (!data->rpc_done)
2657 if (status == -NFS4ERR_BADNAME &&
2658 !(o_arg->open_flags & O_CREAT))
2663 nfs_fattr_map_and_free_names(server, &data->f_attr);
2665 if (o_arg->open_flags & O_CREAT) {
2666 if (o_arg->open_flags & O_EXCL)
2667 data->file_created = true;
2668 else if (o_res->cinfo.before != o_res->cinfo.after)
2669 data->file_created = true;
2670 if (data->file_created ||
2671 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2672 nfs4_update_changeattr(dir, &o_res->cinfo,
2673 o_res->f_attr->time_start,
2674 NFS_INO_INVALID_DATA);
2676 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2677 server->caps &= ~NFS_CAP_POSIX_LOCK;
2678 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2679 status = _nfs4_proc_open_confirm(data);
2683 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2684 nfs4_sequence_free_slot(&o_res->seq_res);
2685 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2686 o_res->f_label, NULL);
2693 * reclaim state on the server after a network partition.
2694 * Assumes caller holds the appropriate lock
2696 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2698 struct nfs4_opendata *opendata;
2701 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2702 NFS4_OPEN_CLAIM_FH);
2703 if (IS_ERR(opendata))
2704 return PTR_ERR(opendata);
2705 ret = nfs4_open_recover(opendata, state);
2707 d_drop(ctx->dentry);
2708 nfs4_opendata_put(opendata);
2712 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2714 struct nfs_server *server = NFS_SERVER(state->inode);
2715 struct nfs4_exception exception = { };
2719 err = _nfs4_open_expired(ctx, state);
2720 trace_nfs4_open_expired(ctx, 0, err);
2721 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2726 case -NFS4ERR_GRACE:
2727 case -NFS4ERR_DELAY:
2728 nfs4_handle_exception(server, err, &exception);
2731 } while (exception.retry);
2736 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2738 struct nfs_open_context *ctx;
2741 ctx = nfs4_state_find_open_context(state);
2744 ret = nfs4_do_open_expired(ctx, state);
2745 put_nfs_open_context(ctx);
2749 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2750 const nfs4_stateid *stateid)
2752 nfs_remove_bad_delegation(state->inode, stateid);
2753 nfs_state_clear_delegation(state);
2756 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2758 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2759 nfs_finish_clear_delegation_stateid(state, NULL);
2762 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2764 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2765 nfs40_clear_delegation_stateid(state);
2766 nfs_state_clear_open_state_flags(state);
2767 return nfs4_open_expired(sp, state);
2770 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2771 nfs4_stateid *stateid,
2772 const struct cred *cred)
2774 return -NFS4ERR_BAD_STATEID;
2777 #if defined(CONFIG_NFS_V4_1)
2778 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2779 nfs4_stateid *stateid,
2780 const struct cred *cred)
2784 switch (stateid->type) {
2787 case NFS4_INVALID_STATEID_TYPE:
2788 case NFS4_SPECIAL_STATEID_TYPE:
2789 return -NFS4ERR_BAD_STATEID;
2790 case NFS4_REVOKED_STATEID_TYPE:
2794 status = nfs41_test_stateid(server, stateid, cred);
2796 case -NFS4ERR_EXPIRED:
2797 case -NFS4ERR_ADMIN_REVOKED:
2798 case -NFS4ERR_DELEG_REVOKED:
2804 /* Ack the revoked state to the server */
2805 nfs41_free_stateid(server, stateid, cred, true);
2806 return -NFS4ERR_EXPIRED;
2809 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2811 struct nfs_server *server = NFS_SERVER(state->inode);
2812 nfs4_stateid stateid;
2813 struct nfs_delegation *delegation;
2814 const struct cred *cred = NULL;
2815 int status, ret = NFS_OK;
2817 /* Get the delegation credential for use by test/free_stateid */
2819 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2820 if (delegation == NULL) {
2822 nfs_state_clear_delegation(state);
2826 spin_lock(&delegation->lock);
2827 nfs4_stateid_copy(&stateid, &delegation->stateid);
2829 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2830 &delegation->flags)) {
2831 spin_unlock(&delegation->lock);
2836 if (delegation->cred)
2837 cred = get_cred(delegation->cred);
2838 spin_unlock(&delegation->lock);
2840 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2841 trace_nfs4_test_delegation_stateid(state, NULL, status);
2842 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2843 nfs_finish_clear_delegation_stateid(state, &stateid);
2851 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2855 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2856 nfs4_copy_delegation_stateid(state->inode, state->state,
2858 nfs4_stateid_match_other(&state->stateid, &tmp))
2859 nfs_state_set_delegation(state, &tmp, state->state);
2861 nfs_state_clear_delegation(state);
2865 * nfs41_check_expired_locks - possibly free a lock stateid
2867 * @state: NFSv4 state for an inode
2869 * Returns NFS_OK if recovery for this stateid is now finished.
2870 * Otherwise a negative NFS4ERR value is returned.
2872 static int nfs41_check_expired_locks(struct nfs4_state *state)
2874 int status, ret = NFS_OK;
2875 struct nfs4_lock_state *lsp, *prev = NULL;
2876 struct nfs_server *server = NFS_SERVER(state->inode);
2878 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2881 spin_lock(&state->state_lock);
2882 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2883 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2884 const struct cred *cred = lsp->ls_state->owner->so_cred;
2886 refcount_inc(&lsp->ls_count);
2887 spin_unlock(&state->state_lock);
2889 nfs4_put_lock_state(prev);
2892 status = nfs41_test_and_free_expired_stateid(server,
2895 trace_nfs4_test_lock_stateid(state, lsp, status);
2896 if (status == -NFS4ERR_EXPIRED ||
2897 status == -NFS4ERR_BAD_STATEID) {
2898 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2899 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2900 if (!recover_lost_locks)
2901 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2902 } else if (status != NFS_OK) {
2904 nfs4_put_lock_state(prev);
2907 spin_lock(&state->state_lock);
2910 spin_unlock(&state->state_lock);
2911 nfs4_put_lock_state(prev);
2917 * nfs41_check_open_stateid - possibly free an open stateid
2919 * @state: NFSv4 state for an inode
2921 * Returns NFS_OK if recovery for this stateid is now finished.
2922 * Otherwise a negative NFS4ERR value is returned.
2924 static int nfs41_check_open_stateid(struct nfs4_state *state)
2926 struct nfs_server *server = NFS_SERVER(state->inode);
2927 nfs4_stateid *stateid = &state->open_stateid;
2928 const struct cred *cred = state->owner->so_cred;
2931 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2932 return -NFS4ERR_BAD_STATEID;
2933 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2934 trace_nfs4_test_open_stateid(state, NULL, status);
2935 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2936 nfs_state_clear_open_state_flags(state);
2937 stateid->type = NFS4_INVALID_STATEID_TYPE;
2940 if (nfs_open_stateid_recover_openmode(state))
2941 return -NFS4ERR_OPENMODE;
2945 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2949 status = nfs41_check_delegation_stateid(state);
2950 if (status != NFS_OK)
2952 nfs41_delegation_recover_stateid(state);
2954 status = nfs41_check_expired_locks(state);
2955 if (status != NFS_OK)
2957 status = nfs41_check_open_stateid(state);
2958 if (status != NFS_OK)
2959 status = nfs4_open_expired(sp, state);
2965 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2966 * fields corresponding to attributes that were used to store the verifier.
2967 * Make sure we clobber those fields in the later setattr call
2969 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2970 struct iattr *sattr, struct nfs4_label **label)
2972 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2977 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2978 attrset[i] = opendata->o_res.attrset[i];
2979 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2980 attrset[i] &= ~bitmask[i];
2983 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2984 sattr->ia_valid : 0;
2986 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2987 if (sattr->ia_valid & ATTR_ATIME_SET)
2988 ret |= ATTR_ATIME_SET;
2993 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2994 if (sattr->ia_valid & ATTR_MTIME_SET)
2995 ret |= ATTR_MTIME_SET;
3000 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3005 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3006 int flags, struct nfs_open_context *ctx)
3008 struct nfs4_state_owner *sp = opendata->owner;
3009 struct nfs_server *server = sp->so_server;
3010 struct dentry *dentry;
3011 struct nfs4_state *state;
3012 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3013 struct inode *dir = d_inode(opendata->dir);
3014 unsigned long dir_verifier;
3018 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3019 dir_verifier = nfs_save_change_attribute(dir);
3021 ret = _nfs4_proc_open(opendata, ctx);
3025 state = _nfs4_opendata_to_nfs4_state(opendata);
3026 ret = PTR_ERR(state);
3030 if (server->caps & NFS_CAP_POSIX_LOCK)
3031 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3032 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3033 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3035 dentry = opendata->dentry;
3036 if (d_really_is_negative(dentry)) {
3037 struct dentry *alias;
3039 alias = d_exact_alias(dentry, state->inode);
3041 alias = d_splice_alias(igrab(state->inode), dentry);
3042 /* d_splice_alias() can't fail here - it's a non-directory */
3045 ctx->dentry = dentry = alias;
3049 switch(opendata->o_arg.claim) {
3052 case NFS4_OPEN_CLAIM_NULL:
3053 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3054 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3055 if (!opendata->rpc_done)
3057 if (opendata->o_res.delegation_type != 0)
3058 dir_verifier = nfs_save_change_attribute(dir);
3059 nfs_set_verifier(dentry, dir_verifier);
3062 /* Parse layoutget results before we check for access */
3063 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3065 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3070 if (d_inode(dentry) == state->inode) {
3071 nfs_inode_attach_open_context(ctx);
3072 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3073 nfs4_schedule_stateid_recovery(server, state);
3077 if (!opendata->cancelled)
3078 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3083 * Returns a referenced nfs4_state
3085 static int _nfs4_do_open(struct inode *dir,
3086 struct nfs_open_context *ctx,
3088 const struct nfs4_open_createattrs *c,
3091 struct nfs4_state_owner *sp;
3092 struct nfs4_state *state = NULL;
3093 struct nfs_server *server = NFS_SERVER(dir);
3094 struct nfs4_opendata *opendata;
3095 struct dentry *dentry = ctx->dentry;
3096 const struct cred *cred = ctx->cred;
3097 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3098 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3099 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3100 struct iattr *sattr = c->sattr;
3101 struct nfs4_label *label = c->label;
3102 struct nfs4_label *olabel = NULL;
3105 /* Protect against reboot recovery conflicts */
3107 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3109 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3112 status = nfs4_client_recover_expired_lease(server->nfs_client);
3114 goto err_put_state_owner;
3115 if (d_really_is_positive(dentry))
3116 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3118 if (d_really_is_positive(dentry))
3119 claim = NFS4_OPEN_CLAIM_FH;
3120 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3121 c, claim, GFP_KERNEL);
3122 if (opendata == NULL)
3123 goto err_put_state_owner;
3126 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3127 if (IS_ERR(olabel)) {
3128 status = PTR_ERR(olabel);
3129 goto err_opendata_put;
3133 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3134 if (!opendata->f_attr.mdsthreshold) {
3135 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3136 if (!opendata->f_attr.mdsthreshold)
3137 goto err_free_label;
3139 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3141 if (d_really_is_positive(dentry))
3142 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3144 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3146 goto err_free_label;
3149 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3150 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3151 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3153 * send create attributes which was not set by open
3154 * with an extra setattr.
3156 if (attrs || label) {
3157 unsigned ia_old = sattr->ia_valid;
3159 sattr->ia_valid = attrs;
3160 nfs_fattr_init(opendata->o_res.f_attr);
3161 status = nfs4_do_setattr(state->inode, cred,
3162 opendata->o_res.f_attr, sattr,
3163 ctx, label, olabel);
3165 nfs_setattr_update_inode(state->inode, sattr,
3166 opendata->o_res.f_attr);
3167 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3169 sattr->ia_valid = ia_old;
3172 if (opened && opendata->file_created)
3175 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3176 *ctx_th = opendata->f_attr.mdsthreshold;
3177 opendata->f_attr.mdsthreshold = NULL;
3180 nfs4_label_free(olabel);
3182 nfs4_opendata_put(opendata);
3183 nfs4_put_state_owner(sp);
3186 nfs4_label_free(olabel);
3188 nfs4_opendata_put(opendata);
3189 err_put_state_owner:
3190 nfs4_put_state_owner(sp);
3196 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3197 struct nfs_open_context *ctx,
3199 struct iattr *sattr,
3200 struct nfs4_label *label,
3203 struct nfs_server *server = NFS_SERVER(dir);
3204 struct nfs4_exception exception = {
3205 .interruptible = true,
3207 struct nfs4_state *res;
3208 struct nfs4_open_createattrs c = {
3212 [0] = (__u32)jiffies,
3213 [1] = (__u32)current->pid,
3219 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3221 trace_nfs4_open_file(ctx, flags, status);
3224 /* NOTE: BAD_SEQID means the server and client disagree about the
3225 * book-keeping w.r.t. state-changing operations
3226 * (OPEN/CLOSE/LOCK/LOCKU...)
3227 * It is actually a sign of a bug on the client or on the server.
3229 * If we receive a BAD_SEQID error in the particular case of
3230 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3231 * have unhashed the old state_owner for us, and that we can
3232 * therefore safely retry using a new one. We should still warn
3233 * the user though...
3235 if (status == -NFS4ERR_BAD_SEQID) {
3236 pr_warn_ratelimited("NFS: v4 server %s "
3237 " returned a bad sequence-id error!\n",
3238 NFS_SERVER(dir)->nfs_client->cl_hostname);
3239 exception.retry = 1;
3243 * BAD_STATEID on OPEN means that the server cancelled our
3244 * state before it received the OPEN_CONFIRM.
3245 * Recover by retrying the request as per the discussion
3246 * on Page 181 of RFC3530.
3248 if (status == -NFS4ERR_BAD_STATEID) {
3249 exception.retry = 1;
3252 if (status == -NFS4ERR_EXPIRED) {
3253 nfs4_schedule_lease_recovery(server->nfs_client);
3254 exception.retry = 1;
3257 if (status == -EAGAIN) {
3258 /* We must have found a delegation */
3259 exception.retry = 1;
3262 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3264 res = ERR_PTR(nfs4_handle_exception(server,
3265 status, &exception));
3266 } while (exception.retry);
3270 static int _nfs4_do_setattr(struct inode *inode,
3271 struct nfs_setattrargs *arg,
3272 struct nfs_setattrres *res,
3273 const struct cred *cred,
3274 struct nfs_open_context *ctx)
3276 struct nfs_server *server = NFS_SERVER(inode);
3277 struct rpc_message msg = {
3278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3283 const struct cred *delegation_cred = NULL;
3284 unsigned long timestamp = jiffies;
3288 nfs_fattr_init(res->fattr);
3290 /* Servers should only apply open mode checks for file size changes */
3291 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3293 nfs4_inode_make_writeable(inode);
3297 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3298 /* Use that stateid */
3299 } else if (ctx != NULL && ctx->state) {
3300 struct nfs_lock_context *l_ctx;
3301 if (!nfs4_valid_open_stateid(ctx->state))
3303 l_ctx = nfs_get_lock_context(ctx);
3305 return PTR_ERR(l_ctx);
3306 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3307 &arg->stateid, &delegation_cred);
3308 nfs_put_lock_context(l_ctx);
3311 else if (status == -EAGAIN)
3315 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3317 if (delegation_cred)
3318 msg.rpc_cred = delegation_cred;
3320 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3322 put_cred(delegation_cred);
3323 if (status == 0 && ctx != NULL)
3324 renew_lease(server, timestamp);
3325 trace_nfs4_setattr(inode, &arg->stateid, status);
3329 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3330 struct nfs_fattr *fattr, struct iattr *sattr,
3331 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3332 struct nfs4_label *olabel)
3334 struct nfs_server *server = NFS_SERVER(inode);
3335 __u32 bitmask[NFS4_BITMASK_SZ];
3336 struct nfs4_state *state = ctx ? ctx->state : NULL;
3337 struct nfs_setattrargs arg = {
3338 .fh = NFS_FH(inode),
3344 struct nfs_setattrres res = {
3349 struct nfs4_exception exception = {
3352 .stateid = &arg.stateid,
3357 nfs4_bitmap_copy_adjust_setattr(bitmask,
3358 nfs4_bitmask(server, olabel),
3361 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3363 case -NFS4ERR_OPENMODE:
3364 if (!(sattr->ia_valid & ATTR_SIZE)) {
3365 pr_warn_once("NFSv4: server %s is incorrectly "
3366 "applying open mode checks to "
3367 "a SETATTR that is not "
3368 "changing file size.\n",
3369 server->nfs_client->cl_hostname);
3371 if (state && !(state->state & FMODE_WRITE)) {
3373 if (sattr->ia_valid & ATTR_OPEN)
3378 err = nfs4_handle_exception(server, err, &exception);
3379 } while (exception.retry);
3385 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3387 if (inode == NULL || !nfs_have_layout(inode))
3390 return pnfs_wait_on_layoutreturn(inode, task);
3394 * Update the seqid of an open stateid
3396 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3397 struct nfs4_state *state)
3404 if (!nfs4_valid_open_stateid(state))
3406 seq = read_seqbegin(&state->seqlock);
3407 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3408 nfs4_stateid_copy(dst, &state->open_stateid);
3409 if (read_seqretry(&state->seqlock, seq))
3413 seqid_open = state->open_stateid.seqid;
3414 if (read_seqretry(&state->seqlock, seq))
3417 dst_seqid = be32_to_cpu(dst->seqid);
3418 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3419 dst->seqid = seqid_open;
3425 * Update the seqid of an open stateid after receiving
3426 * NFS4ERR_OLD_STATEID
3428 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3429 struct nfs4_state *state)
3434 int seq, status = -EAGAIN;
3439 if (!nfs4_valid_open_stateid(state))
3441 seq = read_seqbegin(&state->seqlock);
3442 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3443 if (read_seqretry(&state->seqlock, seq))
3448 write_seqlock(&state->seqlock);
3449 seqid_open = state->open_stateid.seqid;
3451 dst_seqid = be32_to_cpu(dst->seqid);
3453 /* Did another OPEN bump the state's seqid? try again: */
3454 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3455 dst->seqid = seqid_open;
3456 write_sequnlock(&state->seqlock);
3461 /* server says we're behind but we haven't seen the update yet */
3462 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3463 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3464 write_sequnlock(&state->seqlock);
3465 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3467 if (signal_pending(current))
3470 if (schedule_timeout(5*HZ) != 0)
3473 finish_wait(&state->waitq, &wait);
3477 if (status == -EINTR)
3480 /* we slept the whole 5 seconds, we must have lost a seqid */
3481 dst->seqid = cpu_to_be32(dst_seqid + 1);
3489 struct nfs4_closedata {
3490 struct inode *inode;
3491 struct nfs4_state *state;
3492 struct nfs_closeargs arg;
3493 struct nfs_closeres res;
3495 struct nfs4_layoutreturn_args arg;
3496 struct nfs4_layoutreturn_res res;
3497 struct nfs4_xdr_opaque_data ld_private;
3501 struct nfs_fattr fattr;
3502 unsigned long timestamp;
3505 static void nfs4_free_closedata(void *data)
3507 struct nfs4_closedata *calldata = data;
3508 struct nfs4_state_owner *sp = calldata->state->owner;
3509 struct super_block *sb = calldata->state->inode->i_sb;
3511 if (calldata->lr.roc)
3512 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3513 calldata->res.lr_ret);
3514 nfs4_put_open_state(calldata->state);
3515 nfs_free_seqid(calldata->arg.seqid);
3516 nfs4_put_state_owner(sp);
3517 nfs_sb_deactive(sb);
3521 static void nfs4_close_done(struct rpc_task *task, void *data)
3523 struct nfs4_closedata *calldata = data;
3524 struct nfs4_state *state = calldata->state;
3525 struct nfs_server *server = NFS_SERVER(calldata->inode);
3526 nfs4_stateid *res_stateid = NULL;
3527 struct nfs4_exception exception = {
3529 .inode = calldata->inode,
3530 .stateid = &calldata->arg.stateid,
3533 dprintk("%s: begin!\n", __func__);
3534 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3536 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3538 /* Handle Layoutreturn errors */
3539 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3540 &calldata->res.lr_ret) == -EAGAIN)
3543 /* hmm. we are done with the inode, and in the process of freeing
3544 * the state_owner. we keep this around to process errors
3546 switch (task->tk_status) {
3548 res_stateid = &calldata->res.stateid;
3549 renew_lease(server, calldata->timestamp);
3551 case -NFS4ERR_ACCESS:
3552 if (calldata->arg.bitmask != NULL) {
3553 calldata->arg.bitmask = NULL;
3554 calldata->res.fattr = NULL;
3559 case -NFS4ERR_OLD_STATEID:
3560 /* Did we race with OPEN? */
3561 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3565 case -NFS4ERR_ADMIN_REVOKED:
3566 case -NFS4ERR_STALE_STATEID:
3567 case -NFS4ERR_EXPIRED:
3568 nfs4_free_revoked_stateid(server,
3569 &calldata->arg.stateid,
3570 task->tk_msg.rpc_cred);
3572 case -NFS4ERR_BAD_STATEID:
3573 if (calldata->arg.fmode == 0)
3577 task->tk_status = nfs4_async_handle_exception(task,
3578 server, task->tk_status, &exception);
3579 if (exception.retry)
3582 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3583 res_stateid, calldata->arg.fmode);
3585 task->tk_status = 0;
3586 nfs_release_seqid(calldata->arg.seqid);
3587 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3588 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3591 task->tk_status = 0;
3592 rpc_restart_call_prepare(task);
3596 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3598 struct nfs4_closedata *calldata = data;
3599 struct nfs4_state *state = calldata->state;
3600 struct inode *inode = calldata->inode;
3601 struct pnfs_layout_hdr *lo;
3602 bool is_rdonly, is_wronly, is_rdwr;
3605 dprintk("%s: begin!\n", __func__);
3606 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3609 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3610 spin_lock(&state->owner->so_lock);
3611 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3612 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3613 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3614 /* Calculate the change in open mode */
3615 calldata->arg.fmode = 0;
3616 if (state->n_rdwr == 0) {
3617 if (state->n_rdonly == 0)
3618 call_close |= is_rdonly;
3620 calldata->arg.fmode |= FMODE_READ;
3621 if (state->n_wronly == 0)
3622 call_close |= is_wronly;
3624 calldata->arg.fmode |= FMODE_WRITE;
3625 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3626 call_close |= is_rdwr;
3628 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3630 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3631 if (!nfs4_valid_open_stateid(state))
3633 spin_unlock(&state->owner->so_lock);
3636 /* Note: exit _without_ calling nfs4_close_done */
3640 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3641 nfs_release_seqid(calldata->arg.seqid);
3645 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3646 if (lo && !pnfs_layout_is_valid(lo)) {
3647 calldata->arg.lr_args = NULL;
3648 calldata->res.lr_res = NULL;
3651 if (calldata->arg.fmode == 0)
3652 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3654 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3655 /* Close-to-open cache consistency revalidation */
3656 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3657 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3658 nfs4_bitmask_adjust(calldata->arg.bitmask, inode, NFS_SERVER(inode), NULL);
3660 calldata->arg.bitmask = NULL;
3663 calldata->arg.share_access =
3664 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3665 calldata->arg.fmode, 0);
3667 if (calldata->res.fattr == NULL)
3668 calldata->arg.bitmask = NULL;
3669 else if (calldata->arg.bitmask == NULL)
3670 calldata->res.fattr = NULL;
3671 calldata->timestamp = jiffies;
3672 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3673 &calldata->arg.seq_args,
3674 &calldata->res.seq_res,
3676 nfs_release_seqid(calldata->arg.seqid);
3677 dprintk("%s: done!\n", __func__);
3680 task->tk_action = NULL;
3682 nfs4_sequence_done(task, &calldata->res.seq_res);
3685 static const struct rpc_call_ops nfs4_close_ops = {
3686 .rpc_call_prepare = nfs4_close_prepare,
3687 .rpc_call_done = nfs4_close_done,
3688 .rpc_release = nfs4_free_closedata,
3692 * It is possible for data to be read/written from a mem-mapped file
3693 * after the sys_close call (which hits the vfs layer as a flush).
3694 * This means that we can't safely call nfsv4 close on a file until
3695 * the inode is cleared. This in turn means that we are not good
3696 * NFSv4 citizens - we do not indicate to the server to update the file's
3697 * share state even when we are done with one of the three share
3698 * stateid's in the inode.
3700 * NOTE: Caller must be holding the sp->so_owner semaphore!
3702 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3704 struct nfs_server *server = NFS_SERVER(state->inode);
3705 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3706 struct nfs4_closedata *calldata;
3707 struct nfs4_state_owner *sp = state->owner;
3708 struct rpc_task *task;
3709 struct rpc_message msg = {
3710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3711 .rpc_cred = state->owner->so_cred,
3713 struct rpc_task_setup task_setup_data = {
3714 .rpc_client = server->client,
3715 .rpc_message = &msg,
3716 .callback_ops = &nfs4_close_ops,
3717 .workqueue = nfsiod_workqueue,
3718 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3720 int status = -ENOMEM;
3722 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3723 &task_setup_data.rpc_client, &msg);
3725 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3726 if (calldata == NULL)
3728 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3729 calldata->inode = state->inode;
3730 calldata->state = state;
3731 calldata->arg.fh = NFS_FH(state->inode);
3732 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3733 goto out_free_calldata;
3734 /* Serialization for the sequence id */
3735 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3736 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3737 if (IS_ERR(calldata->arg.seqid))
3738 goto out_free_calldata;
3739 nfs_fattr_init(&calldata->fattr);
3740 calldata->arg.fmode = 0;
3741 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3742 calldata->res.fattr = &calldata->fattr;
3743 calldata->res.seqid = calldata->arg.seqid;
3744 calldata->res.server = server;
3745 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3746 calldata->lr.roc = pnfs_roc(state->inode,
3747 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3748 if (calldata->lr.roc) {
3749 calldata->arg.lr_args = &calldata->lr.arg;
3750 calldata->res.lr_res = &calldata->lr.res;
3752 nfs_sb_active(calldata->inode->i_sb);
3754 msg.rpc_argp = &calldata->arg;
3755 msg.rpc_resp = &calldata->res;
3756 task_setup_data.callback_data = calldata;
3757 task = rpc_run_task(&task_setup_data);
3759 return PTR_ERR(task);
3762 status = rpc_wait_for_completion_task(task);
3768 nfs4_put_open_state(state);
3769 nfs4_put_state_owner(sp);
3773 static struct inode *
3774 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3775 int open_flags, struct iattr *attr, int *opened)
3777 struct nfs4_state *state;
3778 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3780 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3782 /* Protect against concurrent sillydeletes */
3783 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3785 nfs4_label_release_security(label);
3788 return ERR_CAST(state);
3789 return state->inode;
3792 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3794 if (ctx->state == NULL)
3797 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3799 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3802 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3803 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3804 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3806 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3808 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3809 struct nfs4_server_caps_arg args = {
3813 struct nfs4_server_caps_res res = {};
3814 struct rpc_message msg = {
3815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3822 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3823 FATTR4_WORD0_FH_EXPIRE_TYPE |
3824 FATTR4_WORD0_LINK_SUPPORT |
3825 FATTR4_WORD0_SYMLINK_SUPPORT |
3826 FATTR4_WORD0_ACLSUPPORT;
3828 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3830 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3832 /* Sanity check the server answers */
3833 switch (minorversion) {
3835 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3836 res.attr_bitmask[2] = 0;
3839 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3842 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3844 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3845 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3846 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3847 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3848 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3849 NFS_CAP_CTIME|NFS_CAP_MTIME|
3850 NFS_CAP_SECURITY_LABEL);
3851 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3852 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3853 server->caps |= NFS_CAP_ACLS;
3854 if (res.has_links != 0)
3855 server->caps |= NFS_CAP_HARDLINKS;
3856 if (res.has_symlinks != 0)
3857 server->caps |= NFS_CAP_SYMLINKS;
3858 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3859 server->caps |= NFS_CAP_FILEID;
3860 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3861 server->caps |= NFS_CAP_MODE;
3862 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3863 server->caps |= NFS_CAP_NLINK;
3864 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3865 server->caps |= NFS_CAP_OWNER;
3866 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3867 server->caps |= NFS_CAP_OWNER_GROUP;
3868 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3869 server->caps |= NFS_CAP_ATIME;
3870 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3871 server->caps |= NFS_CAP_CTIME;
3872 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3873 server->caps |= NFS_CAP_MTIME;
3874 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3875 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3876 server->caps |= NFS_CAP_SECURITY_LABEL;
3878 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3879 sizeof(server->attr_bitmask));
3880 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3882 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3883 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3884 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3885 server->cache_consistency_bitmask[2] = 0;
3887 /* Avoid a regression due to buggy server */
3888 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3889 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3890 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3891 sizeof(server->exclcreat_bitmask));
3893 server->acl_bitmask = res.acl_bitmask;
3894 server->fh_expire_type = res.fh_expire_type;
3900 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3902 struct nfs4_exception exception = {
3903 .interruptible = true,
3907 err = nfs4_handle_exception(server,
3908 _nfs4_server_capabilities(server, fhandle),
3910 } while (exception.retry);
3914 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3915 struct nfs_fsinfo *info)
3918 struct nfs4_lookup_root_arg args = {
3921 struct nfs4_lookup_res res = {
3923 .fattr = info->fattr,
3926 struct rpc_message msg = {
3927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3932 bitmask[0] = nfs4_fattr_bitmap[0];
3933 bitmask[1] = nfs4_fattr_bitmap[1];
3935 * Process the label in the upcoming getfattr
3937 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3939 nfs_fattr_init(info->fattr);
3940 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3943 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3944 struct nfs_fsinfo *info)
3946 struct nfs4_exception exception = {
3947 .interruptible = true,
3951 err = _nfs4_lookup_root(server, fhandle, info);
3952 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3955 case -NFS4ERR_WRONGSEC:
3958 err = nfs4_handle_exception(server, err, &exception);
3960 } while (exception.retry);
3965 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3966 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3968 struct rpc_auth_create_args auth_args = {
3969 .pseudoflavor = flavor,
3971 struct rpc_auth *auth;
3973 auth = rpcauth_create(&auth_args, server->client);
3976 return nfs4_lookup_root(server, fhandle, info);
3980 * Retry pseudoroot lookup with various security flavors. We do this when:
3982 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3983 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3985 * Returns zero on success, or a negative NFS4ERR value, or a
3986 * negative errno value.
3988 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3989 struct nfs_fsinfo *info)
3991 /* Per 3530bis 15.33.5 */
3992 static const rpc_authflavor_t flav_array[] = {
3996 RPC_AUTH_UNIX, /* courtesy */
3999 int status = -EPERM;
4002 if (server->auth_info.flavor_len > 0) {
4003 /* try each flavor specified by user */
4004 for (i = 0; i < server->auth_info.flavor_len; i++) {
4005 status = nfs4_lookup_root_sec(server, fhandle, info,
4006 server->auth_info.flavors[i]);
4007 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4012 /* no flavors specified by user, try default list */
4013 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4014 status = nfs4_lookup_root_sec(server, fhandle, info,
4016 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4023 * -EACCES could mean that the user doesn't have correct permissions
4024 * to access the mount. It could also mean that we tried to mount
4025 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4026 * existing mount programs don't handle -EACCES very well so it should
4027 * be mapped to -EPERM instead.
4029 if (status == -EACCES)
4035 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4036 * @server: initialized nfs_server handle
4037 * @fhandle: we fill in the pseudo-fs root file handle
4038 * @info: we fill in an FSINFO struct
4039 * @auth_probe: probe the auth flavours
4041 * Returns zero on success, or a negative errno.
4043 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4044 struct nfs_fsinfo *info,
4050 status = nfs4_lookup_root(server, fhandle, info);
4052 if (auth_probe || status == NFS4ERR_WRONGSEC)
4053 status = server->nfs_client->cl_mvops->find_root_sec(server,
4057 status = nfs4_server_capabilities(server, fhandle);
4059 status = nfs4_do_fsinfo(server, fhandle, info);
4061 return nfs4_map_errors(status);
4064 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4065 struct nfs_fsinfo *info)
4068 struct nfs_fattr *fattr = info->fattr;
4069 struct nfs4_label *label = fattr->label;
4071 error = nfs4_server_capabilities(server, mntfh);
4073 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4077 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
4079 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4083 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4084 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4085 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4092 * Get locations and (maybe) other attributes of a referral.
4093 * Note that we'll actually follow the referral later when
4094 * we detect fsid mismatch in inode revalidation
4096 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4097 const struct qstr *name, struct nfs_fattr *fattr,
4098 struct nfs_fh *fhandle)
4100 int status = -ENOMEM;
4101 struct page *page = NULL;
4102 struct nfs4_fs_locations *locations = NULL;
4104 page = alloc_page(GFP_KERNEL);
4107 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4108 if (locations == NULL)
4111 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4116 * If the fsid didn't change, this is a migration event, not a
4117 * referral. Cause us to drop into the exception handler, which
4118 * will kick off migration recovery.
4120 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4121 dprintk("%s: server did not return a different fsid for"
4122 " a referral at %s\n", __func__, name->name);
4123 status = -NFS4ERR_MOVED;
4126 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4127 nfs_fixup_referral_attributes(&locations->fattr);
4129 /* replace the lookup nfs_fattr with the locations nfs_fattr */
4130 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4131 memset(fhandle, 0, sizeof(struct nfs_fh));
4139 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4140 struct nfs_fattr *fattr, struct nfs4_label *label,
4141 struct inode *inode)
4143 __u32 bitmask[NFS4_BITMASK_SZ];
4144 struct nfs4_getattr_arg args = {
4148 struct nfs4_getattr_res res = {
4153 struct rpc_message msg = {
4154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4158 unsigned short task_flags = 0;
4160 /* Is this is an attribute revalidation, subject to softreval? */
4161 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4162 task_flags |= RPC_TASK_TIMEOUT;
4164 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
4166 nfs_fattr_init(fattr);
4167 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4168 return nfs4_do_call_sync(server->client, server, &msg,
4169 &args.seq_args, &res.seq_res, task_flags);
4172 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4173 struct nfs_fattr *fattr, struct nfs4_label *label,
4174 struct inode *inode)
4176 struct nfs4_exception exception = {
4177 .interruptible = true,
4181 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4182 trace_nfs4_getattr(server, fhandle, fattr, err);
4183 err = nfs4_handle_exception(server, err,
4185 } while (exception.retry);
4190 * The file is not closed if it is opened due to the a request to change
4191 * the size of the file. The open call will not be needed once the
4192 * VFS layer lookup-intents are implemented.
4194 * Close is called when the inode is destroyed.
4195 * If we haven't opened the file for O_WRONLY, we
4196 * need to in the size_change case to obtain a stateid.
4199 * Because OPEN is always done by name in nfsv4, it is
4200 * possible that we opened a different file by the same
4201 * name. We can recognize this race condition, but we
4202 * can't do anything about it besides returning an error.
4204 * This will be fixed with VFS changes (lookup-intent).
4207 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4208 struct iattr *sattr)
4210 struct inode *inode = d_inode(dentry);
4211 const struct cred *cred = NULL;
4212 struct nfs_open_context *ctx = NULL;
4213 struct nfs4_label *label = NULL;
4216 if (pnfs_ld_layoutret_on_setattr(inode) &&
4217 sattr->ia_valid & ATTR_SIZE &&
4218 sattr->ia_size < i_size_read(inode))
4219 pnfs_commit_and_return_layout(inode);
4221 nfs_fattr_init(fattr);
4223 /* Deal with open(O_TRUNC) */
4224 if (sattr->ia_valid & ATTR_OPEN)
4225 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4227 /* Optimization: if the end result is no change, don't RPC */
4228 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4231 /* Search for an existing open(O_WRITE) file */
4232 if (sattr->ia_valid & ATTR_FILE) {
4234 ctx = nfs_file_open_context(sattr->ia_file);
4239 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4241 return PTR_ERR(label);
4243 /* Return any delegations if we're going to change ACLs */
4244 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4245 nfs4_inode_make_writeable(inode);
4247 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4249 nfs_setattr_update_inode(inode, sattr, fattr);
4250 nfs_setsecurity(inode, fattr, label);
4252 nfs4_label_free(label);
4256 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4257 struct dentry *dentry, struct nfs_fh *fhandle,
4258 struct nfs_fattr *fattr, struct nfs4_label *label)
4260 struct nfs_server *server = NFS_SERVER(dir);
4262 struct nfs4_lookup_arg args = {
4263 .bitmask = server->attr_bitmask,
4264 .dir_fh = NFS_FH(dir),
4265 .name = &dentry->d_name,
4267 struct nfs4_lookup_res res = {
4273 struct rpc_message msg = {
4274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4278 unsigned short task_flags = 0;
4280 /* Is this is an attribute revalidation, subject to softreval? */
4281 if (nfs_lookup_is_soft_revalidate(dentry))
4282 task_flags |= RPC_TASK_TIMEOUT;
4284 args.bitmask = nfs4_bitmask(server, label);
4286 nfs_fattr_init(fattr);
4288 dprintk("NFS call lookup %pd2\n", dentry);
4289 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4290 status = nfs4_do_call_sync(clnt, server, &msg,
4291 &args.seq_args, &res.seq_res, task_flags);
4292 dprintk("NFS reply lookup: %d\n", status);
4296 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4298 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4299 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4300 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4304 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4305 struct dentry *dentry, struct nfs_fh *fhandle,
4306 struct nfs_fattr *fattr, struct nfs4_label *label)
4308 struct nfs4_exception exception = {
4309 .interruptible = true,
4311 struct rpc_clnt *client = *clnt;
4312 const struct qstr *name = &dentry->d_name;
4315 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
4316 trace_nfs4_lookup(dir, name, err);
4318 case -NFS4ERR_BADNAME:
4321 case -NFS4ERR_MOVED:
4322 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4323 if (err == -NFS4ERR_MOVED)
4324 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4326 case -NFS4ERR_WRONGSEC:
4328 if (client != *clnt)
4330 client = nfs4_negotiate_security(client, dir, name);
4332 return PTR_ERR(client);
4334 exception.retry = 1;
4337 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4339 } while (exception.retry);
4344 else if (client != *clnt)
4345 rpc_shutdown_client(client);
4350 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4351 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4352 struct nfs4_label *label)
4355 struct rpc_clnt *client = NFS_CLIENT(dir);
4357 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
4358 if (client != NFS_CLIENT(dir)) {
4359 rpc_shutdown_client(client);
4360 nfs_fixup_secinfo_attributes(fattr);
4366 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4367 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4369 struct rpc_clnt *client = NFS_CLIENT(dir);
4372 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
4374 return ERR_PTR(status);
4375 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4378 static int _nfs4_proc_lookupp(struct inode *inode,
4379 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4380 struct nfs4_label *label)
4382 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4383 struct nfs_server *server = NFS_SERVER(inode);
4385 struct nfs4_lookupp_arg args = {
4386 .bitmask = server->attr_bitmask,
4387 .fh = NFS_FH(inode),
4389 struct nfs4_lookupp_res res = {
4395 struct rpc_message msg = {
4396 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4400 unsigned short task_flags = 0;
4402 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4403 task_flags |= RPC_TASK_TIMEOUT;
4405 args.bitmask = nfs4_bitmask(server, label);
4407 nfs_fattr_init(fattr);
4409 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4410 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4411 &res.seq_res, task_flags);
4412 dprintk("NFS reply lookupp: %d\n", status);
4416 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4417 struct nfs_fattr *fattr, struct nfs4_label *label)
4419 struct nfs4_exception exception = {
4420 .interruptible = true,
4424 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4425 trace_nfs4_lookupp(inode, err);
4426 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4428 } while (exception.retry);
4432 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4434 struct nfs_server *server = NFS_SERVER(inode);
4435 struct nfs4_accessargs args = {
4436 .fh = NFS_FH(inode),
4437 .access = entry->mask,
4439 struct nfs4_accessres res = {
4442 struct rpc_message msg = {
4443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4446 .rpc_cred = entry->cred,
4450 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4451 res.fattr = nfs_alloc_fattr();
4452 if (res.fattr == NULL)
4454 args.bitmask = server->cache_consistency_bitmask;
4456 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4458 nfs_access_set_mask(entry, res.access);
4460 nfs_refresh_inode(inode, res.fattr);
4462 nfs_free_fattr(res.fattr);
4466 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4468 struct nfs4_exception exception = {
4469 .interruptible = true,
4473 err = _nfs4_proc_access(inode, entry);
4474 trace_nfs4_access(inode, err);
4475 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4477 } while (exception.retry);
4482 * TODO: For the time being, we don't try to get any attributes
4483 * along with any of the zero-copy operations READ, READDIR,
4486 * In the case of the first three, we want to put the GETATTR
4487 * after the read-type operation -- this is because it is hard
4488 * to predict the length of a GETATTR response in v4, and thus
4489 * align the READ data correctly. This means that the GETATTR
4490 * may end up partially falling into the page cache, and we should
4491 * shift it into the 'tail' of the xdr_buf before processing.
4492 * To do this efficiently, we need to know the total length
4493 * of data received, which doesn't seem to be available outside
4496 * In the case of WRITE, we also want to put the GETATTR after
4497 * the operation -- in this case because we want to make sure
4498 * we get the post-operation mtime and size.
4500 * Both of these changes to the XDR layer would in fact be quite
4501 * minor, but I decided to leave them for a subsequent patch.
4503 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4504 unsigned int pgbase, unsigned int pglen)
4506 struct nfs4_readlink args = {
4507 .fh = NFS_FH(inode),
4512 struct nfs4_readlink_res res;
4513 struct rpc_message msg = {
4514 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4519 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4522 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4523 unsigned int pgbase, unsigned int pglen)
4525 struct nfs4_exception exception = {
4526 .interruptible = true,
4530 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4531 trace_nfs4_readlink(inode, err);
4532 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4534 } while (exception.retry);
4539 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4542 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4545 struct nfs_server *server = NFS_SERVER(dir);
4546 struct nfs4_label l, *ilabel = NULL;
4547 struct nfs_open_context *ctx;
4548 struct nfs4_state *state;
4551 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4553 return PTR_ERR(ctx);
4555 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4557 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4558 sattr->ia_mode &= ~current_umask();
4559 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4560 if (IS_ERR(state)) {
4561 status = PTR_ERR(state);
4565 nfs4_label_release_security(ilabel);
4566 put_nfs_open_context(ctx);
4571 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4573 struct nfs_server *server = NFS_SERVER(dir);
4574 struct nfs_removeargs args = {
4578 struct nfs_removeres res = {
4581 struct rpc_message msg = {
4582 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4586 unsigned long timestamp = jiffies;
4589 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4591 spin_lock(&dir->i_lock);
4592 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4593 NFS_INO_INVALID_DATA);
4594 /* Removing a directory decrements nlink in the parent */
4595 if (ftype == NF4DIR && dir->i_nlink > 2)
4596 nfs4_dec_nlink_locked(dir);
4597 spin_unlock(&dir->i_lock);
4602 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4604 struct nfs4_exception exception = {
4605 .interruptible = true,
4607 struct inode *inode = d_inode(dentry);
4611 if (inode->i_nlink == 1)
4612 nfs4_inode_return_delegation(inode);
4614 nfs4_inode_make_writeable(inode);
4617 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4618 trace_nfs4_remove(dir, &dentry->d_name, err);
4619 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4621 } while (exception.retry);
4625 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4627 struct nfs4_exception exception = {
4628 .interruptible = true,
4633 err = _nfs4_proc_remove(dir, name, NF4DIR);
4634 trace_nfs4_remove(dir, name, err);
4635 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4637 } while (exception.retry);
4641 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4642 struct dentry *dentry,
4643 struct inode *inode)
4645 struct nfs_removeargs *args = msg->rpc_argp;
4646 struct nfs_removeres *res = msg->rpc_resp;
4648 res->server = NFS_SB(dentry->d_sb);
4649 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4650 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4652 nfs_fattr_init(res->dir_attr);
4655 nfs4_inode_return_delegation(inode);
4658 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4660 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4661 &data->args.seq_args,
4666 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4668 struct nfs_unlinkdata *data = task->tk_calldata;
4669 struct nfs_removeres *res = &data->res;
4671 if (!nfs4_sequence_done(task, &res->seq_res))
4673 if (nfs4_async_handle_error(task, res->server, NULL,
4674 &data->timeout) == -EAGAIN)
4676 if (task->tk_status == 0)
4677 nfs4_update_changeattr(dir, &res->cinfo,
4678 res->dir_attr->time_start,
4679 NFS_INO_INVALID_DATA);
4683 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4684 struct dentry *old_dentry,
4685 struct dentry *new_dentry)
4687 struct nfs_renameargs *arg = msg->rpc_argp;
4688 struct nfs_renameres *res = msg->rpc_resp;
4689 struct inode *old_inode = d_inode(old_dentry);
4690 struct inode *new_inode = d_inode(new_dentry);
4693 nfs4_inode_make_writeable(old_inode);
4695 nfs4_inode_return_delegation(new_inode);
4696 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4697 res->server = NFS_SB(old_dentry->d_sb);
4698 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4701 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4703 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4704 &data->args.seq_args,
4709 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4710 struct inode *new_dir)
4712 struct nfs_renamedata *data = task->tk_calldata;
4713 struct nfs_renameres *res = &data->res;
4715 if (!nfs4_sequence_done(task, &res->seq_res))
4717 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4720 if (task->tk_status == 0) {
4721 if (new_dir != old_dir) {
4722 /* Note: If we moved a directory, nlink will change */
4723 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4724 res->old_fattr->time_start,
4725 NFS_INO_INVALID_OTHER |
4726 NFS_INO_INVALID_DATA);
4727 nfs4_update_changeattr(new_dir, &res->new_cinfo,
4728 res->new_fattr->time_start,
4729 NFS_INO_INVALID_OTHER |
4730 NFS_INO_INVALID_DATA);
4732 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4733 res->old_fattr->time_start,
4734 NFS_INO_INVALID_DATA);
4739 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4741 struct nfs_server *server = NFS_SERVER(inode);
4742 __u32 bitmask[NFS4_BITMASK_SZ];
4743 struct nfs4_link_arg arg = {
4744 .fh = NFS_FH(inode),
4745 .dir_fh = NFS_FH(dir),
4749 struct nfs4_link_res res = {
4753 struct rpc_message msg = {
4754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4758 int status = -ENOMEM;
4760 res.fattr = nfs_alloc_fattr();
4761 if (res.fattr == NULL)
4764 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4765 if (IS_ERR(res.label)) {
4766 status = PTR_ERR(res.label);
4770 nfs4_inode_make_writeable(inode);
4771 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4773 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4775 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4776 NFS_INO_INVALID_DATA);
4777 status = nfs_post_op_update_inode(inode, res.fattr);
4779 nfs_setsecurity(inode, res.fattr, res.label);
4783 nfs4_label_free(res.label);
4786 nfs_free_fattr(res.fattr);
4790 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4792 struct nfs4_exception exception = {
4793 .interruptible = true,
4797 err = nfs4_handle_exception(NFS_SERVER(inode),
4798 _nfs4_proc_link(inode, dir, name),
4800 } while (exception.retry);
4804 struct nfs4_createdata {
4805 struct rpc_message msg;
4806 struct nfs4_create_arg arg;
4807 struct nfs4_create_res res;
4809 struct nfs_fattr fattr;
4810 struct nfs4_label *label;
4813 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4814 const struct qstr *name, struct iattr *sattr, u32 ftype)
4816 struct nfs4_createdata *data;
4818 data = kzalloc(sizeof(*data), GFP_KERNEL);
4820 struct nfs_server *server = NFS_SERVER(dir);
4822 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4823 if (IS_ERR(data->label))
4826 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4827 data->msg.rpc_argp = &data->arg;
4828 data->msg.rpc_resp = &data->res;
4829 data->arg.dir_fh = NFS_FH(dir);
4830 data->arg.server = server;
4831 data->arg.name = name;
4832 data->arg.attrs = sattr;
4833 data->arg.ftype = ftype;
4834 data->arg.bitmask = nfs4_bitmask(server, data->label);
4835 data->arg.umask = current_umask();
4836 data->res.server = server;
4837 data->res.fh = &data->fh;
4838 data->res.fattr = &data->fattr;
4839 data->res.label = data->label;
4840 nfs_fattr_init(data->res.fattr);
4848 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4850 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4851 &data->arg.seq_args, &data->res.seq_res, 1);
4853 spin_lock(&dir->i_lock);
4854 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4855 data->res.fattr->time_start,
4856 NFS_INO_INVALID_DATA);
4857 /* Creating a directory bumps nlink in the parent */
4858 if (data->arg.ftype == NF4DIR)
4859 nfs4_inc_nlink_locked(dir);
4860 spin_unlock(&dir->i_lock);
4861 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4866 static void nfs4_free_createdata(struct nfs4_createdata *data)
4868 nfs4_label_free(data->label);
4872 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4873 struct page *page, unsigned int len, struct iattr *sattr,
4874 struct nfs4_label *label)
4876 struct nfs4_createdata *data;
4877 int status = -ENAMETOOLONG;
4879 if (len > NFS4_MAXPATHLEN)
4883 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4887 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4888 data->arg.u.symlink.pages = &page;
4889 data->arg.u.symlink.len = len;
4890 data->arg.label = label;
4892 status = nfs4_do_create(dir, dentry, data);
4894 nfs4_free_createdata(data);
4899 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4900 struct page *page, unsigned int len, struct iattr *sattr)
4902 struct nfs4_exception exception = {
4903 .interruptible = true,
4905 struct nfs4_label l, *label = NULL;
4908 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4911 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4912 trace_nfs4_symlink(dir, &dentry->d_name, err);
4913 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4915 } while (exception.retry);
4917 nfs4_label_release_security(label);
4921 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4922 struct iattr *sattr, struct nfs4_label *label)
4924 struct nfs4_createdata *data;
4925 int status = -ENOMEM;
4927 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4931 data->arg.label = label;
4932 status = nfs4_do_create(dir, dentry, data);
4934 nfs4_free_createdata(data);
4939 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4940 struct iattr *sattr)
4942 struct nfs_server *server = NFS_SERVER(dir);
4943 struct nfs4_exception exception = {
4944 .interruptible = true,
4946 struct nfs4_label l, *label = NULL;
4949 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4951 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4952 sattr->ia_mode &= ~current_umask();
4954 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4955 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4956 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4958 } while (exception.retry);
4959 nfs4_label_release_security(label);
4964 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
4965 struct nfs_readdir_res *nr_res)
4967 struct inode *dir = d_inode(nr_arg->dentry);
4968 struct nfs_server *server = NFS_SERVER(dir);
4969 struct nfs4_readdir_arg args = {
4971 .pages = nr_arg->pages,
4973 .count = nr_arg->page_len,
4974 .plus = nr_arg->plus,
4976 struct nfs4_readdir_res res;
4977 struct rpc_message msg = {
4978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4981 .rpc_cred = nr_arg->cred,
4985 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
4986 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
4987 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
4988 args.bitmask = server->attr_bitmask_nl;
4990 args.bitmask = server->attr_bitmask;
4992 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
4993 res.pgbase = args.pgbase;
4994 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
4997 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
4998 status += args.pgbase;
5001 nfs_invalidate_atime(dir);
5003 dprintk("%s: returns %d\n", __func__, status);
5007 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5008 struct nfs_readdir_res *res)
5010 struct nfs4_exception exception = {
5011 .interruptible = true,
5015 err = _nfs4_proc_readdir(arg, res);
5016 trace_nfs4_readdir(d_inode(arg->dentry), err);
5017 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5019 } while (exception.retry);
5023 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5024 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5026 struct nfs4_createdata *data;
5027 int mode = sattr->ia_mode;
5028 int status = -ENOMEM;
5030 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5035 data->arg.ftype = NF4FIFO;
5036 else if (S_ISBLK(mode)) {
5037 data->arg.ftype = NF4BLK;
5038 data->arg.u.device.specdata1 = MAJOR(rdev);
5039 data->arg.u.device.specdata2 = MINOR(rdev);
5041 else if (S_ISCHR(mode)) {
5042 data->arg.ftype = NF4CHR;
5043 data->arg.u.device.specdata1 = MAJOR(rdev);
5044 data->arg.u.device.specdata2 = MINOR(rdev);
5045 } else if (!S_ISSOCK(mode)) {
5050 data->arg.label = label;
5051 status = nfs4_do_create(dir, dentry, data);
5053 nfs4_free_createdata(data);
5058 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5059 struct iattr *sattr, dev_t rdev)
5061 struct nfs_server *server = NFS_SERVER(dir);
5062 struct nfs4_exception exception = {
5063 .interruptible = true,
5065 struct nfs4_label l, *label = NULL;
5068 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5070 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5071 sattr->ia_mode &= ~current_umask();
5073 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5074 trace_nfs4_mknod(dir, &dentry->d_name, err);
5075 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5077 } while (exception.retry);
5079 nfs4_label_release_security(label);
5084 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5085 struct nfs_fsstat *fsstat)
5087 struct nfs4_statfs_arg args = {
5089 .bitmask = server->attr_bitmask,
5091 struct nfs4_statfs_res res = {
5094 struct rpc_message msg = {
5095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5100 nfs_fattr_init(fsstat->fattr);
5101 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5104 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5106 struct nfs4_exception exception = {
5107 .interruptible = true,
5111 err = nfs4_handle_exception(server,
5112 _nfs4_proc_statfs(server, fhandle, fsstat),
5114 } while (exception.retry);
5118 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5119 struct nfs_fsinfo *fsinfo)
5121 struct nfs4_fsinfo_arg args = {
5123 .bitmask = server->attr_bitmask,
5125 struct nfs4_fsinfo_res res = {
5128 struct rpc_message msg = {
5129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5134 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5137 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5139 struct nfs4_exception exception = {
5140 .interruptible = true,
5145 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5146 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5148 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5151 err = nfs4_handle_exception(server, err, &exception);
5152 } while (exception.retry);
5156 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5160 nfs_fattr_init(fsinfo->fattr);
5161 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5163 /* block layout checks this! */
5164 server->pnfs_blksize = fsinfo->blksize;
5165 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5171 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5172 struct nfs_pathconf *pathconf)
5174 struct nfs4_pathconf_arg args = {
5176 .bitmask = server->attr_bitmask,
5178 struct nfs4_pathconf_res res = {
5179 .pathconf = pathconf,
5181 struct rpc_message msg = {
5182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5187 /* None of the pathconf attributes are mandatory to implement */
5188 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5189 memset(pathconf, 0, sizeof(*pathconf));
5193 nfs_fattr_init(pathconf->fattr);
5194 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5197 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5198 struct nfs_pathconf *pathconf)
5200 struct nfs4_exception exception = {
5201 .interruptible = true,
5206 err = nfs4_handle_exception(server,
5207 _nfs4_proc_pathconf(server, fhandle, pathconf),
5209 } while (exception.retry);
5213 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5214 const struct nfs_open_context *ctx,
5215 const struct nfs_lock_context *l_ctx,
5218 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5220 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5222 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5223 const struct nfs_open_context *ctx,
5224 const struct nfs_lock_context *l_ctx,
5227 nfs4_stateid _current_stateid;
5229 /* If the current stateid represents a lost lock, then exit */
5230 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5232 return nfs4_stateid_match(stateid, &_current_stateid);
5235 static bool nfs4_error_stateid_expired(int err)
5238 case -NFS4ERR_DELEG_REVOKED:
5239 case -NFS4ERR_ADMIN_REVOKED:
5240 case -NFS4ERR_BAD_STATEID:
5241 case -NFS4ERR_STALE_STATEID:
5242 case -NFS4ERR_OLD_STATEID:
5243 case -NFS4ERR_OPENMODE:
5244 case -NFS4ERR_EXPIRED:
5250 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5252 struct nfs_server *server = NFS_SERVER(hdr->inode);
5254 trace_nfs4_read(hdr, task->tk_status);
5255 if (task->tk_status < 0) {
5256 struct nfs4_exception exception = {
5257 .inode = hdr->inode,
5258 .state = hdr->args.context->state,
5259 .stateid = &hdr->args.stateid,
5261 task->tk_status = nfs4_async_handle_exception(task,
5262 server, task->tk_status, &exception);
5263 if (exception.retry) {
5264 rpc_restart_call_prepare(task);
5269 if (task->tk_status > 0)
5270 renew_lease(server, hdr->timestamp);
5274 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5275 struct nfs_pgio_args *args)
5278 if (!nfs4_error_stateid_expired(task->tk_status) ||
5279 nfs4_stateid_is_current(&args->stateid,
5284 rpc_restart_call_prepare(task);
5288 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5289 struct nfs_pgio_header *hdr)
5291 struct nfs_server *server = NFS_SERVER(hdr->inode);
5292 struct rpc_message *msg = &task->tk_msg;
5294 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5295 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5296 server->caps &= ~NFS_CAP_READ_PLUS;
5297 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5298 rpc_restart_call_prepare(task);
5304 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5306 dprintk("--> %s\n", __func__);
5308 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5310 if (nfs4_read_stateid_changed(task, &hdr->args))
5312 if (nfs4_read_plus_not_supported(task, hdr))
5314 if (task->tk_status > 0)
5315 nfs_invalidate_atime(hdr->inode);
5316 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5317 nfs4_read_done_cb(task, hdr);
5320 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5321 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5322 struct rpc_message *msg)
5324 /* Note: We don't use READ_PLUS with pNFS yet */
5325 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
5326 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5329 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5330 struct rpc_message *msg)
5333 #endif /* CONFIG_NFS_V4_2 */
5335 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5336 struct rpc_message *msg)
5338 hdr->timestamp = jiffies;
5339 if (!hdr->pgio_done_cb)
5340 hdr->pgio_done_cb = nfs4_read_done_cb;
5341 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5342 nfs42_read_plus_support(hdr, msg);
5343 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5346 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5347 struct nfs_pgio_header *hdr)
5349 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5350 &hdr->args.seq_args,
5354 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5355 hdr->args.lock_context,
5356 hdr->rw_mode) == -EIO)
5358 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5363 static int nfs4_write_done_cb(struct rpc_task *task,
5364 struct nfs_pgio_header *hdr)
5366 struct inode *inode = hdr->inode;
5368 trace_nfs4_write(hdr, task->tk_status);
5369 if (task->tk_status < 0) {
5370 struct nfs4_exception exception = {
5371 .inode = hdr->inode,
5372 .state = hdr->args.context->state,
5373 .stateid = &hdr->args.stateid,
5375 task->tk_status = nfs4_async_handle_exception(task,
5376 NFS_SERVER(inode), task->tk_status,
5378 if (exception.retry) {
5379 rpc_restart_call_prepare(task);
5383 if (task->tk_status >= 0) {
5384 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5385 nfs_writeback_update_inode(hdr);
5390 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5391 struct nfs_pgio_args *args)
5394 if (!nfs4_error_stateid_expired(task->tk_status) ||
5395 nfs4_stateid_is_current(&args->stateid,
5400 rpc_restart_call_prepare(task);
5404 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5406 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5408 if (nfs4_write_stateid_changed(task, &hdr->args))
5410 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5411 nfs4_write_done_cb(task, hdr);
5415 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5417 /* Don't request attributes for pNFS or O_DIRECT writes */
5418 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5420 /* Otherwise, request attributes if and only if we don't hold
5423 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5426 static void nfs4_bitmask_adjust(__u32 *bitmask, struct inode *inode,
5427 struct nfs_server *server,
5428 struct nfs4_label *label)
5431 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
5433 if ((cache_validity & NFS_INO_INVALID_DATA) ||
5434 (cache_validity & NFS_INO_REVAL_PAGECACHE) ||
5435 (cache_validity & NFS_INO_REVAL_FORCED) ||
5436 (cache_validity & NFS_INO_INVALID_OTHER))
5437 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
5439 if (cache_validity & NFS_INO_INVALID_ATIME)
5440 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5441 if (cache_validity & NFS_INO_INVALID_ACCESS)
5442 bitmask[0] |= FATTR4_WORD1_MODE | FATTR4_WORD1_OWNER |
5443 FATTR4_WORD1_OWNER_GROUP;
5444 if (cache_validity & NFS_INO_INVALID_ACL)
5445 bitmask[0] |= FATTR4_WORD0_ACL;
5446 if (cache_validity & NFS_INO_INVALID_LABEL)
5447 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
5448 if (cache_validity & NFS_INO_INVALID_CTIME)
5449 bitmask[0] |= FATTR4_WORD0_CHANGE;
5450 if (cache_validity & NFS_INO_INVALID_MTIME)
5451 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5452 if (cache_validity & NFS_INO_INVALID_SIZE)
5453 bitmask[0] |= FATTR4_WORD0_SIZE;
5454 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5455 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5458 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5459 struct rpc_message *msg,
5460 struct rpc_clnt **clnt)
5462 struct nfs_server *server = NFS_SERVER(hdr->inode);
5464 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5465 hdr->args.bitmask = NULL;
5466 hdr->res.fattr = NULL;
5468 hdr->args.bitmask = server->cache_consistency_bitmask;
5469 nfs4_bitmask_adjust(hdr->args.bitmask, hdr->inode, server, NULL);
5472 if (!hdr->pgio_done_cb)
5473 hdr->pgio_done_cb = nfs4_write_done_cb;
5474 hdr->res.server = server;
5475 hdr->timestamp = jiffies;
5477 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5478 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5479 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5482 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5484 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5485 &data->args.seq_args,
5490 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5492 struct inode *inode = data->inode;
5494 trace_nfs4_commit(data, task->tk_status);
5495 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5496 NULL, NULL) == -EAGAIN) {
5497 rpc_restart_call_prepare(task);
5503 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5505 if (!nfs4_sequence_done(task, &data->res.seq_res))
5507 return data->commit_done_cb(task, data);
5510 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5511 struct rpc_clnt **clnt)
5513 struct nfs_server *server = NFS_SERVER(data->inode);
5515 if (data->commit_done_cb == NULL)
5516 data->commit_done_cb = nfs4_commit_done_cb;
5517 data->res.server = server;
5518 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5519 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5520 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5523 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5524 struct nfs_commitres *res)
5526 struct inode *dst_inode = file_inode(dst);
5527 struct nfs_server *server = NFS_SERVER(dst_inode);
5528 struct rpc_message msg = {
5529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5534 args->fh = NFS_FH(dst_inode);
5535 return nfs4_call_sync(server->client, server, &msg,
5536 &args->seq_args, &res->seq_res, 1);
5539 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5541 struct nfs_commitargs args = {
5545 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5546 struct nfs4_exception exception = { };
5550 status = _nfs4_proc_commit(dst, &args, res);
5551 status = nfs4_handle_exception(dst_server, status, &exception);
5552 } while (exception.retry);
5557 struct nfs4_renewdata {
5558 struct nfs_client *client;
5559 unsigned long timestamp;
5563 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5564 * standalone procedure for queueing an asynchronous RENEW.
5566 static void nfs4_renew_release(void *calldata)
5568 struct nfs4_renewdata *data = calldata;
5569 struct nfs_client *clp = data->client;
5571 if (refcount_read(&clp->cl_count) > 1)
5572 nfs4_schedule_state_renewal(clp);
5573 nfs_put_client(clp);
5577 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5579 struct nfs4_renewdata *data = calldata;
5580 struct nfs_client *clp = data->client;
5581 unsigned long timestamp = data->timestamp;
5583 trace_nfs4_renew_async(clp, task->tk_status);
5584 switch (task->tk_status) {
5587 case -NFS4ERR_LEASE_MOVED:
5588 nfs4_schedule_lease_moved_recovery(clp);
5591 /* Unless we're shutting down, schedule state recovery! */
5592 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5594 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5595 nfs4_schedule_lease_recovery(clp);
5598 nfs4_schedule_path_down_recovery(clp);
5600 do_renew_lease(clp, timestamp);
5603 static const struct rpc_call_ops nfs4_renew_ops = {
5604 .rpc_call_done = nfs4_renew_done,
5605 .rpc_release = nfs4_renew_release,
5608 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5610 struct rpc_message msg = {
5611 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5615 struct nfs4_renewdata *data;
5617 if (renew_flags == 0)
5619 if (!refcount_inc_not_zero(&clp->cl_count))
5621 data = kmalloc(sizeof(*data), GFP_NOFS);
5623 nfs_put_client(clp);
5627 data->timestamp = jiffies;
5628 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5629 &nfs4_renew_ops, data);
5632 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5634 struct rpc_message msg = {
5635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5639 unsigned long now = jiffies;
5642 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5645 do_renew_lease(clp, now);
5649 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5651 return server->caps & NFS_CAP_ACLS;
5654 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5655 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5658 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5660 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5661 struct page **pages)
5663 struct page *newpage, **spages;
5669 len = min_t(size_t, PAGE_SIZE, buflen);
5670 newpage = alloc_page(GFP_KERNEL);
5672 if (newpage == NULL)
5674 memcpy(page_address(newpage), buf, len);
5679 } while (buflen != 0);
5685 __free_page(spages[rc-1]);
5689 struct nfs4_cached_acl {
5695 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5697 struct nfs_inode *nfsi = NFS_I(inode);
5699 spin_lock(&inode->i_lock);
5700 kfree(nfsi->nfs4_acl);
5701 nfsi->nfs4_acl = acl;
5702 spin_unlock(&inode->i_lock);
5705 static void nfs4_zap_acl_attr(struct inode *inode)
5707 nfs4_set_cached_acl(inode, NULL);
5710 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5712 struct nfs_inode *nfsi = NFS_I(inode);
5713 struct nfs4_cached_acl *acl;
5716 spin_lock(&inode->i_lock);
5717 acl = nfsi->nfs4_acl;
5720 if (buf == NULL) /* user is just asking for length */
5722 if (acl->cached == 0)
5724 ret = -ERANGE; /* see getxattr(2) man page */
5725 if (acl->len > buflen)
5727 memcpy(buf, acl->data, acl->len);
5731 spin_unlock(&inode->i_lock);
5735 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5737 struct nfs4_cached_acl *acl;
5738 size_t buflen = sizeof(*acl) + acl_len;
5740 if (buflen <= PAGE_SIZE) {
5741 acl = kmalloc(buflen, GFP_KERNEL);
5745 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5747 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5754 nfs4_set_cached_acl(inode, acl);
5758 * The getxattr API returns the required buffer length when called with a
5759 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5760 * the required buf. On a NULL buf, we send a page of data to the server
5761 * guessing that the ACL request can be serviced by a page. If so, we cache
5762 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5763 * the cache. If not so, we throw away the page, and cache the required
5764 * length. The next getxattr call will then produce another round trip to
5765 * the server, this time with the input buf of the required size.
5767 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5769 struct page **pages;
5770 struct nfs_getaclargs args = {
5771 .fh = NFS_FH(inode),
5774 struct nfs_getaclres res = {
5777 struct rpc_message msg = {
5778 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5782 unsigned int npages;
5783 int ret = -ENOMEM, i;
5784 struct nfs_server *server = NFS_SERVER(inode);
5787 buflen = server->rsize;
5789 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5790 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5794 args.acl_pages = pages;
5796 for (i = 0; i < npages; i++) {
5797 pages[i] = alloc_page(GFP_KERNEL);
5802 /* for decoding across pages */
5803 res.acl_scratch = alloc_page(GFP_KERNEL);
5804 if (!res.acl_scratch)
5807 args.acl_len = npages * PAGE_SIZE;
5809 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5810 __func__, buf, buflen, npages, args.acl_len);
5811 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5812 &msg, &args.seq_args, &res.seq_res, 0);
5816 /* Handle the case where the passed-in buffer is too short */
5817 if (res.acl_flags & NFS4_ACL_TRUNC) {
5818 /* Did the user only issue a request for the acl length? */
5824 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5826 if (res.acl_len > buflen) {
5830 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5835 for (i = 0; i < npages; i++)
5837 __free_page(pages[i]);
5838 if (res.acl_scratch)
5839 __free_page(res.acl_scratch);
5844 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5846 struct nfs4_exception exception = {
5847 .interruptible = true,
5851 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5852 trace_nfs4_get_acl(inode, ret);
5855 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5856 } while (exception.retry);
5860 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5862 struct nfs_server *server = NFS_SERVER(inode);
5865 if (!nfs4_server_supports_acls(server))
5867 ret = nfs_revalidate_inode(server, inode);
5870 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5871 nfs_zap_acl_cache(inode);
5872 ret = nfs4_read_cached_acl(inode, buf, buflen);
5874 /* -ENOENT is returned if there is no ACL or if there is an ACL
5875 * but no cached acl data, just the acl length */
5877 return nfs4_get_acl_uncached(inode, buf, buflen);
5880 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5882 struct nfs_server *server = NFS_SERVER(inode);
5883 struct page *pages[NFS4ACL_MAXPAGES];
5884 struct nfs_setaclargs arg = {
5885 .fh = NFS_FH(inode),
5889 struct nfs_setaclres res;
5890 struct rpc_message msg = {
5891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5895 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5898 if (!nfs4_server_supports_acls(server))
5900 if (npages > ARRAY_SIZE(pages))
5902 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5905 nfs4_inode_make_writeable(inode);
5906 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5909 * Free each page after tx, so the only ref left is
5910 * held by the network stack
5913 put_page(pages[i-1]);
5916 * Acl update can result in inode attribute update.
5917 * so mark the attribute cache invalid.
5919 spin_lock(&inode->i_lock);
5920 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5921 | NFS_INO_INVALID_CTIME
5922 | NFS_INO_REVAL_FORCED;
5923 spin_unlock(&inode->i_lock);
5924 nfs_access_zap_cache(inode);
5925 nfs_zap_acl_cache(inode);
5929 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5931 struct nfs4_exception exception = { };
5934 err = __nfs4_proc_set_acl(inode, buf, buflen);
5935 trace_nfs4_set_acl(inode, err);
5936 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5938 } while (exception.retry);
5942 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5943 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5946 struct nfs_server *server = NFS_SERVER(inode);
5947 struct nfs_fattr fattr;
5948 struct nfs4_label label = {0, 0, buflen, buf};
5950 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5951 struct nfs4_getattr_arg arg = {
5952 .fh = NFS_FH(inode),
5955 struct nfs4_getattr_res res = {
5960 struct rpc_message msg = {
5961 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5967 nfs_fattr_init(&fattr);
5969 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5972 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5977 static int nfs4_get_security_label(struct inode *inode, void *buf,
5980 struct nfs4_exception exception = {
5981 .interruptible = true,
5985 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5989 err = _nfs4_get_security_label(inode, buf, buflen);
5990 trace_nfs4_get_security_label(inode, err);
5991 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5993 } while (exception.retry);
5997 static int _nfs4_do_set_security_label(struct inode *inode,
5998 struct nfs4_label *ilabel,
5999 struct nfs_fattr *fattr,
6000 struct nfs4_label *olabel)
6003 struct iattr sattr = {0};
6004 struct nfs_server *server = NFS_SERVER(inode);
6005 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6006 struct nfs_setattrargs arg = {
6007 .fh = NFS_FH(inode),
6013 struct nfs_setattrres res = {
6018 struct rpc_message msg = {
6019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6025 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6027 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6029 dprintk("%s failed: %d\n", __func__, status);
6034 static int nfs4_do_set_security_label(struct inode *inode,
6035 struct nfs4_label *ilabel,
6036 struct nfs_fattr *fattr,
6037 struct nfs4_label *olabel)
6039 struct nfs4_exception exception = { };
6043 err = _nfs4_do_set_security_label(inode, ilabel,
6045 trace_nfs4_set_security_label(inode, err);
6046 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6048 } while (exception.retry);
6053 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6055 struct nfs4_label ilabel, *olabel = NULL;
6056 struct nfs_fattr fattr;
6059 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6062 nfs_fattr_init(&fattr);
6066 ilabel.label = (char *)buf;
6067 ilabel.len = buflen;
6069 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6070 if (IS_ERR(olabel)) {
6071 status = -PTR_ERR(olabel);
6075 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6077 nfs_setsecurity(inode, &fattr, olabel);
6079 nfs4_label_free(olabel);
6083 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6086 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6087 nfs4_verifier *bootverf)
6091 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6092 /* An impossible timestamp guarantees this value
6093 * will never match a generated boot time. */
6094 verf[0] = cpu_to_be32(U32_MAX);
6095 verf[1] = cpu_to_be32(U32_MAX);
6097 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6098 u64 ns = ktime_to_ns(nn->boot_time);
6100 verf[0] = cpu_to_be32(ns >> 32);
6101 verf[1] = cpu_to_be32(ns);
6103 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6107 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6109 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6110 struct nfs_netns_client *nn_clp = nn->nfs_client;
6117 id = rcu_dereference(nn_clp->identifier);
6119 strscpy(buf, id, buflen);
6123 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6124 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6130 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6132 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6137 if (clp->cl_owner_id != NULL)
6142 strlen(clp->cl_rpcclient->cl_nodename) +
6144 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6148 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6152 if (len > NFS4_OPAQUE_LIMIT + 1)
6156 * Since this string is allocated at mount time, and held until the
6157 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6158 * about a memory-reclaim deadlock.
6160 str = kmalloc(len, GFP_KERNEL);
6166 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6167 clp->cl_rpcclient->cl_nodename, buf,
6168 rpc_peeraddr2str(clp->cl_rpcclient,
6171 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6172 clp->cl_rpcclient->cl_nodename,
6173 rpc_peeraddr2str(clp->cl_rpcclient,
6177 clp->cl_owner_id = str;
6182 nfs4_init_uniform_client_string(struct nfs_client *clp)
6184 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6189 if (clp->cl_owner_id != NULL)
6192 len = 10 + 10 + 1 + 10 + 1 +
6193 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6195 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6199 if (len > NFS4_OPAQUE_LIMIT + 1)
6203 * Since this string is allocated at mount time, and held until the
6204 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6205 * about a memory-reclaim deadlock.
6207 str = kmalloc(len, GFP_KERNEL);
6212 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6213 clp->rpc_ops->version, clp->cl_minorversion,
6214 buf, clp->cl_rpcclient->cl_nodename);
6216 scnprintf(str, len, "Linux NFSv%u.%u %s",
6217 clp->rpc_ops->version, clp->cl_minorversion,
6218 clp->cl_rpcclient->cl_nodename);
6219 clp->cl_owner_id = str;
6224 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6225 * services. Advertise one based on the address family of the
6229 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6231 if (strchr(clp->cl_ipaddr, ':') != NULL)
6232 return scnprintf(buf, len, "tcp6");
6234 return scnprintf(buf, len, "tcp");
6237 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6239 struct nfs4_setclientid *sc = calldata;
6241 if (task->tk_status == 0)
6242 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6245 static const struct rpc_call_ops nfs4_setclientid_ops = {
6246 .rpc_call_done = nfs4_setclientid_done,
6250 * nfs4_proc_setclientid - Negotiate client ID
6251 * @clp: state data structure
6252 * @program: RPC program for NFSv4 callback service
6253 * @port: IP port number for NFS4 callback service
6254 * @cred: credential to use for this call
6255 * @res: where to place the result
6257 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6259 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6260 unsigned short port, const struct cred *cred,
6261 struct nfs4_setclientid_res *res)
6263 nfs4_verifier sc_verifier;
6264 struct nfs4_setclientid setclientid = {
6265 .sc_verifier = &sc_verifier,
6269 struct rpc_message msg = {
6270 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6271 .rpc_argp = &setclientid,
6275 struct rpc_task_setup task_setup_data = {
6276 .rpc_client = clp->cl_rpcclient,
6277 .rpc_message = &msg,
6278 .callback_ops = &nfs4_setclientid_ops,
6279 .callback_data = &setclientid,
6280 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6282 unsigned long now = jiffies;
6285 /* nfs_client_id4 */
6286 nfs4_init_boot_verifier(clp, &sc_verifier);
6288 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6289 status = nfs4_init_uniform_client_string(clp);
6291 status = nfs4_init_nonuniform_client_string(clp);
6297 setclientid.sc_netid_len =
6298 nfs4_init_callback_netid(clp,
6299 setclientid.sc_netid,
6300 sizeof(setclientid.sc_netid));
6301 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6302 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6303 clp->cl_ipaddr, port >> 8, port & 255);
6305 dprintk("NFS call setclientid auth=%s, '%s'\n",
6306 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6309 status = nfs4_call_sync_custom(&task_setup_data);
6310 if (setclientid.sc_cred) {
6311 kfree(clp->cl_acceptor);
6312 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6313 put_rpccred(setclientid.sc_cred);
6317 do_renew_lease(clp, now);
6319 trace_nfs4_setclientid(clp, status);
6320 dprintk("NFS reply setclientid: %d\n", status);
6325 * nfs4_proc_setclientid_confirm - Confirm client ID
6326 * @clp: state data structure
6327 * @arg: result of a previous SETCLIENTID
6328 * @cred: credential to use for this call
6330 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6332 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6333 struct nfs4_setclientid_res *arg,
6334 const struct cred *cred)
6336 struct rpc_message msg = {
6337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6343 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6344 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6346 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6347 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6348 trace_nfs4_setclientid_confirm(clp, status);
6349 dprintk("NFS reply setclientid_confirm: %d\n", status);
6353 struct nfs4_delegreturndata {
6354 struct nfs4_delegreturnargs args;
6355 struct nfs4_delegreturnres res;
6357 nfs4_stateid stateid;
6358 unsigned long timestamp;
6360 struct nfs4_layoutreturn_args arg;
6361 struct nfs4_layoutreturn_res res;
6362 struct nfs4_xdr_opaque_data ld_private;
6366 struct nfs_fattr fattr;
6368 struct inode *inode;
6371 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6373 struct nfs4_delegreturndata *data = calldata;
6374 struct nfs4_exception exception = {
6375 .inode = data->inode,
6376 .stateid = &data->stateid,
6379 if (!nfs4_sequence_done(task, &data->res.seq_res))
6382 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6384 /* Handle Layoutreturn errors */
6385 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6386 &data->res.lr_ret) == -EAGAIN)
6389 switch (task->tk_status) {
6391 renew_lease(data->res.server, data->timestamp);
6393 case -NFS4ERR_ADMIN_REVOKED:
6394 case -NFS4ERR_DELEG_REVOKED:
6395 case -NFS4ERR_EXPIRED:
6396 nfs4_free_revoked_stateid(data->res.server,
6398 task->tk_msg.rpc_cred);
6400 case -NFS4ERR_BAD_STATEID:
6401 case -NFS4ERR_STALE_STATEID:
6403 task->tk_status = 0;
6405 case -NFS4ERR_OLD_STATEID:
6406 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6407 nfs4_stateid_seqid_inc(&data->stateid);
6408 if (data->args.bitmask) {
6409 data->args.bitmask = NULL;
6410 data->res.fattr = NULL;
6413 case -NFS4ERR_ACCESS:
6414 if (data->args.bitmask) {
6415 data->args.bitmask = NULL;
6416 data->res.fattr = NULL;
6421 task->tk_status = nfs4_async_handle_exception(task,
6422 data->res.server, task->tk_status,
6424 if (exception.retry)
6427 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6428 data->rpc_status = task->tk_status;
6431 task->tk_status = 0;
6432 rpc_restart_call_prepare(task);
6435 static void nfs4_delegreturn_release(void *calldata)
6437 struct nfs4_delegreturndata *data = calldata;
6438 struct inode *inode = data->inode;
6441 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6444 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6445 nfs_iput_and_deactive(inode);
6450 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6452 struct nfs4_delegreturndata *d_data;
6453 struct pnfs_layout_hdr *lo;
6455 d_data = (struct nfs4_delegreturndata *)data;
6457 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6458 nfs4_sequence_done(task, &d_data->res.seq_res);
6462 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6463 if (lo && !pnfs_layout_is_valid(lo)) {
6464 d_data->args.lr_args = NULL;
6465 d_data->res.lr_res = NULL;
6468 nfs4_setup_sequence(d_data->res.server->nfs_client,
6469 &d_data->args.seq_args,
6470 &d_data->res.seq_res,
6474 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6475 .rpc_call_prepare = nfs4_delegreturn_prepare,
6476 .rpc_call_done = nfs4_delegreturn_done,
6477 .rpc_release = nfs4_delegreturn_release,
6480 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6482 struct nfs4_delegreturndata *data;
6483 struct nfs_server *server = NFS_SERVER(inode);
6484 struct rpc_task *task;
6485 struct rpc_message msg = {
6486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6489 struct rpc_task_setup task_setup_data = {
6490 .rpc_client = server->client,
6491 .rpc_message = &msg,
6492 .callback_ops = &nfs4_delegreturn_ops,
6493 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6497 data = kzalloc(sizeof(*data), GFP_NOFS);
6500 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6502 nfs4_state_protect(server->nfs_client,
6503 NFS_SP4_MACH_CRED_CLEANUP,
6504 &task_setup_data.rpc_client, &msg);
6506 data->args.fhandle = &data->fh;
6507 data->args.stateid = &data->stateid;
6508 data->args.bitmask = server->cache_consistency_bitmask;
6509 nfs4_bitmask_adjust(data->args.bitmask, inode, server, NULL);
6510 nfs_copy_fh(&data->fh, NFS_FH(inode));
6511 nfs4_stateid_copy(&data->stateid, stateid);
6512 data->res.fattr = &data->fattr;
6513 data->res.server = server;
6514 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6515 data->lr.arg.ld_private = &data->lr.ld_private;
6516 nfs_fattr_init(data->res.fattr);
6517 data->timestamp = jiffies;
6518 data->rpc_status = 0;
6519 data->inode = nfs_igrab_and_active(inode);
6520 if (data->inode || issync) {
6521 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6524 data->args.lr_args = &data->lr.arg;
6525 data->res.lr_res = &data->lr.res;
6529 task_setup_data.callback_data = data;
6530 msg.rpc_argp = &data->args;
6531 msg.rpc_resp = &data->res;
6532 task = rpc_run_task(&task_setup_data);
6534 return PTR_ERR(task);
6537 status = rpc_wait_for_completion_task(task);
6540 status = data->rpc_status;
6546 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6548 struct nfs_server *server = NFS_SERVER(inode);
6549 struct nfs4_exception exception = { };
6552 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6553 trace_nfs4_delegreturn(inode, stateid, err);
6555 case -NFS4ERR_STALE_STATEID:
6556 case -NFS4ERR_EXPIRED:
6560 err = nfs4_handle_exception(server, err, &exception);
6561 } while (exception.retry);
6565 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6567 struct inode *inode = state->inode;
6568 struct nfs_server *server = NFS_SERVER(inode);
6569 struct nfs_client *clp = server->nfs_client;
6570 struct nfs_lockt_args arg = {
6571 .fh = NFS_FH(inode),
6574 struct nfs_lockt_res res = {
6577 struct rpc_message msg = {
6578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6581 .rpc_cred = state->owner->so_cred,
6583 struct nfs4_lock_state *lsp;
6586 arg.lock_owner.clientid = clp->cl_clientid;
6587 status = nfs4_set_lock_state(state, request);
6590 lsp = request->fl_u.nfs4_fl.owner;
6591 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6592 arg.lock_owner.s_dev = server->s_dev;
6593 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6596 request->fl_type = F_UNLCK;
6598 case -NFS4ERR_DENIED:
6601 request->fl_ops->fl_release_private(request);
6602 request->fl_ops = NULL;
6607 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6609 struct nfs4_exception exception = {
6610 .interruptible = true,
6615 err = _nfs4_proc_getlk(state, cmd, request);
6616 trace_nfs4_get_lock(request, state, cmd, err);
6617 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6619 } while (exception.retry);
6624 * Update the seqid of a lock stateid after receiving
6625 * NFS4ERR_OLD_STATEID
6627 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6628 struct nfs4_lock_state *lsp)
6630 struct nfs4_state *state = lsp->ls_state;
6633 spin_lock(&state->state_lock);
6634 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6636 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6637 nfs4_stateid_seqid_inc(dst);
6639 dst->seqid = lsp->ls_stateid.seqid;
6642 spin_unlock(&state->state_lock);
6646 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6647 struct nfs4_lock_state *lsp)
6649 struct nfs4_state *state = lsp->ls_state;
6652 spin_lock(&state->state_lock);
6653 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6654 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6655 spin_unlock(&state->state_lock);
6659 struct nfs4_unlockdata {
6660 struct nfs_locku_args arg;
6661 struct nfs_locku_res res;
6662 struct nfs4_lock_state *lsp;
6663 struct nfs_open_context *ctx;
6664 struct nfs_lock_context *l_ctx;
6665 struct file_lock fl;
6666 struct nfs_server *server;
6667 unsigned long timestamp;
6670 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6671 struct nfs_open_context *ctx,
6672 struct nfs4_lock_state *lsp,
6673 struct nfs_seqid *seqid)
6675 struct nfs4_unlockdata *p;
6676 struct nfs4_state *state = lsp->ls_state;
6677 struct inode *inode = state->inode;
6679 p = kzalloc(sizeof(*p), GFP_NOFS);
6682 p->arg.fh = NFS_FH(inode);
6684 p->arg.seqid = seqid;
6685 p->res.seqid = seqid;
6687 /* Ensure we don't close file until we're done freeing locks! */
6688 p->ctx = get_nfs_open_context(ctx);
6689 p->l_ctx = nfs_get_lock_context(ctx);
6690 locks_init_lock(&p->fl);
6691 locks_copy_lock(&p->fl, fl);
6692 p->server = NFS_SERVER(inode);
6693 spin_lock(&state->state_lock);
6694 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6695 spin_unlock(&state->state_lock);
6699 static void nfs4_locku_release_calldata(void *data)
6701 struct nfs4_unlockdata *calldata = data;
6702 nfs_free_seqid(calldata->arg.seqid);
6703 nfs4_put_lock_state(calldata->lsp);
6704 nfs_put_lock_context(calldata->l_ctx);
6705 put_nfs_open_context(calldata->ctx);
6709 static void nfs4_locku_done(struct rpc_task *task, void *data)
6711 struct nfs4_unlockdata *calldata = data;
6712 struct nfs4_exception exception = {
6713 .inode = calldata->lsp->ls_state->inode,
6714 .stateid = &calldata->arg.stateid,
6717 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6719 switch (task->tk_status) {
6721 renew_lease(calldata->server, calldata->timestamp);
6722 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6723 if (nfs4_update_lock_stateid(calldata->lsp,
6724 &calldata->res.stateid))
6727 case -NFS4ERR_ADMIN_REVOKED:
6728 case -NFS4ERR_EXPIRED:
6729 nfs4_free_revoked_stateid(calldata->server,
6730 &calldata->arg.stateid,
6731 task->tk_msg.rpc_cred);
6733 case -NFS4ERR_BAD_STATEID:
6734 case -NFS4ERR_STALE_STATEID:
6735 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6737 rpc_restart_call_prepare(task);
6739 case -NFS4ERR_OLD_STATEID:
6740 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6742 rpc_restart_call_prepare(task);
6745 task->tk_status = nfs4_async_handle_exception(task,
6746 calldata->server, task->tk_status,
6748 if (exception.retry)
6749 rpc_restart_call_prepare(task);
6751 nfs_release_seqid(calldata->arg.seqid);
6754 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6756 struct nfs4_unlockdata *calldata = data;
6758 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6759 nfs_async_iocounter_wait(task, calldata->l_ctx))
6762 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6764 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6765 /* Note: exit _without_ running nfs4_locku_done */
6768 calldata->timestamp = jiffies;
6769 if (nfs4_setup_sequence(calldata->server->nfs_client,
6770 &calldata->arg.seq_args,
6771 &calldata->res.seq_res,
6773 nfs_release_seqid(calldata->arg.seqid);
6776 task->tk_action = NULL;
6778 nfs4_sequence_done(task, &calldata->res.seq_res);
6781 static const struct rpc_call_ops nfs4_locku_ops = {
6782 .rpc_call_prepare = nfs4_locku_prepare,
6783 .rpc_call_done = nfs4_locku_done,
6784 .rpc_release = nfs4_locku_release_calldata,
6787 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6788 struct nfs_open_context *ctx,
6789 struct nfs4_lock_state *lsp,
6790 struct nfs_seqid *seqid)
6792 struct nfs4_unlockdata *data;
6793 struct rpc_message msg = {
6794 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6795 .rpc_cred = ctx->cred,
6797 struct rpc_task_setup task_setup_data = {
6798 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6799 .rpc_message = &msg,
6800 .callback_ops = &nfs4_locku_ops,
6801 .workqueue = nfsiod_workqueue,
6802 .flags = RPC_TASK_ASYNC,
6805 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6806 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6808 /* Ensure this is an unlock - when canceling a lock, the
6809 * canceled lock is passed in, and it won't be an unlock.
6811 fl->fl_type = F_UNLCK;
6812 if (fl->fl_flags & FL_CLOSE)
6813 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6815 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6817 nfs_free_seqid(seqid);
6818 return ERR_PTR(-ENOMEM);
6821 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6822 msg.rpc_argp = &data->arg;
6823 msg.rpc_resp = &data->res;
6824 task_setup_data.callback_data = data;
6825 return rpc_run_task(&task_setup_data);
6828 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6830 struct inode *inode = state->inode;
6831 struct nfs4_state_owner *sp = state->owner;
6832 struct nfs_inode *nfsi = NFS_I(inode);
6833 struct nfs_seqid *seqid;
6834 struct nfs4_lock_state *lsp;
6835 struct rpc_task *task;
6836 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6838 unsigned char fl_flags = request->fl_flags;
6840 status = nfs4_set_lock_state(state, request);
6841 /* Unlock _before_ we do the RPC call */
6842 request->fl_flags |= FL_EXISTS;
6843 /* Exclude nfs_delegation_claim_locks() */
6844 mutex_lock(&sp->so_delegreturn_mutex);
6845 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6846 down_read(&nfsi->rwsem);
6847 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6848 up_read(&nfsi->rwsem);
6849 mutex_unlock(&sp->so_delegreturn_mutex);
6852 up_read(&nfsi->rwsem);
6853 mutex_unlock(&sp->so_delegreturn_mutex);
6856 /* Is this a delegated lock? */
6857 lsp = request->fl_u.nfs4_fl.owner;
6858 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6860 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6861 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6865 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6866 status = PTR_ERR(task);
6869 status = rpc_wait_for_completion_task(task);
6872 request->fl_flags = fl_flags;
6873 trace_nfs4_unlock(request, state, F_SETLK, status);
6877 struct nfs4_lockdata {
6878 struct nfs_lock_args arg;
6879 struct nfs_lock_res res;
6880 struct nfs4_lock_state *lsp;
6881 struct nfs_open_context *ctx;
6882 struct file_lock fl;
6883 unsigned long timestamp;
6886 struct nfs_server *server;
6889 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6890 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6893 struct nfs4_lockdata *p;
6894 struct inode *inode = lsp->ls_state->inode;
6895 struct nfs_server *server = NFS_SERVER(inode);
6896 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6898 p = kzalloc(sizeof(*p), gfp_mask);
6902 p->arg.fh = NFS_FH(inode);
6904 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6905 if (IS_ERR(p->arg.open_seqid))
6907 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6908 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6909 if (IS_ERR(p->arg.lock_seqid))
6910 goto out_free_seqid;
6911 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6912 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6913 p->arg.lock_owner.s_dev = server->s_dev;
6914 p->res.lock_seqid = p->arg.lock_seqid;
6917 p->ctx = get_nfs_open_context(ctx);
6918 locks_init_lock(&p->fl);
6919 locks_copy_lock(&p->fl, fl);
6922 nfs_free_seqid(p->arg.open_seqid);
6928 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6930 struct nfs4_lockdata *data = calldata;
6931 struct nfs4_state *state = data->lsp->ls_state;
6933 dprintk("%s: begin!\n", __func__);
6934 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6936 /* Do we need to do an open_to_lock_owner? */
6937 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6938 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6939 goto out_release_lock_seqid;
6941 nfs4_stateid_copy(&data->arg.open_stateid,
6942 &state->open_stateid);
6943 data->arg.new_lock_owner = 1;
6944 data->res.open_seqid = data->arg.open_seqid;
6946 data->arg.new_lock_owner = 0;
6947 nfs4_stateid_copy(&data->arg.lock_stateid,
6948 &data->lsp->ls_stateid);
6950 if (!nfs4_valid_open_stateid(state)) {
6951 data->rpc_status = -EBADF;
6952 task->tk_action = NULL;
6953 goto out_release_open_seqid;
6955 data->timestamp = jiffies;
6956 if (nfs4_setup_sequence(data->server->nfs_client,
6957 &data->arg.seq_args,
6961 out_release_open_seqid:
6962 nfs_release_seqid(data->arg.open_seqid);
6963 out_release_lock_seqid:
6964 nfs_release_seqid(data->arg.lock_seqid);
6966 nfs4_sequence_done(task, &data->res.seq_res);
6967 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6970 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6972 struct nfs4_lockdata *data = calldata;
6973 struct nfs4_lock_state *lsp = data->lsp;
6975 dprintk("%s: begin!\n", __func__);
6977 if (!nfs4_sequence_done(task, &data->res.seq_res))
6980 data->rpc_status = task->tk_status;
6981 switch (task->tk_status) {
6983 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6985 if (data->arg.new_lock && !data->cancelled) {
6986 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6987 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6990 if (data->arg.new_lock_owner != 0) {
6991 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6992 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6993 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6994 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6997 case -NFS4ERR_BAD_STATEID:
6998 case -NFS4ERR_OLD_STATEID:
6999 case -NFS4ERR_STALE_STATEID:
7000 case -NFS4ERR_EXPIRED:
7001 if (data->arg.new_lock_owner != 0) {
7002 if (!nfs4_stateid_match(&data->arg.open_stateid,
7003 &lsp->ls_state->open_stateid))
7005 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7010 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
7013 if (!data->cancelled)
7014 rpc_restart_call_prepare(task);
7018 static void nfs4_lock_release(void *calldata)
7020 struct nfs4_lockdata *data = calldata;
7022 dprintk("%s: begin!\n", __func__);
7023 nfs_free_seqid(data->arg.open_seqid);
7024 if (data->cancelled && data->rpc_status == 0) {
7025 struct rpc_task *task;
7026 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7027 data->arg.lock_seqid);
7029 rpc_put_task_async(task);
7030 dprintk("%s: cancelling lock!\n", __func__);
7032 nfs_free_seqid(data->arg.lock_seqid);
7033 nfs4_put_lock_state(data->lsp);
7034 put_nfs_open_context(data->ctx);
7036 dprintk("%s: done!\n", __func__);
7039 static const struct rpc_call_ops nfs4_lock_ops = {
7040 .rpc_call_prepare = nfs4_lock_prepare,
7041 .rpc_call_done = nfs4_lock_done,
7042 .rpc_release = nfs4_lock_release,
7045 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7048 case -NFS4ERR_ADMIN_REVOKED:
7049 case -NFS4ERR_EXPIRED:
7050 case -NFS4ERR_BAD_STATEID:
7051 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7052 if (new_lock_owner != 0 ||
7053 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7054 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7056 case -NFS4ERR_STALE_STATEID:
7057 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7058 nfs4_schedule_lease_recovery(server->nfs_client);
7062 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7064 struct nfs4_lockdata *data;
7065 struct rpc_task *task;
7066 struct rpc_message msg = {
7067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7068 .rpc_cred = state->owner->so_cred,
7070 struct rpc_task_setup task_setup_data = {
7071 .rpc_client = NFS_CLIENT(state->inode),
7072 .rpc_message = &msg,
7073 .callback_ops = &nfs4_lock_ops,
7074 .workqueue = nfsiod_workqueue,
7075 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7079 dprintk("%s: begin!\n", __func__);
7080 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7081 fl->fl_u.nfs4_fl.owner,
7082 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
7086 data->arg.block = 1;
7087 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7088 recovery_type > NFS_LOCK_NEW);
7089 msg.rpc_argp = &data->arg;
7090 msg.rpc_resp = &data->res;
7091 task_setup_data.callback_data = data;
7092 if (recovery_type > NFS_LOCK_NEW) {
7093 if (recovery_type == NFS_LOCK_RECLAIM)
7094 data->arg.reclaim = NFS_LOCK_RECLAIM;
7096 data->arg.new_lock = 1;
7097 task = rpc_run_task(&task_setup_data);
7099 return PTR_ERR(task);
7100 ret = rpc_wait_for_completion_task(task);
7102 ret = data->rpc_status;
7104 nfs4_handle_setlk_error(data->server, data->lsp,
7105 data->arg.new_lock_owner, ret);
7107 data->cancelled = true;
7108 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7110 dprintk("%s: done, ret = %d!\n", __func__, ret);
7114 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7116 struct nfs_server *server = NFS_SERVER(state->inode);
7117 struct nfs4_exception exception = {
7118 .inode = state->inode,
7123 /* Cache the lock if possible... */
7124 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7126 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7127 if (err != -NFS4ERR_DELAY)
7129 nfs4_handle_exception(server, err, &exception);
7130 } while (exception.retry);
7134 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7136 struct nfs_server *server = NFS_SERVER(state->inode);
7137 struct nfs4_exception exception = {
7138 .inode = state->inode,
7142 err = nfs4_set_lock_state(state, request);
7145 if (!recover_lost_locks) {
7146 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7150 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7152 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7156 case -NFS4ERR_GRACE:
7157 case -NFS4ERR_DELAY:
7158 nfs4_handle_exception(server, err, &exception);
7161 } while (exception.retry);
7166 #if defined(CONFIG_NFS_V4_1)
7167 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7169 struct nfs4_lock_state *lsp;
7172 status = nfs4_set_lock_state(state, request);
7175 lsp = request->fl_u.nfs4_fl.owner;
7176 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7177 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7179 return nfs4_lock_expired(state, request);
7183 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7185 struct nfs_inode *nfsi = NFS_I(state->inode);
7186 struct nfs4_state_owner *sp = state->owner;
7187 unsigned char fl_flags = request->fl_flags;
7190 request->fl_flags |= FL_ACCESS;
7191 status = locks_lock_inode_wait(state->inode, request);
7194 mutex_lock(&sp->so_delegreturn_mutex);
7195 down_read(&nfsi->rwsem);
7196 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7197 /* Yes: cache locks! */
7198 /* ...but avoid races with delegation recall... */
7199 request->fl_flags = fl_flags & ~FL_SLEEP;
7200 status = locks_lock_inode_wait(state->inode, request);
7201 up_read(&nfsi->rwsem);
7202 mutex_unlock(&sp->so_delegreturn_mutex);
7205 up_read(&nfsi->rwsem);
7206 mutex_unlock(&sp->so_delegreturn_mutex);
7207 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7209 request->fl_flags = fl_flags;
7213 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7215 struct nfs4_exception exception = {
7217 .inode = state->inode,
7218 .interruptible = true,
7223 err = _nfs4_proc_setlk(state, cmd, request);
7224 if (err == -NFS4ERR_DENIED)
7226 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7228 } while (exception.retry);
7232 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7233 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7236 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7237 struct file_lock *request)
7239 int status = -ERESTARTSYS;
7240 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7242 while(!signalled()) {
7243 status = nfs4_proc_setlk(state, cmd, request);
7244 if ((status != -EAGAIN) || IS_SETLK(cmd))
7246 freezable_schedule_timeout_interruptible(timeout);
7248 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7249 status = -ERESTARTSYS;
7254 #ifdef CONFIG_NFS_V4_1
7255 struct nfs4_lock_waiter {
7256 struct task_struct *task;
7257 struct inode *inode;
7258 struct nfs_lowner *owner;
7262 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7265 struct nfs4_lock_waiter *waiter = wait->private;
7267 /* NULL key means to wake up everyone */
7269 struct cb_notify_lock_args *cbnl = key;
7270 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7271 *wowner = waiter->owner;
7273 /* Only wake if the callback was for the same owner. */
7274 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7277 /* Make sure it's for the right inode */
7278 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7282 /* override "private" so we can use default_wake_function */
7283 wait->private = waiter->task;
7284 ret = woken_wake_function(wait, mode, flags, key);
7286 list_del_init(&wait->entry);
7287 wait->private = waiter;
7292 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7294 int status = -ERESTARTSYS;
7295 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7296 struct nfs_server *server = NFS_SERVER(state->inode);
7297 struct nfs_client *clp = server->nfs_client;
7298 wait_queue_head_t *q = &clp->cl_lock_waitq;
7299 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7300 .id = lsp->ls_seqid.owner_id,
7301 .s_dev = server->s_dev };
7302 struct nfs4_lock_waiter waiter = { .task = current,
7303 .inode = state->inode,
7305 wait_queue_entry_t wait;
7307 /* Don't bother with waitqueue if we don't expect a callback */
7308 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7309 return nfs4_retry_setlk_simple(state, cmd, request);
7312 wait.private = &waiter;
7313 wait.func = nfs4_wake_lock_waiter;
7315 while(!signalled()) {
7316 add_wait_queue(q, &wait);
7317 status = nfs4_proc_setlk(state, cmd, request);
7318 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7319 finish_wait(q, &wait);
7323 status = -ERESTARTSYS;
7324 freezer_do_not_count();
7325 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7327 finish_wait(q, &wait);
7332 #else /* !CONFIG_NFS_V4_1 */
7334 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7336 return nfs4_retry_setlk_simple(state, cmd, request);
7341 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7343 struct nfs_open_context *ctx;
7344 struct nfs4_state *state;
7347 /* verify open state */
7348 ctx = nfs_file_open_context(filp);
7351 if (IS_GETLK(cmd)) {
7353 return nfs4_proc_getlk(state, F_GETLK, request);
7357 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7360 if (request->fl_type == F_UNLCK) {
7362 return nfs4_proc_unlck(state, cmd, request);
7369 if ((request->fl_flags & FL_POSIX) &&
7370 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7374 * Don't rely on the VFS having checked the file open mode,
7375 * since it won't do this for flock() locks.
7377 switch (request->fl_type) {
7379 if (!(filp->f_mode & FMODE_READ))
7383 if (!(filp->f_mode & FMODE_WRITE))
7387 status = nfs4_set_lock_state(state, request);
7391 return nfs4_retry_setlk(state, cmd, request);
7394 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7396 struct nfs_server *server = NFS_SERVER(state->inode);
7399 err = nfs4_set_lock_state(state, fl);
7403 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7404 if (err != -NFS4ERR_DELAY)
7407 } while (err == -NFS4ERR_DELAY);
7408 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7411 struct nfs_release_lockowner_data {
7412 struct nfs4_lock_state *lsp;
7413 struct nfs_server *server;
7414 struct nfs_release_lockowner_args args;
7415 struct nfs_release_lockowner_res res;
7416 unsigned long timestamp;
7419 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7421 struct nfs_release_lockowner_data *data = calldata;
7422 struct nfs_server *server = data->server;
7423 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7424 &data->res.seq_res, task);
7425 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7426 data->timestamp = jiffies;
7429 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7431 struct nfs_release_lockowner_data *data = calldata;
7432 struct nfs_server *server = data->server;
7434 nfs40_sequence_done(task, &data->res.seq_res);
7436 switch (task->tk_status) {
7438 renew_lease(server, data->timestamp);
7440 case -NFS4ERR_STALE_CLIENTID:
7441 case -NFS4ERR_EXPIRED:
7442 nfs4_schedule_lease_recovery(server->nfs_client);
7444 case -NFS4ERR_LEASE_MOVED:
7445 case -NFS4ERR_DELAY:
7446 if (nfs4_async_handle_error(task, server,
7447 NULL, NULL) == -EAGAIN)
7448 rpc_restart_call_prepare(task);
7452 static void nfs4_release_lockowner_release(void *calldata)
7454 struct nfs_release_lockowner_data *data = calldata;
7455 nfs4_free_lock_state(data->server, data->lsp);
7459 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7460 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7461 .rpc_call_done = nfs4_release_lockowner_done,
7462 .rpc_release = nfs4_release_lockowner_release,
7466 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7468 struct nfs_release_lockowner_data *data;
7469 struct rpc_message msg = {
7470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7473 if (server->nfs_client->cl_mvops->minor_version != 0)
7476 data = kmalloc(sizeof(*data), GFP_NOFS);
7480 data->server = server;
7481 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7482 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7483 data->args.lock_owner.s_dev = server->s_dev;
7485 msg.rpc_argp = &data->args;
7486 msg.rpc_resp = &data->res;
7487 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7488 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7491 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7493 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7494 struct dentry *unused, struct inode *inode,
7495 const char *key, const void *buf,
7496 size_t buflen, int flags)
7498 return nfs4_proc_set_acl(inode, buf, buflen);
7501 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7502 struct dentry *unused, struct inode *inode,
7503 const char *key, void *buf, size_t buflen)
7505 return nfs4_proc_get_acl(inode, buf, buflen);
7508 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7510 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7513 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7515 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7516 struct dentry *unused, struct inode *inode,
7517 const char *key, const void *buf,
7518 size_t buflen, int flags)
7520 if (security_ismaclabel(key))
7521 return nfs4_set_security_label(inode, buf, buflen);
7526 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7527 struct dentry *unused, struct inode *inode,
7528 const char *key, void *buf, size_t buflen)
7530 if (security_ismaclabel(key))
7531 return nfs4_get_security_label(inode, buf, buflen);
7536 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7540 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7541 len = security_inode_listsecurity(inode, list, list_len);
7542 if (len >= 0 && list_len && len > list_len)
7548 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7549 .prefix = XATTR_SECURITY_PREFIX,
7550 .get = nfs4_xattr_get_nfs4_label,
7551 .set = nfs4_xattr_set_nfs4_label,
7557 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7564 #ifdef CONFIG_NFS_V4_2
7565 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7566 struct dentry *unused, struct inode *inode,
7567 const char *key, const void *buf,
7568 size_t buflen, int flags)
7570 struct nfs_access_entry cache;
7573 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7577 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7578 * flags right now. Handling of xattr operations use the normal
7579 * file read/write permissions.
7581 * Just in case the server has other ideas (which RFC 8276 allows),
7582 * do a cached access check for the XA* flags to possibly avoid
7583 * doing an RPC and getting EACCES back.
7585 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7586 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7591 ret = nfs42_proc_removexattr(inode, key);
7593 nfs4_xattr_cache_remove(inode, key);
7595 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7597 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7603 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7604 struct dentry *unused, struct inode *inode,
7605 const char *key, void *buf, size_t buflen)
7607 struct nfs_access_entry cache;
7610 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7613 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7614 if (!(cache.mask & NFS_ACCESS_XAREAD))
7618 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7622 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7623 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7626 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7632 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7639 struct nfs_access_entry cache;
7641 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7644 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7645 if (!(cache.mask & NFS_ACCESS_XALIST))
7649 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7653 ret = nfs4_xattr_cache_list(inode, list, list_len);
7654 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7659 buflen = list_len ? list_len : XATTR_LIST_MAX;
7660 buf = list_len ? list : NULL;
7664 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7677 nfs4_xattr_cache_set_list(inode, list, size);
7685 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7689 #endif /* CONFIG_NFS_V4_2 */
7692 * nfs_fhget will use either the mounted_on_fileid or the fileid
7694 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7696 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7697 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7698 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7699 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7702 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7703 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7704 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7708 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7709 const struct qstr *name,
7710 struct nfs4_fs_locations *fs_locations,
7713 struct nfs_server *server = NFS_SERVER(dir);
7715 struct nfs4_fs_locations_arg args = {
7716 .dir_fh = NFS_FH(dir),
7721 struct nfs4_fs_locations_res res = {
7722 .fs_locations = fs_locations,
7724 struct rpc_message msg = {
7725 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7731 dprintk("%s: start\n", __func__);
7733 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7734 bitmask[1] = nfs4_fattr_bitmap[1];
7736 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7737 * is not supported */
7738 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7739 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7741 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7743 nfs_fattr_init(&fs_locations->fattr);
7744 fs_locations->server = server;
7745 fs_locations->nlocations = 0;
7746 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7747 dprintk("%s: returned status = %d\n", __func__, status);
7751 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7752 const struct qstr *name,
7753 struct nfs4_fs_locations *fs_locations,
7756 struct nfs4_exception exception = {
7757 .interruptible = true,
7761 err = _nfs4_proc_fs_locations(client, dir, name,
7762 fs_locations, page);
7763 trace_nfs4_get_fs_locations(dir, name, err);
7764 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7766 } while (exception.retry);
7771 * This operation also signals the server that this client is
7772 * performing migration recovery. The server can stop returning
7773 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7774 * appended to this compound to identify the client ID which is
7775 * performing recovery.
7777 static int _nfs40_proc_get_locations(struct inode *inode,
7778 struct nfs4_fs_locations *locations,
7779 struct page *page, const struct cred *cred)
7781 struct nfs_server *server = NFS_SERVER(inode);
7782 struct rpc_clnt *clnt = server->client;
7784 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7786 struct nfs4_fs_locations_arg args = {
7787 .clientid = server->nfs_client->cl_clientid,
7788 .fh = NFS_FH(inode),
7791 .migration = 1, /* skip LOOKUP */
7792 .renew = 1, /* append RENEW */
7794 struct nfs4_fs_locations_res res = {
7795 .fs_locations = locations,
7799 struct rpc_message msg = {
7800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7805 unsigned long now = jiffies;
7808 nfs_fattr_init(&locations->fattr);
7809 locations->server = server;
7810 locations->nlocations = 0;
7812 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7813 status = nfs4_call_sync_sequence(clnt, server, &msg,
7814 &args.seq_args, &res.seq_res);
7818 renew_lease(server, now);
7822 #ifdef CONFIG_NFS_V4_1
7825 * This operation also signals the server that this client is
7826 * performing migration recovery. The server can stop asserting
7827 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7828 * performing this operation is identified in the SEQUENCE
7829 * operation in this compound.
7831 * When the client supports GETATTR(fs_locations_info), it can
7832 * be plumbed in here.
7834 static int _nfs41_proc_get_locations(struct inode *inode,
7835 struct nfs4_fs_locations *locations,
7836 struct page *page, const struct cred *cred)
7838 struct nfs_server *server = NFS_SERVER(inode);
7839 struct rpc_clnt *clnt = server->client;
7841 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7843 struct nfs4_fs_locations_arg args = {
7844 .fh = NFS_FH(inode),
7847 .migration = 1, /* skip LOOKUP */
7849 struct nfs4_fs_locations_res res = {
7850 .fs_locations = locations,
7853 struct rpc_message msg = {
7854 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7861 nfs_fattr_init(&locations->fattr);
7862 locations->server = server;
7863 locations->nlocations = 0;
7865 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7866 status = nfs4_call_sync_sequence(clnt, server, &msg,
7867 &args.seq_args, &res.seq_res);
7868 if (status == NFS4_OK &&
7869 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7870 status = -NFS4ERR_LEASE_MOVED;
7874 #endif /* CONFIG_NFS_V4_1 */
7877 * nfs4_proc_get_locations - discover locations for a migrated FSID
7878 * @inode: inode on FSID that is migrating
7879 * @locations: result of query
7881 * @cred: credential to use for this operation
7883 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7884 * operation failed, or a negative errno if a local error occurred.
7886 * On success, "locations" is filled in, but if the server has
7887 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7890 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7891 * from this client that require migration recovery.
7893 int nfs4_proc_get_locations(struct inode *inode,
7894 struct nfs4_fs_locations *locations,
7895 struct page *page, const struct cred *cred)
7897 struct nfs_server *server = NFS_SERVER(inode);
7898 struct nfs_client *clp = server->nfs_client;
7899 const struct nfs4_mig_recovery_ops *ops =
7900 clp->cl_mvops->mig_recovery_ops;
7901 struct nfs4_exception exception = {
7902 .interruptible = true,
7906 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7907 (unsigned long long)server->fsid.major,
7908 (unsigned long long)server->fsid.minor,
7910 nfs_display_fhandle(NFS_FH(inode), __func__);
7913 status = ops->get_locations(inode, locations, page, cred);
7914 if (status != -NFS4ERR_DELAY)
7916 nfs4_handle_exception(server, status, &exception);
7917 } while (exception.retry);
7922 * This operation also signals the server that this client is
7923 * performing "lease moved" recovery. The server can stop
7924 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7925 * is appended to this compound to identify the client ID which is
7926 * performing recovery.
7928 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7930 struct nfs_server *server = NFS_SERVER(inode);
7931 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7932 struct rpc_clnt *clnt = server->client;
7933 struct nfs4_fsid_present_arg args = {
7934 .fh = NFS_FH(inode),
7935 .clientid = clp->cl_clientid,
7936 .renew = 1, /* append RENEW */
7938 struct nfs4_fsid_present_res res = {
7941 struct rpc_message msg = {
7942 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7947 unsigned long now = jiffies;
7950 res.fh = nfs_alloc_fhandle();
7954 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7955 status = nfs4_call_sync_sequence(clnt, server, &msg,
7956 &args.seq_args, &res.seq_res);
7957 nfs_free_fhandle(res.fh);
7961 do_renew_lease(clp, now);
7965 #ifdef CONFIG_NFS_V4_1
7968 * This operation also signals the server that this client is
7969 * performing "lease moved" recovery. The server can stop asserting
7970 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7971 * this operation is identified in the SEQUENCE operation in this
7974 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7976 struct nfs_server *server = NFS_SERVER(inode);
7977 struct rpc_clnt *clnt = server->client;
7978 struct nfs4_fsid_present_arg args = {
7979 .fh = NFS_FH(inode),
7981 struct nfs4_fsid_present_res res = {
7983 struct rpc_message msg = {
7984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7991 res.fh = nfs_alloc_fhandle();
7995 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7996 status = nfs4_call_sync_sequence(clnt, server, &msg,
7997 &args.seq_args, &res.seq_res);
7998 nfs_free_fhandle(res.fh);
7999 if (status == NFS4_OK &&
8000 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8001 status = -NFS4ERR_LEASE_MOVED;
8005 #endif /* CONFIG_NFS_V4_1 */
8008 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8009 * @inode: inode on FSID to check
8010 * @cred: credential to use for this operation
8012 * Server indicates whether the FSID is present, moved, or not
8013 * recognized. This operation is necessary to clear a LEASE_MOVED
8014 * condition for this client ID.
8016 * Returns NFS4_OK if the FSID is present on this server,
8017 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8018 * NFS4ERR code if some error occurred on the server, or a
8019 * negative errno if a local failure occurred.
8021 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8023 struct nfs_server *server = NFS_SERVER(inode);
8024 struct nfs_client *clp = server->nfs_client;
8025 const struct nfs4_mig_recovery_ops *ops =
8026 clp->cl_mvops->mig_recovery_ops;
8027 struct nfs4_exception exception = {
8028 .interruptible = true,
8032 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8033 (unsigned long long)server->fsid.major,
8034 (unsigned long long)server->fsid.minor,
8036 nfs_display_fhandle(NFS_FH(inode), __func__);
8039 status = ops->fsid_present(inode, cred);
8040 if (status != -NFS4ERR_DELAY)
8042 nfs4_handle_exception(server, status, &exception);
8043 } while (exception.retry);
8048 * If 'use_integrity' is true and the state managment nfs_client
8049 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8050 * and the machine credential as per RFC3530bis and RFC5661 Security
8051 * Considerations sections. Otherwise, just use the user cred with the
8052 * filesystem's rpc_client.
8054 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8057 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8058 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8059 struct nfs4_secinfo_arg args = {
8060 .dir_fh = NFS_FH(dir),
8063 struct nfs4_secinfo_res res = {
8066 struct rpc_message msg = {
8067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8071 struct nfs4_call_sync_data data = {
8072 .seq_server = NFS_SERVER(dir),
8073 .seq_args = &args.seq_args,
8074 .seq_res = &res.seq_res,
8076 struct rpc_task_setup task_setup = {
8078 .rpc_message = &msg,
8079 .callback_ops = clp->cl_mvops->call_sync_ops,
8080 .callback_data = &data,
8081 .flags = RPC_TASK_NO_ROUND_ROBIN,
8083 const struct cred *cred = NULL;
8085 if (use_integrity) {
8086 clnt = clp->cl_rpcclient;
8087 task_setup.rpc_client = clnt;
8089 cred = nfs4_get_clid_cred(clp);
8090 msg.rpc_cred = cred;
8093 dprintk("NFS call secinfo %s\n", name->name);
8095 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8096 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8097 status = nfs4_call_sync_custom(&task_setup);
8099 dprintk("NFS reply secinfo: %d\n", status);
8105 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8106 struct nfs4_secinfo_flavors *flavors)
8108 struct nfs4_exception exception = {
8109 .interruptible = true,
8113 err = -NFS4ERR_WRONGSEC;
8115 /* try to use integrity protection with machine cred */
8116 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8117 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8120 * if unable to use integrity protection, or SECINFO with
8121 * integrity protection returns NFS4ERR_WRONGSEC (which is
8122 * disallowed by spec, but exists in deployed servers) use
8123 * the current filesystem's rpc_client and the user cred.
8125 if (err == -NFS4ERR_WRONGSEC)
8126 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8128 trace_nfs4_secinfo(dir, name, err);
8129 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8131 } while (exception.retry);
8135 #ifdef CONFIG_NFS_V4_1
8137 * Check the exchange flags returned by the server for invalid flags, having
8138 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8141 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8143 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8145 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8147 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8148 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8150 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8154 return -NFS4ERR_INVAL;
8158 nfs41_same_server_scope(struct nfs41_server_scope *a,
8159 struct nfs41_server_scope *b)
8161 if (a->server_scope_sz != b->server_scope_sz)
8163 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8167 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8169 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8170 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8171 struct nfs_client *clp = args->client;
8173 switch (task->tk_status) {
8174 case -NFS4ERR_BADSESSION:
8175 case -NFS4ERR_DEADSESSION:
8176 nfs4_schedule_session_recovery(clp->cl_session,
8179 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8180 res->dir != NFS4_CDFS4_BOTH) {
8181 rpc_task_close_connection(task);
8182 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8183 rpc_restart_call(task);
8187 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8188 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8192 * nfs4_proc_bind_one_conn_to_session()
8194 * The 4.1 client currently uses the same TCP connection for the
8195 * fore and backchannel.
8198 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8199 struct rpc_xprt *xprt,
8200 struct nfs_client *clp,
8201 const struct cred *cred)
8204 struct nfs41_bind_conn_to_session_args args = {
8206 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8209 struct nfs41_bind_conn_to_session_res res;
8210 struct rpc_message msg = {
8212 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8217 struct rpc_task_setup task_setup_data = {
8220 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8221 .rpc_message = &msg,
8222 .flags = RPC_TASK_TIMEOUT,
8224 struct rpc_task *task;
8226 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8227 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8228 args.dir = NFS4_CDFC4_FORE;
8230 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8231 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8232 args.dir = NFS4_CDFC4_FORE;
8234 task = rpc_run_task(&task_setup_data);
8235 if (!IS_ERR(task)) {
8236 status = task->tk_status;
8239 status = PTR_ERR(task);
8240 trace_nfs4_bind_conn_to_session(clp, status);
8242 if (memcmp(res.sessionid.data,
8243 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8244 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8247 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8248 dprintk("NFS: %s: Unexpected direction from server\n",
8252 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8253 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8262 struct rpc_bind_conn_calldata {
8263 struct nfs_client *clp;
8264 const struct cred *cred;
8268 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8269 struct rpc_xprt *xprt,
8272 struct rpc_bind_conn_calldata *p = calldata;
8274 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8277 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8279 struct rpc_bind_conn_calldata data = {
8283 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8284 nfs4_proc_bind_conn_to_session_callback, &data);
8288 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8289 * and operations we'd like to see to enable certain features in the allow map
8291 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8292 .how = SP4_MACH_CRED,
8293 .enforce.u.words = {
8294 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8295 1 << (OP_EXCHANGE_ID - 32) |
8296 1 << (OP_CREATE_SESSION - 32) |
8297 1 << (OP_DESTROY_SESSION - 32) |
8298 1 << (OP_DESTROY_CLIENTID - 32)
8301 [0] = 1 << (OP_CLOSE) |
8302 1 << (OP_OPEN_DOWNGRADE) |
8304 1 << (OP_DELEGRETURN) |
8306 [1] = 1 << (OP_SECINFO - 32) |
8307 1 << (OP_SECINFO_NO_NAME - 32) |
8308 1 << (OP_LAYOUTRETURN - 32) |
8309 1 << (OP_TEST_STATEID - 32) |
8310 1 << (OP_FREE_STATEID - 32) |
8311 1 << (OP_WRITE - 32)
8316 * Select the state protection mode for client `clp' given the server results
8317 * from exchange_id in `sp'.
8319 * Returns 0 on success, negative errno otherwise.
8321 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8322 struct nfs41_state_protection *sp)
8324 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8325 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8326 1 << (OP_EXCHANGE_ID - 32) |
8327 1 << (OP_CREATE_SESSION - 32) |
8328 1 << (OP_DESTROY_SESSION - 32) |
8329 1 << (OP_DESTROY_CLIENTID - 32)
8331 unsigned long flags = 0;
8335 if (sp->how == SP4_MACH_CRED) {
8336 /* Print state protect result */
8337 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8338 for (i = 0; i <= LAST_NFS4_OP; i++) {
8339 if (test_bit(i, sp->enforce.u.longs))
8340 dfprintk(MOUNT, " enforce op %d\n", i);
8341 if (test_bit(i, sp->allow.u.longs))
8342 dfprintk(MOUNT, " allow op %d\n", i);
8345 /* make sure nothing is on enforce list that isn't supported */
8346 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8347 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8348 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8355 * Minimal mode - state operations are allowed to use machine
8356 * credential. Note this already happens by default, so the
8357 * client doesn't have to do anything more than the negotiation.
8359 * NOTE: we don't care if EXCHANGE_ID is in the list -
8360 * we're already using the machine cred for exchange_id
8361 * and will never use a different cred.
8363 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8364 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8365 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8366 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8367 dfprintk(MOUNT, "sp4_mach_cred:\n");
8368 dfprintk(MOUNT, " minimal mode enabled\n");
8369 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8371 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8376 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8377 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8378 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8379 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8380 dfprintk(MOUNT, " cleanup mode enabled\n");
8381 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8384 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8385 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8386 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8389 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8390 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8391 dfprintk(MOUNT, " secinfo mode enabled\n");
8392 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8395 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8396 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8397 dfprintk(MOUNT, " stateid mode enabled\n");
8398 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8401 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8402 dfprintk(MOUNT, " write mode enabled\n");
8403 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8406 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8407 dfprintk(MOUNT, " commit mode enabled\n");
8408 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8412 clp->cl_sp4_flags = flags;
8416 struct nfs41_exchange_id_data {
8417 struct nfs41_exchange_id_res res;
8418 struct nfs41_exchange_id_args args;
8421 static void nfs4_exchange_id_release(void *data)
8423 struct nfs41_exchange_id_data *cdata =
8424 (struct nfs41_exchange_id_data *)data;
8426 nfs_put_client(cdata->args.client);
8427 kfree(cdata->res.impl_id);
8428 kfree(cdata->res.server_scope);
8429 kfree(cdata->res.server_owner);
8433 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8434 .rpc_release = nfs4_exchange_id_release,
8438 * _nfs4_proc_exchange_id()
8440 * Wrapper for EXCHANGE_ID operation.
8442 static struct rpc_task *
8443 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8444 u32 sp4_how, struct rpc_xprt *xprt)
8446 struct rpc_message msg = {
8447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8450 struct rpc_task_setup task_setup_data = {
8451 .rpc_client = clp->cl_rpcclient,
8452 .callback_ops = &nfs4_exchange_id_call_ops,
8453 .rpc_message = &msg,
8454 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8456 struct nfs41_exchange_id_data *calldata;
8459 if (!refcount_inc_not_zero(&clp->cl_count))
8460 return ERR_PTR(-EIO);
8463 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8467 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8469 status = nfs4_init_uniform_client_string(clp);
8473 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8476 if (unlikely(calldata->res.server_owner == NULL))
8479 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8481 if (unlikely(calldata->res.server_scope == NULL))
8482 goto out_server_owner;
8484 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8485 if (unlikely(calldata->res.impl_id == NULL))
8486 goto out_server_scope;
8490 calldata->args.state_protect.how = SP4_NONE;
8494 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8504 task_setup_data.rpc_xprt = xprt;
8505 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8506 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8507 sizeof(calldata->args.verifier.data));
8509 calldata->args.client = clp;
8510 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8511 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8512 #ifdef CONFIG_NFS_V4_1_MIGRATION
8513 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8515 msg.rpc_argp = &calldata->args;
8516 msg.rpc_resp = &calldata->res;
8517 task_setup_data.callback_data = calldata;
8519 return rpc_run_task(&task_setup_data);
8522 kfree(calldata->res.impl_id);
8524 kfree(calldata->res.server_scope);
8526 kfree(calldata->res.server_owner);
8530 nfs_put_client(clp);
8531 return ERR_PTR(status);
8535 * _nfs4_proc_exchange_id()
8537 * Wrapper for EXCHANGE_ID operation.
8539 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8542 struct rpc_task *task;
8543 struct nfs41_exchange_id_args *argp;
8544 struct nfs41_exchange_id_res *resp;
8545 unsigned long now = jiffies;
8548 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8550 return PTR_ERR(task);
8552 argp = task->tk_msg.rpc_argp;
8553 resp = task->tk_msg.rpc_resp;
8554 status = task->tk_status;
8558 status = nfs4_check_cl_exchange_flags(resp->flags,
8559 clp->cl_mvops->minor_version);
8563 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8567 do_renew_lease(clp, now);
8569 clp->cl_clientid = resp->clientid;
8570 clp->cl_exchange_flags = resp->flags;
8571 clp->cl_seqid = resp->seqid;
8572 /* Client ID is not confirmed */
8573 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8574 clear_bit(NFS4_SESSION_ESTABLISHED,
8575 &clp->cl_session->session_state);
8577 if (clp->cl_serverscope != NULL &&
8578 !nfs41_same_server_scope(clp->cl_serverscope,
8579 resp->server_scope)) {
8580 dprintk("%s: server_scope mismatch detected\n",
8582 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8585 swap(clp->cl_serverowner, resp->server_owner);
8586 swap(clp->cl_serverscope, resp->server_scope);
8587 swap(clp->cl_implid, resp->impl_id);
8589 /* Save the EXCHANGE_ID verifier session trunk tests */
8590 memcpy(clp->cl_confirm.data, argp->verifier.data,
8591 sizeof(clp->cl_confirm.data));
8593 trace_nfs4_exchange_id(clp, status);
8599 * nfs4_proc_exchange_id()
8601 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8603 * Since the clientid has expired, all compounds using sessions
8604 * associated with the stale clientid will be returning
8605 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8606 * be in some phase of session reset.
8608 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8610 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8612 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8615 /* try SP4_MACH_CRED if krb5i/p */
8616 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8617 authflavor == RPC_AUTH_GSS_KRB5P) {
8618 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8624 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8628 * nfs4_test_session_trunk
8630 * This is an add_xprt_test() test function called from
8631 * rpc_clnt_setup_test_and_add_xprt.
8633 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8634 * and is dereferrenced in nfs4_exchange_id_release
8636 * Upon success, add the new transport to the rpc_clnt
8638 * @clnt: struct rpc_clnt to get new transport
8639 * @xprt: the rpc_xprt to test
8640 * @data: call data for _nfs4_proc_exchange_id.
8642 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8645 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8646 struct rpc_task *task;
8651 dprintk("--> %s try %s\n", __func__,
8652 xprt->address_strings[RPC_DISPLAY_ADDR]);
8654 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8656 /* Test connection for session trunking. Async exchange_id call */
8657 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8661 status = task->tk_status;
8663 status = nfs4_detect_session_trunking(adata->clp,
8664 task->tk_msg.rpc_resp, xprt);
8667 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8671 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8673 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8674 const struct cred *cred)
8676 struct rpc_message msg = {
8677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8683 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8684 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8685 trace_nfs4_destroy_clientid(clp, status);
8687 dprintk("NFS: Got error %d from the server %s on "
8688 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8692 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8693 const struct cred *cred)
8698 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8699 ret = _nfs4_proc_destroy_clientid(clp, cred);
8701 case -NFS4ERR_DELAY:
8702 case -NFS4ERR_CLIENTID_BUSY:
8712 int nfs4_destroy_clientid(struct nfs_client *clp)
8714 const struct cred *cred;
8717 if (clp->cl_mvops->minor_version < 1)
8719 if (clp->cl_exchange_flags == 0)
8721 if (clp->cl_preserve_clid)
8723 cred = nfs4_get_clid_cred(clp);
8724 ret = nfs4_proc_destroy_clientid(clp, cred);
8728 case -NFS4ERR_STALE_CLIENTID:
8729 clp->cl_exchange_flags = 0;
8735 #endif /* CONFIG_NFS_V4_1 */
8737 struct nfs4_get_lease_time_data {
8738 struct nfs4_get_lease_time_args *args;
8739 struct nfs4_get_lease_time_res *res;
8740 struct nfs_client *clp;
8743 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8746 struct nfs4_get_lease_time_data *data =
8747 (struct nfs4_get_lease_time_data *)calldata;
8749 dprintk("--> %s\n", __func__);
8750 /* just setup sequence, do not trigger session recovery
8751 since we're invoked within one */
8752 nfs4_setup_sequence(data->clp,
8753 &data->args->la_seq_args,
8754 &data->res->lr_seq_res,
8756 dprintk("<-- %s\n", __func__);
8760 * Called from nfs4_state_manager thread for session setup, so don't recover
8761 * from sequence operation or clientid errors.
8763 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8765 struct nfs4_get_lease_time_data *data =
8766 (struct nfs4_get_lease_time_data *)calldata;
8768 dprintk("--> %s\n", __func__);
8769 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8771 switch (task->tk_status) {
8772 case -NFS4ERR_DELAY:
8773 case -NFS4ERR_GRACE:
8774 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8775 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8776 task->tk_status = 0;
8778 case -NFS4ERR_RETRY_UNCACHED_REP:
8779 rpc_restart_call_prepare(task);
8782 dprintk("<-- %s\n", __func__);
8785 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8786 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8787 .rpc_call_done = nfs4_get_lease_time_done,
8790 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8792 struct nfs4_get_lease_time_args args;
8793 struct nfs4_get_lease_time_res res = {
8794 .lr_fsinfo = fsinfo,
8796 struct nfs4_get_lease_time_data data = {
8801 struct rpc_message msg = {
8802 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8806 struct rpc_task_setup task_setup = {
8807 .rpc_client = clp->cl_rpcclient,
8808 .rpc_message = &msg,
8809 .callback_ops = &nfs4_get_lease_time_ops,
8810 .callback_data = &data,
8811 .flags = RPC_TASK_TIMEOUT,
8814 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8815 return nfs4_call_sync_custom(&task_setup);
8818 #ifdef CONFIG_NFS_V4_1
8821 * Initialize the values to be used by the client in CREATE_SESSION
8822 * If nfs4_init_session set the fore channel request and response sizes,
8825 * Set the back channel max_resp_sz_cached to zero to force the client to
8826 * always set csa_cachethis to FALSE because the current implementation
8827 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8829 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8830 struct rpc_clnt *clnt)
8832 unsigned int max_rqst_sz, max_resp_sz;
8833 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8834 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8836 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8837 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8839 /* Fore channel attributes */
8840 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8841 args->fc_attrs.max_resp_sz = max_resp_sz;
8842 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8843 args->fc_attrs.max_reqs = max_session_slots;
8845 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8846 "max_ops=%u max_reqs=%u\n",
8848 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8849 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8851 /* Back channel attributes */
8852 args->bc_attrs.max_rqst_sz = max_bc_payload;
8853 args->bc_attrs.max_resp_sz = max_bc_payload;
8854 args->bc_attrs.max_resp_sz_cached = 0;
8855 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8856 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8857 if (args->bc_attrs.max_reqs > max_bc_slots)
8858 args->bc_attrs.max_reqs = max_bc_slots;
8860 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8861 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8863 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8864 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8865 args->bc_attrs.max_reqs);
8868 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8869 struct nfs41_create_session_res *res)
8871 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8872 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8874 if (rcvd->max_resp_sz > sent->max_resp_sz)
8877 * Our requested max_ops is the minimum we need; we're not
8878 * prepared to break up compounds into smaller pieces than that.
8879 * So, no point even trying to continue if the server won't
8882 if (rcvd->max_ops < sent->max_ops)
8884 if (rcvd->max_reqs == 0)
8886 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8887 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8891 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8892 struct nfs41_create_session_res *res)
8894 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8895 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8897 if (!(res->flags & SESSION4_BACK_CHAN))
8899 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8901 if (rcvd->max_resp_sz < sent->max_resp_sz)
8903 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8905 if (rcvd->max_ops > sent->max_ops)
8907 if (rcvd->max_reqs > sent->max_reqs)
8913 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8914 struct nfs41_create_session_res *res)
8918 ret = nfs4_verify_fore_channel_attrs(args, res);
8921 return nfs4_verify_back_channel_attrs(args, res);
8924 static void nfs4_update_session(struct nfs4_session *session,
8925 struct nfs41_create_session_res *res)
8927 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8928 /* Mark client id and session as being confirmed */
8929 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8930 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8931 session->flags = res->flags;
8932 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8933 if (res->flags & SESSION4_BACK_CHAN)
8934 memcpy(&session->bc_attrs, &res->bc_attrs,
8935 sizeof(session->bc_attrs));
8938 static int _nfs4_proc_create_session(struct nfs_client *clp,
8939 const struct cred *cred)
8941 struct nfs4_session *session = clp->cl_session;
8942 struct nfs41_create_session_args args = {
8944 .clientid = clp->cl_clientid,
8945 .seqid = clp->cl_seqid,
8946 .cb_program = NFS4_CALLBACK,
8948 struct nfs41_create_session_res res;
8950 struct rpc_message msg = {
8951 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8958 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8959 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8961 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8962 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8963 trace_nfs4_create_session(clp, status);
8966 case -NFS4ERR_STALE_CLIENTID:
8967 case -NFS4ERR_DELAY:
8976 /* Verify the session's negotiated channel_attrs values */
8977 status = nfs4_verify_channel_attrs(&args, &res);
8978 /* Increment the clientid slot sequence id */
8981 nfs4_update_session(session, &res);
8988 * Issues a CREATE_SESSION operation to the server.
8989 * It is the responsibility of the caller to verify the session is
8990 * expired before calling this routine.
8992 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8996 struct nfs4_session *session = clp->cl_session;
8998 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9000 status = _nfs4_proc_create_session(clp, cred);
9004 /* Init or reset the session slot tables */
9005 status = nfs4_setup_session_slot_tables(session);
9006 dprintk("slot table setup returned %d\n", status);
9010 ptr = (unsigned *)&session->sess_id.data[0];
9011 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9012 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9014 dprintk("<-- %s\n", __func__);
9019 * Issue the over-the-wire RPC DESTROY_SESSION.
9020 * The caller must serialize access to this routine.
9022 int nfs4_proc_destroy_session(struct nfs4_session *session,
9023 const struct cred *cred)
9025 struct rpc_message msg = {
9026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9027 .rpc_argp = session,
9032 dprintk("--> nfs4_proc_destroy_session\n");
9034 /* session is still being setup */
9035 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9038 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9039 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9040 trace_nfs4_destroy_session(session->clp, status);
9043 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9044 "Session has been destroyed regardless...\n", status);
9046 dprintk("<-- nfs4_proc_destroy_session\n");
9051 * Renew the cl_session lease.
9053 struct nfs4_sequence_data {
9054 struct nfs_client *clp;
9055 struct nfs4_sequence_args args;
9056 struct nfs4_sequence_res res;
9059 static void nfs41_sequence_release(void *data)
9061 struct nfs4_sequence_data *calldata = data;
9062 struct nfs_client *clp = calldata->clp;
9064 if (refcount_read(&clp->cl_count) > 1)
9065 nfs4_schedule_state_renewal(clp);
9066 nfs_put_client(clp);
9070 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9072 switch(task->tk_status) {
9073 case -NFS4ERR_DELAY:
9074 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9077 nfs4_schedule_lease_recovery(clp);
9082 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9084 struct nfs4_sequence_data *calldata = data;
9085 struct nfs_client *clp = calldata->clp;
9087 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9090 trace_nfs4_sequence(clp, task->tk_status);
9091 if (task->tk_status < 0) {
9092 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9093 if (refcount_read(&clp->cl_count) == 1)
9096 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9097 rpc_restart_call_prepare(task);
9101 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9103 dprintk("<-- %s\n", __func__);
9106 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9108 struct nfs4_sequence_data *calldata = data;
9109 struct nfs_client *clp = calldata->clp;
9110 struct nfs4_sequence_args *args;
9111 struct nfs4_sequence_res *res;
9113 args = task->tk_msg.rpc_argp;
9114 res = task->tk_msg.rpc_resp;
9116 nfs4_setup_sequence(clp, args, res, task);
9119 static const struct rpc_call_ops nfs41_sequence_ops = {
9120 .rpc_call_done = nfs41_sequence_call_done,
9121 .rpc_call_prepare = nfs41_sequence_prepare,
9122 .rpc_release = nfs41_sequence_release,
9125 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9126 const struct cred *cred,
9127 struct nfs4_slot *slot,
9130 struct nfs4_sequence_data *calldata;
9131 struct rpc_message msg = {
9132 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9135 struct rpc_task_setup task_setup_data = {
9136 .rpc_client = clp->cl_rpcclient,
9137 .rpc_message = &msg,
9138 .callback_ops = &nfs41_sequence_ops,
9139 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
9141 struct rpc_task *ret;
9143 ret = ERR_PTR(-EIO);
9144 if (!refcount_inc_not_zero(&clp->cl_count))
9147 ret = ERR_PTR(-ENOMEM);
9148 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9149 if (calldata == NULL)
9151 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9152 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9153 msg.rpc_argp = &calldata->args;
9154 msg.rpc_resp = &calldata->res;
9155 calldata->clp = clp;
9156 task_setup_data.callback_data = calldata;
9158 ret = rpc_run_task(&task_setup_data);
9163 nfs_put_client(clp);
9165 nfs41_release_slot(slot);
9169 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9171 struct rpc_task *task;
9174 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9176 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9178 ret = PTR_ERR(task);
9180 rpc_put_task_async(task);
9181 dprintk("<-- %s status=%d\n", __func__, ret);
9185 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9187 struct rpc_task *task;
9190 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9192 ret = PTR_ERR(task);
9195 ret = rpc_wait_for_completion_task(task);
9197 ret = task->tk_status;
9200 dprintk("<-- %s status=%d\n", __func__, ret);
9204 struct nfs4_reclaim_complete_data {
9205 struct nfs_client *clp;
9206 struct nfs41_reclaim_complete_args arg;
9207 struct nfs41_reclaim_complete_res res;
9210 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9212 struct nfs4_reclaim_complete_data *calldata = data;
9214 nfs4_setup_sequence(calldata->clp,
9215 &calldata->arg.seq_args,
9216 &calldata->res.seq_res,
9220 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9222 switch(task->tk_status) {
9224 wake_up_all(&clp->cl_lock_waitq);
9226 case -NFS4ERR_COMPLETE_ALREADY:
9227 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9229 case -NFS4ERR_DELAY:
9230 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9232 case -NFS4ERR_RETRY_UNCACHED_REP:
9234 case -NFS4ERR_BADSESSION:
9235 case -NFS4ERR_DEADSESSION:
9236 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9239 nfs4_schedule_lease_recovery(clp);
9244 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9246 struct nfs4_reclaim_complete_data *calldata = data;
9247 struct nfs_client *clp = calldata->clp;
9248 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9250 dprintk("--> %s\n", __func__);
9251 if (!nfs41_sequence_done(task, res))
9254 trace_nfs4_reclaim_complete(clp, task->tk_status);
9255 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9256 rpc_restart_call_prepare(task);
9259 dprintk("<-- %s\n", __func__);
9262 static void nfs4_free_reclaim_complete_data(void *data)
9264 struct nfs4_reclaim_complete_data *calldata = data;
9269 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9270 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9271 .rpc_call_done = nfs4_reclaim_complete_done,
9272 .rpc_release = nfs4_free_reclaim_complete_data,
9276 * Issue a global reclaim complete.
9278 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9279 const struct cred *cred)
9281 struct nfs4_reclaim_complete_data *calldata;
9282 struct rpc_message msg = {
9283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9286 struct rpc_task_setup task_setup_data = {
9287 .rpc_client = clp->cl_rpcclient,
9288 .rpc_message = &msg,
9289 .callback_ops = &nfs4_reclaim_complete_call_ops,
9290 .flags = RPC_TASK_NO_ROUND_ROBIN,
9292 int status = -ENOMEM;
9294 dprintk("--> %s\n", __func__);
9295 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9296 if (calldata == NULL)
9298 calldata->clp = clp;
9299 calldata->arg.one_fs = 0;
9301 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9302 msg.rpc_argp = &calldata->arg;
9303 msg.rpc_resp = &calldata->res;
9304 task_setup_data.callback_data = calldata;
9305 status = nfs4_call_sync_custom(&task_setup_data);
9307 dprintk("<-- %s status=%d\n", __func__, status);
9312 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9314 struct nfs4_layoutget *lgp = calldata;
9315 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9317 dprintk("--> %s\n", __func__);
9318 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9319 &lgp->res.seq_res, task);
9320 dprintk("<-- %s\n", __func__);
9323 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9325 struct nfs4_layoutget *lgp = calldata;
9327 dprintk("--> %s\n", __func__);
9328 nfs41_sequence_process(task, &lgp->res.seq_res);
9329 dprintk("<-- %s\n", __func__);
9333 nfs4_layoutget_handle_exception(struct rpc_task *task,
9334 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9336 struct inode *inode = lgp->args.inode;
9337 struct nfs_server *server = NFS_SERVER(inode);
9338 struct pnfs_layout_hdr *lo;
9339 int nfs4err = task->tk_status;
9340 int err, status = 0;
9343 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9345 nfs4_sequence_free_slot(&lgp->res.seq_res);
9352 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9353 * on the file. set tk_status to -ENODATA to tell upper layer to
9356 case -NFS4ERR_LAYOUTUNAVAILABLE:
9360 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9361 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9363 case -NFS4ERR_BADLAYOUT:
9364 status = -EOVERFLOW;
9367 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9368 * (or clients) writing to the same RAID stripe except when
9369 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9371 * Treat it like we would RECALLCONFLICT -- we retry for a little
9372 * while, and then eventually give up.
9374 case -NFS4ERR_LAYOUTTRYLATER:
9375 if (lgp->args.minlength == 0) {
9376 status = -EOVERFLOW;
9381 case -NFS4ERR_RECALLCONFLICT:
9382 status = -ERECALLCONFLICT;
9384 case -NFS4ERR_DELEG_REVOKED:
9385 case -NFS4ERR_ADMIN_REVOKED:
9386 case -NFS4ERR_EXPIRED:
9387 case -NFS4ERR_BAD_STATEID:
9388 exception->timeout = 0;
9389 spin_lock(&inode->i_lock);
9390 lo = NFS_I(inode)->layout;
9391 /* If the open stateid was bad, then recover it. */
9392 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9393 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9394 spin_unlock(&inode->i_lock);
9395 exception->state = lgp->args.ctx->state;
9396 exception->stateid = &lgp->args.stateid;
9401 * Mark the bad layout state as invalid, then retry
9403 pnfs_mark_layout_stateid_invalid(lo, &head);
9404 spin_unlock(&inode->i_lock);
9405 nfs_commit_inode(inode, 0);
9406 pnfs_free_lseg_list(&head);
9411 err = nfs4_handle_exception(server, nfs4err, exception);
9413 if (exception->retry)
9419 dprintk("<-- %s\n", __func__);
9423 size_t max_response_pages(struct nfs_server *server)
9425 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9426 return nfs_page_array_len(0, max_resp_sz);
9429 static void nfs4_layoutget_release(void *calldata)
9431 struct nfs4_layoutget *lgp = calldata;
9433 dprintk("--> %s\n", __func__);
9434 nfs4_sequence_free_slot(&lgp->res.seq_res);
9435 pnfs_layoutget_free(lgp);
9436 dprintk("<-- %s\n", __func__);
9439 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9440 .rpc_call_prepare = nfs4_layoutget_prepare,
9441 .rpc_call_done = nfs4_layoutget_done,
9442 .rpc_release = nfs4_layoutget_release,
9445 struct pnfs_layout_segment *
9446 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9448 struct inode *inode = lgp->args.inode;
9449 struct nfs_server *server = NFS_SERVER(inode);
9450 struct rpc_task *task;
9451 struct rpc_message msg = {
9452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9453 .rpc_argp = &lgp->args,
9454 .rpc_resp = &lgp->res,
9455 .rpc_cred = lgp->cred,
9457 struct rpc_task_setup task_setup_data = {
9458 .rpc_client = server->client,
9459 .rpc_message = &msg,
9460 .callback_ops = &nfs4_layoutget_call_ops,
9461 .callback_data = lgp,
9462 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
9464 struct pnfs_layout_segment *lseg = NULL;
9465 struct nfs4_exception exception = {
9467 .timeout = *timeout,
9471 dprintk("--> %s\n", __func__);
9473 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9474 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9476 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9478 task = rpc_run_task(&task_setup_data);
9480 status = rpc_wait_for_completion_task(task);
9484 if (task->tk_status < 0) {
9485 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9486 *timeout = exception.timeout;
9487 } else if (lgp->res.layoutp->len == 0) {
9489 *timeout = nfs4_update_delay(&exception.timeout);
9491 lseg = pnfs_layout_process(lgp);
9493 trace_nfs4_layoutget(lgp->args.ctx,
9500 dprintk("<-- %s status=%d\n", __func__, status);
9502 return ERR_PTR(status);
9507 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9509 struct nfs4_layoutreturn *lrp = calldata;
9511 dprintk("--> %s\n", __func__);
9512 nfs4_setup_sequence(lrp->clp,
9513 &lrp->args.seq_args,
9516 if (!pnfs_layout_is_valid(lrp->args.layout))
9520 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9522 struct nfs4_layoutreturn *lrp = calldata;
9523 struct nfs_server *server;
9525 dprintk("--> %s\n", __func__);
9527 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9531 * Was there an RPC level error? Assume the call succeeded,
9532 * and that we need to release the layout
9534 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9535 lrp->res.lrs_present = 0;
9539 server = NFS_SERVER(lrp->args.inode);
9540 switch (task->tk_status) {
9541 case -NFS4ERR_OLD_STATEID:
9542 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9548 task->tk_status = 0;
9552 case -NFS4ERR_DELAY:
9553 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9557 dprintk("<-- %s\n", __func__);
9560 task->tk_status = 0;
9561 nfs4_sequence_free_slot(&lrp->res.seq_res);
9562 rpc_restart_call_prepare(task);
9565 static void nfs4_layoutreturn_release(void *calldata)
9567 struct nfs4_layoutreturn *lrp = calldata;
9568 struct pnfs_layout_hdr *lo = lrp->args.layout;
9570 dprintk("--> %s\n", __func__);
9571 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9572 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9573 nfs4_sequence_free_slot(&lrp->res.seq_res);
9574 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9575 lrp->ld_private.ops->free(&lrp->ld_private);
9576 pnfs_put_layout_hdr(lrp->args.layout);
9577 nfs_iput_and_deactive(lrp->inode);
9578 put_cred(lrp->cred);
9580 dprintk("<-- %s\n", __func__);
9583 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9584 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9585 .rpc_call_done = nfs4_layoutreturn_done,
9586 .rpc_release = nfs4_layoutreturn_release,
9589 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9591 struct rpc_task *task;
9592 struct rpc_message msg = {
9593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9594 .rpc_argp = &lrp->args,
9595 .rpc_resp = &lrp->res,
9596 .rpc_cred = lrp->cred,
9598 struct rpc_task_setup task_setup_data = {
9599 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9600 .rpc_message = &msg,
9601 .callback_ops = &nfs4_layoutreturn_call_ops,
9602 .callback_data = lrp,
9606 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9607 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9608 &task_setup_data.rpc_client, &msg);
9610 dprintk("--> %s\n", __func__);
9612 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9614 nfs4_layoutreturn_release(lrp);
9617 task_setup_data.flags |= RPC_TASK_ASYNC;
9619 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9620 task = rpc_run_task(&task_setup_data);
9622 return PTR_ERR(task);
9624 status = task->tk_status;
9625 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9626 dprintk("<-- %s status=%d\n", __func__, status);
9632 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9633 struct pnfs_device *pdev,
9634 const struct cred *cred)
9636 struct nfs4_getdeviceinfo_args args = {
9638 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9639 NOTIFY_DEVICEID4_DELETE,
9641 struct nfs4_getdeviceinfo_res res = {
9644 struct rpc_message msg = {
9645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9652 dprintk("--> %s\n", __func__);
9653 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9654 if (res.notification & ~args.notify_types)
9655 dprintk("%s: unsupported notification\n", __func__);
9656 if (res.notification != args.notify_types)
9659 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9661 dprintk("<-- %s status=%d\n", __func__, status);
9666 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9667 struct pnfs_device *pdev,
9668 const struct cred *cred)
9670 struct nfs4_exception exception = { };
9674 err = nfs4_handle_exception(server,
9675 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9677 } while (exception.retry);
9680 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9682 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9684 struct nfs4_layoutcommit_data *data = calldata;
9685 struct nfs_server *server = NFS_SERVER(data->args.inode);
9687 nfs4_setup_sequence(server->nfs_client,
9688 &data->args.seq_args,
9694 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9696 struct nfs4_layoutcommit_data *data = calldata;
9697 struct nfs_server *server = NFS_SERVER(data->args.inode);
9699 if (!nfs41_sequence_done(task, &data->res.seq_res))
9702 switch (task->tk_status) { /* Just ignore these failures */
9703 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9704 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9705 case -NFS4ERR_BADLAYOUT: /* no layout */
9706 case -NFS4ERR_GRACE: /* loca_recalim always false */
9707 task->tk_status = 0;
9711 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9712 rpc_restart_call_prepare(task);
9718 static void nfs4_layoutcommit_release(void *calldata)
9720 struct nfs4_layoutcommit_data *data = calldata;
9722 pnfs_cleanup_layoutcommit(data);
9723 nfs_post_op_update_inode_force_wcc(data->args.inode,
9725 put_cred(data->cred);
9726 nfs_iput_and_deactive(data->inode);
9730 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9731 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9732 .rpc_call_done = nfs4_layoutcommit_done,
9733 .rpc_release = nfs4_layoutcommit_release,
9737 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9739 struct rpc_message msg = {
9740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9741 .rpc_argp = &data->args,
9742 .rpc_resp = &data->res,
9743 .rpc_cred = data->cred,
9745 struct rpc_task_setup task_setup_data = {
9746 .task = &data->task,
9747 .rpc_client = NFS_CLIENT(data->args.inode),
9748 .rpc_message = &msg,
9749 .callback_ops = &nfs4_layoutcommit_ops,
9750 .callback_data = data,
9752 struct rpc_task *task;
9755 dprintk("NFS: initiating layoutcommit call. sync %d "
9756 "lbw: %llu inode %lu\n", sync,
9757 data->args.lastbytewritten,
9758 data->args.inode->i_ino);
9761 data->inode = nfs_igrab_and_active(data->args.inode);
9762 if (data->inode == NULL) {
9763 nfs4_layoutcommit_release(data);
9766 task_setup_data.flags = RPC_TASK_ASYNC;
9768 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9769 task = rpc_run_task(&task_setup_data);
9771 return PTR_ERR(task);
9773 status = task->tk_status;
9774 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9775 dprintk("%s: status %d\n", __func__, status);
9781 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9782 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9785 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9786 struct nfs_fsinfo *info,
9787 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9789 struct nfs41_secinfo_no_name_args args = {
9790 .style = SECINFO_STYLE_CURRENT_FH,
9792 struct nfs4_secinfo_res res = {
9795 struct rpc_message msg = {
9796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9800 struct nfs4_call_sync_data data = {
9801 .seq_server = server,
9802 .seq_args = &args.seq_args,
9803 .seq_res = &res.seq_res,
9805 struct rpc_task_setup task_setup = {
9806 .rpc_client = server->client,
9807 .rpc_message = &msg,
9808 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9809 .callback_data = &data,
9810 .flags = RPC_TASK_NO_ROUND_ROBIN,
9812 const struct cred *cred = NULL;
9815 if (use_integrity) {
9816 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
9818 cred = nfs4_get_clid_cred(server->nfs_client);
9819 msg.rpc_cred = cred;
9822 dprintk("--> %s\n", __func__);
9823 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9824 status = nfs4_call_sync_custom(&task_setup);
9825 dprintk("<-- %s status=%d\n", __func__, status);
9833 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9834 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9836 struct nfs4_exception exception = {
9837 .interruptible = true,
9841 /* first try using integrity protection */
9842 err = -NFS4ERR_WRONGSEC;
9844 /* try to use integrity protection with machine cred */
9845 if (_nfs4_is_integrity_protected(server->nfs_client))
9846 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9850 * if unable to use integrity protection, or SECINFO with
9851 * integrity protection returns NFS4ERR_WRONGSEC (which is
9852 * disallowed by spec, but exists in deployed servers) use
9853 * the current filesystem's rpc_client and the user cred.
9855 if (err == -NFS4ERR_WRONGSEC)
9856 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9861 case -NFS4ERR_WRONGSEC:
9865 err = nfs4_handle_exception(server, err, &exception);
9867 } while (exception.retry);
9873 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9874 struct nfs_fsinfo *info)
9878 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9879 struct nfs4_secinfo_flavors *flavors;
9880 struct nfs4_secinfo4 *secinfo;
9883 page = alloc_page(GFP_KERNEL);
9889 flavors = page_address(page);
9890 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9893 * Fall back on "guess and check" method if
9894 * the server doesn't support SECINFO_NO_NAME
9896 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9897 err = nfs4_find_root_sec(server, fhandle, info);
9903 for (i = 0; i < flavors->num_flavors; i++) {
9904 secinfo = &flavors->flavors[i];
9906 switch (secinfo->flavor) {
9910 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9911 &secinfo->flavor_info);
9914 flavor = RPC_AUTH_MAXFLAVOR;
9918 if (!nfs_auth_info_match(&server->auth_info, flavor))
9919 flavor = RPC_AUTH_MAXFLAVOR;
9921 if (flavor != RPC_AUTH_MAXFLAVOR) {
9922 err = nfs4_lookup_root_sec(server, fhandle,
9929 if (flavor == RPC_AUTH_MAXFLAVOR)
9940 static int _nfs41_test_stateid(struct nfs_server *server,
9941 nfs4_stateid *stateid,
9942 const struct cred *cred)
9945 struct nfs41_test_stateid_args args = {
9948 struct nfs41_test_stateid_res res;
9949 struct rpc_message msg = {
9950 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9955 struct rpc_clnt *rpc_client = server->client;
9957 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9960 dprintk("NFS call test_stateid %p\n", stateid);
9961 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9962 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9963 &args.seq_args, &res.seq_res);
9964 if (status != NFS_OK) {
9965 dprintk("NFS reply test_stateid: failed, %d\n", status);
9968 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9972 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9973 int err, struct nfs4_exception *exception)
9975 exception->retry = 0;
9977 case -NFS4ERR_DELAY:
9978 case -NFS4ERR_RETRY_UNCACHED_REP:
9979 nfs4_handle_exception(server, err, exception);
9981 case -NFS4ERR_BADSESSION:
9982 case -NFS4ERR_BADSLOT:
9983 case -NFS4ERR_BAD_HIGH_SLOT:
9984 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9985 case -NFS4ERR_DEADSESSION:
9986 nfs4_do_handle_exception(server, err, exception);
9991 * nfs41_test_stateid - perform a TEST_STATEID operation
9993 * @server: server / transport on which to perform the operation
9994 * @stateid: state ID to test
9997 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9998 * Otherwise a negative NFS4ERR value is returned if the operation
9999 * failed or the state ID is not currently valid.
10001 static int nfs41_test_stateid(struct nfs_server *server,
10002 nfs4_stateid *stateid,
10003 const struct cred *cred)
10005 struct nfs4_exception exception = {
10006 .interruptible = true,
10010 err = _nfs41_test_stateid(server, stateid, cred);
10011 nfs4_handle_delay_or_session_error(server, err, &exception);
10012 } while (exception.retry);
10016 struct nfs_free_stateid_data {
10017 struct nfs_server *server;
10018 struct nfs41_free_stateid_args args;
10019 struct nfs41_free_stateid_res res;
10022 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10024 struct nfs_free_stateid_data *data = calldata;
10025 nfs4_setup_sequence(data->server->nfs_client,
10026 &data->args.seq_args,
10027 &data->res.seq_res,
10031 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10033 struct nfs_free_stateid_data *data = calldata;
10035 nfs41_sequence_done(task, &data->res.seq_res);
10037 switch (task->tk_status) {
10038 case -NFS4ERR_DELAY:
10039 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10040 rpc_restart_call_prepare(task);
10044 static void nfs41_free_stateid_release(void *calldata)
10049 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10050 .rpc_call_prepare = nfs41_free_stateid_prepare,
10051 .rpc_call_done = nfs41_free_stateid_done,
10052 .rpc_release = nfs41_free_stateid_release,
10056 * nfs41_free_stateid - perform a FREE_STATEID operation
10058 * @server: server / transport on which to perform the operation
10059 * @stateid: state ID to release
10060 * @cred: credential
10061 * @privileged: set to true if this call needs to be privileged
10063 * Note: this function is always asynchronous.
10065 static int nfs41_free_stateid(struct nfs_server *server,
10066 const nfs4_stateid *stateid,
10067 const struct cred *cred,
10070 struct rpc_message msg = {
10071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10074 struct rpc_task_setup task_setup = {
10075 .rpc_client = server->client,
10076 .rpc_message = &msg,
10077 .callback_ops = &nfs41_free_stateid_ops,
10078 .flags = RPC_TASK_ASYNC,
10080 struct nfs_free_stateid_data *data;
10081 struct rpc_task *task;
10083 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10084 &task_setup.rpc_client, &msg);
10086 dprintk("NFS call free_stateid %p\n", stateid);
10087 data = kmalloc(sizeof(*data), GFP_NOFS);
10090 data->server = server;
10091 nfs4_stateid_copy(&data->args.stateid, stateid);
10093 task_setup.callback_data = data;
10095 msg.rpc_argp = &data->args;
10096 msg.rpc_resp = &data->res;
10097 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10098 task = rpc_run_task(&task_setup);
10100 return PTR_ERR(task);
10101 rpc_put_task(task);
10106 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10108 const struct cred *cred = lsp->ls_state->owner->so_cred;
10110 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10111 nfs4_free_lock_state(server, lsp);
10114 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10115 const nfs4_stateid *s2)
10117 if (s1->type != s2->type)
10120 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10123 if (s1->seqid == s2->seqid)
10126 return s1->seqid == 0 || s2->seqid == 0;
10129 #endif /* CONFIG_NFS_V4_1 */
10131 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10132 const nfs4_stateid *s2)
10134 return nfs4_stateid_match(s1, s2);
10138 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10139 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10140 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10141 .recover_open = nfs4_open_reclaim,
10142 .recover_lock = nfs4_lock_reclaim,
10143 .establish_clid = nfs4_init_clientid,
10144 .detect_trunking = nfs40_discover_server_trunking,
10147 #if defined(CONFIG_NFS_V4_1)
10148 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10149 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10150 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10151 .recover_open = nfs4_open_reclaim,
10152 .recover_lock = nfs4_lock_reclaim,
10153 .establish_clid = nfs41_init_clientid,
10154 .reclaim_complete = nfs41_proc_reclaim_complete,
10155 .detect_trunking = nfs41_discover_server_trunking,
10157 #endif /* CONFIG_NFS_V4_1 */
10159 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10160 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10161 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10162 .recover_open = nfs40_open_expired,
10163 .recover_lock = nfs4_lock_expired,
10164 .establish_clid = nfs4_init_clientid,
10167 #if defined(CONFIG_NFS_V4_1)
10168 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10169 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10170 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10171 .recover_open = nfs41_open_expired,
10172 .recover_lock = nfs41_lock_expired,
10173 .establish_clid = nfs41_init_clientid,
10175 #endif /* CONFIG_NFS_V4_1 */
10177 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10178 .sched_state_renewal = nfs4_proc_async_renew,
10179 .get_state_renewal_cred = nfs4_get_renew_cred,
10180 .renew_lease = nfs4_proc_renew,
10183 #if defined(CONFIG_NFS_V4_1)
10184 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10185 .sched_state_renewal = nfs41_proc_async_sequence,
10186 .get_state_renewal_cred = nfs4_get_machine_cred,
10187 .renew_lease = nfs4_proc_sequence,
10191 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10192 .get_locations = _nfs40_proc_get_locations,
10193 .fsid_present = _nfs40_proc_fsid_present,
10196 #if defined(CONFIG_NFS_V4_1)
10197 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10198 .get_locations = _nfs41_proc_get_locations,
10199 .fsid_present = _nfs41_proc_fsid_present,
10201 #endif /* CONFIG_NFS_V4_1 */
10203 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10204 .minor_version = 0,
10205 .init_caps = NFS_CAP_READDIRPLUS
10206 | NFS_CAP_ATOMIC_OPEN
10207 | NFS_CAP_POSIX_LOCK,
10208 .init_client = nfs40_init_client,
10209 .shutdown_client = nfs40_shutdown_client,
10210 .match_stateid = nfs4_match_stateid,
10211 .find_root_sec = nfs4_find_root_sec,
10212 .free_lock_state = nfs4_release_lockowner,
10213 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10214 .alloc_seqid = nfs_alloc_seqid,
10215 .call_sync_ops = &nfs40_call_sync_ops,
10216 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10217 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10218 .state_renewal_ops = &nfs40_state_renewal_ops,
10219 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10222 #if defined(CONFIG_NFS_V4_1)
10223 static struct nfs_seqid *
10224 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10229 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10230 .minor_version = 1,
10231 .init_caps = NFS_CAP_READDIRPLUS
10232 | NFS_CAP_ATOMIC_OPEN
10233 | NFS_CAP_POSIX_LOCK
10234 | NFS_CAP_STATEID_NFSV41
10235 | NFS_CAP_ATOMIC_OPEN_V1
10237 .init_client = nfs41_init_client,
10238 .shutdown_client = nfs41_shutdown_client,
10239 .match_stateid = nfs41_match_stateid,
10240 .find_root_sec = nfs41_find_root_sec,
10241 .free_lock_state = nfs41_free_lock_state,
10242 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10243 .alloc_seqid = nfs_alloc_no_seqid,
10244 .session_trunk = nfs4_test_session_trunk,
10245 .call_sync_ops = &nfs41_call_sync_ops,
10246 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10247 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10248 .state_renewal_ops = &nfs41_state_renewal_ops,
10249 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10253 #if defined(CONFIG_NFS_V4_2)
10254 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10255 .minor_version = 2,
10256 .init_caps = NFS_CAP_READDIRPLUS
10257 | NFS_CAP_ATOMIC_OPEN
10258 | NFS_CAP_POSIX_LOCK
10259 | NFS_CAP_STATEID_NFSV41
10260 | NFS_CAP_ATOMIC_OPEN_V1
10264 | NFS_CAP_OFFLOAD_CANCEL
10265 | NFS_CAP_COPY_NOTIFY
10266 | NFS_CAP_DEALLOCATE
10268 | NFS_CAP_LAYOUTSTATS
10270 | NFS_CAP_LAYOUTERROR
10271 | NFS_CAP_READ_PLUS,
10272 .init_client = nfs41_init_client,
10273 .shutdown_client = nfs41_shutdown_client,
10274 .match_stateid = nfs41_match_stateid,
10275 .find_root_sec = nfs41_find_root_sec,
10276 .free_lock_state = nfs41_free_lock_state,
10277 .call_sync_ops = &nfs41_call_sync_ops,
10278 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10279 .alloc_seqid = nfs_alloc_no_seqid,
10280 .session_trunk = nfs4_test_session_trunk,
10281 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10282 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10283 .state_renewal_ops = &nfs41_state_renewal_ops,
10284 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10288 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10289 [0] = &nfs_v4_0_minor_ops,
10290 #if defined(CONFIG_NFS_V4_1)
10291 [1] = &nfs_v4_1_minor_ops,
10293 #if defined(CONFIG_NFS_V4_2)
10294 [2] = &nfs_v4_2_minor_ops,
10298 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10300 ssize_t error, error2, error3;
10302 error = generic_listxattr(dentry, list, size);
10310 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10319 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10323 return error + error2 + error3;
10326 static const struct inode_operations nfs4_dir_inode_operations = {
10327 .create = nfs_create,
10328 .lookup = nfs_lookup,
10329 .atomic_open = nfs_atomic_open,
10331 .unlink = nfs_unlink,
10332 .symlink = nfs_symlink,
10333 .mkdir = nfs_mkdir,
10334 .rmdir = nfs_rmdir,
10335 .mknod = nfs_mknod,
10336 .rename = nfs_rename,
10337 .permission = nfs_permission,
10338 .getattr = nfs_getattr,
10339 .setattr = nfs_setattr,
10340 .listxattr = nfs4_listxattr,
10343 static const struct inode_operations nfs4_file_inode_operations = {
10344 .permission = nfs_permission,
10345 .getattr = nfs_getattr,
10346 .setattr = nfs_setattr,
10347 .listxattr = nfs4_listxattr,
10350 const struct nfs_rpc_ops nfs_v4_clientops = {
10351 .version = 4, /* protocol version */
10352 .dentry_ops = &nfs4_dentry_operations,
10353 .dir_inode_ops = &nfs4_dir_inode_operations,
10354 .file_inode_ops = &nfs4_file_inode_operations,
10355 .file_ops = &nfs4_file_operations,
10356 .getroot = nfs4_proc_get_root,
10357 .submount = nfs4_submount,
10358 .try_get_tree = nfs4_try_get_tree,
10359 .getattr = nfs4_proc_getattr,
10360 .setattr = nfs4_proc_setattr,
10361 .lookup = nfs4_proc_lookup,
10362 .lookupp = nfs4_proc_lookupp,
10363 .access = nfs4_proc_access,
10364 .readlink = nfs4_proc_readlink,
10365 .create = nfs4_proc_create,
10366 .remove = nfs4_proc_remove,
10367 .unlink_setup = nfs4_proc_unlink_setup,
10368 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10369 .unlink_done = nfs4_proc_unlink_done,
10370 .rename_setup = nfs4_proc_rename_setup,
10371 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10372 .rename_done = nfs4_proc_rename_done,
10373 .link = nfs4_proc_link,
10374 .symlink = nfs4_proc_symlink,
10375 .mkdir = nfs4_proc_mkdir,
10376 .rmdir = nfs4_proc_rmdir,
10377 .readdir = nfs4_proc_readdir,
10378 .mknod = nfs4_proc_mknod,
10379 .statfs = nfs4_proc_statfs,
10380 .fsinfo = nfs4_proc_fsinfo,
10381 .pathconf = nfs4_proc_pathconf,
10382 .set_capabilities = nfs4_server_capabilities,
10383 .decode_dirent = nfs4_decode_dirent,
10384 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10385 .read_setup = nfs4_proc_read_setup,
10386 .read_done = nfs4_read_done,
10387 .write_setup = nfs4_proc_write_setup,
10388 .write_done = nfs4_write_done,
10389 .commit_setup = nfs4_proc_commit_setup,
10390 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10391 .commit_done = nfs4_commit_done,
10392 .lock = nfs4_proc_lock,
10393 .clear_acl_cache = nfs4_zap_acl_attr,
10394 .close_context = nfs4_close_context,
10395 .open_context = nfs4_atomic_open,
10396 .have_delegation = nfs4_have_delegation,
10397 .alloc_client = nfs4_alloc_client,
10398 .init_client = nfs4_init_client,
10399 .free_client = nfs4_free_client,
10400 .create_server = nfs4_create_server,
10401 .clone_server = nfs_clone_server,
10404 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10405 .name = XATTR_NAME_NFSV4_ACL,
10406 .list = nfs4_xattr_list_nfs4_acl,
10407 .get = nfs4_xattr_get_nfs4_acl,
10408 .set = nfs4_xattr_set_nfs4_acl,
10411 #ifdef CONFIG_NFS_V4_2
10412 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10413 .prefix = XATTR_USER_PREFIX,
10414 .get = nfs4_xattr_get_nfs4_user,
10415 .set = nfs4_xattr_set_nfs4_user,
10419 const struct xattr_handler *nfs4_xattr_handlers[] = {
10420 &nfs4_xattr_nfs4_acl_handler,
10421 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10422 &nfs4_xattr_nfs4_label_handler,
10424 #ifdef CONFIG_NFS_V4_2
10425 &nfs4_xattr_nfs4_user_handler,
10432 * c-basic-offset: 8