4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
57 #include <linux/iversion.h>
60 #include "delegation.h"
66 #include "nfs4idmap.h"
67 #include "nfs4session.h"
70 #include "nfs4trace.h"
72 #define NFSDBG_FACILITY NFSDBG_PROC
74 #define NFS4_BITMASK_SZ 3
76 #define NFS4_POLL_RETRY_MIN (HZ/10)
77 #define NFS4_POLL_RETRY_MAX (15*HZ)
79 /* file attributes which can be mapped to nfs attributes */
80 #define NFS4_VALID_ATTRS (ATTR_MODE \
91 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
92 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
93 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
94 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label, struct inode *inode);
95 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
96 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
97 struct nfs_fattr *fattr, struct iattr *sattr,
98 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
99 struct nfs4_label *olabel);
100 #ifdef CONFIG_NFS_V4_1
101 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
102 const struct cred *cred,
103 struct nfs4_slot *slot,
105 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
106 const struct cred *);
107 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
108 const struct cred *, bool);
111 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
112 static inline struct nfs4_label *
113 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
114 struct iattr *sattr, struct nfs4_label *label)
121 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
129 err = security_dentry_init_security(dentry, sattr->ia_mode,
130 &dentry->d_name, (void **)&label->label, &label->len);
137 nfs4_label_release_security(struct nfs4_label *label)
140 security_release_secctx(label->label, label->len);
142 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
145 return server->attr_bitmask;
147 return server->attr_bitmask_nl;
150 static inline struct nfs4_label *
151 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
152 struct iattr *sattr, struct nfs4_label *l)
155 nfs4_label_release_security(struct nfs4_label *label)
158 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
159 { return server->attr_bitmask; }
162 /* Prevent leaks of NFSv4 errors into userland */
163 static int nfs4_map_errors(int err)
168 case -NFS4ERR_RESOURCE:
169 case -NFS4ERR_LAYOUTTRYLATER:
170 case -NFS4ERR_RECALLCONFLICT:
172 case -NFS4ERR_WRONGSEC:
173 case -NFS4ERR_WRONG_CRED:
175 case -NFS4ERR_BADOWNER:
176 case -NFS4ERR_BADNAME:
178 case -NFS4ERR_SHARE_DENIED:
180 case -NFS4ERR_MINOR_VERS_MISMATCH:
181 return -EPROTONOSUPPORT;
182 case -NFS4ERR_FILE_OPEN:
185 dprintk("%s could not handle NFSv4 error %d\n",
193 * This is our standard bitmap for GETATTR requests.
195 const u32 nfs4_fattr_bitmap[3] = {
197 | FATTR4_WORD0_CHANGE
200 | FATTR4_WORD0_FILEID,
202 | FATTR4_WORD1_NUMLINKS
204 | FATTR4_WORD1_OWNER_GROUP
205 | FATTR4_WORD1_RAWDEV
206 | FATTR4_WORD1_SPACE_USED
207 | FATTR4_WORD1_TIME_ACCESS
208 | FATTR4_WORD1_TIME_METADATA
209 | FATTR4_WORD1_TIME_MODIFY
210 | FATTR4_WORD1_MOUNTED_ON_FILEID,
211 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
212 FATTR4_WORD2_SECURITY_LABEL
216 static const u32 nfs4_pnfs_open_bitmap[3] = {
218 | FATTR4_WORD0_CHANGE
221 | FATTR4_WORD0_FILEID,
223 | FATTR4_WORD1_NUMLINKS
225 | FATTR4_WORD1_OWNER_GROUP
226 | FATTR4_WORD1_RAWDEV
227 | FATTR4_WORD1_SPACE_USED
228 | FATTR4_WORD1_TIME_ACCESS
229 | FATTR4_WORD1_TIME_METADATA
230 | FATTR4_WORD1_TIME_MODIFY,
231 FATTR4_WORD2_MDSTHRESHOLD
232 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
233 | FATTR4_WORD2_SECURITY_LABEL
237 static const u32 nfs4_open_noattr_bitmap[3] = {
239 | FATTR4_WORD0_FILEID,
242 const u32 nfs4_statfs_bitmap[3] = {
243 FATTR4_WORD0_FILES_AVAIL
244 | FATTR4_WORD0_FILES_FREE
245 | FATTR4_WORD0_FILES_TOTAL,
246 FATTR4_WORD1_SPACE_AVAIL
247 | FATTR4_WORD1_SPACE_FREE
248 | FATTR4_WORD1_SPACE_TOTAL
251 const u32 nfs4_pathconf_bitmap[3] = {
253 | FATTR4_WORD0_MAXNAME,
257 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
258 | FATTR4_WORD0_MAXREAD
259 | FATTR4_WORD0_MAXWRITE
260 | FATTR4_WORD0_LEASE_TIME,
261 FATTR4_WORD1_TIME_DELTA
262 | FATTR4_WORD1_FS_LAYOUT_TYPES,
263 FATTR4_WORD2_LAYOUT_BLKSIZE
264 | FATTR4_WORD2_CLONE_BLKSIZE
267 const u32 nfs4_fs_locations_bitmap[3] = {
271 | FATTR4_WORD0_FILEID
272 | FATTR4_WORD0_FS_LOCATIONS,
274 | FATTR4_WORD1_OWNER_GROUP
275 | FATTR4_WORD1_RAWDEV
276 | FATTR4_WORD1_SPACE_USED
277 | FATTR4_WORD1_TIME_ACCESS
278 | FATTR4_WORD1_TIME_METADATA
279 | FATTR4_WORD1_TIME_MODIFY
280 | FATTR4_WORD1_MOUNTED_ON_FILEID,
283 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
286 unsigned long cache_validity;
288 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
289 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
292 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
293 if (!(cache_validity & NFS_INO_REVAL_FORCED))
294 cache_validity &= ~(NFS_INO_INVALID_CHANGE
295 | NFS_INO_INVALID_SIZE);
297 if (!(cache_validity & NFS_INO_INVALID_SIZE))
298 dst[0] &= ~FATTR4_WORD0_SIZE;
300 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
301 dst[0] &= ~FATTR4_WORD0_CHANGE;
304 static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
305 const __u32 *src, struct inode *inode)
307 nfs4_bitmap_copy_adjust(dst, src, inode);
310 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
311 struct nfs4_readdir_arg *readdir)
313 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
317 readdir->cookie = cookie;
318 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
323 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
328 * NFSv4 servers do not return entries for '.' and '..'
329 * Therefore, we fake these entries here. We let '.'
330 * have cookie 0 and '..' have cookie 1. Note that
331 * when talking to the server, we always send cookie 0
334 start = p = kmap_atomic(*readdir->pages);
337 *p++ = xdr_one; /* next */
338 *p++ = xdr_zero; /* cookie, first word */
339 *p++ = xdr_one; /* cookie, second word */
340 *p++ = xdr_one; /* entry len */
341 memcpy(p, ".\0\0\0", 4); /* entry */
343 *p++ = xdr_one; /* bitmap length */
344 *p++ = htonl(attrs); /* bitmap */
345 *p++ = htonl(12); /* attribute buffer length */
346 *p++ = htonl(NF4DIR);
347 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
350 *p++ = xdr_one; /* next */
351 *p++ = xdr_zero; /* cookie, first word */
352 *p++ = xdr_two; /* cookie, second word */
353 *p++ = xdr_two; /* entry len */
354 memcpy(p, "..\0\0", 4); /* entry */
356 *p++ = xdr_one; /* bitmap length */
357 *p++ = htonl(attrs); /* bitmap */
358 *p++ = htonl(12); /* attribute buffer length */
359 *p++ = htonl(NF4DIR);
360 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
362 readdir->pgbase = (char *)p - (char *)start;
363 readdir->count -= readdir->pgbase;
364 kunmap_atomic(start);
367 static void nfs4_fattr_set_prechange(struct nfs_fattr *fattr, u64 version)
369 if (!(fattr->valid & NFS_ATTR_FATTR_PRECHANGE)) {
370 fattr->pre_change_attr = version;
371 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
375 static void nfs4_test_and_free_stateid(struct nfs_server *server,
376 nfs4_stateid *stateid,
377 const struct cred *cred)
379 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
381 ops->test_and_free_expired(server, stateid, cred);
384 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
385 nfs4_stateid *stateid,
386 const struct cred *cred)
388 stateid->type = NFS4_REVOKED_STATEID_TYPE;
389 nfs4_test_and_free_stateid(server, stateid, cred);
392 static void nfs4_free_revoked_stateid(struct nfs_server *server,
393 const nfs4_stateid *stateid,
394 const struct cred *cred)
398 nfs4_stateid_copy(&tmp, stateid);
399 __nfs4_free_revoked_stateid(server, &tmp, cred);
402 static long nfs4_update_delay(long *timeout)
406 return NFS4_POLL_RETRY_MAX;
408 *timeout = NFS4_POLL_RETRY_MIN;
409 if (*timeout > NFS4_POLL_RETRY_MAX)
410 *timeout = NFS4_POLL_RETRY_MAX;
416 static int nfs4_delay_killable(long *timeout)
420 freezable_schedule_timeout_killable_unsafe(
421 nfs4_update_delay(timeout));
422 if (!__fatal_signal_pending(current))
427 static int nfs4_delay_interruptible(long *timeout)
431 freezable_schedule_timeout_interruptible(nfs4_update_delay(timeout));
432 if (!signal_pending(current))
434 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
437 static int nfs4_delay(long *timeout, bool interruptible)
440 return nfs4_delay_interruptible(timeout);
441 return nfs4_delay_killable(timeout);
444 static const nfs4_stateid *
445 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
449 switch (stateid->type) {
450 case NFS4_OPEN_STATEID_TYPE:
451 case NFS4_LOCK_STATEID_TYPE:
452 case NFS4_DELEGATION_STATEID_TYPE:
460 /* This is the error handling routine for processes that are allowed
463 static int nfs4_do_handle_exception(struct nfs_server *server,
464 int errorcode, struct nfs4_exception *exception)
466 struct nfs_client *clp = server->nfs_client;
467 struct nfs4_state *state = exception->state;
468 const nfs4_stateid *stateid;
469 struct inode *inode = exception->inode;
472 exception->delay = 0;
473 exception->recovering = 0;
474 exception->retry = 0;
476 stateid = nfs4_recoverable_stateid(exception->stateid);
477 if (stateid == NULL && state != NULL)
478 stateid = nfs4_recoverable_stateid(&state->stateid);
483 case -NFS4ERR_BADHANDLE:
485 if (inode != NULL && S_ISREG(inode->i_mode))
486 pnfs_destroy_layout(NFS_I(inode));
488 case -NFS4ERR_DELEG_REVOKED:
489 case -NFS4ERR_ADMIN_REVOKED:
490 case -NFS4ERR_EXPIRED:
491 case -NFS4ERR_BAD_STATEID:
492 if (inode != NULL && stateid != NULL) {
493 nfs_inode_find_state_and_recover(inode,
495 goto wait_on_recovery;
498 case -NFS4ERR_OPENMODE:
502 err = nfs_async_inode_return_delegation(inode,
505 goto wait_on_recovery;
506 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
507 exception->retry = 1;
513 ret = nfs4_schedule_stateid_recovery(server, state);
516 goto wait_on_recovery;
517 case -NFS4ERR_STALE_STATEID:
518 case -NFS4ERR_STALE_CLIENTID:
519 nfs4_schedule_lease_recovery(clp);
520 goto wait_on_recovery;
522 ret = nfs4_schedule_migration_recovery(server);
525 goto wait_on_recovery;
526 case -NFS4ERR_LEASE_MOVED:
527 nfs4_schedule_lease_moved_recovery(clp);
528 goto wait_on_recovery;
529 #if defined(CONFIG_NFS_V4_1)
530 case -NFS4ERR_BADSESSION:
531 case -NFS4ERR_BADSLOT:
532 case -NFS4ERR_BAD_HIGH_SLOT:
533 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
534 case -NFS4ERR_DEADSESSION:
535 case -NFS4ERR_SEQ_FALSE_RETRY:
536 case -NFS4ERR_SEQ_MISORDERED:
537 /* Handled in nfs41_sequence_process() */
538 goto wait_on_recovery;
539 #endif /* defined(CONFIG_NFS_V4_1) */
540 case -NFS4ERR_FILE_OPEN:
541 if (exception->timeout > HZ) {
542 /* We have retried a decent amount, time to
550 nfs_inc_server_stats(server, NFSIOS_DELAY);
553 case -NFS4ERR_LAYOUTTRYLATER:
554 case -NFS4ERR_RECALLCONFLICT:
555 exception->delay = 1;
558 case -NFS4ERR_RETRY_UNCACHED_REP:
559 case -NFS4ERR_OLD_STATEID:
560 exception->retry = 1;
562 case -NFS4ERR_BADOWNER:
563 /* The following works around a Linux server bug! */
564 case -NFS4ERR_BADNAME:
565 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
566 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
567 exception->retry = 1;
568 printk(KERN_WARNING "NFS: v4 server %s "
569 "does not accept raw "
571 "Reenabling the idmapper.\n",
572 server->nfs_client->cl_hostname);
575 /* We failed to handle the error */
576 return nfs4_map_errors(ret);
578 exception->recovering = 1;
582 /* This is the error handling routine for processes that are allowed
585 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
587 struct nfs_client *clp = server->nfs_client;
590 ret = nfs4_do_handle_exception(server, errorcode, exception);
591 if (exception->delay) {
592 ret = nfs4_delay(&exception->timeout,
593 exception->interruptible);
596 if (exception->recovering) {
597 if (exception->task_is_privileged)
599 ret = nfs4_wait_clnt_recover(clp);
600 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
607 exception->retry = 1;
612 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
613 int errorcode, struct nfs4_exception *exception)
615 struct nfs_client *clp = server->nfs_client;
618 ret = nfs4_do_handle_exception(server, errorcode, exception);
619 if (exception->delay) {
620 rpc_delay(task, nfs4_update_delay(&exception->timeout));
623 if (exception->recovering) {
624 if (exception->task_is_privileged)
626 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
627 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
628 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
631 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
636 exception->retry = 1;
638 * For NFS4ERR_MOVED, the client transport will need to
639 * be recomputed after migration recovery has completed.
641 if (errorcode == -NFS4ERR_MOVED)
642 rpc_task_release_transport(task);
648 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
649 struct nfs4_state *state, long *timeout)
651 struct nfs4_exception exception = {
655 if (task->tk_status >= 0)
658 exception.timeout = *timeout;
659 task->tk_status = nfs4_async_handle_exception(task, server,
662 if (exception.delay && timeout)
663 *timeout = exception.timeout;
670 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
671 * or 'false' otherwise.
673 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
675 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
676 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
679 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
681 spin_lock(&clp->cl_lock);
682 if (time_before(clp->cl_last_renewal,timestamp))
683 clp->cl_last_renewal = timestamp;
684 spin_unlock(&clp->cl_lock);
687 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
689 struct nfs_client *clp = server->nfs_client;
691 if (!nfs4_has_session(clp))
692 do_renew_lease(clp, timestamp);
695 struct nfs4_call_sync_data {
696 const struct nfs_server *seq_server;
697 struct nfs4_sequence_args *seq_args;
698 struct nfs4_sequence_res *seq_res;
701 void nfs4_init_sequence(struct nfs4_sequence_args *args,
702 struct nfs4_sequence_res *res, int cache_reply,
705 args->sa_slot = NULL;
706 args->sa_cache_this = cache_reply;
707 args->sa_privileged = privileged;
712 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
714 struct nfs4_slot *slot = res->sr_slot;
715 struct nfs4_slot_table *tbl;
718 spin_lock(&tbl->slot_tbl_lock);
719 if (!nfs41_wake_and_assign_slot(tbl, slot))
720 nfs4_free_slot(tbl, slot);
721 spin_unlock(&tbl->slot_tbl_lock);
726 static int nfs40_sequence_done(struct rpc_task *task,
727 struct nfs4_sequence_res *res)
729 if (res->sr_slot != NULL)
730 nfs40_sequence_free_slot(res);
734 #if defined(CONFIG_NFS_V4_1)
736 static void nfs41_release_slot(struct nfs4_slot *slot)
738 struct nfs4_session *session;
739 struct nfs4_slot_table *tbl;
740 bool send_new_highest_used_slotid = false;
745 session = tbl->session;
747 /* Bump the slot sequence number */
752 spin_lock(&tbl->slot_tbl_lock);
753 /* Be nice to the server: try to ensure that the last transmitted
754 * value for highest_user_slotid <= target_highest_slotid
756 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
757 send_new_highest_used_slotid = true;
759 if (nfs41_wake_and_assign_slot(tbl, slot)) {
760 send_new_highest_used_slotid = false;
763 nfs4_free_slot(tbl, slot);
765 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
766 send_new_highest_used_slotid = false;
768 spin_unlock(&tbl->slot_tbl_lock);
769 if (send_new_highest_used_slotid)
770 nfs41_notify_server(session->clp);
771 if (waitqueue_active(&tbl->slot_waitq))
772 wake_up_all(&tbl->slot_waitq);
775 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
777 nfs41_release_slot(res->sr_slot);
781 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
784 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
785 slot->seq_nr_highest_sent = seqnr;
787 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot, u32 seqnr)
789 nfs4_slot_sequence_record_sent(slot, seqnr);
790 slot->seq_nr_last_acked = seqnr;
793 static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
794 struct nfs4_slot *slot)
796 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
798 rpc_put_task_async(task);
801 static int nfs41_sequence_process(struct rpc_task *task,
802 struct nfs4_sequence_res *res)
804 struct nfs4_session *session;
805 struct nfs4_slot *slot = res->sr_slot;
806 struct nfs_client *clp;
812 /* don't increment the sequence number if the task wasn't sent */
813 if (!RPC_WAS_SENT(task) || slot->seq_done)
816 session = slot->table->session;
819 trace_nfs4_sequence_done(session, res);
821 status = res->sr_status;
822 if (task->tk_status == -NFS4ERR_DEADSESSION)
823 status = -NFS4ERR_DEADSESSION;
825 /* Check the SEQUENCE operation status */
828 /* Mark this sequence number as having been acked */
829 nfs4_slot_sequence_acked(slot, slot->seq_nr);
830 /* Update the slot's sequence and clientid lease timer */
832 do_renew_lease(clp, res->sr_timestamp);
833 /* Check sequence flags */
834 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
836 nfs41_update_target_slotid(slot->table, slot, res);
840 * sr_status remains 1 if an RPC level error occurred.
841 * The server may or may not have processed the sequence
844 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
848 /* The server detected a resend of the RPC call and
849 * returned NFS4ERR_DELAY as per Section 2.10.6.2
852 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
857 case -NFS4ERR_RETRY_UNCACHED_REP:
858 case -NFS4ERR_SEQ_FALSE_RETRY:
860 * The server thinks we tried to replay a request.
861 * Retry the call after bumping the sequence ID.
863 nfs4_slot_sequence_acked(slot, slot->seq_nr);
865 case -NFS4ERR_BADSLOT:
867 * The slot id we used was probably retired. Try again
868 * using a different slot id.
870 if (slot->slot_nr < slot->table->target_highest_slotid)
871 goto session_recover;
873 case -NFS4ERR_SEQ_MISORDERED:
874 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
876 * Were one or more calls using this slot interrupted?
877 * If the server never received the request, then our
878 * transmitted slot sequence number may be too high. However,
879 * if the server did receive the request then it might
880 * accidentally give us a reply with a mismatched operation.
881 * We can sort this out by sending a lone sequence operation
882 * to the server on the same slot.
884 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
886 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
887 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
894 * A retry might be sent while the original request is
895 * still in progress on the replier. The replier SHOULD
896 * deal with the issue by returning NFS4ERR_DELAY as the
897 * reply to SEQUENCE or CB_SEQUENCE operation, but
898 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
900 * Restart the search after a delay.
902 slot->seq_nr = slot->seq_nr_highest_sent;
904 case -NFS4ERR_BADSESSION:
905 case -NFS4ERR_DEADSESSION:
906 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
907 goto session_recover;
909 /* Just update the slot sequence no. */
913 /* The session may be reset by one of the error handlers. */
914 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
918 set_bit(NFS4_SLOT_TBL_DRAINING, &session->fc_slot_table.slot_tbl_state);
919 nfs4_schedule_session_recovery(session, status);
920 dprintk("%s ERROR: %d Reset session\n", __func__, status);
921 nfs41_sequence_free_slot(res);
926 if (rpc_restart_call_prepare(task)) {
927 nfs41_sequence_free_slot(res);
933 if (!rpc_restart_call(task))
935 rpc_delay(task, NFS4_POLL_RETRY_MAX);
939 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
941 if (!nfs41_sequence_process(task, res))
943 if (res->sr_slot != NULL)
944 nfs41_sequence_free_slot(res);
948 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
950 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
952 if (res->sr_slot == NULL)
954 if (res->sr_slot->table->session != NULL)
955 return nfs41_sequence_process(task, res);
956 return nfs40_sequence_done(task, res);
959 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
961 if (res->sr_slot != NULL) {
962 if (res->sr_slot->table->session != NULL)
963 nfs41_sequence_free_slot(res);
965 nfs40_sequence_free_slot(res);
969 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
971 if (res->sr_slot == NULL)
973 if (!res->sr_slot->table->session)
974 return nfs40_sequence_done(task, res);
975 return nfs41_sequence_done(task, res);
977 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
979 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
981 struct nfs4_call_sync_data *data = calldata;
983 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
985 nfs4_setup_sequence(data->seq_server->nfs_client,
986 data->seq_args, data->seq_res, task);
989 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
991 struct nfs4_call_sync_data *data = calldata;
993 nfs41_sequence_done(task, data->seq_res);
996 static const struct rpc_call_ops nfs41_call_sync_ops = {
997 .rpc_call_prepare = nfs41_call_sync_prepare,
998 .rpc_call_done = nfs41_call_sync_done,
1001 #else /* !CONFIG_NFS_V4_1 */
1003 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1005 return nfs40_sequence_done(task, res);
1008 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1010 if (res->sr_slot != NULL)
1011 nfs40_sequence_free_slot(res);
1014 int nfs4_sequence_done(struct rpc_task *task,
1015 struct nfs4_sequence_res *res)
1017 return nfs40_sequence_done(task, res);
1019 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1021 #endif /* !CONFIG_NFS_V4_1 */
1023 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1025 res->sr_timestamp = jiffies;
1026 res->sr_status_flags = 0;
1031 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1032 struct nfs4_sequence_res *res,
1033 struct nfs4_slot *slot)
1037 slot->privileged = args->sa_privileged ? 1 : 0;
1038 args->sa_slot = slot;
1040 res->sr_slot = slot;
1043 int nfs4_setup_sequence(struct nfs_client *client,
1044 struct nfs4_sequence_args *args,
1045 struct nfs4_sequence_res *res,
1046 struct rpc_task *task)
1048 struct nfs4_session *session = nfs4_get_session(client);
1049 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1050 struct nfs4_slot *slot;
1052 /* slot already allocated? */
1053 if (res->sr_slot != NULL)
1057 tbl = &session->fc_slot_table;
1059 spin_lock(&tbl->slot_tbl_lock);
1060 /* The state manager will wait until the slot table is empty */
1061 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1064 slot = nfs4_alloc_slot(tbl);
1066 if (slot == ERR_PTR(-ENOMEM))
1067 goto out_sleep_timeout;
1070 spin_unlock(&tbl->slot_tbl_lock);
1072 nfs4_sequence_attach_slot(args, res, slot);
1074 trace_nfs4_setup_sequence(session, args);
1076 nfs41_sequence_res_init(res);
1077 rpc_call_start(task);
1080 /* Try again in 1/4 second */
1081 if (args->sa_privileged)
1082 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1083 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1085 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1086 NULL, jiffies + (HZ >> 2));
1087 spin_unlock(&tbl->slot_tbl_lock);
1090 if (args->sa_privileged)
1091 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1092 RPC_PRIORITY_PRIVILEGED);
1094 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1095 spin_unlock(&tbl->slot_tbl_lock);
1098 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1100 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1102 struct nfs4_call_sync_data *data = calldata;
1103 nfs4_setup_sequence(data->seq_server->nfs_client,
1104 data->seq_args, data->seq_res, task);
1107 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1109 struct nfs4_call_sync_data *data = calldata;
1110 nfs4_sequence_done(task, data->seq_res);
1113 static const struct rpc_call_ops nfs40_call_sync_ops = {
1114 .rpc_call_prepare = nfs40_call_sync_prepare,
1115 .rpc_call_done = nfs40_call_sync_done,
1118 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1121 struct rpc_task *task;
1123 task = rpc_run_task(task_setup);
1125 return PTR_ERR(task);
1127 ret = task->tk_status;
1132 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1133 struct nfs_server *server,
1134 struct rpc_message *msg,
1135 struct nfs4_sequence_args *args,
1136 struct nfs4_sequence_res *res)
1138 struct nfs_client *clp = server->nfs_client;
1139 struct nfs4_call_sync_data data = {
1140 .seq_server = server,
1144 struct rpc_task_setup task_setup = {
1147 .callback_ops = clp->cl_mvops->call_sync_ops,
1148 .callback_data = &data
1151 return nfs4_call_sync_custom(&task_setup);
1154 int nfs4_call_sync(struct rpc_clnt *clnt,
1155 struct nfs_server *server,
1156 struct rpc_message *msg,
1157 struct nfs4_sequence_args *args,
1158 struct nfs4_sequence_res *res,
1161 nfs4_init_sequence(args, res, cache_reply, 0);
1162 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1166 nfs4_inc_nlink_locked(struct inode *inode)
1168 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1173 nfs4_dec_nlink_locked(struct inode *inode)
1175 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1180 update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
1181 unsigned long timestamp, unsigned long cache_validity)
1183 struct nfs_inode *nfsi = NFS_I(dir);
1185 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1186 | NFS_INO_INVALID_MTIME
1187 | NFS_INO_INVALID_DATA
1189 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
1190 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1191 nfsi->attrtimeo_timestamp = jiffies;
1193 nfs_force_lookup_revalidate(dir);
1194 if (cinfo->before != inode_peek_iversion_raw(dir))
1195 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1196 NFS_INO_INVALID_ACL;
1198 inode_set_iversion_raw(dir, cinfo->after);
1199 nfsi->read_cache_jiffies = timestamp;
1200 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1201 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1202 nfs_fscache_invalidate(dir);
1206 update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1207 unsigned long timestamp, unsigned long cache_validity)
1209 spin_lock(&dir->i_lock);
1210 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1211 spin_unlock(&dir->i_lock);
1214 struct nfs4_open_createattrs {
1215 struct nfs4_label *label;
1216 struct iattr *sattr;
1217 const __u32 verf[2];
1220 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1221 int err, struct nfs4_exception *exception)
1225 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1227 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1228 exception->retry = 1;
1232 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1234 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1237 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1239 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1241 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1245 nfs4_map_atomic_open_share(struct nfs_server *server,
1246 fmode_t fmode, int openflags)
1250 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1252 res = NFS4_SHARE_ACCESS_READ;
1255 res = NFS4_SHARE_ACCESS_WRITE;
1257 case FMODE_READ|FMODE_WRITE:
1258 res = NFS4_SHARE_ACCESS_BOTH;
1260 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1262 /* Want no delegation if we're using O_DIRECT */
1263 if (openflags & O_DIRECT)
1264 res |= NFS4_SHARE_WANT_NO_DELEG;
1269 static enum open_claim_type4
1270 nfs4_map_atomic_open_claim(struct nfs_server *server,
1271 enum open_claim_type4 claim)
1273 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1278 case NFS4_OPEN_CLAIM_FH:
1279 return NFS4_OPEN_CLAIM_NULL;
1280 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1281 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1282 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1283 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1287 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1289 p->o_res.f_attr = &p->f_attr;
1290 p->o_res.f_label = p->f_label;
1291 p->o_res.seqid = p->o_arg.seqid;
1292 p->c_res.seqid = p->c_arg.seqid;
1293 p->o_res.server = p->o_arg.server;
1294 p->o_res.access_request = p->o_arg.access;
1295 nfs_fattr_init(&p->f_attr);
1296 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1299 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1300 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1301 const struct nfs4_open_createattrs *c,
1302 enum open_claim_type4 claim,
1305 struct dentry *parent = dget_parent(dentry);
1306 struct inode *dir = d_inode(parent);
1307 struct nfs_server *server = NFS_SERVER(dir);
1308 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1309 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1310 struct nfs4_opendata *p;
1312 p = kzalloc(sizeof(*p), gfp_mask);
1316 p->f_label = nfs4_label_alloc(server, gfp_mask);
1317 if (IS_ERR(p->f_label))
1320 p->a_label = nfs4_label_alloc(server, gfp_mask);
1321 if (IS_ERR(p->a_label))
1324 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1325 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1326 if (IS_ERR(p->o_arg.seqid))
1327 goto err_free_label;
1328 nfs_sb_active(dentry->d_sb);
1329 p->dentry = dget(dentry);
1332 atomic_inc(&sp->so_count);
1333 p->o_arg.open_flags = flags;
1334 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1335 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1336 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1338 if (flags & O_CREAT) {
1339 p->o_arg.umask = current_umask();
1340 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1341 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1342 p->o_arg.u.attrs = &p->attrs;
1343 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1345 memcpy(p->o_arg.u.verifier.data, c->verf,
1346 sizeof(p->o_arg.u.verifier.data));
1349 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1350 * will return permission denied for all bits until close */
1351 if (!(flags & O_EXCL)) {
1352 /* ask server to check for all possible rights as results
1354 switch (p->o_arg.claim) {
1357 case NFS4_OPEN_CLAIM_NULL:
1358 case NFS4_OPEN_CLAIM_FH:
1359 p->o_arg.access = NFS4_ACCESS_READ |
1360 NFS4_ACCESS_MODIFY |
1361 NFS4_ACCESS_EXTEND |
1362 NFS4_ACCESS_EXECUTE;
1365 p->o_arg.clientid = server->nfs_client->cl_clientid;
1366 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1367 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1368 p->o_arg.name = &dentry->d_name;
1369 p->o_arg.server = server;
1370 p->o_arg.bitmask = nfs4_bitmask(server, label);
1371 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1372 switch (p->o_arg.claim) {
1373 case NFS4_OPEN_CLAIM_NULL:
1374 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1375 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1376 p->o_arg.fh = NFS_FH(dir);
1378 case NFS4_OPEN_CLAIM_PREVIOUS:
1379 case NFS4_OPEN_CLAIM_FH:
1380 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1381 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1382 p->o_arg.fh = NFS_FH(d_inode(dentry));
1384 p->c_arg.fh = &p->o_res.fh;
1385 p->c_arg.stateid = &p->o_res.stateid;
1386 p->c_arg.seqid = p->o_arg.seqid;
1387 nfs4_init_opendata_res(p);
1388 kref_init(&p->kref);
1392 nfs4_label_free(p->a_label);
1394 nfs4_label_free(p->f_label);
1402 static void nfs4_opendata_free(struct kref *kref)
1404 struct nfs4_opendata *p = container_of(kref,
1405 struct nfs4_opendata, kref);
1406 struct super_block *sb = p->dentry->d_sb;
1408 nfs4_lgopen_release(p->lgp);
1409 nfs_free_seqid(p->o_arg.seqid);
1410 nfs4_sequence_free_slot(&p->o_res.seq_res);
1411 if (p->state != NULL)
1412 nfs4_put_open_state(p->state);
1413 nfs4_put_state_owner(p->owner);
1415 nfs4_label_free(p->a_label);
1416 nfs4_label_free(p->f_label);
1420 nfs_sb_deactive(sb);
1421 nfs_fattr_free_names(&p->f_attr);
1422 kfree(p->f_attr.mdsthreshold);
1426 static void nfs4_opendata_put(struct nfs4_opendata *p)
1429 kref_put(&p->kref, nfs4_opendata_free);
1432 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1435 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1436 case FMODE_READ|FMODE_WRITE:
1437 return state->n_rdwr != 0;
1439 return state->n_wronly != 0;
1441 return state->n_rdonly != 0;
1447 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1448 int open_mode, enum open_claim_type4 claim)
1452 if (open_mode & (O_EXCL|O_TRUNC))
1455 case NFS4_OPEN_CLAIM_NULL:
1456 case NFS4_OPEN_CLAIM_FH:
1461 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1463 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1464 && state->n_rdonly != 0;
1467 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1468 && state->n_wronly != 0;
1470 case FMODE_READ|FMODE_WRITE:
1471 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1472 && state->n_rdwr != 0;
1478 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1479 enum open_claim_type4 claim)
1481 if (delegation == NULL)
1483 if ((delegation->type & fmode) != fmode)
1486 case NFS4_OPEN_CLAIM_NULL:
1487 case NFS4_OPEN_CLAIM_FH:
1489 case NFS4_OPEN_CLAIM_PREVIOUS:
1490 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1496 nfs_mark_delegation_referenced(delegation);
1500 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1509 case FMODE_READ|FMODE_WRITE:
1512 nfs4_state_set_mode_locked(state, state->state | fmode);
1515 #ifdef CONFIG_NFS_V4_1
1516 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1518 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1520 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1522 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1526 #endif /* CONFIG_NFS_V4_1 */
1528 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1530 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1531 wake_up_all(&state->waitq);
1534 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1536 struct nfs_client *clp = state->owner->so_server->nfs_client;
1537 bool need_recover = false;
1539 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1540 need_recover = true;
1541 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1542 need_recover = true;
1543 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1544 need_recover = true;
1546 nfs4_state_mark_reclaim_nograce(clp, state);
1550 * Check for whether or not the caller may update the open stateid
1551 * to the value passed in by stateid.
1553 * Note: This function relies heavily on the server implementing
1554 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1556 * i.e. The stateid seqids have to be initialised to 1, and
1557 * are then incremented on every state transition.
1559 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1560 const nfs4_stateid *stateid)
1562 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1563 /* The common case - we're updating to a new sequence number */
1564 if (nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1565 if (nfs4_stateid_is_next(&state->open_stateid, stateid))
1569 /* The server returned a new stateid */
1571 /* This is the first OPEN in this generation */
1572 if (stateid->seqid == cpu_to_be32(1))
1577 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1579 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1581 if (state->n_wronly)
1582 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1583 if (state->n_rdonly)
1584 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1586 set_bit(NFS_O_RDWR_STATE, &state->flags);
1587 set_bit(NFS_OPEN_STATE, &state->flags);
1590 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1591 nfs4_stateid *stateid, fmode_t fmode)
1593 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1594 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1596 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1599 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1602 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1603 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1604 clear_bit(NFS_OPEN_STATE, &state->flags);
1606 if (stateid == NULL)
1608 /* Handle OPEN+OPEN_DOWNGRADE races */
1609 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1610 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1611 nfs_resync_open_stateid_locked(state);
1614 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1615 nfs4_stateid_copy(&state->stateid, stateid);
1616 nfs4_stateid_copy(&state->open_stateid, stateid);
1617 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1619 nfs_state_log_update_open_stateid(state);
1622 static void nfs_clear_open_stateid(struct nfs4_state *state,
1623 nfs4_stateid *arg_stateid,
1624 nfs4_stateid *stateid, fmode_t fmode)
1626 write_seqlock(&state->seqlock);
1627 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1628 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1629 nfs_clear_open_stateid_locked(state, stateid, fmode);
1630 write_sequnlock(&state->seqlock);
1631 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1632 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1635 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1636 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1637 __must_hold(&state->owner->so_lock)
1638 __must_hold(&state->seqlock)
1646 if (nfs_stateid_is_sequential(state, stateid))
1651 /* Rely on seqids for serialisation with NFSv4.0 */
1652 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1655 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1656 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1658 * Ensure we process the state changes in the same order
1659 * in which the server processed them by delaying the
1660 * update of the stateid until we are in sequence.
1662 write_sequnlock(&state->seqlock);
1663 spin_unlock(&state->owner->so_lock);
1665 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1667 if (!fatal_signal_pending(current)) {
1668 if (schedule_timeout(5*HZ) == 0)
1674 finish_wait(&state->waitq, &wait);
1676 spin_lock(&state->owner->so_lock);
1677 write_seqlock(&state->seqlock);
1680 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1681 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1682 nfs4_stateid_copy(freeme, &state->open_stateid);
1683 nfs_test_and_clear_all_open_stateid(state);
1686 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1687 nfs4_stateid_copy(&state->stateid, stateid);
1688 nfs4_stateid_copy(&state->open_stateid, stateid);
1689 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1690 nfs_state_log_update_open_stateid(state);
1693 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1694 const nfs4_stateid *open_stateid,
1696 nfs4_stateid *freeme)
1699 * Protect the call to nfs4_state_set_mode_locked and
1700 * serialise the stateid update
1702 write_seqlock(&state->seqlock);
1703 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1706 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1709 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1711 case FMODE_READ|FMODE_WRITE:
1712 set_bit(NFS_O_RDWR_STATE, &state->flags);
1714 set_bit(NFS_OPEN_STATE, &state->flags);
1715 write_sequnlock(&state->seqlock);
1718 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1720 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1721 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1722 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1723 clear_bit(NFS_OPEN_STATE, &state->flags);
1726 static void nfs_state_set_delegation(struct nfs4_state *state,
1727 const nfs4_stateid *deleg_stateid,
1731 * Protect the call to nfs4_state_set_mode_locked and
1732 * serialise the stateid update
1734 write_seqlock(&state->seqlock);
1735 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1736 set_bit(NFS_DELEGATED_STATE, &state->flags);
1737 write_sequnlock(&state->seqlock);
1740 static void nfs_state_clear_delegation(struct nfs4_state *state)
1742 write_seqlock(&state->seqlock);
1743 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1744 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1745 write_sequnlock(&state->seqlock);
1748 static int update_open_stateid(struct nfs4_state *state,
1749 const nfs4_stateid *open_stateid,
1750 const nfs4_stateid *delegation,
1753 struct nfs_server *server = NFS_SERVER(state->inode);
1754 struct nfs_client *clp = server->nfs_client;
1755 struct nfs_inode *nfsi = NFS_I(state->inode);
1756 struct nfs_delegation *deleg_cur;
1757 nfs4_stateid freeme = { };
1760 fmode &= (FMODE_READ|FMODE_WRITE);
1763 spin_lock(&state->owner->so_lock);
1764 if (open_stateid != NULL) {
1765 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1769 deleg_cur = rcu_dereference(nfsi->delegation);
1770 if (deleg_cur == NULL)
1773 spin_lock(&deleg_cur->lock);
1774 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1775 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1776 (deleg_cur->type & fmode) != fmode)
1777 goto no_delegation_unlock;
1779 if (delegation == NULL)
1780 delegation = &deleg_cur->stateid;
1781 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1782 goto no_delegation_unlock;
1784 nfs_mark_delegation_referenced(deleg_cur);
1785 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1787 no_delegation_unlock:
1788 spin_unlock(&deleg_cur->lock);
1791 update_open_stateflags(state, fmode);
1792 spin_unlock(&state->owner->so_lock);
1795 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1796 nfs4_schedule_state_manager(clp);
1797 if (freeme.type != 0)
1798 nfs4_test_and_free_stateid(server, &freeme,
1799 state->owner->so_cred);
1804 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1805 const nfs4_stateid *stateid)
1807 struct nfs4_state *state = lsp->ls_state;
1810 spin_lock(&state->state_lock);
1811 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1813 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1815 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1818 spin_unlock(&state->state_lock);
1822 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1824 struct nfs_delegation *delegation;
1826 fmode &= FMODE_READ|FMODE_WRITE;
1828 delegation = rcu_dereference(NFS_I(inode)->delegation);
1829 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1834 nfs4_inode_return_delegation(inode);
1837 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1839 struct nfs4_state *state = opendata->state;
1840 struct nfs_delegation *delegation;
1841 int open_mode = opendata->o_arg.open_flags;
1842 fmode_t fmode = opendata->o_arg.fmode;
1843 enum open_claim_type4 claim = opendata->o_arg.claim;
1844 nfs4_stateid stateid;
1848 spin_lock(&state->owner->so_lock);
1849 if (can_open_cached(state, fmode, open_mode, claim)) {
1850 update_open_stateflags(state, fmode);
1851 spin_unlock(&state->owner->so_lock);
1852 goto out_return_state;
1854 spin_unlock(&state->owner->so_lock);
1856 delegation = nfs4_get_valid_delegation(state->inode);
1857 if (!can_open_delegated(delegation, fmode, claim)) {
1861 /* Save the delegation */
1862 nfs4_stateid_copy(&stateid, &delegation->stateid);
1864 nfs_release_seqid(opendata->o_arg.seqid);
1865 if (!opendata->is_recover) {
1866 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1872 /* Try to update the stateid using the delegation */
1873 if (update_open_stateid(state, NULL, &stateid, fmode))
1874 goto out_return_state;
1877 return ERR_PTR(ret);
1879 refcount_inc(&state->count);
1884 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1886 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1887 struct nfs_delegation *delegation;
1888 int delegation_flags = 0;
1891 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1893 delegation_flags = delegation->flags;
1895 switch (data->o_arg.claim) {
1898 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1899 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1900 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1901 "returning a delegation for "
1902 "OPEN(CLAIM_DELEGATE_CUR)\n",
1906 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1907 nfs_inode_set_delegation(state->inode,
1908 data->owner->so_cred,
1909 data->o_res.delegation_type,
1910 &data->o_res.delegation,
1911 data->o_res.pagemod_limit);
1913 nfs_inode_reclaim_delegation(state->inode,
1914 data->owner->so_cred,
1915 data->o_res.delegation_type,
1916 &data->o_res.delegation,
1917 data->o_res.pagemod_limit);
1919 if (data->o_res.do_recall)
1920 nfs_async_inode_return_delegation(state->inode,
1921 &data->o_res.delegation);
1925 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1926 * and update the nfs4_state.
1928 static struct nfs4_state *
1929 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1931 struct inode *inode = data->state->inode;
1932 struct nfs4_state *state = data->state;
1935 if (!data->rpc_done) {
1936 if (data->rpc_status)
1937 return ERR_PTR(data->rpc_status);
1938 return nfs4_try_open_cached(data);
1941 ret = nfs_refresh_inode(inode, &data->f_attr);
1943 return ERR_PTR(ret);
1945 if (data->o_res.delegation_type != 0)
1946 nfs4_opendata_check_deleg(data, state);
1948 if (!update_open_stateid(state, &data->o_res.stateid,
1949 NULL, data->o_arg.fmode))
1950 return ERR_PTR(-EAGAIN);
1951 refcount_inc(&state->count);
1956 static struct inode *
1957 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1959 struct inode *inode;
1961 switch (data->o_arg.claim) {
1962 case NFS4_OPEN_CLAIM_NULL:
1963 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1964 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1965 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1966 return ERR_PTR(-EAGAIN);
1967 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1968 &data->f_attr, data->f_label);
1971 inode = d_inode(data->dentry);
1973 nfs_refresh_inode(inode, &data->f_attr);
1978 static struct nfs4_state *
1979 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1981 struct nfs4_state *state;
1982 struct inode *inode;
1984 inode = nfs4_opendata_get_inode(data);
1986 return ERR_CAST(inode);
1987 if (data->state != NULL && data->state->inode == inode) {
1988 state = data->state;
1989 refcount_inc(&state->count);
1991 state = nfs4_get_open_state(inode, data->owner);
1994 state = ERR_PTR(-ENOMEM);
1998 static struct nfs4_state *
1999 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2001 struct nfs4_state *state;
2003 if (!data->rpc_done) {
2004 state = nfs4_try_open_cached(data);
2005 trace_nfs4_cached_open(data->state);
2009 state = nfs4_opendata_find_nfs4_state(data);
2013 if (data->o_res.delegation_type != 0)
2014 nfs4_opendata_check_deleg(data, state);
2015 if (!update_open_stateid(state, &data->o_res.stateid,
2016 NULL, data->o_arg.fmode)) {
2017 nfs4_put_open_state(state);
2018 state = ERR_PTR(-EAGAIN);
2021 nfs_release_seqid(data->o_arg.seqid);
2025 static struct nfs4_state *
2026 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2028 struct nfs4_state *ret;
2030 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2031 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2033 ret = _nfs4_opendata_to_nfs4_state(data);
2034 nfs4_sequence_free_slot(&data->o_res.seq_res);
2038 static struct nfs_open_context *
2039 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2041 struct nfs_inode *nfsi = NFS_I(state->inode);
2042 struct nfs_open_context *ctx;
2045 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2046 if (ctx->state != state)
2048 if ((ctx->mode & mode) != mode)
2050 if (!get_nfs_open_context(ctx))
2056 return ERR_PTR(-ENOENT);
2059 static struct nfs_open_context *
2060 nfs4_state_find_open_context(struct nfs4_state *state)
2062 struct nfs_open_context *ctx;
2064 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2067 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2070 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2073 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2074 struct nfs4_state *state, enum open_claim_type4 claim)
2076 struct nfs4_opendata *opendata;
2078 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2079 NULL, claim, GFP_NOFS);
2080 if (opendata == NULL)
2081 return ERR_PTR(-ENOMEM);
2082 opendata->state = state;
2083 refcount_inc(&state->count);
2087 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2090 struct nfs4_state *newstate;
2091 struct nfs_server *server = NFS_SB(opendata->dentry->d_sb);
2092 int openflags = opendata->o_arg.open_flags;
2095 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2097 opendata->o_arg.fmode = fmode;
2098 opendata->o_arg.share_access =
2099 nfs4_map_atomic_open_share(server, fmode, openflags);
2100 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2101 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2102 nfs4_init_opendata_res(opendata);
2103 ret = _nfs4_recover_proc_open(opendata);
2106 newstate = nfs4_opendata_to_nfs4_state(opendata);
2107 if (IS_ERR(newstate))
2108 return PTR_ERR(newstate);
2109 if (newstate != opendata->state)
2111 nfs4_close_state(newstate, fmode);
2115 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2119 /* memory barrier prior to reading state->n_* */
2121 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2124 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2127 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2131 * We may have performed cached opens for all three recoveries.
2132 * Check if we need to update the current stateid.
2134 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2135 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2136 write_seqlock(&state->seqlock);
2137 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2138 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2139 write_sequnlock(&state->seqlock);
2146 * reclaim state on the server after a reboot.
2148 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2150 struct nfs_delegation *delegation;
2151 struct nfs4_opendata *opendata;
2152 fmode_t delegation_type = 0;
2155 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2156 NFS4_OPEN_CLAIM_PREVIOUS);
2157 if (IS_ERR(opendata))
2158 return PTR_ERR(opendata);
2160 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2161 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2162 delegation_type = delegation->type;
2164 opendata->o_arg.u.delegation_type = delegation_type;
2165 status = nfs4_open_recover(opendata, state);
2166 nfs4_opendata_put(opendata);
2170 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2172 struct nfs_server *server = NFS_SERVER(state->inode);
2173 struct nfs4_exception exception = { };
2176 err = _nfs4_do_open_reclaim(ctx, state);
2177 trace_nfs4_open_reclaim(ctx, 0, err);
2178 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2180 if (err != -NFS4ERR_DELAY)
2182 nfs4_handle_exception(server, err, &exception);
2183 } while (exception.retry);
2187 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2189 struct nfs_open_context *ctx;
2192 ctx = nfs4_state_find_open_context(state);
2195 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2196 nfs_state_clear_open_state_flags(state);
2197 ret = nfs4_do_open_reclaim(ctx, state);
2198 put_nfs_open_context(ctx);
2202 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)
2206 printk(KERN_ERR "NFS: %s: unhandled error "
2207 "%d.\n", __func__, err);
2214 case -NFS4ERR_BADSESSION:
2215 case -NFS4ERR_BADSLOT:
2216 case -NFS4ERR_BAD_HIGH_SLOT:
2217 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2218 case -NFS4ERR_DEADSESSION:
2220 case -NFS4ERR_STALE_CLIENTID:
2221 case -NFS4ERR_STALE_STATEID:
2222 /* Don't recall a delegation if it was lost */
2223 nfs4_schedule_lease_recovery(server->nfs_client);
2225 case -NFS4ERR_MOVED:
2226 nfs4_schedule_migration_recovery(server);
2228 case -NFS4ERR_LEASE_MOVED:
2229 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2231 case -NFS4ERR_DELEG_REVOKED:
2232 case -NFS4ERR_ADMIN_REVOKED:
2233 case -NFS4ERR_EXPIRED:
2234 case -NFS4ERR_BAD_STATEID:
2235 case -NFS4ERR_OPENMODE:
2236 nfs_inode_find_state_and_recover(state->inode,
2238 nfs4_schedule_stateid_recovery(server, state);
2240 case -NFS4ERR_DELAY:
2241 case -NFS4ERR_GRACE:
2245 case -NFS4ERR_DENIED:
2247 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2249 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2256 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2257 struct nfs4_state *state, const nfs4_stateid *stateid)
2259 struct nfs_server *server = NFS_SERVER(state->inode);
2260 struct nfs4_opendata *opendata;
2263 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2264 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2265 if (IS_ERR(opendata))
2266 return PTR_ERR(opendata);
2267 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2268 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2269 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2273 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2274 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2278 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2279 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2283 nfs_state_clear_delegation(state);
2285 nfs4_opendata_put(opendata);
2286 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2289 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2291 struct nfs4_opendata *data = calldata;
2293 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2294 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2297 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2299 struct nfs4_opendata *data = calldata;
2301 nfs40_sequence_done(task, &data->c_res.seq_res);
2303 data->rpc_status = task->tk_status;
2304 if (data->rpc_status == 0) {
2305 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2306 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2307 renew_lease(data->o_res.server, data->timestamp);
2308 data->rpc_done = true;
2312 static void nfs4_open_confirm_release(void *calldata)
2314 struct nfs4_opendata *data = calldata;
2315 struct nfs4_state *state = NULL;
2317 /* If this request hasn't been cancelled, do nothing */
2318 if (!data->cancelled)
2320 /* In case of error, no cleanup! */
2321 if (!data->rpc_done)
2323 state = nfs4_opendata_to_nfs4_state(data);
2325 nfs4_close_state(state, data->o_arg.fmode);
2327 nfs4_opendata_put(data);
2330 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2331 .rpc_call_prepare = nfs4_open_confirm_prepare,
2332 .rpc_call_done = nfs4_open_confirm_done,
2333 .rpc_release = nfs4_open_confirm_release,
2337 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2339 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2341 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2342 struct rpc_task *task;
2343 struct rpc_message msg = {
2344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2345 .rpc_argp = &data->c_arg,
2346 .rpc_resp = &data->c_res,
2347 .rpc_cred = data->owner->so_cred,
2349 struct rpc_task_setup task_setup_data = {
2350 .rpc_client = server->client,
2351 .rpc_message = &msg,
2352 .callback_ops = &nfs4_open_confirm_ops,
2353 .callback_data = data,
2354 .workqueue = nfsiod_workqueue,
2355 .flags = RPC_TASK_ASYNC,
2359 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2361 kref_get(&data->kref);
2362 data->rpc_done = false;
2363 data->rpc_status = 0;
2364 data->timestamp = jiffies;
2365 task = rpc_run_task(&task_setup_data);
2367 return PTR_ERR(task);
2368 status = rpc_wait_for_completion_task(task);
2370 data->cancelled = true;
2373 status = data->rpc_status;
2378 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2380 struct nfs4_opendata *data = calldata;
2381 struct nfs4_state_owner *sp = data->owner;
2382 struct nfs_client *clp = sp->so_server->nfs_client;
2383 enum open_claim_type4 claim = data->o_arg.claim;
2385 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2388 * Check if we still need to send an OPEN call, or if we can use
2389 * a delegation instead.
2391 if (data->state != NULL) {
2392 struct nfs_delegation *delegation;
2394 if (can_open_cached(data->state, data->o_arg.fmode,
2395 data->o_arg.open_flags, claim))
2398 delegation = nfs4_get_valid_delegation(data->state->inode);
2399 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2400 goto unlock_no_action;
2403 /* Update client id. */
2404 data->o_arg.clientid = clp->cl_clientid;
2408 case NFS4_OPEN_CLAIM_PREVIOUS:
2409 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2410 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2411 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2413 case NFS4_OPEN_CLAIM_FH:
2414 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2416 data->timestamp = jiffies;
2417 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2418 &data->o_arg.seq_args,
2419 &data->o_res.seq_res,
2421 nfs_release_seqid(data->o_arg.seqid);
2423 /* Set the create mode (note dependency on the session type) */
2424 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2425 if (data->o_arg.open_flags & O_EXCL) {
2426 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2427 if (nfs4_has_persistent_session(clp))
2428 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2429 else if (clp->cl_mvops->minor_version > 0)
2430 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2434 trace_nfs4_cached_open(data->state);
2437 task->tk_action = NULL;
2439 nfs4_sequence_done(task, &data->o_res.seq_res);
2442 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2444 struct nfs4_opendata *data = calldata;
2446 data->rpc_status = task->tk_status;
2448 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2451 if (task->tk_status == 0) {
2452 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2453 switch (data->o_res.f_attr->mode & S_IFMT) {
2457 data->rpc_status = -ELOOP;
2460 data->rpc_status = -EISDIR;
2463 data->rpc_status = -ENOTDIR;
2466 renew_lease(data->o_res.server, data->timestamp);
2467 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2468 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2470 data->rpc_done = true;
2473 static void nfs4_open_release(void *calldata)
2475 struct nfs4_opendata *data = calldata;
2476 struct nfs4_state *state = NULL;
2478 /* If this request hasn't been cancelled, do nothing */
2479 if (!data->cancelled)
2481 /* In case of error, no cleanup! */
2482 if (data->rpc_status != 0 || !data->rpc_done)
2484 /* In case we need an open_confirm, no cleanup! */
2485 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2487 state = nfs4_opendata_to_nfs4_state(data);
2489 nfs4_close_state(state, data->o_arg.fmode);
2491 nfs4_opendata_put(data);
2494 static const struct rpc_call_ops nfs4_open_ops = {
2495 .rpc_call_prepare = nfs4_open_prepare,
2496 .rpc_call_done = nfs4_open_done,
2497 .rpc_release = nfs4_open_release,
2500 static int nfs4_run_open_task(struct nfs4_opendata *data,
2501 struct nfs_open_context *ctx)
2503 struct inode *dir = d_inode(data->dir);
2504 struct nfs_server *server = NFS_SERVER(dir);
2505 struct nfs_openargs *o_arg = &data->o_arg;
2506 struct nfs_openres *o_res = &data->o_res;
2507 struct rpc_task *task;
2508 struct rpc_message msg = {
2509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2512 .rpc_cred = data->owner->so_cred,
2514 struct rpc_task_setup task_setup_data = {
2515 .rpc_client = server->client,
2516 .rpc_message = &msg,
2517 .callback_ops = &nfs4_open_ops,
2518 .callback_data = data,
2519 .workqueue = nfsiod_workqueue,
2520 .flags = RPC_TASK_ASYNC,
2524 kref_get(&data->kref);
2525 data->rpc_done = false;
2526 data->rpc_status = 0;
2527 data->cancelled = false;
2528 data->is_recover = false;
2530 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2531 data->is_recover = true;
2532 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2534 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2535 pnfs_lgopen_prepare(data, ctx);
2537 task = rpc_run_task(&task_setup_data);
2539 return PTR_ERR(task);
2540 status = rpc_wait_for_completion_task(task);
2542 data->cancelled = true;
2545 status = data->rpc_status;
2551 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2553 struct inode *dir = d_inode(data->dir);
2554 struct nfs_openres *o_res = &data->o_res;
2557 status = nfs4_run_open_task(data, NULL);
2558 if (status != 0 || !data->rpc_done)
2561 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2563 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2564 status = _nfs4_proc_open_confirm(data);
2570 * Additional permission checks in order to distinguish between an
2571 * open for read, and an open for execute. This works around the
2572 * fact that NFSv4 OPEN treats read and execute permissions as being
2574 * Note that in the non-execute case, we want to turn off permission
2575 * checking if we just created a new file (POSIX open() semantics).
2577 static int nfs4_opendata_access(const struct cred *cred,
2578 struct nfs4_opendata *opendata,
2579 struct nfs4_state *state, fmode_t fmode,
2582 struct nfs_access_entry cache;
2585 /* access call failed or for some reason the server doesn't
2586 * support any access modes -- defer access call until later */
2587 if (opendata->o_res.access_supported == 0)
2592 * Use openflags to check for exec, because fmode won't
2593 * always have FMODE_EXEC set when file open for exec.
2595 if (openflags & __FMODE_EXEC) {
2596 /* ONLY check for exec rights */
2597 if (S_ISDIR(state->inode->i_mode))
2598 mask = NFS4_ACCESS_LOOKUP;
2600 mask = NFS4_ACCESS_EXECUTE;
2601 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2602 mask = NFS4_ACCESS_READ;
2605 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2606 nfs_access_add_cache(state->inode, &cache);
2608 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2609 if ((mask & ~cache.mask & flags) == 0)
2616 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2618 static int _nfs4_proc_open(struct nfs4_opendata *data,
2619 struct nfs_open_context *ctx)
2621 struct inode *dir = d_inode(data->dir);
2622 struct nfs_server *server = NFS_SERVER(dir);
2623 struct nfs_openargs *o_arg = &data->o_arg;
2624 struct nfs_openres *o_res = &data->o_res;
2627 status = nfs4_run_open_task(data, ctx);
2628 if (!data->rpc_done)
2631 if (status == -NFS4ERR_BADNAME &&
2632 !(o_arg->open_flags & O_CREAT))
2637 nfs_fattr_map_and_free_names(server, &data->f_attr);
2639 if (o_arg->open_flags & O_CREAT) {
2640 if (o_arg->open_flags & O_EXCL)
2641 data->file_created = true;
2642 else if (o_res->cinfo.before != o_res->cinfo.after)
2643 data->file_created = true;
2644 if (data->file_created ||
2645 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2646 update_changeattr(dir, &o_res->cinfo,
2647 o_res->f_attr->time_start, 0);
2649 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2650 server->caps &= ~NFS_CAP_POSIX_LOCK;
2651 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2652 status = _nfs4_proc_open_confirm(data);
2656 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2657 nfs4_sequence_free_slot(&o_res->seq_res);
2658 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2659 o_res->f_label, NULL);
2666 * reclaim state on the server after a network partition.
2667 * Assumes caller holds the appropriate lock
2669 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2671 struct nfs4_opendata *opendata;
2674 opendata = nfs4_open_recoverdata_alloc(ctx, state, NFS4_OPEN_CLAIM_FH);
2675 if (IS_ERR(opendata))
2676 return PTR_ERR(opendata);
2678 * We're not recovering a delegation, so ask for no delegation.
2679 * Otherwise the recovery thread could deadlock with an outstanding
2680 * delegation return.
2682 opendata->o_arg.open_flags = O_DIRECT;
2683 ret = nfs4_open_recover(opendata, state);
2685 d_drop(ctx->dentry);
2686 nfs4_opendata_put(opendata);
2690 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2692 struct nfs_server *server = NFS_SERVER(state->inode);
2693 struct nfs4_exception exception = { };
2697 err = _nfs4_open_expired(ctx, state);
2698 trace_nfs4_open_expired(ctx, 0, err);
2699 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2704 case -NFS4ERR_GRACE:
2705 case -NFS4ERR_DELAY:
2706 nfs4_handle_exception(server, err, &exception);
2709 } while (exception.retry);
2714 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2716 struct nfs_open_context *ctx;
2719 ctx = nfs4_state_find_open_context(state);
2722 ret = nfs4_do_open_expired(ctx, state);
2723 put_nfs_open_context(ctx);
2727 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2728 const nfs4_stateid *stateid)
2730 nfs_remove_bad_delegation(state->inode, stateid);
2731 nfs_state_clear_delegation(state);
2734 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2736 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2737 nfs_finish_clear_delegation_stateid(state, NULL);
2740 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2742 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2743 nfs40_clear_delegation_stateid(state);
2744 nfs_state_clear_open_state_flags(state);
2745 return nfs4_open_expired(sp, state);
2748 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2749 nfs4_stateid *stateid,
2750 const struct cred *cred)
2752 return -NFS4ERR_BAD_STATEID;
2755 #if defined(CONFIG_NFS_V4_1)
2756 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2757 nfs4_stateid *stateid,
2758 const struct cred *cred)
2762 switch (stateid->type) {
2765 case NFS4_INVALID_STATEID_TYPE:
2766 case NFS4_SPECIAL_STATEID_TYPE:
2767 return -NFS4ERR_BAD_STATEID;
2768 case NFS4_REVOKED_STATEID_TYPE:
2772 status = nfs41_test_stateid(server, stateid, cred);
2774 case -NFS4ERR_EXPIRED:
2775 case -NFS4ERR_ADMIN_REVOKED:
2776 case -NFS4ERR_DELEG_REVOKED:
2782 /* Ack the revoked state to the server */
2783 nfs41_free_stateid(server, stateid, cred, true);
2784 return -NFS4ERR_EXPIRED;
2787 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2789 struct nfs_server *server = NFS_SERVER(state->inode);
2790 nfs4_stateid stateid;
2791 struct nfs_delegation *delegation;
2792 const struct cred *cred = NULL;
2793 int status, ret = NFS_OK;
2795 /* Get the delegation credential for use by test/free_stateid */
2797 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2798 if (delegation == NULL) {
2800 nfs_state_clear_delegation(state);
2804 nfs4_stateid_copy(&stateid, &delegation->stateid);
2806 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2807 &delegation->flags)) {
2812 if (delegation->cred)
2813 cred = get_cred(delegation->cred);
2815 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2816 trace_nfs4_test_delegation_stateid(state, NULL, status);
2817 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2818 nfs_finish_clear_delegation_stateid(state, &stateid);
2826 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2830 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2831 nfs4_copy_delegation_stateid(state->inode, state->state,
2833 nfs4_stateid_match_other(&state->stateid, &tmp))
2834 nfs_state_set_delegation(state, &tmp, state->state);
2836 nfs_state_clear_delegation(state);
2840 * nfs41_check_expired_locks - possibly free a lock stateid
2842 * @state: NFSv4 state for an inode
2844 * Returns NFS_OK if recovery for this stateid is now finished.
2845 * Otherwise a negative NFS4ERR value is returned.
2847 static int nfs41_check_expired_locks(struct nfs4_state *state)
2849 int status, ret = NFS_OK;
2850 struct nfs4_lock_state *lsp, *prev = NULL;
2851 struct nfs_server *server = NFS_SERVER(state->inode);
2853 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2856 spin_lock(&state->state_lock);
2857 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2858 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2859 const struct cred *cred = lsp->ls_state->owner->so_cred;
2861 refcount_inc(&lsp->ls_count);
2862 spin_unlock(&state->state_lock);
2864 nfs4_put_lock_state(prev);
2867 status = nfs41_test_and_free_expired_stateid(server,
2870 trace_nfs4_test_lock_stateid(state, lsp, status);
2871 if (status == -NFS4ERR_EXPIRED ||
2872 status == -NFS4ERR_BAD_STATEID) {
2873 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2874 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2875 if (!recover_lost_locks)
2876 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2877 } else if (status != NFS_OK) {
2879 nfs4_put_lock_state(prev);
2882 spin_lock(&state->state_lock);
2885 spin_unlock(&state->state_lock);
2886 nfs4_put_lock_state(prev);
2892 * nfs41_check_open_stateid - possibly free an open stateid
2894 * @state: NFSv4 state for an inode
2896 * Returns NFS_OK if recovery for this stateid is now finished.
2897 * Otherwise a negative NFS4ERR value is returned.
2899 static int nfs41_check_open_stateid(struct nfs4_state *state)
2901 struct nfs_server *server = NFS_SERVER(state->inode);
2902 nfs4_stateid *stateid = &state->open_stateid;
2903 const struct cred *cred = state->owner->so_cred;
2906 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2907 return -NFS4ERR_BAD_STATEID;
2908 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2909 trace_nfs4_test_open_stateid(state, NULL, status);
2910 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2911 nfs_state_clear_open_state_flags(state);
2912 stateid->type = NFS4_INVALID_STATEID_TYPE;
2915 if (nfs_open_stateid_recover_openmode(state))
2916 return -NFS4ERR_OPENMODE;
2920 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2924 status = nfs41_check_delegation_stateid(state);
2925 if (status != NFS_OK)
2927 nfs41_delegation_recover_stateid(state);
2929 status = nfs41_check_expired_locks(state);
2930 if (status != NFS_OK)
2932 status = nfs41_check_open_stateid(state);
2933 if (status != NFS_OK)
2934 status = nfs4_open_expired(sp, state);
2940 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2941 * fields corresponding to attributes that were used to store the verifier.
2942 * Make sure we clobber those fields in the later setattr call
2944 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2945 struct iattr *sattr, struct nfs4_label **label)
2947 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2952 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2953 attrset[i] = opendata->o_res.attrset[i];
2954 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2955 attrset[i] &= ~bitmask[i];
2958 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2959 sattr->ia_valid : 0;
2961 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2962 if (sattr->ia_valid & ATTR_ATIME_SET)
2963 ret |= ATTR_ATIME_SET;
2968 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2969 if (sattr->ia_valid & ATTR_MTIME_SET)
2970 ret |= ATTR_MTIME_SET;
2975 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
2980 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2981 int flags, struct nfs_open_context *ctx)
2983 struct nfs4_state_owner *sp = opendata->owner;
2984 struct nfs_server *server = sp->so_server;
2985 struct dentry *dentry;
2986 struct nfs4_state *state;
2987 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
2988 struct inode *dir = d_inode(opendata->dir);
2989 unsigned long dir_verifier;
2993 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2994 dir_verifier = nfs_save_change_attribute(dir);
2996 ret = _nfs4_proc_open(opendata, ctx);
3000 state = _nfs4_opendata_to_nfs4_state(opendata);
3001 ret = PTR_ERR(state);
3005 if (server->caps & NFS_CAP_POSIX_LOCK)
3006 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3007 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3008 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3010 dentry = opendata->dentry;
3011 if (d_really_is_negative(dentry)) {
3012 struct dentry *alias;
3014 alias = d_exact_alias(dentry, state->inode);
3016 alias = d_splice_alias(igrab(state->inode), dentry);
3017 /* d_splice_alias() can't fail here - it's a non-directory */
3020 ctx->dentry = dentry = alias;
3024 switch(opendata->o_arg.claim) {
3027 case NFS4_OPEN_CLAIM_NULL:
3028 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3029 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3030 if (!opendata->rpc_done)
3032 if (opendata->o_res.delegation_type != 0)
3033 dir_verifier = nfs_save_change_attribute(dir);
3034 nfs_set_verifier(dentry, dir_verifier);
3037 /* Parse layoutget results before we check for access */
3038 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3040 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3045 if (d_inode(dentry) == state->inode) {
3046 nfs_inode_attach_open_context(ctx);
3047 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3048 nfs4_schedule_stateid_recovery(server, state);
3052 if (!opendata->cancelled) {
3053 if (opendata->lgp) {
3054 nfs4_lgopen_release(opendata->lgp);
3055 opendata->lgp = NULL;
3057 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3063 * Returns a referenced nfs4_state
3065 static int _nfs4_do_open(struct inode *dir,
3066 struct nfs_open_context *ctx,
3068 const struct nfs4_open_createattrs *c,
3071 struct nfs4_state_owner *sp;
3072 struct nfs4_state *state = NULL;
3073 struct nfs_server *server = NFS_SERVER(dir);
3074 struct nfs4_opendata *opendata;
3075 struct dentry *dentry = ctx->dentry;
3076 const struct cred *cred = ctx->cred;
3077 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3078 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3079 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3080 struct iattr *sattr = c->sattr;
3081 struct nfs4_label *label = c->label;
3082 struct nfs4_label *olabel = NULL;
3085 /* Protect against reboot recovery conflicts */
3087 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3089 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3092 status = nfs4_client_recover_expired_lease(server->nfs_client);
3094 goto err_put_state_owner;
3095 if (d_really_is_positive(dentry))
3096 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3098 if (d_really_is_positive(dentry))
3099 claim = NFS4_OPEN_CLAIM_FH;
3100 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3101 c, claim, GFP_KERNEL);
3102 if (opendata == NULL)
3103 goto err_put_state_owner;
3106 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3107 if (IS_ERR(olabel)) {
3108 status = PTR_ERR(olabel);
3109 goto err_opendata_put;
3113 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3114 if (!opendata->f_attr.mdsthreshold) {
3115 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3116 if (!opendata->f_attr.mdsthreshold)
3117 goto err_free_label;
3119 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3121 if (d_really_is_positive(dentry))
3122 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3124 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3126 goto err_free_label;
3129 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3130 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3131 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3133 * send create attributes which was not set by open
3134 * with an extra setattr.
3136 if (attrs || label) {
3137 unsigned ia_old = sattr->ia_valid;
3139 sattr->ia_valid = attrs;
3140 nfs_fattr_init(opendata->o_res.f_attr);
3141 status = nfs4_do_setattr(state->inode, cred,
3142 opendata->o_res.f_attr, sattr,
3143 ctx, label, olabel);
3145 nfs_setattr_update_inode(state->inode, sattr,
3146 opendata->o_res.f_attr);
3147 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3149 sattr->ia_valid = ia_old;
3152 if (opened && opendata->file_created)
3155 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3156 *ctx_th = opendata->f_attr.mdsthreshold;
3157 opendata->f_attr.mdsthreshold = NULL;
3160 nfs4_label_free(olabel);
3162 nfs4_opendata_put(opendata);
3163 nfs4_put_state_owner(sp);
3166 nfs4_label_free(olabel);
3168 nfs4_opendata_put(opendata);
3169 err_put_state_owner:
3170 nfs4_put_state_owner(sp);
3176 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3177 struct nfs_open_context *ctx,
3179 struct iattr *sattr,
3180 struct nfs4_label *label,
3183 struct nfs_server *server = NFS_SERVER(dir);
3184 struct nfs4_exception exception = {
3185 .interruptible = true,
3187 struct nfs4_state *res;
3188 struct nfs4_open_createattrs c = {
3192 [0] = (__u32)jiffies,
3193 [1] = (__u32)current->pid,
3199 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3201 trace_nfs4_open_file(ctx, flags, status);
3204 /* NOTE: BAD_SEQID means the server and client disagree about the
3205 * book-keeping w.r.t. state-changing operations
3206 * (OPEN/CLOSE/LOCK/LOCKU...)
3207 * It is actually a sign of a bug on the client or on the server.
3209 * If we receive a BAD_SEQID error in the particular case of
3210 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3211 * have unhashed the old state_owner for us, and that we can
3212 * therefore safely retry using a new one. We should still warn
3213 * the user though...
3215 if (status == -NFS4ERR_BAD_SEQID) {
3216 pr_warn_ratelimited("NFS: v4 server %s "
3217 " returned a bad sequence-id error!\n",
3218 NFS_SERVER(dir)->nfs_client->cl_hostname);
3219 exception.retry = 1;
3223 * BAD_STATEID on OPEN means that the server cancelled our
3224 * state before it received the OPEN_CONFIRM.
3225 * Recover by retrying the request as per the discussion
3226 * on Page 181 of RFC3530.
3228 if (status == -NFS4ERR_BAD_STATEID) {
3229 exception.retry = 1;
3232 if (status == -NFS4ERR_EXPIRED) {
3233 nfs4_schedule_lease_recovery(server->nfs_client);
3234 exception.retry = 1;
3237 if (status == -EAGAIN) {
3238 /* We must have found a delegation */
3239 exception.retry = 1;
3242 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3244 res = ERR_PTR(nfs4_handle_exception(server,
3245 status, &exception));
3246 } while (exception.retry);
3250 static int _nfs4_do_setattr(struct inode *inode,
3251 struct nfs_setattrargs *arg,
3252 struct nfs_setattrres *res,
3253 const struct cred *cred,
3254 struct nfs_open_context *ctx)
3256 struct nfs_server *server = NFS_SERVER(inode);
3257 struct rpc_message msg = {
3258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3263 const struct cred *delegation_cred = NULL;
3264 unsigned long timestamp = jiffies;
3268 nfs_fattr_init(res->fattr);
3270 /* Servers should only apply open mode checks for file size changes */
3271 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3273 nfs4_inode_make_writeable(inode);
3277 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3278 /* Use that stateid */
3279 } else if (ctx != NULL && ctx->state) {
3280 struct nfs_lock_context *l_ctx;
3281 if (!nfs4_valid_open_stateid(ctx->state))
3283 l_ctx = nfs_get_lock_context(ctx);
3285 return PTR_ERR(l_ctx);
3286 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3287 &arg->stateid, &delegation_cred);
3288 nfs_put_lock_context(l_ctx);
3293 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3295 if (delegation_cred)
3296 msg.rpc_cred = delegation_cred;
3298 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3300 put_cred(delegation_cred);
3301 if (status == 0 && ctx != NULL)
3302 renew_lease(server, timestamp);
3303 trace_nfs4_setattr(inode, &arg->stateid, status);
3307 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3308 struct nfs_fattr *fattr, struct iattr *sattr,
3309 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3310 struct nfs4_label *olabel)
3312 struct nfs_server *server = NFS_SERVER(inode);
3313 __u32 bitmask[NFS4_BITMASK_SZ];
3314 struct nfs4_state *state = ctx ? ctx->state : NULL;
3315 struct nfs_setattrargs arg = {
3316 .fh = NFS_FH(inode),
3322 struct nfs_setattrres res = {
3327 struct nfs4_exception exception = {
3330 .stateid = &arg.stateid,
3335 nfs4_bitmap_copy_adjust_setattr(bitmask,
3336 nfs4_bitmask(server, olabel),
3339 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3341 case -NFS4ERR_OPENMODE:
3342 if (!(sattr->ia_valid & ATTR_SIZE)) {
3343 pr_warn_once("NFSv4: server %s is incorrectly "
3344 "applying open mode checks to "
3345 "a SETATTR that is not "
3346 "changing file size.\n",
3347 server->nfs_client->cl_hostname);
3349 if (state && !(state->state & FMODE_WRITE)) {
3351 if (sattr->ia_valid & ATTR_OPEN)
3356 err = nfs4_handle_exception(server, err, &exception);
3357 } while (exception.retry);
3363 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3365 if (inode == NULL || !nfs_have_layout(inode))
3368 return pnfs_wait_on_layoutreturn(inode, task);
3372 * Update the seqid of an open stateid
3374 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3375 struct nfs4_state *state)
3382 if (!nfs4_valid_open_stateid(state))
3384 seq = read_seqbegin(&state->seqlock);
3385 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3386 nfs4_stateid_copy(dst, &state->open_stateid);
3387 if (read_seqretry(&state->seqlock, seq))
3391 seqid_open = state->open_stateid.seqid;
3392 if (read_seqretry(&state->seqlock, seq))
3395 dst_seqid = be32_to_cpu(dst->seqid);
3396 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3397 dst->seqid = seqid_open;
3403 * Update the seqid of an open stateid after receiving
3404 * NFS4ERR_OLD_STATEID
3406 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3407 struct nfs4_state *state)
3412 int seq, status = -EAGAIN;
3417 if (!nfs4_valid_open_stateid(state))
3419 seq = read_seqbegin(&state->seqlock);
3420 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3421 if (read_seqretry(&state->seqlock, seq))
3426 write_seqlock(&state->seqlock);
3427 seqid_open = state->open_stateid.seqid;
3429 dst_seqid = be32_to_cpu(dst->seqid);
3431 /* Did another OPEN bump the state's seqid? try again: */
3432 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3433 dst->seqid = seqid_open;
3434 write_sequnlock(&state->seqlock);
3439 /* server says we're behind but we haven't seen the update yet */
3440 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3441 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3442 write_sequnlock(&state->seqlock);
3443 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3445 if (fatal_signal_pending(current))
3448 if (schedule_timeout(5*HZ) != 0)
3451 finish_wait(&state->waitq, &wait);
3455 if (status == -EINTR)
3458 /* we slept the whole 5 seconds, we must have lost a seqid */
3459 dst->seqid = cpu_to_be32(dst_seqid + 1);
3467 struct nfs4_closedata {
3468 struct inode *inode;
3469 struct nfs4_state *state;
3470 struct nfs_closeargs arg;
3471 struct nfs_closeres res;
3473 struct nfs4_layoutreturn_args arg;
3474 struct nfs4_layoutreturn_res res;
3475 struct nfs4_xdr_opaque_data ld_private;
3479 struct nfs_fattr fattr;
3480 unsigned long timestamp;
3483 static void nfs4_free_closedata(void *data)
3485 struct nfs4_closedata *calldata = data;
3486 struct nfs4_state_owner *sp = calldata->state->owner;
3487 struct super_block *sb = calldata->state->inode->i_sb;
3489 if (calldata->lr.roc)
3490 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3491 calldata->res.lr_ret);
3492 nfs4_put_open_state(calldata->state);
3493 nfs_free_seqid(calldata->arg.seqid);
3494 nfs4_put_state_owner(sp);
3495 nfs_sb_deactive(sb);
3499 static void nfs4_close_done(struct rpc_task *task, void *data)
3501 struct nfs4_closedata *calldata = data;
3502 struct nfs4_state *state = calldata->state;
3503 struct nfs_server *server = NFS_SERVER(calldata->inode);
3504 nfs4_stateid *res_stateid = NULL;
3505 struct nfs4_exception exception = {
3507 .inode = calldata->inode,
3508 .stateid = &calldata->arg.stateid,
3511 dprintk("%s: begin!\n", __func__);
3512 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3514 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3516 /* Handle Layoutreturn errors */
3517 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3518 &calldata->res.lr_ret) == -EAGAIN)
3521 /* hmm. we are done with the inode, and in the process of freeing
3522 * the state_owner. we keep this around to process errors
3524 switch (task->tk_status) {
3526 res_stateid = &calldata->res.stateid;
3527 renew_lease(server, calldata->timestamp);
3529 case -NFS4ERR_ACCESS:
3530 if (calldata->arg.bitmask != NULL) {
3531 calldata->arg.bitmask = NULL;
3532 calldata->res.fattr = NULL;
3537 case -NFS4ERR_OLD_STATEID:
3538 /* Did we race with OPEN? */
3539 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3543 case -NFS4ERR_ADMIN_REVOKED:
3544 case -NFS4ERR_STALE_STATEID:
3545 case -NFS4ERR_EXPIRED:
3546 nfs4_free_revoked_stateid(server,
3547 &calldata->arg.stateid,
3548 task->tk_msg.rpc_cred);
3550 case -NFS4ERR_BAD_STATEID:
3551 if (calldata->arg.fmode == 0)
3555 task->tk_status = nfs4_async_handle_exception(task,
3556 server, task->tk_status, &exception);
3557 if (exception.retry)
3560 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3561 res_stateid, calldata->arg.fmode);
3563 task->tk_status = 0;
3564 nfs_release_seqid(calldata->arg.seqid);
3565 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3566 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3569 task->tk_status = 0;
3570 rpc_restart_call_prepare(task);
3574 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3576 struct nfs4_closedata *calldata = data;
3577 struct nfs4_state *state = calldata->state;
3578 struct inode *inode = calldata->inode;
3579 struct pnfs_layout_hdr *lo;
3580 bool is_rdonly, is_wronly, is_rdwr;
3583 dprintk("%s: begin!\n", __func__);
3584 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3587 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3588 spin_lock(&state->owner->so_lock);
3589 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3590 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3591 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3592 /* Calculate the change in open mode */
3593 calldata->arg.fmode = 0;
3594 if (state->n_rdwr == 0) {
3595 if (state->n_rdonly == 0)
3596 call_close |= is_rdonly;
3598 calldata->arg.fmode |= FMODE_READ;
3599 if (state->n_wronly == 0)
3600 call_close |= is_wronly;
3602 calldata->arg.fmode |= FMODE_WRITE;
3603 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3604 call_close |= is_rdwr;
3606 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3608 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3609 if (!nfs4_valid_open_stateid(state))
3611 spin_unlock(&state->owner->so_lock);
3614 /* Note: exit _without_ calling nfs4_close_done */
3618 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3619 nfs_release_seqid(calldata->arg.seqid);
3623 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3624 if (lo && !pnfs_layout_is_valid(lo)) {
3625 calldata->arg.lr_args = NULL;
3626 calldata->res.lr_res = NULL;
3629 if (calldata->arg.fmode == 0)
3630 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3632 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3633 /* Close-to-open cache consistency revalidation */
3634 if (!nfs4_have_delegation(inode, FMODE_READ))
3635 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3637 calldata->arg.bitmask = NULL;
3640 calldata->arg.share_access =
3641 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3642 calldata->arg.fmode, 0);
3644 if (calldata->res.fattr == NULL)
3645 calldata->arg.bitmask = NULL;
3646 else if (calldata->arg.bitmask == NULL)
3647 calldata->res.fattr = NULL;
3648 calldata->timestamp = jiffies;
3649 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3650 &calldata->arg.seq_args,
3651 &calldata->res.seq_res,
3653 nfs_release_seqid(calldata->arg.seqid);
3654 dprintk("%s: done!\n", __func__);
3657 task->tk_action = NULL;
3659 nfs4_sequence_done(task, &calldata->res.seq_res);
3662 static const struct rpc_call_ops nfs4_close_ops = {
3663 .rpc_call_prepare = nfs4_close_prepare,
3664 .rpc_call_done = nfs4_close_done,
3665 .rpc_release = nfs4_free_closedata,
3669 * It is possible for data to be read/written from a mem-mapped file
3670 * after the sys_close call (which hits the vfs layer as a flush).
3671 * This means that we can't safely call nfsv4 close on a file until
3672 * the inode is cleared. This in turn means that we are not good
3673 * NFSv4 citizens - we do not indicate to the server to update the file's
3674 * share state even when we are done with one of the three share
3675 * stateid's in the inode.
3677 * NOTE: Caller must be holding the sp->so_owner semaphore!
3679 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3681 struct nfs_server *server = NFS_SERVER(state->inode);
3682 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3683 struct nfs4_closedata *calldata;
3684 struct nfs4_state_owner *sp = state->owner;
3685 struct rpc_task *task;
3686 struct rpc_message msg = {
3687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3688 .rpc_cred = state->owner->so_cred,
3690 struct rpc_task_setup task_setup_data = {
3691 .rpc_client = server->client,
3692 .rpc_message = &msg,
3693 .callback_ops = &nfs4_close_ops,
3694 .workqueue = nfsiod_workqueue,
3695 .flags = RPC_TASK_ASYNC,
3697 int status = -ENOMEM;
3699 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3700 &task_setup_data.rpc_client, &msg);
3702 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3703 if (calldata == NULL)
3705 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3706 calldata->inode = state->inode;
3707 calldata->state = state;
3708 calldata->arg.fh = NFS_FH(state->inode);
3709 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3710 goto out_free_calldata;
3711 /* Serialization for the sequence id */
3712 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3713 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3714 if (IS_ERR(calldata->arg.seqid))
3715 goto out_free_calldata;
3716 nfs_fattr_init(&calldata->fattr);
3717 calldata->arg.fmode = 0;
3718 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3719 calldata->res.fattr = &calldata->fattr;
3720 calldata->res.seqid = calldata->arg.seqid;
3721 calldata->res.server = server;
3722 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3723 calldata->lr.roc = pnfs_roc(state->inode,
3724 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3725 if (calldata->lr.roc) {
3726 calldata->arg.lr_args = &calldata->lr.arg;
3727 calldata->res.lr_res = &calldata->lr.res;
3729 nfs_sb_active(calldata->inode->i_sb);
3731 msg.rpc_argp = &calldata->arg;
3732 msg.rpc_resp = &calldata->res;
3733 task_setup_data.callback_data = calldata;
3734 task = rpc_run_task(&task_setup_data);
3736 return PTR_ERR(task);
3739 status = rpc_wait_for_completion_task(task);
3745 nfs4_put_open_state(state);
3746 nfs4_put_state_owner(sp);
3750 static struct inode *
3751 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3752 int open_flags, struct iattr *attr, int *opened)
3754 struct nfs4_state *state;
3755 struct nfs4_label l, *label;
3757 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3759 /* Protect against concurrent sillydeletes */
3760 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3762 nfs4_label_release_security(label);
3765 return ERR_CAST(state);
3766 return state->inode;
3769 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3771 if (ctx->state == NULL)
3774 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3776 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3779 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3780 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3781 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3783 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3785 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3786 struct nfs4_server_caps_arg args = {
3790 struct nfs4_server_caps_res res = {};
3791 struct rpc_message msg = {
3792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3799 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3800 FATTR4_WORD0_FH_EXPIRE_TYPE |
3801 FATTR4_WORD0_LINK_SUPPORT |
3802 FATTR4_WORD0_SYMLINK_SUPPORT |
3803 FATTR4_WORD0_ACLSUPPORT;
3805 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3807 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3809 /* Sanity check the server answers */
3810 switch (minorversion) {
3812 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3813 res.attr_bitmask[2] = 0;
3816 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3819 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3821 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3822 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3823 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3824 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3825 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3826 NFS_CAP_CTIME|NFS_CAP_MTIME|
3827 NFS_CAP_SECURITY_LABEL);
3828 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3829 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3830 server->caps |= NFS_CAP_ACLS;
3831 if (res.has_links != 0)
3832 server->caps |= NFS_CAP_HARDLINKS;
3833 if (res.has_symlinks != 0)
3834 server->caps |= NFS_CAP_SYMLINKS;
3835 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3836 server->caps |= NFS_CAP_FILEID;
3837 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3838 server->caps |= NFS_CAP_MODE;
3839 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3840 server->caps |= NFS_CAP_NLINK;
3841 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3842 server->caps |= NFS_CAP_OWNER;
3843 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3844 server->caps |= NFS_CAP_OWNER_GROUP;
3845 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3846 server->caps |= NFS_CAP_ATIME;
3847 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3848 server->caps |= NFS_CAP_CTIME;
3849 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3850 server->caps |= NFS_CAP_MTIME;
3851 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3852 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3853 server->caps |= NFS_CAP_SECURITY_LABEL;
3855 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3856 sizeof(server->attr_bitmask));
3857 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3859 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3860 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3861 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3862 server->cache_consistency_bitmask[2] = 0;
3864 /* Avoid a regression due to buggy server */
3865 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3866 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3867 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3868 sizeof(server->exclcreat_bitmask));
3870 server->acl_bitmask = res.acl_bitmask;
3871 server->fh_expire_type = res.fh_expire_type;
3877 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3879 struct nfs4_exception exception = {
3880 .interruptible = true,
3884 err = nfs4_handle_exception(server,
3885 _nfs4_server_capabilities(server, fhandle),
3887 } while (exception.retry);
3891 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3892 struct nfs_fsinfo *info)
3895 struct nfs4_lookup_root_arg args = {
3898 struct nfs4_lookup_res res = {
3900 .fattr = info->fattr,
3903 struct rpc_message msg = {
3904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3909 bitmask[0] = nfs4_fattr_bitmap[0];
3910 bitmask[1] = nfs4_fattr_bitmap[1];
3912 * Process the label in the upcoming getfattr
3914 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3916 nfs_fattr_init(info->fattr);
3917 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3920 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3921 struct nfs_fsinfo *info)
3923 struct nfs4_exception exception = {
3924 .interruptible = true,
3928 err = _nfs4_lookup_root(server, fhandle, info);
3929 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3932 case -NFS4ERR_WRONGSEC:
3935 err = nfs4_handle_exception(server, err, &exception);
3937 } while (exception.retry);
3942 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3943 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3945 struct rpc_auth_create_args auth_args = {
3946 .pseudoflavor = flavor,
3948 struct rpc_auth *auth;
3950 auth = rpcauth_create(&auth_args, server->client);
3953 return nfs4_lookup_root(server, fhandle, info);
3957 * Retry pseudoroot lookup with various security flavors. We do this when:
3959 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3960 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3962 * Returns zero on success, or a negative NFS4ERR value, or a
3963 * negative errno value.
3965 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3966 struct nfs_fsinfo *info)
3968 /* Per 3530bis 15.33.5 */
3969 static const rpc_authflavor_t flav_array[] = {
3973 RPC_AUTH_UNIX, /* courtesy */
3976 int status = -EPERM;
3979 if (server->auth_info.flavor_len > 0) {
3980 /* try each flavor specified by user */
3981 for (i = 0; i < server->auth_info.flavor_len; i++) {
3982 status = nfs4_lookup_root_sec(server, fhandle, info,
3983 server->auth_info.flavors[i]);
3984 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3989 /* no flavors specified by user, try default list */
3990 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3991 status = nfs4_lookup_root_sec(server, fhandle, info,
3993 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4000 * -EACCES could mean that the user doesn't have correct permissions
4001 * to access the mount. It could also mean that we tried to mount
4002 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4003 * existing mount programs don't handle -EACCES very well so it should
4004 * be mapped to -EPERM instead.
4006 if (status == -EACCES)
4012 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4013 * @server: initialized nfs_server handle
4014 * @fhandle: we fill in the pseudo-fs root file handle
4015 * @info: we fill in an FSINFO struct
4016 * @auth_probe: probe the auth flavours
4018 * Returns zero on success, or a negative errno.
4020 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4021 struct nfs_fsinfo *info,
4027 status = nfs4_lookup_root(server, fhandle, info);
4029 if (auth_probe || status == NFS4ERR_WRONGSEC)
4030 status = server->nfs_client->cl_mvops->find_root_sec(server,
4034 status = nfs4_server_capabilities(server, fhandle);
4036 status = nfs4_do_fsinfo(server, fhandle, info);
4038 return nfs4_map_errors(status);
4041 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4042 struct nfs_fsinfo *info)
4045 struct nfs_fattr *fattr = info->fattr;
4046 struct nfs4_label *label = NULL;
4048 error = nfs4_server_capabilities(server, mntfh);
4050 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4054 label = nfs4_label_alloc(server, GFP_KERNEL);
4056 return PTR_ERR(label);
4058 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
4060 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4061 goto err_free_label;
4064 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4065 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4066 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4069 nfs4_label_free(label);
4075 * Get locations and (maybe) other attributes of a referral.
4076 * Note that we'll actually follow the referral later when
4077 * we detect fsid mismatch in inode revalidation
4079 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4080 const struct qstr *name, struct nfs_fattr *fattr,
4081 struct nfs_fh *fhandle)
4083 int status = -ENOMEM;
4084 struct page *page = NULL;
4085 struct nfs4_fs_locations *locations = NULL;
4087 page = alloc_page(GFP_KERNEL);
4090 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4091 if (locations == NULL)
4094 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4099 * If the fsid didn't change, this is a migration event, not a
4100 * referral. Cause us to drop into the exception handler, which
4101 * will kick off migration recovery.
4103 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4104 dprintk("%s: server did not return a different fsid for"
4105 " a referral at %s\n", __func__, name->name);
4106 status = -NFS4ERR_MOVED;
4109 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4110 nfs_fixup_referral_attributes(&locations->fattr);
4112 /* replace the lookup nfs_fattr with the locations nfs_fattr */
4113 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4114 memset(fhandle, 0, sizeof(struct nfs_fh));
4122 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4123 struct nfs_fattr *fattr, struct nfs4_label *label,
4124 struct inode *inode)
4126 __u32 bitmask[NFS4_BITMASK_SZ];
4127 struct nfs4_getattr_arg args = {
4131 struct nfs4_getattr_res res = {
4136 struct rpc_message msg = {
4137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4142 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
4144 nfs_fattr_init(fattr);
4145 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4148 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4149 struct nfs_fattr *fattr, struct nfs4_label *label,
4150 struct inode *inode)
4152 struct nfs4_exception exception = {
4153 .interruptible = true,
4157 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4158 trace_nfs4_getattr(server, fhandle, fattr, err);
4159 err = nfs4_handle_exception(server, err,
4161 } while (exception.retry);
4166 * The file is not closed if it is opened due to the a request to change
4167 * the size of the file. The open call will not be needed once the
4168 * VFS layer lookup-intents are implemented.
4170 * Close is called when the inode is destroyed.
4171 * If we haven't opened the file for O_WRONLY, we
4172 * need to in the size_change case to obtain a stateid.
4175 * Because OPEN is always done by name in nfsv4, it is
4176 * possible that we opened a different file by the same
4177 * name. We can recognize this race condition, but we
4178 * can't do anything about it besides returning an error.
4180 * This will be fixed with VFS changes (lookup-intent).
4183 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4184 struct iattr *sattr)
4186 struct inode *inode = d_inode(dentry);
4187 const struct cred *cred = NULL;
4188 struct nfs_open_context *ctx = NULL;
4189 struct nfs4_label *label = NULL;
4192 if (pnfs_ld_layoutret_on_setattr(inode) &&
4193 sattr->ia_valid & ATTR_SIZE &&
4194 sattr->ia_size < i_size_read(inode))
4195 pnfs_commit_and_return_layout(inode);
4197 nfs_fattr_init(fattr);
4199 /* Deal with open(O_TRUNC) */
4200 if (sattr->ia_valid & ATTR_OPEN)
4201 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4203 /* Optimization: if the end result is no change, don't RPC */
4204 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4207 /* Search for an existing open(O_WRITE) file */
4208 if (sattr->ia_valid & ATTR_FILE) {
4210 ctx = nfs_file_open_context(sattr->ia_file);
4215 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4217 return PTR_ERR(label);
4219 /* Return any delegations if we're going to change ACLs */
4220 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4221 nfs4_inode_make_writeable(inode);
4223 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4225 nfs_setattr_update_inode(inode, sattr, fattr);
4226 nfs_setsecurity(inode, fattr, label);
4228 nfs4_label_free(label);
4232 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4233 const struct qstr *name, struct nfs_fh *fhandle,
4234 struct nfs_fattr *fattr, struct nfs4_label *label)
4236 struct nfs_server *server = NFS_SERVER(dir);
4238 struct nfs4_lookup_arg args = {
4239 .bitmask = server->attr_bitmask,
4240 .dir_fh = NFS_FH(dir),
4243 struct nfs4_lookup_res res = {
4249 struct rpc_message msg = {
4250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4255 args.bitmask = nfs4_bitmask(server, label);
4257 nfs_fattr_init(fattr);
4259 dprintk("NFS call lookup %s\n", name->name);
4260 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
4261 dprintk("NFS reply lookup: %d\n", status);
4265 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4267 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4268 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4269 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4273 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4274 const struct qstr *name, struct nfs_fh *fhandle,
4275 struct nfs_fattr *fattr, struct nfs4_label *label)
4277 struct nfs4_exception exception = {
4278 .interruptible = true,
4280 struct rpc_clnt *client = *clnt;
4283 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
4284 trace_nfs4_lookup(dir, name, err);
4286 case -NFS4ERR_BADNAME:
4289 case -NFS4ERR_MOVED:
4290 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4291 if (err == -NFS4ERR_MOVED)
4292 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4294 case -NFS4ERR_WRONGSEC:
4296 if (client != *clnt)
4298 client = nfs4_negotiate_security(client, dir, name);
4300 return PTR_ERR(client);
4302 exception.retry = 1;
4305 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4307 } while (exception.retry);
4312 else if (client != *clnt)
4313 rpc_shutdown_client(client);
4318 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
4319 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4320 struct nfs4_label *label)
4323 struct rpc_clnt *client = NFS_CLIENT(dir);
4325 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
4326 if (client != NFS_CLIENT(dir)) {
4327 rpc_shutdown_client(client);
4328 nfs_fixup_secinfo_attributes(fattr);
4334 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
4335 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4337 struct rpc_clnt *client = NFS_CLIENT(dir);
4340 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
4342 return ERR_PTR(status);
4343 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4346 static int _nfs4_proc_lookupp(struct inode *inode,
4347 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4348 struct nfs4_label *label)
4350 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4351 struct nfs_server *server = NFS_SERVER(inode);
4353 struct nfs4_lookupp_arg args = {
4354 .bitmask = server->attr_bitmask,
4355 .fh = NFS_FH(inode),
4357 struct nfs4_lookupp_res res = {
4363 struct rpc_message msg = {
4364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4369 args.bitmask = nfs4_bitmask(server, label);
4371 nfs_fattr_init(fattr);
4373 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4374 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4376 dprintk("NFS reply lookupp: %d\n", status);
4380 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4381 struct nfs_fattr *fattr, struct nfs4_label *label)
4383 struct nfs4_exception exception = {
4384 .interruptible = true,
4388 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4389 trace_nfs4_lookupp(inode, err);
4390 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4392 } while (exception.retry);
4396 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4398 struct nfs_server *server = NFS_SERVER(inode);
4399 struct nfs4_accessargs args = {
4400 .fh = NFS_FH(inode),
4401 .access = entry->mask,
4403 struct nfs4_accessres res = {
4406 struct rpc_message msg = {
4407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4410 .rpc_cred = entry->cred,
4414 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4415 res.fattr = nfs_alloc_fattr();
4416 if (res.fattr == NULL)
4418 args.bitmask = server->cache_consistency_bitmask;
4420 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4422 nfs_access_set_mask(entry, res.access);
4424 nfs_refresh_inode(inode, res.fattr);
4426 nfs_free_fattr(res.fattr);
4430 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4432 struct nfs4_exception exception = {
4433 .interruptible = true,
4437 err = _nfs4_proc_access(inode, entry);
4438 trace_nfs4_access(inode, err);
4439 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4441 } while (exception.retry);
4446 * TODO: For the time being, we don't try to get any attributes
4447 * along with any of the zero-copy operations READ, READDIR,
4450 * In the case of the first three, we want to put the GETATTR
4451 * after the read-type operation -- this is because it is hard
4452 * to predict the length of a GETATTR response in v4, and thus
4453 * align the READ data correctly. This means that the GETATTR
4454 * may end up partially falling into the page cache, and we should
4455 * shift it into the 'tail' of the xdr_buf before processing.
4456 * To do this efficiently, we need to know the total length
4457 * of data received, which doesn't seem to be available outside
4460 * In the case of WRITE, we also want to put the GETATTR after
4461 * the operation -- in this case because we want to make sure
4462 * we get the post-operation mtime and size.
4464 * Both of these changes to the XDR layer would in fact be quite
4465 * minor, but I decided to leave them for a subsequent patch.
4467 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4468 unsigned int pgbase, unsigned int pglen)
4470 struct nfs4_readlink args = {
4471 .fh = NFS_FH(inode),
4476 struct nfs4_readlink_res res;
4477 struct rpc_message msg = {
4478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4483 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4486 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4487 unsigned int pgbase, unsigned int pglen)
4489 struct nfs4_exception exception = {
4490 .interruptible = true,
4494 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4495 trace_nfs4_readlink(inode, err);
4496 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4498 } while (exception.retry);
4503 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4506 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4509 struct nfs_server *server = NFS_SERVER(dir);
4510 struct nfs4_label l, *ilabel;
4511 struct nfs_open_context *ctx;
4512 struct nfs4_state *state;
4515 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4517 return PTR_ERR(ctx);
4519 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4521 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4522 sattr->ia_mode &= ~current_umask();
4523 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4524 if (IS_ERR(state)) {
4525 status = PTR_ERR(state);
4529 nfs4_label_release_security(ilabel);
4530 put_nfs_open_context(ctx);
4535 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4537 struct nfs_server *server = NFS_SERVER(dir);
4538 struct nfs_removeargs args = {
4542 struct nfs_removeres res = {
4545 struct rpc_message msg = {
4546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4550 unsigned long timestamp = jiffies;
4553 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4555 spin_lock(&dir->i_lock);
4556 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
4557 /* Removing a directory decrements nlink in the parent */
4558 if (ftype == NF4DIR && dir->i_nlink > 2)
4559 nfs4_dec_nlink_locked(dir);
4560 spin_unlock(&dir->i_lock);
4565 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4567 struct nfs4_exception exception = {
4568 .interruptible = true,
4570 struct inode *inode = d_inode(dentry);
4574 if (inode->i_nlink == 1)
4575 nfs4_inode_return_delegation(inode);
4577 nfs4_inode_make_writeable(inode);
4580 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4581 trace_nfs4_remove(dir, &dentry->d_name, err);
4582 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4584 } while (exception.retry);
4588 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4590 struct nfs4_exception exception = {
4591 .interruptible = true,
4596 err = _nfs4_proc_remove(dir, name, NF4DIR);
4597 trace_nfs4_remove(dir, name, err);
4598 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4600 } while (exception.retry);
4604 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4605 struct dentry *dentry,
4606 struct inode *inode)
4608 struct nfs_removeargs *args = msg->rpc_argp;
4609 struct nfs_removeres *res = msg->rpc_resp;
4611 res->server = NFS_SB(dentry->d_sb);
4612 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4613 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4615 nfs_fattr_init(res->dir_attr);
4618 nfs4_inode_return_delegation(inode);
4621 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4623 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4624 &data->args.seq_args,
4629 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4631 struct nfs_unlinkdata *data = task->tk_calldata;
4632 struct nfs_removeres *res = &data->res;
4634 if (!nfs4_sequence_done(task, &res->seq_res))
4636 if (nfs4_async_handle_error(task, res->server, NULL,
4637 &data->timeout) == -EAGAIN)
4639 if (task->tk_status == 0)
4640 update_changeattr(dir, &res->cinfo,
4641 res->dir_attr->time_start, 0);
4645 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4646 struct dentry *old_dentry,
4647 struct dentry *new_dentry)
4649 struct nfs_renameargs *arg = msg->rpc_argp;
4650 struct nfs_renameres *res = msg->rpc_resp;
4651 struct inode *old_inode = d_inode(old_dentry);
4652 struct inode *new_inode = d_inode(new_dentry);
4655 nfs4_inode_make_writeable(old_inode);
4657 nfs4_inode_return_delegation(new_inode);
4658 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4659 res->server = NFS_SB(old_dentry->d_sb);
4660 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4663 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4665 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4666 &data->args.seq_args,
4671 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4672 struct inode *new_dir)
4674 struct nfs_renamedata *data = task->tk_calldata;
4675 struct nfs_renameres *res = &data->res;
4677 if (!nfs4_sequence_done(task, &res->seq_res))
4679 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4682 if (task->tk_status == 0) {
4683 if (new_dir != old_dir) {
4684 /* Note: If we moved a directory, nlink will change */
4685 update_changeattr(old_dir, &res->old_cinfo,
4686 res->old_fattr->time_start,
4687 NFS_INO_INVALID_OTHER);
4688 update_changeattr(new_dir, &res->new_cinfo,
4689 res->new_fattr->time_start,
4690 NFS_INO_INVALID_OTHER);
4692 update_changeattr(old_dir, &res->old_cinfo,
4693 res->old_fattr->time_start,
4699 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4701 struct nfs_server *server = NFS_SERVER(inode);
4702 __u32 bitmask[NFS4_BITMASK_SZ];
4703 struct nfs4_link_arg arg = {
4704 .fh = NFS_FH(inode),
4705 .dir_fh = NFS_FH(dir),
4709 struct nfs4_link_res res = {
4713 struct rpc_message msg = {
4714 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4718 int status = -ENOMEM;
4720 res.fattr = nfs_alloc_fattr();
4721 if (res.fattr == NULL)
4724 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4725 if (IS_ERR(res.label)) {
4726 status = PTR_ERR(res.label);
4730 nfs4_inode_make_writeable(inode);
4731 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4733 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4735 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
4736 status = nfs_post_op_update_inode(inode, res.fattr);
4738 nfs_setsecurity(inode, res.fattr, res.label);
4742 nfs4_label_free(res.label);
4745 nfs_free_fattr(res.fattr);
4749 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4751 struct nfs4_exception exception = {
4752 .interruptible = true,
4756 err = nfs4_handle_exception(NFS_SERVER(inode),
4757 _nfs4_proc_link(inode, dir, name),
4759 } while (exception.retry);
4763 struct nfs4_createdata {
4764 struct rpc_message msg;
4765 struct nfs4_create_arg arg;
4766 struct nfs4_create_res res;
4768 struct nfs_fattr fattr;
4769 struct nfs4_label *label;
4772 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4773 const struct qstr *name, struct iattr *sattr, u32 ftype)
4775 struct nfs4_createdata *data;
4777 data = kzalloc(sizeof(*data), GFP_KERNEL);
4779 struct nfs_server *server = NFS_SERVER(dir);
4781 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4782 if (IS_ERR(data->label))
4785 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4786 data->msg.rpc_argp = &data->arg;
4787 data->msg.rpc_resp = &data->res;
4788 data->arg.dir_fh = NFS_FH(dir);
4789 data->arg.server = server;
4790 data->arg.name = name;
4791 data->arg.attrs = sattr;
4792 data->arg.ftype = ftype;
4793 data->arg.bitmask = nfs4_bitmask(server, data->label);
4794 data->arg.umask = current_umask();
4795 data->res.server = server;
4796 data->res.fh = &data->fh;
4797 data->res.fattr = &data->fattr;
4798 data->res.label = data->label;
4799 nfs_fattr_init(data->res.fattr);
4807 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4809 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4810 &data->arg.seq_args, &data->res.seq_res, 1);
4812 spin_lock(&dir->i_lock);
4813 update_changeattr_locked(dir, &data->res.dir_cinfo,
4814 data->res.fattr->time_start, 0);
4815 /* Creating a directory bumps nlink in the parent */
4816 if (data->arg.ftype == NF4DIR)
4817 nfs4_inc_nlink_locked(dir);
4818 spin_unlock(&dir->i_lock);
4819 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4824 static void nfs4_free_createdata(struct nfs4_createdata *data)
4826 nfs4_label_free(data->label);
4830 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4831 struct page *page, unsigned int len, struct iattr *sattr,
4832 struct nfs4_label *label)
4834 struct nfs4_createdata *data;
4835 int status = -ENAMETOOLONG;
4837 if (len > NFS4_MAXPATHLEN)
4841 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4845 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4846 data->arg.u.symlink.pages = &page;
4847 data->arg.u.symlink.len = len;
4848 data->arg.label = label;
4850 status = nfs4_do_create(dir, dentry, data);
4852 nfs4_free_createdata(data);
4857 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4858 struct page *page, unsigned int len, struct iattr *sattr)
4860 struct nfs4_exception exception = {
4861 .interruptible = true,
4863 struct nfs4_label l, *label;
4866 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4869 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4870 trace_nfs4_symlink(dir, &dentry->d_name, err);
4871 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4873 } while (exception.retry);
4875 nfs4_label_release_security(label);
4879 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4880 struct iattr *sattr, struct nfs4_label *label)
4882 struct nfs4_createdata *data;
4883 int status = -ENOMEM;
4885 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4889 data->arg.label = label;
4890 status = nfs4_do_create(dir, dentry, data);
4892 nfs4_free_createdata(data);
4897 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4898 struct iattr *sattr)
4900 struct nfs_server *server = NFS_SERVER(dir);
4901 struct nfs4_exception exception = {
4902 .interruptible = true,
4904 struct nfs4_label l, *label;
4907 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4909 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4910 sattr->ia_mode &= ~current_umask();
4912 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4913 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4914 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4916 } while (exception.retry);
4917 nfs4_label_release_security(label);
4922 static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4923 u64 cookie, struct page **pages, unsigned int count, bool plus)
4925 struct inode *dir = d_inode(dentry);
4926 struct nfs_server *server = NFS_SERVER(dir);
4927 struct nfs4_readdir_arg args = {
4934 struct nfs4_readdir_res res;
4935 struct rpc_message msg = {
4936 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4943 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4945 (unsigned long long)cookie);
4946 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
4947 args.bitmask = server->attr_bitmask_nl;
4949 args.bitmask = server->attr_bitmask;
4951 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4952 res.pgbase = args.pgbase;
4953 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
4956 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4957 status += args.pgbase;
4960 nfs_invalidate_atime(dir);
4962 dprintk("%s: returns %d\n", __func__, status);
4966 static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4967 u64 cookie, struct page **pages, unsigned int count, bool plus)
4969 struct nfs4_exception exception = {
4970 .interruptible = true,
4974 err = _nfs4_proc_readdir(dentry, cred, cookie,
4975 pages, count, plus);
4976 trace_nfs4_readdir(d_inode(dentry), err);
4977 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4979 } while (exception.retry);
4983 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4984 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4986 struct nfs4_createdata *data;
4987 int mode = sattr->ia_mode;
4988 int status = -ENOMEM;
4990 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4995 data->arg.ftype = NF4FIFO;
4996 else if (S_ISBLK(mode)) {
4997 data->arg.ftype = NF4BLK;
4998 data->arg.u.device.specdata1 = MAJOR(rdev);
4999 data->arg.u.device.specdata2 = MINOR(rdev);
5001 else if (S_ISCHR(mode)) {
5002 data->arg.ftype = NF4CHR;
5003 data->arg.u.device.specdata1 = MAJOR(rdev);
5004 data->arg.u.device.specdata2 = MINOR(rdev);
5005 } else if (!S_ISSOCK(mode)) {
5010 data->arg.label = label;
5011 status = nfs4_do_create(dir, dentry, data);
5013 nfs4_free_createdata(data);
5018 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5019 struct iattr *sattr, dev_t rdev)
5021 struct nfs_server *server = NFS_SERVER(dir);
5022 struct nfs4_exception exception = {
5023 .interruptible = true,
5025 struct nfs4_label l, *label;
5028 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5030 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5031 sattr->ia_mode &= ~current_umask();
5033 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5034 trace_nfs4_mknod(dir, &dentry->d_name, err);
5035 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5037 } while (exception.retry);
5039 nfs4_label_release_security(label);
5044 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5045 struct nfs_fsstat *fsstat)
5047 struct nfs4_statfs_arg args = {
5049 .bitmask = server->attr_bitmask,
5051 struct nfs4_statfs_res res = {
5054 struct rpc_message msg = {
5055 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5060 nfs_fattr_init(fsstat->fattr);
5061 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5064 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5066 struct nfs4_exception exception = {
5067 .interruptible = true,
5071 err = nfs4_handle_exception(server,
5072 _nfs4_proc_statfs(server, fhandle, fsstat),
5074 } while (exception.retry);
5078 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5079 struct nfs_fsinfo *fsinfo)
5081 struct nfs4_fsinfo_arg args = {
5083 .bitmask = server->attr_bitmask,
5085 struct nfs4_fsinfo_res res = {
5088 struct rpc_message msg = {
5089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5094 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5097 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5099 struct nfs4_exception exception = {
5100 .interruptible = true,
5105 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5106 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5108 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5111 err = nfs4_handle_exception(server, err, &exception);
5112 } while (exception.retry);
5116 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5120 nfs_fattr_init(fsinfo->fattr);
5121 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5123 /* block layout checks this! */
5124 server->pnfs_blksize = fsinfo->blksize;
5125 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5131 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5132 struct nfs_pathconf *pathconf)
5134 struct nfs4_pathconf_arg args = {
5136 .bitmask = server->attr_bitmask,
5138 struct nfs4_pathconf_res res = {
5139 .pathconf = pathconf,
5141 struct rpc_message msg = {
5142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5147 /* None of the pathconf attributes are mandatory to implement */
5148 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5149 memset(pathconf, 0, sizeof(*pathconf));
5153 nfs_fattr_init(pathconf->fattr);
5154 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5157 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5158 struct nfs_pathconf *pathconf)
5160 struct nfs4_exception exception = {
5161 .interruptible = true,
5166 err = nfs4_handle_exception(server,
5167 _nfs4_proc_pathconf(server, fhandle, pathconf),
5169 } while (exception.retry);
5173 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5174 const struct nfs_open_context *ctx,
5175 const struct nfs_lock_context *l_ctx,
5178 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5180 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5182 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5183 const struct nfs_open_context *ctx,
5184 const struct nfs_lock_context *l_ctx,
5187 nfs4_stateid current_stateid;
5189 /* If the current stateid represents a lost lock, then exit */
5190 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
5192 return nfs4_stateid_match(stateid, ¤t_stateid);
5195 static bool nfs4_error_stateid_expired(int err)
5198 case -NFS4ERR_DELEG_REVOKED:
5199 case -NFS4ERR_ADMIN_REVOKED:
5200 case -NFS4ERR_BAD_STATEID:
5201 case -NFS4ERR_STALE_STATEID:
5202 case -NFS4ERR_OLD_STATEID:
5203 case -NFS4ERR_OPENMODE:
5204 case -NFS4ERR_EXPIRED:
5210 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5212 struct nfs_server *server = NFS_SERVER(hdr->inode);
5214 trace_nfs4_read(hdr, task->tk_status);
5215 if (task->tk_status < 0) {
5216 struct nfs4_exception exception = {
5217 .inode = hdr->inode,
5218 .state = hdr->args.context->state,
5219 .stateid = &hdr->args.stateid,
5221 task->tk_status = nfs4_async_handle_exception(task,
5222 server, task->tk_status, &exception);
5223 if (exception.retry) {
5224 rpc_restart_call_prepare(task);
5229 if (task->tk_status > 0)
5230 renew_lease(server, hdr->timestamp);
5234 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5235 struct nfs_pgio_args *args)
5238 if (!nfs4_error_stateid_expired(task->tk_status) ||
5239 nfs4_stateid_is_current(&args->stateid,
5244 rpc_restart_call_prepare(task);
5248 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5251 dprintk("--> %s\n", __func__);
5253 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5255 if (nfs4_read_stateid_changed(task, &hdr->args))
5257 if (task->tk_status > 0)
5258 nfs_invalidate_atime(hdr->inode);
5259 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5260 nfs4_read_done_cb(task, hdr);
5263 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5264 struct rpc_message *msg)
5266 hdr->timestamp = jiffies;
5267 if (!hdr->pgio_done_cb)
5268 hdr->pgio_done_cb = nfs4_read_done_cb;
5269 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5270 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5273 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5274 struct nfs_pgio_header *hdr)
5276 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5277 &hdr->args.seq_args,
5281 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5282 hdr->args.lock_context,
5283 hdr->rw_mode) == -EIO)
5285 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5290 static int nfs4_write_done_cb(struct rpc_task *task,
5291 struct nfs_pgio_header *hdr)
5293 struct inode *inode = hdr->inode;
5295 trace_nfs4_write(hdr, task->tk_status);
5296 if (task->tk_status < 0) {
5297 struct nfs4_exception exception = {
5298 .inode = hdr->inode,
5299 .state = hdr->args.context->state,
5300 .stateid = &hdr->args.stateid,
5302 task->tk_status = nfs4_async_handle_exception(task,
5303 NFS_SERVER(inode), task->tk_status,
5305 if (exception.retry) {
5306 rpc_restart_call_prepare(task);
5310 if (task->tk_status >= 0) {
5311 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5312 nfs_writeback_update_inode(hdr);
5317 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5318 struct nfs_pgio_args *args)
5321 if (!nfs4_error_stateid_expired(task->tk_status) ||
5322 nfs4_stateid_is_current(&args->stateid,
5327 rpc_restart_call_prepare(task);
5331 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5333 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5335 if (nfs4_write_stateid_changed(task, &hdr->args))
5337 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5338 nfs4_write_done_cb(task, hdr);
5342 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5344 /* Don't request attributes for pNFS or O_DIRECT writes */
5345 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5347 /* Otherwise, request attributes if and only if we don't hold
5350 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5353 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5354 struct rpc_message *msg,
5355 struct rpc_clnt **clnt)
5357 struct nfs_server *server = NFS_SERVER(hdr->inode);
5359 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5360 hdr->args.bitmask = NULL;
5361 hdr->res.fattr = NULL;
5363 hdr->args.bitmask = server->cache_consistency_bitmask;
5365 if (!hdr->pgio_done_cb)
5366 hdr->pgio_done_cb = nfs4_write_done_cb;
5367 hdr->res.server = server;
5368 hdr->timestamp = jiffies;
5370 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5371 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5372 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5375 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5377 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5378 &data->args.seq_args,
5383 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5385 struct inode *inode = data->inode;
5387 trace_nfs4_commit(data, task->tk_status);
5388 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5389 NULL, NULL) == -EAGAIN) {
5390 rpc_restart_call_prepare(task);
5396 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5398 if (!nfs4_sequence_done(task, &data->res.seq_res))
5400 return data->commit_done_cb(task, data);
5403 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5404 struct rpc_clnt **clnt)
5406 struct nfs_server *server = NFS_SERVER(data->inode);
5408 if (data->commit_done_cb == NULL)
5409 data->commit_done_cb = nfs4_commit_done_cb;
5410 data->res.server = server;
5411 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5412 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5413 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5416 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5417 struct nfs_commitres *res)
5419 struct inode *dst_inode = file_inode(dst);
5420 struct nfs_server *server = NFS_SERVER(dst_inode);
5421 struct rpc_message msg = {
5422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5427 args->fh = NFS_FH(dst_inode);
5428 return nfs4_call_sync(server->client, server, &msg,
5429 &args->seq_args, &res->seq_res, 1);
5432 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5434 struct nfs_commitargs args = {
5438 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5439 struct nfs4_exception exception = { };
5443 status = _nfs4_proc_commit(dst, &args, res);
5444 status = nfs4_handle_exception(dst_server, status, &exception);
5445 } while (exception.retry);
5450 struct nfs4_renewdata {
5451 struct nfs_client *client;
5452 unsigned long timestamp;
5456 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5457 * standalone procedure for queueing an asynchronous RENEW.
5459 static void nfs4_renew_release(void *calldata)
5461 struct nfs4_renewdata *data = calldata;
5462 struct nfs_client *clp = data->client;
5464 if (refcount_read(&clp->cl_count) > 1)
5465 nfs4_schedule_state_renewal(clp);
5466 nfs_put_client(clp);
5470 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5472 struct nfs4_renewdata *data = calldata;
5473 struct nfs_client *clp = data->client;
5474 unsigned long timestamp = data->timestamp;
5476 trace_nfs4_renew_async(clp, task->tk_status);
5477 switch (task->tk_status) {
5480 case -NFS4ERR_LEASE_MOVED:
5481 nfs4_schedule_lease_moved_recovery(clp);
5484 /* Unless we're shutting down, schedule state recovery! */
5485 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5487 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5488 nfs4_schedule_lease_recovery(clp);
5491 nfs4_schedule_path_down_recovery(clp);
5493 do_renew_lease(clp, timestamp);
5496 static const struct rpc_call_ops nfs4_renew_ops = {
5497 .rpc_call_done = nfs4_renew_done,
5498 .rpc_release = nfs4_renew_release,
5501 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5503 struct rpc_message msg = {
5504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5508 struct nfs4_renewdata *data;
5510 if (renew_flags == 0)
5512 if (!refcount_inc_not_zero(&clp->cl_count))
5514 data = kmalloc(sizeof(*data), GFP_NOFS);
5516 nfs_put_client(clp);
5520 data->timestamp = jiffies;
5521 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5522 &nfs4_renew_ops, data);
5525 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5527 struct rpc_message msg = {
5528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5532 unsigned long now = jiffies;
5535 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5538 do_renew_lease(clp, now);
5542 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5544 return server->caps & NFS_CAP_ACLS;
5547 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5548 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5551 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5553 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5554 struct page **pages)
5556 struct page *newpage, **spages;
5562 len = min_t(size_t, PAGE_SIZE, buflen);
5563 newpage = alloc_page(GFP_KERNEL);
5565 if (newpage == NULL)
5567 memcpy(page_address(newpage), buf, len);
5572 } while (buflen != 0);
5578 __free_page(spages[rc-1]);
5582 struct nfs4_cached_acl {
5588 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5590 struct nfs_inode *nfsi = NFS_I(inode);
5592 spin_lock(&inode->i_lock);
5593 kfree(nfsi->nfs4_acl);
5594 nfsi->nfs4_acl = acl;
5595 spin_unlock(&inode->i_lock);
5598 static void nfs4_zap_acl_attr(struct inode *inode)
5600 nfs4_set_cached_acl(inode, NULL);
5603 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5605 struct nfs_inode *nfsi = NFS_I(inode);
5606 struct nfs4_cached_acl *acl;
5609 spin_lock(&inode->i_lock);
5610 acl = nfsi->nfs4_acl;
5613 if (buf == NULL) /* user is just asking for length */
5615 if (acl->cached == 0)
5617 ret = -ERANGE; /* see getxattr(2) man page */
5618 if (acl->len > buflen)
5620 memcpy(buf, acl->data, acl->len);
5624 spin_unlock(&inode->i_lock);
5628 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5630 struct nfs4_cached_acl *acl;
5631 size_t buflen = sizeof(*acl) + acl_len;
5633 if (buflen <= PAGE_SIZE) {
5634 acl = kmalloc(buflen, GFP_KERNEL);
5638 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5640 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5647 nfs4_set_cached_acl(inode, acl);
5651 * The getxattr API returns the required buffer length when called with a
5652 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5653 * the required buf. On a NULL buf, we send a page of data to the server
5654 * guessing that the ACL request can be serviced by a page. If so, we cache
5655 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5656 * the cache. If not so, we throw away the page, and cache the required
5657 * length. The next getxattr call will then produce another round trip to
5658 * the server, this time with the input buf of the required size.
5660 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5662 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5663 struct nfs_getaclargs args = {
5664 .fh = NFS_FH(inode),
5668 struct nfs_getaclres res = {
5671 struct rpc_message msg = {
5672 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5676 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5677 int ret = -ENOMEM, i;
5679 if (npages > ARRAY_SIZE(pages))
5682 for (i = 0; i < npages; i++) {
5683 pages[i] = alloc_page(GFP_KERNEL);
5688 /* for decoding across pages */
5689 res.acl_scratch = alloc_page(GFP_KERNEL);
5690 if (!res.acl_scratch)
5693 args.acl_len = npages * PAGE_SIZE;
5695 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5696 __func__, buf, buflen, npages, args.acl_len);
5697 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5698 &msg, &args.seq_args, &res.seq_res, 0);
5702 /* Handle the case where the passed-in buffer is too short */
5703 if (res.acl_flags & NFS4_ACL_TRUNC) {
5704 /* Did the user only issue a request for the acl length? */
5710 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5712 if (res.acl_len > buflen) {
5716 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5721 for (i = 0; i < npages; i++)
5723 __free_page(pages[i]);
5724 if (res.acl_scratch)
5725 __free_page(res.acl_scratch);
5729 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5731 struct nfs4_exception exception = {
5732 .interruptible = true,
5736 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5737 trace_nfs4_get_acl(inode, ret);
5740 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5741 } while (exception.retry);
5745 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5747 struct nfs_server *server = NFS_SERVER(inode);
5750 if (!nfs4_server_supports_acls(server))
5752 ret = nfs_revalidate_inode(server, inode);
5755 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5756 nfs_zap_acl_cache(inode);
5757 ret = nfs4_read_cached_acl(inode, buf, buflen);
5759 /* -ENOENT is returned if there is no ACL or if there is an ACL
5760 * but no cached acl data, just the acl length */
5762 return nfs4_get_acl_uncached(inode, buf, buflen);
5765 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5767 struct nfs_server *server = NFS_SERVER(inode);
5768 struct page *pages[NFS4ACL_MAXPAGES];
5769 struct nfs_setaclargs arg = {
5770 .fh = NFS_FH(inode),
5774 struct nfs_setaclres res;
5775 struct rpc_message msg = {
5776 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5780 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5783 /* You can't remove system.nfs4_acl: */
5786 if (!nfs4_server_supports_acls(server))
5788 if (npages > ARRAY_SIZE(pages))
5790 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5793 nfs4_inode_make_writeable(inode);
5794 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5797 * Free each page after tx, so the only ref left is
5798 * held by the network stack
5801 put_page(pages[i-1]);
5804 * Acl update can result in inode attribute update.
5805 * so mark the attribute cache invalid.
5807 spin_lock(&inode->i_lock);
5808 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5809 | NFS_INO_INVALID_CTIME
5810 | NFS_INO_REVAL_FORCED;
5811 spin_unlock(&inode->i_lock);
5812 nfs_access_zap_cache(inode);
5813 nfs_zap_acl_cache(inode);
5817 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5819 struct nfs4_exception exception = { };
5822 err = __nfs4_proc_set_acl(inode, buf, buflen);
5823 trace_nfs4_set_acl(inode, err);
5824 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
5826 * no need to retry since the kernel
5827 * isn't involved in encoding the ACEs.
5832 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5834 } while (exception.retry);
5838 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5839 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5842 struct nfs_server *server = NFS_SERVER(inode);
5843 struct nfs_fattr fattr;
5844 struct nfs4_label label = {0, 0, buflen, buf};
5846 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5847 struct nfs4_getattr_arg arg = {
5848 .fh = NFS_FH(inode),
5851 struct nfs4_getattr_res res = {
5856 struct rpc_message msg = {
5857 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5863 nfs_fattr_init(&fattr);
5865 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5868 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5873 static int nfs4_get_security_label(struct inode *inode, void *buf,
5876 struct nfs4_exception exception = {
5877 .interruptible = true,
5881 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5885 err = _nfs4_get_security_label(inode, buf, buflen);
5886 trace_nfs4_get_security_label(inode, err);
5887 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5889 } while (exception.retry);
5893 static int _nfs4_do_set_security_label(struct inode *inode,
5894 struct nfs4_label *ilabel,
5895 struct nfs_fattr *fattr,
5896 struct nfs4_label *olabel)
5899 struct iattr sattr = {0};
5900 struct nfs_server *server = NFS_SERVER(inode);
5901 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5902 struct nfs_setattrargs arg = {
5903 .fh = NFS_FH(inode),
5909 struct nfs_setattrres res = {
5914 struct rpc_message msg = {
5915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5921 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5923 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5925 dprintk("%s failed: %d\n", __func__, status);
5930 static int nfs4_do_set_security_label(struct inode *inode,
5931 struct nfs4_label *ilabel,
5932 struct nfs_fattr *fattr,
5933 struct nfs4_label *olabel)
5935 struct nfs4_exception exception = { };
5939 err = _nfs4_do_set_security_label(inode, ilabel,
5941 trace_nfs4_set_security_label(inode, err);
5942 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5944 } while (exception.retry);
5949 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5951 struct nfs4_label ilabel, *olabel = NULL;
5952 struct nfs_fattr fattr;
5955 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5958 nfs_fattr_init(&fattr);
5962 ilabel.label = (char *)buf;
5963 ilabel.len = buflen;
5965 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5966 if (IS_ERR(olabel)) {
5967 status = -PTR_ERR(olabel);
5971 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5973 nfs_setsecurity(inode, &fattr, olabel);
5975 nfs4_label_free(olabel);
5979 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5982 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5983 nfs4_verifier *bootverf)
5987 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5988 /* An impossible timestamp guarantees this value
5989 * will never match a generated boot time. */
5990 verf[0] = cpu_to_be32(U32_MAX);
5991 verf[1] = cpu_to_be32(U32_MAX);
5993 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5994 u64 ns = ktime_to_ns(nn->boot_time);
5996 verf[0] = cpu_to_be32(ns >> 32);
5997 verf[1] = cpu_to_be32(ns);
5999 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6003 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6008 if (clp->cl_owner_id != NULL)
6013 strlen(clp->cl_rpcclient->cl_nodename) +
6015 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6018 if (nfs4_client_id_uniquifier[0] != '\0')
6019 len += strlen(nfs4_client_id_uniquifier) + 1;
6020 if (len > NFS4_OPAQUE_LIMIT + 1)
6024 * Since this string is allocated at mount time, and held until the
6025 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6026 * about a memory-reclaim deadlock.
6028 str = kmalloc(len, GFP_KERNEL);
6033 if (nfs4_client_id_uniquifier[0] != '\0')
6034 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6035 clp->cl_rpcclient->cl_nodename,
6036 nfs4_client_id_uniquifier,
6037 rpc_peeraddr2str(clp->cl_rpcclient,
6040 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6041 clp->cl_rpcclient->cl_nodename,
6042 rpc_peeraddr2str(clp->cl_rpcclient,
6046 clp->cl_owner_id = str;
6051 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
6056 len = 10 + 10 + 1 + 10 + 1 +
6057 strlen(nfs4_client_id_uniquifier) + 1 +
6058 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6060 if (len > NFS4_OPAQUE_LIMIT + 1)
6064 * Since this string is allocated at mount time, and held until the
6065 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6066 * about a memory-reclaim deadlock.
6068 str = kmalloc(len, GFP_KERNEL);
6072 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6073 clp->rpc_ops->version, clp->cl_minorversion,
6074 nfs4_client_id_uniquifier,
6075 clp->cl_rpcclient->cl_nodename);
6076 clp->cl_owner_id = str;
6081 nfs4_init_uniform_client_string(struct nfs_client *clp)
6086 if (clp->cl_owner_id != NULL)
6089 if (nfs4_client_id_uniquifier[0] != '\0')
6090 return nfs4_init_uniquifier_client_string(clp);
6092 len = 10 + 10 + 1 + 10 + 1 +
6093 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6095 if (len > NFS4_OPAQUE_LIMIT + 1)
6099 * Since this string is allocated at mount time, and held until the
6100 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6101 * about a memory-reclaim deadlock.
6103 str = kmalloc(len, GFP_KERNEL);
6107 scnprintf(str, len, "Linux NFSv%u.%u %s",
6108 clp->rpc_ops->version, clp->cl_minorversion,
6109 clp->cl_rpcclient->cl_nodename);
6110 clp->cl_owner_id = str;
6115 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6116 * services. Advertise one based on the address family of the
6120 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6122 if (strchr(clp->cl_ipaddr, ':') != NULL)
6123 return scnprintf(buf, len, "tcp6");
6125 return scnprintf(buf, len, "tcp");
6128 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6130 struct nfs4_setclientid *sc = calldata;
6132 if (task->tk_status == 0)
6133 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6136 static const struct rpc_call_ops nfs4_setclientid_ops = {
6137 .rpc_call_done = nfs4_setclientid_done,
6141 * nfs4_proc_setclientid - Negotiate client ID
6142 * @clp: state data structure
6143 * @program: RPC program for NFSv4 callback service
6144 * @port: IP port number for NFS4 callback service
6145 * @cred: credential to use for this call
6146 * @res: where to place the result
6148 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6150 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6151 unsigned short port, const struct cred *cred,
6152 struct nfs4_setclientid_res *res)
6154 nfs4_verifier sc_verifier;
6155 struct nfs4_setclientid setclientid = {
6156 .sc_verifier = &sc_verifier,
6160 struct rpc_message msg = {
6161 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6162 .rpc_argp = &setclientid,
6166 struct rpc_task_setup task_setup_data = {
6167 .rpc_client = clp->cl_rpcclient,
6168 .rpc_message = &msg,
6169 .callback_ops = &nfs4_setclientid_ops,
6170 .callback_data = &setclientid,
6171 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6173 unsigned long now = jiffies;
6176 /* nfs_client_id4 */
6177 nfs4_init_boot_verifier(clp, &sc_verifier);
6179 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6180 status = nfs4_init_uniform_client_string(clp);
6182 status = nfs4_init_nonuniform_client_string(clp);
6188 setclientid.sc_netid_len =
6189 nfs4_init_callback_netid(clp,
6190 setclientid.sc_netid,
6191 sizeof(setclientid.sc_netid));
6192 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6193 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6194 clp->cl_ipaddr, port >> 8, port & 255);
6196 dprintk("NFS call setclientid auth=%s, '%s'\n",
6197 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6200 status = nfs4_call_sync_custom(&task_setup_data);
6201 if (setclientid.sc_cred) {
6202 kfree(clp->cl_acceptor);
6203 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6204 put_rpccred(setclientid.sc_cred);
6208 do_renew_lease(clp, now);
6210 trace_nfs4_setclientid(clp, status);
6211 dprintk("NFS reply setclientid: %d\n", status);
6216 * nfs4_proc_setclientid_confirm - Confirm client ID
6217 * @clp: state data structure
6218 * @arg: result of a previous SETCLIENTID
6219 * @cred: credential to use for this call
6221 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6223 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6224 struct nfs4_setclientid_res *arg,
6225 const struct cred *cred)
6227 struct rpc_message msg = {
6228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6234 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6235 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6237 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6238 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6239 trace_nfs4_setclientid_confirm(clp, status);
6240 dprintk("NFS reply setclientid_confirm: %d\n", status);
6244 struct nfs4_delegreturndata {
6245 struct nfs4_delegreturnargs args;
6246 struct nfs4_delegreturnres res;
6248 nfs4_stateid stateid;
6249 unsigned long timestamp;
6251 struct nfs4_layoutreturn_args arg;
6252 struct nfs4_layoutreturn_res res;
6253 struct nfs4_xdr_opaque_data ld_private;
6257 struct nfs_fattr fattr;
6259 struct inode *inode;
6262 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6264 struct nfs4_delegreturndata *data = calldata;
6265 struct nfs4_exception exception = {
6266 .inode = data->inode,
6267 .stateid = &data->stateid,
6268 .task_is_privileged = data->args.seq_args.sa_privileged,
6271 if (!nfs4_sequence_done(task, &data->res.seq_res))
6274 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6276 /* Handle Layoutreturn errors */
6277 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6278 &data->res.lr_ret) == -EAGAIN)
6281 switch (task->tk_status) {
6283 renew_lease(data->res.server, data->timestamp);
6285 case -NFS4ERR_ADMIN_REVOKED:
6286 case -NFS4ERR_DELEG_REVOKED:
6287 case -NFS4ERR_EXPIRED:
6288 nfs4_free_revoked_stateid(data->res.server,
6290 task->tk_msg.rpc_cred);
6292 case -NFS4ERR_BAD_STATEID:
6293 case -NFS4ERR_STALE_STATEID:
6294 task->tk_status = 0;
6296 case -NFS4ERR_OLD_STATEID:
6297 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6299 task->tk_status = 0;
6301 case -NFS4ERR_ACCESS:
6302 if (data->args.bitmask) {
6303 data->args.bitmask = NULL;
6304 data->res.fattr = NULL;
6309 task->tk_status = nfs4_async_handle_exception(task,
6310 data->res.server, task->tk_status,
6312 if (exception.retry)
6315 data->rpc_status = task->tk_status;
6318 task->tk_status = 0;
6319 rpc_restart_call_prepare(task);
6322 static void nfs4_delegreturn_release(void *calldata)
6324 struct nfs4_delegreturndata *data = calldata;
6325 struct inode *inode = data->inode;
6328 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6331 nfs4_fattr_set_prechange(&data->fattr,
6332 inode_peek_iversion_raw(inode));
6333 nfs_refresh_inode(inode, &data->fattr);
6334 nfs_iput_and_deactive(inode);
6339 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6341 struct nfs4_delegreturndata *d_data;
6342 struct pnfs_layout_hdr *lo;
6344 d_data = (struct nfs4_delegreturndata *)data;
6346 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6347 nfs4_sequence_done(task, &d_data->res.seq_res);
6351 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6352 if (lo && !pnfs_layout_is_valid(lo)) {
6353 d_data->args.lr_args = NULL;
6354 d_data->res.lr_res = NULL;
6357 nfs4_setup_sequence(d_data->res.server->nfs_client,
6358 &d_data->args.seq_args,
6359 &d_data->res.seq_res,
6363 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6364 .rpc_call_prepare = nfs4_delegreturn_prepare,
6365 .rpc_call_done = nfs4_delegreturn_done,
6366 .rpc_release = nfs4_delegreturn_release,
6369 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6371 struct nfs4_delegreturndata *data;
6372 struct nfs_server *server = NFS_SERVER(inode);
6373 struct rpc_task *task;
6374 struct rpc_message msg = {
6375 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6378 struct rpc_task_setup task_setup_data = {
6379 .rpc_client = server->client,
6380 .rpc_message = &msg,
6381 .callback_ops = &nfs4_delegreturn_ops,
6382 .flags = RPC_TASK_ASYNC,
6386 data = kzalloc(sizeof(*data), GFP_NOFS);
6390 nfs4_state_protect(server->nfs_client,
6391 NFS_SP4_MACH_CRED_CLEANUP,
6392 &task_setup_data.rpc_client, &msg);
6394 data->args.fhandle = &data->fh;
6395 data->args.stateid = &data->stateid;
6396 data->args.bitmask = server->cache_consistency_bitmask;
6397 nfs_copy_fh(&data->fh, NFS_FH(inode));
6398 nfs4_stateid_copy(&data->stateid, stateid);
6399 data->res.fattr = &data->fattr;
6400 data->res.server = server;
6401 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6402 data->lr.arg.ld_private = &data->lr.ld_private;
6403 nfs_fattr_init(data->res.fattr);
6404 data->timestamp = jiffies;
6405 data->rpc_status = 0;
6406 data->inode = nfs_igrab_and_active(inode);
6407 if (data->inode || issync) {
6408 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6411 data->args.lr_args = &data->lr.arg;
6412 data->res.lr_res = &data->lr.res;
6417 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6420 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6422 task_setup_data.callback_data = data;
6423 msg.rpc_argp = &data->args;
6424 msg.rpc_resp = &data->res;
6425 task = rpc_run_task(&task_setup_data);
6427 return PTR_ERR(task);
6430 status = rpc_wait_for_completion_task(task);
6433 status = data->rpc_status;
6439 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6441 struct nfs_server *server = NFS_SERVER(inode);
6442 struct nfs4_exception exception = { };
6445 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6446 trace_nfs4_delegreturn(inode, stateid, err);
6448 case -NFS4ERR_STALE_STATEID:
6449 case -NFS4ERR_EXPIRED:
6453 err = nfs4_handle_exception(server, err, &exception);
6454 } while (exception.retry);
6458 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6460 struct inode *inode = state->inode;
6461 struct nfs_server *server = NFS_SERVER(inode);
6462 struct nfs_client *clp = server->nfs_client;
6463 struct nfs_lockt_args arg = {
6464 .fh = NFS_FH(inode),
6467 struct nfs_lockt_res res = {
6470 struct rpc_message msg = {
6471 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6474 .rpc_cred = state->owner->so_cred,
6476 struct nfs4_lock_state *lsp;
6479 arg.lock_owner.clientid = clp->cl_clientid;
6480 status = nfs4_set_lock_state(state, request);
6483 lsp = request->fl_u.nfs4_fl.owner;
6484 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6485 arg.lock_owner.s_dev = server->s_dev;
6486 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6489 request->fl_type = F_UNLCK;
6491 case -NFS4ERR_DENIED:
6494 request->fl_ops->fl_release_private(request);
6495 request->fl_ops = NULL;
6500 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6502 struct nfs4_exception exception = {
6503 .interruptible = true,
6508 err = _nfs4_proc_getlk(state, cmd, request);
6509 trace_nfs4_get_lock(request, state, cmd, err);
6510 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6512 } while (exception.retry);
6517 * Update the seqid of a lock stateid after receiving
6518 * NFS4ERR_OLD_STATEID
6520 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6521 struct nfs4_lock_state *lsp)
6523 struct nfs4_state *state = lsp->ls_state;
6526 spin_lock(&state->state_lock);
6527 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6529 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6530 nfs4_stateid_seqid_inc(dst);
6532 dst->seqid = lsp->ls_stateid.seqid;
6535 spin_unlock(&state->state_lock);
6539 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6540 struct nfs4_lock_state *lsp)
6542 struct nfs4_state *state = lsp->ls_state;
6545 spin_lock(&state->state_lock);
6546 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6547 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6548 spin_unlock(&state->state_lock);
6552 struct nfs4_unlockdata {
6553 struct nfs_locku_args arg;
6554 struct nfs_locku_res res;
6555 struct nfs4_lock_state *lsp;
6556 struct nfs_open_context *ctx;
6557 struct nfs_lock_context *l_ctx;
6558 struct file_lock fl;
6559 struct nfs_server *server;
6560 unsigned long timestamp;
6563 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6564 struct nfs_open_context *ctx,
6565 struct nfs4_lock_state *lsp,
6566 struct nfs_seqid *seqid)
6568 struct nfs4_unlockdata *p;
6569 struct nfs4_state *state = lsp->ls_state;
6570 struct inode *inode = state->inode;
6572 p = kzalloc(sizeof(*p), GFP_NOFS);
6575 p->arg.fh = NFS_FH(inode);
6577 p->arg.seqid = seqid;
6578 p->res.seqid = seqid;
6580 /* Ensure we don't close file until we're done freeing locks! */
6581 p->ctx = get_nfs_open_context(ctx);
6582 p->l_ctx = nfs_get_lock_context(ctx);
6583 locks_init_lock(&p->fl);
6584 locks_copy_lock(&p->fl, fl);
6585 p->server = NFS_SERVER(inode);
6586 spin_lock(&state->state_lock);
6587 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6588 spin_unlock(&state->state_lock);
6592 static void nfs4_locku_release_calldata(void *data)
6594 struct nfs4_unlockdata *calldata = data;
6595 nfs_free_seqid(calldata->arg.seqid);
6596 nfs4_put_lock_state(calldata->lsp);
6597 nfs_put_lock_context(calldata->l_ctx);
6598 put_nfs_open_context(calldata->ctx);
6602 static void nfs4_locku_done(struct rpc_task *task, void *data)
6604 struct nfs4_unlockdata *calldata = data;
6605 struct nfs4_exception exception = {
6606 .inode = calldata->lsp->ls_state->inode,
6607 .stateid = &calldata->arg.stateid,
6610 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6612 switch (task->tk_status) {
6614 renew_lease(calldata->server, calldata->timestamp);
6615 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6616 if (nfs4_update_lock_stateid(calldata->lsp,
6617 &calldata->res.stateid))
6620 case -NFS4ERR_ADMIN_REVOKED:
6621 case -NFS4ERR_EXPIRED:
6622 nfs4_free_revoked_stateid(calldata->server,
6623 &calldata->arg.stateid,
6624 task->tk_msg.rpc_cred);
6626 case -NFS4ERR_BAD_STATEID:
6627 case -NFS4ERR_STALE_STATEID:
6628 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6630 rpc_restart_call_prepare(task);
6632 case -NFS4ERR_OLD_STATEID:
6633 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6635 rpc_restart_call_prepare(task);
6638 task->tk_status = nfs4_async_handle_exception(task,
6639 calldata->server, task->tk_status,
6641 if (exception.retry)
6642 rpc_restart_call_prepare(task);
6644 nfs_release_seqid(calldata->arg.seqid);
6647 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6649 struct nfs4_unlockdata *calldata = data;
6651 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6652 nfs_async_iocounter_wait(task, calldata->l_ctx))
6655 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6657 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6658 /* Note: exit _without_ running nfs4_locku_done */
6661 calldata->timestamp = jiffies;
6662 if (nfs4_setup_sequence(calldata->server->nfs_client,
6663 &calldata->arg.seq_args,
6664 &calldata->res.seq_res,
6666 nfs_release_seqid(calldata->arg.seqid);
6669 task->tk_action = NULL;
6671 nfs4_sequence_done(task, &calldata->res.seq_res);
6674 static const struct rpc_call_ops nfs4_locku_ops = {
6675 .rpc_call_prepare = nfs4_locku_prepare,
6676 .rpc_call_done = nfs4_locku_done,
6677 .rpc_release = nfs4_locku_release_calldata,
6680 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6681 struct nfs_open_context *ctx,
6682 struct nfs4_lock_state *lsp,
6683 struct nfs_seqid *seqid)
6685 struct nfs4_unlockdata *data;
6686 struct rpc_message msg = {
6687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6688 .rpc_cred = ctx->cred,
6690 struct rpc_task_setup task_setup_data = {
6691 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6692 .rpc_message = &msg,
6693 .callback_ops = &nfs4_locku_ops,
6694 .workqueue = nfsiod_workqueue,
6695 .flags = RPC_TASK_ASYNC,
6698 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6699 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6701 /* Ensure this is an unlock - when canceling a lock, the
6702 * canceled lock is passed in, and it won't be an unlock.
6704 fl->fl_type = F_UNLCK;
6705 if (fl->fl_flags & FL_CLOSE)
6706 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6708 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6710 nfs_free_seqid(seqid);
6711 return ERR_PTR(-ENOMEM);
6714 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6715 msg.rpc_argp = &data->arg;
6716 msg.rpc_resp = &data->res;
6717 task_setup_data.callback_data = data;
6718 return rpc_run_task(&task_setup_data);
6721 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6723 struct inode *inode = state->inode;
6724 struct nfs4_state_owner *sp = state->owner;
6725 struct nfs_inode *nfsi = NFS_I(inode);
6726 struct nfs_seqid *seqid;
6727 struct nfs4_lock_state *lsp;
6728 struct rpc_task *task;
6729 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6731 unsigned char fl_flags = request->fl_flags;
6733 status = nfs4_set_lock_state(state, request);
6734 /* Unlock _before_ we do the RPC call */
6735 request->fl_flags |= FL_EXISTS;
6736 /* Exclude nfs_delegation_claim_locks() */
6737 mutex_lock(&sp->so_delegreturn_mutex);
6738 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6739 down_read(&nfsi->rwsem);
6740 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6741 up_read(&nfsi->rwsem);
6742 mutex_unlock(&sp->so_delegreturn_mutex);
6745 up_read(&nfsi->rwsem);
6746 mutex_unlock(&sp->so_delegreturn_mutex);
6749 /* Is this a delegated lock? */
6750 lsp = request->fl_u.nfs4_fl.owner;
6751 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6753 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6754 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6758 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6759 status = PTR_ERR(task);
6762 status = rpc_wait_for_completion_task(task);
6765 request->fl_flags = fl_flags;
6766 trace_nfs4_unlock(request, state, F_SETLK, status);
6770 struct nfs4_lockdata {
6771 struct nfs_lock_args arg;
6772 struct nfs_lock_res res;
6773 struct nfs4_lock_state *lsp;
6774 struct nfs_open_context *ctx;
6775 struct file_lock fl;
6776 unsigned long timestamp;
6779 struct nfs_server *server;
6782 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6783 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6786 struct nfs4_lockdata *p;
6787 struct inode *inode = lsp->ls_state->inode;
6788 struct nfs_server *server = NFS_SERVER(inode);
6789 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6791 p = kzalloc(sizeof(*p), gfp_mask);
6795 p->arg.fh = NFS_FH(inode);
6797 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6798 if (IS_ERR(p->arg.open_seqid))
6800 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6801 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6802 if (IS_ERR(p->arg.lock_seqid))
6803 goto out_free_seqid;
6804 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6805 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6806 p->arg.lock_owner.s_dev = server->s_dev;
6807 p->res.lock_seqid = p->arg.lock_seqid;
6810 p->ctx = get_nfs_open_context(ctx);
6811 locks_init_lock(&p->fl);
6812 locks_copy_lock(&p->fl, fl);
6815 nfs_free_seqid(p->arg.open_seqid);
6821 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6823 struct nfs4_lockdata *data = calldata;
6824 struct nfs4_state *state = data->lsp->ls_state;
6826 dprintk("%s: begin!\n", __func__);
6827 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6829 /* Do we need to do an open_to_lock_owner? */
6830 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6831 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6832 goto out_release_lock_seqid;
6834 nfs4_stateid_copy(&data->arg.open_stateid,
6835 &state->open_stateid);
6836 data->arg.new_lock_owner = 1;
6837 data->res.open_seqid = data->arg.open_seqid;
6839 data->arg.new_lock_owner = 0;
6840 nfs4_stateid_copy(&data->arg.lock_stateid,
6841 &data->lsp->ls_stateid);
6843 if (!nfs4_valid_open_stateid(state)) {
6844 data->rpc_status = -EBADF;
6845 task->tk_action = NULL;
6846 goto out_release_open_seqid;
6848 data->timestamp = jiffies;
6849 if (nfs4_setup_sequence(data->server->nfs_client,
6850 &data->arg.seq_args,
6854 out_release_open_seqid:
6855 nfs_release_seqid(data->arg.open_seqid);
6856 out_release_lock_seqid:
6857 nfs_release_seqid(data->arg.lock_seqid);
6859 nfs4_sequence_done(task, &data->res.seq_res);
6860 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6863 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6865 struct nfs4_lockdata *data = calldata;
6866 struct nfs4_lock_state *lsp = data->lsp;
6867 struct nfs_server *server = NFS_SERVER(d_inode(data->ctx->dentry));
6869 dprintk("%s: begin!\n", __func__);
6871 if (!nfs4_sequence_done(task, &data->res.seq_res))
6874 data->rpc_status = task->tk_status;
6875 switch (task->tk_status) {
6877 renew_lease(server, data->timestamp);
6878 if (data->arg.new_lock && !data->cancelled) {
6879 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6880 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6883 if (data->arg.new_lock_owner != 0) {
6884 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6885 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6886 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6887 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6890 case -NFS4ERR_OLD_STATEID:
6891 if (data->arg.new_lock_owner != 0 &&
6892 nfs4_refresh_open_old_stateid(&data->arg.open_stateid,
6895 if (nfs4_refresh_lock_old_stateid(&data->arg.lock_stateid, lsp))
6898 case -NFS4ERR_BAD_STATEID:
6899 case -NFS4ERR_STALE_STATEID:
6900 case -NFS4ERR_EXPIRED:
6901 if (data->arg.new_lock_owner != 0) {
6902 if (!nfs4_stateid_match(&data->arg.open_stateid,
6903 &lsp->ls_state->open_stateid))
6905 else if (nfs4_async_handle_error(task, server, lsp->ls_state, NULL) == -EAGAIN)
6907 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6912 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6915 if (!data->cancelled)
6916 rpc_restart_call_prepare(task);
6920 static void nfs4_lock_release(void *calldata)
6922 struct nfs4_lockdata *data = calldata;
6924 dprintk("%s: begin!\n", __func__);
6925 nfs_free_seqid(data->arg.open_seqid);
6926 if (data->cancelled && data->rpc_status == 0) {
6927 struct rpc_task *task;
6928 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6929 data->arg.lock_seqid);
6931 rpc_put_task_async(task);
6932 dprintk("%s: cancelling lock!\n", __func__);
6934 nfs_free_seqid(data->arg.lock_seqid);
6935 nfs4_put_lock_state(data->lsp);
6936 put_nfs_open_context(data->ctx);
6938 dprintk("%s: done!\n", __func__);
6941 static const struct rpc_call_ops nfs4_lock_ops = {
6942 .rpc_call_prepare = nfs4_lock_prepare,
6943 .rpc_call_done = nfs4_lock_done,
6944 .rpc_release = nfs4_lock_release,
6947 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6950 case -NFS4ERR_ADMIN_REVOKED:
6951 case -NFS4ERR_EXPIRED:
6952 case -NFS4ERR_BAD_STATEID:
6953 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6954 if (new_lock_owner != 0 ||
6955 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6956 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6958 case -NFS4ERR_STALE_STATEID:
6959 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6960 nfs4_schedule_lease_recovery(server->nfs_client);
6964 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6966 struct nfs4_lockdata *data;
6967 struct rpc_task *task;
6968 struct rpc_message msg = {
6969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6970 .rpc_cred = state->owner->so_cred,
6972 struct rpc_task_setup task_setup_data = {
6973 .rpc_client = NFS_CLIENT(state->inode),
6974 .rpc_message = &msg,
6975 .callback_ops = &nfs4_lock_ops,
6976 .workqueue = nfsiod_workqueue,
6977 .flags = RPC_TASK_ASYNC,
6981 dprintk("%s: begin!\n", __func__);
6982 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6983 fl->fl_u.nfs4_fl.owner,
6984 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6988 data->arg.block = 1;
6989 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6990 recovery_type > NFS_LOCK_NEW);
6991 msg.rpc_argp = &data->arg;
6992 msg.rpc_resp = &data->res;
6993 task_setup_data.callback_data = data;
6994 if (recovery_type > NFS_LOCK_NEW) {
6995 if (recovery_type == NFS_LOCK_RECLAIM)
6996 data->arg.reclaim = NFS_LOCK_RECLAIM;
6998 data->arg.new_lock = 1;
6999 task = rpc_run_task(&task_setup_data);
7001 return PTR_ERR(task);
7002 ret = rpc_wait_for_completion_task(task);
7004 ret = data->rpc_status;
7006 nfs4_handle_setlk_error(data->server, data->lsp,
7007 data->arg.new_lock_owner, ret);
7009 data->cancelled = true;
7010 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7012 dprintk("%s: done, ret = %d!\n", __func__, ret);
7016 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7018 struct nfs_server *server = NFS_SERVER(state->inode);
7019 struct nfs4_exception exception = {
7020 .inode = state->inode,
7025 /* Cache the lock if possible... */
7026 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7028 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7029 if (err != -NFS4ERR_DELAY)
7031 nfs4_handle_exception(server, err, &exception);
7032 } while (exception.retry);
7036 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7038 struct nfs_server *server = NFS_SERVER(state->inode);
7039 struct nfs4_exception exception = {
7040 .inode = state->inode,
7044 err = nfs4_set_lock_state(state, request);
7047 if (!recover_lost_locks) {
7048 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7052 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7054 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7058 case -NFS4ERR_GRACE:
7059 case -NFS4ERR_DELAY:
7060 nfs4_handle_exception(server, err, &exception);
7063 } while (exception.retry);
7068 #if defined(CONFIG_NFS_V4_1)
7069 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7071 struct nfs4_lock_state *lsp;
7074 status = nfs4_set_lock_state(state, request);
7077 lsp = request->fl_u.nfs4_fl.owner;
7078 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7079 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7081 return nfs4_lock_expired(state, request);
7085 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7087 struct nfs_inode *nfsi = NFS_I(state->inode);
7088 struct nfs4_state_owner *sp = state->owner;
7089 unsigned char fl_flags = request->fl_flags;
7092 request->fl_flags |= FL_ACCESS;
7093 status = locks_lock_inode_wait(state->inode, request);
7096 mutex_lock(&sp->so_delegreturn_mutex);
7097 down_read(&nfsi->rwsem);
7098 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7099 /* Yes: cache locks! */
7100 /* ...but avoid races with delegation recall... */
7101 request->fl_flags = fl_flags & ~FL_SLEEP;
7102 status = locks_lock_inode_wait(state->inode, request);
7103 up_read(&nfsi->rwsem);
7104 mutex_unlock(&sp->so_delegreturn_mutex);
7107 up_read(&nfsi->rwsem);
7108 mutex_unlock(&sp->so_delegreturn_mutex);
7109 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7111 request->fl_flags = fl_flags;
7115 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7117 struct nfs4_exception exception = {
7119 .inode = state->inode,
7120 .interruptible = true,
7125 err = _nfs4_proc_setlk(state, cmd, request);
7126 if (err == -NFS4ERR_DENIED)
7128 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7130 } while (exception.retry);
7134 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7135 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7138 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7139 struct file_lock *request)
7141 int status = -ERESTARTSYS;
7142 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7144 while(!signalled()) {
7145 status = nfs4_proc_setlk(state, cmd, request);
7146 if ((status != -EAGAIN) || IS_SETLK(cmd))
7148 freezable_schedule_timeout_interruptible(timeout);
7150 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7151 status = -ERESTARTSYS;
7156 #ifdef CONFIG_NFS_V4_1
7157 struct nfs4_lock_waiter {
7158 struct task_struct *task;
7159 struct inode *inode;
7160 struct nfs_lowner *owner;
7164 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7167 struct nfs4_lock_waiter *waiter = wait->private;
7169 /* NULL key means to wake up everyone */
7171 struct cb_notify_lock_args *cbnl = key;
7172 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7173 *wowner = waiter->owner;
7175 /* Only wake if the callback was for the same owner. */
7176 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7179 /* Make sure it's for the right inode */
7180 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7184 /* override "private" so we can use default_wake_function */
7185 wait->private = waiter->task;
7186 ret = woken_wake_function(wait, mode, flags, key);
7188 list_del_init(&wait->entry);
7189 wait->private = waiter;
7194 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7196 int status = -ERESTARTSYS;
7197 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7198 struct nfs_server *server = NFS_SERVER(state->inode);
7199 struct nfs_client *clp = server->nfs_client;
7200 wait_queue_head_t *q = &clp->cl_lock_waitq;
7201 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7202 .id = lsp->ls_seqid.owner_id,
7203 .s_dev = server->s_dev };
7204 struct nfs4_lock_waiter waiter = { .task = current,
7205 .inode = state->inode,
7207 wait_queue_entry_t wait;
7209 /* Don't bother with waitqueue if we don't expect a callback */
7210 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7211 return nfs4_retry_setlk_simple(state, cmd, request);
7214 wait.private = &waiter;
7215 wait.func = nfs4_wake_lock_waiter;
7217 while(!signalled()) {
7218 add_wait_queue(q, &wait);
7219 status = nfs4_proc_setlk(state, cmd, request);
7220 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7221 finish_wait(q, &wait);
7225 status = -ERESTARTSYS;
7226 freezer_do_not_count();
7227 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7229 finish_wait(q, &wait);
7234 #else /* !CONFIG_NFS_V4_1 */
7236 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7238 return nfs4_retry_setlk_simple(state, cmd, request);
7243 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7245 struct nfs_open_context *ctx;
7246 struct nfs4_state *state;
7249 /* verify open state */
7250 ctx = nfs_file_open_context(filp);
7253 if (IS_GETLK(cmd)) {
7255 return nfs4_proc_getlk(state, F_GETLK, request);
7259 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7262 if (request->fl_type == F_UNLCK) {
7264 return nfs4_proc_unlck(state, cmd, request);
7271 if ((request->fl_flags & FL_POSIX) &&
7272 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7276 * Don't rely on the VFS having checked the file open mode,
7277 * since it won't do this for flock() locks.
7279 switch (request->fl_type) {
7281 if (!(filp->f_mode & FMODE_READ))
7285 if (!(filp->f_mode & FMODE_WRITE))
7289 status = nfs4_set_lock_state(state, request);
7293 return nfs4_retry_setlk(state, cmd, request);
7296 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7298 struct nfs_server *server = NFS_SERVER(state->inode);
7301 err = nfs4_set_lock_state(state, fl);
7305 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7306 if (err != -NFS4ERR_DELAY)
7309 } while (err == -NFS4ERR_DELAY);
7310 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7313 struct nfs_release_lockowner_data {
7314 struct nfs4_lock_state *lsp;
7315 struct nfs_server *server;
7316 struct nfs_release_lockowner_args args;
7317 struct nfs_release_lockowner_res res;
7318 unsigned long timestamp;
7321 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7323 struct nfs_release_lockowner_data *data = calldata;
7324 struct nfs_server *server = data->server;
7325 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7326 &data->res.seq_res, task);
7327 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7328 data->timestamp = jiffies;
7331 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7333 struct nfs_release_lockowner_data *data = calldata;
7334 struct nfs_server *server = data->server;
7336 nfs40_sequence_done(task, &data->res.seq_res);
7338 switch (task->tk_status) {
7340 renew_lease(server, data->timestamp);
7342 case -NFS4ERR_STALE_CLIENTID:
7343 case -NFS4ERR_EXPIRED:
7344 nfs4_schedule_lease_recovery(server->nfs_client);
7346 case -NFS4ERR_LEASE_MOVED:
7347 case -NFS4ERR_DELAY:
7348 if (nfs4_async_handle_error(task, server,
7349 NULL, NULL) == -EAGAIN)
7350 rpc_restart_call_prepare(task);
7354 static void nfs4_release_lockowner_release(void *calldata)
7356 struct nfs_release_lockowner_data *data = calldata;
7357 nfs4_free_lock_state(data->server, data->lsp);
7361 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7362 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7363 .rpc_call_done = nfs4_release_lockowner_done,
7364 .rpc_release = nfs4_release_lockowner_release,
7368 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7370 struct nfs_release_lockowner_data *data;
7371 struct rpc_message msg = {
7372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7375 if (server->nfs_client->cl_mvops->minor_version != 0)
7378 data = kmalloc(sizeof(*data), GFP_NOFS);
7382 data->server = server;
7383 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7384 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7385 data->args.lock_owner.s_dev = server->s_dev;
7387 msg.rpc_argp = &data->args;
7388 msg.rpc_resp = &data->res;
7389 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7390 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7393 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7395 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7396 struct dentry *unused, struct inode *inode,
7397 const char *key, const void *buf,
7398 size_t buflen, int flags)
7400 return nfs4_proc_set_acl(inode, buf, buflen);
7403 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7404 struct dentry *unused, struct inode *inode,
7405 const char *key, void *buf, size_t buflen)
7407 return nfs4_proc_get_acl(inode, buf, buflen);
7410 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7412 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7415 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7417 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7418 struct dentry *unused, struct inode *inode,
7419 const char *key, const void *buf,
7420 size_t buflen, int flags)
7422 if (security_ismaclabel(key))
7423 return nfs4_set_security_label(inode, buf, buflen);
7428 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7429 struct dentry *unused, struct inode *inode,
7430 const char *key, void *buf, size_t buflen)
7432 if (security_ismaclabel(key))
7433 return nfs4_get_security_label(inode, buf, buflen);
7438 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7442 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7443 len = security_inode_listsecurity(inode, list, list_len);
7444 if (list_len && len > list_len)
7450 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7451 .prefix = XATTR_SECURITY_PREFIX,
7452 .get = nfs4_xattr_get_nfs4_label,
7453 .set = nfs4_xattr_set_nfs4_label,
7459 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7467 * nfs_fhget will use either the mounted_on_fileid or the fileid
7469 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7471 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7472 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7473 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7474 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7477 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7478 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7479 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7483 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7484 const struct qstr *name,
7485 struct nfs4_fs_locations *fs_locations,
7488 struct nfs_server *server = NFS_SERVER(dir);
7490 struct nfs4_fs_locations_arg args = {
7491 .dir_fh = NFS_FH(dir),
7496 struct nfs4_fs_locations_res res = {
7497 .fs_locations = fs_locations,
7499 struct rpc_message msg = {
7500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7506 dprintk("%s: start\n", __func__);
7508 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7509 bitmask[1] = nfs4_fattr_bitmap[1];
7511 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7512 * is not supported */
7513 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7514 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7516 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7518 nfs_fattr_init(&fs_locations->fattr);
7519 fs_locations->server = server;
7520 fs_locations->nlocations = 0;
7521 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7522 dprintk("%s: returned status = %d\n", __func__, status);
7526 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7527 const struct qstr *name,
7528 struct nfs4_fs_locations *fs_locations,
7531 struct nfs4_exception exception = {
7532 .interruptible = true,
7536 err = _nfs4_proc_fs_locations(client, dir, name,
7537 fs_locations, page);
7538 trace_nfs4_get_fs_locations(dir, name, err);
7539 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7541 } while (exception.retry);
7546 * This operation also signals the server that this client is
7547 * performing migration recovery. The server can stop returning
7548 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7549 * appended to this compound to identify the client ID which is
7550 * performing recovery.
7552 static int _nfs40_proc_get_locations(struct inode *inode,
7553 struct nfs4_fs_locations *locations,
7554 struct page *page, const struct cred *cred)
7556 struct nfs_server *server = NFS_SERVER(inode);
7557 struct rpc_clnt *clnt = server->client;
7559 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7561 struct nfs4_fs_locations_arg args = {
7562 .clientid = server->nfs_client->cl_clientid,
7563 .fh = NFS_FH(inode),
7566 .migration = 1, /* skip LOOKUP */
7567 .renew = 1, /* append RENEW */
7569 struct nfs4_fs_locations_res res = {
7570 .fs_locations = locations,
7574 struct rpc_message msg = {
7575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7580 unsigned long now = jiffies;
7583 nfs_fattr_init(&locations->fattr);
7584 locations->server = server;
7585 locations->nlocations = 0;
7587 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7588 status = nfs4_call_sync_sequence(clnt, server, &msg,
7589 &args.seq_args, &res.seq_res);
7593 renew_lease(server, now);
7597 #ifdef CONFIG_NFS_V4_1
7600 * This operation also signals the server that this client is
7601 * performing migration recovery. The server can stop asserting
7602 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7603 * performing this operation is identified in the SEQUENCE
7604 * operation in this compound.
7606 * When the client supports GETATTR(fs_locations_info), it can
7607 * be plumbed in here.
7609 static int _nfs41_proc_get_locations(struct inode *inode,
7610 struct nfs4_fs_locations *locations,
7611 struct page *page, const struct cred *cred)
7613 struct nfs_server *server = NFS_SERVER(inode);
7614 struct rpc_clnt *clnt = server->client;
7616 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7618 struct nfs4_fs_locations_arg args = {
7619 .fh = NFS_FH(inode),
7622 .migration = 1, /* skip LOOKUP */
7624 struct nfs4_fs_locations_res res = {
7625 .fs_locations = locations,
7628 struct rpc_message msg = {
7629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7636 nfs_fattr_init(&locations->fattr);
7637 locations->server = server;
7638 locations->nlocations = 0;
7640 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7641 status = nfs4_call_sync_sequence(clnt, server, &msg,
7642 &args.seq_args, &res.seq_res);
7643 if (status == NFS4_OK &&
7644 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7645 status = -NFS4ERR_LEASE_MOVED;
7649 #endif /* CONFIG_NFS_V4_1 */
7652 * nfs4_proc_get_locations - discover locations for a migrated FSID
7653 * @inode: inode on FSID that is migrating
7654 * @locations: result of query
7656 * @cred: credential to use for this operation
7658 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7659 * operation failed, or a negative errno if a local error occurred.
7661 * On success, "locations" is filled in, but if the server has
7662 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7665 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7666 * from this client that require migration recovery.
7668 int nfs4_proc_get_locations(struct inode *inode,
7669 struct nfs4_fs_locations *locations,
7670 struct page *page, const struct cred *cred)
7672 struct nfs_server *server = NFS_SERVER(inode);
7673 struct nfs_client *clp = server->nfs_client;
7674 const struct nfs4_mig_recovery_ops *ops =
7675 clp->cl_mvops->mig_recovery_ops;
7676 struct nfs4_exception exception = {
7677 .interruptible = true,
7681 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7682 (unsigned long long)server->fsid.major,
7683 (unsigned long long)server->fsid.minor,
7685 nfs_display_fhandle(NFS_FH(inode), __func__);
7688 status = ops->get_locations(inode, locations, page, cred);
7689 if (status != -NFS4ERR_DELAY)
7691 nfs4_handle_exception(server, status, &exception);
7692 } while (exception.retry);
7697 * This operation also signals the server that this client is
7698 * performing "lease moved" recovery. The server can stop
7699 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7700 * is appended to this compound to identify the client ID which is
7701 * performing recovery.
7703 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7705 struct nfs_server *server = NFS_SERVER(inode);
7706 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7707 struct rpc_clnt *clnt = server->client;
7708 struct nfs4_fsid_present_arg args = {
7709 .fh = NFS_FH(inode),
7710 .clientid = clp->cl_clientid,
7711 .renew = 1, /* append RENEW */
7713 struct nfs4_fsid_present_res res = {
7716 struct rpc_message msg = {
7717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7722 unsigned long now = jiffies;
7725 res.fh = nfs_alloc_fhandle();
7729 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7730 status = nfs4_call_sync_sequence(clnt, server, &msg,
7731 &args.seq_args, &res.seq_res);
7732 nfs_free_fhandle(res.fh);
7736 do_renew_lease(clp, now);
7740 #ifdef CONFIG_NFS_V4_1
7743 * This operation also signals the server that this client is
7744 * performing "lease moved" recovery. The server can stop asserting
7745 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7746 * this operation is identified in the SEQUENCE operation in this
7749 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7751 struct nfs_server *server = NFS_SERVER(inode);
7752 struct rpc_clnt *clnt = server->client;
7753 struct nfs4_fsid_present_arg args = {
7754 .fh = NFS_FH(inode),
7756 struct nfs4_fsid_present_res res = {
7758 struct rpc_message msg = {
7759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7766 res.fh = nfs_alloc_fhandle();
7770 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7771 status = nfs4_call_sync_sequence(clnt, server, &msg,
7772 &args.seq_args, &res.seq_res);
7773 nfs_free_fhandle(res.fh);
7774 if (status == NFS4_OK &&
7775 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7776 status = -NFS4ERR_LEASE_MOVED;
7780 #endif /* CONFIG_NFS_V4_1 */
7783 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7784 * @inode: inode on FSID to check
7785 * @cred: credential to use for this operation
7787 * Server indicates whether the FSID is present, moved, or not
7788 * recognized. This operation is necessary to clear a LEASE_MOVED
7789 * condition for this client ID.
7791 * Returns NFS4_OK if the FSID is present on this server,
7792 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7793 * NFS4ERR code if some error occurred on the server, or a
7794 * negative errno if a local failure occurred.
7796 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
7798 struct nfs_server *server = NFS_SERVER(inode);
7799 struct nfs_client *clp = server->nfs_client;
7800 const struct nfs4_mig_recovery_ops *ops =
7801 clp->cl_mvops->mig_recovery_ops;
7802 struct nfs4_exception exception = {
7803 .interruptible = true,
7807 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7808 (unsigned long long)server->fsid.major,
7809 (unsigned long long)server->fsid.minor,
7811 nfs_display_fhandle(NFS_FH(inode), __func__);
7814 status = ops->fsid_present(inode, cred);
7815 if (status != -NFS4ERR_DELAY)
7817 nfs4_handle_exception(server, status, &exception);
7818 } while (exception.retry);
7823 * If 'use_integrity' is true and the state managment nfs_client
7824 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7825 * and the machine credential as per RFC3530bis and RFC5661 Security
7826 * Considerations sections. Otherwise, just use the user cred with the
7827 * filesystem's rpc_client.
7829 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7832 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7833 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
7834 struct nfs4_secinfo_arg args = {
7835 .dir_fh = NFS_FH(dir),
7838 struct nfs4_secinfo_res res = {
7841 struct rpc_message msg = {
7842 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7846 struct nfs4_call_sync_data data = {
7847 .seq_server = NFS_SERVER(dir),
7848 .seq_args = &args.seq_args,
7849 .seq_res = &res.seq_res,
7851 struct rpc_task_setup task_setup = {
7853 .rpc_message = &msg,
7854 .callback_ops = clp->cl_mvops->call_sync_ops,
7855 .callback_data = &data,
7856 .flags = RPC_TASK_NO_ROUND_ROBIN,
7858 const struct cred *cred = NULL;
7860 if (use_integrity) {
7861 clnt = clp->cl_rpcclient;
7862 task_setup.rpc_client = clnt;
7864 cred = nfs4_get_clid_cred(clp);
7865 msg.rpc_cred = cred;
7868 dprintk("NFS call secinfo %s\n", name->name);
7870 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7871 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
7872 status = nfs4_call_sync_custom(&task_setup);
7874 dprintk("NFS reply secinfo: %d\n", status);
7880 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7881 struct nfs4_secinfo_flavors *flavors)
7883 struct nfs4_exception exception = {
7884 .interruptible = true,
7888 err = -NFS4ERR_WRONGSEC;
7890 /* try to use integrity protection with machine cred */
7891 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7892 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7895 * if unable to use integrity protection, or SECINFO with
7896 * integrity protection returns NFS4ERR_WRONGSEC (which is
7897 * disallowed by spec, but exists in deployed servers) use
7898 * the current filesystem's rpc_client and the user cred.
7900 if (err == -NFS4ERR_WRONGSEC)
7901 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7903 trace_nfs4_secinfo(dir, name, err);
7904 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7906 } while (exception.retry);
7910 #ifdef CONFIG_NFS_V4_1
7912 * Check the exchange flags returned by the server for invalid flags, having
7913 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7916 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
7918 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
7920 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
7922 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7923 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7925 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7929 return -NFS4ERR_INVAL;
7933 nfs41_same_server_scope(struct nfs41_server_scope *a,
7934 struct nfs41_server_scope *b)
7936 if (a->server_scope_sz != b->server_scope_sz)
7938 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7942 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7944 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
7945 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
7946 struct nfs_client *clp = args->client;
7948 switch (task->tk_status) {
7949 case -NFS4ERR_BADSESSION:
7950 case -NFS4ERR_DEADSESSION:
7951 nfs4_schedule_session_recovery(clp->cl_session,
7955 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
7956 res->dir != NFS4_CDFS4_BOTH) {
7957 rpc_task_close_connection(task);
7958 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
7959 rpc_restart_call(task);
7963 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7964 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
7968 * nfs4_proc_bind_one_conn_to_session()
7970 * The 4.1 client currently uses the same TCP connection for the
7971 * fore and backchannel.
7974 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7975 struct rpc_xprt *xprt,
7976 struct nfs_client *clp,
7977 const struct cred *cred)
7980 struct nfs41_bind_conn_to_session_args args = {
7982 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7985 struct nfs41_bind_conn_to_session_res res;
7986 struct rpc_message msg = {
7988 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7993 struct rpc_task_setup task_setup_data = {
7996 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7997 .rpc_message = &msg,
7998 .flags = RPC_TASK_TIMEOUT,
8000 struct rpc_task *task;
8002 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8003 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8004 args.dir = NFS4_CDFC4_FORE;
8006 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8007 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8008 args.dir = NFS4_CDFC4_FORE;
8010 task = rpc_run_task(&task_setup_data);
8011 if (!IS_ERR(task)) {
8012 status = task->tk_status;
8015 status = PTR_ERR(task);
8016 trace_nfs4_bind_conn_to_session(clp, status);
8018 if (memcmp(res.sessionid.data,
8019 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8020 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8023 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8024 dprintk("NFS: %s: Unexpected direction from server\n",
8028 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8029 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8038 struct rpc_bind_conn_calldata {
8039 struct nfs_client *clp;
8040 const struct cred *cred;
8044 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8045 struct rpc_xprt *xprt,
8048 struct rpc_bind_conn_calldata *p = calldata;
8050 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8053 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8055 struct rpc_bind_conn_calldata data = {
8059 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8060 nfs4_proc_bind_conn_to_session_callback, &data);
8064 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8065 * and operations we'd like to see to enable certain features in the allow map
8067 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8068 .how = SP4_MACH_CRED,
8069 .enforce.u.words = {
8070 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8071 1 << (OP_EXCHANGE_ID - 32) |
8072 1 << (OP_CREATE_SESSION - 32) |
8073 1 << (OP_DESTROY_SESSION - 32) |
8074 1 << (OP_DESTROY_CLIENTID - 32)
8077 [0] = 1 << (OP_CLOSE) |
8078 1 << (OP_OPEN_DOWNGRADE) |
8080 1 << (OP_DELEGRETURN) |
8082 [1] = 1 << (OP_SECINFO - 32) |
8083 1 << (OP_SECINFO_NO_NAME - 32) |
8084 1 << (OP_LAYOUTRETURN - 32) |
8085 1 << (OP_TEST_STATEID - 32) |
8086 1 << (OP_FREE_STATEID - 32) |
8087 1 << (OP_WRITE - 32)
8092 * Select the state protection mode for client `clp' given the server results
8093 * from exchange_id in `sp'.
8095 * Returns 0 on success, negative errno otherwise.
8097 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8098 struct nfs41_state_protection *sp)
8100 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8101 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8102 1 << (OP_EXCHANGE_ID - 32) |
8103 1 << (OP_CREATE_SESSION - 32) |
8104 1 << (OP_DESTROY_SESSION - 32) |
8105 1 << (OP_DESTROY_CLIENTID - 32)
8107 unsigned long flags = 0;
8111 if (sp->how == SP4_MACH_CRED) {
8112 /* Print state protect result */
8113 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8114 for (i = 0; i <= LAST_NFS4_OP; i++) {
8115 if (test_bit(i, sp->enforce.u.longs))
8116 dfprintk(MOUNT, " enforce op %d\n", i);
8117 if (test_bit(i, sp->allow.u.longs))
8118 dfprintk(MOUNT, " allow op %d\n", i);
8121 /* make sure nothing is on enforce list that isn't supported */
8122 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8123 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8124 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8131 * Minimal mode - state operations are allowed to use machine
8132 * credential. Note this already happens by default, so the
8133 * client doesn't have to do anything more than the negotiation.
8135 * NOTE: we don't care if EXCHANGE_ID is in the list -
8136 * we're already using the machine cred for exchange_id
8137 * and will never use a different cred.
8139 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8140 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8141 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8142 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8143 dfprintk(MOUNT, "sp4_mach_cred:\n");
8144 dfprintk(MOUNT, " minimal mode enabled\n");
8145 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8147 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8152 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8153 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8154 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8155 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8156 dfprintk(MOUNT, " cleanup mode enabled\n");
8157 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8160 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8161 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8162 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8165 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8166 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8167 dfprintk(MOUNT, " secinfo mode enabled\n");
8168 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8171 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8172 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8173 dfprintk(MOUNT, " stateid mode enabled\n");
8174 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8177 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8178 dfprintk(MOUNT, " write mode enabled\n");
8179 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8182 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8183 dfprintk(MOUNT, " commit mode enabled\n");
8184 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8188 clp->cl_sp4_flags = flags;
8192 struct nfs41_exchange_id_data {
8193 struct nfs41_exchange_id_res res;
8194 struct nfs41_exchange_id_args args;
8197 static void nfs4_exchange_id_release(void *data)
8199 struct nfs41_exchange_id_data *cdata =
8200 (struct nfs41_exchange_id_data *)data;
8202 nfs_put_client(cdata->args.client);
8203 kfree(cdata->res.impl_id);
8204 kfree(cdata->res.server_scope);
8205 kfree(cdata->res.server_owner);
8209 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8210 .rpc_release = nfs4_exchange_id_release,
8214 * _nfs4_proc_exchange_id()
8216 * Wrapper for EXCHANGE_ID operation.
8218 static struct rpc_task *
8219 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8220 u32 sp4_how, struct rpc_xprt *xprt)
8222 struct rpc_message msg = {
8223 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8226 struct rpc_task_setup task_setup_data = {
8227 .rpc_client = clp->cl_rpcclient,
8228 .callback_ops = &nfs4_exchange_id_call_ops,
8229 .rpc_message = &msg,
8230 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8232 struct nfs41_exchange_id_data *calldata;
8235 if (!refcount_inc_not_zero(&clp->cl_count))
8236 return ERR_PTR(-EIO);
8239 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8243 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8245 status = nfs4_init_uniform_client_string(clp);
8249 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8252 if (unlikely(calldata->res.server_owner == NULL))
8255 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8257 if (unlikely(calldata->res.server_scope == NULL))
8258 goto out_server_owner;
8260 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8261 if (unlikely(calldata->res.impl_id == NULL))
8262 goto out_server_scope;
8266 calldata->args.state_protect.how = SP4_NONE;
8270 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8280 task_setup_data.rpc_xprt = xprt;
8281 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8282 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8283 sizeof(calldata->args.verifier.data));
8285 calldata->args.client = clp;
8286 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8287 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8288 #ifdef CONFIG_NFS_V4_1_MIGRATION
8289 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8291 msg.rpc_argp = &calldata->args;
8292 msg.rpc_resp = &calldata->res;
8293 task_setup_data.callback_data = calldata;
8295 return rpc_run_task(&task_setup_data);
8298 kfree(calldata->res.impl_id);
8300 kfree(calldata->res.server_scope);
8302 kfree(calldata->res.server_owner);
8306 nfs_put_client(clp);
8307 return ERR_PTR(status);
8311 * _nfs4_proc_exchange_id()
8313 * Wrapper for EXCHANGE_ID operation.
8315 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8318 struct rpc_task *task;
8319 struct nfs41_exchange_id_args *argp;
8320 struct nfs41_exchange_id_res *resp;
8321 unsigned long now = jiffies;
8324 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8326 return PTR_ERR(task);
8328 argp = task->tk_msg.rpc_argp;
8329 resp = task->tk_msg.rpc_resp;
8330 status = task->tk_status;
8334 status = nfs4_check_cl_exchange_flags(resp->flags,
8335 clp->cl_mvops->minor_version);
8339 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8343 do_renew_lease(clp, now);
8345 clp->cl_clientid = resp->clientid;
8346 clp->cl_exchange_flags = resp->flags;
8347 clp->cl_seqid = resp->seqid;
8348 /* Client ID is not confirmed */
8349 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8350 clear_bit(NFS4_SESSION_ESTABLISHED,
8351 &clp->cl_session->session_state);
8353 if (clp->cl_serverscope != NULL &&
8354 !nfs41_same_server_scope(clp->cl_serverscope,
8355 resp->server_scope)) {
8356 dprintk("%s: server_scope mismatch detected\n",
8358 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8361 swap(clp->cl_serverowner, resp->server_owner);
8362 swap(clp->cl_serverscope, resp->server_scope);
8363 swap(clp->cl_implid, resp->impl_id);
8365 /* Save the EXCHANGE_ID verifier session trunk tests */
8366 memcpy(clp->cl_confirm.data, argp->verifier.data,
8367 sizeof(clp->cl_confirm.data));
8369 trace_nfs4_exchange_id(clp, status);
8375 * nfs4_proc_exchange_id()
8377 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8379 * Since the clientid has expired, all compounds using sessions
8380 * associated with the stale clientid will be returning
8381 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8382 * be in some phase of session reset.
8384 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8386 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8388 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8391 /* try SP4_MACH_CRED if krb5i/p */
8392 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8393 authflavor == RPC_AUTH_GSS_KRB5P) {
8394 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8400 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8404 * nfs4_test_session_trunk
8406 * This is an add_xprt_test() test function called from
8407 * rpc_clnt_setup_test_and_add_xprt.
8409 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8410 * and is dereferrenced in nfs4_exchange_id_release
8412 * Upon success, add the new transport to the rpc_clnt
8414 * @clnt: struct rpc_clnt to get new transport
8415 * @xprt: the rpc_xprt to test
8416 * @data: call data for _nfs4_proc_exchange_id.
8418 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8421 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8422 struct rpc_task *task;
8427 dprintk("--> %s try %s\n", __func__,
8428 xprt->address_strings[RPC_DISPLAY_ADDR]);
8430 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8432 /* Test connection for session trunking. Async exchange_id call */
8433 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8437 status = task->tk_status;
8439 status = nfs4_detect_session_trunking(adata->clp,
8440 task->tk_msg.rpc_resp, xprt);
8443 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8447 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8449 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8450 const struct cred *cred)
8452 struct rpc_message msg = {
8453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8459 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8460 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8461 trace_nfs4_destroy_clientid(clp, status);
8463 dprintk("NFS: Got error %d from the server %s on "
8464 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8468 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8469 const struct cred *cred)
8474 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8475 ret = _nfs4_proc_destroy_clientid(clp, cred);
8477 case -NFS4ERR_DELAY:
8478 case -NFS4ERR_CLIENTID_BUSY:
8488 int nfs4_destroy_clientid(struct nfs_client *clp)
8490 const struct cred *cred;
8493 if (clp->cl_mvops->minor_version < 1)
8495 if (clp->cl_exchange_flags == 0)
8497 if (clp->cl_preserve_clid)
8499 cred = nfs4_get_clid_cred(clp);
8500 ret = nfs4_proc_destroy_clientid(clp, cred);
8504 case -NFS4ERR_STALE_CLIENTID:
8505 clp->cl_exchange_flags = 0;
8511 #endif /* CONFIG_NFS_V4_1 */
8513 struct nfs4_get_lease_time_data {
8514 struct nfs4_get_lease_time_args *args;
8515 struct nfs4_get_lease_time_res *res;
8516 struct nfs_client *clp;
8519 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8522 struct nfs4_get_lease_time_data *data =
8523 (struct nfs4_get_lease_time_data *)calldata;
8525 dprintk("--> %s\n", __func__);
8526 /* just setup sequence, do not trigger session recovery
8527 since we're invoked within one */
8528 nfs4_setup_sequence(data->clp,
8529 &data->args->la_seq_args,
8530 &data->res->lr_seq_res,
8532 dprintk("<-- %s\n", __func__);
8536 * Called from nfs4_state_manager thread for session setup, so don't recover
8537 * from sequence operation or clientid errors.
8539 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8541 struct nfs4_get_lease_time_data *data =
8542 (struct nfs4_get_lease_time_data *)calldata;
8544 dprintk("--> %s\n", __func__);
8545 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8547 switch (task->tk_status) {
8548 case -NFS4ERR_DELAY:
8549 case -NFS4ERR_GRACE:
8550 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8551 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8552 task->tk_status = 0;
8554 case -NFS4ERR_RETRY_UNCACHED_REP:
8555 rpc_restart_call_prepare(task);
8558 dprintk("<-- %s\n", __func__);
8561 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8562 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8563 .rpc_call_done = nfs4_get_lease_time_done,
8566 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8568 struct nfs4_get_lease_time_args args;
8569 struct nfs4_get_lease_time_res res = {
8570 .lr_fsinfo = fsinfo,
8572 struct nfs4_get_lease_time_data data = {
8577 struct rpc_message msg = {
8578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8582 struct rpc_task_setup task_setup = {
8583 .rpc_client = clp->cl_rpcclient,
8584 .rpc_message = &msg,
8585 .callback_ops = &nfs4_get_lease_time_ops,
8586 .callback_data = &data,
8587 .flags = RPC_TASK_TIMEOUT,
8590 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8591 return nfs4_call_sync_custom(&task_setup);
8594 #ifdef CONFIG_NFS_V4_1
8597 * Initialize the values to be used by the client in CREATE_SESSION
8598 * If nfs4_init_session set the fore channel request and response sizes,
8601 * Set the back channel max_resp_sz_cached to zero to force the client to
8602 * always set csa_cachethis to FALSE because the current implementation
8603 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8605 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8606 struct rpc_clnt *clnt)
8608 unsigned int max_rqst_sz, max_resp_sz;
8609 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8610 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8612 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8613 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8615 /* Fore channel attributes */
8616 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8617 args->fc_attrs.max_resp_sz = max_resp_sz;
8618 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8619 args->fc_attrs.max_reqs = max_session_slots;
8621 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8622 "max_ops=%u max_reqs=%u\n",
8624 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8625 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8627 /* Back channel attributes */
8628 args->bc_attrs.max_rqst_sz = max_bc_payload;
8629 args->bc_attrs.max_resp_sz = max_bc_payload;
8630 args->bc_attrs.max_resp_sz_cached = 0;
8631 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8632 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8633 if (args->bc_attrs.max_reqs > max_bc_slots)
8634 args->bc_attrs.max_reqs = max_bc_slots;
8636 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8637 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8639 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8640 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8641 args->bc_attrs.max_reqs);
8644 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8645 struct nfs41_create_session_res *res)
8647 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8648 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8650 if (rcvd->max_resp_sz > sent->max_resp_sz)
8653 * Our requested max_ops is the minimum we need; we're not
8654 * prepared to break up compounds into smaller pieces than that.
8655 * So, no point even trying to continue if the server won't
8658 if (rcvd->max_ops < sent->max_ops)
8660 if (rcvd->max_reqs == 0)
8662 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8663 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8667 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8668 struct nfs41_create_session_res *res)
8670 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8671 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8673 if (!(res->flags & SESSION4_BACK_CHAN))
8675 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8677 if (rcvd->max_resp_sz < sent->max_resp_sz)
8679 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8681 if (rcvd->max_ops > sent->max_ops)
8683 if (rcvd->max_reqs > sent->max_reqs)
8689 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8690 struct nfs41_create_session_res *res)
8694 ret = nfs4_verify_fore_channel_attrs(args, res);
8697 return nfs4_verify_back_channel_attrs(args, res);
8700 static void nfs4_update_session(struct nfs4_session *session,
8701 struct nfs41_create_session_res *res)
8703 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8704 /* Mark client id and session as being confirmed */
8705 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8706 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8707 session->flags = res->flags;
8708 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8709 if (res->flags & SESSION4_BACK_CHAN)
8710 memcpy(&session->bc_attrs, &res->bc_attrs,
8711 sizeof(session->bc_attrs));
8714 static int _nfs4_proc_create_session(struct nfs_client *clp,
8715 const struct cred *cred)
8717 struct nfs4_session *session = clp->cl_session;
8718 struct nfs41_create_session_args args = {
8720 .clientid = clp->cl_clientid,
8721 .seqid = clp->cl_seqid,
8722 .cb_program = NFS4_CALLBACK,
8724 struct nfs41_create_session_res res;
8726 struct rpc_message msg = {
8727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8734 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8735 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8737 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8738 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8739 trace_nfs4_create_session(clp, status);
8742 case -NFS4ERR_STALE_CLIENTID:
8743 case -NFS4ERR_DELAY:
8752 /* Verify the session's negotiated channel_attrs values */
8753 status = nfs4_verify_channel_attrs(&args, &res);
8754 /* Increment the clientid slot sequence id */
8757 nfs4_update_session(session, &res);
8764 * Issues a CREATE_SESSION operation to the server.
8765 * It is the responsibility of the caller to verify the session is
8766 * expired before calling this routine.
8768 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8772 struct nfs4_session *session = clp->cl_session;
8774 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8776 status = _nfs4_proc_create_session(clp, cred);
8780 /* Init or reset the session slot tables */
8781 status = nfs4_setup_session_slot_tables(session);
8782 dprintk("slot table setup returned %d\n", status);
8786 ptr = (unsigned *)&session->sess_id.data[0];
8787 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8788 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8790 dprintk("<-- %s\n", __func__);
8795 * Issue the over-the-wire RPC DESTROY_SESSION.
8796 * The caller must serialize access to this routine.
8798 int nfs4_proc_destroy_session(struct nfs4_session *session,
8799 const struct cred *cred)
8801 struct rpc_message msg = {
8802 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8803 .rpc_argp = session,
8808 dprintk("--> nfs4_proc_destroy_session\n");
8810 /* session is still being setup */
8811 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8814 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8815 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8816 trace_nfs4_destroy_session(session->clp, status);
8819 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8820 "Session has been destroyed regardless...\n", status);
8822 dprintk("<-- nfs4_proc_destroy_session\n");
8827 * Renew the cl_session lease.
8829 struct nfs4_sequence_data {
8830 struct nfs_client *clp;
8831 struct nfs4_sequence_args args;
8832 struct nfs4_sequence_res res;
8835 static void nfs41_sequence_release(void *data)
8837 struct nfs4_sequence_data *calldata = data;
8838 struct nfs_client *clp = calldata->clp;
8840 if (refcount_read(&clp->cl_count) > 1)
8841 nfs4_schedule_state_renewal(clp);
8842 nfs_put_client(clp);
8846 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8848 switch(task->tk_status) {
8849 case -NFS4ERR_DELAY:
8850 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8853 nfs4_schedule_lease_recovery(clp);
8858 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8860 struct nfs4_sequence_data *calldata = data;
8861 struct nfs_client *clp = calldata->clp;
8863 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8866 trace_nfs4_sequence(clp, task->tk_status);
8867 if (task->tk_status < 0) {
8868 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8869 if (refcount_read(&clp->cl_count) == 1)
8872 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8873 rpc_restart_call_prepare(task);
8877 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8879 dprintk("<-- %s\n", __func__);
8882 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8884 struct nfs4_sequence_data *calldata = data;
8885 struct nfs_client *clp = calldata->clp;
8886 struct nfs4_sequence_args *args;
8887 struct nfs4_sequence_res *res;
8889 args = task->tk_msg.rpc_argp;
8890 res = task->tk_msg.rpc_resp;
8892 nfs4_setup_sequence(clp, args, res, task);
8895 static const struct rpc_call_ops nfs41_sequence_ops = {
8896 .rpc_call_done = nfs41_sequence_call_done,
8897 .rpc_call_prepare = nfs41_sequence_prepare,
8898 .rpc_release = nfs41_sequence_release,
8901 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8902 const struct cred *cred,
8903 struct nfs4_slot *slot,
8906 struct nfs4_sequence_data *calldata;
8907 struct rpc_message msg = {
8908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8911 struct rpc_task_setup task_setup_data = {
8912 .rpc_client = clp->cl_rpcclient,
8913 .rpc_message = &msg,
8914 .callback_ops = &nfs41_sequence_ops,
8915 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8917 struct rpc_task *ret;
8919 ret = ERR_PTR(-EIO);
8920 if (!refcount_inc_not_zero(&clp->cl_count))
8923 ret = ERR_PTR(-ENOMEM);
8924 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8925 if (calldata == NULL)
8927 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
8928 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8929 msg.rpc_argp = &calldata->args;
8930 msg.rpc_resp = &calldata->res;
8931 calldata->clp = clp;
8932 task_setup_data.callback_data = calldata;
8934 ret = rpc_run_task(&task_setup_data);
8939 nfs_put_client(clp);
8941 nfs41_release_slot(slot);
8945 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
8947 struct rpc_task *task;
8950 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8952 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8954 ret = PTR_ERR(task);
8956 rpc_put_task_async(task);
8957 dprintk("<-- %s status=%d\n", __func__, ret);
8961 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
8963 struct rpc_task *task;
8966 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8968 ret = PTR_ERR(task);
8971 ret = rpc_wait_for_completion_task(task);
8973 ret = task->tk_status;
8976 dprintk("<-- %s status=%d\n", __func__, ret);
8980 struct nfs4_reclaim_complete_data {
8981 struct nfs_client *clp;
8982 struct nfs41_reclaim_complete_args arg;
8983 struct nfs41_reclaim_complete_res res;
8986 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8988 struct nfs4_reclaim_complete_data *calldata = data;
8990 nfs4_setup_sequence(calldata->clp,
8991 &calldata->arg.seq_args,
8992 &calldata->res.seq_res,
8996 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8998 switch(task->tk_status) {
9000 wake_up_all(&clp->cl_lock_waitq);
9002 case -NFS4ERR_COMPLETE_ALREADY:
9003 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9005 case -NFS4ERR_DELAY:
9006 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9008 case -NFS4ERR_RETRY_UNCACHED_REP:
9010 dprintk("%s: failed to reclaim complete error %d for server %s, retrying\n",
9011 __func__, task->tk_status, clp->cl_hostname);
9013 case -NFS4ERR_BADSESSION:
9014 case -NFS4ERR_DEADSESSION:
9015 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9018 nfs4_schedule_lease_recovery(clp);
9023 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9025 struct nfs4_reclaim_complete_data *calldata = data;
9026 struct nfs_client *clp = calldata->clp;
9027 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9029 dprintk("--> %s\n", __func__);
9030 if (!nfs41_sequence_done(task, res))
9033 trace_nfs4_reclaim_complete(clp, task->tk_status);
9034 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9035 rpc_restart_call_prepare(task);
9038 dprintk("<-- %s\n", __func__);
9041 static void nfs4_free_reclaim_complete_data(void *data)
9043 struct nfs4_reclaim_complete_data *calldata = data;
9048 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9049 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9050 .rpc_call_done = nfs4_reclaim_complete_done,
9051 .rpc_release = nfs4_free_reclaim_complete_data,
9055 * Issue a global reclaim complete.
9057 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9058 const struct cred *cred)
9060 struct nfs4_reclaim_complete_data *calldata;
9061 struct rpc_message msg = {
9062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9065 struct rpc_task_setup task_setup_data = {
9066 .rpc_client = clp->cl_rpcclient,
9067 .rpc_message = &msg,
9068 .callback_ops = &nfs4_reclaim_complete_call_ops,
9069 .flags = RPC_TASK_NO_ROUND_ROBIN,
9071 int status = -ENOMEM;
9073 dprintk("--> %s\n", __func__);
9074 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9075 if (calldata == NULL)
9077 calldata->clp = clp;
9078 calldata->arg.one_fs = 0;
9080 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9081 msg.rpc_argp = &calldata->arg;
9082 msg.rpc_resp = &calldata->res;
9083 task_setup_data.callback_data = calldata;
9084 status = nfs4_call_sync_custom(&task_setup_data);
9086 dprintk("<-- %s status=%d\n", __func__, status);
9091 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9093 struct nfs4_layoutget *lgp = calldata;
9094 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9096 dprintk("--> %s\n", __func__);
9097 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9098 &lgp->res.seq_res, task);
9099 dprintk("<-- %s\n", __func__);
9102 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9104 struct nfs4_layoutget *lgp = calldata;
9106 dprintk("--> %s\n", __func__);
9107 nfs41_sequence_process(task, &lgp->res.seq_res);
9108 dprintk("<-- %s\n", __func__);
9112 nfs4_layoutget_handle_exception(struct rpc_task *task,
9113 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9115 struct inode *inode = lgp->args.inode;
9116 struct nfs_server *server = NFS_SERVER(inode);
9117 struct pnfs_layout_hdr *lo;
9118 int nfs4err = task->tk_status;
9119 int err, status = 0;
9122 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9124 nfs4_sequence_free_slot(&lgp->res.seq_res);
9131 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9132 * on the file. set tk_status to -ENODATA to tell upper layer to
9135 case -NFS4ERR_LAYOUTUNAVAILABLE:
9139 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9140 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9142 case -NFS4ERR_BADLAYOUT:
9143 status = -EOVERFLOW;
9146 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9147 * (or clients) writing to the same RAID stripe except when
9148 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9150 * Treat it like we would RECALLCONFLICT -- we retry for a little
9151 * while, and then eventually give up.
9153 case -NFS4ERR_LAYOUTTRYLATER:
9154 if (lgp->args.minlength == 0) {
9155 status = -EOVERFLOW;
9160 case -NFS4ERR_RECALLCONFLICT:
9161 status = -ERECALLCONFLICT;
9163 case -NFS4ERR_DELEG_REVOKED:
9164 case -NFS4ERR_ADMIN_REVOKED:
9165 case -NFS4ERR_EXPIRED:
9166 case -NFS4ERR_BAD_STATEID:
9167 exception->timeout = 0;
9168 spin_lock(&inode->i_lock);
9169 lo = NFS_I(inode)->layout;
9170 /* If the open stateid was bad, then recover it. */
9171 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9172 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9173 spin_unlock(&inode->i_lock);
9174 exception->state = lgp->args.ctx->state;
9175 exception->stateid = &lgp->args.stateid;
9180 * Mark the bad layout state as invalid, then retry
9182 pnfs_mark_layout_stateid_invalid(lo, &head);
9183 spin_unlock(&inode->i_lock);
9184 nfs_commit_inode(inode, 0);
9185 pnfs_free_lseg_list(&head);
9190 err = nfs4_handle_exception(server, nfs4err, exception);
9192 if (exception->retry)
9198 dprintk("<-- %s\n", __func__);
9202 size_t max_response_pages(struct nfs_server *server)
9204 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9205 return nfs_page_array_len(0, max_resp_sz);
9208 static void nfs4_layoutget_release(void *calldata)
9210 struct nfs4_layoutget *lgp = calldata;
9212 dprintk("--> %s\n", __func__);
9213 nfs4_sequence_free_slot(&lgp->res.seq_res);
9214 pnfs_layoutget_free(lgp);
9215 dprintk("<-- %s\n", __func__);
9218 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9219 .rpc_call_prepare = nfs4_layoutget_prepare,
9220 .rpc_call_done = nfs4_layoutget_done,
9221 .rpc_release = nfs4_layoutget_release,
9224 struct pnfs_layout_segment *
9225 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9227 struct inode *inode = lgp->args.inode;
9228 struct nfs_server *server = NFS_SERVER(inode);
9229 struct rpc_task *task;
9230 struct rpc_message msg = {
9231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9232 .rpc_argp = &lgp->args,
9233 .rpc_resp = &lgp->res,
9234 .rpc_cred = lgp->cred,
9236 struct rpc_task_setup task_setup_data = {
9237 .rpc_client = server->client,
9238 .rpc_message = &msg,
9239 .callback_ops = &nfs4_layoutget_call_ops,
9240 .callback_data = lgp,
9241 .flags = RPC_TASK_ASYNC,
9243 struct pnfs_layout_segment *lseg = NULL;
9244 struct nfs4_exception exception = {
9246 .timeout = *timeout,
9250 dprintk("--> %s\n", __func__);
9252 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9253 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9255 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9257 task = rpc_run_task(&task_setup_data);
9259 return ERR_CAST(task);
9260 status = rpc_wait_for_completion_task(task);
9264 if (task->tk_status < 0) {
9265 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9266 *timeout = exception.timeout;
9267 } else if (lgp->res.layoutp->len == 0) {
9269 *timeout = nfs4_update_delay(&exception.timeout);
9271 lseg = pnfs_layout_process(lgp);
9273 trace_nfs4_layoutget(lgp->args.ctx,
9280 dprintk("<-- %s status=%d\n", __func__, status);
9282 return ERR_PTR(status);
9287 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9289 struct nfs4_layoutreturn *lrp = calldata;
9291 dprintk("--> %s\n", __func__);
9292 nfs4_setup_sequence(lrp->clp,
9293 &lrp->args.seq_args,
9296 if (!pnfs_layout_is_valid(lrp->args.layout))
9300 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9302 struct nfs4_layoutreturn *lrp = calldata;
9303 struct nfs_server *server;
9305 dprintk("--> %s\n", __func__);
9307 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9311 * Was there an RPC level error? Assume the call succeeded,
9312 * and that we need to release the layout
9314 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9315 lrp->res.lrs_present = 0;
9319 server = NFS_SERVER(lrp->args.inode);
9320 switch (task->tk_status) {
9321 case -NFS4ERR_OLD_STATEID:
9322 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9328 task->tk_status = 0;
9332 case -NFS4ERR_DELAY:
9333 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9337 dprintk("<-- %s\n", __func__);
9340 task->tk_status = 0;
9341 nfs4_sequence_free_slot(&lrp->res.seq_res);
9342 rpc_restart_call_prepare(task);
9345 static void nfs4_layoutreturn_release(void *calldata)
9347 struct nfs4_layoutreturn *lrp = calldata;
9348 struct pnfs_layout_hdr *lo = lrp->args.layout;
9350 dprintk("--> %s\n", __func__);
9351 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9352 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9353 nfs4_sequence_free_slot(&lrp->res.seq_res);
9354 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9355 lrp->ld_private.ops->free(&lrp->ld_private);
9356 pnfs_put_layout_hdr(lrp->args.layout);
9357 nfs_iput_and_deactive(lrp->inode);
9359 dprintk("<-- %s\n", __func__);
9362 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9363 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9364 .rpc_call_done = nfs4_layoutreturn_done,
9365 .rpc_release = nfs4_layoutreturn_release,
9368 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9370 struct rpc_task *task;
9371 struct rpc_message msg = {
9372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9373 .rpc_argp = &lrp->args,
9374 .rpc_resp = &lrp->res,
9375 .rpc_cred = lrp->cred,
9377 struct rpc_task_setup task_setup_data = {
9378 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9379 .rpc_message = &msg,
9380 .callback_ops = &nfs4_layoutreturn_call_ops,
9381 .callback_data = lrp,
9385 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9386 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9387 &task_setup_data.rpc_client, &msg);
9389 dprintk("--> %s\n", __func__);
9390 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9393 nfs4_layoutreturn_release(lrp);
9396 task_setup_data.flags |= RPC_TASK_ASYNC;
9399 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9402 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9404 task = rpc_run_task(&task_setup_data);
9406 return PTR_ERR(task);
9408 status = task->tk_status;
9409 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9410 dprintk("<-- %s status=%d\n", __func__, status);
9416 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9417 struct pnfs_device *pdev,
9418 const struct cred *cred)
9420 struct nfs4_getdeviceinfo_args args = {
9422 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9423 NOTIFY_DEVICEID4_DELETE,
9425 struct nfs4_getdeviceinfo_res res = {
9428 struct rpc_message msg = {
9429 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9436 dprintk("--> %s\n", __func__);
9437 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9438 if (res.notification & ~args.notify_types)
9439 dprintk("%s: unsupported notification\n", __func__);
9440 if (res.notification != args.notify_types)
9443 dprintk("<-- %s status=%d\n", __func__, status);
9448 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9449 struct pnfs_device *pdev,
9450 const struct cred *cred)
9452 struct nfs4_exception exception = { };
9456 err = nfs4_handle_exception(server,
9457 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9459 } while (exception.retry);
9462 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9464 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9466 struct nfs4_layoutcommit_data *data = calldata;
9467 struct nfs_server *server = NFS_SERVER(data->args.inode);
9469 nfs4_setup_sequence(server->nfs_client,
9470 &data->args.seq_args,
9476 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9478 struct nfs4_layoutcommit_data *data = calldata;
9479 struct nfs_server *server = NFS_SERVER(data->args.inode);
9481 if (!nfs41_sequence_done(task, &data->res.seq_res))
9484 switch (task->tk_status) { /* Just ignore these failures */
9485 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9486 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9487 case -NFS4ERR_BADLAYOUT: /* no layout */
9488 case -NFS4ERR_GRACE: /* loca_recalim always false */
9489 task->tk_status = 0;
9493 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9494 rpc_restart_call_prepare(task);
9500 static void nfs4_layoutcommit_release(void *calldata)
9502 struct nfs4_layoutcommit_data *data = calldata;
9504 pnfs_cleanup_layoutcommit(data);
9505 nfs_post_op_update_inode_force_wcc(data->args.inode,
9507 put_cred(data->cred);
9508 nfs_iput_and_deactive(data->inode);
9512 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9513 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9514 .rpc_call_done = nfs4_layoutcommit_done,
9515 .rpc_release = nfs4_layoutcommit_release,
9519 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9521 struct rpc_message msg = {
9522 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9523 .rpc_argp = &data->args,
9524 .rpc_resp = &data->res,
9525 .rpc_cred = data->cred,
9527 struct rpc_task_setup task_setup_data = {
9528 .task = &data->task,
9529 .rpc_client = NFS_CLIENT(data->args.inode),
9530 .rpc_message = &msg,
9531 .callback_ops = &nfs4_layoutcommit_ops,
9532 .callback_data = data,
9534 struct rpc_task *task;
9537 dprintk("NFS: initiating layoutcommit call. sync %d "
9538 "lbw: %llu inode %lu\n", sync,
9539 data->args.lastbytewritten,
9540 data->args.inode->i_ino);
9543 data->inode = nfs_igrab_and_active(data->args.inode);
9544 if (data->inode == NULL) {
9545 nfs4_layoutcommit_release(data);
9548 task_setup_data.flags = RPC_TASK_ASYNC;
9550 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9551 task = rpc_run_task(&task_setup_data);
9553 return PTR_ERR(task);
9555 status = task->tk_status;
9556 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9557 dprintk("%s: status %d\n", __func__, status);
9563 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9564 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9567 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9568 struct nfs_fsinfo *info,
9569 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9571 struct nfs41_secinfo_no_name_args args = {
9572 .style = SECINFO_STYLE_CURRENT_FH,
9574 struct nfs4_secinfo_res res = {
9577 struct rpc_message msg = {
9578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9582 struct rpc_clnt *clnt = server->client;
9583 struct nfs4_call_sync_data data = {
9584 .seq_server = server,
9585 .seq_args = &args.seq_args,
9586 .seq_res = &res.seq_res,
9588 struct rpc_task_setup task_setup = {
9589 .rpc_client = server->client,
9590 .rpc_message = &msg,
9591 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9592 .callback_data = &data,
9593 .flags = RPC_TASK_NO_ROUND_ROBIN,
9595 const struct cred *cred = NULL;
9598 if (use_integrity) {
9599 clnt = server->nfs_client->cl_rpcclient;
9600 task_setup.rpc_client = clnt;
9602 cred = nfs4_get_clid_cred(server->nfs_client);
9603 msg.rpc_cred = cred;
9606 dprintk("--> %s\n", __func__);
9607 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9608 status = nfs4_call_sync_custom(&task_setup);
9609 dprintk("<-- %s status=%d\n", __func__, status);
9617 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9618 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9620 struct nfs4_exception exception = {
9621 .interruptible = true,
9625 /* first try using integrity protection */
9626 err = -NFS4ERR_WRONGSEC;
9628 /* try to use integrity protection with machine cred */
9629 if (_nfs4_is_integrity_protected(server->nfs_client))
9630 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9634 * if unable to use integrity protection, or SECINFO with
9635 * integrity protection returns NFS4ERR_WRONGSEC (which is
9636 * disallowed by spec, but exists in deployed servers) use
9637 * the current filesystem's rpc_client and the user cred.
9639 if (err == -NFS4ERR_WRONGSEC)
9640 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9645 case -NFS4ERR_WRONGSEC:
9649 err = nfs4_handle_exception(server, err, &exception);
9651 } while (exception.retry);
9657 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9658 struct nfs_fsinfo *info)
9662 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9663 struct nfs4_secinfo_flavors *flavors;
9664 struct nfs4_secinfo4 *secinfo;
9667 page = alloc_page(GFP_KERNEL);
9673 flavors = page_address(page);
9674 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9677 * Fall back on "guess and check" method if
9678 * the server doesn't support SECINFO_NO_NAME
9680 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9681 err = nfs4_find_root_sec(server, fhandle, info);
9687 for (i = 0; i < flavors->num_flavors; i++) {
9688 secinfo = &flavors->flavors[i];
9690 switch (secinfo->flavor) {
9694 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9695 &secinfo->flavor_info);
9698 flavor = RPC_AUTH_MAXFLAVOR;
9702 if (!nfs_auth_info_match(&server->auth_info, flavor))
9703 flavor = RPC_AUTH_MAXFLAVOR;
9705 if (flavor != RPC_AUTH_MAXFLAVOR) {
9706 err = nfs4_lookup_root_sec(server, fhandle,
9713 if (flavor == RPC_AUTH_MAXFLAVOR)
9724 static int _nfs41_test_stateid(struct nfs_server *server,
9725 nfs4_stateid *stateid,
9726 const struct cred *cred)
9729 struct nfs41_test_stateid_args args = {
9732 struct nfs41_test_stateid_res res;
9733 struct rpc_message msg = {
9734 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9739 struct rpc_clnt *rpc_client = server->client;
9741 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9744 dprintk("NFS call test_stateid %p\n", stateid);
9745 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9746 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9747 &args.seq_args, &res.seq_res);
9748 if (status != NFS_OK) {
9749 dprintk("NFS reply test_stateid: failed, %d\n", status);
9752 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9756 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9757 int err, struct nfs4_exception *exception)
9759 exception->retry = 0;
9761 case -NFS4ERR_DELAY:
9762 case -NFS4ERR_RETRY_UNCACHED_REP:
9763 nfs4_handle_exception(server, err, exception);
9765 case -NFS4ERR_BADSESSION:
9766 case -NFS4ERR_BADSLOT:
9767 case -NFS4ERR_BAD_HIGH_SLOT:
9768 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9769 case -NFS4ERR_DEADSESSION:
9770 nfs4_do_handle_exception(server, err, exception);
9775 * nfs41_test_stateid - perform a TEST_STATEID operation
9777 * @server: server / transport on which to perform the operation
9778 * @stateid: state ID to test
9781 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9782 * Otherwise a negative NFS4ERR value is returned if the operation
9783 * failed or the state ID is not currently valid.
9785 static int nfs41_test_stateid(struct nfs_server *server,
9786 nfs4_stateid *stateid,
9787 const struct cred *cred)
9789 struct nfs4_exception exception = {
9790 .interruptible = true,
9794 err = _nfs41_test_stateid(server, stateid, cred);
9795 nfs4_handle_delay_or_session_error(server, err, &exception);
9796 } while (exception.retry);
9800 struct nfs_free_stateid_data {
9801 struct nfs_server *server;
9802 struct nfs41_free_stateid_args args;
9803 struct nfs41_free_stateid_res res;
9806 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9808 struct nfs_free_stateid_data *data = calldata;
9809 nfs4_setup_sequence(data->server->nfs_client,
9810 &data->args.seq_args,
9815 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9817 struct nfs_free_stateid_data *data = calldata;
9819 nfs41_sequence_done(task, &data->res.seq_res);
9821 switch (task->tk_status) {
9822 case -NFS4ERR_DELAY:
9823 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9824 rpc_restart_call_prepare(task);
9828 static void nfs41_free_stateid_release(void *calldata)
9833 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9834 .rpc_call_prepare = nfs41_free_stateid_prepare,
9835 .rpc_call_done = nfs41_free_stateid_done,
9836 .rpc_release = nfs41_free_stateid_release,
9840 * nfs41_free_stateid - perform a FREE_STATEID operation
9842 * @server: server / transport on which to perform the operation
9843 * @stateid: state ID to release
9845 * @privileged: set to true if this call needs to be privileged
9847 * Note: this function is always asynchronous.
9849 static int nfs41_free_stateid(struct nfs_server *server,
9850 const nfs4_stateid *stateid,
9851 const struct cred *cred,
9854 struct rpc_message msg = {
9855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9858 struct rpc_task_setup task_setup = {
9859 .rpc_client = server->client,
9860 .rpc_message = &msg,
9861 .callback_ops = &nfs41_free_stateid_ops,
9862 .flags = RPC_TASK_ASYNC,
9864 struct nfs_free_stateid_data *data;
9865 struct rpc_task *task;
9867 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9868 &task_setup.rpc_client, &msg);
9870 dprintk("NFS call free_stateid %p\n", stateid);
9871 data = kmalloc(sizeof(*data), GFP_NOFS);
9874 data->server = server;
9875 nfs4_stateid_copy(&data->args.stateid, stateid);
9877 task_setup.callback_data = data;
9879 msg.rpc_argp = &data->args;
9880 msg.rpc_resp = &data->res;
9881 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
9882 task = rpc_run_task(&task_setup);
9884 return PTR_ERR(task);
9890 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9892 const struct cred *cred = lsp->ls_state->owner->so_cred;
9894 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9895 nfs4_free_lock_state(server, lsp);
9898 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9899 const nfs4_stateid *s2)
9901 if (s1->type != s2->type)
9904 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9907 if (s1->seqid == s2->seqid)
9910 return s1->seqid == 0 || s2->seqid == 0;
9913 #endif /* CONFIG_NFS_V4_1 */
9915 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9916 const nfs4_stateid *s2)
9918 return nfs4_stateid_match(s1, s2);
9922 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9923 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9924 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9925 .recover_open = nfs4_open_reclaim,
9926 .recover_lock = nfs4_lock_reclaim,
9927 .establish_clid = nfs4_init_clientid,
9928 .detect_trunking = nfs40_discover_server_trunking,
9931 #if defined(CONFIG_NFS_V4_1)
9932 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9933 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9934 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9935 .recover_open = nfs4_open_reclaim,
9936 .recover_lock = nfs4_lock_reclaim,
9937 .establish_clid = nfs41_init_clientid,
9938 .reclaim_complete = nfs41_proc_reclaim_complete,
9939 .detect_trunking = nfs41_discover_server_trunking,
9941 #endif /* CONFIG_NFS_V4_1 */
9943 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9944 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9945 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9946 .recover_open = nfs40_open_expired,
9947 .recover_lock = nfs4_lock_expired,
9948 .establish_clid = nfs4_init_clientid,
9951 #if defined(CONFIG_NFS_V4_1)
9952 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9953 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9954 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9955 .recover_open = nfs41_open_expired,
9956 .recover_lock = nfs41_lock_expired,
9957 .establish_clid = nfs41_init_clientid,
9959 #endif /* CONFIG_NFS_V4_1 */
9961 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9962 .sched_state_renewal = nfs4_proc_async_renew,
9963 .get_state_renewal_cred = nfs4_get_renew_cred,
9964 .renew_lease = nfs4_proc_renew,
9967 #if defined(CONFIG_NFS_V4_1)
9968 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9969 .sched_state_renewal = nfs41_proc_async_sequence,
9970 .get_state_renewal_cred = nfs4_get_machine_cred,
9971 .renew_lease = nfs4_proc_sequence,
9975 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9976 .get_locations = _nfs40_proc_get_locations,
9977 .fsid_present = _nfs40_proc_fsid_present,
9980 #if defined(CONFIG_NFS_V4_1)
9981 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9982 .get_locations = _nfs41_proc_get_locations,
9983 .fsid_present = _nfs41_proc_fsid_present,
9985 #endif /* CONFIG_NFS_V4_1 */
9987 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9989 .init_caps = NFS_CAP_READDIRPLUS
9990 | NFS_CAP_ATOMIC_OPEN
9991 | NFS_CAP_POSIX_LOCK,
9992 .init_client = nfs40_init_client,
9993 .shutdown_client = nfs40_shutdown_client,
9994 .match_stateid = nfs4_match_stateid,
9995 .find_root_sec = nfs4_find_root_sec,
9996 .free_lock_state = nfs4_release_lockowner,
9997 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9998 .alloc_seqid = nfs_alloc_seqid,
9999 .call_sync_ops = &nfs40_call_sync_ops,
10000 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10001 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10002 .state_renewal_ops = &nfs40_state_renewal_ops,
10003 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10006 #if defined(CONFIG_NFS_V4_1)
10007 static struct nfs_seqid *
10008 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10013 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10014 .minor_version = 1,
10015 .init_caps = NFS_CAP_READDIRPLUS
10016 | NFS_CAP_ATOMIC_OPEN
10017 | NFS_CAP_POSIX_LOCK
10018 | NFS_CAP_STATEID_NFSV41
10019 | NFS_CAP_ATOMIC_OPEN_V1
10021 .init_client = nfs41_init_client,
10022 .shutdown_client = nfs41_shutdown_client,
10023 .match_stateid = nfs41_match_stateid,
10024 .find_root_sec = nfs41_find_root_sec,
10025 .free_lock_state = nfs41_free_lock_state,
10026 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10027 .alloc_seqid = nfs_alloc_no_seqid,
10028 .session_trunk = nfs4_test_session_trunk,
10029 .call_sync_ops = &nfs41_call_sync_ops,
10030 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10031 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10032 .state_renewal_ops = &nfs41_state_renewal_ops,
10033 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10037 #if defined(CONFIG_NFS_V4_2)
10038 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10039 .minor_version = 2,
10040 .init_caps = NFS_CAP_READDIRPLUS
10041 | NFS_CAP_ATOMIC_OPEN
10042 | NFS_CAP_POSIX_LOCK
10043 | NFS_CAP_STATEID_NFSV41
10044 | NFS_CAP_ATOMIC_OPEN_V1
10048 | NFS_CAP_OFFLOAD_CANCEL
10049 | NFS_CAP_DEALLOCATE
10051 | NFS_CAP_LAYOUTSTATS
10053 | NFS_CAP_LAYOUTERROR,
10054 .init_client = nfs41_init_client,
10055 .shutdown_client = nfs41_shutdown_client,
10056 .match_stateid = nfs41_match_stateid,
10057 .find_root_sec = nfs41_find_root_sec,
10058 .free_lock_state = nfs41_free_lock_state,
10059 .call_sync_ops = &nfs41_call_sync_ops,
10060 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10061 .alloc_seqid = nfs_alloc_no_seqid,
10062 .session_trunk = nfs4_test_session_trunk,
10063 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10064 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10065 .state_renewal_ops = &nfs41_state_renewal_ops,
10066 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10070 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10071 [0] = &nfs_v4_0_minor_ops,
10072 #if defined(CONFIG_NFS_V4_1)
10073 [1] = &nfs_v4_1_minor_ops,
10075 #if defined(CONFIG_NFS_V4_2)
10076 [2] = &nfs_v4_2_minor_ops,
10080 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10082 ssize_t error, error2;
10084 error = generic_listxattr(dentry, list, size);
10092 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10095 return error + error2;
10098 static const struct inode_operations nfs4_dir_inode_operations = {
10099 .create = nfs_create,
10100 .lookup = nfs_lookup,
10101 .atomic_open = nfs_atomic_open,
10103 .unlink = nfs_unlink,
10104 .symlink = nfs_symlink,
10105 .mkdir = nfs_mkdir,
10106 .rmdir = nfs_rmdir,
10107 .mknod = nfs_mknod,
10108 .rename = nfs_rename,
10109 .permission = nfs_permission,
10110 .getattr = nfs_getattr,
10111 .setattr = nfs_setattr,
10112 .listxattr = nfs4_listxattr,
10115 static const struct inode_operations nfs4_file_inode_operations = {
10116 .permission = nfs_permission,
10117 .getattr = nfs_getattr,
10118 .setattr = nfs_setattr,
10119 .listxattr = nfs4_listxattr,
10122 const struct nfs_rpc_ops nfs_v4_clientops = {
10123 .version = 4, /* protocol version */
10124 .dentry_ops = &nfs4_dentry_operations,
10125 .dir_inode_ops = &nfs4_dir_inode_operations,
10126 .file_inode_ops = &nfs4_file_inode_operations,
10127 .file_ops = &nfs4_file_operations,
10128 .getroot = nfs4_proc_get_root,
10129 .submount = nfs4_submount,
10130 .try_mount = nfs4_try_mount,
10131 .getattr = nfs4_proc_getattr,
10132 .setattr = nfs4_proc_setattr,
10133 .lookup = nfs4_proc_lookup,
10134 .lookupp = nfs4_proc_lookupp,
10135 .access = nfs4_proc_access,
10136 .readlink = nfs4_proc_readlink,
10137 .create = nfs4_proc_create,
10138 .remove = nfs4_proc_remove,
10139 .unlink_setup = nfs4_proc_unlink_setup,
10140 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10141 .unlink_done = nfs4_proc_unlink_done,
10142 .rename_setup = nfs4_proc_rename_setup,
10143 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10144 .rename_done = nfs4_proc_rename_done,
10145 .link = nfs4_proc_link,
10146 .symlink = nfs4_proc_symlink,
10147 .mkdir = nfs4_proc_mkdir,
10148 .rmdir = nfs4_proc_rmdir,
10149 .readdir = nfs4_proc_readdir,
10150 .mknod = nfs4_proc_mknod,
10151 .statfs = nfs4_proc_statfs,
10152 .fsinfo = nfs4_proc_fsinfo,
10153 .pathconf = nfs4_proc_pathconf,
10154 .set_capabilities = nfs4_server_capabilities,
10155 .decode_dirent = nfs4_decode_dirent,
10156 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10157 .read_setup = nfs4_proc_read_setup,
10158 .read_done = nfs4_read_done,
10159 .write_setup = nfs4_proc_write_setup,
10160 .write_done = nfs4_write_done,
10161 .commit_setup = nfs4_proc_commit_setup,
10162 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10163 .commit_done = nfs4_commit_done,
10164 .lock = nfs4_proc_lock,
10165 .clear_acl_cache = nfs4_zap_acl_attr,
10166 .close_context = nfs4_close_context,
10167 .open_context = nfs4_atomic_open,
10168 .have_delegation = nfs4_have_delegation,
10169 .alloc_client = nfs4_alloc_client,
10170 .init_client = nfs4_init_client,
10171 .free_client = nfs4_free_client,
10172 .create_server = nfs4_create_server,
10173 .clone_server = nfs_clone_server,
10176 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10177 .name = XATTR_NAME_NFSV4_ACL,
10178 .list = nfs4_xattr_list_nfs4_acl,
10179 .get = nfs4_xattr_get_nfs4_acl,
10180 .set = nfs4_xattr_set_nfs4_acl,
10183 const struct xattr_handler *nfs4_xattr_handlers[] = {
10184 &nfs4_xattr_nfs4_acl_handler,
10185 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10186 &nfs4_xattr_nfs4_label_handler,
10193 * c-basic-offset: 8