4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
57 #include <linux/iversion.h>
60 #include "delegation.h"
66 #include "nfs4idmap.h"
67 #include "nfs4session.h"
70 #include "nfs4trace.h"
72 #define NFSDBG_FACILITY NFSDBG_PROC
74 #define NFS4_BITMASK_SZ 3
76 #define NFS4_POLL_RETRY_MIN (HZ/10)
77 #define NFS4_POLL_RETRY_MAX (15*HZ)
79 /* file attributes which can be mapped to nfs attributes */
80 #define NFS4_VALID_ATTRS (ATTR_MODE \
91 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
92 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
93 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
94 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label, struct inode *inode);
95 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
96 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
97 struct nfs_fattr *fattr, struct iattr *sattr,
98 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
99 struct nfs4_label *olabel);
100 #ifdef CONFIG_NFS_V4_1
101 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
102 const struct cred *cred,
103 struct nfs4_slot *slot,
105 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
106 const struct cred *);
107 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
108 const struct cred *, bool);
111 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
112 static inline struct nfs4_label *
113 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
114 struct iattr *sattr, struct nfs4_label *label)
121 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
124 err = security_dentry_init_security(dentry, sattr->ia_mode,
125 &dentry->d_name, (void **)&label->label, &label->len);
132 nfs4_label_release_security(struct nfs4_label *label)
135 security_release_secctx(label->label, label->len);
137 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
140 return server->attr_bitmask;
142 return server->attr_bitmask_nl;
145 static inline struct nfs4_label *
146 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
147 struct iattr *sattr, struct nfs4_label *l)
150 nfs4_label_release_security(struct nfs4_label *label)
153 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
154 { return server->attr_bitmask; }
157 /* Prevent leaks of NFSv4 errors into userland */
158 static int nfs4_map_errors(int err)
163 case -NFS4ERR_RESOURCE:
164 case -NFS4ERR_LAYOUTTRYLATER:
165 case -NFS4ERR_RECALLCONFLICT:
167 case -NFS4ERR_WRONGSEC:
168 case -NFS4ERR_WRONG_CRED:
170 case -NFS4ERR_BADOWNER:
171 case -NFS4ERR_BADNAME:
173 case -NFS4ERR_SHARE_DENIED:
175 case -NFS4ERR_MINOR_VERS_MISMATCH:
176 return -EPROTONOSUPPORT;
177 case -NFS4ERR_FILE_OPEN:
180 dprintk("%s could not handle NFSv4 error %d\n",
188 * This is our standard bitmap for GETATTR requests.
190 const u32 nfs4_fattr_bitmap[3] = {
192 | FATTR4_WORD0_CHANGE
195 | FATTR4_WORD0_FILEID,
197 | FATTR4_WORD1_NUMLINKS
199 | FATTR4_WORD1_OWNER_GROUP
200 | FATTR4_WORD1_RAWDEV
201 | FATTR4_WORD1_SPACE_USED
202 | FATTR4_WORD1_TIME_ACCESS
203 | FATTR4_WORD1_TIME_METADATA
204 | FATTR4_WORD1_TIME_MODIFY
205 | FATTR4_WORD1_MOUNTED_ON_FILEID,
206 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
207 FATTR4_WORD2_SECURITY_LABEL
211 static const u32 nfs4_pnfs_open_bitmap[3] = {
213 | FATTR4_WORD0_CHANGE
216 | FATTR4_WORD0_FILEID,
218 | FATTR4_WORD1_NUMLINKS
220 | FATTR4_WORD1_OWNER_GROUP
221 | FATTR4_WORD1_RAWDEV
222 | FATTR4_WORD1_SPACE_USED
223 | FATTR4_WORD1_TIME_ACCESS
224 | FATTR4_WORD1_TIME_METADATA
225 | FATTR4_WORD1_TIME_MODIFY,
226 FATTR4_WORD2_MDSTHRESHOLD
227 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
228 | FATTR4_WORD2_SECURITY_LABEL
232 static const u32 nfs4_open_noattr_bitmap[3] = {
234 | FATTR4_WORD0_FILEID,
237 const u32 nfs4_statfs_bitmap[3] = {
238 FATTR4_WORD0_FILES_AVAIL
239 | FATTR4_WORD0_FILES_FREE
240 | FATTR4_WORD0_FILES_TOTAL,
241 FATTR4_WORD1_SPACE_AVAIL
242 | FATTR4_WORD1_SPACE_FREE
243 | FATTR4_WORD1_SPACE_TOTAL
246 const u32 nfs4_pathconf_bitmap[3] = {
248 | FATTR4_WORD0_MAXNAME,
252 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
253 | FATTR4_WORD0_MAXREAD
254 | FATTR4_WORD0_MAXWRITE
255 | FATTR4_WORD0_LEASE_TIME,
256 FATTR4_WORD1_TIME_DELTA
257 | FATTR4_WORD1_FS_LAYOUT_TYPES,
258 FATTR4_WORD2_LAYOUT_BLKSIZE
259 | FATTR4_WORD2_CLONE_BLKSIZE
262 const u32 nfs4_fs_locations_bitmap[3] = {
266 | FATTR4_WORD0_FILEID
267 | FATTR4_WORD0_FS_LOCATIONS,
269 | FATTR4_WORD1_OWNER_GROUP
270 | FATTR4_WORD1_RAWDEV
271 | FATTR4_WORD1_SPACE_USED
272 | FATTR4_WORD1_TIME_ACCESS
273 | FATTR4_WORD1_TIME_METADATA
274 | FATTR4_WORD1_TIME_MODIFY
275 | FATTR4_WORD1_MOUNTED_ON_FILEID,
278 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
281 unsigned long cache_validity;
283 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
284 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
287 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
288 if (!(cache_validity & NFS_INO_REVAL_FORCED))
289 cache_validity &= ~(NFS_INO_INVALID_CHANGE
290 | NFS_INO_INVALID_SIZE);
292 if (!(cache_validity & NFS_INO_INVALID_SIZE))
293 dst[0] &= ~FATTR4_WORD0_SIZE;
295 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
296 dst[0] &= ~FATTR4_WORD0_CHANGE;
299 static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
300 const __u32 *src, struct inode *inode)
302 nfs4_bitmap_copy_adjust(dst, src, inode);
305 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
306 struct nfs4_readdir_arg *readdir)
308 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
312 readdir->cookie = cookie;
313 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
318 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
323 * NFSv4 servers do not return entries for '.' and '..'
324 * Therefore, we fake these entries here. We let '.'
325 * have cookie 0 and '..' have cookie 1. Note that
326 * when talking to the server, we always send cookie 0
329 start = p = kmap_atomic(*readdir->pages);
332 *p++ = xdr_one; /* next */
333 *p++ = xdr_zero; /* cookie, first word */
334 *p++ = xdr_one; /* cookie, second word */
335 *p++ = xdr_one; /* entry len */
336 memcpy(p, ".\0\0\0", 4); /* entry */
338 *p++ = xdr_one; /* bitmap length */
339 *p++ = htonl(attrs); /* bitmap */
340 *p++ = htonl(12); /* attribute buffer length */
341 *p++ = htonl(NF4DIR);
342 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
345 *p++ = xdr_one; /* next */
346 *p++ = xdr_zero; /* cookie, first word */
347 *p++ = xdr_two; /* cookie, second word */
348 *p++ = xdr_two; /* entry len */
349 memcpy(p, "..\0\0", 4); /* entry */
351 *p++ = xdr_one; /* bitmap length */
352 *p++ = htonl(attrs); /* bitmap */
353 *p++ = htonl(12); /* attribute buffer length */
354 *p++ = htonl(NF4DIR);
355 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
357 readdir->pgbase = (char *)p - (char *)start;
358 readdir->count -= readdir->pgbase;
359 kunmap_atomic(start);
362 static void nfs4_test_and_free_stateid(struct nfs_server *server,
363 nfs4_stateid *stateid,
364 const struct cred *cred)
366 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
368 ops->test_and_free_expired(server, stateid, cred);
371 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
372 nfs4_stateid *stateid,
373 const struct cred *cred)
375 stateid->type = NFS4_REVOKED_STATEID_TYPE;
376 nfs4_test_and_free_stateid(server, stateid, cred);
379 static void nfs4_free_revoked_stateid(struct nfs_server *server,
380 const nfs4_stateid *stateid,
381 const struct cred *cred)
385 nfs4_stateid_copy(&tmp, stateid);
386 __nfs4_free_revoked_stateid(server, &tmp, cred);
389 static long nfs4_update_delay(long *timeout)
393 return NFS4_POLL_RETRY_MAX;
395 *timeout = NFS4_POLL_RETRY_MIN;
396 if (*timeout > NFS4_POLL_RETRY_MAX)
397 *timeout = NFS4_POLL_RETRY_MAX;
403 static int nfs4_delay_killable(long *timeout)
407 freezable_schedule_timeout_killable_unsafe(
408 nfs4_update_delay(timeout));
409 if (!__fatal_signal_pending(current))
414 static int nfs4_delay_interruptible(long *timeout)
418 freezable_schedule_timeout_interruptible(nfs4_update_delay(timeout));
419 if (!signal_pending(current))
421 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
424 static int nfs4_delay(long *timeout, bool interruptible)
427 return nfs4_delay_interruptible(timeout);
428 return nfs4_delay_killable(timeout);
431 /* This is the error handling routine for processes that are allowed
434 static int nfs4_do_handle_exception(struct nfs_server *server,
435 int errorcode, struct nfs4_exception *exception)
437 struct nfs_client *clp = server->nfs_client;
438 struct nfs4_state *state = exception->state;
439 const nfs4_stateid *stateid = exception->stateid;
440 struct inode *inode = exception->inode;
443 exception->delay = 0;
444 exception->recovering = 0;
445 exception->retry = 0;
447 if (stateid == NULL && state != NULL)
448 stateid = &state->stateid;
453 case -NFS4ERR_BADHANDLE:
455 if (inode != NULL && S_ISREG(inode->i_mode))
456 pnfs_destroy_layout(NFS_I(inode));
458 case -NFS4ERR_DELEG_REVOKED:
459 case -NFS4ERR_ADMIN_REVOKED:
460 case -NFS4ERR_EXPIRED:
461 case -NFS4ERR_BAD_STATEID:
462 if (inode != NULL && stateid != NULL) {
463 nfs_inode_find_state_and_recover(inode,
465 goto wait_on_recovery;
468 case -NFS4ERR_OPENMODE:
472 err = nfs_async_inode_return_delegation(inode,
475 goto wait_on_recovery;
476 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
477 exception->retry = 1;
483 ret = nfs4_schedule_stateid_recovery(server, state);
486 goto wait_on_recovery;
487 case -NFS4ERR_STALE_STATEID:
488 case -NFS4ERR_STALE_CLIENTID:
489 nfs4_schedule_lease_recovery(clp);
490 goto wait_on_recovery;
492 ret = nfs4_schedule_migration_recovery(server);
495 goto wait_on_recovery;
496 case -NFS4ERR_LEASE_MOVED:
497 nfs4_schedule_lease_moved_recovery(clp);
498 goto wait_on_recovery;
499 #if defined(CONFIG_NFS_V4_1)
500 case -NFS4ERR_BADSESSION:
501 case -NFS4ERR_BADSLOT:
502 case -NFS4ERR_BAD_HIGH_SLOT:
503 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
504 case -NFS4ERR_DEADSESSION:
505 case -NFS4ERR_SEQ_FALSE_RETRY:
506 case -NFS4ERR_SEQ_MISORDERED:
507 dprintk("%s ERROR: %d Reset session\n", __func__,
509 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
510 goto wait_on_recovery;
511 #endif /* defined(CONFIG_NFS_V4_1) */
512 case -NFS4ERR_FILE_OPEN:
513 if (exception->timeout > HZ) {
514 /* We have retried a decent amount, time to
522 nfs_inc_server_stats(server, NFSIOS_DELAY);
525 case -NFS4ERR_LAYOUTTRYLATER:
526 case -NFS4ERR_RECALLCONFLICT:
527 exception->delay = 1;
530 case -NFS4ERR_RETRY_UNCACHED_REP:
531 case -NFS4ERR_OLD_STATEID:
532 exception->retry = 1;
534 case -NFS4ERR_BADOWNER:
535 /* The following works around a Linux server bug! */
536 case -NFS4ERR_BADNAME:
537 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
538 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
539 exception->retry = 1;
540 printk(KERN_WARNING "NFS: v4 server %s "
541 "does not accept raw "
543 "Reenabling the idmapper.\n",
544 server->nfs_client->cl_hostname);
547 /* We failed to handle the error */
548 return nfs4_map_errors(ret);
550 exception->recovering = 1;
554 /* This is the error handling routine for processes that are allowed
557 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
559 struct nfs_client *clp = server->nfs_client;
562 ret = nfs4_do_handle_exception(server, errorcode, exception);
563 if (exception->delay) {
564 ret = nfs4_delay(&exception->timeout,
565 exception->interruptible);
568 if (exception->recovering) {
569 ret = nfs4_wait_clnt_recover(clp);
570 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
577 exception->retry = 1;
582 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
583 int errorcode, struct nfs4_exception *exception)
585 struct nfs_client *clp = server->nfs_client;
588 ret = nfs4_do_handle_exception(server, errorcode, exception);
589 if (exception->delay) {
590 rpc_delay(task, nfs4_update_delay(&exception->timeout));
593 if (exception->recovering) {
594 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
595 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
596 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
599 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
604 exception->retry = 1;
606 * For NFS4ERR_MOVED, the client transport will need to
607 * be recomputed after migration recovery has completed.
609 if (errorcode == -NFS4ERR_MOVED)
610 rpc_task_release_transport(task);
616 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
617 struct nfs4_state *state, long *timeout)
619 struct nfs4_exception exception = {
623 if (task->tk_status >= 0)
626 exception.timeout = *timeout;
627 task->tk_status = nfs4_async_handle_exception(task, server,
630 if (exception.delay && timeout)
631 *timeout = exception.timeout;
638 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
639 * or 'false' otherwise.
641 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
643 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
644 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
647 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
649 spin_lock(&clp->cl_lock);
650 if (time_before(clp->cl_last_renewal,timestamp))
651 clp->cl_last_renewal = timestamp;
652 spin_unlock(&clp->cl_lock);
655 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
657 struct nfs_client *clp = server->nfs_client;
659 if (!nfs4_has_session(clp))
660 do_renew_lease(clp, timestamp);
663 struct nfs4_call_sync_data {
664 const struct nfs_server *seq_server;
665 struct nfs4_sequence_args *seq_args;
666 struct nfs4_sequence_res *seq_res;
669 void nfs4_init_sequence(struct nfs4_sequence_args *args,
670 struct nfs4_sequence_res *res, int cache_reply,
673 args->sa_slot = NULL;
674 args->sa_cache_this = cache_reply;
675 args->sa_privileged = privileged;
680 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
682 struct nfs4_slot *slot = res->sr_slot;
683 struct nfs4_slot_table *tbl;
686 spin_lock(&tbl->slot_tbl_lock);
687 if (!nfs41_wake_and_assign_slot(tbl, slot))
688 nfs4_free_slot(tbl, slot);
689 spin_unlock(&tbl->slot_tbl_lock);
694 static int nfs40_sequence_done(struct rpc_task *task,
695 struct nfs4_sequence_res *res)
697 if (res->sr_slot != NULL)
698 nfs40_sequence_free_slot(res);
702 #if defined(CONFIG_NFS_V4_1)
704 static void nfs41_release_slot(struct nfs4_slot *slot)
706 struct nfs4_session *session;
707 struct nfs4_slot_table *tbl;
708 bool send_new_highest_used_slotid = false;
713 session = tbl->session;
715 /* Bump the slot sequence number */
720 spin_lock(&tbl->slot_tbl_lock);
721 /* Be nice to the server: try to ensure that the last transmitted
722 * value for highest_user_slotid <= target_highest_slotid
724 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
725 send_new_highest_used_slotid = true;
727 if (nfs41_wake_and_assign_slot(tbl, slot)) {
728 send_new_highest_used_slotid = false;
731 nfs4_free_slot(tbl, slot);
733 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
734 send_new_highest_used_slotid = false;
736 spin_unlock(&tbl->slot_tbl_lock);
737 if (send_new_highest_used_slotid)
738 nfs41_notify_server(session->clp);
739 if (waitqueue_active(&tbl->slot_waitq))
740 wake_up_all(&tbl->slot_waitq);
743 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
745 nfs41_release_slot(res->sr_slot);
749 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
752 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
753 slot->seq_nr_highest_sent = seqnr;
755 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
758 slot->seq_nr_highest_sent = seqnr;
759 slot->seq_nr_last_acked = seqnr;
762 static int nfs41_sequence_process(struct rpc_task *task,
763 struct nfs4_sequence_res *res)
765 struct nfs4_session *session;
766 struct nfs4_slot *slot = res->sr_slot;
767 struct nfs_client *clp;
772 /* don't increment the sequence number if the task wasn't sent */
773 if (!RPC_WAS_SENT(task) || slot->seq_done)
776 session = slot->table->session;
778 trace_nfs4_sequence_done(session, res);
779 /* Check the SEQUENCE operation status */
780 switch (res->sr_status) {
782 /* Mark this sequence number as having been acked */
783 nfs4_slot_sequence_acked(slot, slot->seq_nr);
784 /* Update the slot's sequence and clientid lease timer */
787 do_renew_lease(clp, res->sr_timestamp);
788 /* Check sequence flags */
789 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
791 nfs41_update_target_slotid(slot->table, slot, res);
795 * sr_status remains 1 if an RPC level error occurred.
796 * The server may or may not have processed the sequence
799 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
803 /* The server detected a resend of the RPC call and
804 * returned NFS4ERR_DELAY as per Section 2.10.6.2
807 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
811 nfs4_slot_sequence_acked(slot, slot->seq_nr);
813 case -NFS4ERR_RETRY_UNCACHED_REP:
814 case -NFS4ERR_SEQ_FALSE_RETRY:
816 * The server thinks we tried to replay a request.
817 * Retry the call after bumping the sequence ID.
819 nfs4_slot_sequence_acked(slot, slot->seq_nr);
821 case -NFS4ERR_BADSLOT:
823 * The slot id we used was probably retired. Try again
824 * using a different slot id.
826 if (slot->slot_nr < slot->table->target_highest_slotid)
827 goto session_recover;
829 case -NFS4ERR_SEQ_MISORDERED:
830 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
832 * Were one or more calls using this slot interrupted?
833 * If the server never received the request, then our
834 * transmitted slot sequence number may be too high.
836 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
842 * A retry might be sent while the original request is
843 * still in progress on the replier. The replier SHOULD
844 * deal with the issue by returning NFS4ERR_DELAY as the
845 * reply to SEQUENCE or CB_SEQUENCE operation, but
846 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
848 * Restart the search after a delay.
850 slot->seq_nr = slot->seq_nr_highest_sent;
853 /* Just update the slot sequence no. */
857 /* The session may be reset by one of the error handlers. */
858 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
862 nfs4_schedule_session_recovery(session, res->sr_status);
867 if (rpc_restart_call_prepare(task)) {
868 nfs41_sequence_free_slot(res);
874 if (!rpc_restart_call(task))
876 rpc_delay(task, NFS4_POLL_RETRY_MAX);
880 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
882 if (!nfs41_sequence_process(task, res))
884 if (res->sr_slot != NULL)
885 nfs41_sequence_free_slot(res);
889 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
891 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
893 if (res->sr_slot == NULL)
895 if (res->sr_slot->table->session != NULL)
896 return nfs41_sequence_process(task, res);
897 return nfs40_sequence_done(task, res);
900 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
902 if (res->sr_slot != NULL) {
903 if (res->sr_slot->table->session != NULL)
904 nfs41_sequence_free_slot(res);
906 nfs40_sequence_free_slot(res);
910 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
912 if (res->sr_slot == NULL)
914 if (!res->sr_slot->table->session)
915 return nfs40_sequence_done(task, res);
916 return nfs41_sequence_done(task, res);
918 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
920 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
922 struct nfs4_call_sync_data *data = calldata;
924 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
926 nfs4_setup_sequence(data->seq_server->nfs_client,
927 data->seq_args, data->seq_res, task);
930 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
932 struct nfs4_call_sync_data *data = calldata;
934 nfs41_sequence_done(task, data->seq_res);
937 static const struct rpc_call_ops nfs41_call_sync_ops = {
938 .rpc_call_prepare = nfs41_call_sync_prepare,
939 .rpc_call_done = nfs41_call_sync_done,
942 #else /* !CONFIG_NFS_V4_1 */
944 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
946 return nfs40_sequence_done(task, res);
949 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
951 if (res->sr_slot != NULL)
952 nfs40_sequence_free_slot(res);
955 int nfs4_sequence_done(struct rpc_task *task,
956 struct nfs4_sequence_res *res)
958 return nfs40_sequence_done(task, res);
960 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
962 #endif /* !CONFIG_NFS_V4_1 */
964 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
966 res->sr_timestamp = jiffies;
967 res->sr_status_flags = 0;
972 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
973 struct nfs4_sequence_res *res,
974 struct nfs4_slot *slot)
978 slot->privileged = args->sa_privileged ? 1 : 0;
979 args->sa_slot = slot;
984 int nfs4_setup_sequence(struct nfs_client *client,
985 struct nfs4_sequence_args *args,
986 struct nfs4_sequence_res *res,
987 struct rpc_task *task)
989 struct nfs4_session *session = nfs4_get_session(client);
990 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
991 struct nfs4_slot *slot;
993 /* slot already allocated? */
994 if (res->sr_slot != NULL)
998 tbl = &session->fc_slot_table;
1000 spin_lock(&tbl->slot_tbl_lock);
1001 /* The state manager will wait until the slot table is empty */
1002 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1005 slot = nfs4_alloc_slot(tbl);
1007 if (slot == ERR_PTR(-ENOMEM))
1008 goto out_sleep_timeout;
1011 spin_unlock(&tbl->slot_tbl_lock);
1013 nfs4_sequence_attach_slot(args, res, slot);
1015 trace_nfs4_setup_sequence(session, args);
1017 nfs41_sequence_res_init(res);
1018 rpc_call_start(task);
1021 /* Try again in 1/4 second */
1022 if (args->sa_privileged)
1023 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1024 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1026 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1027 NULL, jiffies + (HZ >> 2));
1028 spin_unlock(&tbl->slot_tbl_lock);
1031 if (args->sa_privileged)
1032 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1033 RPC_PRIORITY_PRIVILEGED);
1035 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1036 spin_unlock(&tbl->slot_tbl_lock);
1039 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1041 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1043 struct nfs4_call_sync_data *data = calldata;
1044 nfs4_setup_sequence(data->seq_server->nfs_client,
1045 data->seq_args, data->seq_res, task);
1048 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1050 struct nfs4_call_sync_data *data = calldata;
1051 nfs4_sequence_done(task, data->seq_res);
1054 static const struct rpc_call_ops nfs40_call_sync_ops = {
1055 .rpc_call_prepare = nfs40_call_sync_prepare,
1056 .rpc_call_done = nfs40_call_sync_done,
1059 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1060 struct nfs_server *server,
1061 struct rpc_message *msg,
1062 struct nfs4_sequence_args *args,
1063 struct nfs4_sequence_res *res)
1066 struct rpc_task *task;
1067 struct nfs_client *clp = server->nfs_client;
1068 struct nfs4_call_sync_data data = {
1069 .seq_server = server,
1073 struct rpc_task_setup task_setup = {
1076 .callback_ops = clp->cl_mvops->call_sync_ops,
1077 .callback_data = &data
1080 task = rpc_run_task(&task_setup);
1082 ret = PTR_ERR(task);
1084 ret = task->tk_status;
1090 int nfs4_call_sync(struct rpc_clnt *clnt,
1091 struct nfs_server *server,
1092 struct rpc_message *msg,
1093 struct nfs4_sequence_args *args,
1094 struct nfs4_sequence_res *res,
1097 nfs4_init_sequence(args, res, cache_reply, 0);
1098 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1102 nfs4_inc_nlink_locked(struct inode *inode)
1104 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1109 nfs4_dec_nlink_locked(struct inode *inode)
1111 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1116 update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
1117 unsigned long timestamp, unsigned long cache_validity)
1119 struct nfs_inode *nfsi = NFS_I(dir);
1121 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1122 | NFS_INO_INVALID_MTIME
1123 | NFS_INO_INVALID_DATA
1125 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
1126 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1127 nfsi->attrtimeo_timestamp = jiffies;
1129 nfs_force_lookup_revalidate(dir);
1130 if (cinfo->before != inode_peek_iversion_raw(dir))
1131 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1132 NFS_INO_INVALID_ACL;
1134 inode_set_iversion_raw(dir, cinfo->after);
1135 nfsi->read_cache_jiffies = timestamp;
1136 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1137 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1138 nfs_fscache_invalidate(dir);
1142 update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1143 unsigned long timestamp, unsigned long cache_validity)
1145 spin_lock(&dir->i_lock);
1146 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1147 spin_unlock(&dir->i_lock);
1150 struct nfs4_open_createattrs {
1151 struct nfs4_label *label;
1152 struct iattr *sattr;
1153 const __u32 verf[2];
1156 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1157 int err, struct nfs4_exception *exception)
1161 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1163 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1164 exception->retry = 1;
1169 nfs4_map_atomic_open_share(struct nfs_server *server,
1170 fmode_t fmode, int openflags)
1174 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1176 res = NFS4_SHARE_ACCESS_READ;
1179 res = NFS4_SHARE_ACCESS_WRITE;
1181 case FMODE_READ|FMODE_WRITE:
1182 res = NFS4_SHARE_ACCESS_BOTH;
1184 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1186 /* Want no delegation if we're using O_DIRECT */
1187 if (openflags & O_DIRECT)
1188 res |= NFS4_SHARE_WANT_NO_DELEG;
1193 static enum open_claim_type4
1194 nfs4_map_atomic_open_claim(struct nfs_server *server,
1195 enum open_claim_type4 claim)
1197 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1202 case NFS4_OPEN_CLAIM_FH:
1203 return NFS4_OPEN_CLAIM_NULL;
1204 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1205 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1206 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1207 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1211 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1213 p->o_res.f_attr = &p->f_attr;
1214 p->o_res.f_label = p->f_label;
1215 p->o_res.seqid = p->o_arg.seqid;
1216 p->c_res.seqid = p->c_arg.seqid;
1217 p->o_res.server = p->o_arg.server;
1218 p->o_res.access_request = p->o_arg.access;
1219 nfs_fattr_init(&p->f_attr);
1220 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1223 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1224 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1225 const struct nfs4_open_createattrs *c,
1226 enum open_claim_type4 claim,
1229 struct dentry *parent = dget_parent(dentry);
1230 struct inode *dir = d_inode(parent);
1231 struct nfs_server *server = NFS_SERVER(dir);
1232 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1233 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1234 struct nfs4_opendata *p;
1236 p = kzalloc(sizeof(*p), gfp_mask);
1240 p->f_label = nfs4_label_alloc(server, gfp_mask);
1241 if (IS_ERR(p->f_label))
1244 p->a_label = nfs4_label_alloc(server, gfp_mask);
1245 if (IS_ERR(p->a_label))
1248 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1249 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1250 if (IS_ERR(p->o_arg.seqid))
1251 goto err_free_label;
1252 nfs_sb_active(dentry->d_sb);
1253 p->dentry = dget(dentry);
1256 atomic_inc(&sp->so_count);
1257 p->o_arg.open_flags = flags;
1258 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1259 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1260 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1262 if (flags & O_CREAT) {
1263 p->o_arg.umask = current_umask();
1264 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1265 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1266 p->o_arg.u.attrs = &p->attrs;
1267 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1269 memcpy(p->o_arg.u.verifier.data, c->verf,
1270 sizeof(p->o_arg.u.verifier.data));
1273 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1274 * will return permission denied for all bits until close */
1275 if (!(flags & O_EXCL)) {
1276 /* ask server to check for all possible rights as results
1278 switch (p->o_arg.claim) {
1281 case NFS4_OPEN_CLAIM_NULL:
1282 case NFS4_OPEN_CLAIM_FH:
1283 p->o_arg.access = NFS4_ACCESS_READ |
1284 NFS4_ACCESS_MODIFY |
1285 NFS4_ACCESS_EXTEND |
1286 NFS4_ACCESS_EXECUTE;
1289 p->o_arg.clientid = server->nfs_client->cl_clientid;
1290 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1291 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1292 p->o_arg.name = &dentry->d_name;
1293 p->o_arg.server = server;
1294 p->o_arg.bitmask = nfs4_bitmask(server, label);
1295 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1296 switch (p->o_arg.claim) {
1297 case NFS4_OPEN_CLAIM_NULL:
1298 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1299 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1300 p->o_arg.fh = NFS_FH(dir);
1302 case NFS4_OPEN_CLAIM_PREVIOUS:
1303 case NFS4_OPEN_CLAIM_FH:
1304 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1305 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1306 p->o_arg.fh = NFS_FH(d_inode(dentry));
1308 p->c_arg.fh = &p->o_res.fh;
1309 p->c_arg.stateid = &p->o_res.stateid;
1310 p->c_arg.seqid = p->o_arg.seqid;
1311 nfs4_init_opendata_res(p);
1312 kref_init(&p->kref);
1316 nfs4_label_free(p->a_label);
1318 nfs4_label_free(p->f_label);
1326 static void nfs4_opendata_free(struct kref *kref)
1328 struct nfs4_opendata *p = container_of(kref,
1329 struct nfs4_opendata, kref);
1330 struct super_block *sb = p->dentry->d_sb;
1332 nfs4_lgopen_release(p->lgp);
1333 nfs_free_seqid(p->o_arg.seqid);
1334 nfs4_sequence_free_slot(&p->o_res.seq_res);
1335 if (p->state != NULL)
1336 nfs4_put_open_state(p->state);
1337 nfs4_put_state_owner(p->owner);
1339 nfs4_label_free(p->a_label);
1340 nfs4_label_free(p->f_label);
1344 nfs_sb_deactive(sb);
1345 nfs_fattr_free_names(&p->f_attr);
1346 kfree(p->f_attr.mdsthreshold);
1350 static void nfs4_opendata_put(struct nfs4_opendata *p)
1353 kref_put(&p->kref, nfs4_opendata_free);
1356 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1359 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1360 case FMODE_READ|FMODE_WRITE:
1361 return state->n_rdwr != 0;
1363 return state->n_wronly != 0;
1365 return state->n_rdonly != 0;
1371 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1372 int open_mode, enum open_claim_type4 claim)
1376 if (open_mode & (O_EXCL|O_TRUNC))
1379 case NFS4_OPEN_CLAIM_NULL:
1380 case NFS4_OPEN_CLAIM_FH:
1385 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1387 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1388 && state->n_rdonly != 0;
1391 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1392 && state->n_wronly != 0;
1394 case FMODE_READ|FMODE_WRITE:
1395 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1396 && state->n_rdwr != 0;
1402 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1403 enum open_claim_type4 claim)
1405 if (delegation == NULL)
1407 if ((delegation->type & fmode) != fmode)
1409 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1412 case NFS4_OPEN_CLAIM_NULL:
1413 case NFS4_OPEN_CLAIM_FH:
1415 case NFS4_OPEN_CLAIM_PREVIOUS:
1416 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1422 nfs_mark_delegation_referenced(delegation);
1426 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1435 case FMODE_READ|FMODE_WRITE:
1438 nfs4_state_set_mode_locked(state, state->state | fmode);
1441 #ifdef CONFIG_NFS_V4_1
1442 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1444 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1446 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1448 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1452 #endif /* CONFIG_NFS_V4_1 */
1454 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1456 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1457 wake_up_all(&state->waitq);
1460 static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1461 const nfs4_stateid *stateid)
1463 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1464 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1466 if (stateid_seqid == state_seqid + 1U ||
1467 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1468 nfs_state_log_update_open_stateid(state);
1470 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1473 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1475 struct nfs_client *clp = state->owner->so_server->nfs_client;
1476 bool need_recover = false;
1478 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1479 need_recover = true;
1480 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1481 need_recover = true;
1482 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1483 need_recover = true;
1485 nfs4_state_mark_reclaim_nograce(clp, state);
1489 * Check for whether or not the caller may update the open stateid
1490 * to the value passed in by stateid.
1492 * Note: This function relies heavily on the server implementing
1493 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1495 * i.e. The stateid seqids have to be initialised to 1, and
1496 * are then incremented on every state transition.
1498 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1499 const nfs4_stateid *stateid)
1501 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1502 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1503 if (stateid->seqid == cpu_to_be32(1))
1504 nfs_state_log_update_open_stateid(state);
1506 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1510 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1511 nfs_state_log_out_of_order_open_stateid(state, stateid);
1517 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1519 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1521 if (state->n_wronly)
1522 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1523 if (state->n_rdonly)
1524 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1526 set_bit(NFS_O_RDWR_STATE, &state->flags);
1527 set_bit(NFS_OPEN_STATE, &state->flags);
1530 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1531 nfs4_stateid *stateid, fmode_t fmode)
1533 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1534 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1536 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1539 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1542 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1543 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1544 clear_bit(NFS_OPEN_STATE, &state->flags);
1546 if (stateid == NULL)
1548 /* Handle OPEN+OPEN_DOWNGRADE races */
1549 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1550 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1551 nfs_resync_open_stateid_locked(state);
1554 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1555 nfs4_stateid_copy(&state->stateid, stateid);
1556 nfs4_stateid_copy(&state->open_stateid, stateid);
1557 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1559 nfs_state_log_update_open_stateid(state);
1562 static void nfs_clear_open_stateid(struct nfs4_state *state,
1563 nfs4_stateid *arg_stateid,
1564 nfs4_stateid *stateid, fmode_t fmode)
1566 write_seqlock(&state->seqlock);
1567 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1568 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1569 nfs_clear_open_stateid_locked(state, stateid, fmode);
1570 write_sequnlock(&state->seqlock);
1571 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1572 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1575 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1576 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1577 __must_hold(&state->owner->so_lock)
1578 __must_hold(&state->seqlock)
1586 if (!nfs_need_update_open_stateid(state, stateid))
1588 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1592 /* Rely on seqids for serialisation with NFSv4.0 */
1593 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1596 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1598 * Ensure we process the state changes in the same order
1599 * in which the server processed them by delaying the
1600 * update of the stateid until we are in sequence.
1602 write_sequnlock(&state->seqlock);
1603 spin_unlock(&state->owner->so_lock);
1605 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1606 if (!signal_pending(current)) {
1607 if (schedule_timeout(5*HZ) == 0)
1613 finish_wait(&state->waitq, &wait);
1615 spin_lock(&state->owner->so_lock);
1616 write_seqlock(&state->seqlock);
1619 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1620 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1621 nfs4_stateid_copy(freeme, &state->open_stateid);
1622 nfs_test_and_clear_all_open_stateid(state);
1625 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1626 nfs4_stateid_copy(&state->stateid, stateid);
1627 nfs4_stateid_copy(&state->open_stateid, stateid);
1628 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1629 nfs_state_log_update_open_stateid(state);
1632 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1633 const nfs4_stateid *open_stateid,
1635 nfs4_stateid *freeme)
1638 * Protect the call to nfs4_state_set_mode_locked and
1639 * serialise the stateid update
1641 write_seqlock(&state->seqlock);
1642 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1645 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1648 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1650 case FMODE_READ|FMODE_WRITE:
1651 set_bit(NFS_O_RDWR_STATE, &state->flags);
1653 set_bit(NFS_OPEN_STATE, &state->flags);
1654 write_sequnlock(&state->seqlock);
1657 static void nfs_state_set_delegation(struct nfs4_state *state,
1658 const nfs4_stateid *deleg_stateid,
1662 * Protect the call to nfs4_state_set_mode_locked and
1663 * serialise the stateid update
1665 write_seqlock(&state->seqlock);
1666 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1667 set_bit(NFS_DELEGATED_STATE, &state->flags);
1668 write_sequnlock(&state->seqlock);
1671 static void nfs_state_clear_delegation(struct nfs4_state *state)
1673 write_seqlock(&state->seqlock);
1674 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1675 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1676 write_sequnlock(&state->seqlock);
1679 static int update_open_stateid(struct nfs4_state *state,
1680 const nfs4_stateid *open_stateid,
1681 const nfs4_stateid *delegation,
1684 struct nfs_server *server = NFS_SERVER(state->inode);
1685 struct nfs_client *clp = server->nfs_client;
1686 struct nfs_inode *nfsi = NFS_I(state->inode);
1687 struct nfs_delegation *deleg_cur;
1688 nfs4_stateid freeme = { };
1691 fmode &= (FMODE_READ|FMODE_WRITE);
1694 spin_lock(&state->owner->so_lock);
1695 if (open_stateid != NULL) {
1696 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1700 deleg_cur = rcu_dereference(nfsi->delegation);
1701 if (deleg_cur == NULL)
1704 spin_lock(&deleg_cur->lock);
1705 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1706 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1707 (deleg_cur->type & fmode) != fmode)
1708 goto no_delegation_unlock;
1710 if (delegation == NULL)
1711 delegation = &deleg_cur->stateid;
1712 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1713 goto no_delegation_unlock;
1715 nfs_mark_delegation_referenced(deleg_cur);
1716 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1718 no_delegation_unlock:
1719 spin_unlock(&deleg_cur->lock);
1722 update_open_stateflags(state, fmode);
1723 spin_unlock(&state->owner->so_lock);
1726 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1727 nfs4_schedule_state_manager(clp);
1728 if (freeme.type != 0)
1729 nfs4_test_and_free_stateid(server, &freeme,
1730 state->owner->so_cred);
1735 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1736 const nfs4_stateid *stateid)
1738 struct nfs4_state *state = lsp->ls_state;
1741 spin_lock(&state->state_lock);
1742 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1744 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1746 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1749 spin_unlock(&state->state_lock);
1753 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1755 struct nfs_delegation *delegation;
1757 fmode &= FMODE_READ|FMODE_WRITE;
1759 delegation = rcu_dereference(NFS_I(inode)->delegation);
1760 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1765 nfs4_inode_return_delegation(inode);
1768 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1770 struct nfs4_state *state = opendata->state;
1771 struct nfs_inode *nfsi = NFS_I(state->inode);
1772 struct nfs_delegation *delegation;
1773 int open_mode = opendata->o_arg.open_flags;
1774 fmode_t fmode = opendata->o_arg.fmode;
1775 enum open_claim_type4 claim = opendata->o_arg.claim;
1776 nfs4_stateid stateid;
1780 spin_lock(&state->owner->so_lock);
1781 if (can_open_cached(state, fmode, open_mode, claim)) {
1782 update_open_stateflags(state, fmode);
1783 spin_unlock(&state->owner->so_lock);
1784 goto out_return_state;
1786 spin_unlock(&state->owner->so_lock);
1788 delegation = rcu_dereference(nfsi->delegation);
1789 if (!can_open_delegated(delegation, fmode, claim)) {
1793 /* Save the delegation */
1794 nfs4_stateid_copy(&stateid, &delegation->stateid);
1796 nfs_release_seqid(opendata->o_arg.seqid);
1797 if (!opendata->is_recover) {
1798 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1804 /* Try to update the stateid using the delegation */
1805 if (update_open_stateid(state, NULL, &stateid, fmode))
1806 goto out_return_state;
1809 return ERR_PTR(ret);
1811 refcount_inc(&state->count);
1816 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1818 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1819 struct nfs_delegation *delegation;
1820 int delegation_flags = 0;
1823 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1825 delegation_flags = delegation->flags;
1827 switch (data->o_arg.claim) {
1830 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1831 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1832 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1833 "returning a delegation for "
1834 "OPEN(CLAIM_DELEGATE_CUR)\n",
1838 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1839 nfs_inode_set_delegation(state->inode,
1840 data->owner->so_cred,
1841 data->o_res.delegation_type,
1842 &data->o_res.delegation,
1843 data->o_res.pagemod_limit);
1845 nfs_inode_reclaim_delegation(state->inode,
1846 data->owner->so_cred,
1847 data->o_res.delegation_type,
1848 &data->o_res.delegation,
1849 data->o_res.pagemod_limit);
1851 if (data->o_res.do_recall)
1852 nfs_async_inode_return_delegation(state->inode,
1853 &data->o_res.delegation);
1857 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1858 * and update the nfs4_state.
1860 static struct nfs4_state *
1861 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1863 struct inode *inode = data->state->inode;
1864 struct nfs4_state *state = data->state;
1867 if (!data->rpc_done) {
1868 if (data->rpc_status)
1869 return ERR_PTR(data->rpc_status);
1870 /* cached opens have already been processed */
1874 ret = nfs_refresh_inode(inode, &data->f_attr);
1876 return ERR_PTR(ret);
1878 if (data->o_res.delegation_type != 0)
1879 nfs4_opendata_check_deleg(data, state);
1881 update_open_stateid(state, &data->o_res.stateid, NULL,
1883 refcount_inc(&state->count);
1888 static struct inode *
1889 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1891 struct inode *inode;
1893 switch (data->o_arg.claim) {
1894 case NFS4_OPEN_CLAIM_NULL:
1895 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1896 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1897 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1898 return ERR_PTR(-EAGAIN);
1899 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1900 &data->f_attr, data->f_label);
1903 inode = d_inode(data->dentry);
1905 nfs_refresh_inode(inode, &data->f_attr);
1910 static struct nfs4_state *
1911 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1913 struct nfs4_state *state;
1914 struct inode *inode;
1916 inode = nfs4_opendata_get_inode(data);
1918 return ERR_CAST(inode);
1919 if (data->state != NULL && data->state->inode == inode) {
1920 state = data->state;
1921 refcount_inc(&state->count);
1923 state = nfs4_get_open_state(inode, data->owner);
1926 state = ERR_PTR(-ENOMEM);
1930 static struct nfs4_state *
1931 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1933 struct nfs4_state *state;
1935 if (!data->rpc_done) {
1936 state = nfs4_try_open_cached(data);
1937 trace_nfs4_cached_open(data->state);
1941 state = nfs4_opendata_find_nfs4_state(data);
1945 if (data->o_res.delegation_type != 0)
1946 nfs4_opendata_check_deleg(data, state);
1947 update_open_stateid(state, &data->o_res.stateid, NULL,
1950 nfs_release_seqid(data->o_arg.seqid);
1954 static struct nfs4_state *
1955 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1957 struct nfs4_state *ret;
1959 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1960 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1962 ret = _nfs4_opendata_to_nfs4_state(data);
1963 nfs4_sequence_free_slot(&data->o_res.seq_res);
1967 static struct nfs_open_context *
1968 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
1970 struct nfs_inode *nfsi = NFS_I(state->inode);
1971 struct nfs_open_context *ctx;
1974 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
1975 if (ctx->state != state)
1977 if ((ctx->mode & mode) != mode)
1979 if (!get_nfs_open_context(ctx))
1985 return ERR_PTR(-ENOENT);
1988 static struct nfs_open_context *
1989 nfs4_state_find_open_context(struct nfs4_state *state)
1991 struct nfs_open_context *ctx;
1993 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
1996 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
1999 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2002 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2003 struct nfs4_state *state, enum open_claim_type4 claim)
2005 struct nfs4_opendata *opendata;
2007 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2008 NULL, claim, GFP_NOFS);
2009 if (opendata == NULL)
2010 return ERR_PTR(-ENOMEM);
2011 opendata->state = state;
2012 refcount_inc(&state->count);
2016 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2019 struct nfs4_state *newstate;
2022 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2024 opendata->o_arg.open_flags = 0;
2025 opendata->o_arg.fmode = fmode;
2026 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2027 NFS_SB(opendata->dentry->d_sb),
2029 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2030 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2031 nfs4_init_opendata_res(opendata);
2032 ret = _nfs4_recover_proc_open(opendata);
2035 newstate = nfs4_opendata_to_nfs4_state(opendata);
2036 if (IS_ERR(newstate))
2037 return PTR_ERR(newstate);
2038 if (newstate != opendata->state)
2040 nfs4_close_state(newstate, fmode);
2044 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2048 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
2049 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2050 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2051 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2052 /* memory barrier prior to reading state->n_* */
2053 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2054 clear_bit(NFS_OPEN_STATE, &state->flags);
2056 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2059 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2062 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2066 * We may have performed cached opens for all three recoveries.
2067 * Check if we need to update the current stateid.
2069 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2070 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2071 write_seqlock(&state->seqlock);
2072 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2073 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2074 write_sequnlock(&state->seqlock);
2081 * reclaim state on the server after a reboot.
2083 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2085 struct nfs_delegation *delegation;
2086 struct nfs4_opendata *opendata;
2087 fmode_t delegation_type = 0;
2090 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2091 NFS4_OPEN_CLAIM_PREVIOUS);
2092 if (IS_ERR(opendata))
2093 return PTR_ERR(opendata);
2095 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2096 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2097 delegation_type = delegation->type;
2099 opendata->o_arg.u.delegation_type = delegation_type;
2100 status = nfs4_open_recover(opendata, state);
2101 nfs4_opendata_put(opendata);
2105 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2107 struct nfs_server *server = NFS_SERVER(state->inode);
2108 struct nfs4_exception exception = { };
2111 err = _nfs4_do_open_reclaim(ctx, state);
2112 trace_nfs4_open_reclaim(ctx, 0, err);
2113 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2115 if (err != -NFS4ERR_DELAY)
2117 nfs4_handle_exception(server, err, &exception);
2118 } while (exception.retry);
2122 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2124 struct nfs_open_context *ctx;
2127 ctx = nfs4_state_find_open_context(state);
2130 ret = nfs4_do_open_reclaim(ctx, state);
2131 put_nfs_open_context(ctx);
2135 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)
2139 printk(KERN_ERR "NFS: %s: unhandled error "
2140 "%d.\n", __func__, err);
2146 case -NFS4ERR_BADSESSION:
2147 case -NFS4ERR_BADSLOT:
2148 case -NFS4ERR_BAD_HIGH_SLOT:
2149 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2150 case -NFS4ERR_DEADSESSION:
2151 set_bit(NFS_DELEGATED_STATE, &state->flags);
2152 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2154 case -NFS4ERR_STALE_CLIENTID:
2155 case -NFS4ERR_STALE_STATEID:
2156 set_bit(NFS_DELEGATED_STATE, &state->flags);
2157 /* Don't recall a delegation if it was lost */
2158 nfs4_schedule_lease_recovery(server->nfs_client);
2160 case -NFS4ERR_MOVED:
2161 nfs4_schedule_migration_recovery(server);
2163 case -NFS4ERR_LEASE_MOVED:
2164 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2166 case -NFS4ERR_DELEG_REVOKED:
2167 case -NFS4ERR_ADMIN_REVOKED:
2168 case -NFS4ERR_EXPIRED:
2169 case -NFS4ERR_BAD_STATEID:
2170 case -NFS4ERR_OPENMODE:
2171 nfs_inode_find_state_and_recover(state->inode,
2173 nfs4_schedule_stateid_recovery(server, state);
2175 case -NFS4ERR_DELAY:
2176 case -NFS4ERR_GRACE:
2177 set_bit(NFS_DELEGATED_STATE, &state->flags);
2181 case -NFS4ERR_DENIED:
2183 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2185 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2192 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2193 struct nfs4_state *state, const nfs4_stateid *stateid,
2196 struct nfs_server *server = NFS_SERVER(state->inode);
2197 struct nfs4_opendata *opendata;
2200 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2201 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2202 if (IS_ERR(opendata))
2203 return PTR_ERR(opendata);
2204 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2205 nfs_state_clear_delegation(state);
2206 switch (type & (FMODE_READ|FMODE_WRITE)) {
2207 case FMODE_READ|FMODE_WRITE:
2209 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2212 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2217 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2219 nfs4_opendata_put(opendata);
2220 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2223 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2225 struct nfs4_opendata *data = calldata;
2227 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2228 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2231 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2233 struct nfs4_opendata *data = calldata;
2235 nfs40_sequence_done(task, &data->c_res.seq_res);
2237 data->rpc_status = task->tk_status;
2238 if (data->rpc_status == 0) {
2239 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2240 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2241 renew_lease(data->o_res.server, data->timestamp);
2242 data->rpc_done = true;
2246 static void nfs4_open_confirm_release(void *calldata)
2248 struct nfs4_opendata *data = calldata;
2249 struct nfs4_state *state = NULL;
2251 /* If this request hasn't been cancelled, do nothing */
2252 if (!data->cancelled)
2254 /* In case of error, no cleanup! */
2255 if (!data->rpc_done)
2257 state = nfs4_opendata_to_nfs4_state(data);
2259 nfs4_close_state(state, data->o_arg.fmode);
2261 nfs4_opendata_put(data);
2264 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2265 .rpc_call_prepare = nfs4_open_confirm_prepare,
2266 .rpc_call_done = nfs4_open_confirm_done,
2267 .rpc_release = nfs4_open_confirm_release,
2271 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2273 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2275 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2276 struct rpc_task *task;
2277 struct rpc_message msg = {
2278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2279 .rpc_argp = &data->c_arg,
2280 .rpc_resp = &data->c_res,
2281 .rpc_cred = data->owner->so_cred,
2283 struct rpc_task_setup task_setup_data = {
2284 .rpc_client = server->client,
2285 .rpc_message = &msg,
2286 .callback_ops = &nfs4_open_confirm_ops,
2287 .callback_data = data,
2288 .workqueue = nfsiod_workqueue,
2289 .flags = RPC_TASK_ASYNC,
2293 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2295 kref_get(&data->kref);
2296 data->rpc_done = false;
2297 data->rpc_status = 0;
2298 data->timestamp = jiffies;
2299 task = rpc_run_task(&task_setup_data);
2301 return PTR_ERR(task);
2302 status = rpc_wait_for_completion_task(task);
2304 data->cancelled = true;
2307 status = data->rpc_status;
2312 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2314 struct nfs4_opendata *data = calldata;
2315 struct nfs4_state_owner *sp = data->owner;
2316 struct nfs_client *clp = sp->so_server->nfs_client;
2317 enum open_claim_type4 claim = data->o_arg.claim;
2319 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2322 * Check if we still need to send an OPEN call, or if we can use
2323 * a delegation instead.
2325 if (data->state != NULL) {
2326 struct nfs_delegation *delegation;
2328 if (can_open_cached(data->state, data->o_arg.fmode,
2329 data->o_arg.open_flags, claim))
2332 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2333 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2334 goto unlock_no_action;
2337 /* Update client id. */
2338 data->o_arg.clientid = clp->cl_clientid;
2342 case NFS4_OPEN_CLAIM_PREVIOUS:
2343 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2344 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2345 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2347 case NFS4_OPEN_CLAIM_FH:
2348 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2350 data->timestamp = jiffies;
2351 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2352 &data->o_arg.seq_args,
2353 &data->o_res.seq_res,
2355 nfs_release_seqid(data->o_arg.seqid);
2357 /* Set the create mode (note dependency on the session type) */
2358 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2359 if (data->o_arg.open_flags & O_EXCL) {
2360 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2361 if (nfs4_has_persistent_session(clp))
2362 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2363 else if (clp->cl_mvops->minor_version > 0)
2364 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2368 trace_nfs4_cached_open(data->state);
2371 task->tk_action = NULL;
2373 nfs4_sequence_done(task, &data->o_res.seq_res);
2376 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2378 struct nfs4_opendata *data = calldata;
2380 data->rpc_status = task->tk_status;
2382 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2385 if (task->tk_status == 0) {
2386 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2387 switch (data->o_res.f_attr->mode & S_IFMT) {
2391 data->rpc_status = -ELOOP;
2394 data->rpc_status = -EISDIR;
2397 data->rpc_status = -ENOTDIR;
2400 renew_lease(data->o_res.server, data->timestamp);
2401 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2402 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2404 data->rpc_done = true;
2407 static void nfs4_open_release(void *calldata)
2409 struct nfs4_opendata *data = calldata;
2410 struct nfs4_state *state = NULL;
2412 /* If this request hasn't been cancelled, do nothing */
2413 if (!data->cancelled)
2415 /* In case of error, no cleanup! */
2416 if (data->rpc_status != 0 || !data->rpc_done)
2418 /* In case we need an open_confirm, no cleanup! */
2419 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2421 state = nfs4_opendata_to_nfs4_state(data);
2423 nfs4_close_state(state, data->o_arg.fmode);
2425 nfs4_opendata_put(data);
2428 static const struct rpc_call_ops nfs4_open_ops = {
2429 .rpc_call_prepare = nfs4_open_prepare,
2430 .rpc_call_done = nfs4_open_done,
2431 .rpc_release = nfs4_open_release,
2434 static int nfs4_run_open_task(struct nfs4_opendata *data,
2435 struct nfs_open_context *ctx)
2437 struct inode *dir = d_inode(data->dir);
2438 struct nfs_server *server = NFS_SERVER(dir);
2439 struct nfs_openargs *o_arg = &data->o_arg;
2440 struct nfs_openres *o_res = &data->o_res;
2441 struct rpc_task *task;
2442 struct rpc_message msg = {
2443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2446 .rpc_cred = data->owner->so_cred,
2448 struct rpc_task_setup task_setup_data = {
2449 .rpc_client = server->client,
2450 .rpc_message = &msg,
2451 .callback_ops = &nfs4_open_ops,
2452 .callback_data = data,
2453 .workqueue = nfsiod_workqueue,
2454 .flags = RPC_TASK_ASYNC,
2458 kref_get(&data->kref);
2459 data->rpc_done = false;
2460 data->rpc_status = 0;
2461 data->cancelled = false;
2462 data->is_recover = false;
2464 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2465 data->is_recover = true;
2467 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2468 pnfs_lgopen_prepare(data, ctx);
2470 task = rpc_run_task(&task_setup_data);
2472 return PTR_ERR(task);
2473 status = rpc_wait_for_completion_task(task);
2475 data->cancelled = true;
2478 status = data->rpc_status;
2484 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2486 struct inode *dir = d_inode(data->dir);
2487 struct nfs_openres *o_res = &data->o_res;
2490 status = nfs4_run_open_task(data, NULL);
2491 if (status != 0 || !data->rpc_done)
2494 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2496 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2497 status = _nfs4_proc_open_confirm(data);
2503 * Additional permission checks in order to distinguish between an
2504 * open for read, and an open for execute. This works around the
2505 * fact that NFSv4 OPEN treats read and execute permissions as being
2507 * Note that in the non-execute case, we want to turn off permission
2508 * checking if we just created a new file (POSIX open() semantics).
2510 static int nfs4_opendata_access(const struct cred *cred,
2511 struct nfs4_opendata *opendata,
2512 struct nfs4_state *state, fmode_t fmode,
2515 struct nfs_access_entry cache;
2518 /* access call failed or for some reason the server doesn't
2519 * support any access modes -- defer access call until later */
2520 if (opendata->o_res.access_supported == 0)
2525 * Use openflags to check for exec, because fmode won't
2526 * always have FMODE_EXEC set when file open for exec.
2528 if (openflags & __FMODE_EXEC) {
2529 /* ONLY check for exec rights */
2530 if (S_ISDIR(state->inode->i_mode))
2531 mask = NFS4_ACCESS_LOOKUP;
2533 mask = NFS4_ACCESS_EXECUTE;
2534 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2535 mask = NFS4_ACCESS_READ;
2538 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2539 nfs_access_add_cache(state->inode, &cache);
2541 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2542 if ((mask & ~cache.mask & flags) == 0)
2549 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2551 static int _nfs4_proc_open(struct nfs4_opendata *data,
2552 struct nfs_open_context *ctx)
2554 struct inode *dir = d_inode(data->dir);
2555 struct nfs_server *server = NFS_SERVER(dir);
2556 struct nfs_openargs *o_arg = &data->o_arg;
2557 struct nfs_openres *o_res = &data->o_res;
2560 status = nfs4_run_open_task(data, ctx);
2561 if (!data->rpc_done)
2564 if (status == -NFS4ERR_BADNAME &&
2565 !(o_arg->open_flags & O_CREAT))
2570 nfs_fattr_map_and_free_names(server, &data->f_attr);
2572 if (o_arg->open_flags & O_CREAT) {
2573 if (o_arg->open_flags & O_EXCL)
2574 data->file_created = true;
2575 else if (o_res->cinfo.before != o_res->cinfo.after)
2576 data->file_created = true;
2577 if (data->file_created ||
2578 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2579 update_changeattr(dir, &o_res->cinfo,
2580 o_res->f_attr->time_start, 0);
2582 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2583 server->caps &= ~NFS_CAP_POSIX_LOCK;
2584 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2585 status = _nfs4_proc_open_confirm(data);
2589 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2590 nfs4_sequence_free_slot(&o_res->seq_res);
2591 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2592 o_res->f_label, NULL);
2599 * reclaim state on the server after a network partition.
2600 * Assumes caller holds the appropriate lock
2602 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2604 struct nfs4_opendata *opendata;
2607 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2608 NFS4_OPEN_CLAIM_FH);
2609 if (IS_ERR(opendata))
2610 return PTR_ERR(opendata);
2611 ret = nfs4_open_recover(opendata, state);
2613 d_drop(ctx->dentry);
2614 nfs4_opendata_put(opendata);
2618 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2620 struct nfs_server *server = NFS_SERVER(state->inode);
2621 struct nfs4_exception exception = { };
2625 err = _nfs4_open_expired(ctx, state);
2626 trace_nfs4_open_expired(ctx, 0, err);
2627 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2632 case -NFS4ERR_GRACE:
2633 case -NFS4ERR_DELAY:
2634 nfs4_handle_exception(server, err, &exception);
2637 } while (exception.retry);
2642 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2644 struct nfs_open_context *ctx;
2647 ctx = nfs4_state_find_open_context(state);
2650 ret = nfs4_do_open_expired(ctx, state);
2651 put_nfs_open_context(ctx);
2655 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2656 const nfs4_stateid *stateid)
2658 nfs_remove_bad_delegation(state->inode, stateid);
2659 nfs_state_clear_delegation(state);
2662 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2664 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2665 nfs_finish_clear_delegation_stateid(state, NULL);
2668 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2670 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2671 nfs40_clear_delegation_stateid(state);
2672 return nfs4_open_expired(sp, state);
2675 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2676 nfs4_stateid *stateid,
2677 const struct cred *cred)
2679 return -NFS4ERR_BAD_STATEID;
2682 #if defined(CONFIG_NFS_V4_1)
2683 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2684 nfs4_stateid *stateid,
2685 const struct cred *cred)
2689 switch (stateid->type) {
2692 case NFS4_INVALID_STATEID_TYPE:
2693 case NFS4_SPECIAL_STATEID_TYPE:
2694 return -NFS4ERR_BAD_STATEID;
2695 case NFS4_REVOKED_STATEID_TYPE:
2699 status = nfs41_test_stateid(server, stateid, cred);
2701 case -NFS4ERR_EXPIRED:
2702 case -NFS4ERR_ADMIN_REVOKED:
2703 case -NFS4ERR_DELEG_REVOKED:
2709 /* Ack the revoked state to the server */
2710 nfs41_free_stateid(server, stateid, cred, true);
2711 return -NFS4ERR_EXPIRED;
2714 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2716 struct nfs_server *server = NFS_SERVER(state->inode);
2717 nfs4_stateid stateid;
2718 struct nfs_delegation *delegation;
2719 const struct cred *cred = NULL;
2722 /* Get the delegation credential for use by test/free_stateid */
2724 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2725 if (delegation == NULL) {
2727 nfs_state_clear_delegation(state);
2731 nfs4_stateid_copy(&stateid, &delegation->stateid);
2732 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2734 nfs_state_clear_delegation(state);
2738 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2739 &delegation->flags)) {
2744 if (delegation->cred)
2745 cred = get_cred(delegation->cred);
2747 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2748 trace_nfs4_test_delegation_stateid(state, NULL, status);
2749 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2750 nfs_finish_clear_delegation_stateid(state, &stateid);
2752 if (delegation->cred)
2757 * nfs41_check_expired_locks - possibly free a lock stateid
2759 * @state: NFSv4 state for an inode
2761 * Returns NFS_OK if recovery for this stateid is now finished.
2762 * Otherwise a negative NFS4ERR value is returned.
2764 static int nfs41_check_expired_locks(struct nfs4_state *state)
2766 int status, ret = NFS_OK;
2767 struct nfs4_lock_state *lsp, *prev = NULL;
2768 struct nfs_server *server = NFS_SERVER(state->inode);
2770 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2773 spin_lock(&state->state_lock);
2774 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2775 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2776 const struct cred *cred = lsp->ls_state->owner->so_cred;
2778 refcount_inc(&lsp->ls_count);
2779 spin_unlock(&state->state_lock);
2781 nfs4_put_lock_state(prev);
2784 status = nfs41_test_and_free_expired_stateid(server,
2787 trace_nfs4_test_lock_stateid(state, lsp, status);
2788 if (status == -NFS4ERR_EXPIRED ||
2789 status == -NFS4ERR_BAD_STATEID) {
2790 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2791 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2792 if (!recover_lost_locks)
2793 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2794 } else if (status != NFS_OK) {
2796 nfs4_put_lock_state(prev);
2799 spin_lock(&state->state_lock);
2802 spin_unlock(&state->state_lock);
2803 nfs4_put_lock_state(prev);
2809 * nfs41_check_open_stateid - possibly free an open stateid
2811 * @state: NFSv4 state for an inode
2813 * Returns NFS_OK if recovery for this stateid is now finished.
2814 * Otherwise a negative NFS4ERR value is returned.
2816 static int nfs41_check_open_stateid(struct nfs4_state *state)
2818 struct nfs_server *server = NFS_SERVER(state->inode);
2819 nfs4_stateid *stateid = &state->open_stateid;
2820 const struct cred *cred = state->owner->so_cred;
2823 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2824 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2825 if (nfs4_have_delegation(state->inode, state->state))
2827 return -NFS4ERR_OPENMODE;
2829 return -NFS4ERR_BAD_STATEID;
2831 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2832 trace_nfs4_test_open_stateid(state, NULL, status);
2833 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2834 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2835 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2836 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2837 clear_bit(NFS_OPEN_STATE, &state->flags);
2838 stateid->type = NFS4_INVALID_STATEID_TYPE;
2841 if (nfs_open_stateid_recover_openmode(state))
2842 return -NFS4ERR_OPENMODE;
2846 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2850 nfs41_check_delegation_stateid(state);
2851 status = nfs41_check_expired_locks(state);
2852 if (status != NFS_OK)
2854 status = nfs41_check_open_stateid(state);
2855 if (status != NFS_OK)
2856 status = nfs4_open_expired(sp, state);
2862 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2863 * fields corresponding to attributes that were used to store the verifier.
2864 * Make sure we clobber those fields in the later setattr call
2866 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2867 struct iattr *sattr, struct nfs4_label **label)
2869 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2874 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2875 attrset[i] = opendata->o_res.attrset[i];
2876 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2877 attrset[i] &= ~bitmask[i];
2880 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2881 sattr->ia_valid : 0;
2883 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2884 if (sattr->ia_valid & ATTR_ATIME_SET)
2885 ret |= ATTR_ATIME_SET;
2890 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2891 if (sattr->ia_valid & ATTR_MTIME_SET)
2892 ret |= ATTR_MTIME_SET;
2897 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
2902 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2905 struct nfs_open_context *ctx)
2907 struct nfs4_state_owner *sp = opendata->owner;
2908 struct nfs_server *server = sp->so_server;
2909 struct dentry *dentry;
2910 struct nfs4_state *state;
2914 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2916 ret = _nfs4_proc_open(opendata, ctx);
2920 state = _nfs4_opendata_to_nfs4_state(opendata);
2921 ret = PTR_ERR(state);
2925 if (server->caps & NFS_CAP_POSIX_LOCK)
2926 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2927 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2928 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2930 dentry = opendata->dentry;
2931 if (d_really_is_negative(dentry)) {
2932 struct dentry *alias;
2934 alias = d_exact_alias(dentry, state->inode);
2936 alias = d_splice_alias(igrab(state->inode), dentry);
2937 /* d_splice_alias() can't fail here - it's a non-directory */
2940 ctx->dentry = dentry = alias;
2942 nfs_set_verifier(dentry,
2943 nfs_save_change_attribute(d_inode(opendata->dir)));
2946 /* Parse layoutget results before we check for access */
2947 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2949 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2953 if (d_inode(dentry) == state->inode) {
2954 nfs_inode_attach_open_context(ctx);
2955 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2956 nfs4_schedule_stateid_recovery(server, state);
2960 if (!opendata->cancelled)
2961 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
2966 * Returns a referenced nfs4_state
2968 static int _nfs4_do_open(struct inode *dir,
2969 struct nfs_open_context *ctx,
2971 const struct nfs4_open_createattrs *c,
2974 struct nfs4_state_owner *sp;
2975 struct nfs4_state *state = NULL;
2976 struct nfs_server *server = NFS_SERVER(dir);
2977 struct nfs4_opendata *opendata;
2978 struct dentry *dentry = ctx->dentry;
2979 const struct cred *cred = ctx->cred;
2980 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2981 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2982 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2983 struct iattr *sattr = c->sattr;
2984 struct nfs4_label *label = c->label;
2985 struct nfs4_label *olabel = NULL;
2988 /* Protect against reboot recovery conflicts */
2990 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2992 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2995 status = nfs4_client_recover_expired_lease(server->nfs_client);
2997 goto err_put_state_owner;
2998 if (d_really_is_positive(dentry))
2999 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3001 if (d_really_is_positive(dentry))
3002 claim = NFS4_OPEN_CLAIM_FH;
3003 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3004 c, claim, GFP_KERNEL);
3005 if (opendata == NULL)
3006 goto err_put_state_owner;
3009 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3010 if (IS_ERR(olabel)) {
3011 status = PTR_ERR(olabel);
3012 goto err_opendata_put;
3016 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3017 if (!opendata->f_attr.mdsthreshold) {
3018 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3019 if (!opendata->f_attr.mdsthreshold)
3020 goto err_free_label;
3022 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3024 if (d_really_is_positive(dentry))
3025 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3027 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
3029 goto err_free_label;
3032 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3033 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3034 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3036 * send create attributes which was not set by open
3037 * with an extra setattr.
3039 if (attrs || label) {
3040 unsigned ia_old = sattr->ia_valid;
3042 sattr->ia_valid = attrs;
3043 nfs_fattr_init(opendata->o_res.f_attr);
3044 status = nfs4_do_setattr(state->inode, cred,
3045 opendata->o_res.f_attr, sattr,
3046 ctx, label, olabel);
3048 nfs_setattr_update_inode(state->inode, sattr,
3049 opendata->o_res.f_attr);
3050 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3052 sattr->ia_valid = ia_old;
3055 if (opened && opendata->file_created)
3058 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3059 *ctx_th = opendata->f_attr.mdsthreshold;
3060 opendata->f_attr.mdsthreshold = NULL;
3063 nfs4_label_free(olabel);
3065 nfs4_opendata_put(opendata);
3066 nfs4_put_state_owner(sp);
3069 nfs4_label_free(olabel);
3071 nfs4_opendata_put(opendata);
3072 err_put_state_owner:
3073 nfs4_put_state_owner(sp);
3079 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3080 struct nfs_open_context *ctx,
3082 struct iattr *sattr,
3083 struct nfs4_label *label,
3086 struct nfs_server *server = NFS_SERVER(dir);
3087 struct nfs4_exception exception = {
3088 .interruptible = true,
3090 struct nfs4_state *res;
3091 struct nfs4_open_createattrs c = {
3095 [0] = (__u32)jiffies,
3096 [1] = (__u32)current->pid,
3102 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3104 trace_nfs4_open_file(ctx, flags, status);
3107 /* NOTE: BAD_SEQID means the server and client disagree about the
3108 * book-keeping w.r.t. state-changing operations
3109 * (OPEN/CLOSE/LOCK/LOCKU...)
3110 * It is actually a sign of a bug on the client or on the server.
3112 * If we receive a BAD_SEQID error in the particular case of
3113 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3114 * have unhashed the old state_owner for us, and that we can
3115 * therefore safely retry using a new one. We should still warn
3116 * the user though...
3118 if (status == -NFS4ERR_BAD_SEQID) {
3119 pr_warn_ratelimited("NFS: v4 server %s "
3120 " returned a bad sequence-id error!\n",
3121 NFS_SERVER(dir)->nfs_client->cl_hostname);
3122 exception.retry = 1;
3126 * BAD_STATEID on OPEN means that the server cancelled our
3127 * state before it received the OPEN_CONFIRM.
3128 * Recover by retrying the request as per the discussion
3129 * on Page 181 of RFC3530.
3131 if (status == -NFS4ERR_BAD_STATEID) {
3132 exception.retry = 1;
3135 if (status == -EAGAIN) {
3136 /* We must have found a delegation */
3137 exception.retry = 1;
3140 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3142 res = ERR_PTR(nfs4_handle_exception(server,
3143 status, &exception));
3144 } while (exception.retry);
3148 static int _nfs4_do_setattr(struct inode *inode,
3149 struct nfs_setattrargs *arg,
3150 struct nfs_setattrres *res,
3151 const struct cred *cred,
3152 struct nfs_open_context *ctx)
3154 struct nfs_server *server = NFS_SERVER(inode);
3155 struct rpc_message msg = {
3156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3161 const struct cred *delegation_cred = NULL;
3162 unsigned long timestamp = jiffies;
3166 nfs_fattr_init(res->fattr);
3168 /* Servers should only apply open mode checks for file size changes */
3169 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3173 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3174 /* Use that stateid */
3175 } else if (ctx != NULL) {
3176 struct nfs_lock_context *l_ctx;
3177 if (!nfs4_valid_open_stateid(ctx->state))
3179 l_ctx = nfs_get_lock_context(ctx);
3181 return PTR_ERR(l_ctx);
3182 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3183 &arg->stateid, &delegation_cred);
3184 nfs_put_lock_context(l_ctx);
3189 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3191 if (delegation_cred)
3192 msg.rpc_cred = delegation_cred;
3194 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3196 put_cred(delegation_cred);
3197 if (status == 0 && ctx != NULL)
3198 renew_lease(server, timestamp);
3199 trace_nfs4_setattr(inode, &arg->stateid, status);
3203 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3204 struct nfs_fattr *fattr, struct iattr *sattr,
3205 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3206 struct nfs4_label *olabel)
3208 struct nfs_server *server = NFS_SERVER(inode);
3209 __u32 bitmask[NFS4_BITMASK_SZ];
3210 struct nfs4_state *state = ctx ? ctx->state : NULL;
3211 struct nfs_setattrargs arg = {
3212 .fh = NFS_FH(inode),
3218 struct nfs_setattrres res = {
3223 struct nfs4_exception exception = {
3226 .stateid = &arg.stateid,
3231 nfs4_bitmap_copy_adjust_setattr(bitmask,
3232 nfs4_bitmask(server, olabel),
3235 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3237 case -NFS4ERR_OPENMODE:
3238 if (!(sattr->ia_valid & ATTR_SIZE)) {
3239 pr_warn_once("NFSv4: server %s is incorrectly "
3240 "applying open mode checks to "
3241 "a SETATTR that is not "
3242 "changing file size.\n",
3243 server->nfs_client->cl_hostname);
3245 if (state && !(state->state & FMODE_WRITE)) {
3247 if (sattr->ia_valid & ATTR_OPEN)
3252 err = nfs4_handle_exception(server, err, &exception);
3253 } while (exception.retry);
3259 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3261 if (inode == NULL || !nfs_have_layout(inode))
3264 return pnfs_wait_on_layoutreturn(inode, task);
3267 struct nfs4_closedata {
3268 struct inode *inode;
3269 struct nfs4_state *state;
3270 struct nfs_closeargs arg;
3271 struct nfs_closeres res;
3273 struct nfs4_layoutreturn_args arg;
3274 struct nfs4_layoutreturn_res res;
3275 struct nfs4_xdr_opaque_data ld_private;
3279 struct nfs_fattr fattr;
3280 unsigned long timestamp;
3283 static void nfs4_free_closedata(void *data)
3285 struct nfs4_closedata *calldata = data;
3286 struct nfs4_state_owner *sp = calldata->state->owner;
3287 struct super_block *sb = calldata->state->inode->i_sb;
3289 if (calldata->lr.roc)
3290 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3291 calldata->res.lr_ret);
3292 nfs4_put_open_state(calldata->state);
3293 nfs_free_seqid(calldata->arg.seqid);
3294 nfs4_put_state_owner(sp);
3295 nfs_sb_deactive(sb);
3299 static void nfs4_close_done(struct rpc_task *task, void *data)
3301 struct nfs4_closedata *calldata = data;
3302 struct nfs4_state *state = calldata->state;
3303 struct nfs_server *server = NFS_SERVER(calldata->inode);
3304 nfs4_stateid *res_stateid = NULL;
3305 struct nfs4_exception exception = {
3307 .inode = calldata->inode,
3308 .stateid = &calldata->arg.stateid,
3311 dprintk("%s: begin!\n", __func__);
3312 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3314 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3316 /* Handle Layoutreturn errors */
3317 if (calldata->arg.lr_args && task->tk_status != 0) {
3318 switch (calldata->res.lr_ret) {
3320 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3323 calldata->arg.lr_args = NULL;
3324 calldata->res.lr_res = NULL;
3326 case -NFS4ERR_OLD_STATEID:
3327 if (nfs4_layoutreturn_refresh_stateid(&calldata->arg.lr_args->stateid,
3328 &calldata->arg.lr_args->range,
3332 case -NFS4ERR_ADMIN_REVOKED:
3333 case -NFS4ERR_DELEG_REVOKED:
3334 case -NFS4ERR_EXPIRED:
3335 case -NFS4ERR_BAD_STATEID:
3336 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3337 case -NFS4ERR_WRONG_CRED:
3338 calldata->arg.lr_args = NULL;
3339 calldata->res.lr_res = NULL;
3344 /* hmm. we are done with the inode, and in the process of freeing
3345 * the state_owner. we keep this around to process errors
3347 switch (task->tk_status) {
3349 res_stateid = &calldata->res.stateid;
3350 renew_lease(server, calldata->timestamp);
3352 case -NFS4ERR_ACCESS:
3353 if (calldata->arg.bitmask != NULL) {
3354 calldata->arg.bitmask = NULL;
3355 calldata->res.fattr = NULL;
3360 case -NFS4ERR_OLD_STATEID:
3361 /* Did we race with OPEN? */
3362 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
3366 case -NFS4ERR_ADMIN_REVOKED:
3367 case -NFS4ERR_STALE_STATEID:
3368 case -NFS4ERR_EXPIRED:
3369 nfs4_free_revoked_stateid(server,
3370 &calldata->arg.stateid,
3371 task->tk_msg.rpc_cred);
3373 case -NFS4ERR_BAD_STATEID:
3376 task->tk_status = nfs4_async_handle_exception(task,
3377 server, task->tk_status, &exception);
3378 if (exception.retry)
3381 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3382 res_stateid, calldata->arg.fmode);
3384 task->tk_status = 0;
3385 nfs_release_seqid(calldata->arg.seqid);
3386 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3387 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3390 calldata->res.lr_ret = 0;
3392 task->tk_status = 0;
3393 rpc_restart_call_prepare(task);
3397 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3399 struct nfs4_closedata *calldata = data;
3400 struct nfs4_state *state = calldata->state;
3401 struct inode *inode = calldata->inode;
3402 struct pnfs_layout_hdr *lo;
3403 bool is_rdonly, is_wronly, is_rdwr;
3406 dprintk("%s: begin!\n", __func__);
3407 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3410 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3411 spin_lock(&state->owner->so_lock);
3412 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3413 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3414 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3415 /* Calculate the change in open mode */
3416 calldata->arg.fmode = 0;
3417 if (state->n_rdwr == 0) {
3418 if (state->n_rdonly == 0)
3419 call_close |= is_rdonly;
3421 calldata->arg.fmode |= FMODE_READ;
3422 if (state->n_wronly == 0)
3423 call_close |= is_wronly;
3425 calldata->arg.fmode |= FMODE_WRITE;
3426 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3427 call_close |= is_rdwr;
3429 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3431 if (!nfs4_valid_open_stateid(state) ||
3432 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
3434 spin_unlock(&state->owner->so_lock);
3437 /* Note: exit _without_ calling nfs4_close_done */
3441 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3442 nfs_release_seqid(calldata->arg.seqid);
3446 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3447 if (lo && !pnfs_layout_is_valid(lo)) {
3448 calldata->arg.lr_args = NULL;
3449 calldata->res.lr_res = NULL;
3452 if (calldata->arg.fmode == 0)
3453 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3455 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3456 /* Close-to-open cache consistency revalidation */
3457 if (!nfs4_have_delegation(inode, FMODE_READ))
3458 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3460 calldata->arg.bitmask = NULL;
3463 calldata->arg.share_access =
3464 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3465 calldata->arg.fmode, 0);
3467 if (calldata->res.fattr == NULL)
3468 calldata->arg.bitmask = NULL;
3469 else if (calldata->arg.bitmask == NULL)
3470 calldata->res.fattr = NULL;
3471 calldata->timestamp = jiffies;
3472 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3473 &calldata->arg.seq_args,
3474 &calldata->res.seq_res,
3476 nfs_release_seqid(calldata->arg.seqid);
3477 dprintk("%s: done!\n", __func__);
3480 task->tk_action = NULL;
3482 nfs4_sequence_done(task, &calldata->res.seq_res);
3485 static const struct rpc_call_ops nfs4_close_ops = {
3486 .rpc_call_prepare = nfs4_close_prepare,
3487 .rpc_call_done = nfs4_close_done,
3488 .rpc_release = nfs4_free_closedata,
3492 * It is possible for data to be read/written from a mem-mapped file
3493 * after the sys_close call (which hits the vfs layer as a flush).
3494 * This means that we can't safely call nfsv4 close on a file until
3495 * the inode is cleared. This in turn means that we are not good
3496 * NFSv4 citizens - we do not indicate to the server to update the file's
3497 * share state even when we are done with one of the three share
3498 * stateid's in the inode.
3500 * NOTE: Caller must be holding the sp->so_owner semaphore!
3502 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3504 struct nfs_server *server = NFS_SERVER(state->inode);
3505 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3506 struct nfs4_closedata *calldata;
3507 struct nfs4_state_owner *sp = state->owner;
3508 struct rpc_task *task;
3509 struct rpc_message msg = {
3510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3511 .rpc_cred = state->owner->so_cred,
3513 struct rpc_task_setup task_setup_data = {
3514 .rpc_client = server->client,
3515 .rpc_message = &msg,
3516 .callback_ops = &nfs4_close_ops,
3517 .workqueue = nfsiod_workqueue,
3518 .flags = RPC_TASK_ASYNC,
3520 int status = -ENOMEM;
3522 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3523 &task_setup_data.rpc_client, &msg);
3525 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3526 if (calldata == NULL)
3528 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3529 calldata->inode = state->inode;
3530 calldata->state = state;
3531 calldata->arg.fh = NFS_FH(state->inode);
3532 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3533 goto out_free_calldata;
3534 /* Serialization for the sequence id */
3535 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3536 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3537 if (IS_ERR(calldata->arg.seqid))
3538 goto out_free_calldata;
3539 nfs_fattr_init(&calldata->fattr);
3540 calldata->arg.fmode = 0;
3541 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3542 calldata->res.fattr = &calldata->fattr;
3543 calldata->res.seqid = calldata->arg.seqid;
3544 calldata->res.server = server;
3545 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3546 calldata->lr.roc = pnfs_roc(state->inode,
3547 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3548 if (calldata->lr.roc) {
3549 calldata->arg.lr_args = &calldata->lr.arg;
3550 calldata->res.lr_res = &calldata->lr.res;
3552 nfs_sb_active(calldata->inode->i_sb);
3554 msg.rpc_argp = &calldata->arg;
3555 msg.rpc_resp = &calldata->res;
3556 task_setup_data.callback_data = calldata;
3557 task = rpc_run_task(&task_setup_data);
3559 return PTR_ERR(task);
3562 status = rpc_wait_for_completion_task(task);
3568 nfs4_put_open_state(state);
3569 nfs4_put_state_owner(sp);
3573 static struct inode *
3574 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3575 int open_flags, struct iattr *attr, int *opened)
3577 struct nfs4_state *state;
3578 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3580 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3582 /* Protect against concurrent sillydeletes */
3583 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3585 nfs4_label_release_security(label);
3588 return ERR_CAST(state);
3589 return state->inode;
3592 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3594 if (ctx->state == NULL)
3597 nfs4_close_sync(ctx->state, ctx->mode);
3599 nfs4_close_state(ctx->state, ctx->mode);
3602 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3603 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3604 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3606 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3608 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3609 struct nfs4_server_caps_arg args = {
3613 struct nfs4_server_caps_res res = {};
3614 struct rpc_message msg = {
3615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3622 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3623 FATTR4_WORD0_FH_EXPIRE_TYPE |
3624 FATTR4_WORD0_LINK_SUPPORT |
3625 FATTR4_WORD0_SYMLINK_SUPPORT |
3626 FATTR4_WORD0_ACLSUPPORT;
3628 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3630 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3632 /* Sanity check the server answers */
3633 switch (minorversion) {
3635 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3636 res.attr_bitmask[2] = 0;
3639 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3642 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3644 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3645 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3646 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3647 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3648 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3649 NFS_CAP_CTIME|NFS_CAP_MTIME|
3650 NFS_CAP_SECURITY_LABEL);
3651 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3652 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3653 server->caps |= NFS_CAP_ACLS;
3654 if (res.has_links != 0)
3655 server->caps |= NFS_CAP_HARDLINKS;
3656 if (res.has_symlinks != 0)
3657 server->caps |= NFS_CAP_SYMLINKS;
3658 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3659 server->caps |= NFS_CAP_FILEID;
3660 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3661 server->caps |= NFS_CAP_MODE;
3662 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3663 server->caps |= NFS_CAP_NLINK;
3664 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3665 server->caps |= NFS_CAP_OWNER;
3666 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3667 server->caps |= NFS_CAP_OWNER_GROUP;
3668 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3669 server->caps |= NFS_CAP_ATIME;
3670 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3671 server->caps |= NFS_CAP_CTIME;
3672 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3673 server->caps |= NFS_CAP_MTIME;
3674 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3675 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3676 server->caps |= NFS_CAP_SECURITY_LABEL;
3678 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3679 sizeof(server->attr_bitmask));
3680 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3682 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3683 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3684 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3685 server->cache_consistency_bitmask[2] = 0;
3687 /* Avoid a regression due to buggy server */
3688 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3689 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3690 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3691 sizeof(server->exclcreat_bitmask));
3693 server->acl_bitmask = res.acl_bitmask;
3694 server->fh_expire_type = res.fh_expire_type;
3700 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3702 struct nfs4_exception exception = {
3703 .interruptible = true,
3707 err = nfs4_handle_exception(server,
3708 _nfs4_server_capabilities(server, fhandle),
3710 } while (exception.retry);
3714 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3715 struct nfs_fsinfo *info)
3718 struct nfs4_lookup_root_arg args = {
3721 struct nfs4_lookup_res res = {
3723 .fattr = info->fattr,
3726 struct rpc_message msg = {
3727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3732 bitmask[0] = nfs4_fattr_bitmap[0];
3733 bitmask[1] = nfs4_fattr_bitmap[1];
3735 * Process the label in the upcoming getfattr
3737 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3739 nfs_fattr_init(info->fattr);
3740 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3743 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3744 struct nfs_fsinfo *info)
3746 struct nfs4_exception exception = {
3747 .interruptible = true,
3751 err = _nfs4_lookup_root(server, fhandle, info);
3752 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3755 case -NFS4ERR_WRONGSEC:
3758 err = nfs4_handle_exception(server, err, &exception);
3760 } while (exception.retry);
3765 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3766 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3768 struct rpc_auth_create_args auth_args = {
3769 .pseudoflavor = flavor,
3771 struct rpc_auth *auth;
3773 auth = rpcauth_create(&auth_args, server->client);
3776 return nfs4_lookup_root(server, fhandle, info);
3780 * Retry pseudoroot lookup with various security flavors. We do this when:
3782 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3783 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3785 * Returns zero on success, or a negative NFS4ERR value, or a
3786 * negative errno value.
3788 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3789 struct nfs_fsinfo *info)
3791 /* Per 3530bis 15.33.5 */
3792 static const rpc_authflavor_t flav_array[] = {
3796 RPC_AUTH_UNIX, /* courtesy */
3799 int status = -EPERM;
3802 if (server->auth_info.flavor_len > 0) {
3803 /* try each flavor specified by user */
3804 for (i = 0; i < server->auth_info.flavor_len; i++) {
3805 status = nfs4_lookup_root_sec(server, fhandle, info,
3806 server->auth_info.flavors[i]);
3807 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3812 /* no flavors specified by user, try default list */
3813 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3814 status = nfs4_lookup_root_sec(server, fhandle, info,
3816 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3823 * -EACCES could mean that the user doesn't have correct permissions
3824 * to access the mount. It could also mean that we tried to mount
3825 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3826 * existing mount programs don't handle -EACCES very well so it should
3827 * be mapped to -EPERM instead.
3829 if (status == -EACCES)
3835 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3836 * @server: initialized nfs_server handle
3837 * @fhandle: we fill in the pseudo-fs root file handle
3838 * @info: we fill in an FSINFO struct
3839 * @auth_probe: probe the auth flavours
3841 * Returns zero on success, or a negative errno.
3843 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3844 struct nfs_fsinfo *info,
3850 status = nfs4_lookup_root(server, fhandle, info);
3852 if (auth_probe || status == NFS4ERR_WRONGSEC)
3853 status = server->nfs_client->cl_mvops->find_root_sec(server,
3857 status = nfs4_server_capabilities(server, fhandle);
3859 status = nfs4_do_fsinfo(server, fhandle, info);
3861 return nfs4_map_errors(status);
3864 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3865 struct nfs_fsinfo *info)
3868 struct nfs_fattr *fattr = info->fattr;
3869 struct nfs4_label *label = NULL;
3871 error = nfs4_server_capabilities(server, mntfh);
3873 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3877 label = nfs4_label_alloc(server, GFP_KERNEL);
3879 return PTR_ERR(label);
3881 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
3883 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3884 goto err_free_label;
3887 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3888 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3889 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3892 nfs4_label_free(label);
3898 * Get locations and (maybe) other attributes of a referral.
3899 * Note that we'll actually follow the referral later when
3900 * we detect fsid mismatch in inode revalidation
3902 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3903 const struct qstr *name, struct nfs_fattr *fattr,
3904 struct nfs_fh *fhandle)
3906 int status = -ENOMEM;
3907 struct page *page = NULL;
3908 struct nfs4_fs_locations *locations = NULL;
3910 page = alloc_page(GFP_KERNEL);
3913 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3914 if (locations == NULL)
3917 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3922 * If the fsid didn't change, this is a migration event, not a
3923 * referral. Cause us to drop into the exception handler, which
3924 * will kick off migration recovery.
3926 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3927 dprintk("%s: server did not return a different fsid for"
3928 " a referral at %s\n", __func__, name->name);
3929 status = -NFS4ERR_MOVED;
3932 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3933 nfs_fixup_referral_attributes(&locations->fattr);
3935 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3936 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3937 memset(fhandle, 0, sizeof(struct nfs_fh));
3945 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3946 struct nfs_fattr *fattr, struct nfs4_label *label,
3947 struct inode *inode)
3949 __u32 bitmask[NFS4_BITMASK_SZ];
3950 struct nfs4_getattr_arg args = {
3954 struct nfs4_getattr_res res = {
3959 struct rpc_message msg = {
3960 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3965 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
3967 nfs_fattr_init(fattr);
3968 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3971 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3972 struct nfs_fattr *fattr, struct nfs4_label *label,
3973 struct inode *inode)
3975 struct nfs4_exception exception = {
3976 .interruptible = true,
3980 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
3981 trace_nfs4_getattr(server, fhandle, fattr, err);
3982 err = nfs4_handle_exception(server, err,
3984 } while (exception.retry);
3989 * The file is not closed if it is opened due to the a request to change
3990 * the size of the file. The open call will not be needed once the
3991 * VFS layer lookup-intents are implemented.
3993 * Close is called when the inode is destroyed.
3994 * If we haven't opened the file for O_WRONLY, we
3995 * need to in the size_change case to obtain a stateid.
3998 * Because OPEN is always done by name in nfsv4, it is
3999 * possible that we opened a different file by the same
4000 * name. We can recognize this race condition, but we
4001 * can't do anything about it besides returning an error.
4003 * This will be fixed with VFS changes (lookup-intent).
4006 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4007 struct iattr *sattr)
4009 struct inode *inode = d_inode(dentry);
4010 const struct cred *cred = NULL;
4011 struct nfs_open_context *ctx = NULL;
4012 struct nfs4_label *label = NULL;
4015 if (pnfs_ld_layoutret_on_setattr(inode) &&
4016 sattr->ia_valid & ATTR_SIZE &&
4017 sattr->ia_size < i_size_read(inode))
4018 pnfs_commit_and_return_layout(inode);
4020 nfs_fattr_init(fattr);
4022 /* Deal with open(O_TRUNC) */
4023 if (sattr->ia_valid & ATTR_OPEN)
4024 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4026 /* Optimization: if the end result is no change, don't RPC */
4027 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4030 /* Search for an existing open(O_WRITE) file */
4031 if (sattr->ia_valid & ATTR_FILE) {
4033 ctx = nfs_file_open_context(sattr->ia_file);
4038 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4040 return PTR_ERR(label);
4042 /* Return any delegations if we're going to change ACLs */
4043 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4044 nfs4_inode_make_writeable(inode);
4046 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4048 nfs_setattr_update_inode(inode, sattr, fattr);
4049 nfs_setsecurity(inode, fattr, label);
4051 nfs4_label_free(label);
4055 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4056 const struct qstr *name, struct nfs_fh *fhandle,
4057 struct nfs_fattr *fattr, struct nfs4_label *label)
4059 struct nfs_server *server = NFS_SERVER(dir);
4061 struct nfs4_lookup_arg args = {
4062 .bitmask = server->attr_bitmask,
4063 .dir_fh = NFS_FH(dir),
4066 struct nfs4_lookup_res res = {
4072 struct rpc_message msg = {
4073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4078 args.bitmask = nfs4_bitmask(server, label);
4080 nfs_fattr_init(fattr);
4082 dprintk("NFS call lookup %s\n", name->name);
4083 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
4084 dprintk("NFS reply lookup: %d\n", status);
4088 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4090 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4091 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4092 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4096 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4097 const struct qstr *name, struct nfs_fh *fhandle,
4098 struct nfs_fattr *fattr, struct nfs4_label *label)
4100 struct nfs4_exception exception = {
4101 .interruptible = true,
4103 struct rpc_clnt *client = *clnt;
4106 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
4107 trace_nfs4_lookup(dir, name, err);
4109 case -NFS4ERR_BADNAME:
4112 case -NFS4ERR_MOVED:
4113 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4114 if (err == -NFS4ERR_MOVED)
4115 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4117 case -NFS4ERR_WRONGSEC:
4119 if (client != *clnt)
4121 client = nfs4_negotiate_security(client, dir, name);
4123 return PTR_ERR(client);
4125 exception.retry = 1;
4128 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4130 } while (exception.retry);
4135 else if (client != *clnt)
4136 rpc_shutdown_client(client);
4141 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
4142 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4143 struct nfs4_label *label)
4146 struct rpc_clnt *client = NFS_CLIENT(dir);
4148 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
4149 if (client != NFS_CLIENT(dir)) {
4150 rpc_shutdown_client(client);
4151 nfs_fixup_secinfo_attributes(fattr);
4157 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
4158 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4160 struct rpc_clnt *client = NFS_CLIENT(dir);
4163 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
4165 return ERR_PTR(status);
4166 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4169 static int _nfs4_proc_lookupp(struct inode *inode,
4170 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4171 struct nfs4_label *label)
4173 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4174 struct nfs_server *server = NFS_SERVER(inode);
4176 struct nfs4_lookupp_arg args = {
4177 .bitmask = server->attr_bitmask,
4178 .fh = NFS_FH(inode),
4180 struct nfs4_lookupp_res res = {
4186 struct rpc_message msg = {
4187 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4192 args.bitmask = nfs4_bitmask(server, label);
4194 nfs_fattr_init(fattr);
4196 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4197 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4199 dprintk("NFS reply lookupp: %d\n", status);
4203 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4204 struct nfs_fattr *fattr, struct nfs4_label *label)
4206 struct nfs4_exception exception = {
4207 .interruptible = true,
4211 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4212 trace_nfs4_lookupp(inode, err);
4213 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4215 } while (exception.retry);
4219 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4221 struct nfs_server *server = NFS_SERVER(inode);
4222 struct nfs4_accessargs args = {
4223 .fh = NFS_FH(inode),
4224 .access = entry->mask,
4226 struct nfs4_accessres res = {
4229 struct rpc_message msg = {
4230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4233 .rpc_cred = entry->cred,
4237 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4238 res.fattr = nfs_alloc_fattr();
4239 if (res.fattr == NULL)
4241 args.bitmask = server->cache_consistency_bitmask;
4243 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4245 nfs_access_set_mask(entry, res.access);
4247 nfs_refresh_inode(inode, res.fattr);
4249 nfs_free_fattr(res.fattr);
4253 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4255 struct nfs4_exception exception = {
4256 .interruptible = true,
4260 err = _nfs4_proc_access(inode, entry);
4261 trace_nfs4_access(inode, err);
4262 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4264 } while (exception.retry);
4269 * TODO: For the time being, we don't try to get any attributes
4270 * along with any of the zero-copy operations READ, READDIR,
4273 * In the case of the first three, we want to put the GETATTR
4274 * after the read-type operation -- this is because it is hard
4275 * to predict the length of a GETATTR response in v4, and thus
4276 * align the READ data correctly. This means that the GETATTR
4277 * may end up partially falling into the page cache, and we should
4278 * shift it into the 'tail' of the xdr_buf before processing.
4279 * To do this efficiently, we need to know the total length
4280 * of data received, which doesn't seem to be available outside
4283 * In the case of WRITE, we also want to put the GETATTR after
4284 * the operation -- in this case because we want to make sure
4285 * we get the post-operation mtime and size.
4287 * Both of these changes to the XDR layer would in fact be quite
4288 * minor, but I decided to leave them for a subsequent patch.
4290 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4291 unsigned int pgbase, unsigned int pglen)
4293 struct nfs4_readlink args = {
4294 .fh = NFS_FH(inode),
4299 struct nfs4_readlink_res res;
4300 struct rpc_message msg = {
4301 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4306 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4309 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4310 unsigned int pgbase, unsigned int pglen)
4312 struct nfs4_exception exception = {
4313 .interruptible = true,
4317 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4318 trace_nfs4_readlink(inode, err);
4319 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4321 } while (exception.retry);
4326 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4329 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4332 struct nfs_server *server = NFS_SERVER(dir);
4333 struct nfs4_label l, *ilabel = NULL;
4334 struct nfs_open_context *ctx;
4335 struct nfs4_state *state;
4338 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4340 return PTR_ERR(ctx);
4342 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4344 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4345 sattr->ia_mode &= ~current_umask();
4346 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4347 if (IS_ERR(state)) {
4348 status = PTR_ERR(state);
4352 nfs4_label_release_security(ilabel);
4353 put_nfs_open_context(ctx);
4358 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4360 struct nfs_server *server = NFS_SERVER(dir);
4361 struct nfs_removeargs args = {
4365 struct nfs_removeres res = {
4368 struct rpc_message msg = {
4369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4373 unsigned long timestamp = jiffies;
4376 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4378 spin_lock(&dir->i_lock);
4379 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
4380 /* Removing a directory decrements nlink in the parent */
4381 if (ftype == NF4DIR && dir->i_nlink > 2)
4382 nfs4_dec_nlink_locked(dir);
4383 spin_unlock(&dir->i_lock);
4388 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4390 struct nfs4_exception exception = {
4391 .interruptible = true,
4393 struct inode *inode = d_inode(dentry);
4397 if (inode->i_nlink == 1)
4398 nfs4_inode_return_delegation(inode);
4400 nfs4_inode_make_writeable(inode);
4403 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4404 trace_nfs4_remove(dir, &dentry->d_name, err);
4405 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4407 } while (exception.retry);
4411 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4413 struct nfs4_exception exception = {
4414 .interruptible = true,
4419 err = _nfs4_proc_remove(dir, name, NF4DIR);
4420 trace_nfs4_remove(dir, name, err);
4421 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4423 } while (exception.retry);
4427 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4428 struct dentry *dentry,
4429 struct inode *inode)
4431 struct nfs_removeargs *args = msg->rpc_argp;
4432 struct nfs_removeres *res = msg->rpc_resp;
4434 res->server = NFS_SB(dentry->d_sb);
4435 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4436 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4438 nfs_fattr_init(res->dir_attr);
4441 nfs4_inode_return_delegation(inode);
4444 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4446 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4447 &data->args.seq_args,
4452 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4454 struct nfs_unlinkdata *data = task->tk_calldata;
4455 struct nfs_removeres *res = &data->res;
4457 if (!nfs4_sequence_done(task, &res->seq_res))
4459 if (nfs4_async_handle_error(task, res->server, NULL,
4460 &data->timeout) == -EAGAIN)
4462 if (task->tk_status == 0)
4463 update_changeattr(dir, &res->cinfo,
4464 res->dir_attr->time_start, 0);
4468 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4469 struct dentry *old_dentry,
4470 struct dentry *new_dentry)
4472 struct nfs_renameargs *arg = msg->rpc_argp;
4473 struct nfs_renameres *res = msg->rpc_resp;
4474 struct inode *old_inode = d_inode(old_dentry);
4475 struct inode *new_inode = d_inode(new_dentry);
4478 nfs4_inode_make_writeable(old_inode);
4480 nfs4_inode_return_delegation(new_inode);
4481 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4482 res->server = NFS_SB(old_dentry->d_sb);
4483 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4486 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4488 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4489 &data->args.seq_args,
4494 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4495 struct inode *new_dir)
4497 struct nfs_renamedata *data = task->tk_calldata;
4498 struct nfs_renameres *res = &data->res;
4500 if (!nfs4_sequence_done(task, &res->seq_res))
4502 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4505 if (task->tk_status == 0) {
4506 if (new_dir != old_dir) {
4507 /* Note: If we moved a directory, nlink will change */
4508 update_changeattr(old_dir, &res->old_cinfo,
4509 res->old_fattr->time_start,
4510 NFS_INO_INVALID_OTHER);
4511 update_changeattr(new_dir, &res->new_cinfo,
4512 res->new_fattr->time_start,
4513 NFS_INO_INVALID_OTHER);
4515 update_changeattr(old_dir, &res->old_cinfo,
4516 res->old_fattr->time_start,
4522 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4524 struct nfs_server *server = NFS_SERVER(inode);
4525 __u32 bitmask[NFS4_BITMASK_SZ];
4526 struct nfs4_link_arg arg = {
4527 .fh = NFS_FH(inode),
4528 .dir_fh = NFS_FH(dir),
4532 struct nfs4_link_res res = {
4536 struct rpc_message msg = {
4537 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4541 int status = -ENOMEM;
4543 res.fattr = nfs_alloc_fattr();
4544 if (res.fattr == NULL)
4547 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4548 if (IS_ERR(res.label)) {
4549 status = PTR_ERR(res.label);
4553 nfs4_inode_make_writeable(inode);
4554 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4556 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4558 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
4559 status = nfs_post_op_update_inode(inode, res.fattr);
4561 nfs_setsecurity(inode, res.fattr, res.label);
4565 nfs4_label_free(res.label);
4568 nfs_free_fattr(res.fattr);
4572 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4574 struct nfs4_exception exception = {
4575 .interruptible = true,
4579 err = nfs4_handle_exception(NFS_SERVER(inode),
4580 _nfs4_proc_link(inode, dir, name),
4582 } while (exception.retry);
4586 struct nfs4_createdata {
4587 struct rpc_message msg;
4588 struct nfs4_create_arg arg;
4589 struct nfs4_create_res res;
4591 struct nfs_fattr fattr;
4592 struct nfs4_label *label;
4595 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4596 const struct qstr *name, struct iattr *sattr, u32 ftype)
4598 struct nfs4_createdata *data;
4600 data = kzalloc(sizeof(*data), GFP_KERNEL);
4602 struct nfs_server *server = NFS_SERVER(dir);
4604 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4605 if (IS_ERR(data->label))
4608 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4609 data->msg.rpc_argp = &data->arg;
4610 data->msg.rpc_resp = &data->res;
4611 data->arg.dir_fh = NFS_FH(dir);
4612 data->arg.server = server;
4613 data->arg.name = name;
4614 data->arg.attrs = sattr;
4615 data->arg.ftype = ftype;
4616 data->arg.bitmask = nfs4_bitmask(server, data->label);
4617 data->arg.umask = current_umask();
4618 data->res.server = server;
4619 data->res.fh = &data->fh;
4620 data->res.fattr = &data->fattr;
4621 data->res.label = data->label;
4622 nfs_fattr_init(data->res.fattr);
4630 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4632 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4633 &data->arg.seq_args, &data->res.seq_res, 1);
4635 spin_lock(&dir->i_lock);
4636 update_changeattr_locked(dir, &data->res.dir_cinfo,
4637 data->res.fattr->time_start, 0);
4638 /* Creating a directory bumps nlink in the parent */
4639 if (data->arg.ftype == NF4DIR)
4640 nfs4_inc_nlink_locked(dir);
4641 spin_unlock(&dir->i_lock);
4642 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4647 static void nfs4_free_createdata(struct nfs4_createdata *data)
4649 nfs4_label_free(data->label);
4653 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4654 struct page *page, unsigned int len, struct iattr *sattr,
4655 struct nfs4_label *label)
4657 struct nfs4_createdata *data;
4658 int status = -ENAMETOOLONG;
4660 if (len > NFS4_MAXPATHLEN)
4664 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4668 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4669 data->arg.u.symlink.pages = &page;
4670 data->arg.u.symlink.len = len;
4671 data->arg.label = label;
4673 status = nfs4_do_create(dir, dentry, data);
4675 nfs4_free_createdata(data);
4680 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4681 struct page *page, unsigned int len, struct iattr *sattr)
4683 struct nfs4_exception exception = {
4684 .interruptible = true,
4686 struct nfs4_label l, *label = NULL;
4689 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4692 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4693 trace_nfs4_symlink(dir, &dentry->d_name, err);
4694 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4696 } while (exception.retry);
4698 nfs4_label_release_security(label);
4702 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4703 struct iattr *sattr, struct nfs4_label *label)
4705 struct nfs4_createdata *data;
4706 int status = -ENOMEM;
4708 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4712 data->arg.label = label;
4713 status = nfs4_do_create(dir, dentry, data);
4715 nfs4_free_createdata(data);
4720 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4721 struct iattr *sattr)
4723 struct nfs_server *server = NFS_SERVER(dir);
4724 struct nfs4_exception exception = {
4725 .interruptible = true,
4727 struct nfs4_label l, *label = NULL;
4730 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4732 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4733 sattr->ia_mode &= ~current_umask();
4735 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4736 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4737 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4739 } while (exception.retry);
4740 nfs4_label_release_security(label);
4745 static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4746 u64 cookie, struct page **pages, unsigned int count, bool plus)
4748 struct inode *dir = d_inode(dentry);
4749 struct nfs4_readdir_arg args = {
4754 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4757 struct nfs4_readdir_res res;
4758 struct rpc_message msg = {
4759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4766 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4768 (unsigned long long)cookie);
4769 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4770 res.pgbase = args.pgbase;
4771 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4773 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4774 status += args.pgbase;
4777 nfs_invalidate_atime(dir);
4779 dprintk("%s: returns %d\n", __func__, status);
4783 static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4784 u64 cookie, struct page **pages, unsigned int count, bool plus)
4786 struct nfs4_exception exception = {
4787 .interruptible = true,
4791 err = _nfs4_proc_readdir(dentry, cred, cookie,
4792 pages, count, plus);
4793 trace_nfs4_readdir(d_inode(dentry), err);
4794 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4796 } while (exception.retry);
4800 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4801 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4803 struct nfs4_createdata *data;
4804 int mode = sattr->ia_mode;
4805 int status = -ENOMEM;
4807 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4812 data->arg.ftype = NF4FIFO;
4813 else if (S_ISBLK(mode)) {
4814 data->arg.ftype = NF4BLK;
4815 data->arg.u.device.specdata1 = MAJOR(rdev);
4816 data->arg.u.device.specdata2 = MINOR(rdev);
4818 else if (S_ISCHR(mode)) {
4819 data->arg.ftype = NF4CHR;
4820 data->arg.u.device.specdata1 = MAJOR(rdev);
4821 data->arg.u.device.specdata2 = MINOR(rdev);
4822 } else if (!S_ISSOCK(mode)) {
4827 data->arg.label = label;
4828 status = nfs4_do_create(dir, dentry, data);
4830 nfs4_free_createdata(data);
4835 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4836 struct iattr *sattr, dev_t rdev)
4838 struct nfs_server *server = NFS_SERVER(dir);
4839 struct nfs4_exception exception = {
4840 .interruptible = true,
4842 struct nfs4_label l, *label = NULL;
4845 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4847 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4848 sattr->ia_mode &= ~current_umask();
4850 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4851 trace_nfs4_mknod(dir, &dentry->d_name, err);
4852 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4854 } while (exception.retry);
4856 nfs4_label_release_security(label);
4861 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4862 struct nfs_fsstat *fsstat)
4864 struct nfs4_statfs_arg args = {
4866 .bitmask = server->attr_bitmask,
4868 struct nfs4_statfs_res res = {
4871 struct rpc_message msg = {
4872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4877 nfs_fattr_init(fsstat->fattr);
4878 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4881 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4883 struct nfs4_exception exception = {
4884 .interruptible = true,
4888 err = nfs4_handle_exception(server,
4889 _nfs4_proc_statfs(server, fhandle, fsstat),
4891 } while (exception.retry);
4895 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4896 struct nfs_fsinfo *fsinfo)
4898 struct nfs4_fsinfo_arg args = {
4900 .bitmask = server->attr_bitmask,
4902 struct nfs4_fsinfo_res res = {
4905 struct rpc_message msg = {
4906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4911 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4914 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4916 struct nfs4_exception exception = {
4917 .interruptible = true,
4919 unsigned long now = jiffies;
4923 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4924 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4926 nfs4_set_lease_period(server->nfs_client,
4927 fsinfo->lease_time * HZ,
4931 err = nfs4_handle_exception(server, err, &exception);
4932 } while (exception.retry);
4936 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4940 nfs_fattr_init(fsinfo->fattr);
4941 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4943 /* block layout checks this! */
4944 server->pnfs_blksize = fsinfo->blksize;
4945 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4951 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4952 struct nfs_pathconf *pathconf)
4954 struct nfs4_pathconf_arg args = {
4956 .bitmask = server->attr_bitmask,
4958 struct nfs4_pathconf_res res = {
4959 .pathconf = pathconf,
4961 struct rpc_message msg = {
4962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4967 /* None of the pathconf attributes are mandatory to implement */
4968 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4969 memset(pathconf, 0, sizeof(*pathconf));
4973 nfs_fattr_init(pathconf->fattr);
4974 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4977 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4978 struct nfs_pathconf *pathconf)
4980 struct nfs4_exception exception = {
4981 .interruptible = true,
4986 err = nfs4_handle_exception(server,
4987 _nfs4_proc_pathconf(server, fhandle, pathconf),
4989 } while (exception.retry);
4993 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4994 const struct nfs_open_context *ctx,
4995 const struct nfs_lock_context *l_ctx,
4998 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5000 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5002 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5003 const struct nfs_open_context *ctx,
5004 const struct nfs_lock_context *l_ctx,
5007 nfs4_stateid current_stateid;
5009 /* If the current stateid represents a lost lock, then exit */
5010 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
5012 return nfs4_stateid_match(stateid, ¤t_stateid);
5015 static bool nfs4_error_stateid_expired(int err)
5018 case -NFS4ERR_DELEG_REVOKED:
5019 case -NFS4ERR_ADMIN_REVOKED:
5020 case -NFS4ERR_BAD_STATEID:
5021 case -NFS4ERR_STALE_STATEID:
5022 case -NFS4ERR_OLD_STATEID:
5023 case -NFS4ERR_OPENMODE:
5024 case -NFS4ERR_EXPIRED:
5030 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5032 struct nfs_server *server = NFS_SERVER(hdr->inode);
5034 trace_nfs4_read(hdr, task->tk_status);
5035 if (task->tk_status < 0) {
5036 struct nfs4_exception exception = {
5037 .inode = hdr->inode,
5038 .state = hdr->args.context->state,
5039 .stateid = &hdr->args.stateid,
5041 task->tk_status = nfs4_async_handle_exception(task,
5042 server, task->tk_status, &exception);
5043 if (exception.retry) {
5044 rpc_restart_call_prepare(task);
5049 if (task->tk_status > 0)
5050 renew_lease(server, hdr->timestamp);
5054 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5055 struct nfs_pgio_args *args)
5058 if (!nfs4_error_stateid_expired(task->tk_status) ||
5059 nfs4_stateid_is_current(&args->stateid,
5064 rpc_restart_call_prepare(task);
5068 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5071 dprintk("--> %s\n", __func__);
5073 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5075 if (nfs4_read_stateid_changed(task, &hdr->args))
5077 if (task->tk_status > 0)
5078 nfs_invalidate_atime(hdr->inode);
5079 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5080 nfs4_read_done_cb(task, hdr);
5083 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5084 struct rpc_message *msg)
5086 hdr->timestamp = jiffies;
5087 if (!hdr->pgio_done_cb)
5088 hdr->pgio_done_cb = nfs4_read_done_cb;
5089 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5090 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5093 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5094 struct nfs_pgio_header *hdr)
5096 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5097 &hdr->args.seq_args,
5101 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5102 hdr->args.lock_context,
5103 hdr->rw_mode) == -EIO)
5105 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5110 static int nfs4_write_done_cb(struct rpc_task *task,
5111 struct nfs_pgio_header *hdr)
5113 struct inode *inode = hdr->inode;
5115 trace_nfs4_write(hdr, task->tk_status);
5116 if (task->tk_status < 0) {
5117 struct nfs4_exception exception = {
5118 .inode = hdr->inode,
5119 .state = hdr->args.context->state,
5120 .stateid = &hdr->args.stateid,
5122 task->tk_status = nfs4_async_handle_exception(task,
5123 NFS_SERVER(inode), task->tk_status,
5125 if (exception.retry) {
5126 rpc_restart_call_prepare(task);
5130 if (task->tk_status >= 0) {
5131 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5132 nfs_writeback_update_inode(hdr);
5137 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5138 struct nfs_pgio_args *args)
5141 if (!nfs4_error_stateid_expired(task->tk_status) ||
5142 nfs4_stateid_is_current(&args->stateid,
5147 rpc_restart_call_prepare(task);
5151 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5153 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5155 if (nfs4_write_stateid_changed(task, &hdr->args))
5157 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5158 nfs4_write_done_cb(task, hdr);
5162 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5164 /* Don't request attributes for pNFS or O_DIRECT writes */
5165 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5167 /* Otherwise, request attributes if and only if we don't hold
5170 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5173 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5174 struct rpc_message *msg,
5175 struct rpc_clnt **clnt)
5177 struct nfs_server *server = NFS_SERVER(hdr->inode);
5179 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5180 hdr->args.bitmask = NULL;
5181 hdr->res.fattr = NULL;
5183 hdr->args.bitmask = server->cache_consistency_bitmask;
5185 if (!hdr->pgio_done_cb)
5186 hdr->pgio_done_cb = nfs4_write_done_cb;
5187 hdr->res.server = server;
5188 hdr->timestamp = jiffies;
5190 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5191 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
5192 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5195 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5197 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5198 &data->args.seq_args,
5203 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5205 struct inode *inode = data->inode;
5207 trace_nfs4_commit(data, task->tk_status);
5208 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5209 NULL, NULL) == -EAGAIN) {
5210 rpc_restart_call_prepare(task);
5216 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5218 if (!nfs4_sequence_done(task, &data->res.seq_res))
5220 return data->commit_done_cb(task, data);
5223 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5224 struct rpc_clnt **clnt)
5226 struct nfs_server *server = NFS_SERVER(data->inode);
5228 if (data->commit_done_cb == NULL)
5229 data->commit_done_cb = nfs4_commit_done_cb;
5230 data->res.server = server;
5231 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5232 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5233 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5236 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5237 struct nfs_commitres *res)
5239 struct inode *dst_inode = file_inode(dst);
5240 struct nfs_server *server = NFS_SERVER(dst_inode);
5241 struct rpc_message msg = {
5242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5247 args->fh = NFS_FH(dst_inode);
5248 return nfs4_call_sync(server->client, server, &msg,
5249 &args->seq_args, &res->seq_res, 1);
5252 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5254 struct nfs_commitargs args = {
5258 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5259 struct nfs4_exception exception = { };
5263 status = _nfs4_proc_commit(dst, &args, res);
5264 status = nfs4_handle_exception(dst_server, status, &exception);
5265 } while (exception.retry);
5270 struct nfs4_renewdata {
5271 struct nfs_client *client;
5272 unsigned long timestamp;
5276 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5277 * standalone procedure for queueing an asynchronous RENEW.
5279 static void nfs4_renew_release(void *calldata)
5281 struct nfs4_renewdata *data = calldata;
5282 struct nfs_client *clp = data->client;
5284 if (refcount_read(&clp->cl_count) > 1)
5285 nfs4_schedule_state_renewal(clp);
5286 nfs_put_client(clp);
5290 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5292 struct nfs4_renewdata *data = calldata;
5293 struct nfs_client *clp = data->client;
5294 unsigned long timestamp = data->timestamp;
5296 trace_nfs4_renew_async(clp, task->tk_status);
5297 switch (task->tk_status) {
5300 case -NFS4ERR_LEASE_MOVED:
5301 nfs4_schedule_lease_moved_recovery(clp);
5304 /* Unless we're shutting down, schedule state recovery! */
5305 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5307 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5308 nfs4_schedule_lease_recovery(clp);
5311 nfs4_schedule_path_down_recovery(clp);
5313 do_renew_lease(clp, timestamp);
5316 static const struct rpc_call_ops nfs4_renew_ops = {
5317 .rpc_call_done = nfs4_renew_done,
5318 .rpc_release = nfs4_renew_release,
5321 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5323 struct rpc_message msg = {
5324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5328 struct nfs4_renewdata *data;
5330 if (renew_flags == 0)
5332 if (!refcount_inc_not_zero(&clp->cl_count))
5334 data = kmalloc(sizeof(*data), GFP_NOFS);
5336 nfs_put_client(clp);
5340 data->timestamp = jiffies;
5341 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5342 &nfs4_renew_ops, data);
5345 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5347 struct rpc_message msg = {
5348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5352 unsigned long now = jiffies;
5355 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5358 do_renew_lease(clp, now);
5362 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5364 return server->caps & NFS_CAP_ACLS;
5367 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5368 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5371 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5373 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5374 struct page **pages)
5376 struct page *newpage, **spages;
5382 len = min_t(size_t, PAGE_SIZE, buflen);
5383 newpage = alloc_page(GFP_KERNEL);
5385 if (newpage == NULL)
5387 memcpy(page_address(newpage), buf, len);
5392 } while (buflen != 0);
5398 __free_page(spages[rc-1]);
5402 struct nfs4_cached_acl {
5408 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5410 struct nfs_inode *nfsi = NFS_I(inode);
5412 spin_lock(&inode->i_lock);
5413 kfree(nfsi->nfs4_acl);
5414 nfsi->nfs4_acl = acl;
5415 spin_unlock(&inode->i_lock);
5418 static void nfs4_zap_acl_attr(struct inode *inode)
5420 nfs4_set_cached_acl(inode, NULL);
5423 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5425 struct nfs_inode *nfsi = NFS_I(inode);
5426 struct nfs4_cached_acl *acl;
5429 spin_lock(&inode->i_lock);
5430 acl = nfsi->nfs4_acl;
5433 if (buf == NULL) /* user is just asking for length */
5435 if (acl->cached == 0)
5437 ret = -ERANGE; /* see getxattr(2) man page */
5438 if (acl->len > buflen)
5440 memcpy(buf, acl->data, acl->len);
5444 spin_unlock(&inode->i_lock);
5448 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5450 struct nfs4_cached_acl *acl;
5451 size_t buflen = sizeof(*acl) + acl_len;
5453 if (buflen <= PAGE_SIZE) {
5454 acl = kmalloc(buflen, GFP_KERNEL);
5458 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5460 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5467 nfs4_set_cached_acl(inode, acl);
5471 * The getxattr API returns the required buffer length when called with a
5472 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5473 * the required buf. On a NULL buf, we send a page of data to the server
5474 * guessing that the ACL request can be serviced by a page. If so, we cache
5475 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5476 * the cache. If not so, we throw away the page, and cache the required
5477 * length. The next getxattr call will then produce another round trip to
5478 * the server, this time with the input buf of the required size.
5480 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5482 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5483 struct nfs_getaclargs args = {
5484 .fh = NFS_FH(inode),
5488 struct nfs_getaclres res = {
5491 struct rpc_message msg = {
5492 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5496 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5497 int ret = -ENOMEM, i;
5499 if (npages > ARRAY_SIZE(pages))
5502 for (i = 0; i < npages; i++) {
5503 pages[i] = alloc_page(GFP_KERNEL);
5508 /* for decoding across pages */
5509 res.acl_scratch = alloc_page(GFP_KERNEL);
5510 if (!res.acl_scratch)
5513 args.acl_len = npages * PAGE_SIZE;
5515 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5516 __func__, buf, buflen, npages, args.acl_len);
5517 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5518 &msg, &args.seq_args, &res.seq_res, 0);
5522 /* Handle the case where the passed-in buffer is too short */
5523 if (res.acl_flags & NFS4_ACL_TRUNC) {
5524 /* Did the user only issue a request for the acl length? */
5530 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5532 if (res.acl_len > buflen) {
5536 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5541 for (i = 0; i < npages; i++)
5543 __free_page(pages[i]);
5544 if (res.acl_scratch)
5545 __free_page(res.acl_scratch);
5549 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5551 struct nfs4_exception exception = {
5552 .interruptible = true,
5556 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5557 trace_nfs4_get_acl(inode, ret);
5560 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5561 } while (exception.retry);
5565 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5567 struct nfs_server *server = NFS_SERVER(inode);
5570 if (!nfs4_server_supports_acls(server))
5572 ret = nfs_revalidate_inode(server, inode);
5575 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5576 nfs_zap_acl_cache(inode);
5577 ret = nfs4_read_cached_acl(inode, buf, buflen);
5579 /* -ENOENT is returned if there is no ACL or if there is an ACL
5580 * but no cached acl data, just the acl length */
5582 return nfs4_get_acl_uncached(inode, buf, buflen);
5585 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5587 struct nfs_server *server = NFS_SERVER(inode);
5588 struct page *pages[NFS4ACL_MAXPAGES];
5589 struct nfs_setaclargs arg = {
5590 .fh = NFS_FH(inode),
5594 struct nfs_setaclres res;
5595 struct rpc_message msg = {
5596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5600 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5603 if (!nfs4_server_supports_acls(server))
5605 if (npages > ARRAY_SIZE(pages))
5607 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5610 nfs4_inode_make_writeable(inode);
5611 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5614 * Free each page after tx, so the only ref left is
5615 * held by the network stack
5618 put_page(pages[i-1]);
5621 * Acl update can result in inode attribute update.
5622 * so mark the attribute cache invalid.
5624 spin_lock(&inode->i_lock);
5625 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5626 | NFS_INO_INVALID_CTIME
5627 | NFS_INO_REVAL_FORCED;
5628 spin_unlock(&inode->i_lock);
5629 nfs_access_zap_cache(inode);
5630 nfs_zap_acl_cache(inode);
5634 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5636 struct nfs4_exception exception = { };
5639 err = __nfs4_proc_set_acl(inode, buf, buflen);
5640 trace_nfs4_set_acl(inode, err);
5641 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5643 } while (exception.retry);
5647 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5648 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5651 struct nfs_server *server = NFS_SERVER(inode);
5652 struct nfs_fattr fattr;
5653 struct nfs4_label label = {0, 0, buflen, buf};
5655 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5656 struct nfs4_getattr_arg arg = {
5657 .fh = NFS_FH(inode),
5660 struct nfs4_getattr_res res = {
5665 struct rpc_message msg = {
5666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5672 nfs_fattr_init(&fattr);
5674 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5677 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5679 if (buflen < label.len)
5684 static int nfs4_get_security_label(struct inode *inode, void *buf,
5687 struct nfs4_exception exception = {
5688 .interruptible = true,
5692 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5696 err = _nfs4_get_security_label(inode, buf, buflen);
5697 trace_nfs4_get_security_label(inode, err);
5698 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5700 } while (exception.retry);
5704 static int _nfs4_do_set_security_label(struct inode *inode,
5705 struct nfs4_label *ilabel,
5706 struct nfs_fattr *fattr,
5707 struct nfs4_label *olabel)
5710 struct iattr sattr = {0};
5711 struct nfs_server *server = NFS_SERVER(inode);
5712 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5713 struct nfs_setattrargs arg = {
5714 .fh = NFS_FH(inode),
5720 struct nfs_setattrres res = {
5725 struct rpc_message msg = {
5726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5732 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5734 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5736 dprintk("%s failed: %d\n", __func__, status);
5741 static int nfs4_do_set_security_label(struct inode *inode,
5742 struct nfs4_label *ilabel,
5743 struct nfs_fattr *fattr,
5744 struct nfs4_label *olabel)
5746 struct nfs4_exception exception = { };
5750 err = _nfs4_do_set_security_label(inode, ilabel,
5752 trace_nfs4_set_security_label(inode, err);
5753 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5755 } while (exception.retry);
5760 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5762 struct nfs4_label ilabel, *olabel = NULL;
5763 struct nfs_fattr fattr;
5766 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5769 nfs_fattr_init(&fattr);
5773 ilabel.label = (char *)buf;
5774 ilabel.len = buflen;
5776 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5777 if (IS_ERR(olabel)) {
5778 status = -PTR_ERR(olabel);
5782 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5784 nfs_setsecurity(inode, &fattr, olabel);
5786 nfs4_label_free(olabel);
5790 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5793 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5794 nfs4_verifier *bootverf)
5798 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5799 /* An impossible timestamp guarantees this value
5800 * will never match a generated boot time. */
5801 verf[0] = cpu_to_be32(U32_MAX);
5802 verf[1] = cpu_to_be32(U32_MAX);
5804 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5805 u64 ns = ktime_to_ns(nn->boot_time);
5807 verf[0] = cpu_to_be32(ns >> 32);
5808 verf[1] = cpu_to_be32(ns);
5810 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5814 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5819 if (clp->cl_owner_id != NULL)
5824 strlen(clp->cl_rpcclient->cl_nodename) +
5826 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5829 if (nfs4_client_id_uniquifier[0] != '\0')
5830 len += strlen(nfs4_client_id_uniquifier) + 1;
5831 if (len > NFS4_OPAQUE_LIMIT + 1)
5835 * Since this string is allocated at mount time, and held until the
5836 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5837 * about a memory-reclaim deadlock.
5839 str = kmalloc(len, GFP_KERNEL);
5844 if (nfs4_client_id_uniquifier[0] != '\0')
5845 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
5846 clp->cl_rpcclient->cl_nodename,
5847 nfs4_client_id_uniquifier,
5848 rpc_peeraddr2str(clp->cl_rpcclient,
5851 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
5852 clp->cl_rpcclient->cl_nodename,
5853 rpc_peeraddr2str(clp->cl_rpcclient,
5857 clp->cl_owner_id = str;
5862 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5867 len = 10 + 10 + 1 + 10 + 1 +
5868 strlen(nfs4_client_id_uniquifier) + 1 +
5869 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5871 if (len > NFS4_OPAQUE_LIMIT + 1)
5875 * Since this string is allocated at mount time, and held until the
5876 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5877 * about a memory-reclaim deadlock.
5879 str = kmalloc(len, GFP_KERNEL);
5883 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5884 clp->rpc_ops->version, clp->cl_minorversion,
5885 nfs4_client_id_uniquifier,
5886 clp->cl_rpcclient->cl_nodename);
5887 clp->cl_owner_id = str;
5892 nfs4_init_uniform_client_string(struct nfs_client *clp)
5897 if (clp->cl_owner_id != NULL)
5900 if (nfs4_client_id_uniquifier[0] != '\0')
5901 return nfs4_init_uniquifier_client_string(clp);
5903 len = 10 + 10 + 1 + 10 + 1 +
5904 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5906 if (len > NFS4_OPAQUE_LIMIT + 1)
5910 * Since this string is allocated at mount time, and held until the
5911 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5912 * about a memory-reclaim deadlock.
5914 str = kmalloc(len, GFP_KERNEL);
5918 scnprintf(str, len, "Linux NFSv%u.%u %s",
5919 clp->rpc_ops->version, clp->cl_minorversion,
5920 clp->cl_rpcclient->cl_nodename);
5921 clp->cl_owner_id = str;
5926 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5927 * services. Advertise one based on the address family of the
5931 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5933 if (strchr(clp->cl_ipaddr, ':') != NULL)
5934 return scnprintf(buf, len, "tcp6");
5936 return scnprintf(buf, len, "tcp");
5939 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5941 struct nfs4_setclientid *sc = calldata;
5943 if (task->tk_status == 0)
5944 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5947 static const struct rpc_call_ops nfs4_setclientid_ops = {
5948 .rpc_call_done = nfs4_setclientid_done,
5952 * nfs4_proc_setclientid - Negotiate client ID
5953 * @clp: state data structure
5954 * @program: RPC program for NFSv4 callback service
5955 * @port: IP port number for NFS4 callback service
5956 * @cred: credential to use for this call
5957 * @res: where to place the result
5959 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5961 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5962 unsigned short port, const struct cred *cred,
5963 struct nfs4_setclientid_res *res)
5965 nfs4_verifier sc_verifier;
5966 struct nfs4_setclientid setclientid = {
5967 .sc_verifier = &sc_verifier,
5971 struct rpc_message msg = {
5972 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5973 .rpc_argp = &setclientid,
5977 struct rpc_task *task;
5978 struct rpc_task_setup task_setup_data = {
5979 .rpc_client = clp->cl_rpcclient,
5980 .rpc_message = &msg,
5981 .callback_ops = &nfs4_setclientid_ops,
5982 .callback_data = &setclientid,
5983 .flags = RPC_TASK_TIMEOUT,
5987 /* nfs_client_id4 */
5988 nfs4_init_boot_verifier(clp, &sc_verifier);
5990 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5991 status = nfs4_init_uniform_client_string(clp);
5993 status = nfs4_init_nonuniform_client_string(clp);
5999 setclientid.sc_netid_len =
6000 nfs4_init_callback_netid(clp,
6001 setclientid.sc_netid,
6002 sizeof(setclientid.sc_netid));
6003 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6004 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6005 clp->cl_ipaddr, port >> 8, port & 255);
6007 dprintk("NFS call setclientid auth=%s, '%s'\n",
6008 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6010 task = rpc_run_task(&task_setup_data);
6012 status = PTR_ERR(task);
6015 status = task->tk_status;
6016 if (setclientid.sc_cred) {
6017 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6018 put_rpccred(setclientid.sc_cred);
6022 trace_nfs4_setclientid(clp, status);
6023 dprintk("NFS reply setclientid: %d\n", status);
6028 * nfs4_proc_setclientid_confirm - Confirm client ID
6029 * @clp: state data structure
6030 * @arg: result of a previous SETCLIENTID
6031 * @cred: credential to use for this call
6033 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6035 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6036 struct nfs4_setclientid_res *arg,
6037 const struct cred *cred)
6039 struct rpc_message msg = {
6040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6046 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6047 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6049 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6050 trace_nfs4_setclientid_confirm(clp, status);
6051 dprintk("NFS reply setclientid_confirm: %d\n", status);
6055 struct nfs4_delegreturndata {
6056 struct nfs4_delegreturnargs args;
6057 struct nfs4_delegreturnres res;
6059 nfs4_stateid stateid;
6060 unsigned long timestamp;
6062 struct nfs4_layoutreturn_args arg;
6063 struct nfs4_layoutreturn_res res;
6064 struct nfs4_xdr_opaque_data ld_private;
6068 struct nfs_fattr fattr;
6070 struct inode *inode;
6073 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6075 struct nfs4_delegreturndata *data = calldata;
6076 struct nfs4_exception exception = {
6077 .inode = data->inode,
6078 .stateid = &data->stateid,
6081 if (!nfs4_sequence_done(task, &data->res.seq_res))
6084 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6086 /* Handle Layoutreturn errors */
6087 if (data->args.lr_args && task->tk_status != 0) {
6088 switch(data->res.lr_ret) {
6090 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6093 data->args.lr_args = NULL;
6094 data->res.lr_res = NULL;
6096 case -NFS4ERR_OLD_STATEID:
6097 if (nfs4_layoutreturn_refresh_stateid(&data->args.lr_args->stateid,
6098 &data->args.lr_args->range,
6102 case -NFS4ERR_ADMIN_REVOKED:
6103 case -NFS4ERR_DELEG_REVOKED:
6104 case -NFS4ERR_EXPIRED:
6105 case -NFS4ERR_BAD_STATEID:
6106 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
6107 case -NFS4ERR_WRONG_CRED:
6108 data->args.lr_args = NULL;
6109 data->res.lr_res = NULL;
6114 switch (task->tk_status) {
6116 renew_lease(data->res.server, data->timestamp);
6118 case -NFS4ERR_ADMIN_REVOKED:
6119 case -NFS4ERR_DELEG_REVOKED:
6120 case -NFS4ERR_EXPIRED:
6121 nfs4_free_revoked_stateid(data->res.server,
6123 task->tk_msg.rpc_cred);
6125 case -NFS4ERR_BAD_STATEID:
6126 case -NFS4ERR_STALE_STATEID:
6127 task->tk_status = 0;
6129 case -NFS4ERR_OLD_STATEID:
6130 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6132 task->tk_status = 0;
6134 case -NFS4ERR_ACCESS:
6135 if (data->args.bitmask) {
6136 data->args.bitmask = NULL;
6137 data->res.fattr = NULL;
6142 task->tk_status = nfs4_async_handle_exception(task,
6143 data->res.server, task->tk_status,
6145 if (exception.retry)
6148 data->rpc_status = task->tk_status;
6151 data->res.lr_ret = 0;
6153 task->tk_status = 0;
6154 rpc_restart_call_prepare(task);
6157 static void nfs4_delegreturn_release(void *calldata)
6159 struct nfs4_delegreturndata *data = calldata;
6160 struct inode *inode = data->inode;
6164 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6166 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6167 nfs_iput_and_deactive(inode);
6172 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6174 struct nfs4_delegreturndata *d_data;
6175 struct pnfs_layout_hdr *lo;
6177 d_data = (struct nfs4_delegreturndata *)data;
6179 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
6182 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6183 if (lo && !pnfs_layout_is_valid(lo)) {
6184 d_data->args.lr_args = NULL;
6185 d_data->res.lr_res = NULL;
6188 nfs4_setup_sequence(d_data->res.server->nfs_client,
6189 &d_data->args.seq_args,
6190 &d_data->res.seq_res,
6194 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6195 .rpc_call_prepare = nfs4_delegreturn_prepare,
6196 .rpc_call_done = nfs4_delegreturn_done,
6197 .rpc_release = nfs4_delegreturn_release,
6200 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6202 struct nfs4_delegreturndata *data;
6203 struct nfs_server *server = NFS_SERVER(inode);
6204 struct rpc_task *task;
6205 struct rpc_message msg = {
6206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6209 struct rpc_task_setup task_setup_data = {
6210 .rpc_client = server->client,
6211 .rpc_message = &msg,
6212 .callback_ops = &nfs4_delegreturn_ops,
6213 .flags = RPC_TASK_ASYNC,
6217 data = kzalloc(sizeof(*data), GFP_NOFS);
6220 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6222 nfs4_state_protect(server->nfs_client,
6223 NFS_SP4_MACH_CRED_CLEANUP,
6224 &task_setup_data.rpc_client, &msg);
6226 data->args.fhandle = &data->fh;
6227 data->args.stateid = &data->stateid;
6228 data->args.bitmask = server->cache_consistency_bitmask;
6229 nfs_copy_fh(&data->fh, NFS_FH(inode));
6230 nfs4_stateid_copy(&data->stateid, stateid);
6231 data->res.fattr = &data->fattr;
6232 data->res.server = server;
6233 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6234 data->lr.arg.ld_private = &data->lr.ld_private;
6235 nfs_fattr_init(data->res.fattr);
6236 data->timestamp = jiffies;
6237 data->rpc_status = 0;
6238 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
6239 data->inode = nfs_igrab_and_active(inode);
6242 data->args.lr_args = &data->lr.arg;
6243 data->res.lr_res = &data->lr.res;
6245 } else if (data->lr.roc) {
6246 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6247 data->lr.roc = false;
6250 task_setup_data.callback_data = data;
6251 msg.rpc_argp = &data->args;
6252 msg.rpc_resp = &data->res;
6253 task = rpc_run_task(&task_setup_data);
6255 return PTR_ERR(task);
6258 status = rpc_wait_for_completion_task(task);
6261 status = data->rpc_status;
6267 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6269 struct nfs_server *server = NFS_SERVER(inode);
6270 struct nfs4_exception exception = { };
6273 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6274 trace_nfs4_delegreturn(inode, stateid, err);
6276 case -NFS4ERR_STALE_STATEID:
6277 case -NFS4ERR_EXPIRED:
6281 err = nfs4_handle_exception(server, err, &exception);
6282 } while (exception.retry);
6286 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6288 struct inode *inode = state->inode;
6289 struct nfs_server *server = NFS_SERVER(inode);
6290 struct nfs_client *clp = server->nfs_client;
6291 struct nfs_lockt_args arg = {
6292 .fh = NFS_FH(inode),
6295 struct nfs_lockt_res res = {
6298 struct rpc_message msg = {
6299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6302 .rpc_cred = state->owner->so_cred,
6304 struct nfs4_lock_state *lsp;
6307 arg.lock_owner.clientid = clp->cl_clientid;
6308 status = nfs4_set_lock_state(state, request);
6311 lsp = request->fl_u.nfs4_fl.owner;
6312 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6313 arg.lock_owner.s_dev = server->s_dev;
6314 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6317 request->fl_type = F_UNLCK;
6319 case -NFS4ERR_DENIED:
6322 request->fl_ops->fl_release_private(request);
6323 request->fl_ops = NULL;
6328 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6330 struct nfs4_exception exception = {
6331 .interruptible = true,
6336 err = _nfs4_proc_getlk(state, cmd, request);
6337 trace_nfs4_get_lock(request, state, cmd, err);
6338 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6340 } while (exception.retry);
6344 struct nfs4_unlockdata {
6345 struct nfs_locku_args arg;
6346 struct nfs_locku_res res;
6347 struct nfs4_lock_state *lsp;
6348 struct nfs_open_context *ctx;
6349 struct nfs_lock_context *l_ctx;
6350 struct file_lock fl;
6351 struct nfs_server *server;
6352 unsigned long timestamp;
6355 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6356 struct nfs_open_context *ctx,
6357 struct nfs4_lock_state *lsp,
6358 struct nfs_seqid *seqid)
6360 struct nfs4_unlockdata *p;
6361 struct inode *inode = lsp->ls_state->inode;
6363 p = kzalloc(sizeof(*p), GFP_NOFS);
6366 p->arg.fh = NFS_FH(inode);
6368 p->arg.seqid = seqid;
6369 p->res.seqid = seqid;
6371 /* Ensure we don't close file until we're done freeing locks! */
6372 p->ctx = get_nfs_open_context(ctx);
6373 p->l_ctx = nfs_get_lock_context(ctx);
6374 locks_init_lock(&p->fl);
6375 locks_copy_lock(&p->fl, fl);
6376 p->server = NFS_SERVER(inode);
6380 static void nfs4_locku_release_calldata(void *data)
6382 struct nfs4_unlockdata *calldata = data;
6383 nfs_free_seqid(calldata->arg.seqid);
6384 nfs4_put_lock_state(calldata->lsp);
6385 nfs_put_lock_context(calldata->l_ctx);
6386 put_nfs_open_context(calldata->ctx);
6390 static void nfs4_locku_done(struct rpc_task *task, void *data)
6392 struct nfs4_unlockdata *calldata = data;
6393 struct nfs4_exception exception = {
6394 .inode = calldata->lsp->ls_state->inode,
6395 .stateid = &calldata->arg.stateid,
6398 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6400 switch (task->tk_status) {
6402 renew_lease(calldata->server, calldata->timestamp);
6403 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6404 if (nfs4_update_lock_stateid(calldata->lsp,
6405 &calldata->res.stateid))
6408 case -NFS4ERR_ADMIN_REVOKED:
6409 case -NFS4ERR_EXPIRED:
6410 nfs4_free_revoked_stateid(calldata->server,
6411 &calldata->arg.stateid,
6412 task->tk_msg.rpc_cred);
6414 case -NFS4ERR_BAD_STATEID:
6415 case -NFS4ERR_OLD_STATEID:
6416 case -NFS4ERR_STALE_STATEID:
6417 if (!nfs4_stateid_match(&calldata->arg.stateid,
6418 &calldata->lsp->ls_stateid))
6419 rpc_restart_call_prepare(task);
6422 task->tk_status = nfs4_async_handle_exception(task,
6423 calldata->server, task->tk_status,
6425 if (exception.retry)
6426 rpc_restart_call_prepare(task);
6428 nfs_release_seqid(calldata->arg.seqid);
6431 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6433 struct nfs4_unlockdata *calldata = data;
6435 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6436 nfs_async_iocounter_wait(task, calldata->l_ctx))
6439 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6441 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
6442 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6443 /* Note: exit _without_ running nfs4_locku_done */
6446 calldata->timestamp = jiffies;
6447 if (nfs4_setup_sequence(calldata->server->nfs_client,
6448 &calldata->arg.seq_args,
6449 &calldata->res.seq_res,
6451 nfs_release_seqid(calldata->arg.seqid);
6454 task->tk_action = NULL;
6456 nfs4_sequence_done(task, &calldata->res.seq_res);
6459 static const struct rpc_call_ops nfs4_locku_ops = {
6460 .rpc_call_prepare = nfs4_locku_prepare,
6461 .rpc_call_done = nfs4_locku_done,
6462 .rpc_release = nfs4_locku_release_calldata,
6465 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6466 struct nfs_open_context *ctx,
6467 struct nfs4_lock_state *lsp,
6468 struct nfs_seqid *seqid)
6470 struct nfs4_unlockdata *data;
6471 struct rpc_message msg = {
6472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6473 .rpc_cred = ctx->cred,
6475 struct rpc_task_setup task_setup_data = {
6476 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6477 .rpc_message = &msg,
6478 .callback_ops = &nfs4_locku_ops,
6479 .workqueue = nfsiod_workqueue,
6480 .flags = RPC_TASK_ASYNC,
6483 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6484 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6486 /* Ensure this is an unlock - when canceling a lock, the
6487 * canceled lock is passed in, and it won't be an unlock.
6489 fl->fl_type = F_UNLCK;
6490 if (fl->fl_flags & FL_CLOSE)
6491 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6493 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6495 nfs_free_seqid(seqid);
6496 return ERR_PTR(-ENOMEM);
6499 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6500 msg.rpc_argp = &data->arg;
6501 msg.rpc_resp = &data->res;
6502 task_setup_data.callback_data = data;
6503 return rpc_run_task(&task_setup_data);
6506 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6508 struct inode *inode = state->inode;
6509 struct nfs4_state_owner *sp = state->owner;
6510 struct nfs_inode *nfsi = NFS_I(inode);
6511 struct nfs_seqid *seqid;
6512 struct nfs4_lock_state *lsp;
6513 struct rpc_task *task;
6514 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6516 unsigned char fl_flags = request->fl_flags;
6518 status = nfs4_set_lock_state(state, request);
6519 /* Unlock _before_ we do the RPC call */
6520 request->fl_flags |= FL_EXISTS;
6521 /* Exclude nfs_delegation_claim_locks() */
6522 mutex_lock(&sp->so_delegreturn_mutex);
6523 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6524 down_read(&nfsi->rwsem);
6525 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6526 up_read(&nfsi->rwsem);
6527 mutex_unlock(&sp->so_delegreturn_mutex);
6530 up_read(&nfsi->rwsem);
6531 mutex_unlock(&sp->so_delegreturn_mutex);
6534 /* Is this a delegated lock? */
6535 lsp = request->fl_u.nfs4_fl.owner;
6536 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6538 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6539 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6543 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6544 status = PTR_ERR(task);
6547 status = rpc_wait_for_completion_task(task);
6550 request->fl_flags = fl_flags;
6551 trace_nfs4_unlock(request, state, F_SETLK, status);
6555 struct nfs4_lockdata {
6556 struct nfs_lock_args arg;
6557 struct nfs_lock_res res;
6558 struct nfs4_lock_state *lsp;
6559 struct nfs_open_context *ctx;
6560 struct file_lock fl;
6561 unsigned long timestamp;
6564 struct nfs_server *server;
6567 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6568 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6571 struct nfs4_lockdata *p;
6572 struct inode *inode = lsp->ls_state->inode;
6573 struct nfs_server *server = NFS_SERVER(inode);
6574 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6576 p = kzalloc(sizeof(*p), gfp_mask);
6580 p->arg.fh = NFS_FH(inode);
6582 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6583 if (IS_ERR(p->arg.open_seqid))
6585 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6586 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6587 if (IS_ERR(p->arg.lock_seqid))
6588 goto out_free_seqid;
6589 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6590 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6591 p->arg.lock_owner.s_dev = server->s_dev;
6592 p->res.lock_seqid = p->arg.lock_seqid;
6595 p->ctx = get_nfs_open_context(ctx);
6596 locks_init_lock(&p->fl);
6597 locks_copy_lock(&p->fl, fl);
6600 nfs_free_seqid(p->arg.open_seqid);
6606 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6608 struct nfs4_lockdata *data = calldata;
6609 struct nfs4_state *state = data->lsp->ls_state;
6611 dprintk("%s: begin!\n", __func__);
6612 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6614 /* Do we need to do an open_to_lock_owner? */
6615 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6616 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6617 goto out_release_lock_seqid;
6619 nfs4_stateid_copy(&data->arg.open_stateid,
6620 &state->open_stateid);
6621 data->arg.new_lock_owner = 1;
6622 data->res.open_seqid = data->arg.open_seqid;
6624 data->arg.new_lock_owner = 0;
6625 nfs4_stateid_copy(&data->arg.lock_stateid,
6626 &data->lsp->ls_stateid);
6628 if (!nfs4_valid_open_stateid(state)) {
6629 data->rpc_status = -EBADF;
6630 task->tk_action = NULL;
6631 goto out_release_open_seqid;
6633 data->timestamp = jiffies;
6634 if (nfs4_setup_sequence(data->server->nfs_client,
6635 &data->arg.seq_args,
6639 out_release_open_seqid:
6640 nfs_release_seqid(data->arg.open_seqid);
6641 out_release_lock_seqid:
6642 nfs_release_seqid(data->arg.lock_seqid);
6644 nfs4_sequence_done(task, &data->res.seq_res);
6645 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6648 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6650 struct nfs4_lockdata *data = calldata;
6651 struct nfs4_lock_state *lsp = data->lsp;
6653 dprintk("%s: begin!\n", __func__);
6655 if (!nfs4_sequence_done(task, &data->res.seq_res))
6658 data->rpc_status = task->tk_status;
6659 switch (task->tk_status) {
6661 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6663 if (data->arg.new_lock && !data->cancelled) {
6664 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6665 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6668 if (data->arg.new_lock_owner != 0) {
6669 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6670 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6671 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6672 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6675 case -NFS4ERR_BAD_STATEID:
6676 case -NFS4ERR_OLD_STATEID:
6677 case -NFS4ERR_STALE_STATEID:
6678 case -NFS4ERR_EXPIRED:
6679 if (data->arg.new_lock_owner != 0) {
6680 if (!nfs4_stateid_match(&data->arg.open_stateid,
6681 &lsp->ls_state->open_stateid))
6683 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6688 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6691 if (!data->cancelled)
6692 rpc_restart_call_prepare(task);
6696 static void nfs4_lock_release(void *calldata)
6698 struct nfs4_lockdata *data = calldata;
6700 dprintk("%s: begin!\n", __func__);
6701 nfs_free_seqid(data->arg.open_seqid);
6702 if (data->cancelled && data->rpc_status == 0) {
6703 struct rpc_task *task;
6704 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6705 data->arg.lock_seqid);
6707 rpc_put_task_async(task);
6708 dprintk("%s: cancelling lock!\n", __func__);
6710 nfs_free_seqid(data->arg.lock_seqid);
6711 nfs4_put_lock_state(data->lsp);
6712 put_nfs_open_context(data->ctx);
6714 dprintk("%s: done!\n", __func__);
6717 static const struct rpc_call_ops nfs4_lock_ops = {
6718 .rpc_call_prepare = nfs4_lock_prepare,
6719 .rpc_call_done = nfs4_lock_done,
6720 .rpc_release = nfs4_lock_release,
6723 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6726 case -NFS4ERR_ADMIN_REVOKED:
6727 case -NFS4ERR_EXPIRED:
6728 case -NFS4ERR_BAD_STATEID:
6729 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6730 if (new_lock_owner != 0 ||
6731 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6732 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6734 case -NFS4ERR_STALE_STATEID:
6735 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6736 nfs4_schedule_lease_recovery(server->nfs_client);
6740 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6742 struct nfs4_lockdata *data;
6743 struct rpc_task *task;
6744 struct rpc_message msg = {
6745 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6746 .rpc_cred = state->owner->so_cred,
6748 struct rpc_task_setup task_setup_data = {
6749 .rpc_client = NFS_CLIENT(state->inode),
6750 .rpc_message = &msg,
6751 .callback_ops = &nfs4_lock_ops,
6752 .workqueue = nfsiod_workqueue,
6753 .flags = RPC_TASK_ASYNC,
6757 dprintk("%s: begin!\n", __func__);
6758 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6759 fl->fl_u.nfs4_fl.owner,
6760 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6764 data->arg.block = 1;
6765 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6766 recovery_type > NFS_LOCK_NEW);
6767 msg.rpc_argp = &data->arg;
6768 msg.rpc_resp = &data->res;
6769 task_setup_data.callback_data = data;
6770 if (recovery_type > NFS_LOCK_NEW) {
6771 if (recovery_type == NFS_LOCK_RECLAIM)
6772 data->arg.reclaim = NFS_LOCK_RECLAIM;
6774 data->arg.new_lock = 1;
6775 task = rpc_run_task(&task_setup_data);
6777 return PTR_ERR(task);
6778 ret = rpc_wait_for_completion_task(task);
6780 ret = data->rpc_status;
6782 nfs4_handle_setlk_error(data->server, data->lsp,
6783 data->arg.new_lock_owner, ret);
6785 data->cancelled = true;
6787 dprintk("%s: done, ret = %d!\n", __func__, ret);
6788 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6792 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6794 struct nfs_server *server = NFS_SERVER(state->inode);
6795 struct nfs4_exception exception = {
6796 .inode = state->inode,
6801 /* Cache the lock if possible... */
6802 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6804 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6805 if (err != -NFS4ERR_DELAY)
6807 nfs4_handle_exception(server, err, &exception);
6808 } while (exception.retry);
6812 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6814 struct nfs_server *server = NFS_SERVER(state->inode);
6815 struct nfs4_exception exception = {
6816 .inode = state->inode,
6820 err = nfs4_set_lock_state(state, request);
6823 if (!recover_lost_locks) {
6824 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6828 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6830 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6834 case -NFS4ERR_GRACE:
6835 case -NFS4ERR_DELAY:
6836 nfs4_handle_exception(server, err, &exception);
6839 } while (exception.retry);
6844 #if defined(CONFIG_NFS_V4_1)
6845 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6847 struct nfs4_lock_state *lsp;
6850 status = nfs4_set_lock_state(state, request);
6853 lsp = request->fl_u.nfs4_fl.owner;
6854 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6855 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6857 return nfs4_lock_expired(state, request);
6861 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6863 struct nfs_inode *nfsi = NFS_I(state->inode);
6864 struct nfs4_state_owner *sp = state->owner;
6865 unsigned char fl_flags = request->fl_flags;
6868 request->fl_flags |= FL_ACCESS;
6869 status = locks_lock_inode_wait(state->inode, request);
6872 mutex_lock(&sp->so_delegreturn_mutex);
6873 down_read(&nfsi->rwsem);
6874 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6875 /* Yes: cache locks! */
6876 /* ...but avoid races with delegation recall... */
6877 request->fl_flags = fl_flags & ~FL_SLEEP;
6878 status = locks_lock_inode_wait(state->inode, request);
6879 up_read(&nfsi->rwsem);
6880 mutex_unlock(&sp->so_delegreturn_mutex);
6883 up_read(&nfsi->rwsem);
6884 mutex_unlock(&sp->so_delegreturn_mutex);
6885 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6887 request->fl_flags = fl_flags;
6891 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6893 struct nfs4_exception exception = {
6895 .inode = state->inode,
6896 .interruptible = true,
6901 err = _nfs4_proc_setlk(state, cmd, request);
6902 if (err == -NFS4ERR_DENIED)
6904 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6906 } while (exception.retry);
6910 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6911 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6914 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6915 struct file_lock *request)
6917 int status = -ERESTARTSYS;
6918 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6920 while(!signalled()) {
6921 status = nfs4_proc_setlk(state, cmd, request);
6922 if ((status != -EAGAIN) || IS_SETLK(cmd))
6924 freezable_schedule_timeout_interruptible(timeout);
6926 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6927 status = -ERESTARTSYS;
6932 #ifdef CONFIG_NFS_V4_1
6933 struct nfs4_lock_waiter {
6934 struct task_struct *task;
6935 struct inode *inode;
6936 struct nfs_lowner *owner;
6940 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
6943 struct nfs4_lock_waiter *waiter = wait->private;
6945 /* NULL key means to wake up everyone */
6947 struct cb_notify_lock_args *cbnl = key;
6948 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6949 *wowner = waiter->owner;
6951 /* Only wake if the callback was for the same owner. */
6952 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6955 /* Make sure it's for the right inode */
6956 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6960 /* override "private" so we can use default_wake_function */
6961 wait->private = waiter->task;
6962 ret = woken_wake_function(wait, mode, flags, key);
6964 list_del_init(&wait->entry);
6965 wait->private = waiter;
6970 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6972 int status = -ERESTARTSYS;
6973 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6974 struct nfs_server *server = NFS_SERVER(state->inode);
6975 struct nfs_client *clp = server->nfs_client;
6976 wait_queue_head_t *q = &clp->cl_lock_waitq;
6977 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6978 .id = lsp->ls_seqid.owner_id,
6979 .s_dev = server->s_dev };
6980 struct nfs4_lock_waiter waiter = { .task = current,
6981 .inode = state->inode,
6983 wait_queue_entry_t wait;
6985 /* Don't bother with waitqueue if we don't expect a callback */
6986 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6987 return nfs4_retry_setlk_simple(state, cmd, request);
6990 wait.private = &waiter;
6991 wait.func = nfs4_wake_lock_waiter;
6993 while(!signalled()) {
6994 add_wait_queue(q, &wait);
6995 status = nfs4_proc_setlk(state, cmd, request);
6996 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
6997 finish_wait(q, &wait);
7001 status = -ERESTARTSYS;
7002 freezer_do_not_count();
7003 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7005 finish_wait(q, &wait);
7010 #else /* !CONFIG_NFS_V4_1 */
7012 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7014 return nfs4_retry_setlk_simple(state, cmd, request);
7019 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7021 struct nfs_open_context *ctx;
7022 struct nfs4_state *state;
7025 /* verify open state */
7026 ctx = nfs_file_open_context(filp);
7029 if (IS_GETLK(cmd)) {
7031 return nfs4_proc_getlk(state, F_GETLK, request);
7035 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7038 if (request->fl_type == F_UNLCK) {
7040 return nfs4_proc_unlck(state, cmd, request);
7047 if ((request->fl_flags & FL_POSIX) &&
7048 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7052 * Don't rely on the VFS having checked the file open mode,
7053 * since it won't do this for flock() locks.
7055 switch (request->fl_type) {
7057 if (!(filp->f_mode & FMODE_READ))
7061 if (!(filp->f_mode & FMODE_WRITE))
7065 status = nfs4_set_lock_state(state, request);
7069 return nfs4_retry_setlk(state, cmd, request);
7072 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7074 struct nfs_server *server = NFS_SERVER(state->inode);
7077 err = nfs4_set_lock_state(state, fl);
7080 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7081 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7084 struct nfs_release_lockowner_data {
7085 struct nfs4_lock_state *lsp;
7086 struct nfs_server *server;
7087 struct nfs_release_lockowner_args args;
7088 struct nfs_release_lockowner_res res;
7089 unsigned long timestamp;
7092 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7094 struct nfs_release_lockowner_data *data = calldata;
7095 struct nfs_server *server = data->server;
7096 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7097 &data->res.seq_res, task);
7098 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7099 data->timestamp = jiffies;
7102 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7104 struct nfs_release_lockowner_data *data = calldata;
7105 struct nfs_server *server = data->server;
7107 nfs40_sequence_done(task, &data->res.seq_res);
7109 switch (task->tk_status) {
7111 renew_lease(server, data->timestamp);
7113 case -NFS4ERR_STALE_CLIENTID:
7114 case -NFS4ERR_EXPIRED:
7115 nfs4_schedule_lease_recovery(server->nfs_client);
7117 case -NFS4ERR_LEASE_MOVED:
7118 case -NFS4ERR_DELAY:
7119 if (nfs4_async_handle_error(task, server,
7120 NULL, NULL) == -EAGAIN)
7121 rpc_restart_call_prepare(task);
7125 static void nfs4_release_lockowner_release(void *calldata)
7127 struct nfs_release_lockowner_data *data = calldata;
7128 nfs4_free_lock_state(data->server, data->lsp);
7132 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7133 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7134 .rpc_call_done = nfs4_release_lockowner_done,
7135 .rpc_release = nfs4_release_lockowner_release,
7139 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7141 struct nfs_release_lockowner_data *data;
7142 struct rpc_message msg = {
7143 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7146 if (server->nfs_client->cl_mvops->minor_version != 0)
7149 data = kmalloc(sizeof(*data), GFP_NOFS);
7153 data->server = server;
7154 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7155 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7156 data->args.lock_owner.s_dev = server->s_dev;
7158 msg.rpc_argp = &data->args;
7159 msg.rpc_resp = &data->res;
7160 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7161 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7164 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7166 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7167 struct dentry *unused, struct inode *inode,
7168 const char *key, const void *buf,
7169 size_t buflen, int flags)
7171 return nfs4_proc_set_acl(inode, buf, buflen);
7174 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7175 struct dentry *unused, struct inode *inode,
7176 const char *key, void *buf, size_t buflen)
7178 return nfs4_proc_get_acl(inode, buf, buflen);
7181 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7183 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7186 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7188 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7189 struct dentry *unused, struct inode *inode,
7190 const char *key, const void *buf,
7191 size_t buflen, int flags)
7193 if (security_ismaclabel(key))
7194 return nfs4_set_security_label(inode, buf, buflen);
7199 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7200 struct dentry *unused, struct inode *inode,
7201 const char *key, void *buf, size_t buflen)
7203 if (security_ismaclabel(key))
7204 return nfs4_get_security_label(inode, buf, buflen);
7209 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7213 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7214 len = security_inode_listsecurity(inode, list, list_len);
7215 if (list_len && len > list_len)
7221 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7222 .prefix = XATTR_SECURITY_PREFIX,
7223 .get = nfs4_xattr_get_nfs4_label,
7224 .set = nfs4_xattr_set_nfs4_label,
7230 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7238 * nfs_fhget will use either the mounted_on_fileid or the fileid
7240 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7242 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7243 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7244 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7245 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7248 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7249 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7250 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7254 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7255 const struct qstr *name,
7256 struct nfs4_fs_locations *fs_locations,
7259 struct nfs_server *server = NFS_SERVER(dir);
7261 struct nfs4_fs_locations_arg args = {
7262 .dir_fh = NFS_FH(dir),
7267 struct nfs4_fs_locations_res res = {
7268 .fs_locations = fs_locations,
7270 struct rpc_message msg = {
7271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7277 dprintk("%s: start\n", __func__);
7279 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7280 bitmask[1] = nfs4_fattr_bitmap[1];
7282 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7283 * is not supported */
7284 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7285 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7287 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7289 nfs_fattr_init(&fs_locations->fattr);
7290 fs_locations->server = server;
7291 fs_locations->nlocations = 0;
7292 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7293 dprintk("%s: returned status = %d\n", __func__, status);
7297 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7298 const struct qstr *name,
7299 struct nfs4_fs_locations *fs_locations,
7302 struct nfs4_exception exception = {
7303 .interruptible = true,
7307 err = _nfs4_proc_fs_locations(client, dir, name,
7308 fs_locations, page);
7309 trace_nfs4_get_fs_locations(dir, name, err);
7310 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7312 } while (exception.retry);
7317 * This operation also signals the server that this client is
7318 * performing migration recovery. The server can stop returning
7319 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7320 * appended to this compound to identify the client ID which is
7321 * performing recovery.
7323 static int _nfs40_proc_get_locations(struct inode *inode,
7324 struct nfs4_fs_locations *locations,
7325 struct page *page, const struct cred *cred)
7327 struct nfs_server *server = NFS_SERVER(inode);
7328 struct rpc_clnt *clnt = server->client;
7330 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7332 struct nfs4_fs_locations_arg args = {
7333 .clientid = server->nfs_client->cl_clientid,
7334 .fh = NFS_FH(inode),
7337 .migration = 1, /* skip LOOKUP */
7338 .renew = 1, /* append RENEW */
7340 struct nfs4_fs_locations_res res = {
7341 .fs_locations = locations,
7345 struct rpc_message msg = {
7346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7351 unsigned long now = jiffies;
7354 nfs_fattr_init(&locations->fattr);
7355 locations->server = server;
7356 locations->nlocations = 0;
7358 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7359 status = nfs4_call_sync_sequence(clnt, server, &msg,
7360 &args.seq_args, &res.seq_res);
7364 renew_lease(server, now);
7368 #ifdef CONFIG_NFS_V4_1
7371 * This operation also signals the server that this client is
7372 * performing migration recovery. The server can stop asserting
7373 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7374 * performing this operation is identified in the SEQUENCE
7375 * operation in this compound.
7377 * When the client supports GETATTR(fs_locations_info), it can
7378 * be plumbed in here.
7380 static int _nfs41_proc_get_locations(struct inode *inode,
7381 struct nfs4_fs_locations *locations,
7382 struct page *page, const struct cred *cred)
7384 struct nfs_server *server = NFS_SERVER(inode);
7385 struct rpc_clnt *clnt = server->client;
7387 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7389 struct nfs4_fs_locations_arg args = {
7390 .fh = NFS_FH(inode),
7393 .migration = 1, /* skip LOOKUP */
7395 struct nfs4_fs_locations_res res = {
7396 .fs_locations = locations,
7399 struct rpc_message msg = {
7400 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7407 nfs_fattr_init(&locations->fattr);
7408 locations->server = server;
7409 locations->nlocations = 0;
7411 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7412 status = nfs4_call_sync_sequence(clnt, server, &msg,
7413 &args.seq_args, &res.seq_res);
7414 if (status == NFS4_OK &&
7415 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7416 status = -NFS4ERR_LEASE_MOVED;
7420 #endif /* CONFIG_NFS_V4_1 */
7423 * nfs4_proc_get_locations - discover locations for a migrated FSID
7424 * @inode: inode on FSID that is migrating
7425 * @locations: result of query
7427 * @cred: credential to use for this operation
7429 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7430 * operation failed, or a negative errno if a local error occurred.
7432 * On success, "locations" is filled in, but if the server has
7433 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7436 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7437 * from this client that require migration recovery.
7439 int nfs4_proc_get_locations(struct inode *inode,
7440 struct nfs4_fs_locations *locations,
7441 struct page *page, const struct cred *cred)
7443 struct nfs_server *server = NFS_SERVER(inode);
7444 struct nfs_client *clp = server->nfs_client;
7445 const struct nfs4_mig_recovery_ops *ops =
7446 clp->cl_mvops->mig_recovery_ops;
7447 struct nfs4_exception exception = {
7448 .interruptible = true,
7452 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7453 (unsigned long long)server->fsid.major,
7454 (unsigned long long)server->fsid.minor,
7456 nfs_display_fhandle(NFS_FH(inode), __func__);
7459 status = ops->get_locations(inode, locations, page, cred);
7460 if (status != -NFS4ERR_DELAY)
7462 nfs4_handle_exception(server, status, &exception);
7463 } while (exception.retry);
7468 * This operation also signals the server that this client is
7469 * performing "lease moved" recovery. The server can stop
7470 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7471 * is appended to this compound to identify the client ID which is
7472 * performing recovery.
7474 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7476 struct nfs_server *server = NFS_SERVER(inode);
7477 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7478 struct rpc_clnt *clnt = server->client;
7479 struct nfs4_fsid_present_arg args = {
7480 .fh = NFS_FH(inode),
7481 .clientid = clp->cl_clientid,
7482 .renew = 1, /* append RENEW */
7484 struct nfs4_fsid_present_res res = {
7487 struct rpc_message msg = {
7488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7493 unsigned long now = jiffies;
7496 res.fh = nfs_alloc_fhandle();
7500 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7501 status = nfs4_call_sync_sequence(clnt, server, &msg,
7502 &args.seq_args, &res.seq_res);
7503 nfs_free_fhandle(res.fh);
7507 do_renew_lease(clp, now);
7511 #ifdef CONFIG_NFS_V4_1
7514 * This operation also signals the server that this client is
7515 * performing "lease moved" recovery. The server can stop asserting
7516 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7517 * this operation is identified in the SEQUENCE operation in this
7520 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7522 struct nfs_server *server = NFS_SERVER(inode);
7523 struct rpc_clnt *clnt = server->client;
7524 struct nfs4_fsid_present_arg args = {
7525 .fh = NFS_FH(inode),
7527 struct nfs4_fsid_present_res res = {
7529 struct rpc_message msg = {
7530 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7537 res.fh = nfs_alloc_fhandle();
7541 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7542 status = nfs4_call_sync_sequence(clnt, server, &msg,
7543 &args.seq_args, &res.seq_res);
7544 nfs_free_fhandle(res.fh);
7545 if (status == NFS4_OK &&
7546 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7547 status = -NFS4ERR_LEASE_MOVED;
7551 #endif /* CONFIG_NFS_V4_1 */
7554 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7555 * @inode: inode on FSID to check
7556 * @cred: credential to use for this operation
7558 * Server indicates whether the FSID is present, moved, or not
7559 * recognized. This operation is necessary to clear a LEASE_MOVED
7560 * condition for this client ID.
7562 * Returns NFS4_OK if the FSID is present on this server,
7563 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7564 * NFS4ERR code if some error occurred on the server, or a
7565 * negative errno if a local failure occurred.
7567 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
7569 struct nfs_server *server = NFS_SERVER(inode);
7570 struct nfs_client *clp = server->nfs_client;
7571 const struct nfs4_mig_recovery_ops *ops =
7572 clp->cl_mvops->mig_recovery_ops;
7573 struct nfs4_exception exception = {
7574 .interruptible = true,
7578 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7579 (unsigned long long)server->fsid.major,
7580 (unsigned long long)server->fsid.minor,
7582 nfs_display_fhandle(NFS_FH(inode), __func__);
7585 status = ops->fsid_present(inode, cred);
7586 if (status != -NFS4ERR_DELAY)
7588 nfs4_handle_exception(server, status, &exception);
7589 } while (exception.retry);
7594 * If 'use_integrity' is true and the state managment nfs_client
7595 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7596 * and the machine credential as per RFC3530bis and RFC5661 Security
7597 * Considerations sections. Otherwise, just use the user cred with the
7598 * filesystem's rpc_client.
7600 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7603 struct nfs4_secinfo_arg args = {
7604 .dir_fh = NFS_FH(dir),
7607 struct nfs4_secinfo_res res = {
7610 struct rpc_message msg = {
7611 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7615 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7616 const struct cred *cred = NULL;
7618 if (use_integrity) {
7619 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7620 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7621 msg.rpc_cred = cred;
7624 dprintk("NFS call secinfo %s\n", name->name);
7626 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7627 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7629 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7631 dprintk("NFS reply secinfo: %d\n", status);
7638 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7639 struct nfs4_secinfo_flavors *flavors)
7641 struct nfs4_exception exception = {
7642 .interruptible = true,
7646 err = -NFS4ERR_WRONGSEC;
7648 /* try to use integrity protection with machine cred */
7649 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7650 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7653 * if unable to use integrity protection, or SECINFO with
7654 * integrity protection returns NFS4ERR_WRONGSEC (which is
7655 * disallowed by spec, but exists in deployed servers) use
7656 * the current filesystem's rpc_client and the user cred.
7658 if (err == -NFS4ERR_WRONGSEC)
7659 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7661 trace_nfs4_secinfo(dir, name, err);
7662 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7664 } while (exception.retry);
7668 #ifdef CONFIG_NFS_V4_1
7670 * Check the exchange flags returned by the server for invalid flags, having
7671 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7674 static int nfs4_check_cl_exchange_flags(u32 flags)
7676 if (flags & ~EXCHGID4_FLAG_MASK_R)
7678 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7679 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7681 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7685 return -NFS4ERR_INVAL;
7689 nfs41_same_server_scope(struct nfs41_server_scope *a,
7690 struct nfs41_server_scope *b)
7692 if (a->server_scope_sz != b->server_scope_sz)
7694 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7698 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7702 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7703 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7707 * nfs4_proc_bind_one_conn_to_session()
7709 * The 4.1 client currently uses the same TCP connection for the
7710 * fore and backchannel.
7713 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7714 struct rpc_xprt *xprt,
7715 struct nfs_client *clp,
7716 const struct cred *cred)
7719 struct nfs41_bind_conn_to_session_args args = {
7721 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7723 struct nfs41_bind_conn_to_session_res res;
7724 struct rpc_message msg = {
7726 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7731 struct rpc_task_setup task_setup_data = {
7734 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7735 .rpc_message = &msg,
7736 .flags = RPC_TASK_TIMEOUT,
7738 struct rpc_task *task;
7740 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7741 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7742 args.dir = NFS4_CDFC4_FORE;
7744 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7745 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7746 args.dir = NFS4_CDFC4_FORE;
7748 task = rpc_run_task(&task_setup_data);
7749 if (!IS_ERR(task)) {
7750 status = task->tk_status;
7753 status = PTR_ERR(task);
7754 trace_nfs4_bind_conn_to_session(clp, status);
7756 if (memcmp(res.sessionid.data,
7757 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7758 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7761 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7762 dprintk("NFS: %s: Unexpected direction from server\n",
7766 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7767 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7776 struct rpc_bind_conn_calldata {
7777 struct nfs_client *clp;
7778 const struct cred *cred;
7782 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7783 struct rpc_xprt *xprt,
7786 struct rpc_bind_conn_calldata *p = calldata;
7788 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7791 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
7793 struct rpc_bind_conn_calldata data = {
7797 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7798 nfs4_proc_bind_conn_to_session_callback, &data);
7802 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7803 * and operations we'd like to see to enable certain features in the allow map
7805 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7806 .how = SP4_MACH_CRED,
7807 .enforce.u.words = {
7808 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7809 1 << (OP_EXCHANGE_ID - 32) |
7810 1 << (OP_CREATE_SESSION - 32) |
7811 1 << (OP_DESTROY_SESSION - 32) |
7812 1 << (OP_DESTROY_CLIENTID - 32)
7815 [0] = 1 << (OP_CLOSE) |
7816 1 << (OP_OPEN_DOWNGRADE) |
7818 1 << (OP_DELEGRETURN) |
7820 [1] = 1 << (OP_SECINFO - 32) |
7821 1 << (OP_SECINFO_NO_NAME - 32) |
7822 1 << (OP_LAYOUTRETURN - 32) |
7823 1 << (OP_TEST_STATEID - 32) |
7824 1 << (OP_FREE_STATEID - 32) |
7825 1 << (OP_WRITE - 32)
7830 * Select the state protection mode for client `clp' given the server results
7831 * from exchange_id in `sp'.
7833 * Returns 0 on success, negative errno otherwise.
7835 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7836 struct nfs41_state_protection *sp)
7838 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7839 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7840 1 << (OP_EXCHANGE_ID - 32) |
7841 1 << (OP_CREATE_SESSION - 32) |
7842 1 << (OP_DESTROY_SESSION - 32) |
7843 1 << (OP_DESTROY_CLIENTID - 32)
7845 unsigned long flags = 0;
7849 if (sp->how == SP4_MACH_CRED) {
7850 /* Print state protect result */
7851 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7852 for (i = 0; i <= LAST_NFS4_OP; i++) {
7853 if (test_bit(i, sp->enforce.u.longs))
7854 dfprintk(MOUNT, " enforce op %d\n", i);
7855 if (test_bit(i, sp->allow.u.longs))
7856 dfprintk(MOUNT, " allow op %d\n", i);
7859 /* make sure nothing is on enforce list that isn't supported */
7860 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7861 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7862 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7869 * Minimal mode - state operations are allowed to use machine
7870 * credential. Note this already happens by default, so the
7871 * client doesn't have to do anything more than the negotiation.
7873 * NOTE: we don't care if EXCHANGE_ID is in the list -
7874 * we're already using the machine cred for exchange_id
7875 * and will never use a different cred.
7877 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7878 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7879 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7880 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7881 dfprintk(MOUNT, "sp4_mach_cred:\n");
7882 dfprintk(MOUNT, " minimal mode enabled\n");
7883 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
7885 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7890 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7891 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7892 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7893 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7894 dfprintk(MOUNT, " cleanup mode enabled\n");
7895 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
7898 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7899 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7900 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
7903 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7904 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7905 dfprintk(MOUNT, " secinfo mode enabled\n");
7906 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
7909 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7910 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7911 dfprintk(MOUNT, " stateid mode enabled\n");
7912 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
7915 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7916 dfprintk(MOUNT, " write mode enabled\n");
7917 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
7920 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7921 dfprintk(MOUNT, " commit mode enabled\n");
7922 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
7926 clp->cl_sp4_flags = flags;
7930 struct nfs41_exchange_id_data {
7931 struct nfs41_exchange_id_res res;
7932 struct nfs41_exchange_id_args args;
7935 static void nfs4_exchange_id_release(void *data)
7937 struct nfs41_exchange_id_data *cdata =
7938 (struct nfs41_exchange_id_data *)data;
7940 nfs_put_client(cdata->args.client);
7941 kfree(cdata->res.impl_id);
7942 kfree(cdata->res.server_scope);
7943 kfree(cdata->res.server_owner);
7947 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7948 .rpc_release = nfs4_exchange_id_release,
7952 * _nfs4_proc_exchange_id()
7954 * Wrapper for EXCHANGE_ID operation.
7956 static struct rpc_task *
7957 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
7958 u32 sp4_how, struct rpc_xprt *xprt)
7960 struct rpc_message msg = {
7961 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7964 struct rpc_task_setup task_setup_data = {
7965 .rpc_client = clp->cl_rpcclient,
7966 .callback_ops = &nfs4_exchange_id_call_ops,
7967 .rpc_message = &msg,
7968 .flags = RPC_TASK_TIMEOUT,
7970 struct nfs41_exchange_id_data *calldata;
7973 if (!refcount_inc_not_zero(&clp->cl_count))
7974 return ERR_PTR(-EIO);
7977 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7981 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
7983 status = nfs4_init_uniform_client_string(clp);
7987 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7990 if (unlikely(calldata->res.server_owner == NULL))
7993 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7995 if (unlikely(calldata->res.server_scope == NULL))
7996 goto out_server_owner;
7998 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7999 if (unlikely(calldata->res.impl_id == NULL))
8000 goto out_server_scope;
8004 calldata->args.state_protect.how = SP4_NONE;
8008 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8018 task_setup_data.rpc_xprt = xprt;
8019 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8020 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8021 sizeof(calldata->args.verifier.data));
8023 calldata->args.client = clp;
8024 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8025 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8026 #ifdef CONFIG_NFS_V4_1_MIGRATION
8027 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8029 msg.rpc_argp = &calldata->args;
8030 msg.rpc_resp = &calldata->res;
8031 task_setup_data.callback_data = calldata;
8033 return rpc_run_task(&task_setup_data);
8036 kfree(calldata->res.impl_id);
8038 kfree(calldata->res.server_scope);
8040 kfree(calldata->res.server_owner);
8044 nfs_put_client(clp);
8045 return ERR_PTR(status);
8049 * _nfs4_proc_exchange_id()
8051 * Wrapper for EXCHANGE_ID operation.
8053 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8056 struct rpc_task *task;
8057 struct nfs41_exchange_id_args *argp;
8058 struct nfs41_exchange_id_res *resp;
8061 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8063 return PTR_ERR(task);
8065 argp = task->tk_msg.rpc_argp;
8066 resp = task->tk_msg.rpc_resp;
8067 status = task->tk_status;
8071 status = nfs4_check_cl_exchange_flags(resp->flags);
8075 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8079 clp->cl_clientid = resp->clientid;
8080 clp->cl_exchange_flags = resp->flags;
8081 clp->cl_seqid = resp->seqid;
8082 /* Client ID is not confirmed */
8083 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8084 clear_bit(NFS4_SESSION_ESTABLISHED,
8085 &clp->cl_session->session_state);
8087 if (clp->cl_serverscope != NULL &&
8088 !nfs41_same_server_scope(clp->cl_serverscope,
8089 resp->server_scope)) {
8090 dprintk("%s: server_scope mismatch detected\n",
8092 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8095 swap(clp->cl_serverowner, resp->server_owner);
8096 swap(clp->cl_serverscope, resp->server_scope);
8097 swap(clp->cl_implid, resp->impl_id);
8099 /* Save the EXCHANGE_ID verifier session trunk tests */
8100 memcpy(clp->cl_confirm.data, argp->verifier.data,
8101 sizeof(clp->cl_confirm.data));
8103 trace_nfs4_exchange_id(clp, status);
8109 * nfs4_proc_exchange_id()
8111 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8113 * Since the clientid has expired, all compounds using sessions
8114 * associated with the stale clientid will be returning
8115 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8116 * be in some phase of session reset.
8118 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8120 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8122 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8125 /* try SP4_MACH_CRED if krb5i/p */
8126 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8127 authflavor == RPC_AUTH_GSS_KRB5P) {
8128 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8134 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8138 * nfs4_test_session_trunk
8140 * This is an add_xprt_test() test function called from
8141 * rpc_clnt_setup_test_and_add_xprt.
8143 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8144 * and is dereferrenced in nfs4_exchange_id_release
8146 * Upon success, add the new transport to the rpc_clnt
8148 * @clnt: struct rpc_clnt to get new transport
8149 * @xprt: the rpc_xprt to test
8150 * @data: call data for _nfs4_proc_exchange_id.
8152 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8155 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8156 struct rpc_task *task;
8161 dprintk("--> %s try %s\n", __func__,
8162 xprt->address_strings[RPC_DISPLAY_ADDR]);
8164 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8166 /* Test connection for session trunking. Async exchange_id call */
8167 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8171 status = task->tk_status;
8173 status = nfs4_detect_session_trunking(adata->clp,
8174 task->tk_msg.rpc_resp, xprt);
8177 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8181 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8183 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8184 const struct cred *cred)
8186 struct rpc_message msg = {
8187 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8193 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8194 trace_nfs4_destroy_clientid(clp, status);
8196 dprintk("NFS: Got error %d from the server %s on "
8197 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8201 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8202 const struct cred *cred)
8207 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8208 ret = _nfs4_proc_destroy_clientid(clp, cred);
8210 case -NFS4ERR_DELAY:
8211 case -NFS4ERR_CLIENTID_BUSY:
8221 int nfs4_destroy_clientid(struct nfs_client *clp)
8223 const struct cred *cred;
8226 if (clp->cl_mvops->minor_version < 1)
8228 if (clp->cl_exchange_flags == 0)
8230 if (clp->cl_preserve_clid)
8232 cred = nfs4_get_clid_cred(clp);
8233 ret = nfs4_proc_destroy_clientid(clp, cred);
8237 case -NFS4ERR_STALE_CLIENTID:
8238 clp->cl_exchange_flags = 0;
8244 struct nfs4_get_lease_time_data {
8245 struct nfs4_get_lease_time_args *args;
8246 struct nfs4_get_lease_time_res *res;
8247 struct nfs_client *clp;
8250 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8253 struct nfs4_get_lease_time_data *data =
8254 (struct nfs4_get_lease_time_data *)calldata;
8256 dprintk("--> %s\n", __func__);
8257 /* just setup sequence, do not trigger session recovery
8258 since we're invoked within one */
8259 nfs4_setup_sequence(data->clp,
8260 &data->args->la_seq_args,
8261 &data->res->lr_seq_res,
8263 dprintk("<-- %s\n", __func__);
8267 * Called from nfs4_state_manager thread for session setup, so don't recover
8268 * from sequence operation or clientid errors.
8270 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8272 struct nfs4_get_lease_time_data *data =
8273 (struct nfs4_get_lease_time_data *)calldata;
8275 dprintk("--> %s\n", __func__);
8276 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8278 switch (task->tk_status) {
8279 case -NFS4ERR_DELAY:
8280 case -NFS4ERR_GRACE:
8281 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8282 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8283 task->tk_status = 0;
8285 case -NFS4ERR_RETRY_UNCACHED_REP:
8286 rpc_restart_call_prepare(task);
8289 dprintk("<-- %s\n", __func__);
8292 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8293 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8294 .rpc_call_done = nfs4_get_lease_time_done,
8297 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8299 struct rpc_task *task;
8300 struct nfs4_get_lease_time_args args;
8301 struct nfs4_get_lease_time_res res = {
8302 .lr_fsinfo = fsinfo,
8304 struct nfs4_get_lease_time_data data = {
8309 struct rpc_message msg = {
8310 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8314 struct rpc_task_setup task_setup = {
8315 .rpc_client = clp->cl_rpcclient,
8316 .rpc_message = &msg,
8317 .callback_ops = &nfs4_get_lease_time_ops,
8318 .callback_data = &data,
8319 .flags = RPC_TASK_TIMEOUT,
8323 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8324 task = rpc_run_task(&task_setup);
8327 return PTR_ERR(task);
8329 status = task->tk_status;
8335 * Initialize the values to be used by the client in CREATE_SESSION
8336 * If nfs4_init_session set the fore channel request and response sizes,
8339 * Set the back channel max_resp_sz_cached to zero to force the client to
8340 * always set csa_cachethis to FALSE because the current implementation
8341 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8343 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8344 struct rpc_clnt *clnt)
8346 unsigned int max_rqst_sz, max_resp_sz;
8347 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8349 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8350 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8352 /* Fore channel attributes */
8353 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8354 args->fc_attrs.max_resp_sz = max_resp_sz;
8355 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8356 args->fc_attrs.max_reqs = max_session_slots;
8358 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8359 "max_ops=%u max_reqs=%u\n",
8361 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8362 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8364 /* Back channel attributes */
8365 args->bc_attrs.max_rqst_sz = max_bc_payload;
8366 args->bc_attrs.max_resp_sz = max_bc_payload;
8367 args->bc_attrs.max_resp_sz_cached = 0;
8368 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8369 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8371 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8372 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8374 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8375 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8376 args->bc_attrs.max_reqs);
8379 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8380 struct nfs41_create_session_res *res)
8382 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8383 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8385 if (rcvd->max_resp_sz > sent->max_resp_sz)
8388 * Our requested max_ops is the minimum we need; we're not
8389 * prepared to break up compounds into smaller pieces than that.
8390 * So, no point even trying to continue if the server won't
8393 if (rcvd->max_ops < sent->max_ops)
8395 if (rcvd->max_reqs == 0)
8397 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8398 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8402 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8403 struct nfs41_create_session_res *res)
8405 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8406 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8408 if (!(res->flags & SESSION4_BACK_CHAN))
8410 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8412 if (rcvd->max_resp_sz < sent->max_resp_sz)
8414 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8416 if (rcvd->max_ops > sent->max_ops)
8418 if (rcvd->max_reqs > sent->max_reqs)
8424 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8425 struct nfs41_create_session_res *res)
8429 ret = nfs4_verify_fore_channel_attrs(args, res);
8432 return nfs4_verify_back_channel_attrs(args, res);
8435 static void nfs4_update_session(struct nfs4_session *session,
8436 struct nfs41_create_session_res *res)
8438 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8439 /* Mark client id and session as being confirmed */
8440 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8441 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8442 session->flags = res->flags;
8443 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8444 if (res->flags & SESSION4_BACK_CHAN)
8445 memcpy(&session->bc_attrs, &res->bc_attrs,
8446 sizeof(session->bc_attrs));
8449 static int _nfs4_proc_create_session(struct nfs_client *clp,
8450 const struct cred *cred)
8452 struct nfs4_session *session = clp->cl_session;
8453 struct nfs41_create_session_args args = {
8455 .clientid = clp->cl_clientid,
8456 .seqid = clp->cl_seqid,
8457 .cb_program = NFS4_CALLBACK,
8459 struct nfs41_create_session_res res;
8461 struct rpc_message msg = {
8462 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8469 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8470 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8472 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8473 trace_nfs4_create_session(clp, status);
8476 case -NFS4ERR_STALE_CLIENTID:
8477 case -NFS4ERR_DELAY:
8486 /* Verify the session's negotiated channel_attrs values */
8487 status = nfs4_verify_channel_attrs(&args, &res);
8488 /* Increment the clientid slot sequence id */
8491 nfs4_update_session(session, &res);
8498 * Issues a CREATE_SESSION operation to the server.
8499 * It is the responsibility of the caller to verify the session is
8500 * expired before calling this routine.
8502 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8506 struct nfs4_session *session = clp->cl_session;
8508 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8510 status = _nfs4_proc_create_session(clp, cred);
8514 /* Init or reset the session slot tables */
8515 status = nfs4_setup_session_slot_tables(session);
8516 dprintk("slot table setup returned %d\n", status);
8520 ptr = (unsigned *)&session->sess_id.data[0];
8521 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8522 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8524 dprintk("<-- %s\n", __func__);
8529 * Issue the over-the-wire RPC DESTROY_SESSION.
8530 * The caller must serialize access to this routine.
8532 int nfs4_proc_destroy_session(struct nfs4_session *session,
8533 const struct cred *cred)
8535 struct rpc_message msg = {
8536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8537 .rpc_argp = session,
8542 dprintk("--> nfs4_proc_destroy_session\n");
8544 /* session is still being setup */
8545 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8548 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8549 trace_nfs4_destroy_session(session->clp, status);
8552 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8553 "Session has been destroyed regardless...\n", status);
8555 dprintk("<-- nfs4_proc_destroy_session\n");
8560 * Renew the cl_session lease.
8562 struct nfs4_sequence_data {
8563 struct nfs_client *clp;
8564 struct nfs4_sequence_args args;
8565 struct nfs4_sequence_res res;
8568 static void nfs41_sequence_release(void *data)
8570 struct nfs4_sequence_data *calldata = data;
8571 struct nfs_client *clp = calldata->clp;
8573 if (refcount_read(&clp->cl_count) > 1)
8574 nfs4_schedule_state_renewal(clp);
8575 nfs_put_client(clp);
8579 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8581 switch(task->tk_status) {
8582 case -NFS4ERR_DELAY:
8583 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8586 nfs4_schedule_lease_recovery(clp);
8591 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8593 struct nfs4_sequence_data *calldata = data;
8594 struct nfs_client *clp = calldata->clp;
8596 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8599 trace_nfs4_sequence(clp, task->tk_status);
8600 if (task->tk_status < 0) {
8601 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8602 if (refcount_read(&clp->cl_count) == 1)
8605 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8606 rpc_restart_call_prepare(task);
8610 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8612 dprintk("<-- %s\n", __func__);
8615 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8617 struct nfs4_sequence_data *calldata = data;
8618 struct nfs_client *clp = calldata->clp;
8619 struct nfs4_sequence_args *args;
8620 struct nfs4_sequence_res *res;
8622 args = task->tk_msg.rpc_argp;
8623 res = task->tk_msg.rpc_resp;
8625 nfs4_setup_sequence(clp, args, res, task);
8628 static const struct rpc_call_ops nfs41_sequence_ops = {
8629 .rpc_call_done = nfs41_sequence_call_done,
8630 .rpc_call_prepare = nfs41_sequence_prepare,
8631 .rpc_release = nfs41_sequence_release,
8634 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8635 const struct cred *cred,
8636 struct nfs4_slot *slot,
8639 struct nfs4_sequence_data *calldata;
8640 struct rpc_message msg = {
8641 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8644 struct rpc_task_setup task_setup_data = {
8645 .rpc_client = clp->cl_rpcclient,
8646 .rpc_message = &msg,
8647 .callback_ops = &nfs41_sequence_ops,
8648 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8650 struct rpc_task *ret;
8652 ret = ERR_PTR(-EIO);
8653 if (!refcount_inc_not_zero(&clp->cl_count))
8656 ret = ERR_PTR(-ENOMEM);
8657 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8658 if (calldata == NULL)
8660 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
8661 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8662 msg.rpc_argp = &calldata->args;
8663 msg.rpc_resp = &calldata->res;
8664 calldata->clp = clp;
8665 task_setup_data.callback_data = calldata;
8667 ret = rpc_run_task(&task_setup_data);
8672 nfs_put_client(clp);
8674 nfs41_release_slot(slot);
8678 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
8680 struct rpc_task *task;
8683 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8685 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8687 ret = PTR_ERR(task);
8689 rpc_put_task_async(task);
8690 dprintk("<-- %s status=%d\n", __func__, ret);
8694 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
8696 struct rpc_task *task;
8699 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8701 ret = PTR_ERR(task);
8704 ret = rpc_wait_for_completion_task(task);
8706 ret = task->tk_status;
8709 dprintk("<-- %s status=%d\n", __func__, ret);
8713 struct nfs4_reclaim_complete_data {
8714 struct nfs_client *clp;
8715 struct nfs41_reclaim_complete_args arg;
8716 struct nfs41_reclaim_complete_res res;
8719 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8721 struct nfs4_reclaim_complete_data *calldata = data;
8723 nfs4_setup_sequence(calldata->clp,
8724 &calldata->arg.seq_args,
8725 &calldata->res.seq_res,
8729 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8731 switch(task->tk_status) {
8733 wake_up_all(&clp->cl_lock_waitq);
8735 case -NFS4ERR_COMPLETE_ALREADY:
8736 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8738 case -NFS4ERR_DELAY:
8739 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8741 case -NFS4ERR_RETRY_UNCACHED_REP:
8743 case -NFS4ERR_BADSESSION:
8744 case -NFS4ERR_DEADSESSION:
8745 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8746 nfs4_schedule_session_recovery(clp->cl_session,
8750 nfs4_schedule_lease_recovery(clp);
8755 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8757 struct nfs4_reclaim_complete_data *calldata = data;
8758 struct nfs_client *clp = calldata->clp;
8759 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8761 dprintk("--> %s\n", __func__);
8762 if (!nfs41_sequence_done(task, res))
8765 trace_nfs4_reclaim_complete(clp, task->tk_status);
8766 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8767 rpc_restart_call_prepare(task);
8770 dprintk("<-- %s\n", __func__);
8773 static void nfs4_free_reclaim_complete_data(void *data)
8775 struct nfs4_reclaim_complete_data *calldata = data;
8780 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8781 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8782 .rpc_call_done = nfs4_reclaim_complete_done,
8783 .rpc_release = nfs4_free_reclaim_complete_data,
8787 * Issue a global reclaim complete.
8789 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8790 const struct cred *cred)
8792 struct nfs4_reclaim_complete_data *calldata;
8793 struct rpc_task *task;
8794 struct rpc_message msg = {
8795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8798 struct rpc_task_setup task_setup_data = {
8799 .rpc_client = clp->cl_rpcclient,
8800 .rpc_message = &msg,
8801 .callback_ops = &nfs4_reclaim_complete_call_ops,
8802 .flags = RPC_TASK_ASYNC,
8804 int status = -ENOMEM;
8806 dprintk("--> %s\n", __func__);
8807 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8808 if (calldata == NULL)
8810 calldata->clp = clp;
8811 calldata->arg.one_fs = 0;
8813 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
8814 msg.rpc_argp = &calldata->arg;
8815 msg.rpc_resp = &calldata->res;
8816 task_setup_data.callback_data = calldata;
8817 task = rpc_run_task(&task_setup_data);
8819 status = PTR_ERR(task);
8822 status = rpc_wait_for_completion_task(task);
8824 status = task->tk_status;
8827 dprintk("<-- %s status=%d\n", __func__, status);
8832 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8834 struct nfs4_layoutget *lgp = calldata;
8835 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8837 dprintk("--> %s\n", __func__);
8838 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8839 &lgp->res.seq_res, task);
8840 dprintk("<-- %s\n", __func__);
8843 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8845 struct nfs4_layoutget *lgp = calldata;
8847 dprintk("--> %s\n", __func__);
8848 nfs41_sequence_process(task, &lgp->res.seq_res);
8849 dprintk("<-- %s\n", __func__);
8853 nfs4_layoutget_handle_exception(struct rpc_task *task,
8854 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8856 struct inode *inode = lgp->args.inode;
8857 struct nfs_server *server = NFS_SERVER(inode);
8858 struct pnfs_layout_hdr *lo;
8859 int nfs4err = task->tk_status;
8860 int err, status = 0;
8863 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8865 nfs4_sequence_free_slot(&lgp->res.seq_res);
8872 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8873 * on the file. set tk_status to -ENODATA to tell upper layer to
8876 case -NFS4ERR_LAYOUTUNAVAILABLE:
8880 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8881 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8883 case -NFS4ERR_BADLAYOUT:
8884 status = -EOVERFLOW;
8887 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8888 * (or clients) writing to the same RAID stripe except when
8889 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8891 * Treat it like we would RECALLCONFLICT -- we retry for a little
8892 * while, and then eventually give up.
8894 case -NFS4ERR_LAYOUTTRYLATER:
8895 if (lgp->args.minlength == 0) {
8896 status = -EOVERFLOW;
8901 case -NFS4ERR_RECALLCONFLICT:
8902 status = -ERECALLCONFLICT;
8904 case -NFS4ERR_DELEG_REVOKED:
8905 case -NFS4ERR_ADMIN_REVOKED:
8906 case -NFS4ERR_EXPIRED:
8907 case -NFS4ERR_BAD_STATEID:
8908 exception->timeout = 0;
8909 spin_lock(&inode->i_lock);
8910 lo = NFS_I(inode)->layout;
8911 /* If the open stateid was bad, then recover it. */
8912 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8913 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
8914 spin_unlock(&inode->i_lock);
8915 exception->state = lgp->args.ctx->state;
8916 exception->stateid = &lgp->args.stateid;
8921 * Mark the bad layout state as invalid, then retry
8923 pnfs_mark_layout_stateid_invalid(lo, &head);
8924 spin_unlock(&inode->i_lock);
8925 nfs_commit_inode(inode, 0);
8926 pnfs_free_lseg_list(&head);
8931 err = nfs4_handle_exception(server, nfs4err, exception);
8933 if (exception->retry)
8939 dprintk("<-- %s\n", __func__);
8943 size_t max_response_pages(struct nfs_server *server)
8945 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8946 return nfs_page_array_len(0, max_resp_sz);
8949 static void nfs4_layoutget_release(void *calldata)
8951 struct nfs4_layoutget *lgp = calldata;
8953 dprintk("--> %s\n", __func__);
8954 nfs4_sequence_free_slot(&lgp->res.seq_res);
8955 pnfs_layoutget_free(lgp);
8956 dprintk("<-- %s\n", __func__);
8959 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8960 .rpc_call_prepare = nfs4_layoutget_prepare,
8961 .rpc_call_done = nfs4_layoutget_done,
8962 .rpc_release = nfs4_layoutget_release,
8965 struct pnfs_layout_segment *
8966 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
8968 struct inode *inode = lgp->args.inode;
8969 struct nfs_server *server = NFS_SERVER(inode);
8970 struct rpc_task *task;
8971 struct rpc_message msg = {
8972 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8973 .rpc_argp = &lgp->args,
8974 .rpc_resp = &lgp->res,
8975 .rpc_cred = lgp->cred,
8977 struct rpc_task_setup task_setup_data = {
8978 .rpc_client = server->client,
8979 .rpc_message = &msg,
8980 .callback_ops = &nfs4_layoutget_call_ops,
8981 .callback_data = lgp,
8982 .flags = RPC_TASK_ASYNC,
8984 struct pnfs_layout_segment *lseg = NULL;
8985 struct nfs4_exception exception = {
8987 .timeout = *timeout,
8991 dprintk("--> %s\n", __func__);
8993 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8994 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8996 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
8998 task = rpc_run_task(&task_setup_data);
9000 return ERR_CAST(task);
9001 status = rpc_wait_for_completion_task(task);
9005 if (task->tk_status < 0) {
9006 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9007 *timeout = exception.timeout;
9008 } else if (lgp->res.layoutp->len == 0) {
9010 *timeout = nfs4_update_delay(&exception.timeout);
9012 lseg = pnfs_layout_process(lgp);
9014 trace_nfs4_layoutget(lgp->args.ctx,
9021 dprintk("<-- %s status=%d\n", __func__, status);
9023 return ERR_PTR(status);
9028 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9030 struct nfs4_layoutreturn *lrp = calldata;
9032 dprintk("--> %s\n", __func__);
9033 nfs4_setup_sequence(lrp->clp,
9034 &lrp->args.seq_args,
9037 if (!pnfs_layout_is_valid(lrp->args.layout))
9041 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9043 struct nfs4_layoutreturn *lrp = calldata;
9044 struct nfs_server *server;
9046 dprintk("--> %s\n", __func__);
9048 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9051 server = NFS_SERVER(lrp->args.inode);
9052 switch (task->tk_status) {
9053 case -NFS4ERR_OLD_STATEID:
9054 if (nfs4_layoutreturn_refresh_stateid(&lrp->args.stateid,
9060 task->tk_status = 0;
9064 case -NFS4ERR_DELAY:
9065 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9069 dprintk("<-- %s\n", __func__);
9072 task->tk_status = 0;
9073 nfs4_sequence_free_slot(&lrp->res.seq_res);
9074 rpc_restart_call_prepare(task);
9077 static void nfs4_layoutreturn_release(void *calldata)
9079 struct nfs4_layoutreturn *lrp = calldata;
9080 struct pnfs_layout_hdr *lo = lrp->args.layout;
9082 dprintk("--> %s\n", __func__);
9083 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9084 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9085 nfs4_sequence_free_slot(&lrp->res.seq_res);
9086 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9087 lrp->ld_private.ops->free(&lrp->ld_private);
9088 pnfs_put_layout_hdr(lrp->args.layout);
9089 nfs_iput_and_deactive(lrp->inode);
9091 dprintk("<-- %s\n", __func__);
9094 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9095 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9096 .rpc_call_done = nfs4_layoutreturn_done,
9097 .rpc_release = nfs4_layoutreturn_release,
9100 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9102 struct rpc_task *task;
9103 struct rpc_message msg = {
9104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9105 .rpc_argp = &lrp->args,
9106 .rpc_resp = &lrp->res,
9107 .rpc_cred = lrp->cred,
9109 struct rpc_task_setup task_setup_data = {
9110 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9111 .rpc_message = &msg,
9112 .callback_ops = &nfs4_layoutreturn_call_ops,
9113 .callback_data = lrp,
9117 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9118 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9119 &task_setup_data.rpc_client, &msg);
9121 dprintk("--> %s\n", __func__);
9123 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9125 nfs4_layoutreturn_release(lrp);
9128 task_setup_data.flags |= RPC_TASK_ASYNC;
9130 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9131 task = rpc_run_task(&task_setup_data);
9133 return PTR_ERR(task);
9135 status = task->tk_status;
9136 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9137 dprintk("<-- %s status=%d\n", __func__, status);
9143 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9144 struct pnfs_device *pdev,
9145 const struct cred *cred)
9147 struct nfs4_getdeviceinfo_args args = {
9149 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9150 NOTIFY_DEVICEID4_DELETE,
9152 struct nfs4_getdeviceinfo_res res = {
9155 struct rpc_message msg = {
9156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9163 dprintk("--> %s\n", __func__);
9164 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9165 if (res.notification & ~args.notify_types)
9166 dprintk("%s: unsupported notification\n", __func__);
9167 if (res.notification != args.notify_types)
9170 dprintk("<-- %s status=%d\n", __func__, status);
9175 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9176 struct pnfs_device *pdev,
9177 const struct cred *cred)
9179 struct nfs4_exception exception = { };
9183 err = nfs4_handle_exception(server,
9184 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9186 } while (exception.retry);
9189 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9191 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9193 struct nfs4_layoutcommit_data *data = calldata;
9194 struct nfs_server *server = NFS_SERVER(data->args.inode);
9196 nfs4_setup_sequence(server->nfs_client,
9197 &data->args.seq_args,
9203 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9205 struct nfs4_layoutcommit_data *data = calldata;
9206 struct nfs_server *server = NFS_SERVER(data->args.inode);
9208 if (!nfs41_sequence_done(task, &data->res.seq_res))
9211 switch (task->tk_status) { /* Just ignore these failures */
9212 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9213 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9214 case -NFS4ERR_BADLAYOUT: /* no layout */
9215 case -NFS4ERR_GRACE: /* loca_recalim always false */
9216 task->tk_status = 0;
9220 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9221 rpc_restart_call_prepare(task);
9227 static void nfs4_layoutcommit_release(void *calldata)
9229 struct nfs4_layoutcommit_data *data = calldata;
9231 pnfs_cleanup_layoutcommit(data);
9232 nfs_post_op_update_inode_force_wcc(data->args.inode,
9234 put_cred(data->cred);
9235 nfs_iput_and_deactive(data->inode);
9239 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9240 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9241 .rpc_call_done = nfs4_layoutcommit_done,
9242 .rpc_release = nfs4_layoutcommit_release,
9246 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9248 struct rpc_message msg = {
9249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9250 .rpc_argp = &data->args,
9251 .rpc_resp = &data->res,
9252 .rpc_cred = data->cred,
9254 struct rpc_task_setup task_setup_data = {
9255 .task = &data->task,
9256 .rpc_client = NFS_CLIENT(data->args.inode),
9257 .rpc_message = &msg,
9258 .callback_ops = &nfs4_layoutcommit_ops,
9259 .callback_data = data,
9261 struct rpc_task *task;
9264 dprintk("NFS: initiating layoutcommit call. sync %d "
9265 "lbw: %llu inode %lu\n", sync,
9266 data->args.lastbytewritten,
9267 data->args.inode->i_ino);
9270 data->inode = nfs_igrab_and_active(data->args.inode);
9271 if (data->inode == NULL) {
9272 nfs4_layoutcommit_release(data);
9275 task_setup_data.flags = RPC_TASK_ASYNC;
9277 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9278 task = rpc_run_task(&task_setup_data);
9280 return PTR_ERR(task);
9282 status = task->tk_status;
9283 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9284 dprintk("%s: status %d\n", __func__, status);
9290 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9291 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9294 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9295 struct nfs_fsinfo *info,
9296 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9298 struct nfs41_secinfo_no_name_args args = {
9299 .style = SECINFO_STYLE_CURRENT_FH,
9301 struct nfs4_secinfo_res res = {
9304 struct rpc_message msg = {
9305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9309 struct rpc_clnt *clnt = server->client;
9310 const struct cred *cred = NULL;
9313 if (use_integrity) {
9314 clnt = server->nfs_client->cl_rpcclient;
9315 cred = nfs4_get_clid_cred(server->nfs_client);
9316 msg.rpc_cred = cred;
9319 dprintk("--> %s\n", __func__);
9320 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9322 dprintk("<-- %s status=%d\n", __func__, status);
9330 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9331 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9333 struct nfs4_exception exception = {
9334 .interruptible = true,
9338 /* first try using integrity protection */
9339 err = -NFS4ERR_WRONGSEC;
9341 /* try to use integrity protection with machine cred */
9342 if (_nfs4_is_integrity_protected(server->nfs_client))
9343 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9347 * if unable to use integrity protection, or SECINFO with
9348 * integrity protection returns NFS4ERR_WRONGSEC (which is
9349 * disallowed by spec, but exists in deployed servers) use
9350 * the current filesystem's rpc_client and the user cred.
9352 if (err == -NFS4ERR_WRONGSEC)
9353 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9358 case -NFS4ERR_WRONGSEC:
9362 err = nfs4_handle_exception(server, err, &exception);
9364 } while (exception.retry);
9370 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9371 struct nfs_fsinfo *info)
9375 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9376 struct nfs4_secinfo_flavors *flavors;
9377 struct nfs4_secinfo4 *secinfo;
9380 page = alloc_page(GFP_KERNEL);
9386 flavors = page_address(page);
9387 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9390 * Fall back on "guess and check" method if
9391 * the server doesn't support SECINFO_NO_NAME
9393 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9394 err = nfs4_find_root_sec(server, fhandle, info);
9400 for (i = 0; i < flavors->num_flavors; i++) {
9401 secinfo = &flavors->flavors[i];
9403 switch (secinfo->flavor) {
9407 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9408 &secinfo->flavor_info);
9411 flavor = RPC_AUTH_MAXFLAVOR;
9415 if (!nfs_auth_info_match(&server->auth_info, flavor))
9416 flavor = RPC_AUTH_MAXFLAVOR;
9418 if (flavor != RPC_AUTH_MAXFLAVOR) {
9419 err = nfs4_lookup_root_sec(server, fhandle,
9426 if (flavor == RPC_AUTH_MAXFLAVOR)
9437 static int _nfs41_test_stateid(struct nfs_server *server,
9438 nfs4_stateid *stateid,
9439 const struct cred *cred)
9442 struct nfs41_test_stateid_args args = {
9445 struct nfs41_test_stateid_res res;
9446 struct rpc_message msg = {
9447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9452 struct rpc_clnt *rpc_client = server->client;
9454 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9457 dprintk("NFS call test_stateid %p\n", stateid);
9458 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9459 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9460 &args.seq_args, &res.seq_res);
9461 if (status != NFS_OK) {
9462 dprintk("NFS reply test_stateid: failed, %d\n", status);
9465 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9469 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9470 int err, struct nfs4_exception *exception)
9472 exception->retry = 0;
9474 case -NFS4ERR_DELAY:
9475 case -NFS4ERR_RETRY_UNCACHED_REP:
9476 nfs4_handle_exception(server, err, exception);
9478 case -NFS4ERR_BADSESSION:
9479 case -NFS4ERR_BADSLOT:
9480 case -NFS4ERR_BAD_HIGH_SLOT:
9481 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9482 case -NFS4ERR_DEADSESSION:
9483 nfs4_do_handle_exception(server, err, exception);
9488 * nfs41_test_stateid - perform a TEST_STATEID operation
9490 * @server: server / transport on which to perform the operation
9491 * @stateid: state ID to test
9494 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9495 * Otherwise a negative NFS4ERR value is returned if the operation
9496 * failed or the state ID is not currently valid.
9498 static int nfs41_test_stateid(struct nfs_server *server,
9499 nfs4_stateid *stateid,
9500 const struct cred *cred)
9502 struct nfs4_exception exception = {
9503 .interruptible = true,
9507 err = _nfs41_test_stateid(server, stateid, cred);
9508 nfs4_handle_delay_or_session_error(server, err, &exception);
9509 } while (exception.retry);
9513 struct nfs_free_stateid_data {
9514 struct nfs_server *server;
9515 struct nfs41_free_stateid_args args;
9516 struct nfs41_free_stateid_res res;
9519 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9521 struct nfs_free_stateid_data *data = calldata;
9522 nfs4_setup_sequence(data->server->nfs_client,
9523 &data->args.seq_args,
9528 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9530 struct nfs_free_stateid_data *data = calldata;
9532 nfs41_sequence_done(task, &data->res.seq_res);
9534 switch (task->tk_status) {
9535 case -NFS4ERR_DELAY:
9536 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9537 rpc_restart_call_prepare(task);
9541 static void nfs41_free_stateid_release(void *calldata)
9546 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9547 .rpc_call_prepare = nfs41_free_stateid_prepare,
9548 .rpc_call_done = nfs41_free_stateid_done,
9549 .rpc_release = nfs41_free_stateid_release,
9553 * nfs41_free_stateid - perform a FREE_STATEID operation
9555 * @server: server / transport on which to perform the operation
9556 * @stateid: state ID to release
9558 * @privileged: set to true if this call needs to be privileged
9560 * Note: this function is always asynchronous.
9562 static int nfs41_free_stateid(struct nfs_server *server,
9563 const nfs4_stateid *stateid,
9564 const struct cred *cred,
9567 struct rpc_message msg = {
9568 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9571 struct rpc_task_setup task_setup = {
9572 .rpc_client = server->client,
9573 .rpc_message = &msg,
9574 .callback_ops = &nfs41_free_stateid_ops,
9575 .flags = RPC_TASK_ASYNC,
9577 struct nfs_free_stateid_data *data;
9578 struct rpc_task *task;
9580 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9581 &task_setup.rpc_client, &msg);
9583 dprintk("NFS call free_stateid %p\n", stateid);
9584 data = kmalloc(sizeof(*data), GFP_NOFS);
9587 data->server = server;
9588 nfs4_stateid_copy(&data->args.stateid, stateid);
9590 task_setup.callback_data = data;
9592 msg.rpc_argp = &data->args;
9593 msg.rpc_resp = &data->res;
9594 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
9595 task = rpc_run_task(&task_setup);
9597 return PTR_ERR(task);
9603 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9605 const struct cred *cred = lsp->ls_state->owner->so_cred;
9607 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9608 nfs4_free_lock_state(server, lsp);
9611 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9612 const nfs4_stateid *s2)
9614 if (s1->type != s2->type)
9617 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9620 if (s1->seqid == s2->seqid)
9623 return s1->seqid == 0 || s2->seqid == 0;
9626 #endif /* CONFIG_NFS_V4_1 */
9628 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9629 const nfs4_stateid *s2)
9631 return nfs4_stateid_match(s1, s2);
9635 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9636 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9637 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9638 .recover_open = nfs4_open_reclaim,
9639 .recover_lock = nfs4_lock_reclaim,
9640 .establish_clid = nfs4_init_clientid,
9641 .detect_trunking = nfs40_discover_server_trunking,
9644 #if defined(CONFIG_NFS_V4_1)
9645 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9646 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9647 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9648 .recover_open = nfs4_open_reclaim,
9649 .recover_lock = nfs4_lock_reclaim,
9650 .establish_clid = nfs41_init_clientid,
9651 .reclaim_complete = nfs41_proc_reclaim_complete,
9652 .detect_trunking = nfs41_discover_server_trunking,
9654 #endif /* CONFIG_NFS_V4_1 */
9656 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9657 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9658 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9659 .recover_open = nfs40_open_expired,
9660 .recover_lock = nfs4_lock_expired,
9661 .establish_clid = nfs4_init_clientid,
9664 #if defined(CONFIG_NFS_V4_1)
9665 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9666 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9667 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9668 .recover_open = nfs41_open_expired,
9669 .recover_lock = nfs41_lock_expired,
9670 .establish_clid = nfs41_init_clientid,
9672 #endif /* CONFIG_NFS_V4_1 */
9674 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9675 .sched_state_renewal = nfs4_proc_async_renew,
9676 .get_state_renewal_cred = nfs4_get_renew_cred,
9677 .renew_lease = nfs4_proc_renew,
9680 #if defined(CONFIG_NFS_V4_1)
9681 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9682 .sched_state_renewal = nfs41_proc_async_sequence,
9683 .get_state_renewal_cred = nfs4_get_machine_cred,
9684 .renew_lease = nfs4_proc_sequence,
9688 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9689 .get_locations = _nfs40_proc_get_locations,
9690 .fsid_present = _nfs40_proc_fsid_present,
9693 #if defined(CONFIG_NFS_V4_1)
9694 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9695 .get_locations = _nfs41_proc_get_locations,
9696 .fsid_present = _nfs41_proc_fsid_present,
9698 #endif /* CONFIG_NFS_V4_1 */
9700 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9702 .init_caps = NFS_CAP_READDIRPLUS
9703 | NFS_CAP_ATOMIC_OPEN
9704 | NFS_CAP_POSIX_LOCK,
9705 .init_client = nfs40_init_client,
9706 .shutdown_client = nfs40_shutdown_client,
9707 .match_stateid = nfs4_match_stateid,
9708 .find_root_sec = nfs4_find_root_sec,
9709 .free_lock_state = nfs4_release_lockowner,
9710 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9711 .alloc_seqid = nfs_alloc_seqid,
9712 .call_sync_ops = &nfs40_call_sync_ops,
9713 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9714 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9715 .state_renewal_ops = &nfs40_state_renewal_ops,
9716 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9719 #if defined(CONFIG_NFS_V4_1)
9720 static struct nfs_seqid *
9721 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9726 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9728 .init_caps = NFS_CAP_READDIRPLUS
9729 | NFS_CAP_ATOMIC_OPEN
9730 | NFS_CAP_POSIX_LOCK
9731 | NFS_CAP_STATEID_NFSV41
9732 | NFS_CAP_ATOMIC_OPEN_V1
9734 .init_client = nfs41_init_client,
9735 .shutdown_client = nfs41_shutdown_client,
9736 .match_stateid = nfs41_match_stateid,
9737 .find_root_sec = nfs41_find_root_sec,
9738 .free_lock_state = nfs41_free_lock_state,
9739 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9740 .alloc_seqid = nfs_alloc_no_seqid,
9741 .session_trunk = nfs4_test_session_trunk,
9742 .call_sync_ops = &nfs41_call_sync_ops,
9743 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9744 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9745 .state_renewal_ops = &nfs41_state_renewal_ops,
9746 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9750 #if defined(CONFIG_NFS_V4_2)
9751 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9753 .init_caps = NFS_CAP_READDIRPLUS
9754 | NFS_CAP_ATOMIC_OPEN
9755 | NFS_CAP_POSIX_LOCK
9756 | NFS_CAP_STATEID_NFSV41
9757 | NFS_CAP_ATOMIC_OPEN_V1
9761 | NFS_CAP_OFFLOAD_CANCEL
9762 | NFS_CAP_DEALLOCATE
9764 | NFS_CAP_LAYOUTSTATS
9766 | NFS_CAP_LAYOUTERROR,
9767 .init_client = nfs41_init_client,
9768 .shutdown_client = nfs41_shutdown_client,
9769 .match_stateid = nfs41_match_stateid,
9770 .find_root_sec = nfs41_find_root_sec,
9771 .free_lock_state = nfs41_free_lock_state,
9772 .call_sync_ops = &nfs41_call_sync_ops,
9773 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9774 .alloc_seqid = nfs_alloc_no_seqid,
9775 .session_trunk = nfs4_test_session_trunk,
9776 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9777 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9778 .state_renewal_ops = &nfs41_state_renewal_ops,
9779 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9783 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9784 [0] = &nfs_v4_0_minor_ops,
9785 #if defined(CONFIG_NFS_V4_1)
9786 [1] = &nfs_v4_1_minor_ops,
9788 #if defined(CONFIG_NFS_V4_2)
9789 [2] = &nfs_v4_2_minor_ops,
9793 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9795 ssize_t error, error2;
9797 error = generic_listxattr(dentry, list, size);
9805 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9808 return error + error2;
9811 static const struct inode_operations nfs4_dir_inode_operations = {
9812 .create = nfs_create,
9813 .lookup = nfs_lookup,
9814 .atomic_open = nfs_atomic_open,
9816 .unlink = nfs_unlink,
9817 .symlink = nfs_symlink,
9821 .rename = nfs_rename,
9822 .permission = nfs_permission,
9823 .getattr = nfs_getattr,
9824 .setattr = nfs_setattr,
9825 .listxattr = nfs4_listxattr,
9828 static const struct inode_operations nfs4_file_inode_operations = {
9829 .permission = nfs_permission,
9830 .getattr = nfs_getattr,
9831 .setattr = nfs_setattr,
9832 .listxattr = nfs4_listxattr,
9835 const struct nfs_rpc_ops nfs_v4_clientops = {
9836 .version = 4, /* protocol version */
9837 .dentry_ops = &nfs4_dentry_operations,
9838 .dir_inode_ops = &nfs4_dir_inode_operations,
9839 .file_inode_ops = &nfs4_file_inode_operations,
9840 .file_ops = &nfs4_file_operations,
9841 .getroot = nfs4_proc_get_root,
9842 .submount = nfs4_submount,
9843 .try_mount = nfs4_try_mount,
9844 .getattr = nfs4_proc_getattr,
9845 .setattr = nfs4_proc_setattr,
9846 .lookup = nfs4_proc_lookup,
9847 .lookupp = nfs4_proc_lookupp,
9848 .access = nfs4_proc_access,
9849 .readlink = nfs4_proc_readlink,
9850 .create = nfs4_proc_create,
9851 .remove = nfs4_proc_remove,
9852 .unlink_setup = nfs4_proc_unlink_setup,
9853 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9854 .unlink_done = nfs4_proc_unlink_done,
9855 .rename_setup = nfs4_proc_rename_setup,
9856 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9857 .rename_done = nfs4_proc_rename_done,
9858 .link = nfs4_proc_link,
9859 .symlink = nfs4_proc_symlink,
9860 .mkdir = nfs4_proc_mkdir,
9861 .rmdir = nfs4_proc_rmdir,
9862 .readdir = nfs4_proc_readdir,
9863 .mknod = nfs4_proc_mknod,
9864 .statfs = nfs4_proc_statfs,
9865 .fsinfo = nfs4_proc_fsinfo,
9866 .pathconf = nfs4_proc_pathconf,
9867 .set_capabilities = nfs4_server_capabilities,
9868 .decode_dirent = nfs4_decode_dirent,
9869 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9870 .read_setup = nfs4_proc_read_setup,
9871 .read_done = nfs4_read_done,
9872 .write_setup = nfs4_proc_write_setup,
9873 .write_done = nfs4_write_done,
9874 .commit_setup = nfs4_proc_commit_setup,
9875 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9876 .commit_done = nfs4_commit_done,
9877 .lock = nfs4_proc_lock,
9878 .clear_acl_cache = nfs4_zap_acl_attr,
9879 .close_context = nfs4_close_context,
9880 .open_context = nfs4_atomic_open,
9881 .have_delegation = nfs4_have_delegation,
9882 .alloc_client = nfs4_alloc_client,
9883 .init_client = nfs4_init_client,
9884 .free_client = nfs4_free_client,
9885 .create_server = nfs4_create_server,
9886 .clone_server = nfs_clone_server,
9889 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9890 .name = XATTR_NAME_NFSV4_ACL,
9891 .list = nfs4_xattr_list_nfs4_acl,
9892 .get = nfs4_xattr_get_nfs4_acl,
9893 .set = nfs4_xattr_set_nfs4_acl,
9896 const struct xattr_handler *nfs4_xattr_handlers[] = {
9897 &nfs4_xattr_nfs4_acl_handler,
9898 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9899 &nfs4_xattr_nfs4_label_handler,