4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
57 #include <linux/iversion.h>
60 #include "delegation.h"
67 #include "nfs4idmap.h"
68 #include "nfs4session.h"
72 #include "nfs4trace.h"
74 #define NFSDBG_FACILITY NFSDBG_PROC
76 #define NFS4_BITMASK_SZ 3
78 #define NFS4_POLL_RETRY_MIN (HZ/10)
79 #define NFS4_POLL_RETRY_MAX (15*HZ)
81 /* file attributes which can be mapped to nfs attributes */
82 #define NFS4_VALID_ATTRS (ATTR_MODE \
93 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
94 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
95 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
96 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
97 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
98 struct nfs_fattr *fattr, struct iattr *sattr,
99 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
100 struct nfs4_label *olabel);
101 #ifdef CONFIG_NFS_V4_1
102 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
103 const struct cred *cred,
104 struct nfs4_slot *slot,
106 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
107 const struct cred *);
108 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
109 const struct cred *, bool);
111 static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ],
112 const __u32 *src, struct inode *inode,
113 struct nfs_server *server,
114 struct nfs4_label *label);
116 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
117 static inline struct nfs4_label *
118 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
119 struct iattr *sattr, struct nfs4_label *label)
126 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:
184 case -NFS4ERR_NOT_SAME:
187 dprintk("%s could not handle NFSv4 error %d\n",
195 * This is our standard bitmap for GETATTR requests.
197 const u32 nfs4_fattr_bitmap[3] = {
199 | FATTR4_WORD0_CHANGE
202 | FATTR4_WORD0_FILEID,
204 | FATTR4_WORD1_NUMLINKS
206 | FATTR4_WORD1_OWNER_GROUP
207 | FATTR4_WORD1_RAWDEV
208 | FATTR4_WORD1_SPACE_USED
209 | FATTR4_WORD1_TIME_ACCESS
210 | FATTR4_WORD1_TIME_METADATA
211 | FATTR4_WORD1_TIME_MODIFY
212 | FATTR4_WORD1_MOUNTED_ON_FILEID,
213 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
214 FATTR4_WORD2_SECURITY_LABEL
218 static const u32 nfs4_pnfs_open_bitmap[3] = {
220 | FATTR4_WORD0_CHANGE
223 | FATTR4_WORD0_FILEID,
225 | FATTR4_WORD1_NUMLINKS
227 | FATTR4_WORD1_OWNER_GROUP
228 | FATTR4_WORD1_RAWDEV
229 | FATTR4_WORD1_SPACE_USED
230 | FATTR4_WORD1_TIME_ACCESS
231 | FATTR4_WORD1_TIME_METADATA
232 | FATTR4_WORD1_TIME_MODIFY,
233 FATTR4_WORD2_MDSTHRESHOLD
234 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
235 | FATTR4_WORD2_SECURITY_LABEL
239 static const u32 nfs4_open_noattr_bitmap[3] = {
241 | FATTR4_WORD0_FILEID,
244 const u32 nfs4_statfs_bitmap[3] = {
245 FATTR4_WORD0_FILES_AVAIL
246 | FATTR4_WORD0_FILES_FREE
247 | FATTR4_WORD0_FILES_TOTAL,
248 FATTR4_WORD1_SPACE_AVAIL
249 | FATTR4_WORD1_SPACE_FREE
250 | FATTR4_WORD1_SPACE_TOTAL
253 const u32 nfs4_pathconf_bitmap[3] = {
255 | FATTR4_WORD0_MAXNAME,
259 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
260 | FATTR4_WORD0_MAXREAD
261 | FATTR4_WORD0_MAXWRITE
262 | FATTR4_WORD0_LEASE_TIME,
263 FATTR4_WORD1_TIME_DELTA
264 | FATTR4_WORD1_FS_LAYOUT_TYPES,
265 FATTR4_WORD2_LAYOUT_BLKSIZE
266 | FATTR4_WORD2_CLONE_BLKSIZE
267 | FATTR4_WORD2_CHANGE_ATTR_TYPE
268 | FATTR4_WORD2_XATTR_SUPPORT
271 const u32 nfs4_fs_locations_bitmap[3] = {
275 | FATTR4_WORD0_FILEID
276 | FATTR4_WORD0_FS_LOCATIONS,
278 | FATTR4_WORD1_OWNER_GROUP
279 | FATTR4_WORD1_RAWDEV
280 | FATTR4_WORD1_SPACE_USED
281 | FATTR4_WORD1_TIME_ACCESS
282 | FATTR4_WORD1_TIME_METADATA
283 | FATTR4_WORD1_TIME_MODIFY
284 | FATTR4_WORD1_MOUNTED_ON_FILEID,
287 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
288 struct inode *inode, unsigned long flags)
290 unsigned long cache_validity;
292 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
293 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
296 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags;
298 /* Remove the attributes over which we have full control */
299 dst[1] &= ~FATTR4_WORD1_RAWDEV;
300 if (!(cache_validity & NFS_INO_INVALID_SIZE))
301 dst[0] &= ~FATTR4_WORD0_SIZE;
303 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
304 dst[0] &= ~FATTR4_WORD0_CHANGE;
306 if (!(cache_validity & NFS_INO_INVALID_MODE))
307 dst[1] &= ~FATTR4_WORD1_MODE;
308 if (!(cache_validity & NFS_INO_INVALID_OTHER))
309 dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP);
312 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
313 struct nfs4_readdir_arg *readdir)
315 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
319 readdir->cookie = cookie;
320 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
325 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
330 * NFSv4 servers do not return entries for '.' and '..'
331 * Therefore, we fake these entries here. We let '.'
332 * have cookie 0 and '..' have cookie 1. Note that
333 * when talking to the server, we always send cookie 0
336 start = p = kmap_atomic(*readdir->pages);
339 *p++ = xdr_one; /* next */
340 *p++ = xdr_zero; /* cookie, first word */
341 *p++ = xdr_one; /* cookie, second word */
342 *p++ = xdr_one; /* entry len */
343 memcpy(p, ".\0\0\0", 4); /* entry */
345 *p++ = xdr_one; /* bitmap length */
346 *p++ = htonl(attrs); /* bitmap */
347 *p++ = htonl(12); /* attribute buffer length */
348 *p++ = htonl(NF4DIR);
349 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
352 *p++ = xdr_one; /* next */
353 *p++ = xdr_zero; /* cookie, first word */
354 *p++ = xdr_two; /* cookie, second word */
355 *p++ = xdr_two; /* entry len */
356 memcpy(p, "..\0\0", 4); /* entry */
358 *p++ = xdr_one; /* bitmap length */
359 *p++ = htonl(attrs); /* bitmap */
360 *p++ = htonl(12); /* attribute buffer length */
361 *p++ = htonl(NF4DIR);
362 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
364 readdir->pgbase = (char *)p - (char *)start;
365 readdir->count -= readdir->pgbase;
366 kunmap_atomic(start);
369 static void nfs4_test_and_free_stateid(struct nfs_server *server,
370 nfs4_stateid *stateid,
371 const struct cred *cred)
373 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
375 ops->test_and_free_expired(server, stateid, cred);
378 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
379 nfs4_stateid *stateid,
380 const struct cred *cred)
382 stateid->type = NFS4_REVOKED_STATEID_TYPE;
383 nfs4_test_and_free_stateid(server, stateid, cred);
386 static void nfs4_free_revoked_stateid(struct nfs_server *server,
387 const nfs4_stateid *stateid,
388 const struct cred *cred)
392 nfs4_stateid_copy(&tmp, stateid);
393 __nfs4_free_revoked_stateid(server, &tmp, cred);
396 static long nfs4_update_delay(long *timeout)
400 return NFS4_POLL_RETRY_MAX;
402 *timeout = NFS4_POLL_RETRY_MIN;
403 if (*timeout > NFS4_POLL_RETRY_MAX)
404 *timeout = NFS4_POLL_RETRY_MAX;
410 static int nfs4_delay_killable(long *timeout)
414 freezable_schedule_timeout_killable_unsafe(
415 nfs4_update_delay(timeout));
416 if (!__fatal_signal_pending(current))
421 static int nfs4_delay_interruptible(long *timeout)
425 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
426 if (!signal_pending(current))
428 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
431 static int nfs4_delay(long *timeout, bool interruptible)
434 return nfs4_delay_interruptible(timeout);
435 return nfs4_delay_killable(timeout);
438 static const nfs4_stateid *
439 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
443 switch (stateid->type) {
444 case NFS4_OPEN_STATEID_TYPE:
445 case NFS4_LOCK_STATEID_TYPE:
446 case NFS4_DELEGATION_STATEID_TYPE:
454 /* This is the error handling routine for processes that are allowed
457 static int nfs4_do_handle_exception(struct nfs_server *server,
458 int errorcode, struct nfs4_exception *exception)
460 struct nfs_client *clp = server->nfs_client;
461 struct nfs4_state *state = exception->state;
462 const nfs4_stateid *stateid;
463 struct inode *inode = exception->inode;
466 exception->delay = 0;
467 exception->recovering = 0;
468 exception->retry = 0;
470 stateid = nfs4_recoverable_stateid(exception->stateid);
471 if (stateid == NULL && state != NULL)
472 stateid = nfs4_recoverable_stateid(&state->stateid);
477 case -NFS4ERR_BADHANDLE:
479 if (inode != NULL && S_ISREG(inode->i_mode))
480 pnfs_destroy_layout(NFS_I(inode));
482 case -NFS4ERR_DELEG_REVOKED:
483 case -NFS4ERR_ADMIN_REVOKED:
484 case -NFS4ERR_EXPIRED:
485 case -NFS4ERR_BAD_STATEID:
486 case -NFS4ERR_PARTNER_NO_AUTH:
487 if (inode != NULL && stateid != NULL) {
488 nfs_inode_find_state_and_recover(inode,
490 goto wait_on_recovery;
493 case -NFS4ERR_OPENMODE:
497 err = nfs_async_inode_return_delegation(inode,
500 goto wait_on_recovery;
501 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
502 exception->retry = 1;
508 ret = nfs4_schedule_stateid_recovery(server, state);
511 goto wait_on_recovery;
512 case -NFS4ERR_STALE_STATEID:
513 case -NFS4ERR_STALE_CLIENTID:
514 nfs4_schedule_lease_recovery(clp);
515 goto wait_on_recovery;
517 ret = nfs4_schedule_migration_recovery(server);
520 goto wait_on_recovery;
521 case -NFS4ERR_LEASE_MOVED:
522 nfs4_schedule_lease_moved_recovery(clp);
523 goto wait_on_recovery;
524 #if defined(CONFIG_NFS_V4_1)
525 case -NFS4ERR_BADSESSION:
526 case -NFS4ERR_BADSLOT:
527 case -NFS4ERR_BAD_HIGH_SLOT:
528 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
529 case -NFS4ERR_DEADSESSION:
530 case -NFS4ERR_SEQ_FALSE_RETRY:
531 case -NFS4ERR_SEQ_MISORDERED:
532 /* Handled in nfs41_sequence_process() */
533 goto wait_on_recovery;
534 #endif /* defined(CONFIG_NFS_V4_1) */
535 case -NFS4ERR_FILE_OPEN:
536 if (exception->timeout > HZ) {
537 /* We have retried a decent amount, time to
545 nfs_inc_server_stats(server, NFSIOS_DELAY);
548 case -NFS4ERR_LAYOUTTRYLATER:
549 case -NFS4ERR_RECALLCONFLICT:
550 exception->delay = 1;
553 case -NFS4ERR_RETRY_UNCACHED_REP:
554 case -NFS4ERR_OLD_STATEID:
555 exception->retry = 1;
557 case -NFS4ERR_BADOWNER:
558 /* The following works around a Linux server bug! */
559 case -NFS4ERR_BADNAME:
560 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
561 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
562 exception->retry = 1;
563 printk(KERN_WARNING "NFS: v4 server %s "
564 "does not accept raw "
566 "Reenabling the idmapper.\n",
567 server->nfs_client->cl_hostname);
570 /* We failed to handle the error */
571 return nfs4_map_errors(ret);
573 exception->recovering = 1;
577 /* This is the error handling routine for processes that are allowed
580 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
582 struct nfs_client *clp = server->nfs_client;
585 ret = nfs4_do_handle_exception(server, errorcode, exception);
586 if (exception->delay) {
587 ret = nfs4_delay(&exception->timeout,
588 exception->interruptible);
591 if (exception->recovering) {
592 if (exception->task_is_privileged)
594 ret = nfs4_wait_clnt_recover(clp);
595 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
602 exception->retry = 1;
607 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
608 int errorcode, struct nfs4_exception *exception)
610 struct nfs_client *clp = server->nfs_client;
613 ret = nfs4_do_handle_exception(server, errorcode, exception);
614 if (exception->delay) {
615 rpc_delay(task, nfs4_update_delay(&exception->timeout));
618 if (exception->recovering) {
619 if (exception->task_is_privileged)
621 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
622 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
623 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
626 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
631 exception->retry = 1;
633 * For NFS4ERR_MOVED, the client transport will need to
634 * be recomputed after migration recovery has completed.
636 if (errorcode == -NFS4ERR_MOVED)
637 rpc_task_release_transport(task);
643 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
644 struct nfs4_state *state, long *timeout)
646 struct nfs4_exception exception = {
650 if (task->tk_status >= 0)
653 exception.timeout = *timeout;
654 task->tk_status = nfs4_async_handle_exception(task, server,
657 if (exception.delay && timeout)
658 *timeout = exception.timeout;
665 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
666 * or 'false' otherwise.
668 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
670 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
671 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
674 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
676 spin_lock(&clp->cl_lock);
677 if (time_before(clp->cl_last_renewal,timestamp))
678 clp->cl_last_renewal = timestamp;
679 spin_unlock(&clp->cl_lock);
682 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
684 struct nfs_client *clp = server->nfs_client;
686 if (!nfs4_has_session(clp))
687 do_renew_lease(clp, timestamp);
690 struct nfs4_call_sync_data {
691 const struct nfs_server *seq_server;
692 struct nfs4_sequence_args *seq_args;
693 struct nfs4_sequence_res *seq_res;
696 void nfs4_init_sequence(struct nfs4_sequence_args *args,
697 struct nfs4_sequence_res *res, int cache_reply,
700 args->sa_slot = NULL;
701 args->sa_cache_this = cache_reply;
702 args->sa_privileged = privileged;
707 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
709 struct nfs4_slot *slot = res->sr_slot;
710 struct nfs4_slot_table *tbl;
713 spin_lock(&tbl->slot_tbl_lock);
714 if (!nfs41_wake_and_assign_slot(tbl, slot))
715 nfs4_free_slot(tbl, slot);
716 spin_unlock(&tbl->slot_tbl_lock);
721 static int nfs40_sequence_done(struct rpc_task *task,
722 struct nfs4_sequence_res *res)
724 if (res->sr_slot != NULL)
725 nfs40_sequence_free_slot(res);
729 #if defined(CONFIG_NFS_V4_1)
731 static void nfs41_release_slot(struct nfs4_slot *slot)
733 struct nfs4_session *session;
734 struct nfs4_slot_table *tbl;
735 bool send_new_highest_used_slotid = false;
740 session = tbl->session;
742 /* Bump the slot sequence number */
747 spin_lock(&tbl->slot_tbl_lock);
748 /* Be nice to the server: try to ensure that the last transmitted
749 * value for highest_user_slotid <= target_highest_slotid
751 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
752 send_new_highest_used_slotid = true;
754 if (nfs41_wake_and_assign_slot(tbl, slot)) {
755 send_new_highest_used_slotid = false;
758 nfs4_free_slot(tbl, slot);
760 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
761 send_new_highest_used_slotid = false;
763 spin_unlock(&tbl->slot_tbl_lock);
764 if (send_new_highest_used_slotid)
765 nfs41_notify_server(session->clp);
766 if (waitqueue_active(&tbl->slot_waitq))
767 wake_up_all(&tbl->slot_waitq);
770 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
772 nfs41_release_slot(res->sr_slot);
776 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
779 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
780 slot->seq_nr_highest_sent = seqnr;
782 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
785 slot->seq_nr_highest_sent = seqnr;
786 slot->seq_nr_last_acked = seqnr;
789 static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
790 struct nfs4_slot *slot)
792 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
794 rpc_put_task_async(task);
797 static int nfs41_sequence_process(struct rpc_task *task,
798 struct nfs4_sequence_res *res)
800 struct nfs4_session *session;
801 struct nfs4_slot *slot = res->sr_slot;
802 struct nfs_client *clp;
808 /* don't increment the sequence number if the task wasn't sent */
809 if (!RPC_WAS_SENT(task) || slot->seq_done)
812 session = slot->table->session;
815 trace_nfs4_sequence_done(session, res);
817 status = res->sr_status;
818 if (task->tk_status == -NFS4ERR_DEADSESSION)
819 status = -NFS4ERR_DEADSESSION;
821 /* Check the SEQUENCE operation status */
824 /* Mark this sequence number as having been acked */
825 nfs4_slot_sequence_acked(slot, slot->seq_nr);
826 /* Update the slot's sequence and clientid lease timer */
828 do_renew_lease(clp, res->sr_timestamp);
829 /* Check sequence flags */
830 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
832 nfs41_update_target_slotid(slot->table, slot, res);
836 * sr_status remains 1 if an RPC level error occurred.
837 * The server may or may not have processed the sequence
840 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
844 /* The server detected a resend of the RPC call and
845 * returned NFS4ERR_DELAY as per Section 2.10.6.2
848 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
852 nfs4_slot_sequence_acked(slot, slot->seq_nr);
854 case -NFS4ERR_RETRY_UNCACHED_REP:
855 case -NFS4ERR_SEQ_FALSE_RETRY:
857 * The server thinks we tried to replay a request.
858 * Retry the call after bumping the sequence ID.
860 nfs4_slot_sequence_acked(slot, slot->seq_nr);
862 case -NFS4ERR_BADSLOT:
864 * The slot id we used was probably retired. Try again
865 * using a different slot id.
867 if (slot->slot_nr < slot->table->target_highest_slotid)
868 goto session_recover;
870 case -NFS4ERR_SEQ_MISORDERED:
871 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
873 * Were one or more calls using this slot interrupted?
874 * If the server never received the request, then our
875 * transmitted slot sequence number may be too high. However,
876 * if the server did receive the request then it might
877 * accidentally give us a reply with a mismatched operation.
878 * We can sort this out by sending a lone sequence operation
879 * to the server on the same slot.
881 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
883 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
884 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
891 * A retry might be sent while the original request is
892 * still in progress on the replier. The replier SHOULD
893 * deal with the issue by returning NFS4ERR_DELAY as the
894 * reply to SEQUENCE or CB_SEQUENCE operation, but
895 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
897 * Restart the search after a delay.
899 slot->seq_nr = slot->seq_nr_highest_sent;
901 case -NFS4ERR_BADSESSION:
902 case -NFS4ERR_DEADSESSION:
903 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
904 goto session_recover;
906 /* Just update the slot sequence no. */
910 /* The session may be reset by one of the error handlers. */
911 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
915 nfs4_schedule_session_recovery(session, status);
916 dprintk("%s ERROR: %d Reset session\n", __func__, status);
917 nfs41_sequence_free_slot(res);
922 if (rpc_restart_call_prepare(task)) {
923 nfs41_sequence_free_slot(res);
929 if (!rpc_restart_call(task))
931 rpc_delay(task, NFS4_POLL_RETRY_MAX);
935 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
937 if (!nfs41_sequence_process(task, res))
939 if (res->sr_slot != NULL)
940 nfs41_sequence_free_slot(res);
944 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
946 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
948 if (res->sr_slot == NULL)
950 if (res->sr_slot->table->session != NULL)
951 return nfs41_sequence_process(task, res);
952 return nfs40_sequence_done(task, res);
955 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
957 if (res->sr_slot != NULL) {
958 if (res->sr_slot->table->session != NULL)
959 nfs41_sequence_free_slot(res);
961 nfs40_sequence_free_slot(res);
965 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
967 if (res->sr_slot == NULL)
969 if (!res->sr_slot->table->session)
970 return nfs40_sequence_done(task, res);
971 return nfs41_sequence_done(task, res);
973 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
975 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
977 struct nfs4_call_sync_data *data = calldata;
979 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
981 nfs4_setup_sequence(data->seq_server->nfs_client,
982 data->seq_args, data->seq_res, task);
985 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
987 struct nfs4_call_sync_data *data = calldata;
989 nfs41_sequence_done(task, data->seq_res);
992 static const struct rpc_call_ops nfs41_call_sync_ops = {
993 .rpc_call_prepare = nfs41_call_sync_prepare,
994 .rpc_call_done = nfs41_call_sync_done,
997 #else /* !CONFIG_NFS_V4_1 */
999 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1001 return nfs40_sequence_done(task, res);
1004 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1006 if (res->sr_slot != NULL)
1007 nfs40_sequence_free_slot(res);
1010 int nfs4_sequence_done(struct rpc_task *task,
1011 struct nfs4_sequence_res *res)
1013 return nfs40_sequence_done(task, res);
1015 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1017 #endif /* !CONFIG_NFS_V4_1 */
1019 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1021 res->sr_timestamp = jiffies;
1022 res->sr_status_flags = 0;
1027 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1028 struct nfs4_sequence_res *res,
1029 struct nfs4_slot *slot)
1033 slot->privileged = args->sa_privileged ? 1 : 0;
1034 args->sa_slot = slot;
1036 res->sr_slot = slot;
1039 int nfs4_setup_sequence(struct nfs_client *client,
1040 struct nfs4_sequence_args *args,
1041 struct nfs4_sequence_res *res,
1042 struct rpc_task *task)
1044 struct nfs4_session *session = nfs4_get_session(client);
1045 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1046 struct nfs4_slot *slot;
1048 /* slot already allocated? */
1049 if (res->sr_slot != NULL)
1053 tbl = &session->fc_slot_table;
1055 spin_lock(&tbl->slot_tbl_lock);
1056 /* The state manager will wait until the slot table is empty */
1057 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1060 slot = nfs4_alloc_slot(tbl);
1062 if (slot == ERR_PTR(-ENOMEM))
1063 goto out_sleep_timeout;
1066 spin_unlock(&tbl->slot_tbl_lock);
1068 nfs4_sequence_attach_slot(args, res, slot);
1070 trace_nfs4_setup_sequence(session, args);
1072 nfs41_sequence_res_init(res);
1073 rpc_call_start(task);
1076 /* Try again in 1/4 second */
1077 if (args->sa_privileged)
1078 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1079 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1081 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1082 NULL, jiffies + (HZ >> 2));
1083 spin_unlock(&tbl->slot_tbl_lock);
1086 if (args->sa_privileged)
1087 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1088 RPC_PRIORITY_PRIVILEGED);
1090 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1091 spin_unlock(&tbl->slot_tbl_lock);
1094 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1096 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1098 struct nfs4_call_sync_data *data = calldata;
1099 nfs4_setup_sequence(data->seq_server->nfs_client,
1100 data->seq_args, data->seq_res, task);
1103 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1105 struct nfs4_call_sync_data *data = calldata;
1106 nfs4_sequence_done(task, data->seq_res);
1109 static const struct rpc_call_ops nfs40_call_sync_ops = {
1110 .rpc_call_prepare = nfs40_call_sync_prepare,
1111 .rpc_call_done = nfs40_call_sync_done,
1114 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1117 struct rpc_task *task;
1119 task = rpc_run_task(task_setup);
1121 return PTR_ERR(task);
1123 ret = task->tk_status;
1128 static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1129 struct nfs_server *server,
1130 struct rpc_message *msg,
1131 struct nfs4_sequence_args *args,
1132 struct nfs4_sequence_res *res,
1133 unsigned short task_flags)
1135 struct nfs_client *clp = server->nfs_client;
1136 struct nfs4_call_sync_data data = {
1137 .seq_server = server,
1141 struct rpc_task_setup task_setup = {
1144 .callback_ops = clp->cl_mvops->call_sync_ops,
1145 .callback_data = &data,
1146 .flags = task_flags,
1149 return nfs4_call_sync_custom(&task_setup);
1152 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1153 struct nfs_server *server,
1154 struct rpc_message *msg,
1155 struct nfs4_sequence_args *args,
1156 struct nfs4_sequence_res *res)
1158 unsigned short task_flags = 0;
1160 if (server->nfs_client->cl_minorversion)
1161 task_flags = RPC_TASK_MOVEABLE;
1162 return nfs4_do_call_sync(clnt, server, msg, args, res, task_flags);
1166 int nfs4_call_sync(struct rpc_clnt *clnt,
1167 struct nfs_server *server,
1168 struct rpc_message *msg,
1169 struct nfs4_sequence_args *args,
1170 struct nfs4_sequence_res *res,
1173 nfs4_init_sequence(args, res, cache_reply, 0);
1174 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1178 nfs4_inc_nlink_locked(struct inode *inode)
1180 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1181 NFS_INO_INVALID_CTIME |
1182 NFS_INO_INVALID_NLINK);
1187 nfs4_inc_nlink(struct inode *inode)
1189 spin_lock(&inode->i_lock);
1190 nfs4_inc_nlink_locked(inode);
1191 spin_unlock(&inode->i_lock);
1195 nfs4_dec_nlink_locked(struct inode *inode)
1197 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1198 NFS_INO_INVALID_CTIME |
1199 NFS_INO_INVALID_NLINK);
1204 nfs4_update_changeattr_locked(struct inode *inode,
1205 struct nfs4_change_info *cinfo,
1206 unsigned long timestamp, unsigned long cache_validity)
1208 struct nfs_inode *nfsi = NFS_I(inode);
1209 u64 change_attr = inode_peek_iversion_raw(inode);
1211 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
1212 if (S_ISDIR(inode->i_mode))
1213 cache_validity |= NFS_INO_INVALID_DATA;
1215 switch (NFS_SERVER(inode)->change_attr_type) {
1216 case NFS4_CHANGE_TYPE_IS_UNDEFINED:
1217 if (cinfo->after == change_attr)
1221 if ((s64)(change_attr - cinfo->after) >= 0)
1225 inode_set_iversion_raw(inode, cinfo->after);
1226 if (!cinfo->atomic || cinfo->before != change_attr) {
1227 if (S_ISDIR(inode->i_mode))
1228 nfs_force_lookup_revalidate(inode);
1230 if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1232 NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
1233 NFS_INO_INVALID_SIZE | NFS_INO_INVALID_OTHER |
1234 NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
1235 NFS_INO_INVALID_MODE | NFS_INO_INVALID_XATTR |
1236 NFS_INO_REVAL_PAGECACHE;
1237 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1239 nfsi->attrtimeo_timestamp = jiffies;
1240 nfsi->read_cache_jiffies = timestamp;
1241 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1242 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1244 nfs_set_cache_invalid(inode, cache_validity);
1248 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1249 unsigned long timestamp, unsigned long cache_validity)
1251 spin_lock(&dir->i_lock);
1252 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1253 spin_unlock(&dir->i_lock);
1256 struct nfs4_open_createattrs {
1257 struct nfs4_label *label;
1258 struct iattr *sattr;
1259 const __u32 verf[2];
1262 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1263 int err, struct nfs4_exception *exception)
1267 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1269 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1270 exception->retry = 1;
1274 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1276 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1279 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1281 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1283 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1287 nfs4_map_atomic_open_share(struct nfs_server *server,
1288 fmode_t fmode, int openflags)
1292 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1294 res = NFS4_SHARE_ACCESS_READ;
1297 res = NFS4_SHARE_ACCESS_WRITE;
1299 case FMODE_READ|FMODE_WRITE:
1300 res = NFS4_SHARE_ACCESS_BOTH;
1302 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1304 /* Want no delegation if we're using O_DIRECT */
1305 if (openflags & O_DIRECT)
1306 res |= NFS4_SHARE_WANT_NO_DELEG;
1311 static enum open_claim_type4
1312 nfs4_map_atomic_open_claim(struct nfs_server *server,
1313 enum open_claim_type4 claim)
1315 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1320 case NFS4_OPEN_CLAIM_FH:
1321 return NFS4_OPEN_CLAIM_NULL;
1322 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1323 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1324 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1325 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1329 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1331 p->o_res.f_attr = &p->f_attr;
1332 p->o_res.f_label = p->f_label;
1333 p->o_res.seqid = p->o_arg.seqid;
1334 p->c_res.seqid = p->c_arg.seqid;
1335 p->o_res.server = p->o_arg.server;
1336 p->o_res.access_request = p->o_arg.access;
1337 nfs_fattr_init(&p->f_attr);
1338 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1341 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1342 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1343 const struct nfs4_open_createattrs *c,
1344 enum open_claim_type4 claim,
1347 struct dentry *parent = dget_parent(dentry);
1348 struct inode *dir = d_inode(parent);
1349 struct nfs_server *server = NFS_SERVER(dir);
1350 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1351 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1352 struct nfs4_opendata *p;
1354 p = kzalloc(sizeof(*p), gfp_mask);
1358 p->f_label = nfs4_label_alloc(server, gfp_mask);
1359 if (IS_ERR(p->f_label))
1362 p->a_label = nfs4_label_alloc(server, gfp_mask);
1363 if (IS_ERR(p->a_label))
1366 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1367 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1368 if (IS_ERR(p->o_arg.seqid))
1369 goto err_free_label;
1370 nfs_sb_active(dentry->d_sb);
1371 p->dentry = dget(dentry);
1374 atomic_inc(&sp->so_count);
1375 p->o_arg.open_flags = flags;
1376 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1377 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1378 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1380 if (flags & O_CREAT) {
1381 p->o_arg.umask = current_umask();
1382 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1383 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1384 p->o_arg.u.attrs = &p->attrs;
1385 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1387 memcpy(p->o_arg.u.verifier.data, c->verf,
1388 sizeof(p->o_arg.u.verifier.data));
1391 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1392 * will return permission denied for all bits until close */
1393 if (!(flags & O_EXCL)) {
1394 /* ask server to check for all possible rights as results
1396 switch (p->o_arg.claim) {
1399 case NFS4_OPEN_CLAIM_NULL:
1400 case NFS4_OPEN_CLAIM_FH:
1401 p->o_arg.access = NFS4_ACCESS_READ |
1402 NFS4_ACCESS_MODIFY |
1403 NFS4_ACCESS_EXTEND |
1404 NFS4_ACCESS_EXECUTE;
1405 #ifdef CONFIG_NFS_V4_2
1406 if (server->caps & NFS_CAP_XATTR)
1407 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1408 NFS4_ACCESS_XAWRITE |
1413 p->o_arg.clientid = server->nfs_client->cl_clientid;
1414 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1415 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1416 p->o_arg.name = &dentry->d_name;
1417 p->o_arg.server = server;
1418 p->o_arg.bitmask = nfs4_bitmask(server, label);
1419 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1420 switch (p->o_arg.claim) {
1421 case NFS4_OPEN_CLAIM_NULL:
1422 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1423 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1424 p->o_arg.fh = NFS_FH(dir);
1426 case NFS4_OPEN_CLAIM_PREVIOUS:
1427 case NFS4_OPEN_CLAIM_FH:
1428 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1429 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1430 p->o_arg.fh = NFS_FH(d_inode(dentry));
1432 p->c_arg.fh = &p->o_res.fh;
1433 p->c_arg.stateid = &p->o_res.stateid;
1434 p->c_arg.seqid = p->o_arg.seqid;
1435 nfs4_init_opendata_res(p);
1436 kref_init(&p->kref);
1440 nfs4_label_free(p->a_label);
1442 nfs4_label_free(p->f_label);
1450 static void nfs4_opendata_free(struct kref *kref)
1452 struct nfs4_opendata *p = container_of(kref,
1453 struct nfs4_opendata, kref);
1454 struct super_block *sb = p->dentry->d_sb;
1456 nfs4_lgopen_release(p->lgp);
1457 nfs_free_seqid(p->o_arg.seqid);
1458 nfs4_sequence_free_slot(&p->o_res.seq_res);
1459 if (p->state != NULL)
1460 nfs4_put_open_state(p->state);
1461 nfs4_put_state_owner(p->owner);
1463 nfs4_label_free(p->a_label);
1464 nfs4_label_free(p->f_label);
1468 nfs_sb_deactive(sb);
1469 nfs_fattr_free_names(&p->f_attr);
1470 kfree(p->f_attr.mdsthreshold);
1474 static void nfs4_opendata_put(struct nfs4_opendata *p)
1477 kref_put(&p->kref, nfs4_opendata_free);
1480 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1483 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1484 case FMODE_READ|FMODE_WRITE:
1485 return state->n_rdwr != 0;
1487 return state->n_wronly != 0;
1489 return state->n_rdonly != 0;
1495 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1496 int open_mode, enum open_claim_type4 claim)
1500 if (open_mode & (O_EXCL|O_TRUNC))
1503 case NFS4_OPEN_CLAIM_NULL:
1504 case NFS4_OPEN_CLAIM_FH:
1509 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1511 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1512 && state->n_rdonly != 0;
1515 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1516 && state->n_wronly != 0;
1518 case FMODE_READ|FMODE_WRITE:
1519 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1520 && state->n_rdwr != 0;
1526 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1527 enum open_claim_type4 claim)
1529 if (delegation == NULL)
1531 if ((delegation->type & fmode) != fmode)
1534 case NFS4_OPEN_CLAIM_NULL:
1535 case NFS4_OPEN_CLAIM_FH:
1537 case NFS4_OPEN_CLAIM_PREVIOUS:
1538 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1544 nfs_mark_delegation_referenced(delegation);
1548 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1557 case FMODE_READ|FMODE_WRITE:
1560 nfs4_state_set_mode_locked(state, state->state | fmode);
1563 #ifdef CONFIG_NFS_V4_1
1564 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1566 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1568 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1570 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1574 #endif /* CONFIG_NFS_V4_1 */
1576 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1578 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1579 wake_up_all(&state->waitq);
1582 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1584 struct nfs_client *clp = state->owner->so_server->nfs_client;
1585 bool need_recover = false;
1587 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1588 need_recover = true;
1589 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1590 need_recover = true;
1591 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1592 need_recover = true;
1594 nfs4_state_mark_reclaim_nograce(clp, state);
1598 * Check for whether or not the caller may update the open stateid
1599 * to the value passed in by stateid.
1601 * Note: This function relies heavily on the server implementing
1602 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1604 * i.e. The stateid seqids have to be initialised to 1, and
1605 * are then incremented on every state transition.
1607 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1608 const nfs4_stateid *stateid)
1610 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1611 /* The common case - we're updating to a new sequence number */
1612 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1613 nfs4_stateid_is_next(&state->open_stateid, stateid)) {
1617 /* This is the first OPEN in this generation */
1618 if (stateid->seqid == cpu_to_be32(1))
1624 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1626 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1628 if (state->n_wronly)
1629 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1630 if (state->n_rdonly)
1631 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1633 set_bit(NFS_O_RDWR_STATE, &state->flags);
1634 set_bit(NFS_OPEN_STATE, &state->flags);
1637 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1638 nfs4_stateid *stateid, fmode_t fmode)
1640 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1641 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1643 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1646 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1649 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1650 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1651 clear_bit(NFS_OPEN_STATE, &state->flags);
1653 if (stateid == NULL)
1655 /* Handle OPEN+OPEN_DOWNGRADE races */
1656 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1657 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1658 nfs_resync_open_stateid_locked(state);
1661 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1662 nfs4_stateid_copy(&state->stateid, stateid);
1663 nfs4_stateid_copy(&state->open_stateid, stateid);
1664 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1666 nfs_state_log_update_open_stateid(state);
1669 static void nfs_clear_open_stateid(struct nfs4_state *state,
1670 nfs4_stateid *arg_stateid,
1671 nfs4_stateid *stateid, fmode_t fmode)
1673 write_seqlock(&state->seqlock);
1674 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1675 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1676 nfs_clear_open_stateid_locked(state, stateid, fmode);
1677 write_sequnlock(&state->seqlock);
1678 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1679 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1682 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1683 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1684 __must_hold(&state->owner->so_lock)
1685 __must_hold(&state->seqlock)
1693 if (nfs_stateid_is_sequential(state, stateid))
1698 /* Rely on seqids for serialisation with NFSv4.0 */
1699 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1702 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1703 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1705 * Ensure we process the state changes in the same order
1706 * in which the server processed them by delaying the
1707 * update of the stateid until we are in sequence.
1709 write_sequnlock(&state->seqlock);
1710 spin_unlock(&state->owner->so_lock);
1712 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1714 if (!fatal_signal_pending(current)) {
1715 if (schedule_timeout(5*HZ) == 0)
1721 finish_wait(&state->waitq, &wait);
1723 spin_lock(&state->owner->so_lock);
1724 write_seqlock(&state->seqlock);
1727 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1728 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1729 nfs4_stateid_copy(freeme, &state->open_stateid);
1730 nfs_test_and_clear_all_open_stateid(state);
1733 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1734 nfs4_stateid_copy(&state->stateid, stateid);
1735 nfs4_stateid_copy(&state->open_stateid, stateid);
1736 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1737 nfs_state_log_update_open_stateid(state);
1740 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1741 const nfs4_stateid *open_stateid,
1743 nfs4_stateid *freeme)
1746 * Protect the call to nfs4_state_set_mode_locked and
1747 * serialise the stateid update
1749 write_seqlock(&state->seqlock);
1750 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1753 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1756 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1758 case FMODE_READ|FMODE_WRITE:
1759 set_bit(NFS_O_RDWR_STATE, &state->flags);
1761 set_bit(NFS_OPEN_STATE, &state->flags);
1762 write_sequnlock(&state->seqlock);
1765 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1767 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1768 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1769 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1770 clear_bit(NFS_OPEN_STATE, &state->flags);
1773 static void nfs_state_set_delegation(struct nfs4_state *state,
1774 const nfs4_stateid *deleg_stateid,
1778 * Protect the call to nfs4_state_set_mode_locked and
1779 * serialise the stateid update
1781 write_seqlock(&state->seqlock);
1782 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1783 set_bit(NFS_DELEGATED_STATE, &state->flags);
1784 write_sequnlock(&state->seqlock);
1787 static void nfs_state_clear_delegation(struct nfs4_state *state)
1789 write_seqlock(&state->seqlock);
1790 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1791 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1792 write_sequnlock(&state->seqlock);
1795 int update_open_stateid(struct nfs4_state *state,
1796 const nfs4_stateid *open_stateid,
1797 const nfs4_stateid *delegation,
1800 struct nfs_server *server = NFS_SERVER(state->inode);
1801 struct nfs_client *clp = server->nfs_client;
1802 struct nfs_inode *nfsi = NFS_I(state->inode);
1803 struct nfs_delegation *deleg_cur;
1804 nfs4_stateid freeme = { };
1807 fmode &= (FMODE_READ|FMODE_WRITE);
1810 spin_lock(&state->owner->so_lock);
1811 if (open_stateid != NULL) {
1812 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1816 deleg_cur = nfs4_get_valid_delegation(state->inode);
1817 if (deleg_cur == NULL)
1820 spin_lock(&deleg_cur->lock);
1821 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1822 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1823 (deleg_cur->type & fmode) != fmode)
1824 goto no_delegation_unlock;
1826 if (delegation == NULL)
1827 delegation = &deleg_cur->stateid;
1828 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1829 goto no_delegation_unlock;
1831 nfs_mark_delegation_referenced(deleg_cur);
1832 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1834 no_delegation_unlock:
1835 spin_unlock(&deleg_cur->lock);
1838 update_open_stateflags(state, fmode);
1839 spin_unlock(&state->owner->so_lock);
1842 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1843 nfs4_schedule_state_manager(clp);
1844 if (freeme.type != 0)
1845 nfs4_test_and_free_stateid(server, &freeme,
1846 state->owner->so_cred);
1851 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1852 const nfs4_stateid *stateid)
1854 struct nfs4_state *state = lsp->ls_state;
1857 spin_lock(&state->state_lock);
1858 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1860 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1862 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1865 spin_unlock(&state->state_lock);
1869 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1871 struct nfs_delegation *delegation;
1873 fmode &= FMODE_READ|FMODE_WRITE;
1875 delegation = nfs4_get_valid_delegation(inode);
1876 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1881 nfs4_inode_return_delegation(inode);
1884 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1886 struct nfs4_state *state = opendata->state;
1887 struct nfs_delegation *delegation;
1888 int open_mode = opendata->o_arg.open_flags;
1889 fmode_t fmode = opendata->o_arg.fmode;
1890 enum open_claim_type4 claim = opendata->o_arg.claim;
1891 nfs4_stateid stateid;
1895 spin_lock(&state->owner->so_lock);
1896 if (can_open_cached(state, fmode, open_mode, claim)) {
1897 update_open_stateflags(state, fmode);
1898 spin_unlock(&state->owner->so_lock);
1899 goto out_return_state;
1901 spin_unlock(&state->owner->so_lock);
1903 delegation = nfs4_get_valid_delegation(state->inode);
1904 if (!can_open_delegated(delegation, fmode, claim)) {
1908 /* Save the delegation */
1909 nfs4_stateid_copy(&stateid, &delegation->stateid);
1911 nfs_release_seqid(opendata->o_arg.seqid);
1912 if (!opendata->is_recover) {
1913 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1919 /* Try to update the stateid using the delegation */
1920 if (update_open_stateid(state, NULL, &stateid, fmode))
1921 goto out_return_state;
1924 return ERR_PTR(ret);
1926 refcount_inc(&state->count);
1931 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1933 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1934 struct nfs_delegation *delegation;
1935 int delegation_flags = 0;
1938 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1940 delegation_flags = delegation->flags;
1942 switch (data->o_arg.claim) {
1945 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1946 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1947 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1948 "returning a delegation for "
1949 "OPEN(CLAIM_DELEGATE_CUR)\n",
1953 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1954 nfs_inode_set_delegation(state->inode,
1955 data->owner->so_cred,
1956 data->o_res.delegation_type,
1957 &data->o_res.delegation,
1958 data->o_res.pagemod_limit);
1960 nfs_inode_reclaim_delegation(state->inode,
1961 data->owner->so_cred,
1962 data->o_res.delegation_type,
1963 &data->o_res.delegation,
1964 data->o_res.pagemod_limit);
1966 if (data->o_res.do_recall)
1967 nfs_async_inode_return_delegation(state->inode,
1968 &data->o_res.delegation);
1972 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1973 * and update the nfs4_state.
1975 static struct nfs4_state *
1976 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1978 struct inode *inode = data->state->inode;
1979 struct nfs4_state *state = data->state;
1982 if (!data->rpc_done) {
1983 if (data->rpc_status)
1984 return ERR_PTR(data->rpc_status);
1985 /* cached opens have already been processed */
1989 ret = nfs_refresh_inode(inode, &data->f_attr);
1991 return ERR_PTR(ret);
1993 if (data->o_res.delegation_type != 0)
1994 nfs4_opendata_check_deleg(data, state);
1996 if (!update_open_stateid(state, &data->o_res.stateid,
1997 NULL, data->o_arg.fmode))
1998 return ERR_PTR(-EAGAIN);
1999 refcount_inc(&state->count);
2004 static struct inode *
2005 nfs4_opendata_get_inode(struct nfs4_opendata *data)
2007 struct inode *inode;
2009 switch (data->o_arg.claim) {
2010 case NFS4_OPEN_CLAIM_NULL:
2011 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2012 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2013 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2014 return ERR_PTR(-EAGAIN);
2015 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2016 &data->f_attr, data->f_label);
2019 inode = d_inode(data->dentry);
2021 nfs_refresh_inode(inode, &data->f_attr);
2026 static struct nfs4_state *
2027 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2029 struct nfs4_state *state;
2030 struct inode *inode;
2032 inode = nfs4_opendata_get_inode(data);
2034 return ERR_CAST(inode);
2035 if (data->state != NULL && data->state->inode == inode) {
2036 state = data->state;
2037 refcount_inc(&state->count);
2039 state = nfs4_get_open_state(inode, data->owner);
2042 state = ERR_PTR(-ENOMEM);
2046 static struct nfs4_state *
2047 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2049 struct nfs4_state *state;
2051 if (!data->rpc_done) {
2052 state = nfs4_try_open_cached(data);
2053 trace_nfs4_cached_open(data->state);
2057 state = nfs4_opendata_find_nfs4_state(data);
2061 if (data->o_res.delegation_type != 0)
2062 nfs4_opendata_check_deleg(data, state);
2063 if (!update_open_stateid(state, &data->o_res.stateid,
2064 NULL, data->o_arg.fmode)) {
2065 nfs4_put_open_state(state);
2066 state = ERR_PTR(-EAGAIN);
2069 nfs_release_seqid(data->o_arg.seqid);
2073 static struct nfs4_state *
2074 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2076 struct nfs4_state *ret;
2078 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2079 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2081 ret = _nfs4_opendata_to_nfs4_state(data);
2082 nfs4_sequence_free_slot(&data->o_res.seq_res);
2086 static struct nfs_open_context *
2087 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2089 struct nfs_inode *nfsi = NFS_I(state->inode);
2090 struct nfs_open_context *ctx;
2093 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2094 if (ctx->state != state)
2096 if ((ctx->mode & mode) != mode)
2098 if (!get_nfs_open_context(ctx))
2104 return ERR_PTR(-ENOENT);
2107 static struct nfs_open_context *
2108 nfs4_state_find_open_context(struct nfs4_state *state)
2110 struct nfs_open_context *ctx;
2112 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2115 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2118 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2121 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2122 struct nfs4_state *state, enum open_claim_type4 claim)
2124 struct nfs4_opendata *opendata;
2126 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2127 NULL, claim, GFP_NOFS);
2128 if (opendata == NULL)
2129 return ERR_PTR(-ENOMEM);
2130 opendata->state = state;
2131 refcount_inc(&state->count);
2135 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2138 struct nfs4_state *newstate;
2141 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2143 opendata->o_arg.open_flags = 0;
2144 opendata->o_arg.fmode = fmode;
2145 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2146 NFS_SB(opendata->dentry->d_sb),
2148 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2149 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2150 nfs4_init_opendata_res(opendata);
2151 ret = _nfs4_recover_proc_open(opendata);
2154 newstate = nfs4_opendata_to_nfs4_state(opendata);
2155 if (IS_ERR(newstate))
2156 return PTR_ERR(newstate);
2157 if (newstate != opendata->state)
2159 nfs4_close_state(newstate, fmode);
2163 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2167 /* memory barrier prior to reading state->n_* */
2169 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2172 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2175 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2179 * We may have performed cached opens for all three recoveries.
2180 * Check if we need to update the current stateid.
2182 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2183 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2184 write_seqlock(&state->seqlock);
2185 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2186 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2187 write_sequnlock(&state->seqlock);
2194 * reclaim state on the server after a reboot.
2196 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2198 struct nfs_delegation *delegation;
2199 struct nfs4_opendata *opendata;
2200 fmode_t delegation_type = 0;
2203 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2204 NFS4_OPEN_CLAIM_PREVIOUS);
2205 if (IS_ERR(opendata))
2206 return PTR_ERR(opendata);
2208 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2209 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2210 delegation_type = delegation->type;
2212 opendata->o_arg.u.delegation_type = delegation_type;
2213 status = nfs4_open_recover(opendata, state);
2214 nfs4_opendata_put(opendata);
2218 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2220 struct nfs_server *server = NFS_SERVER(state->inode);
2221 struct nfs4_exception exception = { };
2224 err = _nfs4_do_open_reclaim(ctx, state);
2225 trace_nfs4_open_reclaim(ctx, 0, err);
2226 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2228 if (err != -NFS4ERR_DELAY)
2230 nfs4_handle_exception(server, err, &exception);
2231 } while (exception.retry);
2235 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2237 struct nfs_open_context *ctx;
2240 ctx = nfs4_state_find_open_context(state);
2243 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2244 nfs_state_clear_open_state_flags(state);
2245 ret = nfs4_do_open_reclaim(ctx, state);
2246 put_nfs_open_context(ctx);
2250 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)
2254 printk(KERN_ERR "NFS: %s: unhandled error "
2255 "%d.\n", __func__, err);
2263 case -NFS4ERR_BADSESSION:
2264 case -NFS4ERR_BADSLOT:
2265 case -NFS4ERR_BAD_HIGH_SLOT:
2266 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2267 case -NFS4ERR_DEADSESSION:
2269 case -NFS4ERR_STALE_CLIENTID:
2270 case -NFS4ERR_STALE_STATEID:
2271 /* Don't recall a delegation if it was lost */
2272 nfs4_schedule_lease_recovery(server->nfs_client);
2274 case -NFS4ERR_MOVED:
2275 nfs4_schedule_migration_recovery(server);
2277 case -NFS4ERR_LEASE_MOVED:
2278 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2280 case -NFS4ERR_DELEG_REVOKED:
2281 case -NFS4ERR_ADMIN_REVOKED:
2282 case -NFS4ERR_EXPIRED:
2283 case -NFS4ERR_BAD_STATEID:
2284 case -NFS4ERR_OPENMODE:
2285 nfs_inode_find_state_and_recover(state->inode,
2287 nfs4_schedule_stateid_recovery(server, state);
2289 case -NFS4ERR_DELAY:
2290 case -NFS4ERR_GRACE:
2294 case -NFS4ERR_DENIED:
2296 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2298 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2305 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2306 struct nfs4_state *state, const nfs4_stateid *stateid)
2308 struct nfs_server *server = NFS_SERVER(state->inode);
2309 struct nfs4_opendata *opendata;
2312 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2313 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2314 if (IS_ERR(opendata))
2315 return PTR_ERR(opendata);
2316 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2317 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2318 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2322 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2323 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2327 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2328 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2332 nfs_state_clear_delegation(state);
2334 nfs4_opendata_put(opendata);
2335 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2338 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2340 struct nfs4_opendata *data = calldata;
2342 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2343 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2346 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2348 struct nfs4_opendata *data = calldata;
2350 nfs40_sequence_done(task, &data->c_res.seq_res);
2352 data->rpc_status = task->tk_status;
2353 if (data->rpc_status == 0) {
2354 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2355 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2356 renew_lease(data->o_res.server, data->timestamp);
2357 data->rpc_done = true;
2361 static void nfs4_open_confirm_release(void *calldata)
2363 struct nfs4_opendata *data = calldata;
2364 struct nfs4_state *state = NULL;
2366 /* If this request hasn't been cancelled, do nothing */
2367 if (!data->cancelled)
2369 /* In case of error, no cleanup! */
2370 if (!data->rpc_done)
2372 state = nfs4_opendata_to_nfs4_state(data);
2374 nfs4_close_state(state, data->o_arg.fmode);
2376 nfs4_opendata_put(data);
2379 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2380 .rpc_call_prepare = nfs4_open_confirm_prepare,
2381 .rpc_call_done = nfs4_open_confirm_done,
2382 .rpc_release = nfs4_open_confirm_release,
2386 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2388 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2390 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2391 struct rpc_task *task;
2392 struct rpc_message msg = {
2393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2394 .rpc_argp = &data->c_arg,
2395 .rpc_resp = &data->c_res,
2396 .rpc_cred = data->owner->so_cred,
2398 struct rpc_task_setup task_setup_data = {
2399 .rpc_client = server->client,
2400 .rpc_message = &msg,
2401 .callback_ops = &nfs4_open_confirm_ops,
2402 .callback_data = data,
2403 .workqueue = nfsiod_workqueue,
2404 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2408 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2410 kref_get(&data->kref);
2411 data->rpc_done = false;
2412 data->rpc_status = 0;
2413 data->timestamp = jiffies;
2414 task = rpc_run_task(&task_setup_data);
2416 return PTR_ERR(task);
2417 status = rpc_wait_for_completion_task(task);
2419 data->cancelled = true;
2422 status = data->rpc_status;
2427 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2429 struct nfs4_opendata *data = calldata;
2430 struct nfs4_state_owner *sp = data->owner;
2431 struct nfs_client *clp = sp->so_server->nfs_client;
2432 enum open_claim_type4 claim = data->o_arg.claim;
2434 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2437 * Check if we still need to send an OPEN call, or if we can use
2438 * a delegation instead.
2440 if (data->state != NULL) {
2441 struct nfs_delegation *delegation;
2443 if (can_open_cached(data->state, data->o_arg.fmode,
2444 data->o_arg.open_flags, claim))
2447 delegation = nfs4_get_valid_delegation(data->state->inode);
2448 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2449 goto unlock_no_action;
2452 /* Update client id. */
2453 data->o_arg.clientid = clp->cl_clientid;
2457 case NFS4_OPEN_CLAIM_PREVIOUS:
2458 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2459 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2460 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2462 case NFS4_OPEN_CLAIM_FH:
2463 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2465 data->timestamp = jiffies;
2466 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2467 &data->o_arg.seq_args,
2468 &data->o_res.seq_res,
2470 nfs_release_seqid(data->o_arg.seqid);
2472 /* Set the create mode (note dependency on the session type) */
2473 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2474 if (data->o_arg.open_flags & O_EXCL) {
2475 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2476 if (nfs4_has_persistent_session(clp))
2477 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2478 else if (clp->cl_mvops->minor_version > 0)
2479 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2483 trace_nfs4_cached_open(data->state);
2486 task->tk_action = NULL;
2488 nfs4_sequence_done(task, &data->o_res.seq_res);
2491 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2493 struct nfs4_opendata *data = calldata;
2495 data->rpc_status = task->tk_status;
2497 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2500 if (task->tk_status == 0) {
2501 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2502 switch (data->o_res.f_attr->mode & S_IFMT) {
2506 data->rpc_status = -ELOOP;
2509 data->rpc_status = -EISDIR;
2512 data->rpc_status = -ENOTDIR;
2515 renew_lease(data->o_res.server, data->timestamp);
2516 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2517 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2519 data->rpc_done = true;
2522 static void nfs4_open_release(void *calldata)
2524 struct nfs4_opendata *data = calldata;
2525 struct nfs4_state *state = NULL;
2527 /* If this request hasn't been cancelled, do nothing */
2528 if (!data->cancelled)
2530 /* In case of error, no cleanup! */
2531 if (data->rpc_status != 0 || !data->rpc_done)
2533 /* In case we need an open_confirm, no cleanup! */
2534 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2536 state = nfs4_opendata_to_nfs4_state(data);
2538 nfs4_close_state(state, data->o_arg.fmode);
2540 nfs4_opendata_put(data);
2543 static const struct rpc_call_ops nfs4_open_ops = {
2544 .rpc_call_prepare = nfs4_open_prepare,
2545 .rpc_call_done = nfs4_open_done,
2546 .rpc_release = nfs4_open_release,
2549 static int nfs4_run_open_task(struct nfs4_opendata *data,
2550 struct nfs_open_context *ctx)
2552 struct inode *dir = d_inode(data->dir);
2553 struct nfs_server *server = NFS_SERVER(dir);
2554 struct nfs_openargs *o_arg = &data->o_arg;
2555 struct nfs_openres *o_res = &data->o_res;
2556 struct rpc_task *task;
2557 struct rpc_message msg = {
2558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2561 .rpc_cred = data->owner->so_cred,
2563 struct rpc_task_setup task_setup_data = {
2564 .rpc_client = server->client,
2565 .rpc_message = &msg,
2566 .callback_ops = &nfs4_open_ops,
2567 .callback_data = data,
2568 .workqueue = nfsiod_workqueue,
2569 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2573 if (server->nfs_client->cl_minorversion)
2574 task_setup_data.flags |= RPC_TASK_MOVEABLE;
2576 kref_get(&data->kref);
2577 data->rpc_done = false;
2578 data->rpc_status = 0;
2579 data->cancelled = false;
2580 data->is_recover = false;
2582 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2583 data->is_recover = true;
2584 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2586 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2587 pnfs_lgopen_prepare(data, ctx);
2589 task = rpc_run_task(&task_setup_data);
2591 return PTR_ERR(task);
2592 status = rpc_wait_for_completion_task(task);
2594 data->cancelled = true;
2597 status = data->rpc_status;
2603 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2605 struct inode *dir = d_inode(data->dir);
2606 struct nfs_openres *o_res = &data->o_res;
2609 status = nfs4_run_open_task(data, NULL);
2610 if (status != 0 || !data->rpc_done)
2613 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2615 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2616 status = _nfs4_proc_open_confirm(data);
2622 * Additional permission checks in order to distinguish between an
2623 * open for read, and an open for execute. This works around the
2624 * fact that NFSv4 OPEN treats read and execute permissions as being
2626 * Note that in the non-execute case, we want to turn off permission
2627 * checking if we just created a new file (POSIX open() semantics).
2629 static int nfs4_opendata_access(const struct cred *cred,
2630 struct nfs4_opendata *opendata,
2631 struct nfs4_state *state, fmode_t fmode,
2634 struct nfs_access_entry cache;
2637 /* access call failed or for some reason the server doesn't
2638 * support any access modes -- defer access call until later */
2639 if (opendata->o_res.access_supported == 0)
2644 * Use openflags to check for exec, because fmode won't
2645 * always have FMODE_EXEC set when file open for exec.
2647 if (openflags & __FMODE_EXEC) {
2648 /* ONLY check for exec rights */
2649 if (S_ISDIR(state->inode->i_mode))
2650 mask = NFS4_ACCESS_LOOKUP;
2652 mask = NFS4_ACCESS_EXECUTE;
2653 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2654 mask = NFS4_ACCESS_READ;
2657 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2658 nfs_access_add_cache(state->inode, &cache);
2660 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2661 if ((mask & ~cache.mask & flags) == 0)
2668 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2670 static int _nfs4_proc_open(struct nfs4_opendata *data,
2671 struct nfs_open_context *ctx)
2673 struct inode *dir = d_inode(data->dir);
2674 struct nfs_server *server = NFS_SERVER(dir);
2675 struct nfs_openargs *o_arg = &data->o_arg;
2676 struct nfs_openres *o_res = &data->o_res;
2679 status = nfs4_run_open_task(data, ctx);
2680 if (!data->rpc_done)
2683 if (status == -NFS4ERR_BADNAME &&
2684 !(o_arg->open_flags & O_CREAT))
2689 nfs_fattr_map_and_free_names(server, &data->f_attr);
2691 if (o_arg->open_flags & O_CREAT) {
2692 if (o_arg->open_flags & O_EXCL)
2693 data->file_created = true;
2694 else if (o_res->cinfo.before != o_res->cinfo.after)
2695 data->file_created = true;
2696 if (data->file_created ||
2697 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2698 nfs4_update_changeattr(dir, &o_res->cinfo,
2699 o_res->f_attr->time_start,
2700 NFS_INO_INVALID_DATA);
2702 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2703 server->caps &= ~NFS_CAP_POSIX_LOCK;
2704 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2705 status = _nfs4_proc_open_confirm(data);
2709 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2710 nfs4_sequence_free_slot(&o_res->seq_res);
2711 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2712 o_res->f_label, NULL);
2719 * reclaim state on the server after a network partition.
2720 * Assumes caller holds the appropriate lock
2722 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2724 struct nfs4_opendata *opendata;
2727 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2728 NFS4_OPEN_CLAIM_FH);
2729 if (IS_ERR(opendata))
2730 return PTR_ERR(opendata);
2731 ret = nfs4_open_recover(opendata, state);
2733 d_drop(ctx->dentry);
2734 nfs4_opendata_put(opendata);
2738 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2740 struct nfs_server *server = NFS_SERVER(state->inode);
2741 struct nfs4_exception exception = { };
2745 err = _nfs4_open_expired(ctx, state);
2746 trace_nfs4_open_expired(ctx, 0, err);
2747 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2752 case -NFS4ERR_GRACE:
2753 case -NFS4ERR_DELAY:
2754 nfs4_handle_exception(server, err, &exception);
2757 } while (exception.retry);
2762 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2764 struct nfs_open_context *ctx;
2767 ctx = nfs4_state_find_open_context(state);
2770 ret = nfs4_do_open_expired(ctx, state);
2771 put_nfs_open_context(ctx);
2775 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2776 const nfs4_stateid *stateid)
2778 nfs_remove_bad_delegation(state->inode, stateid);
2779 nfs_state_clear_delegation(state);
2782 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2784 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2785 nfs_finish_clear_delegation_stateid(state, NULL);
2788 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2790 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2791 nfs40_clear_delegation_stateid(state);
2792 nfs_state_clear_open_state_flags(state);
2793 return nfs4_open_expired(sp, state);
2796 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2797 nfs4_stateid *stateid,
2798 const struct cred *cred)
2800 return -NFS4ERR_BAD_STATEID;
2803 #if defined(CONFIG_NFS_V4_1)
2804 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2805 nfs4_stateid *stateid,
2806 const struct cred *cred)
2810 switch (stateid->type) {
2813 case NFS4_INVALID_STATEID_TYPE:
2814 case NFS4_SPECIAL_STATEID_TYPE:
2815 return -NFS4ERR_BAD_STATEID;
2816 case NFS4_REVOKED_STATEID_TYPE:
2820 status = nfs41_test_stateid(server, stateid, cred);
2822 case -NFS4ERR_EXPIRED:
2823 case -NFS4ERR_ADMIN_REVOKED:
2824 case -NFS4ERR_DELEG_REVOKED:
2830 /* Ack the revoked state to the server */
2831 nfs41_free_stateid(server, stateid, cred, true);
2832 return -NFS4ERR_EXPIRED;
2835 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2837 struct nfs_server *server = NFS_SERVER(state->inode);
2838 nfs4_stateid stateid;
2839 struct nfs_delegation *delegation;
2840 const struct cred *cred = NULL;
2841 int status, ret = NFS_OK;
2843 /* Get the delegation credential for use by test/free_stateid */
2845 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2846 if (delegation == NULL) {
2848 nfs_state_clear_delegation(state);
2852 spin_lock(&delegation->lock);
2853 nfs4_stateid_copy(&stateid, &delegation->stateid);
2855 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2856 &delegation->flags)) {
2857 spin_unlock(&delegation->lock);
2862 if (delegation->cred)
2863 cred = get_cred(delegation->cred);
2864 spin_unlock(&delegation->lock);
2866 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2867 trace_nfs4_test_delegation_stateid(state, NULL, status);
2868 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2869 nfs_finish_clear_delegation_stateid(state, &stateid);
2877 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2881 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2882 nfs4_copy_delegation_stateid(state->inode, state->state,
2884 nfs4_stateid_match_other(&state->stateid, &tmp))
2885 nfs_state_set_delegation(state, &tmp, state->state);
2887 nfs_state_clear_delegation(state);
2891 * nfs41_check_expired_locks - possibly free a lock stateid
2893 * @state: NFSv4 state for an inode
2895 * Returns NFS_OK if recovery for this stateid is now finished.
2896 * Otherwise a negative NFS4ERR value is returned.
2898 static int nfs41_check_expired_locks(struct nfs4_state *state)
2900 int status, ret = NFS_OK;
2901 struct nfs4_lock_state *lsp, *prev = NULL;
2902 struct nfs_server *server = NFS_SERVER(state->inode);
2904 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2907 spin_lock(&state->state_lock);
2908 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2909 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2910 const struct cred *cred = lsp->ls_state->owner->so_cred;
2912 refcount_inc(&lsp->ls_count);
2913 spin_unlock(&state->state_lock);
2915 nfs4_put_lock_state(prev);
2918 status = nfs41_test_and_free_expired_stateid(server,
2921 trace_nfs4_test_lock_stateid(state, lsp, status);
2922 if (status == -NFS4ERR_EXPIRED ||
2923 status == -NFS4ERR_BAD_STATEID) {
2924 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2925 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2926 if (!recover_lost_locks)
2927 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2928 } else if (status != NFS_OK) {
2930 nfs4_put_lock_state(prev);
2933 spin_lock(&state->state_lock);
2936 spin_unlock(&state->state_lock);
2937 nfs4_put_lock_state(prev);
2943 * nfs41_check_open_stateid - possibly free an open stateid
2945 * @state: NFSv4 state for an inode
2947 * Returns NFS_OK if recovery for this stateid is now finished.
2948 * Otherwise a negative NFS4ERR value is returned.
2950 static int nfs41_check_open_stateid(struct nfs4_state *state)
2952 struct nfs_server *server = NFS_SERVER(state->inode);
2953 nfs4_stateid *stateid = &state->open_stateid;
2954 const struct cred *cred = state->owner->so_cred;
2957 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2958 return -NFS4ERR_BAD_STATEID;
2959 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2960 trace_nfs4_test_open_stateid(state, NULL, status);
2961 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2962 nfs_state_clear_open_state_flags(state);
2963 stateid->type = NFS4_INVALID_STATEID_TYPE;
2966 if (nfs_open_stateid_recover_openmode(state))
2967 return -NFS4ERR_OPENMODE;
2971 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2975 status = nfs41_check_delegation_stateid(state);
2976 if (status != NFS_OK)
2978 nfs41_delegation_recover_stateid(state);
2980 status = nfs41_check_expired_locks(state);
2981 if (status != NFS_OK)
2983 status = nfs41_check_open_stateid(state);
2984 if (status != NFS_OK)
2985 status = nfs4_open_expired(sp, state);
2991 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2992 * fields corresponding to attributes that were used to store the verifier.
2993 * Make sure we clobber those fields in the later setattr call
2995 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2996 struct iattr *sattr, struct nfs4_label **label)
2998 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
3003 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
3004 attrset[i] = opendata->o_res.attrset[i];
3005 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3006 attrset[i] &= ~bitmask[i];
3009 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3010 sattr->ia_valid : 0;
3012 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3013 if (sattr->ia_valid & ATTR_ATIME_SET)
3014 ret |= ATTR_ATIME_SET;
3019 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3020 if (sattr->ia_valid & ATTR_MTIME_SET)
3021 ret |= ATTR_MTIME_SET;
3026 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3031 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3032 int flags, struct nfs_open_context *ctx)
3034 struct nfs4_state_owner *sp = opendata->owner;
3035 struct nfs_server *server = sp->so_server;
3036 struct dentry *dentry;
3037 struct nfs4_state *state;
3038 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3039 struct inode *dir = d_inode(opendata->dir);
3040 unsigned long dir_verifier;
3044 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3045 dir_verifier = nfs_save_change_attribute(dir);
3047 ret = _nfs4_proc_open(opendata, ctx);
3051 state = _nfs4_opendata_to_nfs4_state(opendata);
3052 ret = PTR_ERR(state);
3056 if (server->caps & NFS_CAP_POSIX_LOCK)
3057 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3058 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3059 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3061 dentry = opendata->dentry;
3062 if (d_really_is_negative(dentry)) {
3063 struct dentry *alias;
3065 alias = d_exact_alias(dentry, state->inode);
3067 alias = d_splice_alias(igrab(state->inode), dentry);
3068 /* d_splice_alias() can't fail here - it's a non-directory */
3071 ctx->dentry = dentry = alias;
3075 switch(opendata->o_arg.claim) {
3078 case NFS4_OPEN_CLAIM_NULL:
3079 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3080 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3081 if (!opendata->rpc_done)
3083 if (opendata->o_res.delegation_type != 0)
3084 dir_verifier = nfs_save_change_attribute(dir);
3085 nfs_set_verifier(dentry, dir_verifier);
3088 /* Parse layoutget results before we check for access */
3089 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3091 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3096 if (d_inode(dentry) == state->inode) {
3097 nfs_inode_attach_open_context(ctx);
3098 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3099 nfs4_schedule_stateid_recovery(server, state);
3103 if (!opendata->cancelled)
3104 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3109 * Returns a referenced nfs4_state
3111 static int _nfs4_do_open(struct inode *dir,
3112 struct nfs_open_context *ctx,
3114 const struct nfs4_open_createattrs *c,
3117 struct nfs4_state_owner *sp;
3118 struct nfs4_state *state = NULL;
3119 struct nfs_server *server = NFS_SERVER(dir);
3120 struct nfs4_opendata *opendata;
3121 struct dentry *dentry = ctx->dentry;
3122 const struct cred *cred = ctx->cred;
3123 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3124 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3125 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3126 struct iattr *sattr = c->sattr;
3127 struct nfs4_label *label = c->label;
3128 struct nfs4_label *olabel = NULL;
3131 /* Protect against reboot recovery conflicts */
3133 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3135 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3138 status = nfs4_client_recover_expired_lease(server->nfs_client);
3140 goto err_put_state_owner;
3141 if (d_really_is_positive(dentry))
3142 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3144 if (d_really_is_positive(dentry))
3145 claim = NFS4_OPEN_CLAIM_FH;
3146 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3147 c, claim, GFP_KERNEL);
3148 if (opendata == NULL)
3149 goto err_put_state_owner;
3152 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3153 if (IS_ERR(olabel)) {
3154 status = PTR_ERR(olabel);
3155 goto err_opendata_put;
3159 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3160 if (!opendata->f_attr.mdsthreshold) {
3161 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3162 if (!opendata->f_attr.mdsthreshold)
3163 goto err_free_label;
3165 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3167 if (d_really_is_positive(dentry))
3168 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3170 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3172 goto err_free_label;
3175 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3176 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3177 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3179 * send create attributes which was not set by open
3180 * with an extra setattr.
3182 if (attrs || label) {
3183 unsigned ia_old = sattr->ia_valid;
3185 sattr->ia_valid = attrs;
3186 nfs_fattr_init(opendata->o_res.f_attr);
3187 status = nfs4_do_setattr(state->inode, cred,
3188 opendata->o_res.f_attr, sattr,
3189 ctx, label, olabel);
3191 nfs_setattr_update_inode(state->inode, sattr,
3192 opendata->o_res.f_attr);
3193 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3195 sattr->ia_valid = ia_old;
3198 if (opened && opendata->file_created)
3201 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3202 *ctx_th = opendata->f_attr.mdsthreshold;
3203 opendata->f_attr.mdsthreshold = NULL;
3206 nfs4_label_free(olabel);
3208 nfs4_opendata_put(opendata);
3209 nfs4_put_state_owner(sp);
3212 nfs4_label_free(olabel);
3214 nfs4_opendata_put(opendata);
3215 err_put_state_owner:
3216 nfs4_put_state_owner(sp);
3222 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3223 struct nfs_open_context *ctx,
3225 struct iattr *sattr,
3226 struct nfs4_label *label,
3229 struct nfs_server *server = NFS_SERVER(dir);
3230 struct nfs4_exception exception = {
3231 .interruptible = true,
3233 struct nfs4_state *res;
3234 struct nfs4_open_createattrs c = {
3238 [0] = (__u32)jiffies,
3239 [1] = (__u32)current->pid,
3245 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3247 trace_nfs4_open_file(ctx, flags, status);
3250 /* NOTE: BAD_SEQID means the server and client disagree about the
3251 * book-keeping w.r.t. state-changing operations
3252 * (OPEN/CLOSE/LOCK/LOCKU...)
3253 * It is actually a sign of a bug on the client or on the server.
3255 * If we receive a BAD_SEQID error in the particular case of
3256 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3257 * have unhashed the old state_owner for us, and that we can
3258 * therefore safely retry using a new one. We should still warn
3259 * the user though...
3261 if (status == -NFS4ERR_BAD_SEQID) {
3262 pr_warn_ratelimited("NFS: v4 server %s "
3263 " returned a bad sequence-id error!\n",
3264 NFS_SERVER(dir)->nfs_client->cl_hostname);
3265 exception.retry = 1;
3269 * BAD_STATEID on OPEN means that the server cancelled our
3270 * state before it received the OPEN_CONFIRM.
3271 * Recover by retrying the request as per the discussion
3272 * on Page 181 of RFC3530.
3274 if (status == -NFS4ERR_BAD_STATEID) {
3275 exception.retry = 1;
3278 if (status == -NFS4ERR_EXPIRED) {
3279 nfs4_schedule_lease_recovery(server->nfs_client);
3280 exception.retry = 1;
3283 if (status == -EAGAIN) {
3284 /* We must have found a delegation */
3285 exception.retry = 1;
3288 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3290 res = ERR_PTR(nfs4_handle_exception(server,
3291 status, &exception));
3292 } while (exception.retry);
3296 static int _nfs4_do_setattr(struct inode *inode,
3297 struct nfs_setattrargs *arg,
3298 struct nfs_setattrres *res,
3299 const struct cred *cred,
3300 struct nfs_open_context *ctx)
3302 struct nfs_server *server = NFS_SERVER(inode);
3303 struct rpc_message msg = {
3304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3309 const struct cred *delegation_cred = NULL;
3310 unsigned long timestamp = jiffies;
3314 nfs_fattr_init(res->fattr);
3316 /* Servers should only apply open mode checks for file size changes */
3317 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3319 nfs4_inode_make_writeable(inode);
3323 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3324 /* Use that stateid */
3325 } else if (ctx != NULL && ctx->state) {
3326 struct nfs_lock_context *l_ctx;
3327 if (!nfs4_valid_open_stateid(ctx->state))
3329 l_ctx = nfs_get_lock_context(ctx);
3331 return PTR_ERR(l_ctx);
3332 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3333 &arg->stateid, &delegation_cred);
3334 nfs_put_lock_context(l_ctx);
3337 else if (status == -EAGAIN)
3341 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3343 if (delegation_cred)
3344 msg.rpc_cred = delegation_cred;
3346 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3348 put_cred(delegation_cred);
3349 if (status == 0 && ctx != NULL)
3350 renew_lease(server, timestamp);
3351 trace_nfs4_setattr(inode, &arg->stateid, status);
3355 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3356 struct nfs_fattr *fattr, struct iattr *sattr,
3357 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3358 struct nfs4_label *olabel)
3360 struct nfs_server *server = NFS_SERVER(inode);
3361 __u32 bitmask[NFS4_BITMASK_SZ];
3362 struct nfs4_state *state = ctx ? ctx->state : NULL;
3363 struct nfs_setattrargs arg = {
3364 .fh = NFS_FH(inode),
3370 struct nfs_setattrres res = {
3375 struct nfs4_exception exception = {
3378 .stateid = &arg.stateid,
3380 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
3383 if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3384 adjust_flags |= NFS_INO_INVALID_MODE;
3385 if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
3386 adjust_flags |= NFS_INO_INVALID_OTHER;
3389 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, olabel),
3390 inode, adjust_flags);
3392 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3394 case -NFS4ERR_OPENMODE:
3395 if (!(sattr->ia_valid & ATTR_SIZE)) {
3396 pr_warn_once("NFSv4: server %s is incorrectly "
3397 "applying open mode checks to "
3398 "a SETATTR that is not "
3399 "changing file size.\n",
3400 server->nfs_client->cl_hostname);
3402 if (state && !(state->state & FMODE_WRITE)) {
3404 if (sattr->ia_valid & ATTR_OPEN)
3409 err = nfs4_handle_exception(server, err, &exception);
3410 } while (exception.retry);
3416 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3418 if (inode == NULL || !nfs_have_layout(inode))
3421 return pnfs_wait_on_layoutreturn(inode, task);
3425 * Update the seqid of an open stateid
3427 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3428 struct nfs4_state *state)
3435 if (!nfs4_valid_open_stateid(state))
3437 seq = read_seqbegin(&state->seqlock);
3438 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3439 nfs4_stateid_copy(dst, &state->open_stateid);
3440 if (read_seqretry(&state->seqlock, seq))
3444 seqid_open = state->open_stateid.seqid;
3445 if (read_seqretry(&state->seqlock, seq))
3448 dst_seqid = be32_to_cpu(dst->seqid);
3449 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3450 dst->seqid = seqid_open;
3456 * Update the seqid of an open stateid after receiving
3457 * NFS4ERR_OLD_STATEID
3459 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3460 struct nfs4_state *state)
3465 int seq, status = -EAGAIN;
3470 if (!nfs4_valid_open_stateid(state))
3472 seq = read_seqbegin(&state->seqlock);
3473 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3474 if (read_seqretry(&state->seqlock, seq))
3479 write_seqlock(&state->seqlock);
3480 seqid_open = state->open_stateid.seqid;
3482 dst_seqid = be32_to_cpu(dst->seqid);
3484 /* Did another OPEN bump the state's seqid? try again: */
3485 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3486 dst->seqid = seqid_open;
3487 write_sequnlock(&state->seqlock);
3492 /* server says we're behind but we haven't seen the update yet */
3493 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3494 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3495 write_sequnlock(&state->seqlock);
3496 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3498 if (fatal_signal_pending(current))
3501 if (schedule_timeout(5*HZ) != 0)
3504 finish_wait(&state->waitq, &wait);
3508 if (status == -EINTR)
3511 /* we slept the whole 5 seconds, we must have lost a seqid */
3512 dst->seqid = cpu_to_be32(dst_seqid + 1);
3520 struct nfs4_closedata {
3521 struct inode *inode;
3522 struct nfs4_state *state;
3523 struct nfs_closeargs arg;
3524 struct nfs_closeres res;
3526 struct nfs4_layoutreturn_args arg;
3527 struct nfs4_layoutreturn_res res;
3528 struct nfs4_xdr_opaque_data ld_private;
3532 struct nfs_fattr fattr;
3533 unsigned long timestamp;
3536 static void nfs4_free_closedata(void *data)
3538 struct nfs4_closedata *calldata = data;
3539 struct nfs4_state_owner *sp = calldata->state->owner;
3540 struct super_block *sb = calldata->state->inode->i_sb;
3542 if (calldata->lr.roc)
3543 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3544 calldata->res.lr_ret);
3545 nfs4_put_open_state(calldata->state);
3546 nfs_free_seqid(calldata->arg.seqid);
3547 nfs4_put_state_owner(sp);
3548 nfs_sb_deactive(sb);
3552 static void nfs4_close_done(struct rpc_task *task, void *data)
3554 struct nfs4_closedata *calldata = data;
3555 struct nfs4_state *state = calldata->state;
3556 struct nfs_server *server = NFS_SERVER(calldata->inode);
3557 nfs4_stateid *res_stateid = NULL;
3558 struct nfs4_exception exception = {
3560 .inode = calldata->inode,
3561 .stateid = &calldata->arg.stateid,
3564 dprintk("%s: begin!\n", __func__);
3565 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3567 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3569 /* Handle Layoutreturn errors */
3570 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3571 &calldata->res.lr_ret) == -EAGAIN)
3574 /* hmm. we are done with the inode, and in the process of freeing
3575 * the state_owner. we keep this around to process errors
3577 switch (task->tk_status) {
3579 res_stateid = &calldata->res.stateid;
3580 renew_lease(server, calldata->timestamp);
3582 case -NFS4ERR_ACCESS:
3583 if (calldata->arg.bitmask != NULL) {
3584 calldata->arg.bitmask = NULL;
3585 calldata->res.fattr = NULL;
3590 case -NFS4ERR_OLD_STATEID:
3591 /* Did we race with OPEN? */
3592 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3596 case -NFS4ERR_ADMIN_REVOKED:
3597 case -NFS4ERR_STALE_STATEID:
3598 case -NFS4ERR_EXPIRED:
3599 nfs4_free_revoked_stateid(server,
3600 &calldata->arg.stateid,
3601 task->tk_msg.rpc_cred);
3603 case -NFS4ERR_BAD_STATEID:
3604 if (calldata->arg.fmode == 0)
3608 task->tk_status = nfs4_async_handle_exception(task,
3609 server, task->tk_status, &exception);
3610 if (exception.retry)
3613 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3614 res_stateid, calldata->arg.fmode);
3616 task->tk_status = 0;
3617 nfs_release_seqid(calldata->arg.seqid);
3618 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3619 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3622 task->tk_status = 0;
3623 rpc_restart_call_prepare(task);
3627 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3629 struct nfs4_closedata *calldata = data;
3630 struct nfs4_state *state = calldata->state;
3631 struct inode *inode = calldata->inode;
3632 struct nfs_server *server = NFS_SERVER(inode);
3633 struct pnfs_layout_hdr *lo;
3634 bool is_rdonly, is_wronly, is_rdwr;
3637 dprintk("%s: begin!\n", __func__);
3638 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3641 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3642 spin_lock(&state->owner->so_lock);
3643 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3644 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3645 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3646 /* Calculate the change in open mode */
3647 calldata->arg.fmode = 0;
3648 if (state->n_rdwr == 0) {
3649 if (state->n_rdonly == 0)
3650 call_close |= is_rdonly;
3652 calldata->arg.fmode |= FMODE_READ;
3653 if (state->n_wronly == 0)
3654 call_close |= is_wronly;
3656 calldata->arg.fmode |= FMODE_WRITE;
3657 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3658 call_close |= is_rdwr;
3660 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3662 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3663 if (!nfs4_valid_open_stateid(state))
3665 spin_unlock(&state->owner->so_lock);
3668 /* Note: exit _without_ calling nfs4_close_done */
3672 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3673 nfs_release_seqid(calldata->arg.seqid);
3677 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3678 if (lo && !pnfs_layout_is_valid(lo)) {
3679 calldata->arg.lr_args = NULL;
3680 calldata->res.lr_res = NULL;
3683 if (calldata->arg.fmode == 0)
3684 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3686 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3687 /* Close-to-open cache consistency revalidation */
3688 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3689 nfs4_bitmask_set(calldata->arg.bitmask_store,
3690 server->cache_consistency_bitmask,
3691 inode, server, NULL);
3692 calldata->arg.bitmask = calldata->arg.bitmask_store;
3694 calldata->arg.bitmask = NULL;
3697 calldata->arg.share_access =
3698 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3699 calldata->arg.fmode, 0);
3701 if (calldata->res.fattr == NULL)
3702 calldata->arg.bitmask = NULL;
3703 else if (calldata->arg.bitmask == NULL)
3704 calldata->res.fattr = NULL;
3705 calldata->timestamp = jiffies;
3706 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3707 &calldata->arg.seq_args,
3708 &calldata->res.seq_res,
3710 nfs_release_seqid(calldata->arg.seqid);
3711 dprintk("%s: done!\n", __func__);
3714 task->tk_action = NULL;
3716 nfs4_sequence_done(task, &calldata->res.seq_res);
3719 static const struct rpc_call_ops nfs4_close_ops = {
3720 .rpc_call_prepare = nfs4_close_prepare,
3721 .rpc_call_done = nfs4_close_done,
3722 .rpc_release = nfs4_free_closedata,
3726 * It is possible for data to be read/written from a mem-mapped file
3727 * after the sys_close call (which hits the vfs layer as a flush).
3728 * This means that we can't safely call nfsv4 close on a file until
3729 * the inode is cleared. This in turn means that we are not good
3730 * NFSv4 citizens - we do not indicate to the server to update the file's
3731 * share state even when we are done with one of the three share
3732 * stateid's in the inode.
3734 * NOTE: Caller must be holding the sp->so_owner semaphore!
3736 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3738 struct nfs_server *server = NFS_SERVER(state->inode);
3739 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3740 struct nfs4_closedata *calldata;
3741 struct nfs4_state_owner *sp = state->owner;
3742 struct rpc_task *task;
3743 struct rpc_message msg = {
3744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3745 .rpc_cred = state->owner->so_cred,
3747 struct rpc_task_setup task_setup_data = {
3748 .rpc_client = server->client,
3749 .rpc_message = &msg,
3750 .callback_ops = &nfs4_close_ops,
3751 .workqueue = nfsiod_workqueue,
3752 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3754 int status = -ENOMEM;
3756 if (server->nfs_client->cl_minorversion)
3757 task_setup_data.flags |= RPC_TASK_MOVEABLE;
3759 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3760 &task_setup_data.rpc_client, &msg);
3762 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3763 if (calldata == NULL)
3765 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3766 calldata->inode = state->inode;
3767 calldata->state = state;
3768 calldata->arg.fh = NFS_FH(state->inode);
3769 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3770 goto out_free_calldata;
3771 /* Serialization for the sequence id */
3772 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3773 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3774 if (IS_ERR(calldata->arg.seqid))
3775 goto out_free_calldata;
3776 nfs_fattr_init(&calldata->fattr);
3777 calldata->arg.fmode = 0;
3778 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3779 calldata->res.fattr = &calldata->fattr;
3780 calldata->res.seqid = calldata->arg.seqid;
3781 calldata->res.server = server;
3782 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3783 calldata->lr.roc = pnfs_roc(state->inode,
3784 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3785 if (calldata->lr.roc) {
3786 calldata->arg.lr_args = &calldata->lr.arg;
3787 calldata->res.lr_res = &calldata->lr.res;
3789 nfs_sb_active(calldata->inode->i_sb);
3791 msg.rpc_argp = &calldata->arg;
3792 msg.rpc_resp = &calldata->res;
3793 task_setup_data.callback_data = calldata;
3794 task = rpc_run_task(&task_setup_data);
3796 return PTR_ERR(task);
3799 status = rpc_wait_for_completion_task(task);
3805 nfs4_put_open_state(state);
3806 nfs4_put_state_owner(sp);
3810 static struct inode *
3811 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3812 int open_flags, struct iattr *attr, int *opened)
3814 struct nfs4_state *state;
3815 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3817 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3819 /* Protect against concurrent sillydeletes */
3820 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3822 nfs4_label_release_security(label);
3825 return ERR_CAST(state);
3826 return state->inode;
3829 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3831 if (ctx->state == NULL)
3834 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3836 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3839 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3840 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3841 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3843 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3845 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3846 struct nfs4_server_caps_arg args = {
3850 struct nfs4_server_caps_res res = {};
3851 struct rpc_message msg = {
3852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3859 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3860 FATTR4_WORD0_FH_EXPIRE_TYPE |
3861 FATTR4_WORD0_LINK_SUPPORT |
3862 FATTR4_WORD0_SYMLINK_SUPPORT |
3863 FATTR4_WORD0_ACLSUPPORT;
3865 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3867 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3869 /* Sanity check the server answers */
3870 switch (minorversion) {
3872 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3873 res.attr_bitmask[2] = 0;
3876 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3879 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3881 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3882 server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3883 NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3884 server->fattr_valid = NFS_ATTR_FATTR_V4;
3885 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3886 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3887 server->caps |= NFS_CAP_ACLS;
3888 if (res.has_links != 0)
3889 server->caps |= NFS_CAP_HARDLINKS;
3890 if (res.has_symlinks != 0)
3891 server->caps |= NFS_CAP_SYMLINKS;
3892 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3893 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3894 server->caps |= NFS_CAP_SECURITY_LABEL;
3896 if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3897 server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3898 if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3899 server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3900 if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3901 server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3902 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3903 server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3904 NFS_ATTR_FATTR_OWNER_NAME);
3905 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3906 server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3907 NFS_ATTR_FATTR_GROUP_NAME);
3908 if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3909 server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3910 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3911 server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3912 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3913 server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3914 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3915 server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
3916 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3917 sizeof(server->attr_bitmask));
3918 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3920 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3921 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3922 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3923 server->cache_consistency_bitmask[2] = 0;
3925 /* Avoid a regression due to buggy server */
3926 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3927 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3928 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3929 sizeof(server->exclcreat_bitmask));
3931 server->acl_bitmask = res.acl_bitmask;
3932 server->fh_expire_type = res.fh_expire_type;
3938 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3940 struct nfs4_exception exception = {
3941 .interruptible = true,
3945 err = nfs4_handle_exception(server,
3946 _nfs4_server_capabilities(server, fhandle),
3948 } while (exception.retry);
3952 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3953 struct nfs_fsinfo *info)
3956 struct nfs4_lookup_root_arg args = {
3959 struct nfs4_lookup_res res = {
3961 .fattr = info->fattr,
3964 struct rpc_message msg = {
3965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3970 bitmask[0] = nfs4_fattr_bitmap[0];
3971 bitmask[1] = nfs4_fattr_bitmap[1];
3973 * Process the label in the upcoming getfattr
3975 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3977 nfs_fattr_init(info->fattr);
3978 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3981 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3982 struct nfs_fsinfo *info)
3984 struct nfs4_exception exception = {
3985 .interruptible = true,
3989 err = _nfs4_lookup_root(server, fhandle, info);
3990 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3993 case -NFS4ERR_WRONGSEC:
3996 err = nfs4_handle_exception(server, err, &exception);
3998 } while (exception.retry);
4003 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4004 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4006 struct rpc_auth_create_args auth_args = {
4007 .pseudoflavor = flavor,
4009 struct rpc_auth *auth;
4011 auth = rpcauth_create(&auth_args, server->client);
4014 return nfs4_lookup_root(server, fhandle, info);
4018 * Retry pseudoroot lookup with various security flavors. We do this when:
4020 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4021 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4023 * Returns zero on success, or a negative NFS4ERR value, or a
4024 * negative errno value.
4026 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4027 struct nfs_fsinfo *info)
4029 /* Per 3530bis 15.33.5 */
4030 static const rpc_authflavor_t flav_array[] = {
4034 RPC_AUTH_UNIX, /* courtesy */
4037 int status = -EPERM;
4040 if (server->auth_info.flavor_len > 0) {
4041 /* try each flavor specified by user */
4042 for (i = 0; i < server->auth_info.flavor_len; i++) {
4043 status = nfs4_lookup_root_sec(server, fhandle, info,
4044 server->auth_info.flavors[i]);
4045 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4050 /* no flavors specified by user, try default list */
4051 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4052 status = nfs4_lookup_root_sec(server, fhandle, info,
4054 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4061 * -EACCES could mean that the user doesn't have correct permissions
4062 * to access the mount. It could also mean that we tried to mount
4063 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4064 * existing mount programs don't handle -EACCES very well so it should
4065 * be mapped to -EPERM instead.
4067 if (status == -EACCES)
4073 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4074 * @server: initialized nfs_server handle
4075 * @fhandle: we fill in the pseudo-fs root file handle
4076 * @info: we fill in an FSINFO struct
4077 * @auth_probe: probe the auth flavours
4079 * Returns zero on success, or a negative errno.
4081 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4082 struct nfs_fsinfo *info,
4088 status = nfs4_lookup_root(server, fhandle, info);
4090 if (auth_probe || status == NFS4ERR_WRONGSEC)
4091 status = server->nfs_client->cl_mvops->find_root_sec(server,
4095 status = nfs4_server_capabilities(server, fhandle);
4097 status = nfs4_do_fsinfo(server, fhandle, info);
4099 return nfs4_map_errors(status);
4102 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4103 struct nfs_fsinfo *info)
4106 struct nfs_fattr *fattr = info->fattr;
4107 struct nfs4_label *label = fattr->label;
4109 error = nfs4_server_capabilities(server, mntfh);
4111 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4115 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
4117 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4121 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4122 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4123 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4130 * Get locations and (maybe) other attributes of a referral.
4131 * Note that we'll actually follow the referral later when
4132 * we detect fsid mismatch in inode revalidation
4134 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4135 const struct qstr *name, struct nfs_fattr *fattr,
4136 struct nfs_fh *fhandle)
4138 int status = -ENOMEM;
4139 struct page *page = NULL;
4140 struct nfs4_fs_locations *locations = NULL;
4142 page = alloc_page(GFP_KERNEL);
4145 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4146 if (locations == NULL)
4149 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4154 * If the fsid didn't change, this is a migration event, not a
4155 * referral. Cause us to drop into the exception handler, which
4156 * will kick off migration recovery.
4158 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4159 dprintk("%s: server did not return a different fsid for"
4160 " a referral at %s\n", __func__, name->name);
4161 status = -NFS4ERR_MOVED;
4164 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4165 nfs_fixup_referral_attributes(&locations->fattr);
4167 /* replace the lookup nfs_fattr with the locations nfs_fattr */
4168 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4169 memset(fhandle, 0, sizeof(struct nfs_fh));
4177 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4178 struct nfs_fattr *fattr, struct nfs4_label *label,
4179 struct inode *inode)
4181 __u32 bitmask[NFS4_BITMASK_SZ];
4182 struct nfs4_getattr_arg args = {
4186 struct nfs4_getattr_res res = {
4191 struct rpc_message msg = {
4192 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4196 unsigned short task_flags = 0;
4198 if (nfs4_has_session(server->nfs_client))
4199 task_flags = RPC_TASK_MOVEABLE;
4201 /* Is this is an attribute revalidation, subject to softreval? */
4202 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4203 task_flags |= RPC_TASK_TIMEOUT;
4205 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode, 0);
4206 nfs_fattr_init(fattr);
4207 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4208 return nfs4_do_call_sync(server->client, server, &msg,
4209 &args.seq_args, &res.seq_res, task_flags);
4212 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4213 struct nfs_fattr *fattr, struct nfs4_label *label,
4214 struct inode *inode)
4216 struct nfs4_exception exception = {
4217 .interruptible = true,
4221 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4222 trace_nfs4_getattr(server, fhandle, fattr, err);
4223 err = nfs4_handle_exception(server, err,
4225 } while (exception.retry);
4230 * The file is not closed if it is opened due to the a request to change
4231 * the size of the file. The open call will not be needed once the
4232 * VFS layer lookup-intents are implemented.
4234 * Close is called when the inode is destroyed.
4235 * If we haven't opened the file for O_WRONLY, we
4236 * need to in the size_change case to obtain a stateid.
4239 * Because OPEN is always done by name in nfsv4, it is
4240 * possible that we opened a different file by the same
4241 * name. We can recognize this race condition, but we
4242 * can't do anything about it besides returning an error.
4244 * This will be fixed with VFS changes (lookup-intent).
4247 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4248 struct iattr *sattr)
4250 struct inode *inode = d_inode(dentry);
4251 const struct cred *cred = NULL;
4252 struct nfs_open_context *ctx = NULL;
4253 struct nfs4_label *label = NULL;
4256 if (pnfs_ld_layoutret_on_setattr(inode) &&
4257 sattr->ia_valid & ATTR_SIZE &&
4258 sattr->ia_size < i_size_read(inode))
4259 pnfs_commit_and_return_layout(inode);
4261 nfs_fattr_init(fattr);
4263 /* Deal with open(O_TRUNC) */
4264 if (sattr->ia_valid & ATTR_OPEN)
4265 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4267 /* Optimization: if the end result is no change, don't RPC */
4268 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4271 /* Search for an existing open(O_WRITE) file */
4272 if (sattr->ia_valid & ATTR_FILE) {
4274 ctx = nfs_file_open_context(sattr->ia_file);
4279 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4281 return PTR_ERR(label);
4283 /* Return any delegations if we're going to change ACLs */
4284 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4285 nfs4_inode_make_writeable(inode);
4287 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4289 nfs_setattr_update_inode(inode, sattr, fattr);
4290 nfs_setsecurity(inode, fattr, label);
4292 nfs4_label_free(label);
4296 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4297 struct dentry *dentry, struct nfs_fh *fhandle,
4298 struct nfs_fattr *fattr, struct nfs4_label *label)
4300 struct nfs_server *server = NFS_SERVER(dir);
4302 struct nfs4_lookup_arg args = {
4303 .bitmask = server->attr_bitmask,
4304 .dir_fh = NFS_FH(dir),
4305 .name = &dentry->d_name,
4307 struct nfs4_lookup_res res = {
4313 struct rpc_message msg = {
4314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4318 unsigned short task_flags = 0;
4320 if (server->nfs_client->cl_minorversion)
4321 task_flags = RPC_TASK_MOVEABLE;
4323 /* Is this is an attribute revalidation, subject to softreval? */
4324 if (nfs_lookup_is_soft_revalidate(dentry))
4325 task_flags |= RPC_TASK_TIMEOUT;
4327 args.bitmask = nfs4_bitmask(server, label);
4329 nfs_fattr_init(fattr);
4331 dprintk("NFS call lookup %pd2\n", dentry);
4332 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4333 status = nfs4_do_call_sync(clnt, server, &msg,
4334 &args.seq_args, &res.seq_res, task_flags);
4335 dprintk("NFS reply lookup: %d\n", status);
4339 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4341 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4342 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4343 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4347 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4348 struct dentry *dentry, struct nfs_fh *fhandle,
4349 struct nfs_fattr *fattr, struct nfs4_label *label)
4351 struct nfs4_exception exception = {
4352 .interruptible = true,
4354 struct rpc_clnt *client = *clnt;
4355 const struct qstr *name = &dentry->d_name;
4358 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
4359 trace_nfs4_lookup(dir, name, err);
4361 case -NFS4ERR_BADNAME:
4364 case -NFS4ERR_MOVED:
4365 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4366 if (err == -NFS4ERR_MOVED)
4367 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4369 case -NFS4ERR_WRONGSEC:
4371 if (client != *clnt)
4373 client = nfs4_negotiate_security(client, dir, name);
4375 return PTR_ERR(client);
4377 exception.retry = 1;
4380 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4382 } while (exception.retry);
4387 else if (client != *clnt)
4388 rpc_shutdown_client(client);
4393 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4394 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4395 struct nfs4_label *label)
4398 struct rpc_clnt *client = NFS_CLIENT(dir);
4400 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
4401 if (client != NFS_CLIENT(dir)) {
4402 rpc_shutdown_client(client);
4403 nfs_fixup_secinfo_attributes(fattr);
4409 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4410 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4412 struct rpc_clnt *client = NFS_CLIENT(dir);
4415 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
4417 return ERR_PTR(status);
4418 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4421 static int _nfs4_proc_lookupp(struct inode *inode,
4422 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4423 struct nfs4_label *label)
4425 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4426 struct nfs_server *server = NFS_SERVER(inode);
4428 struct nfs4_lookupp_arg args = {
4429 .bitmask = server->attr_bitmask,
4430 .fh = NFS_FH(inode),
4432 struct nfs4_lookupp_res res = {
4438 struct rpc_message msg = {
4439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4443 unsigned short task_flags = 0;
4445 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4446 task_flags |= RPC_TASK_TIMEOUT;
4448 args.bitmask = nfs4_bitmask(server, label);
4450 nfs_fattr_init(fattr);
4452 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4453 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4454 &res.seq_res, task_flags);
4455 dprintk("NFS reply lookupp: %d\n", status);
4459 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4460 struct nfs_fattr *fattr, struct nfs4_label *label)
4462 struct nfs4_exception exception = {
4463 .interruptible = true,
4467 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4468 trace_nfs4_lookupp(inode, err);
4469 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4471 } while (exception.retry);
4475 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4477 struct nfs_server *server = NFS_SERVER(inode);
4478 struct nfs4_accessargs args = {
4479 .fh = NFS_FH(inode),
4480 .access = entry->mask,
4482 struct nfs4_accessres res = {
4485 struct rpc_message msg = {
4486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4489 .rpc_cred = entry->cred,
4493 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4494 res.fattr = nfs_alloc_fattr();
4495 if (res.fattr == NULL)
4497 args.bitmask = server->cache_consistency_bitmask;
4499 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4501 nfs_access_set_mask(entry, res.access);
4503 nfs_refresh_inode(inode, res.fattr);
4505 nfs_free_fattr(res.fattr);
4509 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4511 struct nfs4_exception exception = {
4512 .interruptible = true,
4516 err = _nfs4_proc_access(inode, entry);
4517 trace_nfs4_access(inode, err);
4518 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4520 } while (exception.retry);
4525 * TODO: For the time being, we don't try to get any attributes
4526 * along with any of the zero-copy operations READ, READDIR,
4529 * In the case of the first three, we want to put the GETATTR
4530 * after the read-type operation -- this is because it is hard
4531 * to predict the length of a GETATTR response in v4, and thus
4532 * align the READ data correctly. This means that the GETATTR
4533 * may end up partially falling into the page cache, and we should
4534 * shift it into the 'tail' of the xdr_buf before processing.
4535 * To do this efficiently, we need to know the total length
4536 * of data received, which doesn't seem to be available outside
4539 * In the case of WRITE, we also want to put the GETATTR after
4540 * the operation -- in this case because we want to make sure
4541 * we get the post-operation mtime and size.
4543 * Both of these changes to the XDR layer would in fact be quite
4544 * minor, but I decided to leave them for a subsequent patch.
4546 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4547 unsigned int pgbase, unsigned int pglen)
4549 struct nfs4_readlink args = {
4550 .fh = NFS_FH(inode),
4555 struct nfs4_readlink_res res;
4556 struct rpc_message msg = {
4557 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4562 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4565 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4566 unsigned int pgbase, unsigned int pglen)
4568 struct nfs4_exception exception = {
4569 .interruptible = true,
4573 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4574 trace_nfs4_readlink(inode, err);
4575 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4577 } while (exception.retry);
4582 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4585 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4588 struct nfs_server *server = NFS_SERVER(dir);
4589 struct nfs4_label l, *ilabel = NULL;
4590 struct nfs_open_context *ctx;
4591 struct nfs4_state *state;
4594 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4596 return PTR_ERR(ctx);
4598 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4600 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4601 sattr->ia_mode &= ~current_umask();
4602 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4603 if (IS_ERR(state)) {
4604 status = PTR_ERR(state);
4608 nfs4_label_release_security(ilabel);
4609 put_nfs_open_context(ctx);
4614 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4616 struct nfs_server *server = NFS_SERVER(dir);
4617 struct nfs_removeargs args = {
4621 struct nfs_removeres res = {
4624 struct rpc_message msg = {
4625 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4629 unsigned long timestamp = jiffies;
4632 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4634 spin_lock(&dir->i_lock);
4635 /* Removing a directory decrements nlink in the parent */
4636 if (ftype == NF4DIR && dir->i_nlink > 2)
4637 nfs4_dec_nlink_locked(dir);
4638 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4639 NFS_INO_INVALID_DATA);
4640 spin_unlock(&dir->i_lock);
4645 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4647 struct nfs4_exception exception = {
4648 .interruptible = true,
4650 struct inode *inode = d_inode(dentry);
4654 if (inode->i_nlink == 1)
4655 nfs4_inode_return_delegation(inode);
4657 nfs4_inode_make_writeable(inode);
4660 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4661 trace_nfs4_remove(dir, &dentry->d_name, err);
4662 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4664 } while (exception.retry);
4668 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4670 struct nfs4_exception exception = {
4671 .interruptible = true,
4676 err = _nfs4_proc_remove(dir, name, NF4DIR);
4677 trace_nfs4_remove(dir, name, err);
4678 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4680 } while (exception.retry);
4684 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4685 struct dentry *dentry,
4686 struct inode *inode)
4688 struct nfs_removeargs *args = msg->rpc_argp;
4689 struct nfs_removeres *res = msg->rpc_resp;
4691 res->server = NFS_SB(dentry->d_sb);
4692 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4693 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4695 nfs_fattr_init(res->dir_attr);
4698 nfs4_inode_return_delegation(inode);
4701 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4703 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4704 &data->args.seq_args,
4709 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4711 struct nfs_unlinkdata *data = task->tk_calldata;
4712 struct nfs_removeres *res = &data->res;
4714 if (!nfs4_sequence_done(task, &res->seq_res))
4716 if (nfs4_async_handle_error(task, res->server, NULL,
4717 &data->timeout) == -EAGAIN)
4719 if (task->tk_status == 0)
4720 nfs4_update_changeattr(dir, &res->cinfo,
4721 res->dir_attr->time_start,
4722 NFS_INO_INVALID_DATA);
4726 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4727 struct dentry *old_dentry,
4728 struct dentry *new_dentry)
4730 struct nfs_renameargs *arg = msg->rpc_argp;
4731 struct nfs_renameres *res = msg->rpc_resp;
4732 struct inode *old_inode = d_inode(old_dentry);
4733 struct inode *new_inode = d_inode(new_dentry);
4736 nfs4_inode_make_writeable(old_inode);
4738 nfs4_inode_return_delegation(new_inode);
4739 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4740 res->server = NFS_SB(old_dentry->d_sb);
4741 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4744 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4746 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4747 &data->args.seq_args,
4752 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4753 struct inode *new_dir)
4755 struct nfs_renamedata *data = task->tk_calldata;
4756 struct nfs_renameres *res = &data->res;
4758 if (!nfs4_sequence_done(task, &res->seq_res))
4760 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4763 if (task->tk_status == 0) {
4764 if (new_dir != old_dir) {
4765 /* Note: If we moved a directory, nlink will change */
4766 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4767 res->old_fattr->time_start,
4768 NFS_INO_INVALID_NLINK |
4769 NFS_INO_INVALID_DATA);
4770 nfs4_update_changeattr(new_dir, &res->new_cinfo,
4771 res->new_fattr->time_start,
4772 NFS_INO_INVALID_NLINK |
4773 NFS_INO_INVALID_DATA);
4775 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4776 res->old_fattr->time_start,
4777 NFS_INO_INVALID_DATA);
4782 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4784 struct nfs_server *server = NFS_SERVER(inode);
4785 __u32 bitmask[NFS4_BITMASK_SZ];
4786 struct nfs4_link_arg arg = {
4787 .fh = NFS_FH(inode),
4788 .dir_fh = NFS_FH(dir),
4792 struct nfs4_link_res res = {
4796 struct rpc_message msg = {
4797 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4801 int status = -ENOMEM;
4803 res.fattr = nfs_alloc_fattr();
4804 if (res.fattr == NULL)
4807 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4808 if (IS_ERR(res.label)) {
4809 status = PTR_ERR(res.label);
4813 nfs4_inode_make_writeable(inode);
4814 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.label), inode,
4815 NFS_INO_INVALID_CHANGE);
4816 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4818 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4819 NFS_INO_INVALID_DATA);
4820 nfs4_inc_nlink(inode);
4821 status = nfs_post_op_update_inode(inode, res.fattr);
4823 nfs_setsecurity(inode, res.fattr, res.label);
4827 nfs4_label_free(res.label);
4830 nfs_free_fattr(res.fattr);
4834 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4836 struct nfs4_exception exception = {
4837 .interruptible = true,
4841 err = nfs4_handle_exception(NFS_SERVER(inode),
4842 _nfs4_proc_link(inode, dir, name),
4844 } while (exception.retry);
4848 struct nfs4_createdata {
4849 struct rpc_message msg;
4850 struct nfs4_create_arg arg;
4851 struct nfs4_create_res res;
4853 struct nfs_fattr fattr;
4854 struct nfs4_label *label;
4857 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4858 const struct qstr *name, struct iattr *sattr, u32 ftype)
4860 struct nfs4_createdata *data;
4862 data = kzalloc(sizeof(*data), GFP_KERNEL);
4864 struct nfs_server *server = NFS_SERVER(dir);
4866 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4867 if (IS_ERR(data->label))
4870 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4871 data->msg.rpc_argp = &data->arg;
4872 data->msg.rpc_resp = &data->res;
4873 data->arg.dir_fh = NFS_FH(dir);
4874 data->arg.server = server;
4875 data->arg.name = name;
4876 data->arg.attrs = sattr;
4877 data->arg.ftype = ftype;
4878 data->arg.bitmask = nfs4_bitmask(server, data->label);
4879 data->arg.umask = current_umask();
4880 data->res.server = server;
4881 data->res.fh = &data->fh;
4882 data->res.fattr = &data->fattr;
4883 data->res.label = data->label;
4884 nfs_fattr_init(data->res.fattr);
4892 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4894 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4895 &data->arg.seq_args, &data->res.seq_res, 1);
4897 spin_lock(&dir->i_lock);
4898 /* Creating a directory bumps nlink in the parent */
4899 if (data->arg.ftype == NF4DIR)
4900 nfs4_inc_nlink_locked(dir);
4901 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4902 data->res.fattr->time_start,
4903 NFS_INO_INVALID_DATA);
4904 spin_unlock(&dir->i_lock);
4905 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4910 static void nfs4_free_createdata(struct nfs4_createdata *data)
4912 nfs4_label_free(data->label);
4916 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4917 struct page *page, unsigned int len, struct iattr *sattr,
4918 struct nfs4_label *label)
4920 struct nfs4_createdata *data;
4921 int status = -ENAMETOOLONG;
4923 if (len > NFS4_MAXPATHLEN)
4927 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4931 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4932 data->arg.u.symlink.pages = &page;
4933 data->arg.u.symlink.len = len;
4934 data->arg.label = label;
4936 status = nfs4_do_create(dir, dentry, data);
4938 nfs4_free_createdata(data);
4943 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4944 struct page *page, unsigned int len, struct iattr *sattr)
4946 struct nfs4_exception exception = {
4947 .interruptible = true,
4949 struct nfs4_label l, *label = NULL;
4952 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4955 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4956 trace_nfs4_symlink(dir, &dentry->d_name, err);
4957 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4959 } while (exception.retry);
4961 nfs4_label_release_security(label);
4965 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4966 struct iattr *sattr, struct nfs4_label *label)
4968 struct nfs4_createdata *data;
4969 int status = -ENOMEM;
4971 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4975 data->arg.label = label;
4976 status = nfs4_do_create(dir, dentry, data);
4978 nfs4_free_createdata(data);
4983 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4984 struct iattr *sattr)
4986 struct nfs_server *server = NFS_SERVER(dir);
4987 struct nfs4_exception exception = {
4988 .interruptible = true,
4990 struct nfs4_label l, *label = NULL;
4993 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4995 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4996 sattr->ia_mode &= ~current_umask();
4998 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4999 trace_nfs4_mkdir(dir, &dentry->d_name, err);
5000 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5002 } while (exception.retry);
5003 nfs4_label_release_security(label);
5008 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5009 struct nfs_readdir_res *nr_res)
5011 struct inode *dir = d_inode(nr_arg->dentry);
5012 struct nfs_server *server = NFS_SERVER(dir);
5013 struct nfs4_readdir_arg args = {
5015 .pages = nr_arg->pages,
5017 .count = nr_arg->page_len,
5018 .plus = nr_arg->plus,
5020 struct nfs4_readdir_res res;
5021 struct rpc_message msg = {
5022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5025 .rpc_cred = nr_arg->cred,
5029 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5030 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5031 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5032 args.bitmask = server->attr_bitmask_nl;
5034 args.bitmask = server->attr_bitmask;
5036 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5037 res.pgbase = args.pgbase;
5038 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5041 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5042 status += args.pgbase;
5045 nfs_invalidate_atime(dir);
5047 dprintk("%s: returns %d\n", __func__, status);
5051 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5052 struct nfs_readdir_res *res)
5054 struct nfs4_exception exception = {
5055 .interruptible = true,
5059 err = _nfs4_proc_readdir(arg, res);
5060 trace_nfs4_readdir(d_inode(arg->dentry), err);
5061 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5063 } while (exception.retry);
5067 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5068 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5070 struct nfs4_createdata *data;
5071 int mode = sattr->ia_mode;
5072 int status = -ENOMEM;
5074 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5079 data->arg.ftype = NF4FIFO;
5080 else if (S_ISBLK(mode)) {
5081 data->arg.ftype = NF4BLK;
5082 data->arg.u.device.specdata1 = MAJOR(rdev);
5083 data->arg.u.device.specdata2 = MINOR(rdev);
5085 else if (S_ISCHR(mode)) {
5086 data->arg.ftype = NF4CHR;
5087 data->arg.u.device.specdata1 = MAJOR(rdev);
5088 data->arg.u.device.specdata2 = MINOR(rdev);
5089 } else if (!S_ISSOCK(mode)) {
5094 data->arg.label = label;
5095 status = nfs4_do_create(dir, dentry, data);
5097 nfs4_free_createdata(data);
5102 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5103 struct iattr *sattr, dev_t rdev)
5105 struct nfs_server *server = NFS_SERVER(dir);
5106 struct nfs4_exception exception = {
5107 .interruptible = true,
5109 struct nfs4_label l, *label = NULL;
5112 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5114 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5115 sattr->ia_mode &= ~current_umask();
5117 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5118 trace_nfs4_mknod(dir, &dentry->d_name, err);
5119 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5121 } while (exception.retry);
5123 nfs4_label_release_security(label);
5128 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5129 struct nfs_fsstat *fsstat)
5131 struct nfs4_statfs_arg args = {
5133 .bitmask = server->attr_bitmask,
5135 struct nfs4_statfs_res res = {
5138 struct rpc_message msg = {
5139 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5144 nfs_fattr_init(fsstat->fattr);
5145 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5148 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5150 struct nfs4_exception exception = {
5151 .interruptible = true,
5155 err = nfs4_handle_exception(server,
5156 _nfs4_proc_statfs(server, fhandle, fsstat),
5158 } while (exception.retry);
5162 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5163 struct nfs_fsinfo *fsinfo)
5165 struct nfs4_fsinfo_arg args = {
5167 .bitmask = server->attr_bitmask,
5169 struct nfs4_fsinfo_res res = {
5172 struct rpc_message msg = {
5173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5178 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5181 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5183 struct nfs4_exception exception = {
5184 .interruptible = true,
5189 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5190 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5192 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5195 err = nfs4_handle_exception(server, err, &exception);
5196 } while (exception.retry);
5200 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5204 nfs_fattr_init(fsinfo->fattr);
5205 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5207 /* block layout checks this! */
5208 server->pnfs_blksize = fsinfo->blksize;
5209 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5215 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5216 struct nfs_pathconf *pathconf)
5218 struct nfs4_pathconf_arg args = {
5220 .bitmask = server->attr_bitmask,
5222 struct nfs4_pathconf_res res = {
5223 .pathconf = pathconf,
5225 struct rpc_message msg = {
5226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5231 /* None of the pathconf attributes are mandatory to implement */
5232 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5233 memset(pathconf, 0, sizeof(*pathconf));
5237 nfs_fattr_init(pathconf->fattr);
5238 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5241 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5242 struct nfs_pathconf *pathconf)
5244 struct nfs4_exception exception = {
5245 .interruptible = true,
5250 err = nfs4_handle_exception(server,
5251 _nfs4_proc_pathconf(server, fhandle, pathconf),
5253 } while (exception.retry);
5257 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5258 const struct nfs_open_context *ctx,
5259 const struct nfs_lock_context *l_ctx,
5262 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5264 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5266 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5267 const struct nfs_open_context *ctx,
5268 const struct nfs_lock_context *l_ctx,
5271 nfs4_stateid _current_stateid;
5273 /* If the current stateid represents a lost lock, then exit */
5274 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5276 return nfs4_stateid_match(stateid, &_current_stateid);
5279 static bool nfs4_error_stateid_expired(int err)
5282 case -NFS4ERR_DELEG_REVOKED:
5283 case -NFS4ERR_ADMIN_REVOKED:
5284 case -NFS4ERR_BAD_STATEID:
5285 case -NFS4ERR_STALE_STATEID:
5286 case -NFS4ERR_OLD_STATEID:
5287 case -NFS4ERR_OPENMODE:
5288 case -NFS4ERR_EXPIRED:
5294 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5296 struct nfs_server *server = NFS_SERVER(hdr->inode);
5298 trace_nfs4_read(hdr, task->tk_status);
5299 if (task->tk_status < 0) {
5300 struct nfs4_exception exception = {
5301 .inode = hdr->inode,
5302 .state = hdr->args.context->state,
5303 .stateid = &hdr->args.stateid,
5305 task->tk_status = nfs4_async_handle_exception(task,
5306 server, task->tk_status, &exception);
5307 if (exception.retry) {
5308 rpc_restart_call_prepare(task);
5313 if (task->tk_status > 0)
5314 renew_lease(server, hdr->timestamp);
5318 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5319 struct nfs_pgio_args *args)
5322 if (!nfs4_error_stateid_expired(task->tk_status) ||
5323 nfs4_stateid_is_current(&args->stateid,
5328 rpc_restart_call_prepare(task);
5332 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5333 struct nfs_pgio_header *hdr)
5335 struct nfs_server *server = NFS_SERVER(hdr->inode);
5336 struct rpc_message *msg = &task->tk_msg;
5338 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5339 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5340 server->caps &= ~NFS_CAP_READ_PLUS;
5341 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5342 rpc_restart_call_prepare(task);
5348 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5350 dprintk("--> %s\n", __func__);
5352 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5354 if (nfs4_read_stateid_changed(task, &hdr->args))
5356 if (nfs4_read_plus_not_supported(task, hdr))
5358 if (task->tk_status > 0)
5359 nfs_invalidate_atime(hdr->inode);
5360 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5361 nfs4_read_done_cb(task, hdr);
5364 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5365 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5366 struct rpc_message *msg)
5368 /* Note: We don't use READ_PLUS with pNFS yet */
5369 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
5370 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5373 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5374 struct rpc_message *msg)
5377 #endif /* CONFIG_NFS_V4_2 */
5379 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5380 struct rpc_message *msg)
5382 hdr->timestamp = jiffies;
5383 if (!hdr->pgio_done_cb)
5384 hdr->pgio_done_cb = nfs4_read_done_cb;
5385 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5386 nfs42_read_plus_support(hdr, msg);
5387 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5390 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5391 struct nfs_pgio_header *hdr)
5393 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5394 &hdr->args.seq_args,
5398 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5399 hdr->args.lock_context,
5400 hdr->rw_mode) == -EIO)
5402 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5407 static int nfs4_write_done_cb(struct rpc_task *task,
5408 struct nfs_pgio_header *hdr)
5410 struct inode *inode = hdr->inode;
5412 trace_nfs4_write(hdr, task->tk_status);
5413 if (task->tk_status < 0) {
5414 struct nfs4_exception exception = {
5415 .inode = hdr->inode,
5416 .state = hdr->args.context->state,
5417 .stateid = &hdr->args.stateid,
5419 task->tk_status = nfs4_async_handle_exception(task,
5420 NFS_SERVER(inode), task->tk_status,
5422 if (exception.retry) {
5423 rpc_restart_call_prepare(task);
5427 if (task->tk_status >= 0) {
5428 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5429 nfs_writeback_update_inode(hdr);
5434 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5435 struct nfs_pgio_args *args)
5438 if (!nfs4_error_stateid_expired(task->tk_status) ||
5439 nfs4_stateid_is_current(&args->stateid,
5444 rpc_restart_call_prepare(task);
5448 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5450 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5452 if (nfs4_write_stateid_changed(task, &hdr->args))
5454 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5455 nfs4_write_done_cb(task, hdr);
5459 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5461 /* Don't request attributes for pNFS or O_DIRECT writes */
5462 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5464 /* Otherwise, request attributes if and only if we don't hold
5467 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5470 static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ], const __u32 *src,
5471 struct inode *inode, struct nfs_server *server,
5472 struct nfs4_label *label)
5474 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
5477 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5479 if (cache_validity & NFS_INO_INVALID_CHANGE)
5480 bitmask[0] |= FATTR4_WORD0_CHANGE;
5481 if (cache_validity & NFS_INO_INVALID_ATIME)
5482 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5483 if (cache_validity & NFS_INO_INVALID_MODE)
5484 bitmask[1] |= FATTR4_WORD1_MODE;
5485 if (cache_validity & NFS_INO_INVALID_OTHER)
5486 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5487 if (cache_validity & NFS_INO_INVALID_NLINK)
5488 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5489 if (label && label->len && cache_validity & NFS_INO_INVALID_LABEL)
5490 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
5491 if (cache_validity & NFS_INO_INVALID_CTIME)
5492 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5493 if (cache_validity & NFS_INO_INVALID_MTIME)
5494 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5495 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5496 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5498 if (cache_validity & NFS_INO_INVALID_SIZE)
5499 bitmask[0] |= FATTR4_WORD0_SIZE;
5501 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5502 bitmask[i] &= server->attr_bitmask[i];
5505 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5506 struct rpc_message *msg,
5507 struct rpc_clnt **clnt)
5509 struct nfs_server *server = NFS_SERVER(hdr->inode);
5511 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5512 hdr->args.bitmask = NULL;
5513 hdr->res.fattr = NULL;
5515 nfs4_bitmask_set(hdr->args.bitmask_store,
5516 server->cache_consistency_bitmask,
5517 hdr->inode, server, NULL);
5518 hdr->args.bitmask = hdr->args.bitmask_store;
5521 if (!hdr->pgio_done_cb)
5522 hdr->pgio_done_cb = nfs4_write_done_cb;
5523 hdr->res.server = server;
5524 hdr->timestamp = jiffies;
5526 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5527 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5528 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5531 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5533 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5534 &data->args.seq_args,
5539 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5541 struct inode *inode = data->inode;
5543 trace_nfs4_commit(data, task->tk_status);
5544 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5545 NULL, NULL) == -EAGAIN) {
5546 rpc_restart_call_prepare(task);
5552 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5554 if (!nfs4_sequence_done(task, &data->res.seq_res))
5556 return data->commit_done_cb(task, data);
5559 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5560 struct rpc_clnt **clnt)
5562 struct nfs_server *server = NFS_SERVER(data->inode);
5564 if (data->commit_done_cb == NULL)
5565 data->commit_done_cb = nfs4_commit_done_cb;
5566 data->res.server = server;
5567 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5568 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5569 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5572 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5573 struct nfs_commitres *res)
5575 struct inode *dst_inode = file_inode(dst);
5576 struct nfs_server *server = NFS_SERVER(dst_inode);
5577 struct rpc_message msg = {
5578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5583 args->fh = NFS_FH(dst_inode);
5584 return nfs4_call_sync(server->client, server, &msg,
5585 &args->seq_args, &res->seq_res, 1);
5588 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5590 struct nfs_commitargs args = {
5594 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5595 struct nfs4_exception exception = { };
5599 status = _nfs4_proc_commit(dst, &args, res);
5600 status = nfs4_handle_exception(dst_server, status, &exception);
5601 } while (exception.retry);
5606 struct nfs4_renewdata {
5607 struct nfs_client *client;
5608 unsigned long timestamp;
5612 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5613 * standalone procedure for queueing an asynchronous RENEW.
5615 static void nfs4_renew_release(void *calldata)
5617 struct nfs4_renewdata *data = calldata;
5618 struct nfs_client *clp = data->client;
5620 if (refcount_read(&clp->cl_count) > 1)
5621 nfs4_schedule_state_renewal(clp);
5622 nfs_put_client(clp);
5626 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5628 struct nfs4_renewdata *data = calldata;
5629 struct nfs_client *clp = data->client;
5630 unsigned long timestamp = data->timestamp;
5632 trace_nfs4_renew_async(clp, task->tk_status);
5633 switch (task->tk_status) {
5636 case -NFS4ERR_LEASE_MOVED:
5637 nfs4_schedule_lease_moved_recovery(clp);
5640 /* Unless we're shutting down, schedule state recovery! */
5641 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5643 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5644 nfs4_schedule_lease_recovery(clp);
5647 nfs4_schedule_path_down_recovery(clp);
5649 do_renew_lease(clp, timestamp);
5652 static const struct rpc_call_ops nfs4_renew_ops = {
5653 .rpc_call_done = nfs4_renew_done,
5654 .rpc_release = nfs4_renew_release,
5657 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5659 struct rpc_message msg = {
5660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5664 struct nfs4_renewdata *data;
5666 if (renew_flags == 0)
5668 if (!refcount_inc_not_zero(&clp->cl_count))
5670 data = kmalloc(sizeof(*data), GFP_NOFS);
5672 nfs_put_client(clp);
5676 data->timestamp = jiffies;
5677 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5678 &nfs4_renew_ops, data);
5681 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5683 struct rpc_message msg = {
5684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5688 unsigned long now = jiffies;
5691 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5694 do_renew_lease(clp, now);
5698 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5700 return server->caps & NFS_CAP_ACLS;
5703 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5704 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5707 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5709 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5710 struct page **pages)
5712 struct page *newpage, **spages;
5718 len = min_t(size_t, PAGE_SIZE, buflen);
5719 newpage = alloc_page(GFP_KERNEL);
5721 if (newpage == NULL)
5723 memcpy(page_address(newpage), buf, len);
5728 } while (buflen != 0);
5734 __free_page(spages[rc-1]);
5738 struct nfs4_cached_acl {
5744 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5746 struct nfs_inode *nfsi = NFS_I(inode);
5748 spin_lock(&inode->i_lock);
5749 kfree(nfsi->nfs4_acl);
5750 nfsi->nfs4_acl = acl;
5751 spin_unlock(&inode->i_lock);
5754 static void nfs4_zap_acl_attr(struct inode *inode)
5756 nfs4_set_cached_acl(inode, NULL);
5759 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5761 struct nfs_inode *nfsi = NFS_I(inode);
5762 struct nfs4_cached_acl *acl;
5765 spin_lock(&inode->i_lock);
5766 acl = nfsi->nfs4_acl;
5769 if (buf == NULL) /* user is just asking for length */
5771 if (acl->cached == 0)
5773 ret = -ERANGE; /* see getxattr(2) man page */
5774 if (acl->len > buflen)
5776 memcpy(buf, acl->data, acl->len);
5780 spin_unlock(&inode->i_lock);
5784 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5786 struct nfs4_cached_acl *acl;
5787 size_t buflen = sizeof(*acl) + acl_len;
5789 if (buflen <= PAGE_SIZE) {
5790 acl = kmalloc(buflen, GFP_KERNEL);
5794 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5796 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5803 nfs4_set_cached_acl(inode, acl);
5807 * The getxattr API returns the required buffer length when called with a
5808 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5809 * the required buf. On a NULL buf, we send a page of data to the server
5810 * guessing that the ACL request can be serviced by a page. If so, we cache
5811 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5812 * the cache. If not so, we throw away the page, and cache the required
5813 * length. The next getxattr call will then produce another round trip to
5814 * the server, this time with the input buf of the required size.
5816 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5818 struct page **pages;
5819 struct nfs_getaclargs args = {
5820 .fh = NFS_FH(inode),
5823 struct nfs_getaclres res = {
5826 struct rpc_message msg = {
5827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5831 unsigned int npages;
5832 int ret = -ENOMEM, i;
5833 struct nfs_server *server = NFS_SERVER(inode);
5836 buflen = server->rsize;
5838 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5839 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5843 args.acl_pages = pages;
5845 for (i = 0; i < npages; i++) {
5846 pages[i] = alloc_page(GFP_KERNEL);
5851 /* for decoding across pages */
5852 res.acl_scratch = alloc_page(GFP_KERNEL);
5853 if (!res.acl_scratch)
5856 args.acl_len = npages * PAGE_SIZE;
5858 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5859 __func__, buf, buflen, npages, args.acl_len);
5860 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5861 &msg, &args.seq_args, &res.seq_res, 0);
5865 /* Handle the case where the passed-in buffer is too short */
5866 if (res.acl_flags & NFS4_ACL_TRUNC) {
5867 /* Did the user only issue a request for the acl length? */
5873 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5875 if (res.acl_len > buflen) {
5879 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5884 for (i = 0; i < npages; i++)
5886 __free_page(pages[i]);
5887 if (res.acl_scratch)
5888 __free_page(res.acl_scratch);
5893 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5895 struct nfs4_exception exception = {
5896 .interruptible = true,
5900 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5901 trace_nfs4_get_acl(inode, ret);
5904 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5905 } while (exception.retry);
5909 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5911 struct nfs_server *server = NFS_SERVER(inode);
5914 if (!nfs4_server_supports_acls(server))
5916 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
5919 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5920 nfs_zap_acl_cache(inode);
5921 ret = nfs4_read_cached_acl(inode, buf, buflen);
5923 /* -ENOENT is returned if there is no ACL or if there is an ACL
5924 * but no cached acl data, just the acl length */
5926 return nfs4_get_acl_uncached(inode, buf, buflen);
5929 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5931 struct nfs_server *server = NFS_SERVER(inode);
5932 struct page *pages[NFS4ACL_MAXPAGES];
5933 struct nfs_setaclargs arg = {
5934 .fh = NFS_FH(inode),
5938 struct nfs_setaclres res;
5939 struct rpc_message msg = {
5940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5944 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5947 /* You can't remove system.nfs4_acl: */
5950 if (!nfs4_server_supports_acls(server))
5952 if (npages > ARRAY_SIZE(pages))
5954 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5957 nfs4_inode_make_writeable(inode);
5958 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5961 * Free each page after tx, so the only ref left is
5962 * held by the network stack
5965 put_page(pages[i-1]);
5968 * Acl update can result in inode attribute update.
5969 * so mark the attribute cache invalid.
5971 spin_lock(&inode->i_lock);
5972 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
5973 NFS_INO_INVALID_CTIME |
5974 NFS_INO_REVAL_FORCED);
5975 spin_unlock(&inode->i_lock);
5976 nfs_access_zap_cache(inode);
5977 nfs_zap_acl_cache(inode);
5981 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5983 struct nfs4_exception exception = { };
5986 err = __nfs4_proc_set_acl(inode, buf, buflen);
5987 trace_nfs4_set_acl(inode, err);
5988 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
5990 * no need to retry since the kernel
5991 * isn't involved in encoding the ACEs.
5996 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5998 } while (exception.retry);
6002 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6003 static int _nfs4_get_security_label(struct inode *inode, void *buf,
6006 struct nfs_server *server = NFS_SERVER(inode);
6007 struct nfs_fattr fattr;
6008 struct nfs4_label label = {0, 0, buflen, buf};
6010 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6011 struct nfs4_getattr_arg arg = {
6012 .fh = NFS_FH(inode),
6015 struct nfs4_getattr_res res = {
6020 struct rpc_message msg = {
6021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6027 nfs_fattr_init(&fattr);
6029 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6032 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6037 static int nfs4_get_security_label(struct inode *inode, void *buf,
6040 struct nfs4_exception exception = {
6041 .interruptible = true,
6045 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6049 err = _nfs4_get_security_label(inode, buf, buflen);
6050 trace_nfs4_get_security_label(inode, err);
6051 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6053 } while (exception.retry);
6057 static int _nfs4_do_set_security_label(struct inode *inode,
6058 struct nfs4_label *ilabel,
6059 struct nfs_fattr *fattr,
6060 struct nfs4_label *olabel)
6063 struct iattr sattr = {0};
6064 struct nfs_server *server = NFS_SERVER(inode);
6065 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6066 struct nfs_setattrargs arg = {
6067 .fh = NFS_FH(inode),
6073 struct nfs_setattrres res = {
6078 struct rpc_message msg = {
6079 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6085 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6087 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6089 dprintk("%s failed: %d\n", __func__, status);
6094 static int nfs4_do_set_security_label(struct inode *inode,
6095 struct nfs4_label *ilabel,
6096 struct nfs_fattr *fattr,
6097 struct nfs4_label *olabel)
6099 struct nfs4_exception exception = { };
6103 err = _nfs4_do_set_security_label(inode, ilabel,
6105 trace_nfs4_set_security_label(inode, err);
6106 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6108 } while (exception.retry);
6113 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6115 struct nfs4_label ilabel, *olabel = NULL;
6116 struct nfs_fattr fattr;
6119 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6122 nfs_fattr_init(&fattr);
6126 ilabel.label = (char *)buf;
6127 ilabel.len = buflen;
6129 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6130 if (IS_ERR(olabel)) {
6131 status = -PTR_ERR(olabel);
6135 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6137 nfs_setsecurity(inode, &fattr, olabel);
6139 nfs4_label_free(olabel);
6143 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6146 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6147 nfs4_verifier *bootverf)
6151 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6152 /* An impossible timestamp guarantees this value
6153 * will never match a generated boot time. */
6154 verf[0] = cpu_to_be32(U32_MAX);
6155 verf[1] = cpu_to_be32(U32_MAX);
6157 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6158 u64 ns = ktime_to_ns(nn->boot_time);
6160 verf[0] = cpu_to_be32(ns >> 32);
6161 verf[1] = cpu_to_be32(ns);
6163 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6167 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6169 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6170 struct nfs_netns_client *nn_clp = nn->nfs_client;
6177 id = rcu_dereference(nn_clp->identifier);
6179 strscpy(buf, id, buflen);
6183 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6184 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6190 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6192 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6197 if (clp->cl_owner_id != NULL)
6202 strlen(clp->cl_rpcclient->cl_nodename) +
6204 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6208 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6212 if (len > NFS4_OPAQUE_LIMIT + 1)
6216 * Since this string is allocated at mount time, and held until the
6217 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6218 * about a memory-reclaim deadlock.
6220 str = kmalloc(len, GFP_KERNEL);
6226 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6227 clp->cl_rpcclient->cl_nodename, buf,
6228 rpc_peeraddr2str(clp->cl_rpcclient,
6231 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6232 clp->cl_rpcclient->cl_nodename,
6233 rpc_peeraddr2str(clp->cl_rpcclient,
6237 clp->cl_owner_id = str;
6242 nfs4_init_uniform_client_string(struct nfs_client *clp)
6244 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6249 if (clp->cl_owner_id != NULL)
6252 len = 10 + 10 + 1 + 10 + 1 +
6253 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6255 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6259 if (len > NFS4_OPAQUE_LIMIT + 1)
6263 * Since this string is allocated at mount time, and held until the
6264 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6265 * about a memory-reclaim deadlock.
6267 str = kmalloc(len, GFP_KERNEL);
6272 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6273 clp->rpc_ops->version, clp->cl_minorversion,
6274 buf, clp->cl_rpcclient->cl_nodename);
6276 scnprintf(str, len, "Linux NFSv%u.%u %s",
6277 clp->rpc_ops->version, clp->cl_minorversion,
6278 clp->cl_rpcclient->cl_nodename);
6279 clp->cl_owner_id = str;
6284 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6285 * services. Advertise one based on the address family of the
6289 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6291 if (strchr(clp->cl_ipaddr, ':') != NULL)
6292 return scnprintf(buf, len, "tcp6");
6294 return scnprintf(buf, len, "tcp");
6297 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6299 struct nfs4_setclientid *sc = calldata;
6301 if (task->tk_status == 0)
6302 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6305 static const struct rpc_call_ops nfs4_setclientid_ops = {
6306 .rpc_call_done = nfs4_setclientid_done,
6310 * nfs4_proc_setclientid - Negotiate client ID
6311 * @clp: state data structure
6312 * @program: RPC program for NFSv4 callback service
6313 * @port: IP port number for NFS4 callback service
6314 * @cred: credential to use for this call
6315 * @res: where to place the result
6317 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6319 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6320 unsigned short port, const struct cred *cred,
6321 struct nfs4_setclientid_res *res)
6323 nfs4_verifier sc_verifier;
6324 struct nfs4_setclientid setclientid = {
6325 .sc_verifier = &sc_verifier,
6329 struct rpc_message msg = {
6330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6331 .rpc_argp = &setclientid,
6335 struct rpc_task_setup task_setup_data = {
6336 .rpc_client = clp->cl_rpcclient,
6337 .rpc_message = &msg,
6338 .callback_ops = &nfs4_setclientid_ops,
6339 .callback_data = &setclientid,
6340 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6342 unsigned long now = jiffies;
6345 /* nfs_client_id4 */
6346 nfs4_init_boot_verifier(clp, &sc_verifier);
6348 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6349 status = nfs4_init_uniform_client_string(clp);
6351 status = nfs4_init_nonuniform_client_string(clp);
6357 setclientid.sc_netid_len =
6358 nfs4_init_callback_netid(clp,
6359 setclientid.sc_netid,
6360 sizeof(setclientid.sc_netid));
6361 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6362 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6363 clp->cl_ipaddr, port >> 8, port & 255);
6365 dprintk("NFS call setclientid auth=%s, '%s'\n",
6366 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6369 status = nfs4_call_sync_custom(&task_setup_data);
6370 if (setclientid.sc_cred) {
6371 kfree(clp->cl_acceptor);
6372 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6373 put_rpccred(setclientid.sc_cred);
6377 do_renew_lease(clp, now);
6379 trace_nfs4_setclientid(clp, status);
6380 dprintk("NFS reply setclientid: %d\n", status);
6385 * nfs4_proc_setclientid_confirm - Confirm client ID
6386 * @clp: state data structure
6387 * @arg: result of a previous SETCLIENTID
6388 * @cred: credential to use for this call
6390 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6392 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6393 struct nfs4_setclientid_res *arg,
6394 const struct cred *cred)
6396 struct rpc_message msg = {
6397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6403 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6404 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6406 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6407 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6408 trace_nfs4_setclientid_confirm(clp, status);
6409 dprintk("NFS reply setclientid_confirm: %d\n", status);
6413 struct nfs4_delegreturndata {
6414 struct nfs4_delegreturnargs args;
6415 struct nfs4_delegreturnres res;
6417 nfs4_stateid stateid;
6418 unsigned long timestamp;
6420 struct nfs4_layoutreturn_args arg;
6421 struct nfs4_layoutreturn_res res;
6422 struct nfs4_xdr_opaque_data ld_private;
6426 struct nfs_fattr fattr;
6428 struct inode *inode;
6431 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6433 struct nfs4_delegreturndata *data = calldata;
6434 struct nfs4_exception exception = {
6435 .inode = data->inode,
6436 .stateid = &data->stateid,
6437 .task_is_privileged = data->args.seq_args.sa_privileged,
6440 if (!nfs4_sequence_done(task, &data->res.seq_res))
6443 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6445 /* Handle Layoutreturn errors */
6446 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6447 &data->res.lr_ret) == -EAGAIN)
6450 switch (task->tk_status) {
6452 renew_lease(data->res.server, data->timestamp);
6454 case -NFS4ERR_ADMIN_REVOKED:
6455 case -NFS4ERR_DELEG_REVOKED:
6456 case -NFS4ERR_EXPIRED:
6457 nfs4_free_revoked_stateid(data->res.server,
6459 task->tk_msg.rpc_cred);
6461 case -NFS4ERR_BAD_STATEID:
6462 case -NFS4ERR_STALE_STATEID:
6464 task->tk_status = 0;
6466 case -NFS4ERR_OLD_STATEID:
6467 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6468 nfs4_stateid_seqid_inc(&data->stateid);
6469 if (data->args.bitmask) {
6470 data->args.bitmask = NULL;
6471 data->res.fattr = NULL;
6474 case -NFS4ERR_ACCESS:
6475 if (data->args.bitmask) {
6476 data->args.bitmask = NULL;
6477 data->res.fattr = NULL;
6482 task->tk_status = nfs4_async_handle_exception(task,
6483 data->res.server, task->tk_status,
6485 if (exception.retry)
6488 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6489 data->rpc_status = task->tk_status;
6492 task->tk_status = 0;
6493 rpc_restart_call_prepare(task);
6496 static void nfs4_delegreturn_release(void *calldata)
6498 struct nfs4_delegreturndata *data = calldata;
6499 struct inode *inode = data->inode;
6502 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6505 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6506 nfs_iput_and_deactive(inode);
6511 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6513 struct nfs4_delegreturndata *d_data;
6514 struct pnfs_layout_hdr *lo;
6516 d_data = (struct nfs4_delegreturndata *)data;
6518 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6519 nfs4_sequence_done(task, &d_data->res.seq_res);
6523 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6524 if (lo && !pnfs_layout_is_valid(lo)) {
6525 d_data->args.lr_args = NULL;
6526 d_data->res.lr_res = NULL;
6529 nfs4_setup_sequence(d_data->res.server->nfs_client,
6530 &d_data->args.seq_args,
6531 &d_data->res.seq_res,
6535 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6536 .rpc_call_prepare = nfs4_delegreturn_prepare,
6537 .rpc_call_done = nfs4_delegreturn_done,
6538 .rpc_release = nfs4_delegreturn_release,
6541 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6543 struct nfs4_delegreturndata *data;
6544 struct nfs_server *server = NFS_SERVER(inode);
6545 struct rpc_task *task;
6546 struct rpc_message msg = {
6547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6550 struct rpc_task_setup task_setup_data = {
6551 .rpc_client = server->client,
6552 .rpc_message = &msg,
6553 .callback_ops = &nfs4_delegreturn_ops,
6554 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
6558 data = kzalloc(sizeof(*data), GFP_NOFS);
6562 nfs4_state_protect(server->nfs_client,
6563 NFS_SP4_MACH_CRED_CLEANUP,
6564 &task_setup_data.rpc_client, &msg);
6566 data->args.fhandle = &data->fh;
6567 data->args.stateid = &data->stateid;
6568 nfs4_bitmask_set(data->args.bitmask_store,
6569 server->cache_consistency_bitmask, inode, server,
6571 data->args.bitmask = data->args.bitmask_store;
6572 nfs_copy_fh(&data->fh, NFS_FH(inode));
6573 nfs4_stateid_copy(&data->stateid, stateid);
6574 data->res.fattr = &data->fattr;
6575 data->res.server = server;
6576 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6577 data->lr.arg.ld_private = &data->lr.ld_private;
6578 nfs_fattr_init(data->res.fattr);
6579 data->timestamp = jiffies;
6580 data->rpc_status = 0;
6581 data->inode = nfs_igrab_and_active(inode);
6582 if (data->inode || issync) {
6583 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6586 data->args.lr_args = &data->lr.arg;
6587 data->res.lr_res = &data->lr.res;
6592 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6595 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6597 task_setup_data.callback_data = data;
6598 msg.rpc_argp = &data->args;
6599 msg.rpc_resp = &data->res;
6600 task = rpc_run_task(&task_setup_data);
6602 return PTR_ERR(task);
6605 status = rpc_wait_for_completion_task(task);
6608 status = data->rpc_status;
6614 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6616 struct nfs_server *server = NFS_SERVER(inode);
6617 struct nfs4_exception exception = { };
6620 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6621 trace_nfs4_delegreturn(inode, stateid, err);
6623 case -NFS4ERR_STALE_STATEID:
6624 case -NFS4ERR_EXPIRED:
6628 err = nfs4_handle_exception(server, err, &exception);
6629 } while (exception.retry);
6633 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6635 struct inode *inode = state->inode;
6636 struct nfs_server *server = NFS_SERVER(inode);
6637 struct nfs_client *clp = server->nfs_client;
6638 struct nfs_lockt_args arg = {
6639 .fh = NFS_FH(inode),
6642 struct nfs_lockt_res res = {
6645 struct rpc_message msg = {
6646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6649 .rpc_cred = state->owner->so_cred,
6651 struct nfs4_lock_state *lsp;
6654 arg.lock_owner.clientid = clp->cl_clientid;
6655 status = nfs4_set_lock_state(state, request);
6658 lsp = request->fl_u.nfs4_fl.owner;
6659 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6660 arg.lock_owner.s_dev = server->s_dev;
6661 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6664 request->fl_type = F_UNLCK;
6666 case -NFS4ERR_DENIED:
6669 request->fl_ops->fl_release_private(request);
6670 request->fl_ops = NULL;
6675 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6677 struct nfs4_exception exception = {
6678 .interruptible = true,
6683 err = _nfs4_proc_getlk(state, cmd, request);
6684 trace_nfs4_get_lock(request, state, cmd, err);
6685 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6687 } while (exception.retry);
6692 * Update the seqid of a lock stateid after receiving
6693 * NFS4ERR_OLD_STATEID
6695 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6696 struct nfs4_lock_state *lsp)
6698 struct nfs4_state *state = lsp->ls_state;
6701 spin_lock(&state->state_lock);
6702 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6704 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6705 nfs4_stateid_seqid_inc(dst);
6707 dst->seqid = lsp->ls_stateid.seqid;
6710 spin_unlock(&state->state_lock);
6714 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6715 struct nfs4_lock_state *lsp)
6717 struct nfs4_state *state = lsp->ls_state;
6720 spin_lock(&state->state_lock);
6721 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6722 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6723 spin_unlock(&state->state_lock);
6727 struct nfs4_unlockdata {
6728 struct nfs_locku_args arg;
6729 struct nfs_locku_res res;
6730 struct nfs4_lock_state *lsp;
6731 struct nfs_open_context *ctx;
6732 struct nfs_lock_context *l_ctx;
6733 struct file_lock fl;
6734 struct nfs_server *server;
6735 unsigned long timestamp;
6738 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6739 struct nfs_open_context *ctx,
6740 struct nfs4_lock_state *lsp,
6741 struct nfs_seqid *seqid)
6743 struct nfs4_unlockdata *p;
6744 struct nfs4_state *state = lsp->ls_state;
6745 struct inode *inode = state->inode;
6747 p = kzalloc(sizeof(*p), GFP_NOFS);
6750 p->arg.fh = NFS_FH(inode);
6752 p->arg.seqid = seqid;
6753 p->res.seqid = seqid;
6755 /* Ensure we don't close file until we're done freeing locks! */
6756 p->ctx = get_nfs_open_context(ctx);
6757 p->l_ctx = nfs_get_lock_context(ctx);
6758 locks_init_lock(&p->fl);
6759 locks_copy_lock(&p->fl, fl);
6760 p->server = NFS_SERVER(inode);
6761 spin_lock(&state->state_lock);
6762 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6763 spin_unlock(&state->state_lock);
6767 static void nfs4_locku_release_calldata(void *data)
6769 struct nfs4_unlockdata *calldata = data;
6770 nfs_free_seqid(calldata->arg.seqid);
6771 nfs4_put_lock_state(calldata->lsp);
6772 nfs_put_lock_context(calldata->l_ctx);
6773 put_nfs_open_context(calldata->ctx);
6777 static void nfs4_locku_done(struct rpc_task *task, void *data)
6779 struct nfs4_unlockdata *calldata = data;
6780 struct nfs4_exception exception = {
6781 .inode = calldata->lsp->ls_state->inode,
6782 .stateid = &calldata->arg.stateid,
6785 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6787 switch (task->tk_status) {
6789 renew_lease(calldata->server, calldata->timestamp);
6790 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6791 if (nfs4_update_lock_stateid(calldata->lsp,
6792 &calldata->res.stateid))
6795 case -NFS4ERR_ADMIN_REVOKED:
6796 case -NFS4ERR_EXPIRED:
6797 nfs4_free_revoked_stateid(calldata->server,
6798 &calldata->arg.stateid,
6799 task->tk_msg.rpc_cred);
6801 case -NFS4ERR_BAD_STATEID:
6802 case -NFS4ERR_STALE_STATEID:
6803 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6805 rpc_restart_call_prepare(task);
6807 case -NFS4ERR_OLD_STATEID:
6808 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6810 rpc_restart_call_prepare(task);
6813 task->tk_status = nfs4_async_handle_exception(task,
6814 calldata->server, task->tk_status,
6816 if (exception.retry)
6817 rpc_restart_call_prepare(task);
6819 nfs_release_seqid(calldata->arg.seqid);
6822 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6824 struct nfs4_unlockdata *calldata = data;
6826 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6827 nfs_async_iocounter_wait(task, calldata->l_ctx))
6830 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6832 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6833 /* Note: exit _without_ running nfs4_locku_done */
6836 calldata->timestamp = jiffies;
6837 if (nfs4_setup_sequence(calldata->server->nfs_client,
6838 &calldata->arg.seq_args,
6839 &calldata->res.seq_res,
6841 nfs_release_seqid(calldata->arg.seqid);
6844 task->tk_action = NULL;
6846 nfs4_sequence_done(task, &calldata->res.seq_res);
6849 static const struct rpc_call_ops nfs4_locku_ops = {
6850 .rpc_call_prepare = nfs4_locku_prepare,
6851 .rpc_call_done = nfs4_locku_done,
6852 .rpc_release = nfs4_locku_release_calldata,
6855 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6856 struct nfs_open_context *ctx,
6857 struct nfs4_lock_state *lsp,
6858 struct nfs_seqid *seqid)
6860 struct nfs4_unlockdata *data;
6861 struct rpc_message msg = {
6862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6863 .rpc_cred = ctx->cred,
6865 struct rpc_task_setup task_setup_data = {
6866 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6867 .rpc_message = &msg,
6868 .callback_ops = &nfs4_locku_ops,
6869 .workqueue = nfsiod_workqueue,
6870 .flags = RPC_TASK_ASYNC,
6872 struct nfs_client *client =
6873 NFS_SERVER(lsp->ls_state->inode)->nfs_client;
6875 if (client->cl_minorversion)
6876 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6878 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6879 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6881 /* Ensure this is an unlock - when canceling a lock, the
6882 * canceled lock is passed in, and it won't be an unlock.
6884 fl->fl_type = F_UNLCK;
6885 if (fl->fl_flags & FL_CLOSE)
6886 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6888 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6890 nfs_free_seqid(seqid);
6891 return ERR_PTR(-ENOMEM);
6894 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6895 msg.rpc_argp = &data->arg;
6896 msg.rpc_resp = &data->res;
6897 task_setup_data.callback_data = data;
6898 return rpc_run_task(&task_setup_data);
6901 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6903 struct inode *inode = state->inode;
6904 struct nfs4_state_owner *sp = state->owner;
6905 struct nfs_inode *nfsi = NFS_I(inode);
6906 struct nfs_seqid *seqid;
6907 struct nfs4_lock_state *lsp;
6908 struct rpc_task *task;
6909 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6911 unsigned char fl_flags = request->fl_flags;
6913 status = nfs4_set_lock_state(state, request);
6914 /* Unlock _before_ we do the RPC call */
6915 request->fl_flags |= FL_EXISTS;
6916 /* Exclude nfs_delegation_claim_locks() */
6917 mutex_lock(&sp->so_delegreturn_mutex);
6918 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6919 down_read(&nfsi->rwsem);
6920 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6921 up_read(&nfsi->rwsem);
6922 mutex_unlock(&sp->so_delegreturn_mutex);
6925 up_read(&nfsi->rwsem);
6926 mutex_unlock(&sp->so_delegreturn_mutex);
6929 /* Is this a delegated lock? */
6930 lsp = request->fl_u.nfs4_fl.owner;
6931 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6933 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6934 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6938 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6939 status = PTR_ERR(task);
6942 status = rpc_wait_for_completion_task(task);
6945 request->fl_flags = fl_flags;
6946 trace_nfs4_unlock(request, state, F_SETLK, status);
6950 struct nfs4_lockdata {
6951 struct nfs_lock_args arg;
6952 struct nfs_lock_res res;
6953 struct nfs4_lock_state *lsp;
6954 struct nfs_open_context *ctx;
6955 struct file_lock fl;
6956 unsigned long timestamp;
6959 struct nfs_server *server;
6962 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6963 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6966 struct nfs4_lockdata *p;
6967 struct inode *inode = lsp->ls_state->inode;
6968 struct nfs_server *server = NFS_SERVER(inode);
6969 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6971 p = kzalloc(sizeof(*p), gfp_mask);
6975 p->arg.fh = NFS_FH(inode);
6977 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6978 if (IS_ERR(p->arg.open_seqid))
6980 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6981 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6982 if (IS_ERR(p->arg.lock_seqid))
6983 goto out_free_seqid;
6984 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6985 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6986 p->arg.lock_owner.s_dev = server->s_dev;
6987 p->res.lock_seqid = p->arg.lock_seqid;
6990 p->ctx = get_nfs_open_context(ctx);
6991 locks_init_lock(&p->fl);
6992 locks_copy_lock(&p->fl, fl);
6995 nfs_free_seqid(p->arg.open_seqid);
7001 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7003 struct nfs4_lockdata *data = calldata;
7004 struct nfs4_state *state = data->lsp->ls_state;
7006 dprintk("%s: begin!\n", __func__);
7007 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
7009 /* Do we need to do an open_to_lock_owner? */
7010 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
7011 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
7012 goto out_release_lock_seqid;
7014 nfs4_stateid_copy(&data->arg.open_stateid,
7015 &state->open_stateid);
7016 data->arg.new_lock_owner = 1;
7017 data->res.open_seqid = data->arg.open_seqid;
7019 data->arg.new_lock_owner = 0;
7020 nfs4_stateid_copy(&data->arg.lock_stateid,
7021 &data->lsp->ls_stateid);
7023 if (!nfs4_valid_open_stateid(state)) {
7024 data->rpc_status = -EBADF;
7025 task->tk_action = NULL;
7026 goto out_release_open_seqid;
7028 data->timestamp = jiffies;
7029 if (nfs4_setup_sequence(data->server->nfs_client,
7030 &data->arg.seq_args,
7034 out_release_open_seqid:
7035 nfs_release_seqid(data->arg.open_seqid);
7036 out_release_lock_seqid:
7037 nfs_release_seqid(data->arg.lock_seqid);
7039 nfs4_sequence_done(task, &data->res.seq_res);
7040 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
7043 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7045 struct nfs4_lockdata *data = calldata;
7046 struct nfs4_lock_state *lsp = data->lsp;
7048 dprintk("%s: begin!\n", __func__);
7050 if (!nfs4_sequence_done(task, &data->res.seq_res))
7053 data->rpc_status = task->tk_status;
7054 switch (task->tk_status) {
7056 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
7058 if (data->arg.new_lock && !data->cancelled) {
7059 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
7060 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7063 if (data->arg.new_lock_owner != 0) {
7064 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7065 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7066 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7067 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7070 case -NFS4ERR_BAD_STATEID:
7071 case -NFS4ERR_OLD_STATEID:
7072 case -NFS4ERR_STALE_STATEID:
7073 case -NFS4ERR_EXPIRED:
7074 if (data->arg.new_lock_owner != 0) {
7075 if (!nfs4_stateid_match(&data->arg.open_stateid,
7076 &lsp->ls_state->open_stateid))
7078 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7083 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
7086 if (!data->cancelled)
7087 rpc_restart_call_prepare(task);
7091 static void nfs4_lock_release(void *calldata)
7093 struct nfs4_lockdata *data = calldata;
7095 dprintk("%s: begin!\n", __func__);
7096 nfs_free_seqid(data->arg.open_seqid);
7097 if (data->cancelled && data->rpc_status == 0) {
7098 struct rpc_task *task;
7099 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7100 data->arg.lock_seqid);
7102 rpc_put_task_async(task);
7103 dprintk("%s: cancelling lock!\n", __func__);
7105 nfs_free_seqid(data->arg.lock_seqid);
7106 nfs4_put_lock_state(data->lsp);
7107 put_nfs_open_context(data->ctx);
7109 dprintk("%s: done!\n", __func__);
7112 static const struct rpc_call_ops nfs4_lock_ops = {
7113 .rpc_call_prepare = nfs4_lock_prepare,
7114 .rpc_call_done = nfs4_lock_done,
7115 .rpc_release = nfs4_lock_release,
7118 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7121 case -NFS4ERR_ADMIN_REVOKED:
7122 case -NFS4ERR_EXPIRED:
7123 case -NFS4ERR_BAD_STATEID:
7124 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7125 if (new_lock_owner != 0 ||
7126 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7127 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7129 case -NFS4ERR_STALE_STATEID:
7130 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7131 nfs4_schedule_lease_recovery(server->nfs_client);
7135 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7137 struct nfs4_lockdata *data;
7138 struct rpc_task *task;
7139 struct rpc_message msg = {
7140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7141 .rpc_cred = state->owner->so_cred,
7143 struct rpc_task_setup task_setup_data = {
7144 .rpc_client = NFS_CLIENT(state->inode),
7145 .rpc_message = &msg,
7146 .callback_ops = &nfs4_lock_ops,
7147 .workqueue = nfsiod_workqueue,
7148 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7151 struct nfs_client *client = NFS_SERVER(state->inode)->nfs_client;
7153 if (client->cl_minorversion)
7154 task_setup_data.flags |= RPC_TASK_MOVEABLE;
7156 dprintk("%s: begin!\n", __func__);
7157 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7158 fl->fl_u.nfs4_fl.owner,
7159 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
7163 data->arg.block = 1;
7164 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7165 recovery_type > NFS_LOCK_NEW);
7166 msg.rpc_argp = &data->arg;
7167 msg.rpc_resp = &data->res;
7168 task_setup_data.callback_data = data;
7169 if (recovery_type > NFS_LOCK_NEW) {
7170 if (recovery_type == NFS_LOCK_RECLAIM)
7171 data->arg.reclaim = NFS_LOCK_RECLAIM;
7173 data->arg.new_lock = 1;
7174 task = rpc_run_task(&task_setup_data);
7176 return PTR_ERR(task);
7177 ret = rpc_wait_for_completion_task(task);
7179 ret = data->rpc_status;
7181 nfs4_handle_setlk_error(data->server, data->lsp,
7182 data->arg.new_lock_owner, ret);
7184 data->cancelled = true;
7185 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7187 dprintk("%s: done, ret = %d!\n", __func__, ret);
7191 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7193 struct nfs_server *server = NFS_SERVER(state->inode);
7194 struct nfs4_exception exception = {
7195 .inode = state->inode,
7200 /* Cache the lock if possible... */
7201 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7203 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7204 if (err != -NFS4ERR_DELAY)
7206 nfs4_handle_exception(server, err, &exception);
7207 } while (exception.retry);
7211 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7213 struct nfs_server *server = NFS_SERVER(state->inode);
7214 struct nfs4_exception exception = {
7215 .inode = state->inode,
7219 err = nfs4_set_lock_state(state, request);
7222 if (!recover_lost_locks) {
7223 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7227 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7229 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7233 case -NFS4ERR_GRACE:
7234 case -NFS4ERR_DELAY:
7235 nfs4_handle_exception(server, err, &exception);
7238 } while (exception.retry);
7243 #if defined(CONFIG_NFS_V4_1)
7244 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7246 struct nfs4_lock_state *lsp;
7249 status = nfs4_set_lock_state(state, request);
7252 lsp = request->fl_u.nfs4_fl.owner;
7253 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7254 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7256 return nfs4_lock_expired(state, request);
7260 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7262 struct nfs_inode *nfsi = NFS_I(state->inode);
7263 struct nfs4_state_owner *sp = state->owner;
7264 unsigned char fl_flags = request->fl_flags;
7267 request->fl_flags |= FL_ACCESS;
7268 status = locks_lock_inode_wait(state->inode, request);
7271 mutex_lock(&sp->so_delegreturn_mutex);
7272 down_read(&nfsi->rwsem);
7273 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7274 /* Yes: cache locks! */
7275 /* ...but avoid races with delegation recall... */
7276 request->fl_flags = fl_flags & ~FL_SLEEP;
7277 status = locks_lock_inode_wait(state->inode, request);
7278 up_read(&nfsi->rwsem);
7279 mutex_unlock(&sp->so_delegreturn_mutex);
7282 up_read(&nfsi->rwsem);
7283 mutex_unlock(&sp->so_delegreturn_mutex);
7284 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7286 request->fl_flags = fl_flags;
7290 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7292 struct nfs4_exception exception = {
7294 .inode = state->inode,
7295 .interruptible = true,
7300 err = _nfs4_proc_setlk(state, cmd, request);
7301 if (err == -NFS4ERR_DENIED)
7303 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7305 } while (exception.retry);
7309 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7310 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7313 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7314 struct file_lock *request)
7316 int status = -ERESTARTSYS;
7317 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7319 while(!signalled()) {
7320 status = nfs4_proc_setlk(state, cmd, request);
7321 if ((status != -EAGAIN) || IS_SETLK(cmd))
7323 freezable_schedule_timeout_interruptible(timeout);
7325 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7326 status = -ERESTARTSYS;
7331 #ifdef CONFIG_NFS_V4_1
7332 struct nfs4_lock_waiter {
7333 struct inode *inode;
7334 struct nfs_lowner owner;
7335 wait_queue_entry_t wait;
7339 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7341 struct nfs4_lock_waiter *waiter =
7342 container_of(wait, struct nfs4_lock_waiter, wait);
7344 /* NULL key means to wake up everyone */
7346 struct cb_notify_lock_args *cbnl = key;
7347 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7348 *wowner = &waiter->owner;
7350 /* Only wake if the callback was for the same owner. */
7351 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7354 /* Make sure it's for the right inode */
7355 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7359 return woken_wake_function(wait, mode, flags, key);
7363 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7365 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7366 struct nfs_server *server = NFS_SERVER(state->inode);
7367 struct nfs_client *clp = server->nfs_client;
7368 wait_queue_head_t *q = &clp->cl_lock_waitq;
7369 struct nfs4_lock_waiter waiter = {
7370 .inode = state->inode,
7371 .owner = { .clientid = clp->cl_clientid,
7372 .id = lsp->ls_seqid.owner_id,
7373 .s_dev = server->s_dev },
7377 /* Don't bother with waitqueue if we don't expect a callback */
7378 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7379 return nfs4_retry_setlk_simple(state, cmd, request);
7381 init_wait(&waiter.wait);
7382 waiter.wait.func = nfs4_wake_lock_waiter;
7383 add_wait_queue(q, &waiter.wait);
7386 status = nfs4_proc_setlk(state, cmd, request);
7387 if (status != -EAGAIN || IS_SETLK(cmd))
7390 status = -ERESTARTSYS;
7391 freezer_do_not_count();
7392 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE,
7393 NFS4_LOCK_MAXTIMEOUT);
7395 } while (!signalled());
7397 remove_wait_queue(q, &waiter.wait);
7401 #else /* !CONFIG_NFS_V4_1 */
7403 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7405 return nfs4_retry_setlk_simple(state, cmd, request);
7410 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7412 struct nfs_open_context *ctx;
7413 struct nfs4_state *state;
7416 /* verify open state */
7417 ctx = nfs_file_open_context(filp);
7420 if (IS_GETLK(cmd)) {
7422 return nfs4_proc_getlk(state, F_GETLK, request);
7426 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7429 if (request->fl_type == F_UNLCK) {
7431 return nfs4_proc_unlck(state, cmd, request);
7438 if ((request->fl_flags & FL_POSIX) &&
7439 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7443 * Don't rely on the VFS having checked the file open mode,
7444 * since it won't do this for flock() locks.
7446 switch (request->fl_type) {
7448 if (!(filp->f_mode & FMODE_READ))
7452 if (!(filp->f_mode & FMODE_WRITE))
7456 status = nfs4_set_lock_state(state, request);
7460 return nfs4_retry_setlk(state, cmd, request);
7463 static int nfs4_delete_lease(struct file *file, void **priv)
7465 return generic_setlease(file, F_UNLCK, NULL, priv);
7468 static int nfs4_add_lease(struct file *file, long arg, struct file_lock **lease,
7471 struct inode *inode = file_inode(file);
7472 fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7475 /* No delegation, no lease */
7476 if (!nfs4_have_delegation(inode, type))
7478 ret = generic_setlease(file, arg, lease, priv);
7479 if (ret || nfs4_have_delegation(inode, type))
7481 /* We raced with a delegation return */
7482 nfs4_delete_lease(file, priv);
7486 int nfs4_proc_setlease(struct file *file, long arg, struct file_lock **lease,
7492 return nfs4_add_lease(file, arg, lease, priv);
7494 return nfs4_delete_lease(file, priv);
7500 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7502 struct nfs_server *server = NFS_SERVER(state->inode);
7505 err = nfs4_set_lock_state(state, fl);
7509 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7510 if (err != -NFS4ERR_DELAY)
7513 } while (err == -NFS4ERR_DELAY);
7514 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7517 struct nfs_release_lockowner_data {
7518 struct nfs4_lock_state *lsp;
7519 struct nfs_server *server;
7520 struct nfs_release_lockowner_args args;
7521 struct nfs_release_lockowner_res res;
7522 unsigned long timestamp;
7525 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7527 struct nfs_release_lockowner_data *data = calldata;
7528 struct nfs_server *server = data->server;
7529 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7530 &data->res.seq_res, task);
7531 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7532 data->timestamp = jiffies;
7535 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7537 struct nfs_release_lockowner_data *data = calldata;
7538 struct nfs_server *server = data->server;
7540 nfs40_sequence_done(task, &data->res.seq_res);
7542 switch (task->tk_status) {
7544 renew_lease(server, data->timestamp);
7546 case -NFS4ERR_STALE_CLIENTID:
7547 case -NFS4ERR_EXPIRED:
7548 nfs4_schedule_lease_recovery(server->nfs_client);
7550 case -NFS4ERR_LEASE_MOVED:
7551 case -NFS4ERR_DELAY:
7552 if (nfs4_async_handle_error(task, server,
7553 NULL, NULL) == -EAGAIN)
7554 rpc_restart_call_prepare(task);
7558 static void nfs4_release_lockowner_release(void *calldata)
7560 struct nfs_release_lockowner_data *data = calldata;
7561 nfs4_free_lock_state(data->server, data->lsp);
7565 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7566 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7567 .rpc_call_done = nfs4_release_lockowner_done,
7568 .rpc_release = nfs4_release_lockowner_release,
7572 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7574 struct nfs_release_lockowner_data *data;
7575 struct rpc_message msg = {
7576 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7579 if (server->nfs_client->cl_mvops->minor_version != 0)
7582 data = kmalloc(sizeof(*data), GFP_NOFS);
7586 data->server = server;
7587 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7588 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7589 data->args.lock_owner.s_dev = server->s_dev;
7591 msg.rpc_argp = &data->args;
7592 msg.rpc_resp = &data->res;
7593 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7594 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7597 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7599 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7600 struct user_namespace *mnt_userns,
7601 struct dentry *unused, struct inode *inode,
7602 const char *key, const void *buf,
7603 size_t buflen, int flags)
7605 return nfs4_proc_set_acl(inode, buf, buflen);
7608 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7609 struct dentry *unused, struct inode *inode,
7610 const char *key, void *buf, size_t buflen)
7612 return nfs4_proc_get_acl(inode, buf, buflen);
7615 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7617 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7620 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7622 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7623 struct user_namespace *mnt_userns,
7624 struct dentry *unused, struct inode *inode,
7625 const char *key, const void *buf,
7626 size_t buflen, int flags)
7628 if (security_ismaclabel(key))
7629 return nfs4_set_security_label(inode, buf, buflen);
7634 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7635 struct dentry *unused, struct inode *inode,
7636 const char *key, void *buf, size_t buflen)
7638 if (security_ismaclabel(key))
7639 return nfs4_get_security_label(inode, buf, buflen);
7644 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7648 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7649 len = security_inode_listsecurity(inode, list, list_len);
7650 if (len >= 0 && list_len && len > list_len)
7656 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7657 .prefix = XATTR_SECURITY_PREFIX,
7658 .get = nfs4_xattr_get_nfs4_label,
7659 .set = nfs4_xattr_set_nfs4_label,
7665 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7672 #ifdef CONFIG_NFS_V4_2
7673 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7674 struct user_namespace *mnt_userns,
7675 struct dentry *unused, struct inode *inode,
7676 const char *key, const void *buf,
7677 size_t buflen, int flags)
7679 struct nfs_access_entry cache;
7682 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7686 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7687 * flags right now. Handling of xattr operations use the normal
7688 * file read/write permissions.
7690 * Just in case the server has other ideas (which RFC 8276 allows),
7691 * do a cached access check for the XA* flags to possibly avoid
7692 * doing an RPC and getting EACCES back.
7694 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7695 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7700 ret = nfs42_proc_removexattr(inode, key);
7702 nfs4_xattr_cache_remove(inode, key);
7704 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7706 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7712 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7713 struct dentry *unused, struct inode *inode,
7714 const char *key, void *buf, size_t buflen)
7716 struct nfs_access_entry cache;
7719 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7722 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7723 if (!(cache.mask & NFS_ACCESS_XAREAD))
7727 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7731 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7732 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7735 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7741 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7748 struct nfs_access_entry cache;
7750 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7753 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7754 if (!(cache.mask & NFS_ACCESS_XALIST))
7758 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7762 ret = nfs4_xattr_cache_list(inode, list, list_len);
7763 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7768 buflen = list_len ? list_len : XATTR_LIST_MAX;
7769 buf = list_len ? list : NULL;
7773 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7786 nfs4_xattr_cache_set_list(inode, list, size);
7794 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7798 #endif /* CONFIG_NFS_V4_2 */
7801 * nfs_fhget will use either the mounted_on_fileid or the fileid
7803 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7805 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7806 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7807 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7808 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7811 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7812 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7813 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7817 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7818 const struct qstr *name,
7819 struct nfs4_fs_locations *fs_locations,
7822 struct nfs_server *server = NFS_SERVER(dir);
7824 struct nfs4_fs_locations_arg args = {
7825 .dir_fh = NFS_FH(dir),
7830 struct nfs4_fs_locations_res res = {
7831 .fs_locations = fs_locations,
7833 struct rpc_message msg = {
7834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7840 dprintk("%s: start\n", __func__);
7842 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7843 bitmask[1] = nfs4_fattr_bitmap[1];
7845 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7846 * is not supported */
7847 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7848 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7850 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7852 nfs_fattr_init(&fs_locations->fattr);
7853 fs_locations->server = server;
7854 fs_locations->nlocations = 0;
7855 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7856 dprintk("%s: returned status = %d\n", __func__, status);
7860 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7861 const struct qstr *name,
7862 struct nfs4_fs_locations *fs_locations,
7865 struct nfs4_exception exception = {
7866 .interruptible = true,
7870 err = _nfs4_proc_fs_locations(client, dir, name,
7871 fs_locations, page);
7872 trace_nfs4_get_fs_locations(dir, name, err);
7873 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7875 } while (exception.retry);
7880 * This operation also signals the server that this client is
7881 * performing migration recovery. The server can stop returning
7882 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7883 * appended to this compound to identify the client ID which is
7884 * performing recovery.
7886 static int _nfs40_proc_get_locations(struct inode *inode,
7887 struct nfs4_fs_locations *locations,
7888 struct page *page, const struct cred *cred)
7890 struct nfs_server *server = NFS_SERVER(inode);
7891 struct rpc_clnt *clnt = server->client;
7893 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7895 struct nfs4_fs_locations_arg args = {
7896 .clientid = server->nfs_client->cl_clientid,
7897 .fh = NFS_FH(inode),
7900 .migration = 1, /* skip LOOKUP */
7901 .renew = 1, /* append RENEW */
7903 struct nfs4_fs_locations_res res = {
7904 .fs_locations = locations,
7908 struct rpc_message msg = {
7909 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7914 unsigned long now = jiffies;
7917 nfs_fattr_init(&locations->fattr);
7918 locations->server = server;
7919 locations->nlocations = 0;
7921 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7922 status = nfs4_call_sync_sequence(clnt, server, &msg,
7923 &args.seq_args, &res.seq_res);
7927 renew_lease(server, now);
7931 #ifdef CONFIG_NFS_V4_1
7934 * This operation also signals the server that this client is
7935 * performing migration recovery. The server can stop asserting
7936 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7937 * performing this operation is identified in the SEQUENCE
7938 * operation in this compound.
7940 * When the client supports GETATTR(fs_locations_info), it can
7941 * be plumbed in here.
7943 static int _nfs41_proc_get_locations(struct inode *inode,
7944 struct nfs4_fs_locations *locations,
7945 struct page *page, const struct cred *cred)
7947 struct nfs_server *server = NFS_SERVER(inode);
7948 struct rpc_clnt *clnt = server->client;
7950 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7952 struct nfs4_fs_locations_arg args = {
7953 .fh = NFS_FH(inode),
7956 .migration = 1, /* skip LOOKUP */
7958 struct nfs4_fs_locations_res res = {
7959 .fs_locations = locations,
7962 struct rpc_message msg = {
7963 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7970 nfs_fattr_init(&locations->fattr);
7971 locations->server = server;
7972 locations->nlocations = 0;
7974 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7975 status = nfs4_call_sync_sequence(clnt, server, &msg,
7976 &args.seq_args, &res.seq_res);
7977 if (status == NFS4_OK &&
7978 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7979 status = -NFS4ERR_LEASE_MOVED;
7983 #endif /* CONFIG_NFS_V4_1 */
7986 * nfs4_proc_get_locations - discover locations for a migrated FSID
7987 * @inode: inode on FSID that is migrating
7988 * @locations: result of query
7990 * @cred: credential to use for this operation
7992 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7993 * operation failed, or a negative errno if a local error occurred.
7995 * On success, "locations" is filled in, but if the server has
7996 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7999 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
8000 * from this client that require migration recovery.
8002 int nfs4_proc_get_locations(struct inode *inode,
8003 struct nfs4_fs_locations *locations,
8004 struct page *page, const struct cred *cred)
8006 struct nfs_server *server = NFS_SERVER(inode);
8007 struct nfs_client *clp = server->nfs_client;
8008 const struct nfs4_mig_recovery_ops *ops =
8009 clp->cl_mvops->mig_recovery_ops;
8010 struct nfs4_exception exception = {
8011 .interruptible = true,
8015 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8016 (unsigned long long)server->fsid.major,
8017 (unsigned long long)server->fsid.minor,
8019 nfs_display_fhandle(NFS_FH(inode), __func__);
8022 status = ops->get_locations(inode, locations, page, cred);
8023 if (status != -NFS4ERR_DELAY)
8025 nfs4_handle_exception(server, status, &exception);
8026 } while (exception.retry);
8031 * This operation also signals the server that this client is
8032 * performing "lease moved" recovery. The server can stop
8033 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
8034 * is appended to this compound to identify the client ID which is
8035 * performing recovery.
8037 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
8039 struct nfs_server *server = NFS_SERVER(inode);
8040 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8041 struct rpc_clnt *clnt = server->client;
8042 struct nfs4_fsid_present_arg args = {
8043 .fh = NFS_FH(inode),
8044 .clientid = clp->cl_clientid,
8045 .renew = 1, /* append RENEW */
8047 struct nfs4_fsid_present_res res = {
8050 struct rpc_message msg = {
8051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8056 unsigned long now = jiffies;
8059 res.fh = nfs_alloc_fhandle();
8063 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8064 status = nfs4_call_sync_sequence(clnt, server, &msg,
8065 &args.seq_args, &res.seq_res);
8066 nfs_free_fhandle(res.fh);
8070 do_renew_lease(clp, now);
8074 #ifdef CONFIG_NFS_V4_1
8077 * This operation also signals the server that this client is
8078 * performing "lease moved" recovery. The server can stop asserting
8079 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8080 * this operation is identified in the SEQUENCE operation in this
8083 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8085 struct nfs_server *server = NFS_SERVER(inode);
8086 struct rpc_clnt *clnt = server->client;
8087 struct nfs4_fsid_present_arg args = {
8088 .fh = NFS_FH(inode),
8090 struct nfs4_fsid_present_res res = {
8092 struct rpc_message msg = {
8093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8100 res.fh = nfs_alloc_fhandle();
8104 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8105 status = nfs4_call_sync_sequence(clnt, server, &msg,
8106 &args.seq_args, &res.seq_res);
8107 nfs_free_fhandle(res.fh);
8108 if (status == NFS4_OK &&
8109 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8110 status = -NFS4ERR_LEASE_MOVED;
8114 #endif /* CONFIG_NFS_V4_1 */
8117 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8118 * @inode: inode on FSID to check
8119 * @cred: credential to use for this operation
8121 * Server indicates whether the FSID is present, moved, or not
8122 * recognized. This operation is necessary to clear a LEASE_MOVED
8123 * condition for this client ID.
8125 * Returns NFS4_OK if the FSID is present on this server,
8126 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8127 * NFS4ERR code if some error occurred on the server, or a
8128 * negative errno if a local failure occurred.
8130 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8132 struct nfs_server *server = NFS_SERVER(inode);
8133 struct nfs_client *clp = server->nfs_client;
8134 const struct nfs4_mig_recovery_ops *ops =
8135 clp->cl_mvops->mig_recovery_ops;
8136 struct nfs4_exception exception = {
8137 .interruptible = true,
8141 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8142 (unsigned long long)server->fsid.major,
8143 (unsigned long long)server->fsid.minor,
8145 nfs_display_fhandle(NFS_FH(inode), __func__);
8148 status = ops->fsid_present(inode, cred);
8149 if (status != -NFS4ERR_DELAY)
8151 nfs4_handle_exception(server, status, &exception);
8152 } while (exception.retry);
8157 * If 'use_integrity' is true and the state managment nfs_client
8158 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8159 * and the machine credential as per RFC3530bis and RFC5661 Security
8160 * Considerations sections. Otherwise, just use the user cred with the
8161 * filesystem's rpc_client.
8163 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8166 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8167 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8168 struct nfs4_secinfo_arg args = {
8169 .dir_fh = NFS_FH(dir),
8172 struct nfs4_secinfo_res res = {
8175 struct rpc_message msg = {
8176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8180 struct nfs4_call_sync_data data = {
8181 .seq_server = NFS_SERVER(dir),
8182 .seq_args = &args.seq_args,
8183 .seq_res = &res.seq_res,
8185 struct rpc_task_setup task_setup = {
8187 .rpc_message = &msg,
8188 .callback_ops = clp->cl_mvops->call_sync_ops,
8189 .callback_data = &data,
8190 .flags = RPC_TASK_NO_ROUND_ROBIN,
8192 const struct cred *cred = NULL;
8194 if (use_integrity) {
8195 clnt = clp->cl_rpcclient;
8196 task_setup.rpc_client = clnt;
8198 cred = nfs4_get_clid_cred(clp);
8199 msg.rpc_cred = cred;
8202 dprintk("NFS call secinfo %s\n", name->name);
8204 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8205 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8206 status = nfs4_call_sync_custom(&task_setup);
8208 dprintk("NFS reply secinfo: %d\n", status);
8214 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8215 struct nfs4_secinfo_flavors *flavors)
8217 struct nfs4_exception exception = {
8218 .interruptible = true,
8222 err = -NFS4ERR_WRONGSEC;
8224 /* try to use integrity protection with machine cred */
8225 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8226 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8229 * if unable to use integrity protection, or SECINFO with
8230 * integrity protection returns NFS4ERR_WRONGSEC (which is
8231 * disallowed by spec, but exists in deployed servers) use
8232 * the current filesystem's rpc_client and the user cred.
8234 if (err == -NFS4ERR_WRONGSEC)
8235 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8237 trace_nfs4_secinfo(dir, name, err);
8238 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8240 } while (exception.retry);
8244 #ifdef CONFIG_NFS_V4_1
8246 * Check the exchange flags returned by the server for invalid flags, having
8247 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8250 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8252 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8254 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8256 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8257 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8259 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8263 return -NFS4ERR_INVAL;
8267 nfs41_same_server_scope(struct nfs41_server_scope *a,
8268 struct nfs41_server_scope *b)
8270 if (a->server_scope_sz != b->server_scope_sz)
8272 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8276 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8278 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8279 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8280 struct nfs_client *clp = args->client;
8282 switch (task->tk_status) {
8283 case -NFS4ERR_BADSESSION:
8284 case -NFS4ERR_DEADSESSION:
8285 nfs4_schedule_session_recovery(clp->cl_session,
8288 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8289 res->dir != NFS4_CDFS4_BOTH) {
8290 rpc_task_close_connection(task);
8291 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8292 rpc_restart_call(task);
8296 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8297 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8301 * nfs4_proc_bind_one_conn_to_session()
8303 * The 4.1 client currently uses the same TCP connection for the
8304 * fore and backchannel.
8307 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8308 struct rpc_xprt *xprt,
8309 struct nfs_client *clp,
8310 const struct cred *cred)
8313 struct nfs41_bind_conn_to_session_args args = {
8315 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8318 struct nfs41_bind_conn_to_session_res res;
8319 struct rpc_message msg = {
8321 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8326 struct rpc_task_setup task_setup_data = {
8329 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8330 .rpc_message = &msg,
8331 .flags = RPC_TASK_TIMEOUT,
8333 struct rpc_task *task;
8335 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8336 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8337 args.dir = NFS4_CDFC4_FORE;
8339 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8340 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8341 args.dir = NFS4_CDFC4_FORE;
8343 task = rpc_run_task(&task_setup_data);
8344 if (!IS_ERR(task)) {
8345 status = task->tk_status;
8348 status = PTR_ERR(task);
8349 trace_nfs4_bind_conn_to_session(clp, status);
8351 if (memcmp(res.sessionid.data,
8352 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8353 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8356 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8357 dprintk("NFS: %s: Unexpected direction from server\n",
8361 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8362 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8371 struct rpc_bind_conn_calldata {
8372 struct nfs_client *clp;
8373 const struct cred *cred;
8377 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8378 struct rpc_xprt *xprt,
8381 struct rpc_bind_conn_calldata *p = calldata;
8383 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8386 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8388 struct rpc_bind_conn_calldata data = {
8392 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8393 nfs4_proc_bind_conn_to_session_callback, &data);
8397 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8398 * and operations we'd like to see to enable certain features in the allow map
8400 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8401 .how = SP4_MACH_CRED,
8402 .enforce.u.words = {
8403 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8404 1 << (OP_EXCHANGE_ID - 32) |
8405 1 << (OP_CREATE_SESSION - 32) |
8406 1 << (OP_DESTROY_SESSION - 32) |
8407 1 << (OP_DESTROY_CLIENTID - 32)
8410 [0] = 1 << (OP_CLOSE) |
8411 1 << (OP_OPEN_DOWNGRADE) |
8413 1 << (OP_DELEGRETURN) |
8415 [1] = 1 << (OP_SECINFO - 32) |
8416 1 << (OP_SECINFO_NO_NAME - 32) |
8417 1 << (OP_LAYOUTRETURN - 32) |
8418 1 << (OP_TEST_STATEID - 32) |
8419 1 << (OP_FREE_STATEID - 32) |
8420 1 << (OP_WRITE - 32)
8425 * Select the state protection mode for client `clp' given the server results
8426 * from exchange_id in `sp'.
8428 * Returns 0 on success, negative errno otherwise.
8430 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8431 struct nfs41_state_protection *sp)
8433 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8434 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8435 1 << (OP_EXCHANGE_ID - 32) |
8436 1 << (OP_CREATE_SESSION - 32) |
8437 1 << (OP_DESTROY_SESSION - 32) |
8438 1 << (OP_DESTROY_CLIENTID - 32)
8440 unsigned long flags = 0;
8444 if (sp->how == SP4_MACH_CRED) {
8445 /* Print state protect result */
8446 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8447 for (i = 0; i <= LAST_NFS4_OP; i++) {
8448 if (test_bit(i, sp->enforce.u.longs))
8449 dfprintk(MOUNT, " enforce op %d\n", i);
8450 if (test_bit(i, sp->allow.u.longs))
8451 dfprintk(MOUNT, " allow op %d\n", i);
8454 /* make sure nothing is on enforce list that isn't supported */
8455 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8456 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8457 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8464 * Minimal mode - state operations are allowed to use machine
8465 * credential. Note this already happens by default, so the
8466 * client doesn't have to do anything more than the negotiation.
8468 * NOTE: we don't care if EXCHANGE_ID is in the list -
8469 * we're already using the machine cred for exchange_id
8470 * and will never use a different cred.
8472 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8473 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8474 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8475 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8476 dfprintk(MOUNT, "sp4_mach_cred:\n");
8477 dfprintk(MOUNT, " minimal mode enabled\n");
8478 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8480 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8485 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8486 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8487 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8488 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8489 dfprintk(MOUNT, " cleanup mode enabled\n");
8490 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8493 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8494 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8495 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8498 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8499 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8500 dfprintk(MOUNT, " secinfo mode enabled\n");
8501 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8504 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8505 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8506 dfprintk(MOUNT, " stateid mode enabled\n");
8507 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8510 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8511 dfprintk(MOUNT, " write mode enabled\n");
8512 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8515 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8516 dfprintk(MOUNT, " commit mode enabled\n");
8517 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8521 clp->cl_sp4_flags = flags;
8525 struct nfs41_exchange_id_data {
8526 struct nfs41_exchange_id_res res;
8527 struct nfs41_exchange_id_args args;
8530 static void nfs4_exchange_id_release(void *data)
8532 struct nfs41_exchange_id_data *cdata =
8533 (struct nfs41_exchange_id_data *)data;
8535 nfs_put_client(cdata->args.client);
8536 kfree(cdata->res.impl_id);
8537 kfree(cdata->res.server_scope);
8538 kfree(cdata->res.server_owner);
8542 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8543 .rpc_release = nfs4_exchange_id_release,
8547 * _nfs4_proc_exchange_id()
8549 * Wrapper for EXCHANGE_ID operation.
8551 static struct rpc_task *
8552 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8553 u32 sp4_how, struct rpc_xprt *xprt)
8555 struct rpc_message msg = {
8556 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8559 struct rpc_task_setup task_setup_data = {
8560 .rpc_client = clp->cl_rpcclient,
8561 .callback_ops = &nfs4_exchange_id_call_ops,
8562 .rpc_message = &msg,
8563 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8565 struct nfs41_exchange_id_data *calldata;
8568 if (!refcount_inc_not_zero(&clp->cl_count))
8569 return ERR_PTR(-EIO);
8572 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8576 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8578 status = nfs4_init_uniform_client_string(clp);
8582 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8585 if (unlikely(calldata->res.server_owner == NULL))
8588 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8590 if (unlikely(calldata->res.server_scope == NULL))
8591 goto out_server_owner;
8593 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8594 if (unlikely(calldata->res.impl_id == NULL))
8595 goto out_server_scope;
8599 calldata->args.state_protect.how = SP4_NONE;
8603 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8613 task_setup_data.rpc_xprt = xprt;
8614 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8615 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8616 sizeof(calldata->args.verifier.data));
8618 calldata->args.client = clp;
8619 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8620 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8621 #ifdef CONFIG_NFS_V4_1_MIGRATION
8622 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8624 msg.rpc_argp = &calldata->args;
8625 msg.rpc_resp = &calldata->res;
8626 task_setup_data.callback_data = calldata;
8628 return rpc_run_task(&task_setup_data);
8631 kfree(calldata->res.impl_id);
8633 kfree(calldata->res.server_scope);
8635 kfree(calldata->res.server_owner);
8639 nfs_put_client(clp);
8640 return ERR_PTR(status);
8644 * _nfs4_proc_exchange_id()
8646 * Wrapper for EXCHANGE_ID operation.
8648 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8651 struct rpc_task *task;
8652 struct nfs41_exchange_id_args *argp;
8653 struct nfs41_exchange_id_res *resp;
8654 unsigned long now = jiffies;
8657 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8659 return PTR_ERR(task);
8661 argp = task->tk_msg.rpc_argp;
8662 resp = task->tk_msg.rpc_resp;
8663 status = task->tk_status;
8667 status = nfs4_check_cl_exchange_flags(resp->flags,
8668 clp->cl_mvops->minor_version);
8672 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8676 do_renew_lease(clp, now);
8678 clp->cl_clientid = resp->clientid;
8679 clp->cl_exchange_flags = resp->flags;
8680 clp->cl_seqid = resp->seqid;
8681 /* Client ID is not confirmed */
8682 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8683 clear_bit(NFS4_SESSION_ESTABLISHED,
8684 &clp->cl_session->session_state);
8686 if (clp->cl_serverscope != NULL &&
8687 !nfs41_same_server_scope(clp->cl_serverscope,
8688 resp->server_scope)) {
8689 dprintk("%s: server_scope mismatch detected\n",
8691 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8694 swap(clp->cl_serverowner, resp->server_owner);
8695 swap(clp->cl_serverscope, resp->server_scope);
8696 swap(clp->cl_implid, resp->impl_id);
8698 /* Save the EXCHANGE_ID verifier session trunk tests */
8699 memcpy(clp->cl_confirm.data, argp->verifier.data,
8700 sizeof(clp->cl_confirm.data));
8702 trace_nfs4_exchange_id(clp, status);
8708 * nfs4_proc_exchange_id()
8710 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8712 * Since the clientid has expired, all compounds using sessions
8713 * associated with the stale clientid will be returning
8714 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8715 * be in some phase of session reset.
8717 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8719 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8721 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8724 /* try SP4_MACH_CRED if krb5i/p */
8725 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8726 authflavor == RPC_AUTH_GSS_KRB5P) {
8727 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8733 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8737 * nfs4_test_session_trunk
8739 * This is an add_xprt_test() test function called from
8740 * rpc_clnt_setup_test_and_add_xprt.
8742 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8743 * and is dereferrenced in nfs4_exchange_id_release
8745 * Upon success, add the new transport to the rpc_clnt
8747 * @clnt: struct rpc_clnt to get new transport
8748 * @xprt: the rpc_xprt to test
8749 * @data: call data for _nfs4_proc_exchange_id.
8751 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8754 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8755 struct rpc_task *task;
8760 dprintk("--> %s try %s\n", __func__,
8761 xprt->address_strings[RPC_DISPLAY_ADDR]);
8763 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8765 /* Test connection for session trunking. Async exchange_id call */
8766 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8770 status = task->tk_status;
8772 status = nfs4_detect_session_trunking(adata->clp,
8773 task->tk_msg.rpc_resp, xprt);
8776 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8780 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8782 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8783 const struct cred *cred)
8785 struct rpc_message msg = {
8786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8792 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8793 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8794 trace_nfs4_destroy_clientid(clp, status);
8796 dprintk("NFS: Got error %d from the server %s on "
8797 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8801 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8802 const struct cred *cred)
8807 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8808 ret = _nfs4_proc_destroy_clientid(clp, cred);
8810 case -NFS4ERR_DELAY:
8811 case -NFS4ERR_CLIENTID_BUSY:
8821 int nfs4_destroy_clientid(struct nfs_client *clp)
8823 const struct cred *cred;
8826 if (clp->cl_mvops->minor_version < 1)
8828 if (clp->cl_exchange_flags == 0)
8830 if (clp->cl_preserve_clid)
8832 cred = nfs4_get_clid_cred(clp);
8833 ret = nfs4_proc_destroy_clientid(clp, cred);
8837 case -NFS4ERR_STALE_CLIENTID:
8838 clp->cl_exchange_flags = 0;
8844 #endif /* CONFIG_NFS_V4_1 */
8846 struct nfs4_get_lease_time_data {
8847 struct nfs4_get_lease_time_args *args;
8848 struct nfs4_get_lease_time_res *res;
8849 struct nfs_client *clp;
8852 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8855 struct nfs4_get_lease_time_data *data =
8856 (struct nfs4_get_lease_time_data *)calldata;
8858 dprintk("--> %s\n", __func__);
8859 /* just setup sequence, do not trigger session recovery
8860 since we're invoked within one */
8861 nfs4_setup_sequence(data->clp,
8862 &data->args->la_seq_args,
8863 &data->res->lr_seq_res,
8865 dprintk("<-- %s\n", __func__);
8869 * Called from nfs4_state_manager thread for session setup, so don't recover
8870 * from sequence operation or clientid errors.
8872 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8874 struct nfs4_get_lease_time_data *data =
8875 (struct nfs4_get_lease_time_data *)calldata;
8877 dprintk("--> %s\n", __func__);
8878 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8880 switch (task->tk_status) {
8881 case -NFS4ERR_DELAY:
8882 case -NFS4ERR_GRACE:
8883 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8884 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8885 task->tk_status = 0;
8887 case -NFS4ERR_RETRY_UNCACHED_REP:
8888 rpc_restart_call_prepare(task);
8891 dprintk("<-- %s\n", __func__);
8894 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8895 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8896 .rpc_call_done = nfs4_get_lease_time_done,
8899 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8901 struct nfs4_get_lease_time_args args;
8902 struct nfs4_get_lease_time_res res = {
8903 .lr_fsinfo = fsinfo,
8905 struct nfs4_get_lease_time_data data = {
8910 struct rpc_message msg = {
8911 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8915 struct rpc_task_setup task_setup = {
8916 .rpc_client = clp->cl_rpcclient,
8917 .rpc_message = &msg,
8918 .callback_ops = &nfs4_get_lease_time_ops,
8919 .callback_data = &data,
8920 .flags = RPC_TASK_TIMEOUT,
8923 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8924 return nfs4_call_sync_custom(&task_setup);
8927 #ifdef CONFIG_NFS_V4_1
8930 * Initialize the values to be used by the client in CREATE_SESSION
8931 * If nfs4_init_session set the fore channel request and response sizes,
8934 * Set the back channel max_resp_sz_cached to zero to force the client to
8935 * always set csa_cachethis to FALSE because the current implementation
8936 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8938 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8939 struct rpc_clnt *clnt)
8941 unsigned int max_rqst_sz, max_resp_sz;
8942 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8943 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8945 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8946 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8948 /* Fore channel attributes */
8949 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8950 args->fc_attrs.max_resp_sz = max_resp_sz;
8951 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8952 args->fc_attrs.max_reqs = max_session_slots;
8954 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8955 "max_ops=%u max_reqs=%u\n",
8957 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8958 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8960 /* Back channel attributes */
8961 args->bc_attrs.max_rqst_sz = max_bc_payload;
8962 args->bc_attrs.max_resp_sz = max_bc_payload;
8963 args->bc_attrs.max_resp_sz_cached = 0;
8964 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8965 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8966 if (args->bc_attrs.max_reqs > max_bc_slots)
8967 args->bc_attrs.max_reqs = max_bc_slots;
8969 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8970 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8972 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8973 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8974 args->bc_attrs.max_reqs);
8977 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8978 struct nfs41_create_session_res *res)
8980 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8981 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8983 if (rcvd->max_resp_sz > sent->max_resp_sz)
8986 * Our requested max_ops is the minimum we need; we're not
8987 * prepared to break up compounds into smaller pieces than that.
8988 * So, no point even trying to continue if the server won't
8991 if (rcvd->max_ops < sent->max_ops)
8993 if (rcvd->max_reqs == 0)
8995 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8996 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
9000 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
9001 struct nfs41_create_session_res *res)
9003 struct nfs4_channel_attrs *sent = &args->bc_attrs;
9004 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
9006 if (!(res->flags & SESSION4_BACK_CHAN))
9008 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
9010 if (rcvd->max_resp_sz < sent->max_resp_sz)
9012 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
9014 if (rcvd->max_ops > sent->max_ops)
9016 if (rcvd->max_reqs > sent->max_reqs)
9022 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
9023 struct nfs41_create_session_res *res)
9027 ret = nfs4_verify_fore_channel_attrs(args, res);
9030 return nfs4_verify_back_channel_attrs(args, res);
9033 static void nfs4_update_session(struct nfs4_session *session,
9034 struct nfs41_create_session_res *res)
9036 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
9037 /* Mark client id and session as being confirmed */
9038 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
9039 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
9040 session->flags = res->flags;
9041 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
9042 if (res->flags & SESSION4_BACK_CHAN)
9043 memcpy(&session->bc_attrs, &res->bc_attrs,
9044 sizeof(session->bc_attrs));
9047 static int _nfs4_proc_create_session(struct nfs_client *clp,
9048 const struct cred *cred)
9050 struct nfs4_session *session = clp->cl_session;
9051 struct nfs41_create_session_args args = {
9053 .clientid = clp->cl_clientid,
9054 .seqid = clp->cl_seqid,
9055 .cb_program = NFS4_CALLBACK,
9057 struct nfs41_create_session_res res;
9059 struct rpc_message msg = {
9060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9067 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
9068 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
9070 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9071 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9072 trace_nfs4_create_session(clp, status);
9075 case -NFS4ERR_STALE_CLIENTID:
9076 case -NFS4ERR_DELAY:
9085 /* Verify the session's negotiated channel_attrs values */
9086 status = nfs4_verify_channel_attrs(&args, &res);
9087 /* Increment the clientid slot sequence id */
9090 nfs4_update_session(session, &res);
9097 * Issues a CREATE_SESSION operation to the server.
9098 * It is the responsibility of the caller to verify the session is
9099 * expired before calling this routine.
9101 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9105 struct nfs4_session *session = clp->cl_session;
9107 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9109 status = _nfs4_proc_create_session(clp, cred);
9113 /* Init or reset the session slot tables */
9114 status = nfs4_setup_session_slot_tables(session);
9115 dprintk("slot table setup returned %d\n", status);
9119 ptr = (unsigned *)&session->sess_id.data[0];
9120 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9121 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9123 dprintk("<-- %s\n", __func__);
9128 * Issue the over-the-wire RPC DESTROY_SESSION.
9129 * The caller must serialize access to this routine.
9131 int nfs4_proc_destroy_session(struct nfs4_session *session,
9132 const struct cred *cred)
9134 struct rpc_message msg = {
9135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9136 .rpc_argp = session,
9141 dprintk("--> nfs4_proc_destroy_session\n");
9143 /* session is still being setup */
9144 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9147 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9148 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9149 trace_nfs4_destroy_session(session->clp, status);
9152 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9153 "Session has been destroyed regardless...\n", status);
9155 dprintk("<-- nfs4_proc_destroy_session\n");
9160 * Renew the cl_session lease.
9162 struct nfs4_sequence_data {
9163 struct nfs_client *clp;
9164 struct nfs4_sequence_args args;
9165 struct nfs4_sequence_res res;
9168 static void nfs41_sequence_release(void *data)
9170 struct nfs4_sequence_data *calldata = data;
9171 struct nfs_client *clp = calldata->clp;
9173 if (refcount_read(&clp->cl_count) > 1)
9174 nfs4_schedule_state_renewal(clp);
9175 nfs_put_client(clp);
9179 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9181 switch(task->tk_status) {
9182 case -NFS4ERR_DELAY:
9183 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9186 nfs4_schedule_lease_recovery(clp);
9191 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9193 struct nfs4_sequence_data *calldata = data;
9194 struct nfs_client *clp = calldata->clp;
9196 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9199 trace_nfs4_sequence(clp, task->tk_status);
9200 if (task->tk_status < 0) {
9201 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9202 if (refcount_read(&clp->cl_count) == 1)
9205 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9206 rpc_restart_call_prepare(task);
9210 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9212 dprintk("<-- %s\n", __func__);
9215 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9217 struct nfs4_sequence_data *calldata = data;
9218 struct nfs_client *clp = calldata->clp;
9219 struct nfs4_sequence_args *args;
9220 struct nfs4_sequence_res *res;
9222 args = task->tk_msg.rpc_argp;
9223 res = task->tk_msg.rpc_resp;
9225 nfs4_setup_sequence(clp, args, res, task);
9228 static const struct rpc_call_ops nfs41_sequence_ops = {
9229 .rpc_call_done = nfs41_sequence_call_done,
9230 .rpc_call_prepare = nfs41_sequence_prepare,
9231 .rpc_release = nfs41_sequence_release,
9234 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9235 const struct cred *cred,
9236 struct nfs4_slot *slot,
9239 struct nfs4_sequence_data *calldata;
9240 struct rpc_message msg = {
9241 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9244 struct rpc_task_setup task_setup_data = {
9245 .rpc_client = clp->cl_rpcclient,
9246 .rpc_message = &msg,
9247 .callback_ops = &nfs41_sequence_ops,
9248 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
9250 struct rpc_task *ret;
9252 ret = ERR_PTR(-EIO);
9253 if (!refcount_inc_not_zero(&clp->cl_count))
9256 ret = ERR_PTR(-ENOMEM);
9257 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9258 if (calldata == NULL)
9260 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9261 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9262 msg.rpc_argp = &calldata->args;
9263 msg.rpc_resp = &calldata->res;
9264 calldata->clp = clp;
9265 task_setup_data.callback_data = calldata;
9267 ret = rpc_run_task(&task_setup_data);
9272 nfs_put_client(clp);
9274 nfs41_release_slot(slot);
9278 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9280 struct rpc_task *task;
9283 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9285 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9287 ret = PTR_ERR(task);
9289 rpc_put_task_async(task);
9290 dprintk("<-- %s status=%d\n", __func__, ret);
9294 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9296 struct rpc_task *task;
9299 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9301 ret = PTR_ERR(task);
9304 ret = rpc_wait_for_completion_task(task);
9306 ret = task->tk_status;
9309 dprintk("<-- %s status=%d\n", __func__, ret);
9313 struct nfs4_reclaim_complete_data {
9314 struct nfs_client *clp;
9315 struct nfs41_reclaim_complete_args arg;
9316 struct nfs41_reclaim_complete_res res;
9319 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9321 struct nfs4_reclaim_complete_data *calldata = data;
9323 nfs4_setup_sequence(calldata->clp,
9324 &calldata->arg.seq_args,
9325 &calldata->res.seq_res,
9329 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9331 switch(task->tk_status) {
9333 wake_up_all(&clp->cl_lock_waitq);
9335 case -NFS4ERR_COMPLETE_ALREADY:
9336 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9338 case -NFS4ERR_DELAY:
9339 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9341 case -NFS4ERR_RETRY_UNCACHED_REP:
9343 case -NFS4ERR_BADSESSION:
9344 case -NFS4ERR_DEADSESSION:
9345 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9348 nfs4_schedule_lease_recovery(clp);
9353 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9355 struct nfs4_reclaim_complete_data *calldata = data;
9356 struct nfs_client *clp = calldata->clp;
9357 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9359 dprintk("--> %s\n", __func__);
9360 if (!nfs41_sequence_done(task, res))
9363 trace_nfs4_reclaim_complete(clp, task->tk_status);
9364 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9365 rpc_restart_call_prepare(task);
9368 dprintk("<-- %s\n", __func__);
9371 static void nfs4_free_reclaim_complete_data(void *data)
9373 struct nfs4_reclaim_complete_data *calldata = data;
9378 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9379 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9380 .rpc_call_done = nfs4_reclaim_complete_done,
9381 .rpc_release = nfs4_free_reclaim_complete_data,
9385 * Issue a global reclaim complete.
9387 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9388 const struct cred *cred)
9390 struct nfs4_reclaim_complete_data *calldata;
9391 struct rpc_message msg = {
9392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9395 struct rpc_task_setup task_setup_data = {
9396 .rpc_client = clp->cl_rpcclient,
9397 .rpc_message = &msg,
9398 .callback_ops = &nfs4_reclaim_complete_call_ops,
9399 .flags = RPC_TASK_NO_ROUND_ROBIN,
9401 int status = -ENOMEM;
9403 dprintk("--> %s\n", __func__);
9404 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9405 if (calldata == NULL)
9407 calldata->clp = clp;
9408 calldata->arg.one_fs = 0;
9410 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9411 msg.rpc_argp = &calldata->arg;
9412 msg.rpc_resp = &calldata->res;
9413 task_setup_data.callback_data = calldata;
9414 status = nfs4_call_sync_custom(&task_setup_data);
9416 dprintk("<-- %s status=%d\n", __func__, status);
9421 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9423 struct nfs4_layoutget *lgp = calldata;
9424 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9426 dprintk("--> %s\n", __func__);
9427 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9428 &lgp->res.seq_res, task);
9429 dprintk("<-- %s\n", __func__);
9432 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9434 struct nfs4_layoutget *lgp = calldata;
9436 dprintk("--> %s\n", __func__);
9437 nfs41_sequence_process(task, &lgp->res.seq_res);
9438 dprintk("<-- %s\n", __func__);
9442 nfs4_layoutget_handle_exception(struct rpc_task *task,
9443 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9445 struct inode *inode = lgp->args.inode;
9446 struct nfs_server *server = NFS_SERVER(inode);
9447 struct pnfs_layout_hdr *lo = lgp->lo;
9448 int nfs4err = task->tk_status;
9449 int err, status = 0;
9452 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9454 nfs4_sequence_free_slot(&lgp->res.seq_res);
9461 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9462 * on the file. set tk_status to -ENODATA to tell upper layer to
9465 case -NFS4ERR_LAYOUTUNAVAILABLE:
9469 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9470 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9472 case -NFS4ERR_BADLAYOUT:
9473 status = -EOVERFLOW;
9476 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9477 * (or clients) writing to the same RAID stripe except when
9478 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9480 * Treat it like we would RECALLCONFLICT -- we retry for a little
9481 * while, and then eventually give up.
9483 case -NFS4ERR_LAYOUTTRYLATER:
9484 if (lgp->args.minlength == 0) {
9485 status = -EOVERFLOW;
9490 case -NFS4ERR_RECALLCONFLICT:
9491 status = -ERECALLCONFLICT;
9493 case -NFS4ERR_DELEG_REVOKED:
9494 case -NFS4ERR_ADMIN_REVOKED:
9495 case -NFS4ERR_EXPIRED:
9496 case -NFS4ERR_BAD_STATEID:
9497 exception->timeout = 0;
9498 spin_lock(&inode->i_lock);
9499 /* If the open stateid was bad, then recover it. */
9500 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9501 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9502 spin_unlock(&inode->i_lock);
9503 exception->state = lgp->args.ctx->state;
9504 exception->stateid = &lgp->args.stateid;
9509 * Mark the bad layout state as invalid, then retry
9511 pnfs_mark_layout_stateid_invalid(lo, &head);
9512 spin_unlock(&inode->i_lock);
9513 nfs_commit_inode(inode, 0);
9514 pnfs_free_lseg_list(&head);
9519 err = nfs4_handle_exception(server, nfs4err, exception);
9521 if (exception->retry)
9527 dprintk("<-- %s\n", __func__);
9531 size_t max_response_pages(struct nfs_server *server)
9533 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9534 return nfs_page_array_len(0, max_resp_sz);
9537 static void nfs4_layoutget_release(void *calldata)
9539 struct nfs4_layoutget *lgp = calldata;
9541 dprintk("--> %s\n", __func__);
9542 nfs4_sequence_free_slot(&lgp->res.seq_res);
9543 pnfs_layoutget_free(lgp);
9544 dprintk("<-- %s\n", __func__);
9547 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9548 .rpc_call_prepare = nfs4_layoutget_prepare,
9549 .rpc_call_done = nfs4_layoutget_done,
9550 .rpc_release = nfs4_layoutget_release,
9553 struct pnfs_layout_segment *
9554 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9556 struct inode *inode = lgp->args.inode;
9557 struct nfs_server *server = NFS_SERVER(inode);
9558 struct rpc_task *task;
9559 struct rpc_message msg = {
9560 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9561 .rpc_argp = &lgp->args,
9562 .rpc_resp = &lgp->res,
9563 .rpc_cred = lgp->cred,
9565 struct rpc_task_setup task_setup_data = {
9566 .rpc_client = server->client,
9567 .rpc_message = &msg,
9568 .callback_ops = &nfs4_layoutget_call_ops,
9569 .callback_data = lgp,
9570 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
9573 struct pnfs_layout_segment *lseg = NULL;
9574 struct nfs4_exception exception = {
9576 .timeout = *timeout,
9580 dprintk("--> %s\n", __func__);
9582 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9584 task = rpc_run_task(&task_setup_data);
9586 status = rpc_wait_for_completion_task(task);
9590 if (task->tk_status < 0) {
9591 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9592 *timeout = exception.timeout;
9593 } else if (lgp->res.layoutp->len == 0) {
9595 *timeout = nfs4_update_delay(&exception.timeout);
9597 lseg = pnfs_layout_process(lgp);
9599 trace_nfs4_layoutget(lgp->args.ctx,
9606 dprintk("<-- %s status=%d\n", __func__, status);
9608 return ERR_PTR(status);
9613 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9615 struct nfs4_layoutreturn *lrp = calldata;
9617 dprintk("--> %s\n", __func__);
9618 nfs4_setup_sequence(lrp->clp,
9619 &lrp->args.seq_args,
9622 if (!pnfs_layout_is_valid(lrp->args.layout))
9626 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9628 struct nfs4_layoutreturn *lrp = calldata;
9629 struct nfs_server *server;
9631 dprintk("--> %s\n", __func__);
9633 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9637 * Was there an RPC level error? Assume the call succeeded,
9638 * and that we need to release the layout
9640 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9641 lrp->res.lrs_present = 0;
9645 server = NFS_SERVER(lrp->args.inode);
9646 switch (task->tk_status) {
9647 case -NFS4ERR_OLD_STATEID:
9648 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9654 task->tk_status = 0;
9658 case -NFS4ERR_DELAY:
9659 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9663 dprintk("<-- %s\n", __func__);
9666 task->tk_status = 0;
9667 nfs4_sequence_free_slot(&lrp->res.seq_res);
9668 rpc_restart_call_prepare(task);
9671 static void nfs4_layoutreturn_release(void *calldata)
9673 struct nfs4_layoutreturn *lrp = calldata;
9674 struct pnfs_layout_hdr *lo = lrp->args.layout;
9676 dprintk("--> %s\n", __func__);
9677 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9678 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9679 nfs4_sequence_free_slot(&lrp->res.seq_res);
9680 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9681 lrp->ld_private.ops->free(&lrp->ld_private);
9682 pnfs_put_layout_hdr(lrp->args.layout);
9683 nfs_iput_and_deactive(lrp->inode);
9684 put_cred(lrp->cred);
9686 dprintk("<-- %s\n", __func__);
9689 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9690 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9691 .rpc_call_done = nfs4_layoutreturn_done,
9692 .rpc_release = nfs4_layoutreturn_release,
9695 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9697 struct rpc_task *task;
9698 struct rpc_message msg = {
9699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9700 .rpc_argp = &lrp->args,
9701 .rpc_resp = &lrp->res,
9702 .rpc_cred = lrp->cred,
9704 struct rpc_task_setup task_setup_data = {
9705 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9706 .rpc_message = &msg,
9707 .callback_ops = &nfs4_layoutreturn_call_ops,
9708 .callback_data = lrp,
9709 .flags = RPC_TASK_MOVEABLE,
9713 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9714 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9715 &task_setup_data.rpc_client, &msg);
9717 dprintk("--> %s\n", __func__);
9718 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9721 nfs4_layoutreturn_release(lrp);
9724 task_setup_data.flags |= RPC_TASK_ASYNC;
9727 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9730 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9732 task = rpc_run_task(&task_setup_data);
9734 return PTR_ERR(task);
9736 status = task->tk_status;
9737 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9738 dprintk("<-- %s status=%d\n", __func__, status);
9744 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9745 struct pnfs_device *pdev,
9746 const struct cred *cred)
9748 struct nfs4_getdeviceinfo_args args = {
9750 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9751 NOTIFY_DEVICEID4_DELETE,
9753 struct nfs4_getdeviceinfo_res res = {
9756 struct rpc_message msg = {
9757 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9764 dprintk("--> %s\n", __func__);
9765 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9766 if (res.notification & ~args.notify_types)
9767 dprintk("%s: unsupported notification\n", __func__);
9768 if (res.notification != args.notify_types)
9771 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9773 dprintk("<-- %s status=%d\n", __func__, status);
9778 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9779 struct pnfs_device *pdev,
9780 const struct cred *cred)
9782 struct nfs4_exception exception = { };
9786 err = nfs4_handle_exception(server,
9787 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9789 } while (exception.retry);
9792 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9794 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9796 struct nfs4_layoutcommit_data *data = calldata;
9797 struct nfs_server *server = NFS_SERVER(data->args.inode);
9799 nfs4_setup_sequence(server->nfs_client,
9800 &data->args.seq_args,
9806 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9808 struct nfs4_layoutcommit_data *data = calldata;
9809 struct nfs_server *server = NFS_SERVER(data->args.inode);
9811 if (!nfs41_sequence_done(task, &data->res.seq_res))
9814 switch (task->tk_status) { /* Just ignore these failures */
9815 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9816 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9817 case -NFS4ERR_BADLAYOUT: /* no layout */
9818 case -NFS4ERR_GRACE: /* loca_recalim always false */
9819 task->tk_status = 0;
9824 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9825 rpc_restart_call_prepare(task);
9831 static void nfs4_layoutcommit_release(void *calldata)
9833 struct nfs4_layoutcommit_data *data = calldata;
9835 pnfs_cleanup_layoutcommit(data);
9836 nfs_post_op_update_inode_force_wcc(data->args.inode,
9838 put_cred(data->cred);
9839 nfs_iput_and_deactive(data->inode);
9843 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9844 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9845 .rpc_call_done = nfs4_layoutcommit_done,
9846 .rpc_release = nfs4_layoutcommit_release,
9850 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9852 struct rpc_message msg = {
9853 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9854 .rpc_argp = &data->args,
9855 .rpc_resp = &data->res,
9856 .rpc_cred = data->cred,
9858 struct rpc_task_setup task_setup_data = {
9859 .task = &data->task,
9860 .rpc_client = NFS_CLIENT(data->args.inode),
9861 .rpc_message = &msg,
9862 .callback_ops = &nfs4_layoutcommit_ops,
9863 .callback_data = data,
9864 .flags = RPC_TASK_MOVEABLE,
9866 struct rpc_task *task;
9869 dprintk("NFS: initiating layoutcommit call. sync %d "
9870 "lbw: %llu inode %lu\n", sync,
9871 data->args.lastbytewritten,
9872 data->args.inode->i_ino);
9875 data->inode = nfs_igrab_and_active(data->args.inode);
9876 if (data->inode == NULL) {
9877 nfs4_layoutcommit_release(data);
9880 task_setup_data.flags = RPC_TASK_ASYNC;
9882 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9883 task = rpc_run_task(&task_setup_data);
9885 return PTR_ERR(task);
9887 status = task->tk_status;
9888 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9889 dprintk("%s: status %d\n", __func__, status);
9895 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9896 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9899 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9900 struct nfs_fsinfo *info,
9901 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9903 struct nfs41_secinfo_no_name_args args = {
9904 .style = SECINFO_STYLE_CURRENT_FH,
9906 struct nfs4_secinfo_res res = {
9909 struct rpc_message msg = {
9910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9914 struct nfs4_call_sync_data data = {
9915 .seq_server = server,
9916 .seq_args = &args.seq_args,
9917 .seq_res = &res.seq_res,
9919 struct rpc_task_setup task_setup = {
9920 .rpc_client = server->client,
9921 .rpc_message = &msg,
9922 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9923 .callback_data = &data,
9924 .flags = RPC_TASK_NO_ROUND_ROBIN,
9926 const struct cred *cred = NULL;
9929 if (use_integrity) {
9930 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
9932 cred = nfs4_get_clid_cred(server->nfs_client);
9933 msg.rpc_cred = cred;
9936 dprintk("--> %s\n", __func__);
9937 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9938 status = nfs4_call_sync_custom(&task_setup);
9939 dprintk("<-- %s status=%d\n", __func__, status);
9947 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9948 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9950 struct nfs4_exception exception = {
9951 .interruptible = true,
9955 /* first try using integrity protection */
9956 err = -NFS4ERR_WRONGSEC;
9958 /* try to use integrity protection with machine cred */
9959 if (_nfs4_is_integrity_protected(server->nfs_client))
9960 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9964 * if unable to use integrity protection, or SECINFO with
9965 * integrity protection returns NFS4ERR_WRONGSEC (which is
9966 * disallowed by spec, but exists in deployed servers) use
9967 * the current filesystem's rpc_client and the user cred.
9969 if (err == -NFS4ERR_WRONGSEC)
9970 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9975 case -NFS4ERR_WRONGSEC:
9979 err = nfs4_handle_exception(server, err, &exception);
9981 } while (exception.retry);
9987 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9988 struct nfs_fsinfo *info)
9992 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9993 struct nfs4_secinfo_flavors *flavors;
9994 struct nfs4_secinfo4 *secinfo;
9997 page = alloc_page(GFP_KERNEL);
10003 flavors = page_address(page);
10004 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
10007 * Fall back on "guess and check" method if
10008 * the server doesn't support SECINFO_NO_NAME
10010 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
10011 err = nfs4_find_root_sec(server, fhandle, info);
10017 for (i = 0; i < flavors->num_flavors; i++) {
10018 secinfo = &flavors->flavors[i];
10020 switch (secinfo->flavor) {
10021 case RPC_AUTH_NULL:
10022 case RPC_AUTH_UNIX:
10024 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
10025 &secinfo->flavor_info);
10028 flavor = RPC_AUTH_MAXFLAVOR;
10032 if (!nfs_auth_info_match(&server->auth_info, flavor))
10033 flavor = RPC_AUTH_MAXFLAVOR;
10035 if (flavor != RPC_AUTH_MAXFLAVOR) {
10036 err = nfs4_lookup_root_sec(server, fhandle,
10043 if (flavor == RPC_AUTH_MAXFLAVOR)
10048 if (err == -EACCES)
10054 static int _nfs41_test_stateid(struct nfs_server *server,
10055 nfs4_stateid *stateid,
10056 const struct cred *cred)
10059 struct nfs41_test_stateid_args args = {
10060 .stateid = stateid,
10062 struct nfs41_test_stateid_res res;
10063 struct rpc_message msg = {
10064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10069 struct rpc_clnt *rpc_client = server->client;
10071 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10072 &rpc_client, &msg);
10074 dprintk("NFS call test_stateid %p\n", stateid);
10075 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
10076 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
10077 &args.seq_args, &res.seq_res);
10078 if (status != NFS_OK) {
10079 dprintk("NFS reply test_stateid: failed, %d\n", status);
10082 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10083 return -res.status;
10086 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10087 int err, struct nfs4_exception *exception)
10089 exception->retry = 0;
10091 case -NFS4ERR_DELAY:
10092 case -NFS4ERR_RETRY_UNCACHED_REP:
10093 nfs4_handle_exception(server, err, exception);
10095 case -NFS4ERR_BADSESSION:
10096 case -NFS4ERR_BADSLOT:
10097 case -NFS4ERR_BAD_HIGH_SLOT:
10098 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10099 case -NFS4ERR_DEADSESSION:
10100 nfs4_do_handle_exception(server, err, exception);
10105 * nfs41_test_stateid - perform a TEST_STATEID operation
10107 * @server: server / transport on which to perform the operation
10108 * @stateid: state ID to test
10109 * @cred: credential
10111 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10112 * Otherwise a negative NFS4ERR value is returned if the operation
10113 * failed or the state ID is not currently valid.
10115 static int nfs41_test_stateid(struct nfs_server *server,
10116 nfs4_stateid *stateid,
10117 const struct cred *cred)
10119 struct nfs4_exception exception = {
10120 .interruptible = true,
10124 err = _nfs41_test_stateid(server, stateid, cred);
10125 nfs4_handle_delay_or_session_error(server, err, &exception);
10126 } while (exception.retry);
10130 struct nfs_free_stateid_data {
10131 struct nfs_server *server;
10132 struct nfs41_free_stateid_args args;
10133 struct nfs41_free_stateid_res res;
10136 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10138 struct nfs_free_stateid_data *data = calldata;
10139 nfs4_setup_sequence(data->server->nfs_client,
10140 &data->args.seq_args,
10141 &data->res.seq_res,
10145 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10147 struct nfs_free_stateid_data *data = calldata;
10149 nfs41_sequence_done(task, &data->res.seq_res);
10151 switch (task->tk_status) {
10152 case -NFS4ERR_DELAY:
10153 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10154 rpc_restart_call_prepare(task);
10158 static void nfs41_free_stateid_release(void *calldata)
10163 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10164 .rpc_call_prepare = nfs41_free_stateid_prepare,
10165 .rpc_call_done = nfs41_free_stateid_done,
10166 .rpc_release = nfs41_free_stateid_release,
10170 * nfs41_free_stateid - perform a FREE_STATEID operation
10172 * @server: server / transport on which to perform the operation
10173 * @stateid: state ID to release
10174 * @cred: credential
10175 * @privileged: set to true if this call needs to be privileged
10177 * Note: this function is always asynchronous.
10179 static int nfs41_free_stateid(struct nfs_server *server,
10180 const nfs4_stateid *stateid,
10181 const struct cred *cred,
10184 struct rpc_message msg = {
10185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10188 struct rpc_task_setup task_setup = {
10189 .rpc_client = server->client,
10190 .rpc_message = &msg,
10191 .callback_ops = &nfs41_free_stateid_ops,
10192 .flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
10194 struct nfs_free_stateid_data *data;
10195 struct rpc_task *task;
10197 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10198 &task_setup.rpc_client, &msg);
10200 dprintk("NFS call free_stateid %p\n", stateid);
10201 data = kmalloc(sizeof(*data), GFP_NOFS);
10204 data->server = server;
10205 nfs4_stateid_copy(&data->args.stateid, stateid);
10207 task_setup.callback_data = data;
10209 msg.rpc_argp = &data->args;
10210 msg.rpc_resp = &data->res;
10211 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10212 task = rpc_run_task(&task_setup);
10214 return PTR_ERR(task);
10215 rpc_put_task(task);
10220 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10222 const struct cred *cred = lsp->ls_state->owner->so_cred;
10224 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10225 nfs4_free_lock_state(server, lsp);
10228 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10229 const nfs4_stateid *s2)
10231 if (s1->type != s2->type)
10234 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10237 if (s1->seqid == s2->seqid)
10240 return s1->seqid == 0 || s2->seqid == 0;
10243 #endif /* CONFIG_NFS_V4_1 */
10245 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10246 const nfs4_stateid *s2)
10248 return nfs4_stateid_match(s1, s2);
10252 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10253 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10254 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10255 .recover_open = nfs4_open_reclaim,
10256 .recover_lock = nfs4_lock_reclaim,
10257 .establish_clid = nfs4_init_clientid,
10258 .detect_trunking = nfs40_discover_server_trunking,
10261 #if defined(CONFIG_NFS_V4_1)
10262 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10263 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10264 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10265 .recover_open = nfs4_open_reclaim,
10266 .recover_lock = nfs4_lock_reclaim,
10267 .establish_clid = nfs41_init_clientid,
10268 .reclaim_complete = nfs41_proc_reclaim_complete,
10269 .detect_trunking = nfs41_discover_server_trunking,
10271 #endif /* CONFIG_NFS_V4_1 */
10273 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10274 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10275 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10276 .recover_open = nfs40_open_expired,
10277 .recover_lock = nfs4_lock_expired,
10278 .establish_clid = nfs4_init_clientid,
10281 #if defined(CONFIG_NFS_V4_1)
10282 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10283 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10284 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10285 .recover_open = nfs41_open_expired,
10286 .recover_lock = nfs41_lock_expired,
10287 .establish_clid = nfs41_init_clientid,
10289 #endif /* CONFIG_NFS_V4_1 */
10291 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10292 .sched_state_renewal = nfs4_proc_async_renew,
10293 .get_state_renewal_cred = nfs4_get_renew_cred,
10294 .renew_lease = nfs4_proc_renew,
10297 #if defined(CONFIG_NFS_V4_1)
10298 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10299 .sched_state_renewal = nfs41_proc_async_sequence,
10300 .get_state_renewal_cred = nfs4_get_machine_cred,
10301 .renew_lease = nfs4_proc_sequence,
10305 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10306 .get_locations = _nfs40_proc_get_locations,
10307 .fsid_present = _nfs40_proc_fsid_present,
10310 #if defined(CONFIG_NFS_V4_1)
10311 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10312 .get_locations = _nfs41_proc_get_locations,
10313 .fsid_present = _nfs41_proc_fsid_present,
10315 #endif /* CONFIG_NFS_V4_1 */
10317 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10318 .minor_version = 0,
10319 .init_caps = NFS_CAP_READDIRPLUS
10320 | NFS_CAP_ATOMIC_OPEN
10321 | NFS_CAP_POSIX_LOCK,
10322 .init_client = nfs40_init_client,
10323 .shutdown_client = nfs40_shutdown_client,
10324 .match_stateid = nfs4_match_stateid,
10325 .find_root_sec = nfs4_find_root_sec,
10326 .free_lock_state = nfs4_release_lockowner,
10327 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10328 .alloc_seqid = nfs_alloc_seqid,
10329 .call_sync_ops = &nfs40_call_sync_ops,
10330 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10331 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10332 .state_renewal_ops = &nfs40_state_renewal_ops,
10333 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10336 #if defined(CONFIG_NFS_V4_1)
10337 static struct nfs_seqid *
10338 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10343 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10344 .minor_version = 1,
10345 .init_caps = NFS_CAP_READDIRPLUS
10346 | NFS_CAP_ATOMIC_OPEN
10347 | NFS_CAP_POSIX_LOCK
10348 | NFS_CAP_STATEID_NFSV41
10349 | NFS_CAP_ATOMIC_OPEN_V1
10351 .init_client = nfs41_init_client,
10352 .shutdown_client = nfs41_shutdown_client,
10353 .match_stateid = nfs41_match_stateid,
10354 .find_root_sec = nfs41_find_root_sec,
10355 .free_lock_state = nfs41_free_lock_state,
10356 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10357 .alloc_seqid = nfs_alloc_no_seqid,
10358 .session_trunk = nfs4_test_session_trunk,
10359 .call_sync_ops = &nfs41_call_sync_ops,
10360 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10361 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10362 .state_renewal_ops = &nfs41_state_renewal_ops,
10363 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10367 #if defined(CONFIG_NFS_V4_2)
10368 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10369 .minor_version = 2,
10370 .init_caps = NFS_CAP_READDIRPLUS
10371 | NFS_CAP_ATOMIC_OPEN
10372 | NFS_CAP_POSIX_LOCK
10373 | NFS_CAP_STATEID_NFSV41
10374 | NFS_CAP_ATOMIC_OPEN_V1
10378 | NFS_CAP_OFFLOAD_CANCEL
10379 | NFS_CAP_COPY_NOTIFY
10380 | NFS_CAP_DEALLOCATE
10382 | NFS_CAP_LAYOUTSTATS
10384 | NFS_CAP_LAYOUTERROR
10385 | NFS_CAP_READ_PLUS,
10386 .init_client = nfs41_init_client,
10387 .shutdown_client = nfs41_shutdown_client,
10388 .match_stateid = nfs41_match_stateid,
10389 .find_root_sec = nfs41_find_root_sec,
10390 .free_lock_state = nfs41_free_lock_state,
10391 .call_sync_ops = &nfs41_call_sync_ops,
10392 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10393 .alloc_seqid = nfs_alloc_no_seqid,
10394 .session_trunk = nfs4_test_session_trunk,
10395 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10396 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10397 .state_renewal_ops = &nfs41_state_renewal_ops,
10398 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10402 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10403 [0] = &nfs_v4_0_minor_ops,
10404 #if defined(CONFIG_NFS_V4_1)
10405 [1] = &nfs_v4_1_minor_ops,
10407 #if defined(CONFIG_NFS_V4_2)
10408 [2] = &nfs_v4_2_minor_ops,
10412 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10414 ssize_t error, error2, error3;
10416 error = generic_listxattr(dentry, list, size);
10424 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10433 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10437 return error + error2 + error3;
10440 static const struct inode_operations nfs4_dir_inode_operations = {
10441 .create = nfs_create,
10442 .lookup = nfs_lookup,
10443 .atomic_open = nfs_atomic_open,
10445 .unlink = nfs_unlink,
10446 .symlink = nfs_symlink,
10447 .mkdir = nfs_mkdir,
10448 .rmdir = nfs_rmdir,
10449 .mknod = nfs_mknod,
10450 .rename = nfs_rename,
10451 .permission = nfs_permission,
10452 .getattr = nfs_getattr,
10453 .setattr = nfs_setattr,
10454 .listxattr = nfs4_listxattr,
10457 static const struct inode_operations nfs4_file_inode_operations = {
10458 .permission = nfs_permission,
10459 .getattr = nfs_getattr,
10460 .setattr = nfs_setattr,
10461 .listxattr = nfs4_listxattr,
10464 const struct nfs_rpc_ops nfs_v4_clientops = {
10465 .version = 4, /* protocol version */
10466 .dentry_ops = &nfs4_dentry_operations,
10467 .dir_inode_ops = &nfs4_dir_inode_operations,
10468 .file_inode_ops = &nfs4_file_inode_operations,
10469 .file_ops = &nfs4_file_operations,
10470 .getroot = nfs4_proc_get_root,
10471 .submount = nfs4_submount,
10472 .try_get_tree = nfs4_try_get_tree,
10473 .getattr = nfs4_proc_getattr,
10474 .setattr = nfs4_proc_setattr,
10475 .lookup = nfs4_proc_lookup,
10476 .lookupp = nfs4_proc_lookupp,
10477 .access = nfs4_proc_access,
10478 .readlink = nfs4_proc_readlink,
10479 .create = nfs4_proc_create,
10480 .remove = nfs4_proc_remove,
10481 .unlink_setup = nfs4_proc_unlink_setup,
10482 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10483 .unlink_done = nfs4_proc_unlink_done,
10484 .rename_setup = nfs4_proc_rename_setup,
10485 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10486 .rename_done = nfs4_proc_rename_done,
10487 .link = nfs4_proc_link,
10488 .symlink = nfs4_proc_symlink,
10489 .mkdir = nfs4_proc_mkdir,
10490 .rmdir = nfs4_proc_rmdir,
10491 .readdir = nfs4_proc_readdir,
10492 .mknod = nfs4_proc_mknod,
10493 .statfs = nfs4_proc_statfs,
10494 .fsinfo = nfs4_proc_fsinfo,
10495 .pathconf = nfs4_proc_pathconf,
10496 .set_capabilities = nfs4_server_capabilities,
10497 .decode_dirent = nfs4_decode_dirent,
10498 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10499 .read_setup = nfs4_proc_read_setup,
10500 .read_done = nfs4_read_done,
10501 .write_setup = nfs4_proc_write_setup,
10502 .write_done = nfs4_write_done,
10503 .commit_setup = nfs4_proc_commit_setup,
10504 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10505 .commit_done = nfs4_commit_done,
10506 .lock = nfs4_proc_lock,
10507 .clear_acl_cache = nfs4_zap_acl_attr,
10508 .close_context = nfs4_close_context,
10509 .open_context = nfs4_atomic_open,
10510 .have_delegation = nfs4_have_delegation,
10511 .alloc_client = nfs4_alloc_client,
10512 .init_client = nfs4_init_client,
10513 .free_client = nfs4_free_client,
10514 .create_server = nfs4_create_server,
10515 .clone_server = nfs_clone_server,
10518 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10519 .name = XATTR_NAME_NFSV4_ACL,
10520 .list = nfs4_xattr_list_nfs4_acl,
10521 .get = nfs4_xattr_get_nfs4_acl,
10522 .set = nfs4_xattr_set_nfs4_acl,
10525 #ifdef CONFIG_NFS_V4_2
10526 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10527 .prefix = XATTR_USER_PREFIX,
10528 .get = nfs4_xattr_get_nfs4_user,
10529 .set = nfs4_xattr_set_nfs4_user,
10533 const struct xattr_handler *nfs4_xattr_handlers[] = {
10534 &nfs4_xattr_nfs4_acl_handler,
10535 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10536 &nfs4_xattr_nfs4_label_handler,
10538 #ifdef CONFIG_NFS_V4_2
10539 &nfs4_xattr_nfs4_user_handler,