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.umask = current_umask();
1260 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1261 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1263 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1264 * will return permission denied for all bits until close */
1265 if (!(flags & O_EXCL)) {
1266 /* ask server to check for all possible rights as results
1268 switch (p->o_arg.claim) {
1271 case NFS4_OPEN_CLAIM_NULL:
1272 case NFS4_OPEN_CLAIM_FH:
1273 p->o_arg.access = NFS4_ACCESS_READ |
1274 NFS4_ACCESS_MODIFY |
1275 NFS4_ACCESS_EXTEND |
1276 NFS4_ACCESS_EXECUTE;
1279 p->o_arg.clientid = server->nfs_client->cl_clientid;
1280 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1281 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1282 p->o_arg.name = &dentry->d_name;
1283 p->o_arg.server = server;
1284 p->o_arg.bitmask = nfs4_bitmask(server, label);
1285 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1286 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1287 switch (p->o_arg.claim) {
1288 case NFS4_OPEN_CLAIM_NULL:
1289 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1290 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1291 p->o_arg.fh = NFS_FH(dir);
1293 case NFS4_OPEN_CLAIM_PREVIOUS:
1294 case NFS4_OPEN_CLAIM_FH:
1295 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1296 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1297 p->o_arg.fh = NFS_FH(d_inode(dentry));
1299 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
1300 p->o_arg.u.attrs = &p->attrs;
1301 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1303 memcpy(p->o_arg.u.verifier.data, c->verf,
1304 sizeof(p->o_arg.u.verifier.data));
1306 p->c_arg.fh = &p->o_res.fh;
1307 p->c_arg.stateid = &p->o_res.stateid;
1308 p->c_arg.seqid = p->o_arg.seqid;
1309 nfs4_init_opendata_res(p);
1310 kref_init(&p->kref);
1314 nfs4_label_free(p->a_label);
1316 nfs4_label_free(p->f_label);
1324 static void nfs4_opendata_free(struct kref *kref)
1326 struct nfs4_opendata *p = container_of(kref,
1327 struct nfs4_opendata, kref);
1328 struct super_block *sb = p->dentry->d_sb;
1330 nfs4_lgopen_release(p->lgp);
1331 nfs_free_seqid(p->o_arg.seqid);
1332 nfs4_sequence_free_slot(&p->o_res.seq_res);
1333 if (p->state != NULL)
1334 nfs4_put_open_state(p->state);
1335 nfs4_put_state_owner(p->owner);
1337 nfs4_label_free(p->a_label);
1338 nfs4_label_free(p->f_label);
1342 nfs_sb_deactive(sb);
1343 nfs_fattr_free_names(&p->f_attr);
1344 kfree(p->f_attr.mdsthreshold);
1348 static void nfs4_opendata_put(struct nfs4_opendata *p)
1351 kref_put(&p->kref, nfs4_opendata_free);
1354 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1357 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1358 case FMODE_READ|FMODE_WRITE:
1359 return state->n_rdwr != 0;
1361 return state->n_wronly != 0;
1363 return state->n_rdonly != 0;
1369 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1370 int open_mode, enum open_claim_type4 claim)
1374 if (open_mode & (O_EXCL|O_TRUNC))
1377 case NFS4_OPEN_CLAIM_NULL:
1378 case NFS4_OPEN_CLAIM_FH:
1383 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1385 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1386 && state->n_rdonly != 0;
1389 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1390 && state->n_wronly != 0;
1392 case FMODE_READ|FMODE_WRITE:
1393 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1394 && state->n_rdwr != 0;
1400 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1401 enum open_claim_type4 claim)
1403 if (delegation == NULL)
1405 if ((delegation->type & fmode) != fmode)
1407 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1410 case NFS4_OPEN_CLAIM_NULL:
1411 case NFS4_OPEN_CLAIM_FH:
1413 case NFS4_OPEN_CLAIM_PREVIOUS:
1414 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1420 nfs_mark_delegation_referenced(delegation);
1424 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1433 case FMODE_READ|FMODE_WRITE:
1436 nfs4_state_set_mode_locked(state, state->state | fmode);
1439 #ifdef CONFIG_NFS_V4_1
1440 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1442 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1444 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1446 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1450 #endif /* CONFIG_NFS_V4_1 */
1452 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1454 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1455 wake_up_all(&state->waitq);
1458 static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1459 const nfs4_stateid *stateid)
1461 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1462 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1464 if (stateid_seqid == state_seqid + 1U ||
1465 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1466 nfs_state_log_update_open_stateid(state);
1468 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1471 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1473 struct nfs_client *clp = state->owner->so_server->nfs_client;
1474 bool need_recover = false;
1476 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1477 need_recover = true;
1478 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1479 need_recover = true;
1480 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1481 need_recover = true;
1483 nfs4_state_mark_reclaim_nograce(clp, state);
1487 * Check for whether or not the caller may update the open stateid
1488 * to the value passed in by stateid.
1490 * Note: This function relies heavily on the server implementing
1491 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1493 * i.e. The stateid seqids have to be initialised to 1, and
1494 * are then incremented on every state transition.
1496 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1497 const nfs4_stateid *stateid)
1499 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1500 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1501 if (stateid->seqid == cpu_to_be32(1))
1502 nfs_state_log_update_open_stateid(state);
1504 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1508 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1509 nfs_state_log_out_of_order_open_stateid(state, stateid);
1515 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1517 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1519 if (state->n_wronly)
1520 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1521 if (state->n_rdonly)
1522 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1524 set_bit(NFS_O_RDWR_STATE, &state->flags);
1525 set_bit(NFS_OPEN_STATE, &state->flags);
1528 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1529 nfs4_stateid *stateid, fmode_t fmode)
1531 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1532 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1534 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1537 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1540 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1541 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1542 clear_bit(NFS_OPEN_STATE, &state->flags);
1544 if (stateid == NULL)
1546 /* Handle OPEN+OPEN_DOWNGRADE races */
1547 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1548 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1549 nfs_resync_open_stateid_locked(state);
1552 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1553 nfs4_stateid_copy(&state->stateid, stateid);
1554 nfs4_stateid_copy(&state->open_stateid, stateid);
1555 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1557 nfs_state_log_update_open_stateid(state);
1560 static void nfs_clear_open_stateid(struct nfs4_state *state,
1561 nfs4_stateid *arg_stateid,
1562 nfs4_stateid *stateid, fmode_t fmode)
1564 write_seqlock(&state->seqlock);
1565 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1566 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1567 nfs_clear_open_stateid_locked(state, stateid, fmode);
1568 write_sequnlock(&state->seqlock);
1569 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1570 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1573 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1574 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1575 __must_hold(&state->owner->so_lock)
1576 __must_hold(&state->seqlock)
1584 if (!nfs_need_update_open_stateid(state, stateid))
1586 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1590 /* Rely on seqids for serialisation with NFSv4.0 */
1591 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1594 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1596 * Ensure we process the state changes in the same order
1597 * in which the server processed them by delaying the
1598 * update of the stateid until we are in sequence.
1600 write_sequnlock(&state->seqlock);
1601 spin_unlock(&state->owner->so_lock);
1603 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1604 if (!signal_pending(current)) {
1605 if (schedule_timeout(5*HZ) == 0)
1611 finish_wait(&state->waitq, &wait);
1613 spin_lock(&state->owner->so_lock);
1614 write_seqlock(&state->seqlock);
1617 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1618 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1619 nfs4_stateid_copy(freeme, &state->open_stateid);
1620 nfs_test_and_clear_all_open_stateid(state);
1623 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1624 nfs4_stateid_copy(&state->stateid, stateid);
1625 nfs4_stateid_copy(&state->open_stateid, stateid);
1626 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1627 nfs_state_log_update_open_stateid(state);
1630 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1631 const nfs4_stateid *open_stateid,
1633 nfs4_stateid *freeme)
1636 * Protect the call to nfs4_state_set_mode_locked and
1637 * serialise the stateid update
1639 write_seqlock(&state->seqlock);
1640 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1643 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1646 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1648 case FMODE_READ|FMODE_WRITE:
1649 set_bit(NFS_O_RDWR_STATE, &state->flags);
1651 set_bit(NFS_OPEN_STATE, &state->flags);
1652 write_sequnlock(&state->seqlock);
1655 static void nfs_state_set_delegation(struct nfs4_state *state,
1656 const nfs4_stateid *deleg_stateid,
1660 * Protect the call to nfs4_state_set_mode_locked and
1661 * serialise the stateid update
1663 write_seqlock(&state->seqlock);
1664 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1665 set_bit(NFS_DELEGATED_STATE, &state->flags);
1666 write_sequnlock(&state->seqlock);
1669 static void nfs_state_clear_delegation(struct nfs4_state *state)
1671 write_seqlock(&state->seqlock);
1672 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1673 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1674 write_sequnlock(&state->seqlock);
1677 static int update_open_stateid(struct nfs4_state *state,
1678 const nfs4_stateid *open_stateid,
1679 const nfs4_stateid *delegation,
1682 struct nfs_server *server = NFS_SERVER(state->inode);
1683 struct nfs_client *clp = server->nfs_client;
1684 struct nfs_inode *nfsi = NFS_I(state->inode);
1685 struct nfs_delegation *deleg_cur;
1686 nfs4_stateid freeme = { };
1689 fmode &= (FMODE_READ|FMODE_WRITE);
1692 spin_lock(&state->owner->so_lock);
1693 if (open_stateid != NULL) {
1694 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1698 deleg_cur = rcu_dereference(nfsi->delegation);
1699 if (deleg_cur == NULL)
1702 spin_lock(&deleg_cur->lock);
1703 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1704 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1705 (deleg_cur->type & fmode) != fmode)
1706 goto no_delegation_unlock;
1708 if (delegation == NULL)
1709 delegation = &deleg_cur->stateid;
1710 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1711 goto no_delegation_unlock;
1713 nfs_mark_delegation_referenced(deleg_cur);
1714 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1716 no_delegation_unlock:
1717 spin_unlock(&deleg_cur->lock);
1720 update_open_stateflags(state, fmode);
1721 spin_unlock(&state->owner->so_lock);
1724 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1725 nfs4_schedule_state_manager(clp);
1726 if (freeme.type != 0)
1727 nfs4_test_and_free_stateid(server, &freeme,
1728 state->owner->so_cred);
1733 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1734 const nfs4_stateid *stateid)
1736 struct nfs4_state *state = lsp->ls_state;
1739 spin_lock(&state->state_lock);
1740 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1742 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1744 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1747 spin_unlock(&state->state_lock);
1751 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1753 struct nfs_delegation *delegation;
1755 fmode &= FMODE_READ|FMODE_WRITE;
1757 delegation = rcu_dereference(NFS_I(inode)->delegation);
1758 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1763 nfs4_inode_return_delegation(inode);
1766 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1768 struct nfs4_state *state = opendata->state;
1769 struct nfs_inode *nfsi = NFS_I(state->inode);
1770 struct nfs_delegation *delegation;
1771 int open_mode = opendata->o_arg.open_flags;
1772 fmode_t fmode = opendata->o_arg.fmode;
1773 enum open_claim_type4 claim = opendata->o_arg.claim;
1774 nfs4_stateid stateid;
1778 spin_lock(&state->owner->so_lock);
1779 if (can_open_cached(state, fmode, open_mode, claim)) {
1780 update_open_stateflags(state, fmode);
1781 spin_unlock(&state->owner->so_lock);
1782 goto out_return_state;
1784 spin_unlock(&state->owner->so_lock);
1786 delegation = rcu_dereference(nfsi->delegation);
1787 if (!can_open_delegated(delegation, fmode, claim)) {
1791 /* Save the delegation */
1792 nfs4_stateid_copy(&stateid, &delegation->stateid);
1794 nfs_release_seqid(opendata->o_arg.seqid);
1795 if (!opendata->is_recover) {
1796 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1802 /* Try to update the stateid using the delegation */
1803 if (update_open_stateid(state, NULL, &stateid, fmode))
1804 goto out_return_state;
1807 return ERR_PTR(ret);
1809 refcount_inc(&state->count);
1814 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1816 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1817 struct nfs_delegation *delegation;
1818 int delegation_flags = 0;
1821 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1823 delegation_flags = delegation->flags;
1825 switch (data->o_arg.claim) {
1828 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1829 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1830 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1831 "returning a delegation for "
1832 "OPEN(CLAIM_DELEGATE_CUR)\n",
1836 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1837 nfs_inode_set_delegation(state->inode,
1838 data->owner->so_cred,
1839 data->o_res.delegation_type,
1840 &data->o_res.delegation,
1841 data->o_res.pagemod_limit);
1843 nfs_inode_reclaim_delegation(state->inode,
1844 data->owner->so_cred,
1845 data->o_res.delegation_type,
1846 &data->o_res.delegation,
1847 data->o_res.pagemod_limit);
1849 if (data->o_res.do_recall)
1850 nfs_async_inode_return_delegation(state->inode,
1851 &data->o_res.delegation);
1855 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1856 * and update the nfs4_state.
1858 static struct nfs4_state *
1859 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1861 struct inode *inode = data->state->inode;
1862 struct nfs4_state *state = data->state;
1865 if (!data->rpc_done) {
1866 if (data->rpc_status)
1867 return ERR_PTR(data->rpc_status);
1868 /* cached opens have already been processed */
1872 ret = nfs_refresh_inode(inode, &data->f_attr);
1874 return ERR_PTR(ret);
1876 if (data->o_res.delegation_type != 0)
1877 nfs4_opendata_check_deleg(data, state);
1879 update_open_stateid(state, &data->o_res.stateid, NULL,
1881 refcount_inc(&state->count);
1886 static struct inode *
1887 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1889 struct inode *inode;
1891 switch (data->o_arg.claim) {
1892 case NFS4_OPEN_CLAIM_NULL:
1893 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1894 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1895 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1896 return ERR_PTR(-EAGAIN);
1897 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1898 &data->f_attr, data->f_label);
1901 inode = d_inode(data->dentry);
1903 nfs_refresh_inode(inode, &data->f_attr);
1908 static struct nfs4_state *
1909 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1911 struct nfs4_state *state;
1912 struct inode *inode;
1914 inode = nfs4_opendata_get_inode(data);
1916 return ERR_CAST(inode);
1917 if (data->state != NULL && data->state->inode == inode) {
1918 state = data->state;
1919 refcount_inc(&state->count);
1921 state = nfs4_get_open_state(inode, data->owner);
1924 state = ERR_PTR(-ENOMEM);
1928 static struct nfs4_state *
1929 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1931 struct nfs4_state *state;
1933 if (!data->rpc_done) {
1934 state = nfs4_try_open_cached(data);
1935 trace_nfs4_cached_open(data->state);
1939 state = nfs4_opendata_find_nfs4_state(data);
1943 if (data->o_res.delegation_type != 0)
1944 nfs4_opendata_check_deleg(data, state);
1945 update_open_stateid(state, &data->o_res.stateid, NULL,
1948 nfs_release_seqid(data->o_arg.seqid);
1952 static struct nfs4_state *
1953 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1955 struct nfs4_state *ret;
1957 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1958 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1960 ret = _nfs4_opendata_to_nfs4_state(data);
1961 nfs4_sequence_free_slot(&data->o_res.seq_res);
1965 static struct nfs_open_context *
1966 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
1968 struct nfs_inode *nfsi = NFS_I(state->inode);
1969 struct nfs_open_context *ctx;
1972 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
1973 if (ctx->state != state)
1975 if ((ctx->mode & mode) != mode)
1977 if (!get_nfs_open_context(ctx))
1983 return ERR_PTR(-ENOENT);
1986 static struct nfs_open_context *
1987 nfs4_state_find_open_context(struct nfs4_state *state)
1989 struct nfs_open_context *ctx;
1991 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
1994 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
1997 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2000 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2001 struct nfs4_state *state, enum open_claim_type4 claim)
2003 struct nfs4_opendata *opendata;
2005 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2006 NULL, claim, GFP_NOFS);
2007 if (opendata == NULL)
2008 return ERR_PTR(-ENOMEM);
2009 opendata->state = state;
2010 refcount_inc(&state->count);
2014 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2017 struct nfs4_state *newstate;
2020 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2022 opendata->o_arg.open_flags = 0;
2023 opendata->o_arg.fmode = fmode;
2024 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2025 NFS_SB(opendata->dentry->d_sb),
2027 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2028 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2029 nfs4_init_opendata_res(opendata);
2030 ret = _nfs4_recover_proc_open(opendata);
2033 newstate = nfs4_opendata_to_nfs4_state(opendata);
2034 if (IS_ERR(newstate))
2035 return PTR_ERR(newstate);
2036 if (newstate != opendata->state)
2038 nfs4_close_state(newstate, fmode);
2042 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2046 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
2047 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2048 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2049 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2050 /* memory barrier prior to reading state->n_* */
2051 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2052 clear_bit(NFS_OPEN_STATE, &state->flags);
2054 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2057 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2060 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2064 * We may have performed cached opens for all three recoveries.
2065 * Check if we need to update the current stateid.
2067 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2068 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2069 write_seqlock(&state->seqlock);
2070 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2071 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2072 write_sequnlock(&state->seqlock);
2079 * reclaim state on the server after a reboot.
2081 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2083 struct nfs_delegation *delegation;
2084 struct nfs4_opendata *opendata;
2085 fmode_t delegation_type = 0;
2088 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2089 NFS4_OPEN_CLAIM_PREVIOUS);
2090 if (IS_ERR(opendata))
2091 return PTR_ERR(opendata);
2093 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2094 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2095 delegation_type = delegation->type;
2097 opendata->o_arg.u.delegation_type = delegation_type;
2098 status = nfs4_open_recover(opendata, state);
2099 nfs4_opendata_put(opendata);
2103 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2105 struct nfs_server *server = NFS_SERVER(state->inode);
2106 struct nfs4_exception exception = { };
2109 err = _nfs4_do_open_reclaim(ctx, state);
2110 trace_nfs4_open_reclaim(ctx, 0, err);
2111 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2113 if (err != -NFS4ERR_DELAY)
2115 nfs4_handle_exception(server, err, &exception);
2116 } while (exception.retry);
2120 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2122 struct nfs_open_context *ctx;
2125 ctx = nfs4_state_find_open_context(state);
2128 ret = nfs4_do_open_reclaim(ctx, state);
2129 put_nfs_open_context(ctx);
2133 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)
2137 printk(KERN_ERR "NFS: %s: unhandled error "
2138 "%d.\n", __func__, err);
2144 case -NFS4ERR_BADSESSION:
2145 case -NFS4ERR_BADSLOT:
2146 case -NFS4ERR_BAD_HIGH_SLOT:
2147 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2148 case -NFS4ERR_DEADSESSION:
2149 set_bit(NFS_DELEGATED_STATE, &state->flags);
2150 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2152 case -NFS4ERR_STALE_CLIENTID:
2153 case -NFS4ERR_STALE_STATEID:
2154 set_bit(NFS_DELEGATED_STATE, &state->flags);
2155 /* Don't recall a delegation if it was lost */
2156 nfs4_schedule_lease_recovery(server->nfs_client);
2158 case -NFS4ERR_MOVED:
2159 nfs4_schedule_migration_recovery(server);
2161 case -NFS4ERR_LEASE_MOVED:
2162 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2164 case -NFS4ERR_DELEG_REVOKED:
2165 case -NFS4ERR_ADMIN_REVOKED:
2166 case -NFS4ERR_EXPIRED:
2167 case -NFS4ERR_BAD_STATEID:
2168 case -NFS4ERR_OPENMODE:
2169 nfs_inode_find_state_and_recover(state->inode,
2171 nfs4_schedule_stateid_recovery(server, state);
2173 case -NFS4ERR_DELAY:
2174 case -NFS4ERR_GRACE:
2175 set_bit(NFS_DELEGATED_STATE, &state->flags);
2179 case -NFS4ERR_DENIED:
2181 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2183 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2190 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2191 struct nfs4_state *state, const nfs4_stateid *stateid,
2194 struct nfs_server *server = NFS_SERVER(state->inode);
2195 struct nfs4_opendata *opendata;
2198 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2199 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2200 if (IS_ERR(opendata))
2201 return PTR_ERR(opendata);
2202 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2203 nfs_state_clear_delegation(state);
2204 switch (type & (FMODE_READ|FMODE_WRITE)) {
2205 case FMODE_READ|FMODE_WRITE:
2207 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2210 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2215 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2217 nfs4_opendata_put(opendata);
2218 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2221 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2223 struct nfs4_opendata *data = calldata;
2225 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2226 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2229 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2231 struct nfs4_opendata *data = calldata;
2233 nfs40_sequence_done(task, &data->c_res.seq_res);
2235 data->rpc_status = task->tk_status;
2236 if (data->rpc_status == 0) {
2237 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2238 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2239 renew_lease(data->o_res.server, data->timestamp);
2240 data->rpc_done = true;
2244 static void nfs4_open_confirm_release(void *calldata)
2246 struct nfs4_opendata *data = calldata;
2247 struct nfs4_state *state = NULL;
2249 /* If this request hasn't been cancelled, do nothing */
2250 if (!data->cancelled)
2252 /* In case of error, no cleanup! */
2253 if (!data->rpc_done)
2255 state = nfs4_opendata_to_nfs4_state(data);
2257 nfs4_close_state(state, data->o_arg.fmode);
2259 nfs4_opendata_put(data);
2262 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2263 .rpc_call_prepare = nfs4_open_confirm_prepare,
2264 .rpc_call_done = nfs4_open_confirm_done,
2265 .rpc_release = nfs4_open_confirm_release,
2269 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2271 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2273 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2274 struct rpc_task *task;
2275 struct rpc_message msg = {
2276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2277 .rpc_argp = &data->c_arg,
2278 .rpc_resp = &data->c_res,
2279 .rpc_cred = data->owner->so_cred,
2281 struct rpc_task_setup task_setup_data = {
2282 .rpc_client = server->client,
2283 .rpc_message = &msg,
2284 .callback_ops = &nfs4_open_confirm_ops,
2285 .callback_data = data,
2286 .workqueue = nfsiod_workqueue,
2287 .flags = RPC_TASK_ASYNC,
2291 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2293 kref_get(&data->kref);
2294 data->rpc_done = false;
2295 data->rpc_status = 0;
2296 data->timestamp = jiffies;
2297 task = rpc_run_task(&task_setup_data);
2299 return PTR_ERR(task);
2300 status = rpc_wait_for_completion_task(task);
2302 data->cancelled = true;
2305 status = data->rpc_status;
2310 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2312 struct nfs4_opendata *data = calldata;
2313 struct nfs4_state_owner *sp = data->owner;
2314 struct nfs_client *clp = sp->so_server->nfs_client;
2315 enum open_claim_type4 claim = data->o_arg.claim;
2317 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2320 * Check if we still need to send an OPEN call, or if we can use
2321 * a delegation instead.
2323 if (data->state != NULL) {
2324 struct nfs_delegation *delegation;
2326 if (can_open_cached(data->state, data->o_arg.fmode,
2327 data->o_arg.open_flags, claim))
2330 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2331 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2332 goto unlock_no_action;
2335 /* Update client id. */
2336 data->o_arg.clientid = clp->cl_clientid;
2340 case NFS4_OPEN_CLAIM_PREVIOUS:
2341 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2342 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2343 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2345 case NFS4_OPEN_CLAIM_FH:
2346 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2348 data->timestamp = jiffies;
2349 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2350 &data->o_arg.seq_args,
2351 &data->o_res.seq_res,
2353 nfs_release_seqid(data->o_arg.seqid);
2355 /* Set the create mode (note dependency on the session type) */
2356 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2357 if (data->o_arg.open_flags & O_EXCL) {
2358 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2359 if (nfs4_has_persistent_session(clp))
2360 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2361 else if (clp->cl_mvops->minor_version > 0)
2362 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2366 trace_nfs4_cached_open(data->state);
2369 task->tk_action = NULL;
2371 nfs4_sequence_done(task, &data->o_res.seq_res);
2374 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2376 struct nfs4_opendata *data = calldata;
2378 data->rpc_status = task->tk_status;
2380 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2383 if (task->tk_status == 0) {
2384 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2385 switch (data->o_res.f_attr->mode & S_IFMT) {
2389 data->rpc_status = -ELOOP;
2392 data->rpc_status = -EISDIR;
2395 data->rpc_status = -ENOTDIR;
2398 renew_lease(data->o_res.server, data->timestamp);
2399 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2400 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2402 data->rpc_done = true;
2405 static void nfs4_open_release(void *calldata)
2407 struct nfs4_opendata *data = calldata;
2408 struct nfs4_state *state = NULL;
2410 /* If this request hasn't been cancelled, do nothing */
2411 if (!data->cancelled)
2413 /* In case of error, no cleanup! */
2414 if (data->rpc_status != 0 || !data->rpc_done)
2416 /* In case we need an open_confirm, no cleanup! */
2417 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2419 state = nfs4_opendata_to_nfs4_state(data);
2421 nfs4_close_state(state, data->o_arg.fmode);
2423 nfs4_opendata_put(data);
2426 static const struct rpc_call_ops nfs4_open_ops = {
2427 .rpc_call_prepare = nfs4_open_prepare,
2428 .rpc_call_done = nfs4_open_done,
2429 .rpc_release = nfs4_open_release,
2432 static int nfs4_run_open_task(struct nfs4_opendata *data,
2433 struct nfs_open_context *ctx)
2435 struct inode *dir = d_inode(data->dir);
2436 struct nfs_server *server = NFS_SERVER(dir);
2437 struct nfs_openargs *o_arg = &data->o_arg;
2438 struct nfs_openres *o_res = &data->o_res;
2439 struct rpc_task *task;
2440 struct rpc_message msg = {
2441 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2444 .rpc_cred = data->owner->so_cred,
2446 struct rpc_task_setup task_setup_data = {
2447 .rpc_client = server->client,
2448 .rpc_message = &msg,
2449 .callback_ops = &nfs4_open_ops,
2450 .callback_data = data,
2451 .workqueue = nfsiod_workqueue,
2452 .flags = RPC_TASK_ASYNC,
2456 kref_get(&data->kref);
2457 data->rpc_done = false;
2458 data->rpc_status = 0;
2459 data->cancelled = false;
2460 data->is_recover = false;
2462 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2463 data->is_recover = true;
2465 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2466 pnfs_lgopen_prepare(data, ctx);
2468 task = rpc_run_task(&task_setup_data);
2470 return PTR_ERR(task);
2471 status = rpc_wait_for_completion_task(task);
2473 data->cancelled = true;
2476 status = data->rpc_status;
2482 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2484 struct inode *dir = d_inode(data->dir);
2485 struct nfs_openres *o_res = &data->o_res;
2488 status = nfs4_run_open_task(data, NULL);
2489 if (status != 0 || !data->rpc_done)
2492 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2494 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2495 status = _nfs4_proc_open_confirm(data);
2501 * Additional permission checks in order to distinguish between an
2502 * open for read, and an open for execute. This works around the
2503 * fact that NFSv4 OPEN treats read and execute permissions as being
2505 * Note that in the non-execute case, we want to turn off permission
2506 * checking if we just created a new file (POSIX open() semantics).
2508 static int nfs4_opendata_access(const struct cred *cred,
2509 struct nfs4_opendata *opendata,
2510 struct nfs4_state *state, fmode_t fmode,
2513 struct nfs_access_entry cache;
2516 /* access call failed or for some reason the server doesn't
2517 * support any access modes -- defer access call until later */
2518 if (opendata->o_res.access_supported == 0)
2523 * Use openflags to check for exec, because fmode won't
2524 * always have FMODE_EXEC set when file open for exec.
2526 if (openflags & __FMODE_EXEC) {
2527 /* ONLY check for exec rights */
2528 if (S_ISDIR(state->inode->i_mode))
2529 mask = NFS4_ACCESS_LOOKUP;
2531 mask = NFS4_ACCESS_EXECUTE;
2532 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2533 mask = NFS4_ACCESS_READ;
2536 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2537 nfs_access_add_cache(state->inode, &cache);
2539 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2540 if ((mask & ~cache.mask & flags) == 0)
2547 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2549 static int _nfs4_proc_open(struct nfs4_opendata *data,
2550 struct nfs_open_context *ctx)
2552 struct inode *dir = d_inode(data->dir);
2553 struct nfs_server *server = NFS_SERVER(dir);
2554 struct nfs_openargs *o_arg = &data->o_arg;
2555 struct nfs_openres *o_res = &data->o_res;
2558 status = nfs4_run_open_task(data, ctx);
2559 if (!data->rpc_done)
2562 if (status == -NFS4ERR_BADNAME &&
2563 !(o_arg->open_flags & O_CREAT))
2568 nfs_fattr_map_and_free_names(server, &data->f_attr);
2570 if (o_arg->open_flags & O_CREAT) {
2571 if (o_arg->open_flags & O_EXCL)
2572 data->file_created = true;
2573 else if (o_res->cinfo.before != o_res->cinfo.after)
2574 data->file_created = true;
2575 if (data->file_created ||
2576 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2577 update_changeattr(dir, &o_res->cinfo,
2578 o_res->f_attr->time_start, 0);
2580 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2581 server->caps &= ~NFS_CAP_POSIX_LOCK;
2582 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2583 status = _nfs4_proc_open_confirm(data);
2587 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2588 nfs4_sequence_free_slot(&o_res->seq_res);
2589 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2590 o_res->f_label, NULL);
2597 * reclaim state on the server after a network partition.
2598 * Assumes caller holds the appropriate lock
2600 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2602 struct nfs4_opendata *opendata;
2605 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2606 NFS4_OPEN_CLAIM_FH);
2607 if (IS_ERR(opendata))
2608 return PTR_ERR(opendata);
2609 ret = nfs4_open_recover(opendata, state);
2611 d_drop(ctx->dentry);
2612 nfs4_opendata_put(opendata);
2616 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2618 struct nfs_server *server = NFS_SERVER(state->inode);
2619 struct nfs4_exception exception = { };
2623 err = _nfs4_open_expired(ctx, state);
2624 trace_nfs4_open_expired(ctx, 0, err);
2625 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2630 case -NFS4ERR_GRACE:
2631 case -NFS4ERR_DELAY:
2632 nfs4_handle_exception(server, err, &exception);
2635 } while (exception.retry);
2640 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2642 struct nfs_open_context *ctx;
2645 ctx = nfs4_state_find_open_context(state);
2648 ret = nfs4_do_open_expired(ctx, state);
2649 put_nfs_open_context(ctx);
2653 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2654 const nfs4_stateid *stateid)
2656 nfs_remove_bad_delegation(state->inode, stateid);
2657 nfs_state_clear_delegation(state);
2660 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2662 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2663 nfs_finish_clear_delegation_stateid(state, NULL);
2666 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2668 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2669 nfs40_clear_delegation_stateid(state);
2670 return nfs4_open_expired(sp, state);
2673 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2674 nfs4_stateid *stateid,
2675 const struct cred *cred)
2677 return -NFS4ERR_BAD_STATEID;
2680 #if defined(CONFIG_NFS_V4_1)
2681 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2682 nfs4_stateid *stateid,
2683 const struct cred *cred)
2687 switch (stateid->type) {
2690 case NFS4_INVALID_STATEID_TYPE:
2691 case NFS4_SPECIAL_STATEID_TYPE:
2692 return -NFS4ERR_BAD_STATEID;
2693 case NFS4_REVOKED_STATEID_TYPE:
2697 status = nfs41_test_stateid(server, stateid, cred);
2699 case -NFS4ERR_EXPIRED:
2700 case -NFS4ERR_ADMIN_REVOKED:
2701 case -NFS4ERR_DELEG_REVOKED:
2707 /* Ack the revoked state to the server */
2708 nfs41_free_stateid(server, stateid, cred, true);
2709 return -NFS4ERR_EXPIRED;
2712 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2714 struct nfs_server *server = NFS_SERVER(state->inode);
2715 nfs4_stateid stateid;
2716 struct nfs_delegation *delegation;
2717 const struct cred *cred = NULL;
2720 /* Get the delegation credential for use by test/free_stateid */
2722 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2723 if (delegation == NULL) {
2725 nfs_state_clear_delegation(state);
2729 nfs4_stateid_copy(&stateid, &delegation->stateid);
2730 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2732 nfs_state_clear_delegation(state);
2736 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2737 &delegation->flags)) {
2742 if (delegation->cred)
2743 cred = get_cred(delegation->cred);
2745 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2746 trace_nfs4_test_delegation_stateid(state, NULL, status);
2747 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2748 nfs_finish_clear_delegation_stateid(state, &stateid);
2750 if (delegation->cred)
2755 * nfs41_check_expired_locks - possibly free a lock stateid
2757 * @state: NFSv4 state for an inode
2759 * Returns NFS_OK if recovery for this stateid is now finished.
2760 * Otherwise a negative NFS4ERR value is returned.
2762 static int nfs41_check_expired_locks(struct nfs4_state *state)
2764 int status, ret = NFS_OK;
2765 struct nfs4_lock_state *lsp, *prev = NULL;
2766 struct nfs_server *server = NFS_SERVER(state->inode);
2768 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2771 spin_lock(&state->state_lock);
2772 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2773 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2774 const struct cred *cred = lsp->ls_state->owner->so_cred;
2776 refcount_inc(&lsp->ls_count);
2777 spin_unlock(&state->state_lock);
2779 nfs4_put_lock_state(prev);
2782 status = nfs41_test_and_free_expired_stateid(server,
2785 trace_nfs4_test_lock_stateid(state, lsp, status);
2786 if (status == -NFS4ERR_EXPIRED ||
2787 status == -NFS4ERR_BAD_STATEID) {
2788 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2789 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2790 if (!recover_lost_locks)
2791 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2792 } else if (status != NFS_OK) {
2794 nfs4_put_lock_state(prev);
2797 spin_lock(&state->state_lock);
2800 spin_unlock(&state->state_lock);
2801 nfs4_put_lock_state(prev);
2807 * nfs41_check_open_stateid - possibly free an open stateid
2809 * @state: NFSv4 state for an inode
2811 * Returns NFS_OK if recovery for this stateid is now finished.
2812 * Otherwise a negative NFS4ERR value is returned.
2814 static int nfs41_check_open_stateid(struct nfs4_state *state)
2816 struct nfs_server *server = NFS_SERVER(state->inode);
2817 nfs4_stateid *stateid = &state->open_stateid;
2818 const struct cred *cred = state->owner->so_cred;
2821 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2822 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2823 if (nfs4_have_delegation(state->inode, state->state))
2825 return -NFS4ERR_OPENMODE;
2827 return -NFS4ERR_BAD_STATEID;
2829 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2830 trace_nfs4_test_open_stateid(state, NULL, status);
2831 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2832 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2833 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2834 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2835 clear_bit(NFS_OPEN_STATE, &state->flags);
2836 stateid->type = NFS4_INVALID_STATEID_TYPE;
2839 if (nfs_open_stateid_recover_openmode(state))
2840 return -NFS4ERR_OPENMODE;
2844 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2848 nfs41_check_delegation_stateid(state);
2849 status = nfs41_check_expired_locks(state);
2850 if (status != NFS_OK)
2852 status = nfs41_check_open_stateid(state);
2853 if (status != NFS_OK)
2854 status = nfs4_open_expired(sp, state);
2860 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2861 * fields corresponding to attributes that were used to store the verifier.
2862 * Make sure we clobber those fields in the later setattr call
2864 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2865 struct iattr *sattr, struct nfs4_label **label)
2867 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2872 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2873 attrset[i] = opendata->o_res.attrset[i];
2874 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2875 attrset[i] &= ~bitmask[i];
2878 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2879 sattr->ia_valid : 0;
2881 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2882 if (sattr->ia_valid & ATTR_ATIME_SET)
2883 ret |= ATTR_ATIME_SET;
2888 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2889 if (sattr->ia_valid & ATTR_MTIME_SET)
2890 ret |= ATTR_MTIME_SET;
2895 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
2900 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2903 struct nfs_open_context *ctx)
2905 struct nfs4_state_owner *sp = opendata->owner;
2906 struct nfs_server *server = sp->so_server;
2907 struct dentry *dentry;
2908 struct nfs4_state *state;
2912 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2914 ret = _nfs4_proc_open(opendata, ctx);
2918 state = _nfs4_opendata_to_nfs4_state(opendata);
2919 ret = PTR_ERR(state);
2923 if (server->caps & NFS_CAP_POSIX_LOCK)
2924 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2925 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2926 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2928 dentry = opendata->dentry;
2929 if (d_really_is_negative(dentry)) {
2930 struct dentry *alias;
2932 alias = d_exact_alias(dentry, state->inode);
2934 alias = d_splice_alias(igrab(state->inode), dentry);
2935 /* d_splice_alias() can't fail here - it's a non-directory */
2938 ctx->dentry = dentry = alias;
2940 nfs_set_verifier(dentry,
2941 nfs_save_change_attribute(d_inode(opendata->dir)));
2944 /* Parse layoutget results before we check for access */
2945 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2947 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2951 if (d_inode(dentry) == state->inode) {
2952 nfs_inode_attach_open_context(ctx);
2953 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2954 nfs4_schedule_stateid_recovery(server, state);
2958 if (!opendata->cancelled)
2959 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
2964 * Returns a referenced nfs4_state
2966 static int _nfs4_do_open(struct inode *dir,
2967 struct nfs_open_context *ctx,
2969 const struct nfs4_open_createattrs *c,
2972 struct nfs4_state_owner *sp;
2973 struct nfs4_state *state = NULL;
2974 struct nfs_server *server = NFS_SERVER(dir);
2975 struct nfs4_opendata *opendata;
2976 struct dentry *dentry = ctx->dentry;
2977 const struct cred *cred = ctx->cred;
2978 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2979 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2980 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2981 struct iattr *sattr = c->sattr;
2982 struct nfs4_label *label = c->label;
2983 struct nfs4_label *olabel = NULL;
2986 /* Protect against reboot recovery conflicts */
2988 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2990 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2993 status = nfs4_client_recover_expired_lease(server->nfs_client);
2995 goto err_put_state_owner;
2996 if (d_really_is_positive(dentry))
2997 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2999 if (d_really_is_positive(dentry))
3000 claim = NFS4_OPEN_CLAIM_FH;
3001 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3002 c, claim, GFP_KERNEL);
3003 if (opendata == NULL)
3004 goto err_put_state_owner;
3007 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3008 if (IS_ERR(olabel)) {
3009 status = PTR_ERR(olabel);
3010 goto err_opendata_put;
3014 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3015 if (!opendata->f_attr.mdsthreshold) {
3016 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3017 if (!opendata->f_attr.mdsthreshold)
3018 goto err_free_label;
3020 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3022 if (d_really_is_positive(dentry))
3023 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3025 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
3027 goto err_free_label;
3030 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3031 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3032 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3034 * send create attributes which was not set by open
3035 * with an extra setattr.
3037 if (attrs || label) {
3038 unsigned ia_old = sattr->ia_valid;
3040 sattr->ia_valid = attrs;
3041 nfs_fattr_init(opendata->o_res.f_attr);
3042 status = nfs4_do_setattr(state->inode, cred,
3043 opendata->o_res.f_attr, sattr,
3044 ctx, label, olabel);
3046 nfs_setattr_update_inode(state->inode, sattr,
3047 opendata->o_res.f_attr);
3048 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3050 sattr->ia_valid = ia_old;
3053 if (opened && opendata->file_created)
3056 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3057 *ctx_th = opendata->f_attr.mdsthreshold;
3058 opendata->f_attr.mdsthreshold = NULL;
3061 nfs4_label_free(olabel);
3063 nfs4_opendata_put(opendata);
3064 nfs4_put_state_owner(sp);
3067 nfs4_label_free(olabel);
3069 nfs4_opendata_put(opendata);
3070 err_put_state_owner:
3071 nfs4_put_state_owner(sp);
3077 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3078 struct nfs_open_context *ctx,
3080 struct iattr *sattr,
3081 struct nfs4_label *label,
3084 struct nfs_server *server = NFS_SERVER(dir);
3085 struct nfs4_exception exception = {
3086 .interruptible = true,
3088 struct nfs4_state *res;
3089 struct nfs4_open_createattrs c = {
3093 [0] = (__u32)jiffies,
3094 [1] = (__u32)current->pid,
3100 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3102 trace_nfs4_open_file(ctx, flags, status);
3105 /* NOTE: BAD_SEQID means the server and client disagree about the
3106 * book-keeping w.r.t. state-changing operations
3107 * (OPEN/CLOSE/LOCK/LOCKU...)
3108 * It is actually a sign of a bug on the client or on the server.
3110 * If we receive a BAD_SEQID error in the particular case of
3111 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3112 * have unhashed the old state_owner for us, and that we can
3113 * therefore safely retry using a new one. We should still warn
3114 * the user though...
3116 if (status == -NFS4ERR_BAD_SEQID) {
3117 pr_warn_ratelimited("NFS: v4 server %s "
3118 " returned a bad sequence-id error!\n",
3119 NFS_SERVER(dir)->nfs_client->cl_hostname);
3120 exception.retry = 1;
3124 * BAD_STATEID on OPEN means that the server cancelled our
3125 * state before it received the OPEN_CONFIRM.
3126 * Recover by retrying the request as per the discussion
3127 * on Page 181 of RFC3530.
3129 if (status == -NFS4ERR_BAD_STATEID) {
3130 exception.retry = 1;
3133 if (status == -EAGAIN) {
3134 /* We must have found a delegation */
3135 exception.retry = 1;
3138 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3140 res = ERR_PTR(nfs4_handle_exception(server,
3141 status, &exception));
3142 } while (exception.retry);
3146 static int _nfs4_do_setattr(struct inode *inode,
3147 struct nfs_setattrargs *arg,
3148 struct nfs_setattrres *res,
3149 const struct cred *cred,
3150 struct nfs_open_context *ctx)
3152 struct nfs_server *server = NFS_SERVER(inode);
3153 struct rpc_message msg = {
3154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3159 const struct cred *delegation_cred = NULL;
3160 unsigned long timestamp = jiffies;
3164 nfs_fattr_init(res->fattr);
3166 /* Servers should only apply open mode checks for file size changes */
3167 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3171 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3172 /* Use that stateid */
3173 } else if (ctx != NULL) {
3174 struct nfs_lock_context *l_ctx;
3175 if (!nfs4_valid_open_stateid(ctx->state))
3177 l_ctx = nfs_get_lock_context(ctx);
3179 return PTR_ERR(l_ctx);
3180 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3181 &arg->stateid, &delegation_cred);
3182 nfs_put_lock_context(l_ctx);
3187 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3189 if (delegation_cred)
3190 msg.rpc_cred = delegation_cred;
3192 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3194 put_cred(delegation_cred);
3195 if (status == 0 && ctx != NULL)
3196 renew_lease(server, timestamp);
3197 trace_nfs4_setattr(inode, &arg->stateid, status);
3201 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3202 struct nfs_fattr *fattr, struct iattr *sattr,
3203 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3204 struct nfs4_label *olabel)
3206 struct nfs_server *server = NFS_SERVER(inode);
3207 __u32 bitmask[NFS4_BITMASK_SZ];
3208 struct nfs4_state *state = ctx ? ctx->state : NULL;
3209 struct nfs_setattrargs arg = {
3210 .fh = NFS_FH(inode),
3216 struct nfs_setattrres res = {
3221 struct nfs4_exception exception = {
3224 .stateid = &arg.stateid,
3229 nfs4_bitmap_copy_adjust_setattr(bitmask,
3230 nfs4_bitmask(server, olabel),
3233 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3235 case -NFS4ERR_OPENMODE:
3236 if (!(sattr->ia_valid & ATTR_SIZE)) {
3237 pr_warn_once("NFSv4: server %s is incorrectly "
3238 "applying open mode checks to "
3239 "a SETATTR that is not "
3240 "changing file size.\n",
3241 server->nfs_client->cl_hostname);
3243 if (state && !(state->state & FMODE_WRITE)) {
3245 if (sattr->ia_valid & ATTR_OPEN)
3250 err = nfs4_handle_exception(server, err, &exception);
3251 } while (exception.retry);
3257 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3259 if (inode == NULL || !nfs_have_layout(inode))
3262 return pnfs_wait_on_layoutreturn(inode, task);
3265 struct nfs4_closedata {
3266 struct inode *inode;
3267 struct nfs4_state *state;
3268 struct nfs_closeargs arg;
3269 struct nfs_closeres res;
3271 struct nfs4_layoutreturn_args arg;
3272 struct nfs4_layoutreturn_res res;
3273 struct nfs4_xdr_opaque_data ld_private;
3277 struct nfs_fattr fattr;
3278 unsigned long timestamp;
3281 static void nfs4_free_closedata(void *data)
3283 struct nfs4_closedata *calldata = data;
3284 struct nfs4_state_owner *sp = calldata->state->owner;
3285 struct super_block *sb = calldata->state->inode->i_sb;
3287 if (calldata->lr.roc)
3288 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3289 calldata->res.lr_ret);
3290 nfs4_put_open_state(calldata->state);
3291 nfs_free_seqid(calldata->arg.seqid);
3292 nfs4_put_state_owner(sp);
3293 nfs_sb_deactive(sb);
3297 static void nfs4_close_done(struct rpc_task *task, void *data)
3299 struct nfs4_closedata *calldata = data;
3300 struct nfs4_state *state = calldata->state;
3301 struct nfs_server *server = NFS_SERVER(calldata->inode);
3302 nfs4_stateid *res_stateid = NULL;
3303 struct nfs4_exception exception = {
3305 .inode = calldata->inode,
3306 .stateid = &calldata->arg.stateid,
3309 dprintk("%s: begin!\n", __func__);
3310 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3312 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3314 /* Handle Layoutreturn errors */
3315 if (calldata->arg.lr_args && task->tk_status != 0) {
3316 switch (calldata->res.lr_ret) {
3318 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3321 calldata->arg.lr_args = NULL;
3322 calldata->res.lr_res = NULL;
3324 case -NFS4ERR_OLD_STATEID:
3325 if (nfs4_layoutreturn_refresh_stateid(&calldata->arg.lr_args->stateid,
3326 &calldata->arg.lr_args->range,
3330 case -NFS4ERR_ADMIN_REVOKED:
3331 case -NFS4ERR_DELEG_REVOKED:
3332 case -NFS4ERR_EXPIRED:
3333 case -NFS4ERR_BAD_STATEID:
3334 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3335 case -NFS4ERR_WRONG_CRED:
3336 calldata->arg.lr_args = NULL;
3337 calldata->res.lr_res = NULL;
3342 /* hmm. we are done with the inode, and in the process of freeing
3343 * the state_owner. we keep this around to process errors
3345 switch (task->tk_status) {
3347 res_stateid = &calldata->res.stateid;
3348 renew_lease(server, calldata->timestamp);
3350 case -NFS4ERR_ACCESS:
3351 if (calldata->arg.bitmask != NULL) {
3352 calldata->arg.bitmask = NULL;
3353 calldata->res.fattr = NULL;
3358 case -NFS4ERR_OLD_STATEID:
3359 /* Did we race with OPEN? */
3360 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
3364 case -NFS4ERR_ADMIN_REVOKED:
3365 case -NFS4ERR_STALE_STATEID:
3366 case -NFS4ERR_EXPIRED:
3367 nfs4_free_revoked_stateid(server,
3368 &calldata->arg.stateid,
3369 task->tk_msg.rpc_cred);
3371 case -NFS4ERR_BAD_STATEID:
3374 task->tk_status = nfs4_async_handle_exception(task,
3375 server, task->tk_status, &exception);
3376 if (exception.retry)
3379 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3380 res_stateid, calldata->arg.fmode);
3382 task->tk_status = 0;
3383 nfs_release_seqid(calldata->arg.seqid);
3384 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3385 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3388 calldata->res.lr_ret = 0;
3390 task->tk_status = 0;
3391 rpc_restart_call_prepare(task);
3395 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3397 struct nfs4_closedata *calldata = data;
3398 struct nfs4_state *state = calldata->state;
3399 struct inode *inode = calldata->inode;
3400 struct pnfs_layout_hdr *lo;
3401 bool is_rdonly, is_wronly, is_rdwr;
3404 dprintk("%s: begin!\n", __func__);
3405 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3408 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3409 spin_lock(&state->owner->so_lock);
3410 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3411 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3412 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3413 /* Calculate the change in open mode */
3414 calldata->arg.fmode = 0;
3415 if (state->n_rdwr == 0) {
3416 if (state->n_rdonly == 0)
3417 call_close |= is_rdonly;
3419 calldata->arg.fmode |= FMODE_READ;
3420 if (state->n_wronly == 0)
3421 call_close |= is_wronly;
3423 calldata->arg.fmode |= FMODE_WRITE;
3424 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3425 call_close |= is_rdwr;
3427 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3429 if (!nfs4_valid_open_stateid(state) ||
3430 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
3432 spin_unlock(&state->owner->so_lock);
3435 /* Note: exit _without_ calling nfs4_close_done */
3439 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3440 nfs_release_seqid(calldata->arg.seqid);
3444 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3445 if (lo && !pnfs_layout_is_valid(lo)) {
3446 calldata->arg.lr_args = NULL;
3447 calldata->res.lr_res = NULL;
3450 if (calldata->arg.fmode == 0)
3451 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3453 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3454 /* Close-to-open cache consistency revalidation */
3455 if (!nfs4_have_delegation(inode, FMODE_READ))
3456 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3458 calldata->arg.bitmask = NULL;
3461 calldata->arg.share_access =
3462 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3463 calldata->arg.fmode, 0);
3465 if (calldata->res.fattr == NULL)
3466 calldata->arg.bitmask = NULL;
3467 else if (calldata->arg.bitmask == NULL)
3468 calldata->res.fattr = NULL;
3469 calldata->timestamp = jiffies;
3470 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3471 &calldata->arg.seq_args,
3472 &calldata->res.seq_res,
3474 nfs_release_seqid(calldata->arg.seqid);
3475 dprintk("%s: done!\n", __func__);
3478 task->tk_action = NULL;
3480 nfs4_sequence_done(task, &calldata->res.seq_res);
3483 static const struct rpc_call_ops nfs4_close_ops = {
3484 .rpc_call_prepare = nfs4_close_prepare,
3485 .rpc_call_done = nfs4_close_done,
3486 .rpc_release = nfs4_free_closedata,
3490 * It is possible for data to be read/written from a mem-mapped file
3491 * after the sys_close call (which hits the vfs layer as a flush).
3492 * This means that we can't safely call nfsv4 close on a file until
3493 * the inode is cleared. This in turn means that we are not good
3494 * NFSv4 citizens - we do not indicate to the server to update the file's
3495 * share state even when we are done with one of the three share
3496 * stateid's in the inode.
3498 * NOTE: Caller must be holding the sp->so_owner semaphore!
3500 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3502 struct nfs_server *server = NFS_SERVER(state->inode);
3503 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3504 struct nfs4_closedata *calldata;
3505 struct nfs4_state_owner *sp = state->owner;
3506 struct rpc_task *task;
3507 struct rpc_message msg = {
3508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3509 .rpc_cred = state->owner->so_cred,
3511 struct rpc_task_setup task_setup_data = {
3512 .rpc_client = server->client,
3513 .rpc_message = &msg,
3514 .callback_ops = &nfs4_close_ops,
3515 .workqueue = nfsiod_workqueue,
3516 .flags = RPC_TASK_ASYNC,
3518 int status = -ENOMEM;
3520 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3521 &task_setup_data.rpc_client, &msg);
3523 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3524 if (calldata == NULL)
3526 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3527 calldata->inode = state->inode;
3528 calldata->state = state;
3529 calldata->arg.fh = NFS_FH(state->inode);
3530 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3531 goto out_free_calldata;
3532 /* Serialization for the sequence id */
3533 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3534 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3535 if (IS_ERR(calldata->arg.seqid))
3536 goto out_free_calldata;
3537 nfs_fattr_init(&calldata->fattr);
3538 calldata->arg.fmode = 0;
3539 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3540 calldata->res.fattr = &calldata->fattr;
3541 calldata->res.seqid = calldata->arg.seqid;
3542 calldata->res.server = server;
3543 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3544 calldata->lr.roc = pnfs_roc(state->inode,
3545 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3546 if (calldata->lr.roc) {
3547 calldata->arg.lr_args = &calldata->lr.arg;
3548 calldata->res.lr_res = &calldata->lr.res;
3550 nfs_sb_active(calldata->inode->i_sb);
3552 msg.rpc_argp = &calldata->arg;
3553 msg.rpc_resp = &calldata->res;
3554 task_setup_data.callback_data = calldata;
3555 task = rpc_run_task(&task_setup_data);
3557 return PTR_ERR(task);
3560 status = rpc_wait_for_completion_task(task);
3566 nfs4_put_open_state(state);
3567 nfs4_put_state_owner(sp);
3571 static struct inode *
3572 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3573 int open_flags, struct iattr *attr, int *opened)
3575 struct nfs4_state *state;
3576 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3578 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3580 /* Protect against concurrent sillydeletes */
3581 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3583 nfs4_label_release_security(label);
3586 return ERR_CAST(state);
3587 return state->inode;
3590 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3592 if (ctx->state == NULL)
3595 nfs4_close_sync(ctx->state, ctx->mode);
3597 nfs4_close_state(ctx->state, ctx->mode);
3600 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3601 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3602 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3604 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3606 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3607 struct nfs4_server_caps_arg args = {
3611 struct nfs4_server_caps_res res = {};
3612 struct rpc_message msg = {
3613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3620 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3621 FATTR4_WORD0_FH_EXPIRE_TYPE |
3622 FATTR4_WORD0_LINK_SUPPORT |
3623 FATTR4_WORD0_SYMLINK_SUPPORT |
3624 FATTR4_WORD0_ACLSUPPORT;
3626 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3628 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3630 /* Sanity check the server answers */
3631 switch (minorversion) {
3633 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3634 res.attr_bitmask[2] = 0;
3637 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3640 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3642 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3643 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3644 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3645 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3646 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3647 NFS_CAP_CTIME|NFS_CAP_MTIME|
3648 NFS_CAP_SECURITY_LABEL);
3649 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3650 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3651 server->caps |= NFS_CAP_ACLS;
3652 if (res.has_links != 0)
3653 server->caps |= NFS_CAP_HARDLINKS;
3654 if (res.has_symlinks != 0)
3655 server->caps |= NFS_CAP_SYMLINKS;
3656 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3657 server->caps |= NFS_CAP_FILEID;
3658 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3659 server->caps |= NFS_CAP_MODE;
3660 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3661 server->caps |= NFS_CAP_NLINK;
3662 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3663 server->caps |= NFS_CAP_OWNER;
3664 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3665 server->caps |= NFS_CAP_OWNER_GROUP;
3666 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3667 server->caps |= NFS_CAP_ATIME;
3668 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3669 server->caps |= NFS_CAP_CTIME;
3670 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3671 server->caps |= NFS_CAP_MTIME;
3672 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3673 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3674 server->caps |= NFS_CAP_SECURITY_LABEL;
3676 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3677 sizeof(server->attr_bitmask));
3678 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3680 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3681 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3682 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3683 server->cache_consistency_bitmask[2] = 0;
3685 /* Avoid a regression due to buggy server */
3686 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3687 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3688 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3689 sizeof(server->exclcreat_bitmask));
3691 server->acl_bitmask = res.acl_bitmask;
3692 server->fh_expire_type = res.fh_expire_type;
3698 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3700 struct nfs4_exception exception = {
3701 .interruptible = true,
3705 err = nfs4_handle_exception(server,
3706 _nfs4_server_capabilities(server, fhandle),
3708 } while (exception.retry);
3712 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3713 struct nfs_fsinfo *info)
3716 struct nfs4_lookup_root_arg args = {
3719 struct nfs4_lookup_res res = {
3721 .fattr = info->fattr,
3724 struct rpc_message msg = {
3725 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3730 bitmask[0] = nfs4_fattr_bitmap[0];
3731 bitmask[1] = nfs4_fattr_bitmap[1];
3733 * Process the label in the upcoming getfattr
3735 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3737 nfs_fattr_init(info->fattr);
3738 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3741 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3742 struct nfs_fsinfo *info)
3744 struct nfs4_exception exception = {
3745 .interruptible = true,
3749 err = _nfs4_lookup_root(server, fhandle, info);
3750 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3753 case -NFS4ERR_WRONGSEC:
3756 err = nfs4_handle_exception(server, err, &exception);
3758 } while (exception.retry);
3763 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3764 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3766 struct rpc_auth_create_args auth_args = {
3767 .pseudoflavor = flavor,
3769 struct rpc_auth *auth;
3771 auth = rpcauth_create(&auth_args, server->client);
3774 return nfs4_lookup_root(server, fhandle, info);
3778 * Retry pseudoroot lookup with various security flavors. We do this when:
3780 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3781 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3783 * Returns zero on success, or a negative NFS4ERR value, or a
3784 * negative errno value.
3786 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3787 struct nfs_fsinfo *info)
3789 /* Per 3530bis 15.33.5 */
3790 static const rpc_authflavor_t flav_array[] = {
3794 RPC_AUTH_UNIX, /* courtesy */
3797 int status = -EPERM;
3800 if (server->auth_info.flavor_len > 0) {
3801 /* try each flavor specified by user */
3802 for (i = 0; i < server->auth_info.flavor_len; i++) {
3803 status = nfs4_lookup_root_sec(server, fhandle, info,
3804 server->auth_info.flavors[i]);
3805 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3810 /* no flavors specified by user, try default list */
3811 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3812 status = nfs4_lookup_root_sec(server, fhandle, info,
3814 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3821 * -EACCES could mean that the user doesn't have correct permissions
3822 * to access the mount. It could also mean that we tried to mount
3823 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3824 * existing mount programs don't handle -EACCES very well so it should
3825 * be mapped to -EPERM instead.
3827 if (status == -EACCES)
3833 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3834 * @server: initialized nfs_server handle
3835 * @fhandle: we fill in the pseudo-fs root file handle
3836 * @info: we fill in an FSINFO struct
3837 * @auth_probe: probe the auth flavours
3839 * Returns zero on success, or a negative errno.
3841 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3842 struct nfs_fsinfo *info,
3848 status = nfs4_lookup_root(server, fhandle, info);
3850 if (auth_probe || status == NFS4ERR_WRONGSEC)
3851 status = server->nfs_client->cl_mvops->find_root_sec(server,
3855 status = nfs4_server_capabilities(server, fhandle);
3857 status = nfs4_do_fsinfo(server, fhandle, info);
3859 return nfs4_map_errors(status);
3862 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3863 struct nfs_fsinfo *info)
3866 struct nfs_fattr *fattr = info->fattr;
3867 struct nfs4_label *label = NULL;
3869 error = nfs4_server_capabilities(server, mntfh);
3871 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3875 label = nfs4_label_alloc(server, GFP_KERNEL);
3877 return PTR_ERR(label);
3879 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
3881 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3882 goto err_free_label;
3885 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3886 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3887 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3890 nfs4_label_free(label);
3896 * Get locations and (maybe) other attributes of a referral.
3897 * Note that we'll actually follow the referral later when
3898 * we detect fsid mismatch in inode revalidation
3900 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3901 const struct qstr *name, struct nfs_fattr *fattr,
3902 struct nfs_fh *fhandle)
3904 int status = -ENOMEM;
3905 struct page *page = NULL;
3906 struct nfs4_fs_locations *locations = NULL;
3908 page = alloc_page(GFP_KERNEL);
3911 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3912 if (locations == NULL)
3915 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3920 * If the fsid didn't change, this is a migration event, not a
3921 * referral. Cause us to drop into the exception handler, which
3922 * will kick off migration recovery.
3924 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3925 dprintk("%s: server did not return a different fsid for"
3926 " a referral at %s\n", __func__, name->name);
3927 status = -NFS4ERR_MOVED;
3930 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3931 nfs_fixup_referral_attributes(&locations->fattr);
3933 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3934 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3935 memset(fhandle, 0, sizeof(struct nfs_fh));
3943 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3944 struct nfs_fattr *fattr, struct nfs4_label *label,
3945 struct inode *inode)
3947 __u32 bitmask[NFS4_BITMASK_SZ];
3948 struct nfs4_getattr_arg args = {
3952 struct nfs4_getattr_res res = {
3957 struct rpc_message msg = {
3958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3963 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
3965 nfs_fattr_init(fattr);
3966 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3969 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3970 struct nfs_fattr *fattr, struct nfs4_label *label,
3971 struct inode *inode)
3973 struct nfs4_exception exception = {
3974 .interruptible = true,
3978 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
3979 trace_nfs4_getattr(server, fhandle, fattr, err);
3980 err = nfs4_handle_exception(server, err,
3982 } while (exception.retry);
3987 * The file is not closed if it is opened due to the a request to change
3988 * the size of the file. The open call will not be needed once the
3989 * VFS layer lookup-intents are implemented.
3991 * Close is called when the inode is destroyed.
3992 * If we haven't opened the file for O_WRONLY, we
3993 * need to in the size_change case to obtain a stateid.
3996 * Because OPEN is always done by name in nfsv4, it is
3997 * possible that we opened a different file by the same
3998 * name. We can recognize this race condition, but we
3999 * can't do anything about it besides returning an error.
4001 * This will be fixed with VFS changes (lookup-intent).
4004 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4005 struct iattr *sattr)
4007 struct inode *inode = d_inode(dentry);
4008 const struct cred *cred = NULL;
4009 struct nfs_open_context *ctx = NULL;
4010 struct nfs4_label *label = NULL;
4013 if (pnfs_ld_layoutret_on_setattr(inode) &&
4014 sattr->ia_valid & ATTR_SIZE &&
4015 sattr->ia_size < i_size_read(inode))
4016 pnfs_commit_and_return_layout(inode);
4018 nfs_fattr_init(fattr);
4020 /* Deal with open(O_TRUNC) */
4021 if (sattr->ia_valid & ATTR_OPEN)
4022 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4024 /* Optimization: if the end result is no change, don't RPC */
4025 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4028 /* Search for an existing open(O_WRITE) file */
4029 if (sattr->ia_valid & ATTR_FILE) {
4031 ctx = nfs_file_open_context(sattr->ia_file);
4036 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4038 return PTR_ERR(label);
4040 /* Return any delegations if we're going to change ACLs */
4041 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4042 nfs4_inode_make_writeable(inode);
4044 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4046 nfs_setattr_update_inode(inode, sattr, fattr);
4047 nfs_setsecurity(inode, fattr, label);
4049 nfs4_label_free(label);
4053 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4054 const struct qstr *name, struct nfs_fh *fhandle,
4055 struct nfs_fattr *fattr, struct nfs4_label *label)
4057 struct nfs_server *server = NFS_SERVER(dir);
4059 struct nfs4_lookup_arg args = {
4060 .bitmask = server->attr_bitmask,
4061 .dir_fh = NFS_FH(dir),
4064 struct nfs4_lookup_res res = {
4070 struct rpc_message msg = {
4071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4076 args.bitmask = nfs4_bitmask(server, label);
4078 nfs_fattr_init(fattr);
4080 dprintk("NFS call lookup %s\n", name->name);
4081 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
4082 dprintk("NFS reply lookup: %d\n", status);
4086 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4088 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4089 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4090 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4094 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4095 const struct qstr *name, struct nfs_fh *fhandle,
4096 struct nfs_fattr *fattr, struct nfs4_label *label)
4098 struct nfs4_exception exception = {
4099 .interruptible = true,
4101 struct rpc_clnt *client = *clnt;
4104 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
4105 trace_nfs4_lookup(dir, name, err);
4107 case -NFS4ERR_BADNAME:
4110 case -NFS4ERR_MOVED:
4111 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4112 if (err == -NFS4ERR_MOVED)
4113 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4115 case -NFS4ERR_WRONGSEC:
4117 if (client != *clnt)
4119 client = nfs4_negotiate_security(client, dir, name);
4121 return PTR_ERR(client);
4123 exception.retry = 1;
4126 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4128 } while (exception.retry);
4133 else if (client != *clnt)
4134 rpc_shutdown_client(client);
4139 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
4140 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4141 struct nfs4_label *label)
4144 struct rpc_clnt *client = NFS_CLIENT(dir);
4146 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
4147 if (client != NFS_CLIENT(dir)) {
4148 rpc_shutdown_client(client);
4149 nfs_fixup_secinfo_attributes(fattr);
4155 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
4156 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4158 struct rpc_clnt *client = NFS_CLIENT(dir);
4161 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
4163 return ERR_PTR(status);
4164 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4167 static int _nfs4_proc_lookupp(struct inode *inode,
4168 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4169 struct nfs4_label *label)
4171 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4172 struct nfs_server *server = NFS_SERVER(inode);
4174 struct nfs4_lookupp_arg args = {
4175 .bitmask = server->attr_bitmask,
4176 .fh = NFS_FH(inode),
4178 struct nfs4_lookupp_res res = {
4184 struct rpc_message msg = {
4185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4190 args.bitmask = nfs4_bitmask(server, label);
4192 nfs_fattr_init(fattr);
4194 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4195 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4197 dprintk("NFS reply lookupp: %d\n", status);
4201 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4202 struct nfs_fattr *fattr, struct nfs4_label *label)
4204 struct nfs4_exception exception = {
4205 .interruptible = true,
4209 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4210 trace_nfs4_lookupp(inode, err);
4211 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4213 } while (exception.retry);
4217 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4219 struct nfs_server *server = NFS_SERVER(inode);
4220 struct nfs4_accessargs args = {
4221 .fh = NFS_FH(inode),
4222 .access = entry->mask,
4224 struct nfs4_accessres res = {
4227 struct rpc_message msg = {
4228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4231 .rpc_cred = entry->cred,
4235 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4236 res.fattr = nfs_alloc_fattr();
4237 if (res.fattr == NULL)
4239 args.bitmask = server->cache_consistency_bitmask;
4241 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4243 nfs_access_set_mask(entry, res.access);
4245 nfs_refresh_inode(inode, res.fattr);
4247 nfs_free_fattr(res.fattr);
4251 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4253 struct nfs4_exception exception = {
4254 .interruptible = true,
4258 err = _nfs4_proc_access(inode, entry);
4259 trace_nfs4_access(inode, err);
4260 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4262 } while (exception.retry);
4267 * TODO: For the time being, we don't try to get any attributes
4268 * along with any of the zero-copy operations READ, READDIR,
4271 * In the case of the first three, we want to put the GETATTR
4272 * after the read-type operation -- this is because it is hard
4273 * to predict the length of a GETATTR response in v4, and thus
4274 * align the READ data correctly. This means that the GETATTR
4275 * may end up partially falling into the page cache, and we should
4276 * shift it into the 'tail' of the xdr_buf before processing.
4277 * To do this efficiently, we need to know the total length
4278 * of data received, which doesn't seem to be available outside
4281 * In the case of WRITE, we also want to put the GETATTR after
4282 * the operation -- in this case because we want to make sure
4283 * we get the post-operation mtime and size.
4285 * Both of these changes to the XDR layer would in fact be quite
4286 * minor, but I decided to leave them for a subsequent patch.
4288 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4289 unsigned int pgbase, unsigned int pglen)
4291 struct nfs4_readlink args = {
4292 .fh = NFS_FH(inode),
4297 struct nfs4_readlink_res res;
4298 struct rpc_message msg = {
4299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4304 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4307 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4308 unsigned int pgbase, unsigned int pglen)
4310 struct nfs4_exception exception = {
4311 .interruptible = true,
4315 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4316 trace_nfs4_readlink(inode, err);
4317 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4319 } while (exception.retry);
4324 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4327 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4330 struct nfs_server *server = NFS_SERVER(dir);
4331 struct nfs4_label l, *ilabel = NULL;
4332 struct nfs_open_context *ctx;
4333 struct nfs4_state *state;
4336 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4338 return PTR_ERR(ctx);
4340 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4342 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4343 sattr->ia_mode &= ~current_umask();
4344 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4345 if (IS_ERR(state)) {
4346 status = PTR_ERR(state);
4350 nfs4_label_release_security(ilabel);
4351 put_nfs_open_context(ctx);
4356 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4358 struct nfs_server *server = NFS_SERVER(dir);
4359 struct nfs_removeargs args = {
4363 struct nfs_removeres res = {
4366 struct rpc_message msg = {
4367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4371 unsigned long timestamp = jiffies;
4374 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4376 spin_lock(&dir->i_lock);
4377 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
4378 /* Removing a directory decrements nlink in the parent */
4379 if (ftype == NF4DIR && dir->i_nlink > 2)
4380 nfs4_dec_nlink_locked(dir);
4381 spin_unlock(&dir->i_lock);
4386 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4388 struct nfs4_exception exception = {
4389 .interruptible = true,
4391 struct inode *inode = d_inode(dentry);
4395 if (inode->i_nlink == 1)
4396 nfs4_inode_return_delegation(inode);
4398 nfs4_inode_make_writeable(inode);
4401 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4402 trace_nfs4_remove(dir, &dentry->d_name, err);
4403 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4405 } while (exception.retry);
4409 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4411 struct nfs4_exception exception = {
4412 .interruptible = true,
4417 err = _nfs4_proc_remove(dir, name, NF4DIR);
4418 trace_nfs4_remove(dir, name, err);
4419 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4421 } while (exception.retry);
4425 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4426 struct dentry *dentry,
4427 struct inode *inode)
4429 struct nfs_removeargs *args = msg->rpc_argp;
4430 struct nfs_removeres *res = msg->rpc_resp;
4432 res->server = NFS_SB(dentry->d_sb);
4433 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4434 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4436 nfs_fattr_init(res->dir_attr);
4439 nfs4_inode_return_delegation(inode);
4442 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4444 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4445 &data->args.seq_args,
4450 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4452 struct nfs_unlinkdata *data = task->tk_calldata;
4453 struct nfs_removeres *res = &data->res;
4455 if (!nfs4_sequence_done(task, &res->seq_res))
4457 if (nfs4_async_handle_error(task, res->server, NULL,
4458 &data->timeout) == -EAGAIN)
4460 if (task->tk_status == 0)
4461 update_changeattr(dir, &res->cinfo,
4462 res->dir_attr->time_start, 0);
4466 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4467 struct dentry *old_dentry,
4468 struct dentry *new_dentry)
4470 struct nfs_renameargs *arg = msg->rpc_argp;
4471 struct nfs_renameres *res = msg->rpc_resp;
4472 struct inode *old_inode = d_inode(old_dentry);
4473 struct inode *new_inode = d_inode(new_dentry);
4476 nfs4_inode_make_writeable(old_inode);
4478 nfs4_inode_return_delegation(new_inode);
4479 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4480 res->server = NFS_SB(old_dentry->d_sb);
4481 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4484 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4486 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4487 &data->args.seq_args,
4492 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4493 struct inode *new_dir)
4495 struct nfs_renamedata *data = task->tk_calldata;
4496 struct nfs_renameres *res = &data->res;
4498 if (!nfs4_sequence_done(task, &res->seq_res))
4500 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4503 if (task->tk_status == 0) {
4504 if (new_dir != old_dir) {
4505 /* Note: If we moved a directory, nlink will change */
4506 update_changeattr(old_dir, &res->old_cinfo,
4507 res->old_fattr->time_start,
4508 NFS_INO_INVALID_OTHER);
4509 update_changeattr(new_dir, &res->new_cinfo,
4510 res->new_fattr->time_start,
4511 NFS_INO_INVALID_OTHER);
4513 update_changeattr(old_dir, &res->old_cinfo,
4514 res->old_fattr->time_start,
4520 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4522 struct nfs_server *server = NFS_SERVER(inode);
4523 __u32 bitmask[NFS4_BITMASK_SZ];
4524 struct nfs4_link_arg arg = {
4525 .fh = NFS_FH(inode),
4526 .dir_fh = NFS_FH(dir),
4530 struct nfs4_link_res res = {
4534 struct rpc_message msg = {
4535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4539 int status = -ENOMEM;
4541 res.fattr = nfs_alloc_fattr();
4542 if (res.fattr == NULL)
4545 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4546 if (IS_ERR(res.label)) {
4547 status = PTR_ERR(res.label);
4551 nfs4_inode_make_writeable(inode);
4552 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4554 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4556 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
4557 status = nfs_post_op_update_inode(inode, res.fattr);
4559 nfs_setsecurity(inode, res.fattr, res.label);
4563 nfs4_label_free(res.label);
4566 nfs_free_fattr(res.fattr);
4570 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4572 struct nfs4_exception exception = {
4573 .interruptible = true,
4577 err = nfs4_handle_exception(NFS_SERVER(inode),
4578 _nfs4_proc_link(inode, dir, name),
4580 } while (exception.retry);
4584 struct nfs4_createdata {
4585 struct rpc_message msg;
4586 struct nfs4_create_arg arg;
4587 struct nfs4_create_res res;
4589 struct nfs_fattr fattr;
4590 struct nfs4_label *label;
4593 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4594 const struct qstr *name, struct iattr *sattr, u32 ftype)
4596 struct nfs4_createdata *data;
4598 data = kzalloc(sizeof(*data), GFP_KERNEL);
4600 struct nfs_server *server = NFS_SERVER(dir);
4602 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4603 if (IS_ERR(data->label))
4606 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4607 data->msg.rpc_argp = &data->arg;
4608 data->msg.rpc_resp = &data->res;
4609 data->arg.dir_fh = NFS_FH(dir);
4610 data->arg.server = server;
4611 data->arg.name = name;
4612 data->arg.attrs = sattr;
4613 data->arg.ftype = ftype;
4614 data->arg.bitmask = nfs4_bitmask(server, data->label);
4615 data->arg.umask = current_umask();
4616 data->res.server = server;
4617 data->res.fh = &data->fh;
4618 data->res.fattr = &data->fattr;
4619 data->res.label = data->label;
4620 nfs_fattr_init(data->res.fattr);
4628 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4630 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4631 &data->arg.seq_args, &data->res.seq_res, 1);
4633 spin_lock(&dir->i_lock);
4634 update_changeattr_locked(dir, &data->res.dir_cinfo,
4635 data->res.fattr->time_start, 0);
4636 /* Creating a directory bumps nlink in the parent */
4637 if (data->arg.ftype == NF4DIR)
4638 nfs4_inc_nlink_locked(dir);
4639 spin_unlock(&dir->i_lock);
4640 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4645 static void nfs4_free_createdata(struct nfs4_createdata *data)
4647 nfs4_label_free(data->label);
4651 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4652 struct page *page, unsigned int len, struct iattr *sattr,
4653 struct nfs4_label *label)
4655 struct nfs4_createdata *data;
4656 int status = -ENAMETOOLONG;
4658 if (len > NFS4_MAXPATHLEN)
4662 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4666 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4667 data->arg.u.symlink.pages = &page;
4668 data->arg.u.symlink.len = len;
4669 data->arg.label = label;
4671 status = nfs4_do_create(dir, dentry, data);
4673 nfs4_free_createdata(data);
4678 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4679 struct page *page, unsigned int len, struct iattr *sattr)
4681 struct nfs4_exception exception = {
4682 .interruptible = true,
4684 struct nfs4_label l, *label = NULL;
4687 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4690 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4691 trace_nfs4_symlink(dir, &dentry->d_name, err);
4692 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4694 } while (exception.retry);
4696 nfs4_label_release_security(label);
4700 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4701 struct iattr *sattr, struct nfs4_label *label)
4703 struct nfs4_createdata *data;
4704 int status = -ENOMEM;
4706 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4710 data->arg.label = label;
4711 status = nfs4_do_create(dir, dentry, data);
4713 nfs4_free_createdata(data);
4718 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4719 struct iattr *sattr)
4721 struct nfs_server *server = NFS_SERVER(dir);
4722 struct nfs4_exception exception = {
4723 .interruptible = true,
4725 struct nfs4_label l, *label = NULL;
4728 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4730 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4731 sattr->ia_mode &= ~current_umask();
4733 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4734 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4735 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4737 } while (exception.retry);
4738 nfs4_label_release_security(label);
4743 static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4744 u64 cookie, struct page **pages, unsigned int count, bool plus)
4746 struct inode *dir = d_inode(dentry);
4747 struct nfs4_readdir_arg args = {
4752 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4755 struct nfs4_readdir_res res;
4756 struct rpc_message msg = {
4757 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4764 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4766 (unsigned long long)cookie);
4767 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4768 res.pgbase = args.pgbase;
4769 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4771 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4772 status += args.pgbase;
4775 nfs_invalidate_atime(dir);
4777 dprintk("%s: returns %d\n", __func__, status);
4781 static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4782 u64 cookie, struct page **pages, unsigned int count, bool plus)
4784 struct nfs4_exception exception = {
4785 .interruptible = true,
4789 err = _nfs4_proc_readdir(dentry, cred, cookie,
4790 pages, count, plus);
4791 trace_nfs4_readdir(d_inode(dentry), err);
4792 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4794 } while (exception.retry);
4798 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4799 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4801 struct nfs4_createdata *data;
4802 int mode = sattr->ia_mode;
4803 int status = -ENOMEM;
4805 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4810 data->arg.ftype = NF4FIFO;
4811 else if (S_ISBLK(mode)) {
4812 data->arg.ftype = NF4BLK;
4813 data->arg.u.device.specdata1 = MAJOR(rdev);
4814 data->arg.u.device.specdata2 = MINOR(rdev);
4816 else if (S_ISCHR(mode)) {
4817 data->arg.ftype = NF4CHR;
4818 data->arg.u.device.specdata1 = MAJOR(rdev);
4819 data->arg.u.device.specdata2 = MINOR(rdev);
4820 } else if (!S_ISSOCK(mode)) {
4825 data->arg.label = label;
4826 status = nfs4_do_create(dir, dentry, data);
4828 nfs4_free_createdata(data);
4833 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4834 struct iattr *sattr, dev_t rdev)
4836 struct nfs_server *server = NFS_SERVER(dir);
4837 struct nfs4_exception exception = {
4838 .interruptible = true,
4840 struct nfs4_label l, *label = NULL;
4843 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4845 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4846 sattr->ia_mode &= ~current_umask();
4848 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4849 trace_nfs4_mknod(dir, &dentry->d_name, err);
4850 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4852 } while (exception.retry);
4854 nfs4_label_release_security(label);
4859 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4860 struct nfs_fsstat *fsstat)
4862 struct nfs4_statfs_arg args = {
4864 .bitmask = server->attr_bitmask,
4866 struct nfs4_statfs_res res = {
4869 struct rpc_message msg = {
4870 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4875 nfs_fattr_init(fsstat->fattr);
4876 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4879 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4881 struct nfs4_exception exception = {
4882 .interruptible = true,
4886 err = nfs4_handle_exception(server,
4887 _nfs4_proc_statfs(server, fhandle, fsstat),
4889 } while (exception.retry);
4893 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4894 struct nfs_fsinfo *fsinfo)
4896 struct nfs4_fsinfo_arg args = {
4898 .bitmask = server->attr_bitmask,
4900 struct nfs4_fsinfo_res res = {
4903 struct rpc_message msg = {
4904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4909 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4912 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4914 struct nfs4_exception exception = {
4915 .interruptible = true,
4917 unsigned long now = jiffies;
4921 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4922 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4924 nfs4_set_lease_period(server->nfs_client,
4925 fsinfo->lease_time * HZ,
4929 err = nfs4_handle_exception(server, err, &exception);
4930 } while (exception.retry);
4934 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4938 nfs_fattr_init(fsinfo->fattr);
4939 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4941 /* block layout checks this! */
4942 server->pnfs_blksize = fsinfo->blksize;
4943 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4949 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4950 struct nfs_pathconf *pathconf)
4952 struct nfs4_pathconf_arg args = {
4954 .bitmask = server->attr_bitmask,
4956 struct nfs4_pathconf_res res = {
4957 .pathconf = pathconf,
4959 struct rpc_message msg = {
4960 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4965 /* None of the pathconf attributes are mandatory to implement */
4966 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4967 memset(pathconf, 0, sizeof(*pathconf));
4971 nfs_fattr_init(pathconf->fattr);
4972 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4975 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4976 struct nfs_pathconf *pathconf)
4978 struct nfs4_exception exception = {
4979 .interruptible = true,
4984 err = nfs4_handle_exception(server,
4985 _nfs4_proc_pathconf(server, fhandle, pathconf),
4987 } while (exception.retry);
4991 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4992 const struct nfs_open_context *ctx,
4993 const struct nfs_lock_context *l_ctx,
4996 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
4998 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5000 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5001 const struct nfs_open_context *ctx,
5002 const struct nfs_lock_context *l_ctx,
5005 nfs4_stateid current_stateid;
5007 /* If the current stateid represents a lost lock, then exit */
5008 if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO)
5010 return nfs4_stateid_match(stateid, ¤t_stateid);
5013 static bool nfs4_error_stateid_expired(int err)
5016 case -NFS4ERR_DELEG_REVOKED:
5017 case -NFS4ERR_ADMIN_REVOKED:
5018 case -NFS4ERR_BAD_STATEID:
5019 case -NFS4ERR_STALE_STATEID:
5020 case -NFS4ERR_OLD_STATEID:
5021 case -NFS4ERR_OPENMODE:
5022 case -NFS4ERR_EXPIRED:
5028 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5030 struct nfs_server *server = NFS_SERVER(hdr->inode);
5032 trace_nfs4_read(hdr, task->tk_status);
5033 if (task->tk_status < 0) {
5034 struct nfs4_exception exception = {
5035 .inode = hdr->inode,
5036 .state = hdr->args.context->state,
5037 .stateid = &hdr->args.stateid,
5039 task->tk_status = nfs4_async_handle_exception(task,
5040 server, task->tk_status, &exception);
5041 if (exception.retry) {
5042 rpc_restart_call_prepare(task);
5047 if (task->tk_status > 0)
5048 renew_lease(server, hdr->timestamp);
5052 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5053 struct nfs_pgio_args *args)
5056 if (!nfs4_error_stateid_expired(task->tk_status) ||
5057 nfs4_stateid_is_current(&args->stateid,
5062 rpc_restart_call_prepare(task);
5066 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5069 dprintk("--> %s\n", __func__);
5071 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5073 if (nfs4_read_stateid_changed(task, &hdr->args))
5075 if (task->tk_status > 0)
5076 nfs_invalidate_atime(hdr->inode);
5077 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5078 nfs4_read_done_cb(task, hdr);
5081 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5082 struct rpc_message *msg)
5084 hdr->timestamp = jiffies;
5085 if (!hdr->pgio_done_cb)
5086 hdr->pgio_done_cb = nfs4_read_done_cb;
5087 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5088 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5091 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5092 struct nfs_pgio_header *hdr)
5094 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5095 &hdr->args.seq_args,
5099 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5100 hdr->args.lock_context,
5101 hdr->rw_mode) == -EIO)
5103 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5108 static int nfs4_write_done_cb(struct rpc_task *task,
5109 struct nfs_pgio_header *hdr)
5111 struct inode *inode = hdr->inode;
5113 trace_nfs4_write(hdr, task->tk_status);
5114 if (task->tk_status < 0) {
5115 struct nfs4_exception exception = {
5116 .inode = hdr->inode,
5117 .state = hdr->args.context->state,
5118 .stateid = &hdr->args.stateid,
5120 task->tk_status = nfs4_async_handle_exception(task,
5121 NFS_SERVER(inode), task->tk_status,
5123 if (exception.retry) {
5124 rpc_restart_call_prepare(task);
5128 if (task->tk_status >= 0) {
5129 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5130 nfs_writeback_update_inode(hdr);
5135 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5136 struct nfs_pgio_args *args)
5139 if (!nfs4_error_stateid_expired(task->tk_status) ||
5140 nfs4_stateid_is_current(&args->stateid,
5145 rpc_restart_call_prepare(task);
5149 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5151 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5153 if (nfs4_write_stateid_changed(task, &hdr->args))
5155 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5156 nfs4_write_done_cb(task, hdr);
5160 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5162 /* Don't request attributes for pNFS or O_DIRECT writes */
5163 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5165 /* Otherwise, request attributes if and only if we don't hold
5168 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5171 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5172 struct rpc_message *msg,
5173 struct rpc_clnt **clnt)
5175 struct nfs_server *server = NFS_SERVER(hdr->inode);
5177 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5178 hdr->args.bitmask = NULL;
5179 hdr->res.fattr = NULL;
5181 hdr->args.bitmask = server->cache_consistency_bitmask;
5183 if (!hdr->pgio_done_cb)
5184 hdr->pgio_done_cb = nfs4_write_done_cb;
5185 hdr->res.server = server;
5186 hdr->timestamp = jiffies;
5188 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5189 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
5190 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5193 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5195 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5196 &data->args.seq_args,
5201 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5203 struct inode *inode = data->inode;
5205 trace_nfs4_commit(data, task->tk_status);
5206 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5207 NULL, NULL) == -EAGAIN) {
5208 rpc_restart_call_prepare(task);
5214 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5216 if (!nfs4_sequence_done(task, &data->res.seq_res))
5218 return data->commit_done_cb(task, data);
5221 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5222 struct rpc_clnt **clnt)
5224 struct nfs_server *server = NFS_SERVER(data->inode);
5226 if (data->commit_done_cb == NULL)
5227 data->commit_done_cb = nfs4_commit_done_cb;
5228 data->res.server = server;
5229 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5230 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5231 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5234 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5235 struct nfs_commitres *res)
5237 struct inode *dst_inode = file_inode(dst);
5238 struct nfs_server *server = NFS_SERVER(dst_inode);
5239 struct rpc_message msg = {
5240 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5245 args->fh = NFS_FH(dst_inode);
5246 return nfs4_call_sync(server->client, server, &msg,
5247 &args->seq_args, &res->seq_res, 1);
5250 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5252 struct nfs_commitargs args = {
5256 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5257 struct nfs4_exception exception = { };
5261 status = _nfs4_proc_commit(dst, &args, res);
5262 status = nfs4_handle_exception(dst_server, status, &exception);
5263 } while (exception.retry);
5268 struct nfs4_renewdata {
5269 struct nfs_client *client;
5270 unsigned long timestamp;
5274 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5275 * standalone procedure for queueing an asynchronous RENEW.
5277 static void nfs4_renew_release(void *calldata)
5279 struct nfs4_renewdata *data = calldata;
5280 struct nfs_client *clp = data->client;
5282 if (refcount_read(&clp->cl_count) > 1)
5283 nfs4_schedule_state_renewal(clp);
5284 nfs_put_client(clp);
5288 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5290 struct nfs4_renewdata *data = calldata;
5291 struct nfs_client *clp = data->client;
5292 unsigned long timestamp = data->timestamp;
5294 trace_nfs4_renew_async(clp, task->tk_status);
5295 switch (task->tk_status) {
5298 case -NFS4ERR_LEASE_MOVED:
5299 nfs4_schedule_lease_moved_recovery(clp);
5302 /* Unless we're shutting down, schedule state recovery! */
5303 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5305 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5306 nfs4_schedule_lease_recovery(clp);
5309 nfs4_schedule_path_down_recovery(clp);
5311 do_renew_lease(clp, timestamp);
5314 static const struct rpc_call_ops nfs4_renew_ops = {
5315 .rpc_call_done = nfs4_renew_done,
5316 .rpc_release = nfs4_renew_release,
5319 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5321 struct rpc_message msg = {
5322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5326 struct nfs4_renewdata *data;
5328 if (renew_flags == 0)
5330 if (!refcount_inc_not_zero(&clp->cl_count))
5332 data = kmalloc(sizeof(*data), GFP_NOFS);
5334 nfs_put_client(clp);
5338 data->timestamp = jiffies;
5339 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5340 &nfs4_renew_ops, data);
5343 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5345 struct rpc_message msg = {
5346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5350 unsigned long now = jiffies;
5353 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5356 do_renew_lease(clp, now);
5360 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5362 return server->caps & NFS_CAP_ACLS;
5365 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5366 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5369 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5371 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5372 struct page **pages)
5374 struct page *newpage, **spages;
5380 len = min_t(size_t, PAGE_SIZE, buflen);
5381 newpage = alloc_page(GFP_KERNEL);
5383 if (newpage == NULL)
5385 memcpy(page_address(newpage), buf, len);
5390 } while (buflen != 0);
5396 __free_page(spages[rc-1]);
5400 struct nfs4_cached_acl {
5406 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5408 struct nfs_inode *nfsi = NFS_I(inode);
5410 spin_lock(&inode->i_lock);
5411 kfree(nfsi->nfs4_acl);
5412 nfsi->nfs4_acl = acl;
5413 spin_unlock(&inode->i_lock);
5416 static void nfs4_zap_acl_attr(struct inode *inode)
5418 nfs4_set_cached_acl(inode, NULL);
5421 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5423 struct nfs_inode *nfsi = NFS_I(inode);
5424 struct nfs4_cached_acl *acl;
5427 spin_lock(&inode->i_lock);
5428 acl = nfsi->nfs4_acl;
5431 if (buf == NULL) /* user is just asking for length */
5433 if (acl->cached == 0)
5435 ret = -ERANGE; /* see getxattr(2) man page */
5436 if (acl->len > buflen)
5438 memcpy(buf, acl->data, acl->len);
5442 spin_unlock(&inode->i_lock);
5446 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5448 struct nfs4_cached_acl *acl;
5449 size_t buflen = sizeof(*acl) + acl_len;
5451 if (buflen <= PAGE_SIZE) {
5452 acl = kmalloc(buflen, GFP_KERNEL);
5456 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5458 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5465 nfs4_set_cached_acl(inode, acl);
5469 * The getxattr API returns the required buffer length when called with a
5470 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5471 * the required buf. On a NULL buf, we send a page of data to the server
5472 * guessing that the ACL request can be serviced by a page. If so, we cache
5473 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5474 * the cache. If not so, we throw away the page, and cache the required
5475 * length. The next getxattr call will then produce another round trip to
5476 * the server, this time with the input buf of the required size.
5478 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5480 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5481 struct nfs_getaclargs args = {
5482 .fh = NFS_FH(inode),
5486 struct nfs_getaclres res = {
5489 struct rpc_message msg = {
5490 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5494 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5495 int ret = -ENOMEM, i;
5497 if (npages > ARRAY_SIZE(pages))
5500 for (i = 0; i < npages; i++) {
5501 pages[i] = alloc_page(GFP_KERNEL);
5506 /* for decoding across pages */
5507 res.acl_scratch = alloc_page(GFP_KERNEL);
5508 if (!res.acl_scratch)
5511 args.acl_len = npages * PAGE_SIZE;
5513 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5514 __func__, buf, buflen, npages, args.acl_len);
5515 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5516 &msg, &args.seq_args, &res.seq_res, 0);
5520 /* Handle the case where the passed-in buffer is too short */
5521 if (res.acl_flags & NFS4_ACL_TRUNC) {
5522 /* Did the user only issue a request for the acl length? */
5528 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5530 if (res.acl_len > buflen) {
5534 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5539 for (i = 0; i < npages; i++)
5541 __free_page(pages[i]);
5542 if (res.acl_scratch)
5543 __free_page(res.acl_scratch);
5547 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5549 struct nfs4_exception exception = {
5550 .interruptible = true,
5554 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5555 trace_nfs4_get_acl(inode, ret);
5558 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5559 } while (exception.retry);
5563 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5565 struct nfs_server *server = NFS_SERVER(inode);
5568 if (!nfs4_server_supports_acls(server))
5570 ret = nfs_revalidate_inode(server, inode);
5573 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5574 nfs_zap_acl_cache(inode);
5575 ret = nfs4_read_cached_acl(inode, buf, buflen);
5577 /* -ENOENT is returned if there is no ACL or if there is an ACL
5578 * but no cached acl data, just the acl length */
5580 return nfs4_get_acl_uncached(inode, buf, buflen);
5583 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5585 struct nfs_server *server = NFS_SERVER(inode);
5586 struct page *pages[NFS4ACL_MAXPAGES];
5587 struct nfs_setaclargs arg = {
5588 .fh = NFS_FH(inode),
5592 struct nfs_setaclres res;
5593 struct rpc_message msg = {
5594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5598 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5601 if (!nfs4_server_supports_acls(server))
5603 if (npages > ARRAY_SIZE(pages))
5605 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5608 nfs4_inode_make_writeable(inode);
5609 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5612 * Free each page after tx, so the only ref left is
5613 * held by the network stack
5616 put_page(pages[i-1]);
5619 * Acl update can result in inode attribute update.
5620 * so mark the attribute cache invalid.
5622 spin_lock(&inode->i_lock);
5623 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5624 | NFS_INO_INVALID_CTIME
5625 | NFS_INO_REVAL_FORCED;
5626 spin_unlock(&inode->i_lock);
5627 nfs_access_zap_cache(inode);
5628 nfs_zap_acl_cache(inode);
5632 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5634 struct nfs4_exception exception = { };
5637 err = __nfs4_proc_set_acl(inode, buf, buflen);
5638 trace_nfs4_set_acl(inode, err);
5639 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5641 } while (exception.retry);
5645 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5646 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5649 struct nfs_server *server = NFS_SERVER(inode);
5650 struct nfs_fattr fattr;
5651 struct nfs4_label label = {0, 0, buflen, buf};
5653 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5654 struct nfs4_getattr_arg arg = {
5655 .fh = NFS_FH(inode),
5658 struct nfs4_getattr_res res = {
5663 struct rpc_message msg = {
5664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5670 nfs_fattr_init(&fattr);
5672 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5675 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5677 if (buflen < label.len)
5682 static int nfs4_get_security_label(struct inode *inode, void *buf,
5685 struct nfs4_exception exception = {
5686 .interruptible = true,
5690 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5694 err = _nfs4_get_security_label(inode, buf, buflen);
5695 trace_nfs4_get_security_label(inode, err);
5696 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5698 } while (exception.retry);
5702 static int _nfs4_do_set_security_label(struct inode *inode,
5703 struct nfs4_label *ilabel,
5704 struct nfs_fattr *fattr,
5705 struct nfs4_label *olabel)
5708 struct iattr sattr = {0};
5709 struct nfs_server *server = NFS_SERVER(inode);
5710 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5711 struct nfs_setattrargs arg = {
5712 .fh = NFS_FH(inode),
5718 struct nfs_setattrres res = {
5723 struct rpc_message msg = {
5724 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5730 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5732 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5734 dprintk("%s failed: %d\n", __func__, status);
5739 static int nfs4_do_set_security_label(struct inode *inode,
5740 struct nfs4_label *ilabel,
5741 struct nfs_fattr *fattr,
5742 struct nfs4_label *olabel)
5744 struct nfs4_exception exception = { };
5748 err = _nfs4_do_set_security_label(inode, ilabel,
5750 trace_nfs4_set_security_label(inode, err);
5751 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5753 } while (exception.retry);
5758 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5760 struct nfs4_label ilabel, *olabel = NULL;
5761 struct nfs_fattr fattr;
5764 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5767 nfs_fattr_init(&fattr);
5771 ilabel.label = (char *)buf;
5772 ilabel.len = buflen;
5774 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5775 if (IS_ERR(olabel)) {
5776 status = -PTR_ERR(olabel);
5780 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5782 nfs_setsecurity(inode, &fattr, olabel);
5784 nfs4_label_free(olabel);
5788 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5791 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5792 nfs4_verifier *bootverf)
5796 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5797 /* An impossible timestamp guarantees this value
5798 * will never match a generated boot time. */
5799 verf[0] = cpu_to_be32(U32_MAX);
5800 verf[1] = cpu_to_be32(U32_MAX);
5802 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5803 u64 ns = ktime_to_ns(nn->boot_time);
5805 verf[0] = cpu_to_be32(ns >> 32);
5806 verf[1] = cpu_to_be32(ns);
5808 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5812 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5817 if (clp->cl_owner_id != NULL)
5822 strlen(clp->cl_rpcclient->cl_nodename) +
5824 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5827 if (nfs4_client_id_uniquifier[0] != '\0')
5828 len += strlen(nfs4_client_id_uniquifier) + 1;
5829 if (len > NFS4_OPAQUE_LIMIT + 1)
5833 * Since this string is allocated at mount time, and held until the
5834 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5835 * about a memory-reclaim deadlock.
5837 str = kmalloc(len, GFP_KERNEL);
5842 if (nfs4_client_id_uniquifier[0] != '\0')
5843 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
5844 clp->cl_rpcclient->cl_nodename,
5845 nfs4_client_id_uniquifier,
5846 rpc_peeraddr2str(clp->cl_rpcclient,
5849 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
5850 clp->cl_rpcclient->cl_nodename,
5851 rpc_peeraddr2str(clp->cl_rpcclient,
5855 clp->cl_owner_id = str;
5860 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5865 len = 10 + 10 + 1 + 10 + 1 +
5866 strlen(nfs4_client_id_uniquifier) + 1 +
5867 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5869 if (len > NFS4_OPAQUE_LIMIT + 1)
5873 * Since this string is allocated at mount time, and held until the
5874 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5875 * about a memory-reclaim deadlock.
5877 str = kmalloc(len, GFP_KERNEL);
5881 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5882 clp->rpc_ops->version, clp->cl_minorversion,
5883 nfs4_client_id_uniquifier,
5884 clp->cl_rpcclient->cl_nodename);
5885 clp->cl_owner_id = str;
5890 nfs4_init_uniform_client_string(struct nfs_client *clp)
5895 if (clp->cl_owner_id != NULL)
5898 if (nfs4_client_id_uniquifier[0] != '\0')
5899 return nfs4_init_uniquifier_client_string(clp);
5901 len = 10 + 10 + 1 + 10 + 1 +
5902 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5904 if (len > NFS4_OPAQUE_LIMIT + 1)
5908 * Since this string is allocated at mount time, and held until the
5909 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5910 * about a memory-reclaim deadlock.
5912 str = kmalloc(len, GFP_KERNEL);
5916 scnprintf(str, len, "Linux NFSv%u.%u %s",
5917 clp->rpc_ops->version, clp->cl_minorversion,
5918 clp->cl_rpcclient->cl_nodename);
5919 clp->cl_owner_id = str;
5924 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5925 * services. Advertise one based on the address family of the
5929 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5931 if (strchr(clp->cl_ipaddr, ':') != NULL)
5932 return scnprintf(buf, len, "tcp6");
5934 return scnprintf(buf, len, "tcp");
5937 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5939 struct nfs4_setclientid *sc = calldata;
5941 if (task->tk_status == 0)
5942 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5945 static const struct rpc_call_ops nfs4_setclientid_ops = {
5946 .rpc_call_done = nfs4_setclientid_done,
5950 * nfs4_proc_setclientid - Negotiate client ID
5951 * @clp: state data structure
5952 * @program: RPC program for NFSv4 callback service
5953 * @port: IP port number for NFS4 callback service
5954 * @cred: credential to use for this call
5955 * @res: where to place the result
5957 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5959 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5960 unsigned short port, const struct cred *cred,
5961 struct nfs4_setclientid_res *res)
5963 nfs4_verifier sc_verifier;
5964 struct nfs4_setclientid setclientid = {
5965 .sc_verifier = &sc_verifier,
5969 struct rpc_message msg = {
5970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5971 .rpc_argp = &setclientid,
5975 struct rpc_task *task;
5976 struct rpc_task_setup task_setup_data = {
5977 .rpc_client = clp->cl_rpcclient,
5978 .rpc_message = &msg,
5979 .callback_ops = &nfs4_setclientid_ops,
5980 .callback_data = &setclientid,
5981 .flags = RPC_TASK_TIMEOUT,
5985 /* nfs_client_id4 */
5986 nfs4_init_boot_verifier(clp, &sc_verifier);
5988 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5989 status = nfs4_init_uniform_client_string(clp);
5991 status = nfs4_init_nonuniform_client_string(clp);
5997 setclientid.sc_netid_len =
5998 nfs4_init_callback_netid(clp,
5999 setclientid.sc_netid,
6000 sizeof(setclientid.sc_netid));
6001 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6002 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6003 clp->cl_ipaddr, port >> 8, port & 255);
6005 dprintk("NFS call setclientid auth=%s, '%s'\n",
6006 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6008 task = rpc_run_task(&task_setup_data);
6010 status = PTR_ERR(task);
6013 status = task->tk_status;
6014 if (setclientid.sc_cred) {
6015 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6016 put_rpccred(setclientid.sc_cred);
6020 trace_nfs4_setclientid(clp, status);
6021 dprintk("NFS reply setclientid: %d\n", status);
6026 * nfs4_proc_setclientid_confirm - Confirm client ID
6027 * @clp: state data structure
6028 * @arg: result of a previous SETCLIENTID
6029 * @cred: credential to use for this call
6031 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6033 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6034 struct nfs4_setclientid_res *arg,
6035 const struct cred *cred)
6037 struct rpc_message msg = {
6038 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6044 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6045 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6047 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6048 trace_nfs4_setclientid_confirm(clp, status);
6049 dprintk("NFS reply setclientid_confirm: %d\n", status);
6053 struct nfs4_delegreturndata {
6054 struct nfs4_delegreturnargs args;
6055 struct nfs4_delegreturnres res;
6057 nfs4_stateid stateid;
6058 unsigned long timestamp;
6060 struct nfs4_layoutreturn_args arg;
6061 struct nfs4_layoutreturn_res res;
6062 struct nfs4_xdr_opaque_data ld_private;
6066 struct nfs_fattr fattr;
6068 struct inode *inode;
6071 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6073 struct nfs4_delegreturndata *data = calldata;
6074 struct nfs4_exception exception = {
6075 .inode = data->inode,
6076 .stateid = &data->stateid,
6079 if (!nfs4_sequence_done(task, &data->res.seq_res))
6082 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6084 /* Handle Layoutreturn errors */
6085 if (data->args.lr_args && task->tk_status != 0) {
6086 switch(data->res.lr_ret) {
6088 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6091 data->args.lr_args = NULL;
6092 data->res.lr_res = NULL;
6094 case -NFS4ERR_OLD_STATEID:
6095 if (nfs4_layoutreturn_refresh_stateid(&data->args.lr_args->stateid,
6096 &data->args.lr_args->range,
6100 case -NFS4ERR_ADMIN_REVOKED:
6101 case -NFS4ERR_DELEG_REVOKED:
6102 case -NFS4ERR_EXPIRED:
6103 case -NFS4ERR_BAD_STATEID:
6104 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
6105 case -NFS4ERR_WRONG_CRED:
6106 data->args.lr_args = NULL;
6107 data->res.lr_res = NULL;
6112 switch (task->tk_status) {
6114 renew_lease(data->res.server, data->timestamp);
6116 case -NFS4ERR_ADMIN_REVOKED:
6117 case -NFS4ERR_DELEG_REVOKED:
6118 case -NFS4ERR_EXPIRED:
6119 nfs4_free_revoked_stateid(data->res.server,
6121 task->tk_msg.rpc_cred);
6123 case -NFS4ERR_BAD_STATEID:
6124 case -NFS4ERR_STALE_STATEID:
6125 task->tk_status = 0;
6127 case -NFS4ERR_OLD_STATEID:
6128 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6130 task->tk_status = 0;
6132 case -NFS4ERR_ACCESS:
6133 if (data->args.bitmask) {
6134 data->args.bitmask = NULL;
6135 data->res.fattr = NULL;
6140 task->tk_status = nfs4_async_handle_exception(task,
6141 data->res.server, task->tk_status,
6143 if (exception.retry)
6146 data->rpc_status = task->tk_status;
6149 data->res.lr_ret = 0;
6151 task->tk_status = 0;
6152 rpc_restart_call_prepare(task);
6155 static void nfs4_delegreturn_release(void *calldata)
6157 struct nfs4_delegreturndata *data = calldata;
6158 struct inode *inode = data->inode;
6162 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6164 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6165 nfs_iput_and_deactive(inode);
6170 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6172 struct nfs4_delegreturndata *d_data;
6173 struct pnfs_layout_hdr *lo;
6175 d_data = (struct nfs4_delegreturndata *)data;
6177 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
6180 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6181 if (lo && !pnfs_layout_is_valid(lo)) {
6182 d_data->args.lr_args = NULL;
6183 d_data->res.lr_res = NULL;
6186 nfs4_setup_sequence(d_data->res.server->nfs_client,
6187 &d_data->args.seq_args,
6188 &d_data->res.seq_res,
6192 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6193 .rpc_call_prepare = nfs4_delegreturn_prepare,
6194 .rpc_call_done = nfs4_delegreturn_done,
6195 .rpc_release = nfs4_delegreturn_release,
6198 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6200 struct nfs4_delegreturndata *data;
6201 struct nfs_server *server = NFS_SERVER(inode);
6202 struct rpc_task *task;
6203 struct rpc_message msg = {
6204 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6207 struct rpc_task_setup task_setup_data = {
6208 .rpc_client = server->client,
6209 .rpc_message = &msg,
6210 .callback_ops = &nfs4_delegreturn_ops,
6211 .flags = RPC_TASK_ASYNC,
6215 data = kzalloc(sizeof(*data), GFP_NOFS);
6218 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6220 nfs4_state_protect(server->nfs_client,
6221 NFS_SP4_MACH_CRED_CLEANUP,
6222 &task_setup_data.rpc_client, &msg);
6224 data->args.fhandle = &data->fh;
6225 data->args.stateid = &data->stateid;
6226 data->args.bitmask = server->cache_consistency_bitmask;
6227 nfs_copy_fh(&data->fh, NFS_FH(inode));
6228 nfs4_stateid_copy(&data->stateid, stateid);
6229 data->res.fattr = &data->fattr;
6230 data->res.server = server;
6231 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6232 data->lr.arg.ld_private = &data->lr.ld_private;
6233 nfs_fattr_init(data->res.fattr);
6234 data->timestamp = jiffies;
6235 data->rpc_status = 0;
6236 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
6237 data->inode = nfs_igrab_and_active(inode);
6240 data->args.lr_args = &data->lr.arg;
6241 data->res.lr_res = &data->lr.res;
6243 } else if (data->lr.roc) {
6244 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6245 data->lr.roc = false;
6248 task_setup_data.callback_data = data;
6249 msg.rpc_argp = &data->args;
6250 msg.rpc_resp = &data->res;
6251 task = rpc_run_task(&task_setup_data);
6253 return PTR_ERR(task);
6256 status = rpc_wait_for_completion_task(task);
6259 status = data->rpc_status;
6265 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6267 struct nfs_server *server = NFS_SERVER(inode);
6268 struct nfs4_exception exception = { };
6271 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6272 trace_nfs4_delegreturn(inode, stateid, err);
6274 case -NFS4ERR_STALE_STATEID:
6275 case -NFS4ERR_EXPIRED:
6279 err = nfs4_handle_exception(server, err, &exception);
6280 } while (exception.retry);
6284 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6286 struct inode *inode = state->inode;
6287 struct nfs_server *server = NFS_SERVER(inode);
6288 struct nfs_client *clp = server->nfs_client;
6289 struct nfs_lockt_args arg = {
6290 .fh = NFS_FH(inode),
6293 struct nfs_lockt_res res = {
6296 struct rpc_message msg = {
6297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6300 .rpc_cred = state->owner->so_cred,
6302 struct nfs4_lock_state *lsp;
6305 arg.lock_owner.clientid = clp->cl_clientid;
6306 status = nfs4_set_lock_state(state, request);
6309 lsp = request->fl_u.nfs4_fl.owner;
6310 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6311 arg.lock_owner.s_dev = server->s_dev;
6312 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6315 request->fl_type = F_UNLCK;
6317 case -NFS4ERR_DENIED:
6320 request->fl_ops->fl_release_private(request);
6321 request->fl_ops = NULL;
6326 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6328 struct nfs4_exception exception = {
6329 .interruptible = true,
6334 err = _nfs4_proc_getlk(state, cmd, request);
6335 trace_nfs4_get_lock(request, state, cmd, err);
6336 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6338 } while (exception.retry);
6342 struct nfs4_unlockdata {
6343 struct nfs_locku_args arg;
6344 struct nfs_locku_res res;
6345 struct nfs4_lock_state *lsp;
6346 struct nfs_open_context *ctx;
6347 struct nfs_lock_context *l_ctx;
6348 struct file_lock fl;
6349 struct nfs_server *server;
6350 unsigned long timestamp;
6353 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6354 struct nfs_open_context *ctx,
6355 struct nfs4_lock_state *lsp,
6356 struct nfs_seqid *seqid)
6358 struct nfs4_unlockdata *p;
6359 struct inode *inode = lsp->ls_state->inode;
6361 p = kzalloc(sizeof(*p), GFP_NOFS);
6364 p->arg.fh = NFS_FH(inode);
6366 p->arg.seqid = seqid;
6367 p->res.seqid = seqid;
6369 /* Ensure we don't close file until we're done freeing locks! */
6370 p->ctx = get_nfs_open_context(ctx);
6371 p->l_ctx = nfs_get_lock_context(ctx);
6372 locks_init_lock(&p->fl);
6373 locks_copy_lock(&p->fl, fl);
6374 p->server = NFS_SERVER(inode);
6378 static void nfs4_locku_release_calldata(void *data)
6380 struct nfs4_unlockdata *calldata = data;
6381 nfs_free_seqid(calldata->arg.seqid);
6382 nfs4_put_lock_state(calldata->lsp);
6383 nfs_put_lock_context(calldata->l_ctx);
6384 put_nfs_open_context(calldata->ctx);
6388 static void nfs4_locku_done(struct rpc_task *task, void *data)
6390 struct nfs4_unlockdata *calldata = data;
6391 struct nfs4_exception exception = {
6392 .inode = calldata->lsp->ls_state->inode,
6393 .stateid = &calldata->arg.stateid,
6396 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6398 switch (task->tk_status) {
6400 renew_lease(calldata->server, calldata->timestamp);
6401 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6402 if (nfs4_update_lock_stateid(calldata->lsp,
6403 &calldata->res.stateid))
6406 case -NFS4ERR_ADMIN_REVOKED:
6407 case -NFS4ERR_EXPIRED:
6408 nfs4_free_revoked_stateid(calldata->server,
6409 &calldata->arg.stateid,
6410 task->tk_msg.rpc_cred);
6412 case -NFS4ERR_BAD_STATEID:
6413 case -NFS4ERR_OLD_STATEID:
6414 case -NFS4ERR_STALE_STATEID:
6415 if (!nfs4_stateid_match(&calldata->arg.stateid,
6416 &calldata->lsp->ls_stateid))
6417 rpc_restart_call_prepare(task);
6420 task->tk_status = nfs4_async_handle_exception(task,
6421 calldata->server, task->tk_status,
6423 if (exception.retry)
6424 rpc_restart_call_prepare(task);
6426 nfs_release_seqid(calldata->arg.seqid);
6429 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6431 struct nfs4_unlockdata *calldata = data;
6433 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6434 nfs_async_iocounter_wait(task, calldata->l_ctx))
6437 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6439 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
6440 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6441 /* Note: exit _without_ running nfs4_locku_done */
6444 calldata->timestamp = jiffies;
6445 if (nfs4_setup_sequence(calldata->server->nfs_client,
6446 &calldata->arg.seq_args,
6447 &calldata->res.seq_res,
6449 nfs_release_seqid(calldata->arg.seqid);
6452 task->tk_action = NULL;
6454 nfs4_sequence_done(task, &calldata->res.seq_res);
6457 static const struct rpc_call_ops nfs4_locku_ops = {
6458 .rpc_call_prepare = nfs4_locku_prepare,
6459 .rpc_call_done = nfs4_locku_done,
6460 .rpc_release = nfs4_locku_release_calldata,
6463 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6464 struct nfs_open_context *ctx,
6465 struct nfs4_lock_state *lsp,
6466 struct nfs_seqid *seqid)
6468 struct nfs4_unlockdata *data;
6469 struct rpc_message msg = {
6470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6471 .rpc_cred = ctx->cred,
6473 struct rpc_task_setup task_setup_data = {
6474 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6475 .rpc_message = &msg,
6476 .callback_ops = &nfs4_locku_ops,
6477 .workqueue = nfsiod_workqueue,
6478 .flags = RPC_TASK_ASYNC,
6481 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6482 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6484 /* Ensure this is an unlock - when canceling a lock, the
6485 * canceled lock is passed in, and it won't be an unlock.
6487 fl->fl_type = F_UNLCK;
6488 if (fl->fl_flags & FL_CLOSE)
6489 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6491 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6493 nfs_free_seqid(seqid);
6494 return ERR_PTR(-ENOMEM);
6497 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6498 msg.rpc_argp = &data->arg;
6499 msg.rpc_resp = &data->res;
6500 task_setup_data.callback_data = data;
6501 return rpc_run_task(&task_setup_data);
6504 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6506 struct inode *inode = state->inode;
6507 struct nfs4_state_owner *sp = state->owner;
6508 struct nfs_inode *nfsi = NFS_I(inode);
6509 struct nfs_seqid *seqid;
6510 struct nfs4_lock_state *lsp;
6511 struct rpc_task *task;
6512 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6514 unsigned char fl_flags = request->fl_flags;
6516 status = nfs4_set_lock_state(state, request);
6517 /* Unlock _before_ we do the RPC call */
6518 request->fl_flags |= FL_EXISTS;
6519 /* Exclude nfs_delegation_claim_locks() */
6520 mutex_lock(&sp->so_delegreturn_mutex);
6521 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6522 down_read(&nfsi->rwsem);
6523 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6524 up_read(&nfsi->rwsem);
6525 mutex_unlock(&sp->so_delegreturn_mutex);
6528 up_read(&nfsi->rwsem);
6529 mutex_unlock(&sp->so_delegreturn_mutex);
6532 /* Is this a delegated lock? */
6533 lsp = request->fl_u.nfs4_fl.owner;
6534 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6536 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6537 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6541 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6542 status = PTR_ERR(task);
6545 status = rpc_wait_for_completion_task(task);
6548 request->fl_flags = fl_flags;
6549 trace_nfs4_unlock(request, state, F_SETLK, status);
6553 struct nfs4_lockdata {
6554 struct nfs_lock_args arg;
6555 struct nfs_lock_res res;
6556 struct nfs4_lock_state *lsp;
6557 struct nfs_open_context *ctx;
6558 struct file_lock fl;
6559 unsigned long timestamp;
6562 struct nfs_server *server;
6565 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6566 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6569 struct nfs4_lockdata *p;
6570 struct inode *inode = lsp->ls_state->inode;
6571 struct nfs_server *server = NFS_SERVER(inode);
6572 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6574 p = kzalloc(sizeof(*p), gfp_mask);
6578 p->arg.fh = NFS_FH(inode);
6580 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6581 if (IS_ERR(p->arg.open_seqid))
6583 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6584 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6585 if (IS_ERR(p->arg.lock_seqid))
6586 goto out_free_seqid;
6587 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6588 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6589 p->arg.lock_owner.s_dev = server->s_dev;
6590 p->res.lock_seqid = p->arg.lock_seqid;
6593 p->ctx = get_nfs_open_context(ctx);
6594 locks_init_lock(&p->fl);
6595 locks_copy_lock(&p->fl, fl);
6598 nfs_free_seqid(p->arg.open_seqid);
6604 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6606 struct nfs4_lockdata *data = calldata;
6607 struct nfs4_state *state = data->lsp->ls_state;
6609 dprintk("%s: begin!\n", __func__);
6610 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6612 /* Do we need to do an open_to_lock_owner? */
6613 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6614 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6615 goto out_release_lock_seqid;
6617 nfs4_stateid_copy(&data->arg.open_stateid,
6618 &state->open_stateid);
6619 data->arg.new_lock_owner = 1;
6620 data->res.open_seqid = data->arg.open_seqid;
6622 data->arg.new_lock_owner = 0;
6623 nfs4_stateid_copy(&data->arg.lock_stateid,
6624 &data->lsp->ls_stateid);
6626 if (!nfs4_valid_open_stateid(state)) {
6627 data->rpc_status = -EBADF;
6628 task->tk_action = NULL;
6629 goto out_release_open_seqid;
6631 data->timestamp = jiffies;
6632 if (nfs4_setup_sequence(data->server->nfs_client,
6633 &data->arg.seq_args,
6637 out_release_open_seqid:
6638 nfs_release_seqid(data->arg.open_seqid);
6639 out_release_lock_seqid:
6640 nfs_release_seqid(data->arg.lock_seqid);
6642 nfs4_sequence_done(task, &data->res.seq_res);
6643 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6646 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6648 struct nfs4_lockdata *data = calldata;
6649 struct nfs4_lock_state *lsp = data->lsp;
6651 dprintk("%s: begin!\n", __func__);
6653 if (!nfs4_sequence_done(task, &data->res.seq_res))
6656 data->rpc_status = task->tk_status;
6657 switch (task->tk_status) {
6659 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6661 if (data->arg.new_lock && !data->cancelled) {
6662 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6663 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6666 if (data->arg.new_lock_owner != 0) {
6667 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6668 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6669 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6670 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6673 case -NFS4ERR_BAD_STATEID:
6674 case -NFS4ERR_OLD_STATEID:
6675 case -NFS4ERR_STALE_STATEID:
6676 case -NFS4ERR_EXPIRED:
6677 if (data->arg.new_lock_owner != 0) {
6678 if (!nfs4_stateid_match(&data->arg.open_stateid,
6679 &lsp->ls_state->open_stateid))
6681 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6686 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6689 if (!data->cancelled)
6690 rpc_restart_call_prepare(task);
6694 static void nfs4_lock_release(void *calldata)
6696 struct nfs4_lockdata *data = calldata;
6698 dprintk("%s: begin!\n", __func__);
6699 nfs_free_seqid(data->arg.open_seqid);
6700 if (data->cancelled && data->rpc_status == 0) {
6701 struct rpc_task *task;
6702 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6703 data->arg.lock_seqid);
6705 rpc_put_task_async(task);
6706 dprintk("%s: cancelling lock!\n", __func__);
6708 nfs_free_seqid(data->arg.lock_seqid);
6709 nfs4_put_lock_state(data->lsp);
6710 put_nfs_open_context(data->ctx);
6712 dprintk("%s: done!\n", __func__);
6715 static const struct rpc_call_ops nfs4_lock_ops = {
6716 .rpc_call_prepare = nfs4_lock_prepare,
6717 .rpc_call_done = nfs4_lock_done,
6718 .rpc_release = nfs4_lock_release,
6721 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6724 case -NFS4ERR_ADMIN_REVOKED:
6725 case -NFS4ERR_EXPIRED:
6726 case -NFS4ERR_BAD_STATEID:
6727 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6728 if (new_lock_owner != 0 ||
6729 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6730 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6732 case -NFS4ERR_STALE_STATEID:
6733 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6734 nfs4_schedule_lease_recovery(server->nfs_client);
6738 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6740 struct nfs4_lockdata *data;
6741 struct rpc_task *task;
6742 struct rpc_message msg = {
6743 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6744 .rpc_cred = state->owner->so_cred,
6746 struct rpc_task_setup task_setup_data = {
6747 .rpc_client = NFS_CLIENT(state->inode),
6748 .rpc_message = &msg,
6749 .callback_ops = &nfs4_lock_ops,
6750 .workqueue = nfsiod_workqueue,
6751 .flags = RPC_TASK_ASYNC,
6755 dprintk("%s: begin!\n", __func__);
6756 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6757 fl->fl_u.nfs4_fl.owner,
6758 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6762 data->arg.block = 1;
6763 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6764 recovery_type > NFS_LOCK_NEW);
6765 msg.rpc_argp = &data->arg;
6766 msg.rpc_resp = &data->res;
6767 task_setup_data.callback_data = data;
6768 if (recovery_type > NFS_LOCK_NEW) {
6769 if (recovery_type == NFS_LOCK_RECLAIM)
6770 data->arg.reclaim = NFS_LOCK_RECLAIM;
6772 data->arg.new_lock = 1;
6773 task = rpc_run_task(&task_setup_data);
6775 return PTR_ERR(task);
6776 ret = rpc_wait_for_completion_task(task);
6778 ret = data->rpc_status;
6780 nfs4_handle_setlk_error(data->server, data->lsp,
6781 data->arg.new_lock_owner, ret);
6783 data->cancelled = true;
6785 dprintk("%s: done, ret = %d!\n", __func__, ret);
6786 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6790 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6792 struct nfs_server *server = NFS_SERVER(state->inode);
6793 struct nfs4_exception exception = {
6794 .inode = state->inode,
6799 /* Cache the lock if possible... */
6800 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6802 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6803 if (err != -NFS4ERR_DELAY)
6805 nfs4_handle_exception(server, err, &exception);
6806 } while (exception.retry);
6810 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6812 struct nfs_server *server = NFS_SERVER(state->inode);
6813 struct nfs4_exception exception = {
6814 .inode = state->inode,
6818 err = nfs4_set_lock_state(state, request);
6821 if (!recover_lost_locks) {
6822 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6826 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6828 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6832 case -NFS4ERR_GRACE:
6833 case -NFS4ERR_DELAY:
6834 nfs4_handle_exception(server, err, &exception);
6837 } while (exception.retry);
6842 #if defined(CONFIG_NFS_V4_1)
6843 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6845 struct nfs4_lock_state *lsp;
6848 status = nfs4_set_lock_state(state, request);
6851 lsp = request->fl_u.nfs4_fl.owner;
6852 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6853 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6855 return nfs4_lock_expired(state, request);
6859 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6861 struct nfs_inode *nfsi = NFS_I(state->inode);
6862 struct nfs4_state_owner *sp = state->owner;
6863 unsigned char fl_flags = request->fl_flags;
6866 request->fl_flags |= FL_ACCESS;
6867 status = locks_lock_inode_wait(state->inode, request);
6870 mutex_lock(&sp->so_delegreturn_mutex);
6871 down_read(&nfsi->rwsem);
6872 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6873 /* Yes: cache locks! */
6874 /* ...but avoid races with delegation recall... */
6875 request->fl_flags = fl_flags & ~FL_SLEEP;
6876 status = locks_lock_inode_wait(state->inode, request);
6877 up_read(&nfsi->rwsem);
6878 mutex_unlock(&sp->so_delegreturn_mutex);
6881 up_read(&nfsi->rwsem);
6882 mutex_unlock(&sp->so_delegreturn_mutex);
6883 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6885 request->fl_flags = fl_flags;
6889 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6891 struct nfs4_exception exception = {
6893 .inode = state->inode,
6894 .interruptible = true,
6899 err = _nfs4_proc_setlk(state, cmd, request);
6900 if (err == -NFS4ERR_DENIED)
6902 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6904 } while (exception.retry);
6908 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6909 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6912 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6913 struct file_lock *request)
6915 int status = -ERESTARTSYS;
6916 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6918 while(!signalled()) {
6919 status = nfs4_proc_setlk(state, cmd, request);
6920 if ((status != -EAGAIN) || IS_SETLK(cmd))
6922 freezable_schedule_timeout_interruptible(timeout);
6924 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6925 status = -ERESTARTSYS;
6930 #ifdef CONFIG_NFS_V4_1
6931 struct nfs4_lock_waiter {
6932 struct task_struct *task;
6933 struct inode *inode;
6934 struct nfs_lowner *owner;
6939 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
6942 struct nfs4_lock_waiter *waiter = wait->private;
6944 /* NULL key means to wake up everyone */
6946 struct cb_notify_lock_args *cbnl = key;
6947 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6948 *wowner = waiter->owner;
6950 /* Only wake if the callback was for the same owner. */
6951 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6954 /* Make sure it's for the right inode */
6955 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6958 waiter->notified = true;
6961 /* override "private" so we can use default_wake_function */
6962 wait->private = waiter->task;
6963 ret = autoremove_wake_function(wait, mode, flags, key);
6964 wait->private = waiter;
6969 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6971 int status = -ERESTARTSYS;
6972 unsigned long flags;
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 .notified = false };
6984 wait_queue_entry_t wait;
6986 /* Don't bother with waitqueue if we don't expect a callback */
6987 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6988 return nfs4_retry_setlk_simple(state, cmd, request);
6991 wait.private = &waiter;
6992 wait.func = nfs4_wake_lock_waiter;
6993 add_wait_queue(q, &wait);
6995 while(!signalled()) {
6996 waiter.notified = false;
6997 status = nfs4_proc_setlk(state, cmd, request);
6998 if ((status != -EAGAIN) || IS_SETLK(cmd))
7001 status = -ERESTARTSYS;
7002 spin_lock_irqsave(&q->lock, flags);
7003 if (waiter.notified) {
7004 spin_unlock_irqrestore(&q->lock, flags);
7007 set_current_state(TASK_INTERRUPTIBLE);
7008 spin_unlock_irqrestore(&q->lock, flags);
7010 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
7013 finish_wait(q, &wait);
7016 #else /* !CONFIG_NFS_V4_1 */
7018 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7020 return nfs4_retry_setlk_simple(state, cmd, request);
7025 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7027 struct nfs_open_context *ctx;
7028 struct nfs4_state *state;
7031 /* verify open state */
7032 ctx = nfs_file_open_context(filp);
7035 if (IS_GETLK(cmd)) {
7037 return nfs4_proc_getlk(state, F_GETLK, request);
7041 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7044 if (request->fl_type == F_UNLCK) {
7046 return nfs4_proc_unlck(state, cmd, request);
7053 if ((request->fl_flags & FL_POSIX) &&
7054 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7058 * Don't rely on the VFS having checked the file open mode,
7059 * since it won't do this for flock() locks.
7061 switch (request->fl_type) {
7063 if (!(filp->f_mode & FMODE_READ))
7067 if (!(filp->f_mode & FMODE_WRITE))
7071 status = nfs4_set_lock_state(state, request);
7075 return nfs4_retry_setlk(state, cmd, request);
7078 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7080 struct nfs_server *server = NFS_SERVER(state->inode);
7083 err = nfs4_set_lock_state(state, fl);
7086 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7087 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7090 struct nfs_release_lockowner_data {
7091 struct nfs4_lock_state *lsp;
7092 struct nfs_server *server;
7093 struct nfs_release_lockowner_args args;
7094 struct nfs_release_lockowner_res res;
7095 unsigned long timestamp;
7098 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7100 struct nfs_release_lockowner_data *data = calldata;
7101 struct nfs_server *server = data->server;
7102 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7103 &data->res.seq_res, task);
7104 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7105 data->timestamp = jiffies;
7108 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7110 struct nfs_release_lockowner_data *data = calldata;
7111 struct nfs_server *server = data->server;
7113 nfs40_sequence_done(task, &data->res.seq_res);
7115 switch (task->tk_status) {
7117 renew_lease(server, data->timestamp);
7119 case -NFS4ERR_STALE_CLIENTID:
7120 case -NFS4ERR_EXPIRED:
7121 nfs4_schedule_lease_recovery(server->nfs_client);
7123 case -NFS4ERR_LEASE_MOVED:
7124 case -NFS4ERR_DELAY:
7125 if (nfs4_async_handle_error(task, server,
7126 NULL, NULL) == -EAGAIN)
7127 rpc_restart_call_prepare(task);
7131 static void nfs4_release_lockowner_release(void *calldata)
7133 struct nfs_release_lockowner_data *data = calldata;
7134 nfs4_free_lock_state(data->server, data->lsp);
7138 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7139 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7140 .rpc_call_done = nfs4_release_lockowner_done,
7141 .rpc_release = nfs4_release_lockowner_release,
7145 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7147 struct nfs_release_lockowner_data *data;
7148 struct rpc_message msg = {
7149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7152 if (server->nfs_client->cl_mvops->minor_version != 0)
7155 data = kmalloc(sizeof(*data), GFP_NOFS);
7159 data->server = server;
7160 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7161 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7162 data->args.lock_owner.s_dev = server->s_dev;
7164 msg.rpc_argp = &data->args;
7165 msg.rpc_resp = &data->res;
7166 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7167 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7170 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7172 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7173 struct dentry *unused, struct inode *inode,
7174 const char *key, const void *buf,
7175 size_t buflen, int flags)
7177 return nfs4_proc_set_acl(inode, buf, buflen);
7180 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7181 struct dentry *unused, struct inode *inode,
7182 const char *key, void *buf, size_t buflen)
7184 return nfs4_proc_get_acl(inode, buf, buflen);
7187 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7189 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7192 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7194 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7195 struct dentry *unused, struct inode *inode,
7196 const char *key, const void *buf,
7197 size_t buflen, int flags)
7199 if (security_ismaclabel(key))
7200 return nfs4_set_security_label(inode, buf, buflen);
7205 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7206 struct dentry *unused, struct inode *inode,
7207 const char *key, void *buf, size_t buflen)
7209 if (security_ismaclabel(key))
7210 return nfs4_get_security_label(inode, buf, buflen);
7215 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7219 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7220 len = security_inode_listsecurity(inode, list, list_len);
7221 if (list_len && len > list_len)
7227 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7228 .prefix = XATTR_SECURITY_PREFIX,
7229 .get = nfs4_xattr_get_nfs4_label,
7230 .set = nfs4_xattr_set_nfs4_label,
7236 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7244 * nfs_fhget will use either the mounted_on_fileid or the fileid
7246 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7248 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7249 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7250 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7251 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7254 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7255 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7256 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7260 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7261 const struct qstr *name,
7262 struct nfs4_fs_locations *fs_locations,
7265 struct nfs_server *server = NFS_SERVER(dir);
7267 struct nfs4_fs_locations_arg args = {
7268 .dir_fh = NFS_FH(dir),
7273 struct nfs4_fs_locations_res res = {
7274 .fs_locations = fs_locations,
7276 struct rpc_message msg = {
7277 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7283 dprintk("%s: start\n", __func__);
7285 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7286 bitmask[1] = nfs4_fattr_bitmap[1];
7288 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7289 * is not supported */
7290 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7291 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7293 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7295 nfs_fattr_init(&fs_locations->fattr);
7296 fs_locations->server = server;
7297 fs_locations->nlocations = 0;
7298 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7299 dprintk("%s: returned status = %d\n", __func__, status);
7303 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7304 const struct qstr *name,
7305 struct nfs4_fs_locations *fs_locations,
7308 struct nfs4_exception exception = {
7309 .interruptible = true,
7313 err = _nfs4_proc_fs_locations(client, dir, name,
7314 fs_locations, page);
7315 trace_nfs4_get_fs_locations(dir, name, err);
7316 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7318 } while (exception.retry);
7323 * This operation also signals the server that this client is
7324 * performing migration recovery. The server can stop returning
7325 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7326 * appended to this compound to identify the client ID which is
7327 * performing recovery.
7329 static int _nfs40_proc_get_locations(struct inode *inode,
7330 struct nfs4_fs_locations *locations,
7331 struct page *page, const struct cred *cred)
7333 struct nfs_server *server = NFS_SERVER(inode);
7334 struct rpc_clnt *clnt = server->client;
7336 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7338 struct nfs4_fs_locations_arg args = {
7339 .clientid = server->nfs_client->cl_clientid,
7340 .fh = NFS_FH(inode),
7343 .migration = 1, /* skip LOOKUP */
7344 .renew = 1, /* append RENEW */
7346 struct nfs4_fs_locations_res res = {
7347 .fs_locations = locations,
7351 struct rpc_message msg = {
7352 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7357 unsigned long now = jiffies;
7360 nfs_fattr_init(&locations->fattr);
7361 locations->server = server;
7362 locations->nlocations = 0;
7364 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7365 status = nfs4_call_sync_sequence(clnt, server, &msg,
7366 &args.seq_args, &res.seq_res);
7370 renew_lease(server, now);
7374 #ifdef CONFIG_NFS_V4_1
7377 * This operation also signals the server that this client is
7378 * performing migration recovery. The server can stop asserting
7379 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7380 * performing this operation is identified in the SEQUENCE
7381 * operation in this compound.
7383 * When the client supports GETATTR(fs_locations_info), it can
7384 * be plumbed in here.
7386 static int _nfs41_proc_get_locations(struct inode *inode,
7387 struct nfs4_fs_locations *locations,
7388 struct page *page, const struct cred *cred)
7390 struct nfs_server *server = NFS_SERVER(inode);
7391 struct rpc_clnt *clnt = server->client;
7393 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7395 struct nfs4_fs_locations_arg args = {
7396 .fh = NFS_FH(inode),
7399 .migration = 1, /* skip LOOKUP */
7401 struct nfs4_fs_locations_res res = {
7402 .fs_locations = locations,
7405 struct rpc_message msg = {
7406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7413 nfs_fattr_init(&locations->fattr);
7414 locations->server = server;
7415 locations->nlocations = 0;
7417 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7418 status = nfs4_call_sync_sequence(clnt, server, &msg,
7419 &args.seq_args, &res.seq_res);
7420 if (status == NFS4_OK &&
7421 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7422 status = -NFS4ERR_LEASE_MOVED;
7426 #endif /* CONFIG_NFS_V4_1 */
7429 * nfs4_proc_get_locations - discover locations for a migrated FSID
7430 * @inode: inode on FSID that is migrating
7431 * @locations: result of query
7433 * @cred: credential to use for this operation
7435 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7436 * operation failed, or a negative errno if a local error occurred.
7438 * On success, "locations" is filled in, but if the server has
7439 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7442 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7443 * from this client that require migration recovery.
7445 int nfs4_proc_get_locations(struct inode *inode,
7446 struct nfs4_fs_locations *locations,
7447 struct page *page, const struct cred *cred)
7449 struct nfs_server *server = NFS_SERVER(inode);
7450 struct nfs_client *clp = server->nfs_client;
7451 const struct nfs4_mig_recovery_ops *ops =
7452 clp->cl_mvops->mig_recovery_ops;
7453 struct nfs4_exception exception = {
7454 .interruptible = true,
7458 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7459 (unsigned long long)server->fsid.major,
7460 (unsigned long long)server->fsid.minor,
7462 nfs_display_fhandle(NFS_FH(inode), __func__);
7465 status = ops->get_locations(inode, locations, page, cred);
7466 if (status != -NFS4ERR_DELAY)
7468 nfs4_handle_exception(server, status, &exception);
7469 } while (exception.retry);
7474 * This operation also signals the server that this client is
7475 * performing "lease moved" recovery. The server can stop
7476 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7477 * is appended to this compound to identify the client ID which is
7478 * performing recovery.
7480 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7482 struct nfs_server *server = NFS_SERVER(inode);
7483 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7484 struct rpc_clnt *clnt = server->client;
7485 struct nfs4_fsid_present_arg args = {
7486 .fh = NFS_FH(inode),
7487 .clientid = clp->cl_clientid,
7488 .renew = 1, /* append RENEW */
7490 struct nfs4_fsid_present_res res = {
7493 struct rpc_message msg = {
7494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7499 unsigned long now = jiffies;
7502 res.fh = nfs_alloc_fhandle();
7506 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7507 status = nfs4_call_sync_sequence(clnt, server, &msg,
7508 &args.seq_args, &res.seq_res);
7509 nfs_free_fhandle(res.fh);
7513 do_renew_lease(clp, now);
7517 #ifdef CONFIG_NFS_V4_1
7520 * This operation also signals the server that this client is
7521 * performing "lease moved" recovery. The server can stop asserting
7522 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7523 * this operation is identified in the SEQUENCE operation in this
7526 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7528 struct nfs_server *server = NFS_SERVER(inode);
7529 struct rpc_clnt *clnt = server->client;
7530 struct nfs4_fsid_present_arg args = {
7531 .fh = NFS_FH(inode),
7533 struct nfs4_fsid_present_res res = {
7535 struct rpc_message msg = {
7536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7543 res.fh = nfs_alloc_fhandle();
7547 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7548 status = nfs4_call_sync_sequence(clnt, server, &msg,
7549 &args.seq_args, &res.seq_res);
7550 nfs_free_fhandle(res.fh);
7551 if (status == NFS4_OK &&
7552 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7553 status = -NFS4ERR_LEASE_MOVED;
7557 #endif /* CONFIG_NFS_V4_1 */
7560 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7561 * @inode: inode on FSID to check
7562 * @cred: credential to use for this operation
7564 * Server indicates whether the FSID is present, moved, or not
7565 * recognized. This operation is necessary to clear a LEASE_MOVED
7566 * condition for this client ID.
7568 * Returns NFS4_OK if the FSID is present on this server,
7569 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7570 * NFS4ERR code if some error occurred on the server, or a
7571 * negative errno if a local failure occurred.
7573 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
7575 struct nfs_server *server = NFS_SERVER(inode);
7576 struct nfs_client *clp = server->nfs_client;
7577 const struct nfs4_mig_recovery_ops *ops =
7578 clp->cl_mvops->mig_recovery_ops;
7579 struct nfs4_exception exception = {
7580 .interruptible = true,
7584 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7585 (unsigned long long)server->fsid.major,
7586 (unsigned long long)server->fsid.minor,
7588 nfs_display_fhandle(NFS_FH(inode), __func__);
7591 status = ops->fsid_present(inode, cred);
7592 if (status != -NFS4ERR_DELAY)
7594 nfs4_handle_exception(server, status, &exception);
7595 } while (exception.retry);
7600 * If 'use_integrity' is true and the state managment nfs_client
7601 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7602 * and the machine credential as per RFC3530bis and RFC5661 Security
7603 * Considerations sections. Otherwise, just use the user cred with the
7604 * filesystem's rpc_client.
7606 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7609 struct nfs4_secinfo_arg args = {
7610 .dir_fh = NFS_FH(dir),
7613 struct nfs4_secinfo_res res = {
7616 struct rpc_message msg = {
7617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7621 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7622 const struct cred *cred = NULL;
7624 if (use_integrity) {
7625 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7626 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7627 msg.rpc_cred = cred;
7630 dprintk("NFS call secinfo %s\n", name->name);
7632 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7633 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7635 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7637 dprintk("NFS reply secinfo: %d\n", status);
7644 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7645 struct nfs4_secinfo_flavors *flavors)
7647 struct nfs4_exception exception = {
7648 .interruptible = true,
7652 err = -NFS4ERR_WRONGSEC;
7654 /* try to use integrity protection with machine cred */
7655 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7656 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7659 * if unable to use integrity protection, or SECINFO with
7660 * integrity protection returns NFS4ERR_WRONGSEC (which is
7661 * disallowed by spec, but exists in deployed servers) use
7662 * the current filesystem's rpc_client and the user cred.
7664 if (err == -NFS4ERR_WRONGSEC)
7665 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7667 trace_nfs4_secinfo(dir, name, err);
7668 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7670 } while (exception.retry);
7674 #ifdef CONFIG_NFS_V4_1
7676 * Check the exchange flags returned by the server for invalid flags, having
7677 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7680 static int nfs4_check_cl_exchange_flags(u32 flags)
7682 if (flags & ~EXCHGID4_FLAG_MASK_R)
7684 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7685 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7687 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7691 return -NFS4ERR_INVAL;
7695 nfs41_same_server_scope(struct nfs41_server_scope *a,
7696 struct nfs41_server_scope *b)
7698 if (a->server_scope_sz != b->server_scope_sz)
7700 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7704 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7708 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7709 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7713 * nfs4_proc_bind_one_conn_to_session()
7715 * The 4.1 client currently uses the same TCP connection for the
7716 * fore and backchannel.
7719 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7720 struct rpc_xprt *xprt,
7721 struct nfs_client *clp,
7722 const struct cred *cred)
7725 struct nfs41_bind_conn_to_session_args args = {
7727 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7729 struct nfs41_bind_conn_to_session_res res;
7730 struct rpc_message msg = {
7732 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7737 struct rpc_task_setup task_setup_data = {
7740 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7741 .rpc_message = &msg,
7742 .flags = RPC_TASK_TIMEOUT,
7744 struct rpc_task *task;
7746 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7747 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7748 args.dir = NFS4_CDFC4_FORE;
7750 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7751 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7752 args.dir = NFS4_CDFC4_FORE;
7754 task = rpc_run_task(&task_setup_data);
7755 if (!IS_ERR(task)) {
7756 status = task->tk_status;
7759 status = PTR_ERR(task);
7760 trace_nfs4_bind_conn_to_session(clp, status);
7762 if (memcmp(res.sessionid.data,
7763 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7764 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7767 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7768 dprintk("NFS: %s: Unexpected direction from server\n",
7772 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7773 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7782 struct rpc_bind_conn_calldata {
7783 struct nfs_client *clp;
7784 const struct cred *cred;
7788 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7789 struct rpc_xprt *xprt,
7792 struct rpc_bind_conn_calldata *p = calldata;
7794 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7797 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
7799 struct rpc_bind_conn_calldata data = {
7803 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7804 nfs4_proc_bind_conn_to_session_callback, &data);
7808 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7809 * and operations we'd like to see to enable certain features in the allow map
7811 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7812 .how = SP4_MACH_CRED,
7813 .enforce.u.words = {
7814 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7815 1 << (OP_EXCHANGE_ID - 32) |
7816 1 << (OP_CREATE_SESSION - 32) |
7817 1 << (OP_DESTROY_SESSION - 32) |
7818 1 << (OP_DESTROY_CLIENTID - 32)
7821 [0] = 1 << (OP_CLOSE) |
7822 1 << (OP_OPEN_DOWNGRADE) |
7824 1 << (OP_DELEGRETURN) |
7826 [1] = 1 << (OP_SECINFO - 32) |
7827 1 << (OP_SECINFO_NO_NAME - 32) |
7828 1 << (OP_LAYOUTRETURN - 32) |
7829 1 << (OP_TEST_STATEID - 32) |
7830 1 << (OP_FREE_STATEID - 32) |
7831 1 << (OP_WRITE - 32)
7836 * Select the state protection mode for client `clp' given the server results
7837 * from exchange_id in `sp'.
7839 * Returns 0 on success, negative errno otherwise.
7841 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7842 struct nfs41_state_protection *sp)
7844 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7845 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7846 1 << (OP_EXCHANGE_ID - 32) |
7847 1 << (OP_CREATE_SESSION - 32) |
7848 1 << (OP_DESTROY_SESSION - 32) |
7849 1 << (OP_DESTROY_CLIENTID - 32)
7851 unsigned long flags = 0;
7855 if (sp->how == SP4_MACH_CRED) {
7856 /* Print state protect result */
7857 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7858 for (i = 0; i <= LAST_NFS4_OP; i++) {
7859 if (test_bit(i, sp->enforce.u.longs))
7860 dfprintk(MOUNT, " enforce op %d\n", i);
7861 if (test_bit(i, sp->allow.u.longs))
7862 dfprintk(MOUNT, " allow op %d\n", i);
7865 /* make sure nothing is on enforce list that isn't supported */
7866 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7867 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7868 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7875 * Minimal mode - state operations are allowed to use machine
7876 * credential. Note this already happens by default, so the
7877 * client doesn't have to do anything more than the negotiation.
7879 * NOTE: we don't care if EXCHANGE_ID is in the list -
7880 * we're already using the machine cred for exchange_id
7881 * and will never use a different cred.
7883 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7884 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7885 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7886 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7887 dfprintk(MOUNT, "sp4_mach_cred:\n");
7888 dfprintk(MOUNT, " minimal mode enabled\n");
7889 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
7891 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7896 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7897 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7898 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7899 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7900 dfprintk(MOUNT, " cleanup mode enabled\n");
7901 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
7904 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7905 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7906 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
7909 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7910 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7911 dfprintk(MOUNT, " secinfo mode enabled\n");
7912 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
7915 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7916 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7917 dfprintk(MOUNT, " stateid mode enabled\n");
7918 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
7921 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7922 dfprintk(MOUNT, " write mode enabled\n");
7923 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
7926 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7927 dfprintk(MOUNT, " commit mode enabled\n");
7928 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
7932 clp->cl_sp4_flags = flags;
7936 struct nfs41_exchange_id_data {
7937 struct nfs41_exchange_id_res res;
7938 struct nfs41_exchange_id_args args;
7941 static void nfs4_exchange_id_release(void *data)
7943 struct nfs41_exchange_id_data *cdata =
7944 (struct nfs41_exchange_id_data *)data;
7946 nfs_put_client(cdata->args.client);
7947 kfree(cdata->res.impl_id);
7948 kfree(cdata->res.server_scope);
7949 kfree(cdata->res.server_owner);
7953 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7954 .rpc_release = nfs4_exchange_id_release,
7958 * _nfs4_proc_exchange_id()
7960 * Wrapper for EXCHANGE_ID operation.
7962 static struct rpc_task *
7963 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
7964 u32 sp4_how, struct rpc_xprt *xprt)
7966 struct rpc_message msg = {
7967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7970 struct rpc_task_setup task_setup_data = {
7971 .rpc_client = clp->cl_rpcclient,
7972 .callback_ops = &nfs4_exchange_id_call_ops,
7973 .rpc_message = &msg,
7974 .flags = RPC_TASK_TIMEOUT,
7976 struct nfs41_exchange_id_data *calldata;
7979 if (!refcount_inc_not_zero(&clp->cl_count))
7980 return ERR_PTR(-EIO);
7983 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7987 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
7989 status = nfs4_init_uniform_client_string(clp);
7993 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7996 if (unlikely(calldata->res.server_owner == NULL))
7999 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8001 if (unlikely(calldata->res.server_scope == NULL))
8002 goto out_server_owner;
8004 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8005 if (unlikely(calldata->res.impl_id == NULL))
8006 goto out_server_scope;
8010 calldata->args.state_protect.how = SP4_NONE;
8014 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8024 task_setup_data.rpc_xprt = xprt;
8025 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8026 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8027 sizeof(calldata->args.verifier.data));
8029 calldata->args.client = clp;
8030 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8031 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8032 #ifdef CONFIG_NFS_V4_1_MIGRATION
8033 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8035 msg.rpc_argp = &calldata->args;
8036 msg.rpc_resp = &calldata->res;
8037 task_setup_data.callback_data = calldata;
8039 return rpc_run_task(&task_setup_data);
8042 kfree(calldata->res.impl_id);
8044 kfree(calldata->res.server_scope);
8046 kfree(calldata->res.server_owner);
8050 nfs_put_client(clp);
8051 return ERR_PTR(status);
8055 * _nfs4_proc_exchange_id()
8057 * Wrapper for EXCHANGE_ID operation.
8059 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8062 struct rpc_task *task;
8063 struct nfs41_exchange_id_args *argp;
8064 struct nfs41_exchange_id_res *resp;
8067 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8069 return PTR_ERR(task);
8071 argp = task->tk_msg.rpc_argp;
8072 resp = task->tk_msg.rpc_resp;
8073 status = task->tk_status;
8077 status = nfs4_check_cl_exchange_flags(resp->flags);
8081 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8085 clp->cl_clientid = resp->clientid;
8086 clp->cl_exchange_flags = resp->flags;
8087 clp->cl_seqid = resp->seqid;
8088 /* Client ID is not confirmed */
8089 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8090 clear_bit(NFS4_SESSION_ESTABLISHED,
8091 &clp->cl_session->session_state);
8093 if (clp->cl_serverscope != NULL &&
8094 !nfs41_same_server_scope(clp->cl_serverscope,
8095 resp->server_scope)) {
8096 dprintk("%s: server_scope mismatch detected\n",
8098 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8101 swap(clp->cl_serverowner, resp->server_owner);
8102 swap(clp->cl_serverscope, resp->server_scope);
8103 swap(clp->cl_implid, resp->impl_id);
8105 /* Save the EXCHANGE_ID verifier session trunk tests */
8106 memcpy(clp->cl_confirm.data, argp->verifier.data,
8107 sizeof(clp->cl_confirm.data));
8109 trace_nfs4_exchange_id(clp, status);
8115 * nfs4_proc_exchange_id()
8117 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8119 * Since the clientid has expired, all compounds using sessions
8120 * associated with the stale clientid will be returning
8121 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8122 * be in some phase of session reset.
8124 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8126 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8128 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8131 /* try SP4_MACH_CRED if krb5i/p */
8132 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8133 authflavor == RPC_AUTH_GSS_KRB5P) {
8134 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8140 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8144 * nfs4_test_session_trunk
8146 * This is an add_xprt_test() test function called from
8147 * rpc_clnt_setup_test_and_add_xprt.
8149 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8150 * and is dereferrenced in nfs4_exchange_id_release
8152 * Upon success, add the new transport to the rpc_clnt
8154 * @clnt: struct rpc_clnt to get new transport
8155 * @xprt: the rpc_xprt to test
8156 * @data: call data for _nfs4_proc_exchange_id.
8158 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8161 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8162 struct rpc_task *task;
8167 dprintk("--> %s try %s\n", __func__,
8168 xprt->address_strings[RPC_DISPLAY_ADDR]);
8170 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8172 /* Test connection for session trunking. Async exchange_id call */
8173 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8177 status = task->tk_status;
8179 status = nfs4_detect_session_trunking(adata->clp,
8180 task->tk_msg.rpc_resp, xprt);
8183 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8187 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8189 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8190 const struct cred *cred)
8192 struct rpc_message msg = {
8193 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8199 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8200 trace_nfs4_destroy_clientid(clp, status);
8202 dprintk("NFS: Got error %d from the server %s on "
8203 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8207 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8208 const struct cred *cred)
8213 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8214 ret = _nfs4_proc_destroy_clientid(clp, cred);
8216 case -NFS4ERR_DELAY:
8217 case -NFS4ERR_CLIENTID_BUSY:
8227 int nfs4_destroy_clientid(struct nfs_client *clp)
8229 const struct cred *cred;
8232 if (clp->cl_mvops->minor_version < 1)
8234 if (clp->cl_exchange_flags == 0)
8236 if (clp->cl_preserve_clid)
8238 cred = nfs4_get_clid_cred(clp);
8239 ret = nfs4_proc_destroy_clientid(clp, cred);
8243 case -NFS4ERR_STALE_CLIENTID:
8244 clp->cl_exchange_flags = 0;
8250 struct nfs4_get_lease_time_data {
8251 struct nfs4_get_lease_time_args *args;
8252 struct nfs4_get_lease_time_res *res;
8253 struct nfs_client *clp;
8256 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8259 struct nfs4_get_lease_time_data *data =
8260 (struct nfs4_get_lease_time_data *)calldata;
8262 dprintk("--> %s\n", __func__);
8263 /* just setup sequence, do not trigger session recovery
8264 since we're invoked within one */
8265 nfs4_setup_sequence(data->clp,
8266 &data->args->la_seq_args,
8267 &data->res->lr_seq_res,
8269 dprintk("<-- %s\n", __func__);
8273 * Called from nfs4_state_manager thread for session setup, so don't recover
8274 * from sequence operation or clientid errors.
8276 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8278 struct nfs4_get_lease_time_data *data =
8279 (struct nfs4_get_lease_time_data *)calldata;
8281 dprintk("--> %s\n", __func__);
8282 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8284 switch (task->tk_status) {
8285 case -NFS4ERR_DELAY:
8286 case -NFS4ERR_GRACE:
8287 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8288 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8289 task->tk_status = 0;
8291 case -NFS4ERR_RETRY_UNCACHED_REP:
8292 rpc_restart_call_prepare(task);
8295 dprintk("<-- %s\n", __func__);
8298 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8299 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8300 .rpc_call_done = nfs4_get_lease_time_done,
8303 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8305 struct rpc_task *task;
8306 struct nfs4_get_lease_time_args args;
8307 struct nfs4_get_lease_time_res res = {
8308 .lr_fsinfo = fsinfo,
8310 struct nfs4_get_lease_time_data data = {
8315 struct rpc_message msg = {
8316 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8320 struct rpc_task_setup task_setup = {
8321 .rpc_client = clp->cl_rpcclient,
8322 .rpc_message = &msg,
8323 .callback_ops = &nfs4_get_lease_time_ops,
8324 .callback_data = &data,
8325 .flags = RPC_TASK_TIMEOUT,
8329 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8330 task = rpc_run_task(&task_setup);
8333 return PTR_ERR(task);
8335 status = task->tk_status;
8341 * Initialize the values to be used by the client in CREATE_SESSION
8342 * If nfs4_init_session set the fore channel request and response sizes,
8345 * Set the back channel max_resp_sz_cached to zero to force the client to
8346 * always set csa_cachethis to FALSE because the current implementation
8347 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8349 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8350 struct rpc_clnt *clnt)
8352 unsigned int max_rqst_sz, max_resp_sz;
8353 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8355 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8356 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8358 /* Fore channel attributes */
8359 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8360 args->fc_attrs.max_resp_sz = max_resp_sz;
8361 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8362 args->fc_attrs.max_reqs = max_session_slots;
8364 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8365 "max_ops=%u max_reqs=%u\n",
8367 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8368 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8370 /* Back channel attributes */
8371 args->bc_attrs.max_rqst_sz = max_bc_payload;
8372 args->bc_attrs.max_resp_sz = max_bc_payload;
8373 args->bc_attrs.max_resp_sz_cached = 0;
8374 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8375 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8377 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8378 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8380 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8381 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8382 args->bc_attrs.max_reqs);
8385 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8386 struct nfs41_create_session_res *res)
8388 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8389 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8391 if (rcvd->max_resp_sz > sent->max_resp_sz)
8394 * Our requested max_ops is the minimum we need; we're not
8395 * prepared to break up compounds into smaller pieces than that.
8396 * So, no point even trying to continue if the server won't
8399 if (rcvd->max_ops < sent->max_ops)
8401 if (rcvd->max_reqs == 0)
8403 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8404 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8408 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8409 struct nfs41_create_session_res *res)
8411 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8412 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8414 if (!(res->flags & SESSION4_BACK_CHAN))
8416 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8418 if (rcvd->max_resp_sz < sent->max_resp_sz)
8420 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8422 if (rcvd->max_ops > sent->max_ops)
8424 if (rcvd->max_reqs > sent->max_reqs)
8430 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8431 struct nfs41_create_session_res *res)
8435 ret = nfs4_verify_fore_channel_attrs(args, res);
8438 return nfs4_verify_back_channel_attrs(args, res);
8441 static void nfs4_update_session(struct nfs4_session *session,
8442 struct nfs41_create_session_res *res)
8444 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8445 /* Mark client id and session as being confirmed */
8446 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8447 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8448 session->flags = res->flags;
8449 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8450 if (res->flags & SESSION4_BACK_CHAN)
8451 memcpy(&session->bc_attrs, &res->bc_attrs,
8452 sizeof(session->bc_attrs));
8455 static int _nfs4_proc_create_session(struct nfs_client *clp,
8456 const struct cred *cred)
8458 struct nfs4_session *session = clp->cl_session;
8459 struct nfs41_create_session_args args = {
8461 .clientid = clp->cl_clientid,
8462 .seqid = clp->cl_seqid,
8463 .cb_program = NFS4_CALLBACK,
8465 struct nfs41_create_session_res res;
8467 struct rpc_message msg = {
8468 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8475 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8476 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8478 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8479 trace_nfs4_create_session(clp, status);
8482 case -NFS4ERR_STALE_CLIENTID:
8483 case -NFS4ERR_DELAY:
8492 /* Verify the session's negotiated channel_attrs values */
8493 status = nfs4_verify_channel_attrs(&args, &res);
8494 /* Increment the clientid slot sequence id */
8497 nfs4_update_session(session, &res);
8504 * Issues a CREATE_SESSION operation to the server.
8505 * It is the responsibility of the caller to verify the session is
8506 * expired before calling this routine.
8508 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8512 struct nfs4_session *session = clp->cl_session;
8514 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8516 status = _nfs4_proc_create_session(clp, cred);
8520 /* Init or reset the session slot tables */
8521 status = nfs4_setup_session_slot_tables(session);
8522 dprintk("slot table setup returned %d\n", status);
8526 ptr = (unsigned *)&session->sess_id.data[0];
8527 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8528 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8530 dprintk("<-- %s\n", __func__);
8535 * Issue the over-the-wire RPC DESTROY_SESSION.
8536 * The caller must serialize access to this routine.
8538 int nfs4_proc_destroy_session(struct nfs4_session *session,
8539 const struct cred *cred)
8541 struct rpc_message msg = {
8542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8543 .rpc_argp = session,
8548 dprintk("--> nfs4_proc_destroy_session\n");
8550 /* session is still being setup */
8551 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8554 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8555 trace_nfs4_destroy_session(session->clp, status);
8558 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8559 "Session has been destroyed regardless...\n", status);
8561 dprintk("<-- nfs4_proc_destroy_session\n");
8566 * Renew the cl_session lease.
8568 struct nfs4_sequence_data {
8569 struct nfs_client *clp;
8570 struct nfs4_sequence_args args;
8571 struct nfs4_sequence_res res;
8574 static void nfs41_sequence_release(void *data)
8576 struct nfs4_sequence_data *calldata = data;
8577 struct nfs_client *clp = calldata->clp;
8579 if (refcount_read(&clp->cl_count) > 1)
8580 nfs4_schedule_state_renewal(clp);
8581 nfs_put_client(clp);
8585 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8587 switch(task->tk_status) {
8588 case -NFS4ERR_DELAY:
8589 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8592 nfs4_schedule_lease_recovery(clp);
8597 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8599 struct nfs4_sequence_data *calldata = data;
8600 struct nfs_client *clp = calldata->clp;
8602 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8605 trace_nfs4_sequence(clp, task->tk_status);
8606 if (task->tk_status < 0) {
8607 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8608 if (refcount_read(&clp->cl_count) == 1)
8611 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8612 rpc_restart_call_prepare(task);
8616 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8618 dprintk("<-- %s\n", __func__);
8621 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8623 struct nfs4_sequence_data *calldata = data;
8624 struct nfs_client *clp = calldata->clp;
8625 struct nfs4_sequence_args *args;
8626 struct nfs4_sequence_res *res;
8628 args = task->tk_msg.rpc_argp;
8629 res = task->tk_msg.rpc_resp;
8631 nfs4_setup_sequence(clp, args, res, task);
8634 static const struct rpc_call_ops nfs41_sequence_ops = {
8635 .rpc_call_done = nfs41_sequence_call_done,
8636 .rpc_call_prepare = nfs41_sequence_prepare,
8637 .rpc_release = nfs41_sequence_release,
8640 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8641 const struct cred *cred,
8642 struct nfs4_slot *slot,
8645 struct nfs4_sequence_data *calldata;
8646 struct rpc_message msg = {
8647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8650 struct rpc_task_setup task_setup_data = {
8651 .rpc_client = clp->cl_rpcclient,
8652 .rpc_message = &msg,
8653 .callback_ops = &nfs41_sequence_ops,
8654 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8656 struct rpc_task *ret;
8658 ret = ERR_PTR(-EIO);
8659 if (!refcount_inc_not_zero(&clp->cl_count))
8662 ret = ERR_PTR(-ENOMEM);
8663 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8664 if (calldata == NULL)
8666 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
8667 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8668 msg.rpc_argp = &calldata->args;
8669 msg.rpc_resp = &calldata->res;
8670 calldata->clp = clp;
8671 task_setup_data.callback_data = calldata;
8673 ret = rpc_run_task(&task_setup_data);
8678 nfs_put_client(clp);
8680 nfs41_release_slot(slot);
8684 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
8686 struct rpc_task *task;
8689 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8691 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8693 ret = PTR_ERR(task);
8695 rpc_put_task_async(task);
8696 dprintk("<-- %s status=%d\n", __func__, ret);
8700 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
8702 struct rpc_task *task;
8705 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8707 ret = PTR_ERR(task);
8710 ret = rpc_wait_for_completion_task(task);
8712 ret = task->tk_status;
8715 dprintk("<-- %s status=%d\n", __func__, ret);
8719 struct nfs4_reclaim_complete_data {
8720 struct nfs_client *clp;
8721 struct nfs41_reclaim_complete_args arg;
8722 struct nfs41_reclaim_complete_res res;
8725 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8727 struct nfs4_reclaim_complete_data *calldata = data;
8729 nfs4_setup_sequence(calldata->clp,
8730 &calldata->arg.seq_args,
8731 &calldata->res.seq_res,
8735 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8737 switch(task->tk_status) {
8739 wake_up_all(&clp->cl_lock_waitq);
8741 case -NFS4ERR_COMPLETE_ALREADY:
8742 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8744 case -NFS4ERR_DELAY:
8745 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8747 case -NFS4ERR_RETRY_UNCACHED_REP:
8749 case -NFS4ERR_BADSESSION:
8750 case -NFS4ERR_DEADSESSION:
8751 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8752 nfs4_schedule_session_recovery(clp->cl_session,
8756 nfs4_schedule_lease_recovery(clp);
8761 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8763 struct nfs4_reclaim_complete_data *calldata = data;
8764 struct nfs_client *clp = calldata->clp;
8765 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8767 dprintk("--> %s\n", __func__);
8768 if (!nfs41_sequence_done(task, res))
8771 trace_nfs4_reclaim_complete(clp, task->tk_status);
8772 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8773 rpc_restart_call_prepare(task);
8776 dprintk("<-- %s\n", __func__);
8779 static void nfs4_free_reclaim_complete_data(void *data)
8781 struct nfs4_reclaim_complete_data *calldata = data;
8786 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8787 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8788 .rpc_call_done = nfs4_reclaim_complete_done,
8789 .rpc_release = nfs4_free_reclaim_complete_data,
8793 * Issue a global reclaim complete.
8795 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8796 const struct cred *cred)
8798 struct nfs4_reclaim_complete_data *calldata;
8799 struct rpc_task *task;
8800 struct rpc_message msg = {
8801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8804 struct rpc_task_setup task_setup_data = {
8805 .rpc_client = clp->cl_rpcclient,
8806 .rpc_message = &msg,
8807 .callback_ops = &nfs4_reclaim_complete_call_ops,
8808 .flags = RPC_TASK_ASYNC,
8810 int status = -ENOMEM;
8812 dprintk("--> %s\n", __func__);
8813 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8814 if (calldata == NULL)
8816 calldata->clp = clp;
8817 calldata->arg.one_fs = 0;
8819 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
8820 msg.rpc_argp = &calldata->arg;
8821 msg.rpc_resp = &calldata->res;
8822 task_setup_data.callback_data = calldata;
8823 task = rpc_run_task(&task_setup_data);
8825 status = PTR_ERR(task);
8828 status = rpc_wait_for_completion_task(task);
8830 status = task->tk_status;
8833 dprintk("<-- %s status=%d\n", __func__, status);
8838 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8840 struct nfs4_layoutget *lgp = calldata;
8841 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8843 dprintk("--> %s\n", __func__);
8844 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8845 &lgp->res.seq_res, task);
8846 dprintk("<-- %s\n", __func__);
8849 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8851 struct nfs4_layoutget *lgp = calldata;
8853 dprintk("--> %s\n", __func__);
8854 nfs41_sequence_process(task, &lgp->res.seq_res);
8855 dprintk("<-- %s\n", __func__);
8859 nfs4_layoutget_handle_exception(struct rpc_task *task,
8860 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8862 struct inode *inode = lgp->args.inode;
8863 struct nfs_server *server = NFS_SERVER(inode);
8864 struct pnfs_layout_hdr *lo;
8865 int nfs4err = task->tk_status;
8866 int err, status = 0;
8869 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8871 nfs4_sequence_free_slot(&lgp->res.seq_res);
8878 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8879 * on the file. set tk_status to -ENODATA to tell upper layer to
8882 case -NFS4ERR_LAYOUTUNAVAILABLE:
8886 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8887 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8889 case -NFS4ERR_BADLAYOUT:
8890 status = -EOVERFLOW;
8893 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8894 * (or clients) writing to the same RAID stripe except when
8895 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8897 * Treat it like we would RECALLCONFLICT -- we retry for a little
8898 * while, and then eventually give up.
8900 case -NFS4ERR_LAYOUTTRYLATER:
8901 if (lgp->args.minlength == 0) {
8902 status = -EOVERFLOW;
8907 case -NFS4ERR_RECALLCONFLICT:
8908 status = -ERECALLCONFLICT;
8910 case -NFS4ERR_DELEG_REVOKED:
8911 case -NFS4ERR_ADMIN_REVOKED:
8912 case -NFS4ERR_EXPIRED:
8913 case -NFS4ERR_BAD_STATEID:
8914 exception->timeout = 0;
8915 spin_lock(&inode->i_lock);
8916 lo = NFS_I(inode)->layout;
8917 /* If the open stateid was bad, then recover it. */
8918 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8919 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
8920 spin_unlock(&inode->i_lock);
8921 exception->state = lgp->args.ctx->state;
8922 exception->stateid = &lgp->args.stateid;
8927 * Mark the bad layout state as invalid, then retry
8929 pnfs_mark_layout_stateid_invalid(lo, &head);
8930 spin_unlock(&inode->i_lock);
8931 nfs_commit_inode(inode, 0);
8932 pnfs_free_lseg_list(&head);
8937 err = nfs4_handle_exception(server, nfs4err, exception);
8939 if (exception->retry)
8945 dprintk("<-- %s\n", __func__);
8949 size_t max_response_pages(struct nfs_server *server)
8951 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8952 return nfs_page_array_len(0, max_resp_sz);
8955 static void nfs4_layoutget_release(void *calldata)
8957 struct nfs4_layoutget *lgp = calldata;
8959 dprintk("--> %s\n", __func__);
8960 nfs4_sequence_free_slot(&lgp->res.seq_res);
8961 pnfs_layoutget_free(lgp);
8962 dprintk("<-- %s\n", __func__);
8965 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8966 .rpc_call_prepare = nfs4_layoutget_prepare,
8967 .rpc_call_done = nfs4_layoutget_done,
8968 .rpc_release = nfs4_layoutget_release,
8971 struct pnfs_layout_segment *
8972 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
8974 struct inode *inode = lgp->args.inode;
8975 struct nfs_server *server = NFS_SERVER(inode);
8976 struct rpc_task *task;
8977 struct rpc_message msg = {
8978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8979 .rpc_argp = &lgp->args,
8980 .rpc_resp = &lgp->res,
8981 .rpc_cred = lgp->cred,
8983 struct rpc_task_setup task_setup_data = {
8984 .rpc_client = server->client,
8985 .rpc_message = &msg,
8986 .callback_ops = &nfs4_layoutget_call_ops,
8987 .callback_data = lgp,
8988 .flags = RPC_TASK_ASYNC,
8990 struct pnfs_layout_segment *lseg = NULL;
8991 struct nfs4_exception exception = {
8993 .timeout = *timeout,
8997 dprintk("--> %s\n", __func__);
8999 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9000 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9002 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9004 task = rpc_run_task(&task_setup_data);
9006 return ERR_CAST(task);
9007 status = rpc_wait_for_completion_task(task);
9011 if (task->tk_status < 0) {
9012 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9013 *timeout = exception.timeout;
9014 } else if (lgp->res.layoutp->len == 0) {
9016 *timeout = nfs4_update_delay(&exception.timeout);
9018 lseg = pnfs_layout_process(lgp);
9020 trace_nfs4_layoutget(lgp->args.ctx,
9027 dprintk("<-- %s status=%d\n", __func__, status);
9029 return ERR_PTR(status);
9034 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9036 struct nfs4_layoutreturn *lrp = calldata;
9038 dprintk("--> %s\n", __func__);
9039 nfs4_setup_sequence(lrp->clp,
9040 &lrp->args.seq_args,
9043 if (!pnfs_layout_is_valid(lrp->args.layout))
9047 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9049 struct nfs4_layoutreturn *lrp = calldata;
9050 struct nfs_server *server;
9052 dprintk("--> %s\n", __func__);
9054 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9057 server = NFS_SERVER(lrp->args.inode);
9058 switch (task->tk_status) {
9059 case -NFS4ERR_OLD_STATEID:
9060 if (nfs4_layoutreturn_refresh_stateid(&lrp->args.stateid,
9066 task->tk_status = 0;
9070 case -NFS4ERR_DELAY:
9071 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9075 dprintk("<-- %s\n", __func__);
9078 task->tk_status = 0;
9079 nfs4_sequence_free_slot(&lrp->res.seq_res);
9080 rpc_restart_call_prepare(task);
9083 static void nfs4_layoutreturn_release(void *calldata)
9085 struct nfs4_layoutreturn *lrp = calldata;
9086 struct pnfs_layout_hdr *lo = lrp->args.layout;
9088 dprintk("--> %s\n", __func__);
9089 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9090 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9091 nfs4_sequence_free_slot(&lrp->res.seq_res);
9092 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9093 lrp->ld_private.ops->free(&lrp->ld_private);
9094 pnfs_put_layout_hdr(lrp->args.layout);
9095 nfs_iput_and_deactive(lrp->inode);
9097 dprintk("<-- %s\n", __func__);
9100 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9101 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9102 .rpc_call_done = nfs4_layoutreturn_done,
9103 .rpc_release = nfs4_layoutreturn_release,
9106 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9108 struct rpc_task *task;
9109 struct rpc_message msg = {
9110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9111 .rpc_argp = &lrp->args,
9112 .rpc_resp = &lrp->res,
9113 .rpc_cred = lrp->cred,
9115 struct rpc_task_setup task_setup_data = {
9116 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9117 .rpc_message = &msg,
9118 .callback_ops = &nfs4_layoutreturn_call_ops,
9119 .callback_data = lrp,
9123 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9124 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9125 &task_setup_data.rpc_client, &msg);
9127 dprintk("--> %s\n", __func__);
9129 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9131 nfs4_layoutreturn_release(lrp);
9134 task_setup_data.flags |= RPC_TASK_ASYNC;
9136 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9137 task = rpc_run_task(&task_setup_data);
9139 return PTR_ERR(task);
9141 status = task->tk_status;
9142 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9143 dprintk("<-- %s status=%d\n", __func__, status);
9149 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9150 struct pnfs_device *pdev,
9151 const struct cred *cred)
9153 struct nfs4_getdeviceinfo_args args = {
9155 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9156 NOTIFY_DEVICEID4_DELETE,
9158 struct nfs4_getdeviceinfo_res res = {
9161 struct rpc_message msg = {
9162 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9169 dprintk("--> %s\n", __func__);
9170 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9171 if (res.notification & ~args.notify_types)
9172 dprintk("%s: unsupported notification\n", __func__);
9173 if (res.notification != args.notify_types)
9176 dprintk("<-- %s status=%d\n", __func__, status);
9181 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9182 struct pnfs_device *pdev,
9183 const struct cred *cred)
9185 struct nfs4_exception exception = { };
9189 err = nfs4_handle_exception(server,
9190 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9192 } while (exception.retry);
9195 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9197 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9199 struct nfs4_layoutcommit_data *data = calldata;
9200 struct nfs_server *server = NFS_SERVER(data->args.inode);
9202 nfs4_setup_sequence(server->nfs_client,
9203 &data->args.seq_args,
9209 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9211 struct nfs4_layoutcommit_data *data = calldata;
9212 struct nfs_server *server = NFS_SERVER(data->args.inode);
9214 if (!nfs41_sequence_done(task, &data->res.seq_res))
9217 switch (task->tk_status) { /* Just ignore these failures */
9218 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9219 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9220 case -NFS4ERR_BADLAYOUT: /* no layout */
9221 case -NFS4ERR_GRACE: /* loca_recalim always false */
9222 task->tk_status = 0;
9226 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9227 rpc_restart_call_prepare(task);
9233 static void nfs4_layoutcommit_release(void *calldata)
9235 struct nfs4_layoutcommit_data *data = calldata;
9237 pnfs_cleanup_layoutcommit(data);
9238 nfs_post_op_update_inode_force_wcc(data->args.inode,
9240 put_cred(data->cred);
9241 nfs_iput_and_deactive(data->inode);
9245 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9246 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9247 .rpc_call_done = nfs4_layoutcommit_done,
9248 .rpc_release = nfs4_layoutcommit_release,
9252 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9254 struct rpc_message msg = {
9255 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9256 .rpc_argp = &data->args,
9257 .rpc_resp = &data->res,
9258 .rpc_cred = data->cred,
9260 struct rpc_task_setup task_setup_data = {
9261 .task = &data->task,
9262 .rpc_client = NFS_CLIENT(data->args.inode),
9263 .rpc_message = &msg,
9264 .callback_ops = &nfs4_layoutcommit_ops,
9265 .callback_data = data,
9267 struct rpc_task *task;
9270 dprintk("NFS: initiating layoutcommit call. sync %d "
9271 "lbw: %llu inode %lu\n", sync,
9272 data->args.lastbytewritten,
9273 data->args.inode->i_ino);
9276 data->inode = nfs_igrab_and_active(data->args.inode);
9277 if (data->inode == NULL) {
9278 nfs4_layoutcommit_release(data);
9281 task_setup_data.flags = RPC_TASK_ASYNC;
9283 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9284 task = rpc_run_task(&task_setup_data);
9286 return PTR_ERR(task);
9288 status = task->tk_status;
9289 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9290 dprintk("%s: status %d\n", __func__, status);
9296 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9297 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9300 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9301 struct nfs_fsinfo *info,
9302 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9304 struct nfs41_secinfo_no_name_args args = {
9305 .style = SECINFO_STYLE_CURRENT_FH,
9307 struct nfs4_secinfo_res res = {
9310 struct rpc_message msg = {
9311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9315 struct rpc_clnt *clnt = server->client;
9316 const struct cred *cred = NULL;
9319 if (use_integrity) {
9320 clnt = server->nfs_client->cl_rpcclient;
9321 cred = nfs4_get_clid_cred(server->nfs_client);
9322 msg.rpc_cred = cred;
9325 dprintk("--> %s\n", __func__);
9326 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9328 dprintk("<-- %s status=%d\n", __func__, status);
9336 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9337 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9339 struct nfs4_exception exception = {
9340 .interruptible = true,
9344 /* first try using integrity protection */
9345 err = -NFS4ERR_WRONGSEC;
9347 /* try to use integrity protection with machine cred */
9348 if (_nfs4_is_integrity_protected(server->nfs_client))
9349 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9353 * if unable to use integrity protection, or SECINFO with
9354 * integrity protection returns NFS4ERR_WRONGSEC (which is
9355 * disallowed by spec, but exists in deployed servers) use
9356 * the current filesystem's rpc_client and the user cred.
9358 if (err == -NFS4ERR_WRONGSEC)
9359 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9364 case -NFS4ERR_WRONGSEC:
9368 err = nfs4_handle_exception(server, err, &exception);
9370 } while (exception.retry);
9376 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9377 struct nfs_fsinfo *info)
9381 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9382 struct nfs4_secinfo_flavors *flavors;
9383 struct nfs4_secinfo4 *secinfo;
9386 page = alloc_page(GFP_KERNEL);
9392 flavors = page_address(page);
9393 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9396 * Fall back on "guess and check" method if
9397 * the server doesn't support SECINFO_NO_NAME
9399 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9400 err = nfs4_find_root_sec(server, fhandle, info);
9406 for (i = 0; i < flavors->num_flavors; i++) {
9407 secinfo = &flavors->flavors[i];
9409 switch (secinfo->flavor) {
9413 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9414 &secinfo->flavor_info);
9417 flavor = RPC_AUTH_MAXFLAVOR;
9421 if (!nfs_auth_info_match(&server->auth_info, flavor))
9422 flavor = RPC_AUTH_MAXFLAVOR;
9424 if (flavor != RPC_AUTH_MAXFLAVOR) {
9425 err = nfs4_lookup_root_sec(server, fhandle,
9432 if (flavor == RPC_AUTH_MAXFLAVOR)
9443 static int _nfs41_test_stateid(struct nfs_server *server,
9444 nfs4_stateid *stateid,
9445 const struct cred *cred)
9448 struct nfs41_test_stateid_args args = {
9451 struct nfs41_test_stateid_res res;
9452 struct rpc_message msg = {
9453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9458 struct rpc_clnt *rpc_client = server->client;
9460 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9463 dprintk("NFS call test_stateid %p\n", stateid);
9464 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9465 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9466 &args.seq_args, &res.seq_res);
9467 if (status != NFS_OK) {
9468 dprintk("NFS reply test_stateid: failed, %d\n", status);
9471 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9475 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9476 int err, struct nfs4_exception *exception)
9478 exception->retry = 0;
9480 case -NFS4ERR_DELAY:
9481 case -NFS4ERR_RETRY_UNCACHED_REP:
9482 nfs4_handle_exception(server, err, exception);
9484 case -NFS4ERR_BADSESSION:
9485 case -NFS4ERR_BADSLOT:
9486 case -NFS4ERR_BAD_HIGH_SLOT:
9487 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9488 case -NFS4ERR_DEADSESSION:
9489 nfs4_do_handle_exception(server, err, exception);
9494 * nfs41_test_stateid - perform a TEST_STATEID operation
9496 * @server: server / transport on which to perform the operation
9497 * @stateid: state ID to test
9500 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9501 * Otherwise a negative NFS4ERR value is returned if the operation
9502 * failed or the state ID is not currently valid.
9504 static int nfs41_test_stateid(struct nfs_server *server,
9505 nfs4_stateid *stateid,
9506 const struct cred *cred)
9508 struct nfs4_exception exception = {
9509 .interruptible = true,
9513 err = _nfs41_test_stateid(server, stateid, cred);
9514 nfs4_handle_delay_or_session_error(server, err, &exception);
9515 } while (exception.retry);
9519 struct nfs_free_stateid_data {
9520 struct nfs_server *server;
9521 struct nfs41_free_stateid_args args;
9522 struct nfs41_free_stateid_res res;
9525 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9527 struct nfs_free_stateid_data *data = calldata;
9528 nfs4_setup_sequence(data->server->nfs_client,
9529 &data->args.seq_args,
9534 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9536 struct nfs_free_stateid_data *data = calldata;
9538 nfs41_sequence_done(task, &data->res.seq_res);
9540 switch (task->tk_status) {
9541 case -NFS4ERR_DELAY:
9542 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9543 rpc_restart_call_prepare(task);
9547 static void nfs41_free_stateid_release(void *calldata)
9552 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9553 .rpc_call_prepare = nfs41_free_stateid_prepare,
9554 .rpc_call_done = nfs41_free_stateid_done,
9555 .rpc_release = nfs41_free_stateid_release,
9559 * nfs41_free_stateid - perform a FREE_STATEID operation
9561 * @server: server / transport on which to perform the operation
9562 * @stateid: state ID to release
9564 * @privileged: set to true if this call needs to be privileged
9566 * Note: this function is always asynchronous.
9568 static int nfs41_free_stateid(struct nfs_server *server,
9569 const nfs4_stateid *stateid,
9570 const struct cred *cred,
9573 struct rpc_message msg = {
9574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9577 struct rpc_task_setup task_setup = {
9578 .rpc_client = server->client,
9579 .rpc_message = &msg,
9580 .callback_ops = &nfs41_free_stateid_ops,
9581 .flags = RPC_TASK_ASYNC,
9583 struct nfs_free_stateid_data *data;
9584 struct rpc_task *task;
9586 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9587 &task_setup.rpc_client, &msg);
9589 dprintk("NFS call free_stateid %p\n", stateid);
9590 data = kmalloc(sizeof(*data), GFP_NOFS);
9593 data->server = server;
9594 nfs4_stateid_copy(&data->args.stateid, stateid);
9596 task_setup.callback_data = data;
9598 msg.rpc_argp = &data->args;
9599 msg.rpc_resp = &data->res;
9600 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
9601 task = rpc_run_task(&task_setup);
9603 return PTR_ERR(task);
9609 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9611 const struct cred *cred = lsp->ls_state->owner->so_cred;
9613 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9614 nfs4_free_lock_state(server, lsp);
9617 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9618 const nfs4_stateid *s2)
9620 if (s1->type != s2->type)
9623 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9626 if (s1->seqid == s2->seqid)
9629 return s1->seqid == 0 || s2->seqid == 0;
9632 #endif /* CONFIG_NFS_V4_1 */
9634 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9635 const nfs4_stateid *s2)
9637 return nfs4_stateid_match(s1, s2);
9641 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9642 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9643 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9644 .recover_open = nfs4_open_reclaim,
9645 .recover_lock = nfs4_lock_reclaim,
9646 .establish_clid = nfs4_init_clientid,
9647 .detect_trunking = nfs40_discover_server_trunking,
9650 #if defined(CONFIG_NFS_V4_1)
9651 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9652 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9653 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9654 .recover_open = nfs4_open_reclaim,
9655 .recover_lock = nfs4_lock_reclaim,
9656 .establish_clid = nfs41_init_clientid,
9657 .reclaim_complete = nfs41_proc_reclaim_complete,
9658 .detect_trunking = nfs41_discover_server_trunking,
9660 #endif /* CONFIG_NFS_V4_1 */
9662 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9663 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9664 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9665 .recover_open = nfs40_open_expired,
9666 .recover_lock = nfs4_lock_expired,
9667 .establish_clid = nfs4_init_clientid,
9670 #if defined(CONFIG_NFS_V4_1)
9671 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9672 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9673 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9674 .recover_open = nfs41_open_expired,
9675 .recover_lock = nfs41_lock_expired,
9676 .establish_clid = nfs41_init_clientid,
9678 #endif /* CONFIG_NFS_V4_1 */
9680 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9681 .sched_state_renewal = nfs4_proc_async_renew,
9682 .get_state_renewal_cred = nfs4_get_renew_cred,
9683 .renew_lease = nfs4_proc_renew,
9686 #if defined(CONFIG_NFS_V4_1)
9687 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9688 .sched_state_renewal = nfs41_proc_async_sequence,
9689 .get_state_renewal_cred = nfs4_get_machine_cred,
9690 .renew_lease = nfs4_proc_sequence,
9694 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9695 .get_locations = _nfs40_proc_get_locations,
9696 .fsid_present = _nfs40_proc_fsid_present,
9699 #if defined(CONFIG_NFS_V4_1)
9700 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9701 .get_locations = _nfs41_proc_get_locations,
9702 .fsid_present = _nfs41_proc_fsid_present,
9704 #endif /* CONFIG_NFS_V4_1 */
9706 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9708 .init_caps = NFS_CAP_READDIRPLUS
9709 | NFS_CAP_ATOMIC_OPEN
9710 | NFS_CAP_POSIX_LOCK,
9711 .init_client = nfs40_init_client,
9712 .shutdown_client = nfs40_shutdown_client,
9713 .match_stateid = nfs4_match_stateid,
9714 .find_root_sec = nfs4_find_root_sec,
9715 .free_lock_state = nfs4_release_lockowner,
9716 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9717 .alloc_seqid = nfs_alloc_seqid,
9718 .call_sync_ops = &nfs40_call_sync_ops,
9719 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9720 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9721 .state_renewal_ops = &nfs40_state_renewal_ops,
9722 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9725 #if defined(CONFIG_NFS_V4_1)
9726 static struct nfs_seqid *
9727 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9732 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9734 .init_caps = NFS_CAP_READDIRPLUS
9735 | NFS_CAP_ATOMIC_OPEN
9736 | NFS_CAP_POSIX_LOCK
9737 | NFS_CAP_STATEID_NFSV41
9738 | NFS_CAP_ATOMIC_OPEN_V1
9740 .init_client = nfs41_init_client,
9741 .shutdown_client = nfs41_shutdown_client,
9742 .match_stateid = nfs41_match_stateid,
9743 .find_root_sec = nfs41_find_root_sec,
9744 .free_lock_state = nfs41_free_lock_state,
9745 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9746 .alloc_seqid = nfs_alloc_no_seqid,
9747 .session_trunk = nfs4_test_session_trunk,
9748 .call_sync_ops = &nfs41_call_sync_ops,
9749 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9750 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9751 .state_renewal_ops = &nfs41_state_renewal_ops,
9752 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9756 #if defined(CONFIG_NFS_V4_2)
9757 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9759 .init_caps = NFS_CAP_READDIRPLUS
9760 | NFS_CAP_ATOMIC_OPEN
9761 | NFS_CAP_POSIX_LOCK
9762 | NFS_CAP_STATEID_NFSV41
9763 | NFS_CAP_ATOMIC_OPEN_V1
9767 | NFS_CAP_OFFLOAD_CANCEL
9768 | NFS_CAP_DEALLOCATE
9770 | NFS_CAP_LAYOUTSTATS
9772 | NFS_CAP_LAYOUTERROR,
9773 .init_client = nfs41_init_client,
9774 .shutdown_client = nfs41_shutdown_client,
9775 .match_stateid = nfs41_match_stateid,
9776 .find_root_sec = nfs41_find_root_sec,
9777 .free_lock_state = nfs41_free_lock_state,
9778 .call_sync_ops = &nfs41_call_sync_ops,
9779 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9780 .alloc_seqid = nfs_alloc_no_seqid,
9781 .session_trunk = nfs4_test_session_trunk,
9782 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9783 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9784 .state_renewal_ops = &nfs41_state_renewal_ops,
9785 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9789 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9790 [0] = &nfs_v4_0_minor_ops,
9791 #if defined(CONFIG_NFS_V4_1)
9792 [1] = &nfs_v4_1_minor_ops,
9794 #if defined(CONFIG_NFS_V4_2)
9795 [2] = &nfs_v4_2_minor_ops,
9799 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9801 ssize_t error, error2;
9803 error = generic_listxattr(dentry, list, size);
9811 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9814 return error + error2;
9817 static const struct inode_operations nfs4_dir_inode_operations = {
9818 .create = nfs_create,
9819 .lookup = nfs_lookup,
9820 .atomic_open = nfs_atomic_open,
9822 .unlink = nfs_unlink,
9823 .symlink = nfs_symlink,
9827 .rename = nfs_rename,
9828 .permission = nfs_permission,
9829 .getattr = nfs_getattr,
9830 .setattr = nfs_setattr,
9831 .listxattr = nfs4_listxattr,
9834 static const struct inode_operations nfs4_file_inode_operations = {
9835 .permission = nfs_permission,
9836 .getattr = nfs_getattr,
9837 .setattr = nfs_setattr,
9838 .listxattr = nfs4_listxattr,
9841 const struct nfs_rpc_ops nfs_v4_clientops = {
9842 .version = 4, /* protocol version */
9843 .dentry_ops = &nfs4_dentry_operations,
9844 .dir_inode_ops = &nfs4_dir_inode_operations,
9845 .file_inode_ops = &nfs4_file_inode_operations,
9846 .file_ops = &nfs4_file_operations,
9847 .getroot = nfs4_proc_get_root,
9848 .submount = nfs4_submount,
9849 .try_mount = nfs4_try_mount,
9850 .getattr = nfs4_proc_getattr,
9851 .setattr = nfs4_proc_setattr,
9852 .lookup = nfs4_proc_lookup,
9853 .lookupp = nfs4_proc_lookupp,
9854 .access = nfs4_proc_access,
9855 .readlink = nfs4_proc_readlink,
9856 .create = nfs4_proc_create,
9857 .remove = nfs4_proc_remove,
9858 .unlink_setup = nfs4_proc_unlink_setup,
9859 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9860 .unlink_done = nfs4_proc_unlink_done,
9861 .rename_setup = nfs4_proc_rename_setup,
9862 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9863 .rename_done = nfs4_proc_rename_done,
9864 .link = nfs4_proc_link,
9865 .symlink = nfs4_proc_symlink,
9866 .mkdir = nfs4_proc_mkdir,
9867 .rmdir = nfs4_proc_rmdir,
9868 .readdir = nfs4_proc_readdir,
9869 .mknod = nfs4_proc_mknod,
9870 .statfs = nfs4_proc_statfs,
9871 .fsinfo = nfs4_proc_fsinfo,
9872 .pathconf = nfs4_proc_pathconf,
9873 .set_capabilities = nfs4_server_capabilities,
9874 .decode_dirent = nfs4_decode_dirent,
9875 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9876 .read_setup = nfs4_proc_read_setup,
9877 .read_done = nfs4_read_done,
9878 .write_setup = nfs4_proc_write_setup,
9879 .write_done = nfs4_write_done,
9880 .commit_setup = nfs4_proc_commit_setup,
9881 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9882 .commit_done = nfs4_commit_done,
9883 .lock = nfs4_proc_lock,
9884 .clear_acl_cache = nfs4_zap_acl_attr,
9885 .close_context = nfs4_close_context,
9886 .open_context = nfs4_atomic_open,
9887 .have_delegation = nfs4_have_delegation,
9888 .alloc_client = nfs4_alloc_client,
9889 .init_client = nfs4_init_client,
9890 .free_client = nfs4_free_client,
9891 .create_server = nfs4_create_server,
9892 .clone_server = nfs_clone_server,
9895 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9896 .name = XATTR_NAME_NFSV4_ACL,
9897 .list = nfs4_xattr_list_nfs4_acl,
9898 .get = nfs4_xattr_get_nfs4_acl,
9899 .set = nfs4_xattr_set_nfs4_acl,
9902 const struct xattr_handler *nfs4_xattr_handlers[] = {
9903 &nfs4_xattr_nfs4_acl_handler,
9904 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9905 &nfs4_xattr_nfs4_label_handler,