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 *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
95 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
97 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
98 struct nfs4_label *olabel);
99 #ifdef CONFIG_NFS_V4_1
100 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
101 const struct cred *cred,
102 struct nfs4_slot *slot,
104 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
105 const struct cred *);
106 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
107 const struct cred *, bool);
110 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
111 static inline struct nfs4_label *
112 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
113 struct iattr *sattr, struct nfs4_label *label)
120 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
123 err = security_dentry_init_security(dentry, sattr->ia_mode,
124 &dentry->d_name, (void **)&label->label, &label->len);
131 nfs4_label_release_security(struct nfs4_label *label)
134 security_release_secctx(label->label, label->len);
136 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
139 return server->attr_bitmask;
141 return server->attr_bitmask_nl;
144 static inline struct nfs4_label *
145 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
146 struct iattr *sattr, struct nfs4_label *l)
149 nfs4_label_release_security(struct nfs4_label *label)
152 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
153 { return server->attr_bitmask; }
156 /* Prevent leaks of NFSv4 errors into userland */
157 static int nfs4_map_errors(int err)
162 case -NFS4ERR_RESOURCE:
163 case -NFS4ERR_LAYOUTTRYLATER:
164 case -NFS4ERR_RECALLCONFLICT:
166 case -NFS4ERR_WRONGSEC:
167 case -NFS4ERR_WRONG_CRED:
169 case -NFS4ERR_BADOWNER:
170 case -NFS4ERR_BADNAME:
172 case -NFS4ERR_SHARE_DENIED:
174 case -NFS4ERR_MINOR_VERS_MISMATCH:
175 return -EPROTONOSUPPORT;
176 case -NFS4ERR_FILE_OPEN:
179 dprintk("%s could not handle NFSv4 error %d\n",
187 * This is our standard bitmap for GETATTR requests.
189 const u32 nfs4_fattr_bitmap[3] = {
191 | FATTR4_WORD0_CHANGE
194 | FATTR4_WORD0_FILEID,
196 | FATTR4_WORD1_NUMLINKS
198 | FATTR4_WORD1_OWNER_GROUP
199 | FATTR4_WORD1_RAWDEV
200 | FATTR4_WORD1_SPACE_USED
201 | FATTR4_WORD1_TIME_ACCESS
202 | FATTR4_WORD1_TIME_METADATA
203 | FATTR4_WORD1_TIME_MODIFY
204 | FATTR4_WORD1_MOUNTED_ON_FILEID,
205 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
206 FATTR4_WORD2_SECURITY_LABEL
210 static const u32 nfs4_pnfs_open_bitmap[3] = {
212 | FATTR4_WORD0_CHANGE
215 | FATTR4_WORD0_FILEID,
217 | FATTR4_WORD1_NUMLINKS
219 | FATTR4_WORD1_OWNER_GROUP
220 | FATTR4_WORD1_RAWDEV
221 | FATTR4_WORD1_SPACE_USED
222 | FATTR4_WORD1_TIME_ACCESS
223 | FATTR4_WORD1_TIME_METADATA
224 | FATTR4_WORD1_TIME_MODIFY,
225 FATTR4_WORD2_MDSTHRESHOLD
226 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
227 | FATTR4_WORD2_SECURITY_LABEL
231 static const u32 nfs4_open_noattr_bitmap[3] = {
233 | FATTR4_WORD0_FILEID,
236 const u32 nfs4_statfs_bitmap[3] = {
237 FATTR4_WORD0_FILES_AVAIL
238 | FATTR4_WORD0_FILES_FREE
239 | FATTR4_WORD0_FILES_TOTAL,
240 FATTR4_WORD1_SPACE_AVAIL
241 | FATTR4_WORD1_SPACE_FREE
242 | FATTR4_WORD1_SPACE_TOTAL
245 const u32 nfs4_pathconf_bitmap[3] = {
247 | FATTR4_WORD0_MAXNAME,
251 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
252 | FATTR4_WORD0_MAXREAD
253 | FATTR4_WORD0_MAXWRITE
254 | FATTR4_WORD0_LEASE_TIME,
255 FATTR4_WORD1_TIME_DELTA
256 | FATTR4_WORD1_FS_LAYOUT_TYPES,
257 FATTR4_WORD2_LAYOUT_BLKSIZE
258 | FATTR4_WORD2_CLONE_BLKSIZE
261 const u32 nfs4_fs_locations_bitmap[3] = {
265 | FATTR4_WORD0_FILEID
266 | FATTR4_WORD0_FS_LOCATIONS,
268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
277 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
280 unsigned long cache_validity;
282 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
283 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
286 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
287 if (!(cache_validity & NFS_INO_REVAL_FORCED))
288 cache_validity &= ~(NFS_INO_INVALID_CHANGE
289 | NFS_INO_INVALID_SIZE);
291 if (!(cache_validity & NFS_INO_INVALID_SIZE))
292 dst[0] &= ~FATTR4_WORD0_SIZE;
294 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
295 dst[0] &= ~FATTR4_WORD0_CHANGE;
298 static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
299 const __u32 *src, struct inode *inode)
301 nfs4_bitmap_copy_adjust(dst, src, inode);
304 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
305 struct nfs4_readdir_arg *readdir)
307 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
311 readdir->cookie = cookie;
312 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
317 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
322 * NFSv4 servers do not return entries for '.' and '..'
323 * Therefore, we fake these entries here. We let '.'
324 * have cookie 0 and '..' have cookie 1. Note that
325 * when talking to the server, we always send cookie 0
328 start = p = kmap_atomic(*readdir->pages);
331 *p++ = xdr_one; /* next */
332 *p++ = xdr_zero; /* cookie, first word */
333 *p++ = xdr_one; /* cookie, second word */
334 *p++ = xdr_one; /* entry len */
335 memcpy(p, ".\0\0\0", 4); /* entry */
337 *p++ = xdr_one; /* bitmap length */
338 *p++ = htonl(attrs); /* bitmap */
339 *p++ = htonl(12); /* attribute buffer length */
340 *p++ = htonl(NF4DIR);
341 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
344 *p++ = xdr_one; /* next */
345 *p++ = xdr_zero; /* cookie, first word */
346 *p++ = xdr_two; /* cookie, second word */
347 *p++ = xdr_two; /* entry len */
348 memcpy(p, "..\0\0", 4); /* entry */
350 *p++ = xdr_one; /* bitmap length */
351 *p++ = htonl(attrs); /* bitmap */
352 *p++ = htonl(12); /* attribute buffer length */
353 *p++ = htonl(NF4DIR);
354 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
356 readdir->pgbase = (char *)p - (char *)start;
357 readdir->count -= readdir->pgbase;
358 kunmap_atomic(start);
361 static void nfs4_test_and_free_stateid(struct nfs_server *server,
362 nfs4_stateid *stateid,
363 const struct cred *cred)
365 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
367 ops->test_and_free_expired(server, stateid, cred);
370 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
371 nfs4_stateid *stateid,
372 const struct cred *cred)
374 stateid->type = NFS4_REVOKED_STATEID_TYPE;
375 nfs4_test_and_free_stateid(server, stateid, cred);
378 static void nfs4_free_revoked_stateid(struct nfs_server *server,
379 const nfs4_stateid *stateid,
380 const struct cred *cred)
384 nfs4_stateid_copy(&tmp, stateid);
385 __nfs4_free_revoked_stateid(server, &tmp, cred);
388 static long nfs4_update_delay(long *timeout)
392 return NFS4_POLL_RETRY_MAX;
394 *timeout = NFS4_POLL_RETRY_MIN;
395 if (*timeout > NFS4_POLL_RETRY_MAX)
396 *timeout = NFS4_POLL_RETRY_MAX;
402 static int nfs4_delay_killable(long *timeout)
406 freezable_schedule_timeout_killable_unsafe(
407 nfs4_update_delay(timeout));
408 if (!__fatal_signal_pending(current))
413 static int nfs4_delay_interruptible(long *timeout)
417 freezable_schedule_timeout_interruptible(nfs4_update_delay(timeout));
418 if (!signal_pending(current))
420 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
423 static int nfs4_delay(long *timeout, bool interruptible)
426 return nfs4_delay_interruptible(timeout);
427 return nfs4_delay_killable(timeout);
430 static const nfs4_stateid *
431 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
435 switch (stateid->type) {
436 case NFS4_OPEN_STATEID_TYPE:
437 case NFS4_LOCK_STATEID_TYPE:
438 case NFS4_DELEGATION_STATEID_TYPE:
446 /* This is the error handling routine for processes that are allowed
449 static int nfs4_do_handle_exception(struct nfs_server *server,
450 int errorcode, struct nfs4_exception *exception)
452 struct nfs_client *clp = server->nfs_client;
453 struct nfs4_state *state = exception->state;
454 const nfs4_stateid *stateid;
455 struct inode *inode = exception->inode;
458 exception->delay = 0;
459 exception->recovering = 0;
460 exception->retry = 0;
462 stateid = nfs4_recoverable_stateid(exception->stateid);
463 if (stateid == NULL && state != NULL)
464 stateid = nfs4_recoverable_stateid(&state->stateid);
469 case -NFS4ERR_BADHANDLE:
471 if (inode != NULL && S_ISREG(inode->i_mode))
472 pnfs_destroy_layout(NFS_I(inode));
474 case -NFS4ERR_DELEG_REVOKED:
475 case -NFS4ERR_ADMIN_REVOKED:
476 case -NFS4ERR_EXPIRED:
477 case -NFS4ERR_BAD_STATEID:
478 case -NFS4ERR_PARTNER_NO_AUTH:
479 if (inode != NULL && stateid != NULL) {
480 nfs_inode_find_state_and_recover(inode,
482 goto wait_on_recovery;
485 case -NFS4ERR_OPENMODE:
489 err = nfs_async_inode_return_delegation(inode,
492 goto wait_on_recovery;
493 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
494 exception->retry = 1;
500 ret = nfs4_schedule_stateid_recovery(server, state);
503 goto wait_on_recovery;
504 case -NFS4ERR_STALE_STATEID:
505 case -NFS4ERR_STALE_CLIENTID:
506 nfs4_schedule_lease_recovery(clp);
507 goto wait_on_recovery;
509 ret = nfs4_schedule_migration_recovery(server);
512 goto wait_on_recovery;
513 case -NFS4ERR_LEASE_MOVED:
514 nfs4_schedule_lease_moved_recovery(clp);
515 goto wait_on_recovery;
516 #if defined(CONFIG_NFS_V4_1)
517 case -NFS4ERR_BADSESSION:
518 case -NFS4ERR_BADSLOT:
519 case -NFS4ERR_BAD_HIGH_SLOT:
520 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
521 case -NFS4ERR_DEADSESSION:
522 case -NFS4ERR_SEQ_FALSE_RETRY:
523 case -NFS4ERR_SEQ_MISORDERED:
524 /* Handled in nfs41_sequence_process() */
525 goto wait_on_recovery;
526 #endif /* defined(CONFIG_NFS_V4_1) */
527 case -NFS4ERR_FILE_OPEN:
528 if (exception->timeout > HZ) {
529 /* We have retried a decent amount, time to
537 nfs_inc_server_stats(server, NFSIOS_DELAY);
540 case -NFS4ERR_LAYOUTTRYLATER:
541 case -NFS4ERR_RECALLCONFLICT:
542 exception->delay = 1;
545 case -NFS4ERR_RETRY_UNCACHED_REP:
546 case -NFS4ERR_OLD_STATEID:
547 exception->retry = 1;
549 case -NFS4ERR_BADOWNER:
550 /* The following works around a Linux server bug! */
551 case -NFS4ERR_BADNAME:
552 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
553 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
554 exception->retry = 1;
555 printk(KERN_WARNING "NFS: v4 server %s "
556 "does not accept raw "
558 "Reenabling the idmapper.\n",
559 server->nfs_client->cl_hostname);
562 /* We failed to handle the error */
563 return nfs4_map_errors(ret);
565 exception->recovering = 1;
569 /* This is the error handling routine for processes that are allowed
572 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
574 struct nfs_client *clp = server->nfs_client;
577 ret = nfs4_do_handle_exception(server, errorcode, exception);
578 if (exception->delay) {
579 ret = nfs4_delay(&exception->timeout,
580 exception->interruptible);
583 if (exception->recovering) {
584 ret = nfs4_wait_clnt_recover(clp);
585 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
592 exception->retry = 1;
597 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
598 int errorcode, struct nfs4_exception *exception)
600 struct nfs_client *clp = server->nfs_client;
603 ret = nfs4_do_handle_exception(server, errorcode, exception);
604 if (exception->delay) {
605 rpc_delay(task, nfs4_update_delay(&exception->timeout));
608 if (exception->recovering) {
609 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
610 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
611 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
614 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
619 exception->retry = 1;
621 * For NFS4ERR_MOVED, the client transport will need to
622 * be recomputed after migration recovery has completed.
624 if (errorcode == -NFS4ERR_MOVED)
625 rpc_task_release_transport(task);
631 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
632 struct nfs4_state *state, long *timeout)
634 struct nfs4_exception exception = {
638 if (task->tk_status >= 0)
641 exception.timeout = *timeout;
642 task->tk_status = nfs4_async_handle_exception(task, server,
645 if (exception.delay && timeout)
646 *timeout = exception.timeout;
653 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
654 * or 'false' otherwise.
656 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
658 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
659 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
662 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
664 spin_lock(&clp->cl_lock);
665 if (time_before(clp->cl_last_renewal,timestamp))
666 clp->cl_last_renewal = timestamp;
667 spin_unlock(&clp->cl_lock);
670 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
672 struct nfs_client *clp = server->nfs_client;
674 if (!nfs4_has_session(clp))
675 do_renew_lease(clp, timestamp);
678 struct nfs4_call_sync_data {
679 const struct nfs_server *seq_server;
680 struct nfs4_sequence_args *seq_args;
681 struct nfs4_sequence_res *seq_res;
684 void nfs4_init_sequence(struct nfs4_sequence_args *args,
685 struct nfs4_sequence_res *res, int cache_reply,
688 args->sa_slot = NULL;
689 args->sa_cache_this = cache_reply;
690 args->sa_privileged = privileged;
695 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
697 struct nfs4_slot *slot = res->sr_slot;
698 struct nfs4_slot_table *tbl;
701 spin_lock(&tbl->slot_tbl_lock);
702 if (!nfs41_wake_and_assign_slot(tbl, slot))
703 nfs4_free_slot(tbl, slot);
704 spin_unlock(&tbl->slot_tbl_lock);
709 static int nfs40_sequence_done(struct rpc_task *task,
710 struct nfs4_sequence_res *res)
712 if (res->sr_slot != NULL)
713 nfs40_sequence_free_slot(res);
717 #if defined(CONFIG_NFS_V4_1)
719 static void nfs41_release_slot(struct nfs4_slot *slot)
721 struct nfs4_session *session;
722 struct nfs4_slot_table *tbl;
723 bool send_new_highest_used_slotid = false;
728 session = tbl->session;
730 /* Bump the slot sequence number */
735 spin_lock(&tbl->slot_tbl_lock);
736 /* Be nice to the server: try to ensure that the last transmitted
737 * value for highest_user_slotid <= target_highest_slotid
739 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
740 send_new_highest_used_slotid = true;
742 if (nfs41_wake_and_assign_slot(tbl, slot)) {
743 send_new_highest_used_slotid = false;
746 nfs4_free_slot(tbl, slot);
748 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
749 send_new_highest_used_slotid = false;
751 spin_unlock(&tbl->slot_tbl_lock);
752 if (send_new_highest_used_slotid)
753 nfs41_notify_server(session->clp);
754 if (waitqueue_active(&tbl->slot_waitq))
755 wake_up_all(&tbl->slot_waitq);
758 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
760 nfs41_release_slot(res->sr_slot);
764 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
767 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
768 slot->seq_nr_highest_sent = seqnr;
770 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
773 slot->seq_nr_highest_sent = seqnr;
774 slot->seq_nr_last_acked = seqnr;
777 static int nfs41_sequence_process(struct rpc_task *task,
778 struct nfs4_sequence_res *res)
780 struct nfs4_session *session;
781 struct nfs4_slot *slot = res->sr_slot;
782 struct nfs_client *clp;
788 /* don't increment the sequence number if the task wasn't sent */
789 if (!RPC_WAS_SENT(task) || slot->seq_done)
792 session = slot->table->session;
794 trace_nfs4_sequence_done(session, res);
796 status = res->sr_status;
797 if (task->tk_status == -NFS4ERR_DEADSESSION)
798 status = -NFS4ERR_DEADSESSION;
800 /* Check the SEQUENCE operation status */
803 /* Mark this sequence number as having been acked */
804 nfs4_slot_sequence_acked(slot, slot->seq_nr);
805 /* Update the slot's sequence and clientid lease timer */
808 do_renew_lease(clp, res->sr_timestamp);
809 /* Check sequence flags */
810 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
812 nfs41_update_target_slotid(slot->table, slot, res);
816 * sr_status remains 1 if an RPC level error occurred.
817 * The server may or may not have processed the sequence
820 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
824 /* The server detected a resend of the RPC call and
825 * returned NFS4ERR_DELAY as per Section 2.10.6.2
828 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
832 nfs4_slot_sequence_acked(slot, slot->seq_nr);
834 case -NFS4ERR_RETRY_UNCACHED_REP:
835 case -NFS4ERR_SEQ_FALSE_RETRY:
837 * The server thinks we tried to replay a request.
838 * Retry the call after bumping the sequence ID.
840 nfs4_slot_sequence_acked(slot, slot->seq_nr);
842 case -NFS4ERR_BADSLOT:
844 * The slot id we used was probably retired. Try again
845 * using a different slot id.
847 if (slot->slot_nr < slot->table->target_highest_slotid)
848 goto session_recover;
850 case -NFS4ERR_SEQ_MISORDERED:
851 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
853 * Were one or more calls using this slot interrupted?
854 * If the server never received the request, then our
855 * transmitted slot sequence number may be too high.
857 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
863 * A retry might be sent while the original request is
864 * still in progress on the replier. The replier SHOULD
865 * deal with the issue by returning NFS4ERR_DELAY as the
866 * reply to SEQUENCE or CB_SEQUENCE operation, but
867 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
869 * Restart the search after a delay.
871 slot->seq_nr = slot->seq_nr_highest_sent;
873 case -NFS4ERR_BADSESSION:
874 case -NFS4ERR_DEADSESSION:
875 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
876 goto session_recover;
878 /* Just update the slot sequence no. */
882 /* The session may be reset by one of the error handlers. */
883 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
887 nfs4_schedule_session_recovery(session, status);
888 dprintk("%s ERROR: %d Reset session\n", __func__, status);
889 nfs41_sequence_free_slot(res);
894 if (rpc_restart_call_prepare(task)) {
895 nfs41_sequence_free_slot(res);
901 if (!rpc_restart_call(task))
903 rpc_delay(task, NFS4_POLL_RETRY_MAX);
907 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
909 if (!nfs41_sequence_process(task, res))
911 if (res->sr_slot != NULL)
912 nfs41_sequence_free_slot(res);
916 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
918 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
920 if (res->sr_slot == NULL)
922 if (res->sr_slot->table->session != NULL)
923 return nfs41_sequence_process(task, res);
924 return nfs40_sequence_done(task, res);
927 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
929 if (res->sr_slot != NULL) {
930 if (res->sr_slot->table->session != NULL)
931 nfs41_sequence_free_slot(res);
933 nfs40_sequence_free_slot(res);
937 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
939 if (res->sr_slot == NULL)
941 if (!res->sr_slot->table->session)
942 return nfs40_sequence_done(task, res);
943 return nfs41_sequence_done(task, res);
945 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
947 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
949 struct nfs4_call_sync_data *data = calldata;
951 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
953 nfs4_setup_sequence(data->seq_server->nfs_client,
954 data->seq_args, data->seq_res, task);
957 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
959 struct nfs4_call_sync_data *data = calldata;
961 nfs41_sequence_done(task, data->seq_res);
964 static const struct rpc_call_ops nfs41_call_sync_ops = {
965 .rpc_call_prepare = nfs41_call_sync_prepare,
966 .rpc_call_done = nfs41_call_sync_done,
969 #else /* !CONFIG_NFS_V4_1 */
971 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
973 return nfs40_sequence_done(task, res);
976 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
978 if (res->sr_slot != NULL)
979 nfs40_sequence_free_slot(res);
982 int nfs4_sequence_done(struct rpc_task *task,
983 struct nfs4_sequence_res *res)
985 return nfs40_sequence_done(task, res);
987 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
989 #endif /* !CONFIG_NFS_V4_1 */
991 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
993 res->sr_timestamp = jiffies;
994 res->sr_status_flags = 0;
999 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1000 struct nfs4_sequence_res *res,
1001 struct nfs4_slot *slot)
1005 slot->privileged = args->sa_privileged ? 1 : 0;
1006 args->sa_slot = slot;
1008 res->sr_slot = slot;
1011 int nfs4_setup_sequence(struct nfs_client *client,
1012 struct nfs4_sequence_args *args,
1013 struct nfs4_sequence_res *res,
1014 struct rpc_task *task)
1016 struct nfs4_session *session = nfs4_get_session(client);
1017 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1018 struct nfs4_slot *slot;
1020 /* slot already allocated? */
1021 if (res->sr_slot != NULL)
1025 tbl = &session->fc_slot_table;
1027 spin_lock(&tbl->slot_tbl_lock);
1028 /* The state manager will wait until the slot table is empty */
1029 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1032 slot = nfs4_alloc_slot(tbl);
1034 if (slot == ERR_PTR(-ENOMEM))
1035 goto out_sleep_timeout;
1038 spin_unlock(&tbl->slot_tbl_lock);
1040 nfs4_sequence_attach_slot(args, res, slot);
1042 trace_nfs4_setup_sequence(session, args);
1044 nfs41_sequence_res_init(res);
1045 rpc_call_start(task);
1048 /* Try again in 1/4 second */
1049 if (args->sa_privileged)
1050 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1051 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1053 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1054 NULL, jiffies + (HZ >> 2));
1055 spin_unlock(&tbl->slot_tbl_lock);
1058 if (args->sa_privileged)
1059 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1060 RPC_PRIORITY_PRIVILEGED);
1062 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1063 spin_unlock(&tbl->slot_tbl_lock);
1066 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1068 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1070 struct nfs4_call_sync_data *data = calldata;
1071 nfs4_setup_sequence(data->seq_server->nfs_client,
1072 data->seq_args, data->seq_res, task);
1075 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1077 struct nfs4_call_sync_data *data = calldata;
1078 nfs4_sequence_done(task, data->seq_res);
1081 static const struct rpc_call_ops nfs40_call_sync_ops = {
1082 .rpc_call_prepare = nfs40_call_sync_prepare,
1083 .rpc_call_done = nfs40_call_sync_done,
1086 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1089 struct rpc_task *task;
1091 task = rpc_run_task(task_setup);
1093 return PTR_ERR(task);
1095 ret = task->tk_status;
1100 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1101 struct nfs_server *server,
1102 struct rpc_message *msg,
1103 struct nfs4_sequence_args *args,
1104 struct nfs4_sequence_res *res)
1106 struct nfs_client *clp = server->nfs_client;
1107 struct nfs4_call_sync_data data = {
1108 .seq_server = server,
1112 struct rpc_task_setup task_setup = {
1115 .callback_ops = clp->cl_mvops->call_sync_ops,
1116 .callback_data = &data
1119 return nfs4_call_sync_custom(&task_setup);
1122 int nfs4_call_sync(struct rpc_clnt *clnt,
1123 struct nfs_server *server,
1124 struct rpc_message *msg,
1125 struct nfs4_sequence_args *args,
1126 struct nfs4_sequence_res *res,
1129 nfs4_init_sequence(args, res, cache_reply, 0);
1130 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1134 nfs4_inc_nlink_locked(struct inode *inode)
1136 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1141 nfs4_dec_nlink_locked(struct inode *inode)
1143 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1148 update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
1149 unsigned long timestamp, unsigned long cache_validity)
1151 struct nfs_inode *nfsi = NFS_I(dir);
1153 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1154 | NFS_INO_INVALID_MTIME
1155 | NFS_INO_INVALID_DATA
1157 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
1158 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1159 nfsi->attrtimeo_timestamp = jiffies;
1161 nfs_force_lookup_revalidate(dir);
1162 if (cinfo->before != inode_peek_iversion_raw(dir))
1163 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1164 NFS_INO_INVALID_ACL;
1166 inode_set_iversion_raw(dir, cinfo->after);
1167 nfsi->read_cache_jiffies = timestamp;
1168 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1169 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1170 nfs_fscache_invalidate(dir);
1174 update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1175 unsigned long timestamp, unsigned long cache_validity)
1177 spin_lock(&dir->i_lock);
1178 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1179 spin_unlock(&dir->i_lock);
1182 struct nfs4_open_createattrs {
1183 struct nfs4_label *label;
1184 struct iattr *sattr;
1185 const __u32 verf[2];
1188 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1189 int err, struct nfs4_exception *exception)
1193 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1195 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1196 exception->retry = 1;
1200 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1202 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1205 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1207 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1209 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1213 nfs4_map_atomic_open_share(struct nfs_server *server,
1214 fmode_t fmode, int openflags)
1218 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1220 res = NFS4_SHARE_ACCESS_READ;
1223 res = NFS4_SHARE_ACCESS_WRITE;
1225 case FMODE_READ|FMODE_WRITE:
1226 res = NFS4_SHARE_ACCESS_BOTH;
1228 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1230 /* Want no delegation if we're using O_DIRECT */
1231 if (openflags & O_DIRECT)
1232 res |= NFS4_SHARE_WANT_NO_DELEG;
1237 static enum open_claim_type4
1238 nfs4_map_atomic_open_claim(struct nfs_server *server,
1239 enum open_claim_type4 claim)
1241 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1246 case NFS4_OPEN_CLAIM_FH:
1247 return NFS4_OPEN_CLAIM_NULL;
1248 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1249 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1250 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1251 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1255 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1257 p->o_res.f_attr = &p->f_attr;
1258 p->o_res.f_label = p->f_label;
1259 p->o_res.seqid = p->o_arg.seqid;
1260 p->c_res.seqid = p->c_arg.seqid;
1261 p->o_res.server = p->o_arg.server;
1262 p->o_res.access_request = p->o_arg.access;
1263 nfs_fattr_init(&p->f_attr);
1264 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1267 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1268 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1269 const struct nfs4_open_createattrs *c,
1270 enum open_claim_type4 claim,
1273 struct dentry *parent = dget_parent(dentry);
1274 struct inode *dir = d_inode(parent);
1275 struct nfs_server *server = NFS_SERVER(dir);
1276 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1277 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1278 struct nfs4_opendata *p;
1280 p = kzalloc(sizeof(*p), gfp_mask);
1284 p->f_label = nfs4_label_alloc(server, gfp_mask);
1285 if (IS_ERR(p->f_label))
1288 p->a_label = nfs4_label_alloc(server, gfp_mask);
1289 if (IS_ERR(p->a_label))
1292 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1293 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1294 if (IS_ERR(p->o_arg.seqid))
1295 goto err_free_label;
1296 nfs_sb_active(dentry->d_sb);
1297 p->dentry = dget(dentry);
1300 atomic_inc(&sp->so_count);
1301 p->o_arg.open_flags = flags;
1302 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1303 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1304 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1306 if (flags & O_CREAT) {
1307 p->o_arg.umask = current_umask();
1308 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1309 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1310 p->o_arg.u.attrs = &p->attrs;
1311 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1313 memcpy(p->o_arg.u.verifier.data, c->verf,
1314 sizeof(p->o_arg.u.verifier.data));
1317 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1318 * will return permission denied for all bits until close */
1319 if (!(flags & O_EXCL)) {
1320 /* ask server to check for all possible rights as results
1322 switch (p->o_arg.claim) {
1325 case NFS4_OPEN_CLAIM_NULL:
1326 case NFS4_OPEN_CLAIM_FH:
1327 p->o_arg.access = NFS4_ACCESS_READ |
1328 NFS4_ACCESS_MODIFY |
1329 NFS4_ACCESS_EXTEND |
1330 NFS4_ACCESS_EXECUTE;
1333 p->o_arg.clientid = server->nfs_client->cl_clientid;
1334 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1335 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1336 p->o_arg.name = &dentry->d_name;
1337 p->o_arg.server = server;
1338 p->o_arg.bitmask = nfs4_bitmask(server, label);
1339 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1340 switch (p->o_arg.claim) {
1341 case NFS4_OPEN_CLAIM_NULL:
1342 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1343 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1344 p->o_arg.fh = NFS_FH(dir);
1346 case NFS4_OPEN_CLAIM_PREVIOUS:
1347 case NFS4_OPEN_CLAIM_FH:
1348 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1349 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1350 p->o_arg.fh = NFS_FH(d_inode(dentry));
1352 p->c_arg.fh = &p->o_res.fh;
1353 p->c_arg.stateid = &p->o_res.stateid;
1354 p->c_arg.seqid = p->o_arg.seqid;
1355 nfs4_init_opendata_res(p);
1356 kref_init(&p->kref);
1360 nfs4_label_free(p->a_label);
1362 nfs4_label_free(p->f_label);
1370 static void nfs4_opendata_free(struct kref *kref)
1372 struct nfs4_opendata *p = container_of(kref,
1373 struct nfs4_opendata, kref);
1374 struct super_block *sb = p->dentry->d_sb;
1376 nfs4_lgopen_release(p->lgp);
1377 nfs_free_seqid(p->o_arg.seqid);
1378 nfs4_sequence_free_slot(&p->o_res.seq_res);
1379 if (p->state != NULL)
1380 nfs4_put_open_state(p->state);
1381 nfs4_put_state_owner(p->owner);
1383 nfs4_label_free(p->a_label);
1384 nfs4_label_free(p->f_label);
1388 nfs_sb_deactive(sb);
1389 nfs_fattr_free_names(&p->f_attr);
1390 kfree(p->f_attr.mdsthreshold);
1394 static void nfs4_opendata_put(struct nfs4_opendata *p)
1397 kref_put(&p->kref, nfs4_opendata_free);
1400 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1403 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1404 case FMODE_READ|FMODE_WRITE:
1405 return state->n_rdwr != 0;
1407 return state->n_wronly != 0;
1409 return state->n_rdonly != 0;
1415 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1416 int open_mode, enum open_claim_type4 claim)
1420 if (open_mode & (O_EXCL|O_TRUNC))
1423 case NFS4_OPEN_CLAIM_NULL:
1424 case NFS4_OPEN_CLAIM_FH:
1429 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1431 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1432 && state->n_rdonly != 0;
1435 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1436 && state->n_wronly != 0;
1438 case FMODE_READ|FMODE_WRITE:
1439 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1440 && state->n_rdwr != 0;
1446 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1447 enum open_claim_type4 claim)
1449 if (delegation == NULL)
1451 if ((delegation->type & fmode) != fmode)
1454 case NFS4_OPEN_CLAIM_NULL:
1455 case NFS4_OPEN_CLAIM_FH:
1457 case NFS4_OPEN_CLAIM_PREVIOUS:
1458 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1464 nfs_mark_delegation_referenced(delegation);
1468 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1477 case FMODE_READ|FMODE_WRITE:
1480 nfs4_state_set_mode_locked(state, state->state | fmode);
1483 #ifdef CONFIG_NFS_V4_1
1484 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1486 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1488 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1490 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1494 #endif /* CONFIG_NFS_V4_1 */
1496 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1498 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1499 wake_up_all(&state->waitq);
1502 static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1503 const nfs4_stateid *stateid)
1505 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1506 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1508 if (stateid_seqid == state_seqid + 1U ||
1509 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1510 nfs_state_log_update_open_stateid(state);
1512 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1515 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1517 struct nfs_client *clp = state->owner->so_server->nfs_client;
1518 bool need_recover = false;
1520 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1521 need_recover = true;
1522 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1523 need_recover = true;
1524 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1525 need_recover = true;
1527 nfs4_state_mark_reclaim_nograce(clp, state);
1531 * Check for whether or not the caller may update the open stateid
1532 * to the value passed in by stateid.
1534 * Note: This function relies heavily on the server implementing
1535 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1537 * i.e. The stateid seqids have to be initialised to 1, and
1538 * are then incremented on every state transition.
1540 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1541 const nfs4_stateid *stateid)
1543 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1544 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1545 if (stateid->seqid == cpu_to_be32(1))
1546 nfs_state_log_update_open_stateid(state);
1548 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1552 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1553 nfs_state_log_out_of_order_open_stateid(state, stateid);
1559 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1561 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1563 if (state->n_wronly)
1564 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1565 if (state->n_rdonly)
1566 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1568 set_bit(NFS_O_RDWR_STATE, &state->flags);
1569 set_bit(NFS_OPEN_STATE, &state->flags);
1572 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1573 nfs4_stateid *stateid, fmode_t fmode)
1575 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1576 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1578 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1581 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1584 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1585 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1586 clear_bit(NFS_OPEN_STATE, &state->flags);
1588 if (stateid == NULL)
1590 /* Handle OPEN+OPEN_DOWNGRADE races */
1591 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1592 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1593 nfs_resync_open_stateid_locked(state);
1596 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1597 nfs4_stateid_copy(&state->stateid, stateid);
1598 nfs4_stateid_copy(&state->open_stateid, stateid);
1599 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1601 nfs_state_log_update_open_stateid(state);
1604 static void nfs_clear_open_stateid(struct nfs4_state *state,
1605 nfs4_stateid *arg_stateid,
1606 nfs4_stateid *stateid, fmode_t fmode)
1608 write_seqlock(&state->seqlock);
1609 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1610 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1611 nfs_clear_open_stateid_locked(state, stateid, fmode);
1612 write_sequnlock(&state->seqlock);
1613 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1614 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1617 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1618 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1619 __must_hold(&state->owner->so_lock)
1620 __must_hold(&state->seqlock)
1628 if (!nfs_need_update_open_stateid(state, stateid))
1630 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1634 /* Rely on seqids for serialisation with NFSv4.0 */
1635 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1638 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1640 * Ensure we process the state changes in the same order
1641 * in which the server processed them by delaying the
1642 * update of the stateid until we are in sequence.
1644 write_sequnlock(&state->seqlock);
1645 spin_unlock(&state->owner->so_lock);
1647 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1648 if (!signal_pending(current)) {
1649 if (schedule_timeout(5*HZ) == 0)
1655 finish_wait(&state->waitq, &wait);
1657 spin_lock(&state->owner->so_lock);
1658 write_seqlock(&state->seqlock);
1661 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1662 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1663 nfs4_stateid_copy(freeme, &state->open_stateid);
1664 nfs_test_and_clear_all_open_stateid(state);
1667 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1668 nfs4_stateid_copy(&state->stateid, stateid);
1669 nfs4_stateid_copy(&state->open_stateid, stateid);
1670 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1671 nfs_state_log_update_open_stateid(state);
1674 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1675 const nfs4_stateid *open_stateid,
1677 nfs4_stateid *freeme)
1680 * Protect the call to nfs4_state_set_mode_locked and
1681 * serialise the stateid update
1683 write_seqlock(&state->seqlock);
1684 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1687 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1690 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1692 case FMODE_READ|FMODE_WRITE:
1693 set_bit(NFS_O_RDWR_STATE, &state->flags);
1695 set_bit(NFS_OPEN_STATE, &state->flags);
1696 write_sequnlock(&state->seqlock);
1699 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1701 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1702 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1703 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1704 clear_bit(NFS_OPEN_STATE, &state->flags);
1707 static void nfs_state_set_delegation(struct nfs4_state *state,
1708 const nfs4_stateid *deleg_stateid,
1712 * Protect the call to nfs4_state_set_mode_locked and
1713 * serialise the stateid update
1715 write_seqlock(&state->seqlock);
1716 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1717 set_bit(NFS_DELEGATED_STATE, &state->flags);
1718 write_sequnlock(&state->seqlock);
1721 static void nfs_state_clear_delegation(struct nfs4_state *state)
1723 write_seqlock(&state->seqlock);
1724 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1725 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1726 write_sequnlock(&state->seqlock);
1729 int update_open_stateid(struct nfs4_state *state,
1730 const nfs4_stateid *open_stateid,
1731 const nfs4_stateid *delegation,
1734 struct nfs_server *server = NFS_SERVER(state->inode);
1735 struct nfs_client *clp = server->nfs_client;
1736 struct nfs_inode *nfsi = NFS_I(state->inode);
1737 struct nfs_delegation *deleg_cur;
1738 nfs4_stateid freeme = { };
1741 fmode &= (FMODE_READ|FMODE_WRITE);
1744 spin_lock(&state->owner->so_lock);
1745 if (open_stateid != NULL) {
1746 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1750 deleg_cur = nfs4_get_valid_delegation(state->inode);
1751 if (deleg_cur == NULL)
1754 spin_lock(&deleg_cur->lock);
1755 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1756 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1757 (deleg_cur->type & fmode) != fmode)
1758 goto no_delegation_unlock;
1760 if (delegation == NULL)
1761 delegation = &deleg_cur->stateid;
1762 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1763 goto no_delegation_unlock;
1765 nfs_mark_delegation_referenced(deleg_cur);
1766 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1768 no_delegation_unlock:
1769 spin_unlock(&deleg_cur->lock);
1772 update_open_stateflags(state, fmode);
1773 spin_unlock(&state->owner->so_lock);
1776 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1777 nfs4_schedule_state_manager(clp);
1778 if (freeme.type != 0)
1779 nfs4_test_and_free_stateid(server, &freeme,
1780 state->owner->so_cred);
1785 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1786 const nfs4_stateid *stateid)
1788 struct nfs4_state *state = lsp->ls_state;
1791 spin_lock(&state->state_lock);
1792 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1794 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1796 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1799 spin_unlock(&state->state_lock);
1803 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1805 struct nfs_delegation *delegation;
1807 fmode &= FMODE_READ|FMODE_WRITE;
1809 delegation = nfs4_get_valid_delegation(inode);
1810 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1815 nfs4_inode_return_delegation(inode);
1818 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1820 struct nfs4_state *state = opendata->state;
1821 struct nfs_delegation *delegation;
1822 int open_mode = opendata->o_arg.open_flags;
1823 fmode_t fmode = opendata->o_arg.fmode;
1824 enum open_claim_type4 claim = opendata->o_arg.claim;
1825 nfs4_stateid stateid;
1829 spin_lock(&state->owner->so_lock);
1830 if (can_open_cached(state, fmode, open_mode, claim)) {
1831 update_open_stateflags(state, fmode);
1832 spin_unlock(&state->owner->so_lock);
1833 goto out_return_state;
1835 spin_unlock(&state->owner->so_lock);
1837 delegation = nfs4_get_valid_delegation(state->inode);
1838 if (!can_open_delegated(delegation, fmode, claim)) {
1842 /* Save the delegation */
1843 nfs4_stateid_copy(&stateid, &delegation->stateid);
1845 nfs_release_seqid(opendata->o_arg.seqid);
1846 if (!opendata->is_recover) {
1847 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1853 /* Try to update the stateid using the delegation */
1854 if (update_open_stateid(state, NULL, &stateid, fmode))
1855 goto out_return_state;
1858 return ERR_PTR(ret);
1860 refcount_inc(&state->count);
1865 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1867 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1868 struct nfs_delegation *delegation;
1869 int delegation_flags = 0;
1872 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1874 delegation_flags = delegation->flags;
1876 switch (data->o_arg.claim) {
1879 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1880 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1881 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1882 "returning a delegation for "
1883 "OPEN(CLAIM_DELEGATE_CUR)\n",
1887 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1888 nfs_inode_set_delegation(state->inode,
1889 data->owner->so_cred,
1890 data->o_res.delegation_type,
1891 &data->o_res.delegation,
1892 data->o_res.pagemod_limit);
1894 nfs_inode_reclaim_delegation(state->inode,
1895 data->owner->so_cred,
1896 data->o_res.delegation_type,
1897 &data->o_res.delegation,
1898 data->o_res.pagemod_limit);
1900 if (data->o_res.do_recall)
1901 nfs_async_inode_return_delegation(state->inode,
1902 &data->o_res.delegation);
1906 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1907 * and update the nfs4_state.
1909 static struct nfs4_state *
1910 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1912 struct inode *inode = data->state->inode;
1913 struct nfs4_state *state = data->state;
1916 if (!data->rpc_done) {
1917 if (data->rpc_status)
1918 return ERR_PTR(data->rpc_status);
1919 /* cached opens have already been processed */
1923 ret = nfs_refresh_inode(inode, &data->f_attr);
1925 return ERR_PTR(ret);
1927 if (data->o_res.delegation_type != 0)
1928 nfs4_opendata_check_deleg(data, state);
1930 if (!update_open_stateid(state, &data->o_res.stateid,
1931 NULL, data->o_arg.fmode))
1932 return ERR_PTR(-EAGAIN);
1933 refcount_inc(&state->count);
1938 static struct inode *
1939 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1941 struct inode *inode;
1943 switch (data->o_arg.claim) {
1944 case NFS4_OPEN_CLAIM_NULL:
1945 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1946 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1947 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1948 return ERR_PTR(-EAGAIN);
1949 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1950 &data->f_attr, data->f_label);
1953 inode = d_inode(data->dentry);
1955 nfs_refresh_inode(inode, &data->f_attr);
1960 static struct nfs4_state *
1961 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1963 struct nfs4_state *state;
1964 struct inode *inode;
1966 inode = nfs4_opendata_get_inode(data);
1968 return ERR_CAST(inode);
1969 if (data->state != NULL && data->state->inode == inode) {
1970 state = data->state;
1971 refcount_inc(&state->count);
1973 state = nfs4_get_open_state(inode, data->owner);
1976 state = ERR_PTR(-ENOMEM);
1980 static struct nfs4_state *
1981 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1983 struct nfs4_state *state;
1985 if (!data->rpc_done) {
1986 state = nfs4_try_open_cached(data);
1987 trace_nfs4_cached_open(data->state);
1991 state = nfs4_opendata_find_nfs4_state(data);
1995 if (data->o_res.delegation_type != 0)
1996 nfs4_opendata_check_deleg(data, state);
1997 if (!update_open_stateid(state, &data->o_res.stateid,
1998 NULL, data->o_arg.fmode)) {
1999 nfs4_put_open_state(state);
2000 state = ERR_PTR(-EAGAIN);
2003 nfs_release_seqid(data->o_arg.seqid);
2007 static struct nfs4_state *
2008 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2010 struct nfs4_state *ret;
2012 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2013 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2015 ret = _nfs4_opendata_to_nfs4_state(data);
2016 nfs4_sequence_free_slot(&data->o_res.seq_res);
2020 static struct nfs_open_context *
2021 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2023 struct nfs_inode *nfsi = NFS_I(state->inode);
2024 struct nfs_open_context *ctx;
2027 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2028 if (ctx->state != state)
2030 if ((ctx->mode & mode) != mode)
2032 if (!get_nfs_open_context(ctx))
2038 return ERR_PTR(-ENOENT);
2041 static struct nfs_open_context *
2042 nfs4_state_find_open_context(struct nfs4_state *state)
2044 struct nfs_open_context *ctx;
2046 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2049 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2052 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2055 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2056 struct nfs4_state *state, enum open_claim_type4 claim)
2058 struct nfs4_opendata *opendata;
2060 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2061 NULL, claim, GFP_NOFS);
2062 if (opendata == NULL)
2063 return ERR_PTR(-ENOMEM);
2064 opendata->state = state;
2065 refcount_inc(&state->count);
2069 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2072 struct nfs4_state *newstate;
2075 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2077 opendata->o_arg.open_flags = 0;
2078 opendata->o_arg.fmode = fmode;
2079 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2080 NFS_SB(opendata->dentry->d_sb),
2082 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2083 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2084 nfs4_init_opendata_res(opendata);
2085 ret = _nfs4_recover_proc_open(opendata);
2088 newstate = nfs4_opendata_to_nfs4_state(opendata);
2089 if (IS_ERR(newstate))
2090 return PTR_ERR(newstate);
2091 if (newstate != opendata->state)
2093 nfs4_close_state(newstate, fmode);
2097 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2101 /* memory barrier prior to reading state->n_* */
2103 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2106 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2109 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2113 * We may have performed cached opens for all three recoveries.
2114 * Check if we need to update the current stateid.
2116 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2117 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2118 write_seqlock(&state->seqlock);
2119 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2120 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2121 write_sequnlock(&state->seqlock);
2128 * reclaim state on the server after a reboot.
2130 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2132 struct nfs_delegation *delegation;
2133 struct nfs4_opendata *opendata;
2134 fmode_t delegation_type = 0;
2137 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2138 NFS4_OPEN_CLAIM_PREVIOUS);
2139 if (IS_ERR(opendata))
2140 return PTR_ERR(opendata);
2142 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2143 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2144 delegation_type = delegation->type;
2146 opendata->o_arg.u.delegation_type = delegation_type;
2147 status = nfs4_open_recover(opendata, state);
2148 nfs4_opendata_put(opendata);
2152 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2154 struct nfs_server *server = NFS_SERVER(state->inode);
2155 struct nfs4_exception exception = { };
2158 err = _nfs4_do_open_reclaim(ctx, state);
2159 trace_nfs4_open_reclaim(ctx, 0, err);
2160 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2162 if (err != -NFS4ERR_DELAY)
2164 nfs4_handle_exception(server, err, &exception);
2165 } while (exception.retry);
2169 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2171 struct nfs_open_context *ctx;
2174 ctx = nfs4_state_find_open_context(state);
2177 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2178 nfs_state_clear_open_state_flags(state);
2179 ret = nfs4_do_open_reclaim(ctx, state);
2180 put_nfs_open_context(ctx);
2184 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)
2188 printk(KERN_ERR "NFS: %s: unhandled error "
2189 "%d.\n", __func__, err);
2196 case -NFS4ERR_BADSESSION:
2197 case -NFS4ERR_BADSLOT:
2198 case -NFS4ERR_BAD_HIGH_SLOT:
2199 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2200 case -NFS4ERR_DEADSESSION:
2202 case -NFS4ERR_STALE_CLIENTID:
2203 case -NFS4ERR_STALE_STATEID:
2204 /* Don't recall a delegation if it was lost */
2205 nfs4_schedule_lease_recovery(server->nfs_client);
2207 case -NFS4ERR_MOVED:
2208 nfs4_schedule_migration_recovery(server);
2210 case -NFS4ERR_LEASE_MOVED:
2211 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2213 case -NFS4ERR_DELEG_REVOKED:
2214 case -NFS4ERR_ADMIN_REVOKED:
2215 case -NFS4ERR_EXPIRED:
2216 case -NFS4ERR_BAD_STATEID:
2217 case -NFS4ERR_OPENMODE:
2218 nfs_inode_find_state_and_recover(state->inode,
2220 nfs4_schedule_stateid_recovery(server, state);
2222 case -NFS4ERR_DELAY:
2223 case -NFS4ERR_GRACE:
2227 case -NFS4ERR_DENIED:
2229 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2231 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2238 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2239 struct nfs4_state *state, const nfs4_stateid *stateid)
2241 struct nfs_server *server = NFS_SERVER(state->inode);
2242 struct nfs4_opendata *opendata;
2245 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2246 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2247 if (IS_ERR(opendata))
2248 return PTR_ERR(opendata);
2249 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2250 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2251 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2255 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2256 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2260 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2261 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2265 nfs_state_clear_delegation(state);
2267 nfs4_opendata_put(opendata);
2268 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2271 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2273 struct nfs4_opendata *data = calldata;
2275 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2276 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2279 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2281 struct nfs4_opendata *data = calldata;
2283 nfs40_sequence_done(task, &data->c_res.seq_res);
2285 data->rpc_status = task->tk_status;
2286 if (data->rpc_status == 0) {
2287 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2288 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2289 renew_lease(data->o_res.server, data->timestamp);
2290 data->rpc_done = true;
2294 static void nfs4_open_confirm_release(void *calldata)
2296 struct nfs4_opendata *data = calldata;
2297 struct nfs4_state *state = NULL;
2299 /* If this request hasn't been cancelled, do nothing */
2300 if (!data->cancelled)
2302 /* In case of error, no cleanup! */
2303 if (!data->rpc_done)
2305 state = nfs4_opendata_to_nfs4_state(data);
2307 nfs4_close_state(state, data->o_arg.fmode);
2309 nfs4_opendata_put(data);
2312 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2313 .rpc_call_prepare = nfs4_open_confirm_prepare,
2314 .rpc_call_done = nfs4_open_confirm_done,
2315 .rpc_release = nfs4_open_confirm_release,
2319 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2321 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2323 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2324 struct rpc_task *task;
2325 struct rpc_message msg = {
2326 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2327 .rpc_argp = &data->c_arg,
2328 .rpc_resp = &data->c_res,
2329 .rpc_cred = data->owner->so_cred,
2331 struct rpc_task_setup task_setup_data = {
2332 .rpc_client = server->client,
2333 .rpc_message = &msg,
2334 .callback_ops = &nfs4_open_confirm_ops,
2335 .callback_data = data,
2336 .workqueue = nfsiod_workqueue,
2337 .flags = RPC_TASK_ASYNC,
2341 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2343 kref_get(&data->kref);
2344 data->rpc_done = false;
2345 data->rpc_status = 0;
2346 data->timestamp = jiffies;
2347 task = rpc_run_task(&task_setup_data);
2349 return PTR_ERR(task);
2350 status = rpc_wait_for_completion_task(task);
2352 data->cancelled = true;
2355 status = data->rpc_status;
2360 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2362 struct nfs4_opendata *data = calldata;
2363 struct nfs4_state_owner *sp = data->owner;
2364 struct nfs_client *clp = sp->so_server->nfs_client;
2365 enum open_claim_type4 claim = data->o_arg.claim;
2367 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2370 * Check if we still need to send an OPEN call, or if we can use
2371 * a delegation instead.
2373 if (data->state != NULL) {
2374 struct nfs_delegation *delegation;
2376 if (can_open_cached(data->state, data->o_arg.fmode,
2377 data->o_arg.open_flags, claim))
2380 delegation = nfs4_get_valid_delegation(data->state->inode);
2381 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2382 goto unlock_no_action;
2385 /* Update client id. */
2386 data->o_arg.clientid = clp->cl_clientid;
2390 case NFS4_OPEN_CLAIM_PREVIOUS:
2391 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2392 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2393 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2395 case NFS4_OPEN_CLAIM_FH:
2396 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2398 data->timestamp = jiffies;
2399 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2400 &data->o_arg.seq_args,
2401 &data->o_res.seq_res,
2403 nfs_release_seqid(data->o_arg.seqid);
2405 /* Set the create mode (note dependency on the session type) */
2406 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2407 if (data->o_arg.open_flags & O_EXCL) {
2408 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2409 if (nfs4_has_persistent_session(clp))
2410 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2411 else if (clp->cl_mvops->minor_version > 0)
2412 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2416 trace_nfs4_cached_open(data->state);
2419 task->tk_action = NULL;
2421 nfs4_sequence_done(task, &data->o_res.seq_res);
2424 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2426 struct nfs4_opendata *data = calldata;
2428 data->rpc_status = task->tk_status;
2430 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2433 if (task->tk_status == 0) {
2434 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2435 switch (data->o_res.f_attr->mode & S_IFMT) {
2439 data->rpc_status = -ELOOP;
2442 data->rpc_status = -EISDIR;
2445 data->rpc_status = -ENOTDIR;
2448 renew_lease(data->o_res.server, data->timestamp);
2449 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2450 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2452 data->rpc_done = true;
2455 static void nfs4_open_release(void *calldata)
2457 struct nfs4_opendata *data = calldata;
2458 struct nfs4_state *state = NULL;
2460 /* If this request hasn't been cancelled, do nothing */
2461 if (!data->cancelled)
2463 /* In case of error, no cleanup! */
2464 if (data->rpc_status != 0 || !data->rpc_done)
2466 /* In case we need an open_confirm, no cleanup! */
2467 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2469 state = nfs4_opendata_to_nfs4_state(data);
2471 nfs4_close_state(state, data->o_arg.fmode);
2473 nfs4_opendata_put(data);
2476 static const struct rpc_call_ops nfs4_open_ops = {
2477 .rpc_call_prepare = nfs4_open_prepare,
2478 .rpc_call_done = nfs4_open_done,
2479 .rpc_release = nfs4_open_release,
2482 static int nfs4_run_open_task(struct nfs4_opendata *data,
2483 struct nfs_open_context *ctx)
2485 struct inode *dir = d_inode(data->dir);
2486 struct nfs_server *server = NFS_SERVER(dir);
2487 struct nfs_openargs *o_arg = &data->o_arg;
2488 struct nfs_openres *o_res = &data->o_res;
2489 struct rpc_task *task;
2490 struct rpc_message msg = {
2491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2494 .rpc_cred = data->owner->so_cred,
2496 struct rpc_task_setup task_setup_data = {
2497 .rpc_client = server->client,
2498 .rpc_message = &msg,
2499 .callback_ops = &nfs4_open_ops,
2500 .callback_data = data,
2501 .workqueue = nfsiod_workqueue,
2502 .flags = RPC_TASK_ASYNC,
2506 kref_get(&data->kref);
2507 data->rpc_done = false;
2508 data->rpc_status = 0;
2509 data->cancelled = false;
2510 data->is_recover = false;
2512 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2513 data->is_recover = true;
2514 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2516 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2517 pnfs_lgopen_prepare(data, ctx);
2519 task = rpc_run_task(&task_setup_data);
2521 return PTR_ERR(task);
2522 status = rpc_wait_for_completion_task(task);
2524 data->cancelled = true;
2527 status = data->rpc_status;
2533 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2535 struct inode *dir = d_inode(data->dir);
2536 struct nfs_openres *o_res = &data->o_res;
2539 status = nfs4_run_open_task(data, NULL);
2540 if (status != 0 || !data->rpc_done)
2543 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2545 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2546 status = _nfs4_proc_open_confirm(data);
2552 * Additional permission checks in order to distinguish between an
2553 * open for read, and an open for execute. This works around the
2554 * fact that NFSv4 OPEN treats read and execute permissions as being
2556 * Note that in the non-execute case, we want to turn off permission
2557 * checking if we just created a new file (POSIX open() semantics).
2559 static int nfs4_opendata_access(const struct cred *cred,
2560 struct nfs4_opendata *opendata,
2561 struct nfs4_state *state, fmode_t fmode,
2564 struct nfs_access_entry cache;
2567 /* access call failed or for some reason the server doesn't
2568 * support any access modes -- defer access call until later */
2569 if (opendata->o_res.access_supported == 0)
2574 * Use openflags to check for exec, because fmode won't
2575 * always have FMODE_EXEC set when file open for exec.
2577 if (openflags & __FMODE_EXEC) {
2578 /* ONLY check for exec rights */
2579 if (S_ISDIR(state->inode->i_mode))
2580 mask = NFS4_ACCESS_LOOKUP;
2582 mask = NFS4_ACCESS_EXECUTE;
2583 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2584 mask = NFS4_ACCESS_READ;
2587 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2588 nfs_access_add_cache(state->inode, &cache);
2590 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2591 if ((mask & ~cache.mask & flags) == 0)
2598 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2600 static int _nfs4_proc_open(struct nfs4_opendata *data,
2601 struct nfs_open_context *ctx)
2603 struct inode *dir = d_inode(data->dir);
2604 struct nfs_server *server = NFS_SERVER(dir);
2605 struct nfs_openargs *o_arg = &data->o_arg;
2606 struct nfs_openres *o_res = &data->o_res;
2609 status = nfs4_run_open_task(data, ctx);
2610 if (!data->rpc_done)
2613 if (status == -NFS4ERR_BADNAME &&
2614 !(o_arg->open_flags & O_CREAT))
2619 nfs_fattr_map_and_free_names(server, &data->f_attr);
2621 if (o_arg->open_flags & O_CREAT) {
2622 if (o_arg->open_flags & O_EXCL)
2623 data->file_created = true;
2624 else if (o_res->cinfo.before != o_res->cinfo.after)
2625 data->file_created = true;
2626 if (data->file_created ||
2627 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2628 update_changeattr(dir, &o_res->cinfo,
2629 o_res->f_attr->time_start, 0);
2631 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2632 server->caps &= ~NFS_CAP_POSIX_LOCK;
2633 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2634 status = _nfs4_proc_open_confirm(data);
2638 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2639 nfs4_sequence_free_slot(&o_res->seq_res);
2640 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2641 o_res->f_label, NULL);
2648 * reclaim state on the server after a network partition.
2649 * Assumes caller holds the appropriate lock
2651 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2653 struct nfs4_opendata *opendata;
2656 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2657 NFS4_OPEN_CLAIM_FH);
2658 if (IS_ERR(opendata))
2659 return PTR_ERR(opendata);
2660 ret = nfs4_open_recover(opendata, state);
2662 d_drop(ctx->dentry);
2663 nfs4_opendata_put(opendata);
2667 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2669 struct nfs_server *server = NFS_SERVER(state->inode);
2670 struct nfs4_exception exception = { };
2674 err = _nfs4_open_expired(ctx, state);
2675 trace_nfs4_open_expired(ctx, 0, err);
2676 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2681 case -NFS4ERR_GRACE:
2682 case -NFS4ERR_DELAY:
2683 nfs4_handle_exception(server, err, &exception);
2686 } while (exception.retry);
2691 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2693 struct nfs_open_context *ctx;
2696 ctx = nfs4_state_find_open_context(state);
2699 ret = nfs4_do_open_expired(ctx, state);
2700 put_nfs_open_context(ctx);
2704 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2705 const nfs4_stateid *stateid)
2707 nfs_remove_bad_delegation(state->inode, stateid);
2708 nfs_state_clear_delegation(state);
2711 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2713 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2714 nfs_finish_clear_delegation_stateid(state, NULL);
2717 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2719 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2720 nfs40_clear_delegation_stateid(state);
2721 nfs_state_clear_open_state_flags(state);
2722 return nfs4_open_expired(sp, state);
2725 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2726 nfs4_stateid *stateid,
2727 const struct cred *cred)
2729 return -NFS4ERR_BAD_STATEID;
2732 #if defined(CONFIG_NFS_V4_1)
2733 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2734 nfs4_stateid *stateid,
2735 const struct cred *cred)
2739 switch (stateid->type) {
2742 case NFS4_INVALID_STATEID_TYPE:
2743 case NFS4_SPECIAL_STATEID_TYPE:
2744 return -NFS4ERR_BAD_STATEID;
2745 case NFS4_REVOKED_STATEID_TYPE:
2749 status = nfs41_test_stateid(server, stateid, cred);
2751 case -NFS4ERR_EXPIRED:
2752 case -NFS4ERR_ADMIN_REVOKED:
2753 case -NFS4ERR_DELEG_REVOKED:
2759 /* Ack the revoked state to the server */
2760 nfs41_free_stateid(server, stateid, cred, true);
2761 return -NFS4ERR_EXPIRED;
2764 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2766 struct nfs_server *server = NFS_SERVER(state->inode);
2767 nfs4_stateid stateid;
2768 struct nfs_delegation *delegation;
2769 const struct cred *cred = NULL;
2770 int status, ret = NFS_OK;
2772 /* Get the delegation credential for use by test/free_stateid */
2774 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2775 if (delegation == NULL) {
2777 nfs_state_clear_delegation(state);
2781 nfs4_stateid_copy(&stateid, &delegation->stateid);
2783 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2784 &delegation->flags)) {
2789 if (delegation->cred)
2790 cred = get_cred(delegation->cred);
2792 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2793 trace_nfs4_test_delegation_stateid(state, NULL, status);
2794 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2795 nfs_finish_clear_delegation_stateid(state, &stateid);
2803 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2807 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2808 nfs4_copy_delegation_stateid(state->inode, state->state,
2810 nfs4_stateid_match_other(&state->stateid, &tmp))
2811 nfs_state_set_delegation(state, &tmp, state->state);
2813 nfs_state_clear_delegation(state);
2817 * nfs41_check_expired_locks - possibly free a lock stateid
2819 * @state: NFSv4 state for an inode
2821 * Returns NFS_OK if recovery for this stateid is now finished.
2822 * Otherwise a negative NFS4ERR value is returned.
2824 static int nfs41_check_expired_locks(struct nfs4_state *state)
2826 int status, ret = NFS_OK;
2827 struct nfs4_lock_state *lsp, *prev = NULL;
2828 struct nfs_server *server = NFS_SERVER(state->inode);
2830 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2833 spin_lock(&state->state_lock);
2834 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2835 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2836 const struct cred *cred = lsp->ls_state->owner->so_cred;
2838 refcount_inc(&lsp->ls_count);
2839 spin_unlock(&state->state_lock);
2841 nfs4_put_lock_state(prev);
2844 status = nfs41_test_and_free_expired_stateid(server,
2847 trace_nfs4_test_lock_stateid(state, lsp, status);
2848 if (status == -NFS4ERR_EXPIRED ||
2849 status == -NFS4ERR_BAD_STATEID) {
2850 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2851 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2852 if (!recover_lost_locks)
2853 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2854 } else if (status != NFS_OK) {
2856 nfs4_put_lock_state(prev);
2859 spin_lock(&state->state_lock);
2862 spin_unlock(&state->state_lock);
2863 nfs4_put_lock_state(prev);
2869 * nfs41_check_open_stateid - possibly free an open stateid
2871 * @state: NFSv4 state for an inode
2873 * Returns NFS_OK if recovery for this stateid is now finished.
2874 * Otherwise a negative NFS4ERR value is returned.
2876 static int nfs41_check_open_stateid(struct nfs4_state *state)
2878 struct nfs_server *server = NFS_SERVER(state->inode);
2879 nfs4_stateid *stateid = &state->open_stateid;
2880 const struct cred *cred = state->owner->so_cred;
2883 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2884 return -NFS4ERR_BAD_STATEID;
2885 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2886 trace_nfs4_test_open_stateid(state, NULL, status);
2887 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2888 nfs_state_clear_open_state_flags(state);
2889 stateid->type = NFS4_INVALID_STATEID_TYPE;
2892 if (nfs_open_stateid_recover_openmode(state))
2893 return -NFS4ERR_OPENMODE;
2897 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2901 status = nfs41_check_delegation_stateid(state);
2902 if (status != NFS_OK)
2904 nfs41_delegation_recover_stateid(state);
2906 status = nfs41_check_expired_locks(state);
2907 if (status != NFS_OK)
2909 status = nfs41_check_open_stateid(state);
2910 if (status != NFS_OK)
2911 status = nfs4_open_expired(sp, state);
2917 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2918 * fields corresponding to attributes that were used to store the verifier.
2919 * Make sure we clobber those fields in the later setattr call
2921 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2922 struct iattr *sattr, struct nfs4_label **label)
2924 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2929 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2930 attrset[i] = opendata->o_res.attrset[i];
2931 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2932 attrset[i] &= ~bitmask[i];
2935 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2936 sattr->ia_valid : 0;
2938 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2939 if (sattr->ia_valid & ATTR_ATIME_SET)
2940 ret |= ATTR_ATIME_SET;
2945 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2946 if (sattr->ia_valid & ATTR_MTIME_SET)
2947 ret |= ATTR_MTIME_SET;
2952 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
2957 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2958 int flags, struct nfs_open_context *ctx)
2960 struct nfs4_state_owner *sp = opendata->owner;
2961 struct nfs_server *server = sp->so_server;
2962 struct dentry *dentry;
2963 struct nfs4_state *state;
2964 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
2968 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2970 ret = _nfs4_proc_open(opendata, ctx);
2974 state = _nfs4_opendata_to_nfs4_state(opendata);
2975 ret = PTR_ERR(state);
2979 if (server->caps & NFS_CAP_POSIX_LOCK)
2980 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2981 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2982 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2984 dentry = opendata->dentry;
2985 if (d_really_is_negative(dentry)) {
2986 struct dentry *alias;
2988 alias = d_exact_alias(dentry, state->inode);
2990 alias = d_splice_alias(igrab(state->inode), dentry);
2991 /* d_splice_alias() can't fail here - it's a non-directory */
2994 ctx->dentry = dentry = alias;
2996 nfs_set_verifier(dentry,
2997 nfs_save_change_attribute(d_inode(opendata->dir)));
3000 /* Parse layoutget results before we check for access */
3001 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3003 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3008 if (d_inode(dentry) == state->inode) {
3009 nfs_inode_attach_open_context(ctx);
3010 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3011 nfs4_schedule_stateid_recovery(server, state);
3015 if (!opendata->cancelled)
3016 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3021 * Returns a referenced nfs4_state
3023 static int _nfs4_do_open(struct inode *dir,
3024 struct nfs_open_context *ctx,
3026 const struct nfs4_open_createattrs *c,
3029 struct nfs4_state_owner *sp;
3030 struct nfs4_state *state = NULL;
3031 struct nfs_server *server = NFS_SERVER(dir);
3032 struct nfs4_opendata *opendata;
3033 struct dentry *dentry = ctx->dentry;
3034 const struct cred *cred = ctx->cred;
3035 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3036 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3037 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3038 struct iattr *sattr = c->sattr;
3039 struct nfs4_label *label = c->label;
3040 struct nfs4_label *olabel = NULL;
3043 /* Protect against reboot recovery conflicts */
3045 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3047 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3050 status = nfs4_client_recover_expired_lease(server->nfs_client);
3052 goto err_put_state_owner;
3053 if (d_really_is_positive(dentry))
3054 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3056 if (d_really_is_positive(dentry))
3057 claim = NFS4_OPEN_CLAIM_FH;
3058 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3059 c, claim, GFP_KERNEL);
3060 if (opendata == NULL)
3061 goto err_put_state_owner;
3064 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3065 if (IS_ERR(olabel)) {
3066 status = PTR_ERR(olabel);
3067 goto err_opendata_put;
3071 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3072 if (!opendata->f_attr.mdsthreshold) {
3073 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3074 if (!opendata->f_attr.mdsthreshold)
3075 goto err_free_label;
3077 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3079 if (d_really_is_positive(dentry))
3080 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3082 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3084 goto err_free_label;
3087 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3088 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3089 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3091 * send create attributes which was not set by open
3092 * with an extra setattr.
3094 if (attrs || label) {
3095 unsigned ia_old = sattr->ia_valid;
3097 sattr->ia_valid = attrs;
3098 nfs_fattr_init(opendata->o_res.f_attr);
3099 status = nfs4_do_setattr(state->inode, cred,
3100 opendata->o_res.f_attr, sattr,
3101 ctx, label, olabel);
3103 nfs_setattr_update_inode(state->inode, sattr,
3104 opendata->o_res.f_attr);
3105 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3107 sattr->ia_valid = ia_old;
3110 if (opened && opendata->file_created)
3113 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3114 *ctx_th = opendata->f_attr.mdsthreshold;
3115 opendata->f_attr.mdsthreshold = NULL;
3118 nfs4_label_free(olabel);
3120 nfs4_opendata_put(opendata);
3121 nfs4_put_state_owner(sp);
3124 nfs4_label_free(olabel);
3126 nfs4_opendata_put(opendata);
3127 err_put_state_owner:
3128 nfs4_put_state_owner(sp);
3134 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3135 struct nfs_open_context *ctx,
3137 struct iattr *sattr,
3138 struct nfs4_label *label,
3141 struct nfs_server *server = NFS_SERVER(dir);
3142 struct nfs4_exception exception = {
3143 .interruptible = true,
3145 struct nfs4_state *res;
3146 struct nfs4_open_createattrs c = {
3150 [0] = (__u32)jiffies,
3151 [1] = (__u32)current->pid,
3157 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3159 trace_nfs4_open_file(ctx, flags, status);
3162 /* NOTE: BAD_SEQID means the server and client disagree about the
3163 * book-keeping w.r.t. state-changing operations
3164 * (OPEN/CLOSE/LOCK/LOCKU...)
3165 * It is actually a sign of a bug on the client or on the server.
3167 * If we receive a BAD_SEQID error in the particular case of
3168 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3169 * have unhashed the old state_owner for us, and that we can
3170 * therefore safely retry using a new one. We should still warn
3171 * the user though...
3173 if (status == -NFS4ERR_BAD_SEQID) {
3174 pr_warn_ratelimited("NFS: v4 server %s "
3175 " returned a bad sequence-id error!\n",
3176 NFS_SERVER(dir)->nfs_client->cl_hostname);
3177 exception.retry = 1;
3181 * BAD_STATEID on OPEN means that the server cancelled our
3182 * state before it received the OPEN_CONFIRM.
3183 * Recover by retrying the request as per the discussion
3184 * on Page 181 of RFC3530.
3186 if (status == -NFS4ERR_BAD_STATEID) {
3187 exception.retry = 1;
3190 if (status == -EAGAIN) {
3191 /* We must have found a delegation */
3192 exception.retry = 1;
3195 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3197 res = ERR_PTR(nfs4_handle_exception(server,
3198 status, &exception));
3199 } while (exception.retry);
3203 static int _nfs4_do_setattr(struct inode *inode,
3204 struct nfs_setattrargs *arg,
3205 struct nfs_setattrres *res,
3206 const struct cred *cred,
3207 struct nfs_open_context *ctx)
3209 struct nfs_server *server = NFS_SERVER(inode);
3210 struct rpc_message msg = {
3211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3216 const struct cred *delegation_cred = NULL;
3217 unsigned long timestamp = jiffies;
3221 nfs_fattr_init(res->fattr);
3223 /* Servers should only apply open mode checks for file size changes */
3224 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3228 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3229 /* Use that stateid */
3230 } else if (ctx != NULL && ctx->state) {
3231 struct nfs_lock_context *l_ctx;
3232 if (!nfs4_valid_open_stateid(ctx->state))
3234 l_ctx = nfs_get_lock_context(ctx);
3236 return PTR_ERR(l_ctx);
3237 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3238 &arg->stateid, &delegation_cred);
3239 nfs_put_lock_context(l_ctx);
3244 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3246 if (delegation_cred)
3247 msg.rpc_cred = delegation_cred;
3249 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3251 put_cred(delegation_cred);
3252 if (status == 0 && ctx != NULL)
3253 renew_lease(server, timestamp);
3254 trace_nfs4_setattr(inode, &arg->stateid, status);
3258 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3259 struct nfs_fattr *fattr, struct iattr *sattr,
3260 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3261 struct nfs4_label *olabel)
3263 struct nfs_server *server = NFS_SERVER(inode);
3264 __u32 bitmask[NFS4_BITMASK_SZ];
3265 struct nfs4_state *state = ctx ? ctx->state : NULL;
3266 struct nfs_setattrargs arg = {
3267 .fh = NFS_FH(inode),
3273 struct nfs_setattrres res = {
3278 struct nfs4_exception exception = {
3281 .stateid = &arg.stateid,
3286 nfs4_bitmap_copy_adjust_setattr(bitmask,
3287 nfs4_bitmask(server, olabel),
3290 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3292 case -NFS4ERR_OPENMODE:
3293 if (!(sattr->ia_valid & ATTR_SIZE)) {
3294 pr_warn_once("NFSv4: server %s is incorrectly "
3295 "applying open mode checks to "
3296 "a SETATTR that is not "
3297 "changing file size.\n",
3298 server->nfs_client->cl_hostname);
3300 if (state && !(state->state & FMODE_WRITE)) {
3302 if (sattr->ia_valid & ATTR_OPEN)
3307 err = nfs4_handle_exception(server, err, &exception);
3308 } while (exception.retry);
3314 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3316 if (inode == NULL || !nfs_have_layout(inode))
3319 return pnfs_wait_on_layoutreturn(inode, task);
3323 * Update the seqid of an open stateid
3325 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3326 struct nfs4_state *state)
3333 if (!nfs4_valid_open_stateid(state))
3335 seq = read_seqbegin(&state->seqlock);
3336 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3337 nfs4_stateid_copy(dst, &state->open_stateid);
3338 if (read_seqretry(&state->seqlock, seq))
3342 seqid_open = state->open_stateid.seqid;
3343 if (read_seqretry(&state->seqlock, seq))
3346 dst_seqid = be32_to_cpu(dst->seqid);
3347 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3348 dst->seqid = seqid_open;
3354 * Update the seqid of an open stateid after receiving
3355 * NFS4ERR_OLD_STATEID
3357 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3358 struct nfs4_state *state)
3367 if (!nfs4_valid_open_stateid(state))
3369 seq = read_seqbegin(&state->seqlock);
3370 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3371 if (read_seqretry(&state->seqlock, seq))
3375 seqid_open = state->open_stateid.seqid;
3376 if (read_seqretry(&state->seqlock, seq))
3379 dst_seqid = be32_to_cpu(dst->seqid);
3380 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) >= 0)
3381 dst->seqid = cpu_to_be32(dst_seqid + 1);
3383 dst->seqid = seqid_open;
3391 struct nfs4_closedata {
3392 struct inode *inode;
3393 struct nfs4_state *state;
3394 struct nfs_closeargs arg;
3395 struct nfs_closeres res;
3397 struct nfs4_layoutreturn_args arg;
3398 struct nfs4_layoutreturn_res res;
3399 struct nfs4_xdr_opaque_data ld_private;
3403 struct nfs_fattr fattr;
3404 unsigned long timestamp;
3407 static void nfs4_free_closedata(void *data)
3409 struct nfs4_closedata *calldata = data;
3410 struct nfs4_state_owner *sp = calldata->state->owner;
3411 struct super_block *sb = calldata->state->inode->i_sb;
3413 if (calldata->lr.roc)
3414 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3415 calldata->res.lr_ret);
3416 nfs4_put_open_state(calldata->state);
3417 nfs_free_seqid(calldata->arg.seqid);
3418 nfs4_put_state_owner(sp);
3419 nfs_sb_deactive(sb);
3423 static void nfs4_close_done(struct rpc_task *task, void *data)
3425 struct nfs4_closedata *calldata = data;
3426 struct nfs4_state *state = calldata->state;
3427 struct nfs_server *server = NFS_SERVER(calldata->inode);
3428 nfs4_stateid *res_stateid = NULL;
3429 struct nfs4_exception exception = {
3431 .inode = calldata->inode,
3432 .stateid = &calldata->arg.stateid,
3435 dprintk("%s: begin!\n", __func__);
3436 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3438 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3440 /* Handle Layoutreturn errors */
3441 if (pnfs_roc_done(task, calldata->inode,
3442 &calldata->arg.lr_args,
3443 &calldata->res.lr_res,
3444 &calldata->res.lr_ret) == -EAGAIN)
3447 /* hmm. we are done with the inode, and in the process of freeing
3448 * the state_owner. we keep this around to process errors
3450 switch (task->tk_status) {
3452 res_stateid = &calldata->res.stateid;
3453 renew_lease(server, calldata->timestamp);
3455 case -NFS4ERR_ACCESS:
3456 if (calldata->arg.bitmask != NULL) {
3457 calldata->arg.bitmask = NULL;
3458 calldata->res.fattr = NULL;
3463 case -NFS4ERR_OLD_STATEID:
3464 /* Did we race with OPEN? */
3465 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3469 case -NFS4ERR_ADMIN_REVOKED:
3470 case -NFS4ERR_STALE_STATEID:
3471 case -NFS4ERR_EXPIRED:
3472 nfs4_free_revoked_stateid(server,
3473 &calldata->arg.stateid,
3474 task->tk_msg.rpc_cred);
3476 case -NFS4ERR_BAD_STATEID:
3477 if (calldata->arg.fmode == 0)
3481 task->tk_status = nfs4_async_handle_exception(task,
3482 server, task->tk_status, &exception);
3483 if (exception.retry)
3486 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3487 res_stateid, calldata->arg.fmode);
3489 task->tk_status = 0;
3490 nfs_release_seqid(calldata->arg.seqid);
3491 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3492 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3495 task->tk_status = 0;
3496 rpc_restart_call_prepare(task);
3500 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3502 struct nfs4_closedata *calldata = data;
3503 struct nfs4_state *state = calldata->state;
3504 struct inode *inode = calldata->inode;
3505 struct pnfs_layout_hdr *lo;
3506 bool is_rdonly, is_wronly, is_rdwr;
3509 dprintk("%s: begin!\n", __func__);
3510 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3513 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3514 spin_lock(&state->owner->so_lock);
3515 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3516 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3517 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3518 /* Calculate the change in open mode */
3519 calldata->arg.fmode = 0;
3520 if (state->n_rdwr == 0) {
3521 if (state->n_rdonly == 0)
3522 call_close |= is_rdonly;
3524 calldata->arg.fmode |= FMODE_READ;
3525 if (state->n_wronly == 0)
3526 call_close |= is_wronly;
3528 calldata->arg.fmode |= FMODE_WRITE;
3529 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3530 call_close |= is_rdwr;
3532 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3534 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3535 if (!nfs4_valid_open_stateid(state))
3537 spin_unlock(&state->owner->so_lock);
3540 /* Note: exit _without_ calling nfs4_close_done */
3544 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3545 nfs_release_seqid(calldata->arg.seqid);
3549 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3550 if (lo && !pnfs_layout_is_valid(lo)) {
3551 calldata->arg.lr_args = NULL;
3552 calldata->res.lr_res = NULL;
3555 if (calldata->arg.fmode == 0)
3556 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3558 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3559 /* Close-to-open cache consistency revalidation */
3560 if (!nfs4_have_delegation(inode, FMODE_READ))
3561 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3563 calldata->arg.bitmask = NULL;
3566 calldata->arg.share_access =
3567 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3568 calldata->arg.fmode, 0);
3570 if (calldata->res.fattr == NULL)
3571 calldata->arg.bitmask = NULL;
3572 else if (calldata->arg.bitmask == NULL)
3573 calldata->res.fattr = NULL;
3574 calldata->timestamp = jiffies;
3575 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3576 &calldata->arg.seq_args,
3577 &calldata->res.seq_res,
3579 nfs_release_seqid(calldata->arg.seqid);
3580 dprintk("%s: done!\n", __func__);
3583 task->tk_action = NULL;
3585 nfs4_sequence_done(task, &calldata->res.seq_res);
3588 static const struct rpc_call_ops nfs4_close_ops = {
3589 .rpc_call_prepare = nfs4_close_prepare,
3590 .rpc_call_done = nfs4_close_done,
3591 .rpc_release = nfs4_free_closedata,
3595 * It is possible for data to be read/written from a mem-mapped file
3596 * after the sys_close call (which hits the vfs layer as a flush).
3597 * This means that we can't safely call nfsv4 close on a file until
3598 * the inode is cleared. This in turn means that we are not good
3599 * NFSv4 citizens - we do not indicate to the server to update the file's
3600 * share state even when we are done with one of the three share
3601 * stateid's in the inode.
3603 * NOTE: Caller must be holding the sp->so_owner semaphore!
3605 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3607 struct nfs_server *server = NFS_SERVER(state->inode);
3608 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3609 struct nfs4_closedata *calldata;
3610 struct nfs4_state_owner *sp = state->owner;
3611 struct rpc_task *task;
3612 struct rpc_message msg = {
3613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3614 .rpc_cred = state->owner->so_cred,
3616 struct rpc_task_setup task_setup_data = {
3617 .rpc_client = server->client,
3618 .rpc_message = &msg,
3619 .callback_ops = &nfs4_close_ops,
3620 .workqueue = nfsiod_workqueue,
3621 .flags = RPC_TASK_ASYNC,
3623 int status = -ENOMEM;
3625 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3626 &task_setup_data.rpc_client, &msg);
3628 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3629 if (calldata == NULL)
3631 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3632 calldata->inode = state->inode;
3633 calldata->state = state;
3634 calldata->arg.fh = NFS_FH(state->inode);
3635 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3636 goto out_free_calldata;
3637 /* Serialization for the sequence id */
3638 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3639 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3640 if (IS_ERR(calldata->arg.seqid))
3641 goto out_free_calldata;
3642 nfs_fattr_init(&calldata->fattr);
3643 calldata->arg.fmode = 0;
3644 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3645 calldata->res.fattr = &calldata->fattr;
3646 calldata->res.seqid = calldata->arg.seqid;
3647 calldata->res.server = server;
3648 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3649 calldata->lr.roc = pnfs_roc(state->inode,
3650 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3651 if (calldata->lr.roc) {
3652 calldata->arg.lr_args = &calldata->lr.arg;
3653 calldata->res.lr_res = &calldata->lr.res;
3655 nfs_sb_active(calldata->inode->i_sb);
3657 msg.rpc_argp = &calldata->arg;
3658 msg.rpc_resp = &calldata->res;
3659 task_setup_data.callback_data = calldata;
3660 task = rpc_run_task(&task_setup_data);
3662 return PTR_ERR(task);
3665 status = rpc_wait_for_completion_task(task);
3671 nfs4_put_open_state(state);
3672 nfs4_put_state_owner(sp);
3676 static struct inode *
3677 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3678 int open_flags, struct iattr *attr, int *opened)
3680 struct nfs4_state *state;
3681 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3683 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3685 /* Protect against concurrent sillydeletes */
3686 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3688 nfs4_label_release_security(label);
3691 return ERR_CAST(state);
3692 return state->inode;
3695 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3697 if (ctx->state == NULL)
3700 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3702 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3705 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3706 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3707 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3709 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3711 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3712 struct nfs4_server_caps_arg args = {
3716 struct nfs4_server_caps_res res = {};
3717 struct rpc_message msg = {
3718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3725 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3726 FATTR4_WORD0_FH_EXPIRE_TYPE |
3727 FATTR4_WORD0_LINK_SUPPORT |
3728 FATTR4_WORD0_SYMLINK_SUPPORT |
3729 FATTR4_WORD0_ACLSUPPORT;
3731 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3733 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3735 /* Sanity check the server answers */
3736 switch (minorversion) {
3738 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3739 res.attr_bitmask[2] = 0;
3742 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3745 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3747 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3748 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3749 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3750 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3751 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3752 NFS_CAP_CTIME|NFS_CAP_MTIME|
3753 NFS_CAP_SECURITY_LABEL);
3754 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3755 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3756 server->caps |= NFS_CAP_ACLS;
3757 if (res.has_links != 0)
3758 server->caps |= NFS_CAP_HARDLINKS;
3759 if (res.has_symlinks != 0)
3760 server->caps |= NFS_CAP_SYMLINKS;
3761 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3762 server->caps |= NFS_CAP_FILEID;
3763 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3764 server->caps |= NFS_CAP_MODE;
3765 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3766 server->caps |= NFS_CAP_NLINK;
3767 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3768 server->caps |= NFS_CAP_OWNER;
3769 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3770 server->caps |= NFS_CAP_OWNER_GROUP;
3771 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3772 server->caps |= NFS_CAP_ATIME;
3773 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3774 server->caps |= NFS_CAP_CTIME;
3775 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3776 server->caps |= NFS_CAP_MTIME;
3777 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3778 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3779 server->caps |= NFS_CAP_SECURITY_LABEL;
3781 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3782 sizeof(server->attr_bitmask));
3783 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3785 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3786 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3787 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3788 server->cache_consistency_bitmask[2] = 0;
3790 /* Avoid a regression due to buggy server */
3791 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3792 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3793 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3794 sizeof(server->exclcreat_bitmask));
3796 server->acl_bitmask = res.acl_bitmask;
3797 server->fh_expire_type = res.fh_expire_type;
3803 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3805 struct nfs4_exception exception = {
3806 .interruptible = true,
3810 err = nfs4_handle_exception(server,
3811 _nfs4_server_capabilities(server, fhandle),
3813 } while (exception.retry);
3817 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3818 struct nfs_fsinfo *info)
3821 struct nfs4_lookup_root_arg args = {
3824 struct nfs4_lookup_res res = {
3826 .fattr = info->fattr,
3829 struct rpc_message msg = {
3830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3835 bitmask[0] = nfs4_fattr_bitmap[0];
3836 bitmask[1] = nfs4_fattr_bitmap[1];
3838 * Process the label in the upcoming getfattr
3840 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3842 nfs_fattr_init(info->fattr);
3843 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3846 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3847 struct nfs_fsinfo *info)
3849 struct nfs4_exception exception = {
3850 .interruptible = true,
3854 err = _nfs4_lookup_root(server, fhandle, info);
3855 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3858 case -NFS4ERR_WRONGSEC:
3861 err = nfs4_handle_exception(server, err, &exception);
3863 } while (exception.retry);
3868 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3869 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3871 struct rpc_auth_create_args auth_args = {
3872 .pseudoflavor = flavor,
3874 struct rpc_auth *auth;
3876 auth = rpcauth_create(&auth_args, server->client);
3879 return nfs4_lookup_root(server, fhandle, info);
3883 * Retry pseudoroot lookup with various security flavors. We do this when:
3885 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3886 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3888 * Returns zero on success, or a negative NFS4ERR value, or a
3889 * negative errno value.
3891 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3892 struct nfs_fsinfo *info)
3894 /* Per 3530bis 15.33.5 */
3895 static const rpc_authflavor_t flav_array[] = {
3899 RPC_AUTH_UNIX, /* courtesy */
3902 int status = -EPERM;
3905 if (server->auth_info.flavor_len > 0) {
3906 /* try each flavor specified by user */
3907 for (i = 0; i < server->auth_info.flavor_len; i++) {
3908 status = nfs4_lookup_root_sec(server, fhandle, info,
3909 server->auth_info.flavors[i]);
3910 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3915 /* no flavors specified by user, try default list */
3916 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3917 status = nfs4_lookup_root_sec(server, fhandle, info,
3919 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3926 * -EACCES could mean that the user doesn't have correct permissions
3927 * to access the mount. It could also mean that we tried to mount
3928 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3929 * existing mount programs don't handle -EACCES very well so it should
3930 * be mapped to -EPERM instead.
3932 if (status == -EACCES)
3938 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3939 * @server: initialized nfs_server handle
3940 * @fhandle: we fill in the pseudo-fs root file handle
3941 * @info: we fill in an FSINFO struct
3942 * @auth_probe: probe the auth flavours
3944 * Returns zero on success, or a negative errno.
3946 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3947 struct nfs_fsinfo *info,
3953 status = nfs4_lookup_root(server, fhandle, info);
3955 if (auth_probe || status == NFS4ERR_WRONGSEC)
3956 status = server->nfs_client->cl_mvops->find_root_sec(server,
3960 status = nfs4_server_capabilities(server, fhandle);
3962 status = nfs4_do_fsinfo(server, fhandle, info);
3964 return nfs4_map_errors(status);
3967 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3968 struct nfs_fsinfo *info)
3971 struct nfs_fattr *fattr = info->fattr;
3972 struct nfs4_label *label = NULL;
3974 error = nfs4_server_capabilities(server, mntfh);
3976 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3980 label = nfs4_label_alloc(server, GFP_KERNEL);
3982 return PTR_ERR(label);
3984 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
3986 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3987 goto err_free_label;
3990 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3991 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3992 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3995 nfs4_label_free(label);
4001 * Get locations and (maybe) other attributes of a referral.
4002 * Note that we'll actually follow the referral later when
4003 * we detect fsid mismatch in inode revalidation
4005 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4006 const struct qstr *name, struct nfs_fattr *fattr,
4007 struct nfs_fh *fhandle)
4009 int status = -ENOMEM;
4010 struct page *page = NULL;
4011 struct nfs4_fs_locations *locations = NULL;
4013 page = alloc_page(GFP_KERNEL);
4016 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4017 if (locations == NULL)
4020 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4025 * If the fsid didn't change, this is a migration event, not a
4026 * referral. Cause us to drop into the exception handler, which
4027 * will kick off migration recovery.
4029 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4030 dprintk("%s: server did not return a different fsid for"
4031 " a referral at %s\n", __func__, name->name);
4032 status = -NFS4ERR_MOVED;
4035 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4036 nfs_fixup_referral_attributes(&locations->fattr);
4038 /* replace the lookup nfs_fattr with the locations nfs_fattr */
4039 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4040 memset(fhandle, 0, sizeof(struct nfs_fh));
4048 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4049 struct nfs_fattr *fattr, struct nfs4_label *label,
4050 struct inode *inode)
4052 __u32 bitmask[NFS4_BITMASK_SZ];
4053 struct nfs4_getattr_arg args = {
4057 struct nfs4_getattr_res res = {
4062 struct rpc_message msg = {
4063 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4068 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
4070 nfs_fattr_init(fattr);
4071 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4074 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4075 struct nfs_fattr *fattr, struct nfs4_label *label,
4076 struct inode *inode)
4078 struct nfs4_exception exception = {
4079 .interruptible = true,
4083 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4084 trace_nfs4_getattr(server, fhandle, fattr, err);
4085 err = nfs4_handle_exception(server, err,
4087 } while (exception.retry);
4092 * The file is not closed if it is opened due to the a request to change
4093 * the size of the file. The open call will not be needed once the
4094 * VFS layer lookup-intents are implemented.
4096 * Close is called when the inode is destroyed.
4097 * If we haven't opened the file for O_WRONLY, we
4098 * need to in the size_change case to obtain a stateid.
4101 * Because OPEN is always done by name in nfsv4, it is
4102 * possible that we opened a different file by the same
4103 * name. We can recognize this race condition, but we
4104 * can't do anything about it besides returning an error.
4106 * This will be fixed with VFS changes (lookup-intent).
4109 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4110 struct iattr *sattr)
4112 struct inode *inode = d_inode(dentry);
4113 const struct cred *cred = NULL;
4114 struct nfs_open_context *ctx = NULL;
4115 struct nfs4_label *label = NULL;
4118 if (pnfs_ld_layoutret_on_setattr(inode) &&
4119 sattr->ia_valid & ATTR_SIZE &&
4120 sattr->ia_size < i_size_read(inode))
4121 pnfs_commit_and_return_layout(inode);
4123 nfs_fattr_init(fattr);
4125 /* Deal with open(O_TRUNC) */
4126 if (sattr->ia_valid & ATTR_OPEN)
4127 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4129 /* Optimization: if the end result is no change, don't RPC */
4130 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4133 /* Search for an existing open(O_WRITE) file */
4134 if (sattr->ia_valid & ATTR_FILE) {
4136 ctx = nfs_file_open_context(sattr->ia_file);
4141 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4143 return PTR_ERR(label);
4145 /* Return any delegations if we're going to change ACLs */
4146 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4147 nfs4_inode_make_writeable(inode);
4149 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4151 nfs_setattr_update_inode(inode, sattr, fattr);
4152 nfs_setsecurity(inode, fattr, label);
4154 nfs4_label_free(label);
4158 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4159 const struct qstr *name, struct nfs_fh *fhandle,
4160 struct nfs_fattr *fattr, struct nfs4_label *label)
4162 struct nfs_server *server = NFS_SERVER(dir);
4164 struct nfs4_lookup_arg args = {
4165 .bitmask = server->attr_bitmask,
4166 .dir_fh = NFS_FH(dir),
4169 struct nfs4_lookup_res res = {
4175 struct rpc_message msg = {
4176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4181 args.bitmask = nfs4_bitmask(server, label);
4183 nfs_fattr_init(fattr);
4185 dprintk("NFS call lookup %s\n", name->name);
4186 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
4187 dprintk("NFS reply lookup: %d\n", status);
4191 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4193 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4194 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4195 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4199 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4200 const struct qstr *name, struct nfs_fh *fhandle,
4201 struct nfs_fattr *fattr, struct nfs4_label *label)
4203 struct nfs4_exception exception = {
4204 .interruptible = true,
4206 struct rpc_clnt *client = *clnt;
4209 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
4210 trace_nfs4_lookup(dir, name, err);
4212 case -NFS4ERR_BADNAME:
4215 case -NFS4ERR_MOVED:
4216 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4217 if (err == -NFS4ERR_MOVED)
4218 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4220 case -NFS4ERR_WRONGSEC:
4222 if (client != *clnt)
4224 client = nfs4_negotiate_security(client, dir, name);
4226 return PTR_ERR(client);
4228 exception.retry = 1;
4231 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4233 } while (exception.retry);
4238 else if (client != *clnt)
4239 rpc_shutdown_client(client);
4244 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
4245 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4246 struct nfs4_label *label)
4249 struct rpc_clnt *client = NFS_CLIENT(dir);
4251 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
4252 if (client != NFS_CLIENT(dir)) {
4253 rpc_shutdown_client(client);
4254 nfs_fixup_secinfo_attributes(fattr);
4260 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
4261 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4263 struct rpc_clnt *client = NFS_CLIENT(dir);
4266 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
4268 return ERR_PTR(status);
4269 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4272 static int _nfs4_proc_lookupp(struct inode *inode,
4273 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4274 struct nfs4_label *label)
4276 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4277 struct nfs_server *server = NFS_SERVER(inode);
4279 struct nfs4_lookupp_arg args = {
4280 .bitmask = server->attr_bitmask,
4281 .fh = NFS_FH(inode),
4283 struct nfs4_lookupp_res res = {
4289 struct rpc_message msg = {
4290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4295 args.bitmask = nfs4_bitmask(server, label);
4297 nfs_fattr_init(fattr);
4299 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4300 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4302 dprintk("NFS reply lookupp: %d\n", status);
4306 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4307 struct nfs_fattr *fattr, struct nfs4_label *label)
4309 struct nfs4_exception exception = {
4310 .interruptible = true,
4314 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4315 trace_nfs4_lookupp(inode, err);
4316 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4318 } while (exception.retry);
4322 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4324 struct nfs_server *server = NFS_SERVER(inode);
4325 struct nfs4_accessargs args = {
4326 .fh = NFS_FH(inode),
4327 .access = entry->mask,
4329 struct nfs4_accessres res = {
4332 struct rpc_message msg = {
4333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4336 .rpc_cred = entry->cred,
4340 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4341 res.fattr = nfs_alloc_fattr();
4342 if (res.fattr == NULL)
4344 args.bitmask = server->cache_consistency_bitmask;
4346 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4348 nfs_access_set_mask(entry, res.access);
4350 nfs_refresh_inode(inode, res.fattr);
4352 nfs_free_fattr(res.fattr);
4356 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4358 struct nfs4_exception exception = {
4359 .interruptible = true,
4363 err = _nfs4_proc_access(inode, entry);
4364 trace_nfs4_access(inode, err);
4365 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4367 } while (exception.retry);
4372 * TODO: For the time being, we don't try to get any attributes
4373 * along with any of the zero-copy operations READ, READDIR,
4376 * In the case of the first three, we want to put the GETATTR
4377 * after the read-type operation -- this is because it is hard
4378 * to predict the length of a GETATTR response in v4, and thus
4379 * align the READ data correctly. This means that the GETATTR
4380 * may end up partially falling into the page cache, and we should
4381 * shift it into the 'tail' of the xdr_buf before processing.
4382 * To do this efficiently, we need to know the total length
4383 * of data received, which doesn't seem to be available outside
4386 * In the case of WRITE, we also want to put the GETATTR after
4387 * the operation -- in this case because we want to make sure
4388 * we get the post-operation mtime and size.
4390 * Both of these changes to the XDR layer would in fact be quite
4391 * minor, but I decided to leave them for a subsequent patch.
4393 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4394 unsigned int pgbase, unsigned int pglen)
4396 struct nfs4_readlink args = {
4397 .fh = NFS_FH(inode),
4402 struct nfs4_readlink_res res;
4403 struct rpc_message msg = {
4404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4409 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4412 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4413 unsigned int pgbase, unsigned int pglen)
4415 struct nfs4_exception exception = {
4416 .interruptible = true,
4420 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4421 trace_nfs4_readlink(inode, err);
4422 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4424 } while (exception.retry);
4429 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4432 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4435 struct nfs_server *server = NFS_SERVER(dir);
4436 struct nfs4_label l, *ilabel = NULL;
4437 struct nfs_open_context *ctx;
4438 struct nfs4_state *state;
4441 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4443 return PTR_ERR(ctx);
4445 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4447 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4448 sattr->ia_mode &= ~current_umask();
4449 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4450 if (IS_ERR(state)) {
4451 status = PTR_ERR(state);
4455 nfs4_label_release_security(ilabel);
4456 put_nfs_open_context(ctx);
4461 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4463 struct nfs_server *server = NFS_SERVER(dir);
4464 struct nfs_removeargs args = {
4468 struct nfs_removeres res = {
4471 struct rpc_message msg = {
4472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4476 unsigned long timestamp = jiffies;
4479 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4481 spin_lock(&dir->i_lock);
4482 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
4483 /* Removing a directory decrements nlink in the parent */
4484 if (ftype == NF4DIR && dir->i_nlink > 2)
4485 nfs4_dec_nlink_locked(dir);
4486 spin_unlock(&dir->i_lock);
4491 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4493 struct nfs4_exception exception = {
4494 .interruptible = true,
4496 struct inode *inode = d_inode(dentry);
4500 if (inode->i_nlink == 1)
4501 nfs4_inode_return_delegation(inode);
4503 nfs4_inode_make_writeable(inode);
4506 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4507 trace_nfs4_remove(dir, &dentry->d_name, err);
4508 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4510 } while (exception.retry);
4514 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4516 struct nfs4_exception exception = {
4517 .interruptible = true,
4522 err = _nfs4_proc_remove(dir, name, NF4DIR);
4523 trace_nfs4_remove(dir, name, err);
4524 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4526 } while (exception.retry);
4530 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4531 struct dentry *dentry,
4532 struct inode *inode)
4534 struct nfs_removeargs *args = msg->rpc_argp;
4535 struct nfs_removeres *res = msg->rpc_resp;
4537 res->server = NFS_SB(dentry->d_sb);
4538 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4539 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4541 nfs_fattr_init(res->dir_attr);
4544 nfs4_inode_return_delegation(inode);
4547 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4549 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4550 &data->args.seq_args,
4555 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4557 struct nfs_unlinkdata *data = task->tk_calldata;
4558 struct nfs_removeres *res = &data->res;
4560 if (!nfs4_sequence_done(task, &res->seq_res))
4562 if (nfs4_async_handle_error(task, res->server, NULL,
4563 &data->timeout) == -EAGAIN)
4565 if (task->tk_status == 0)
4566 update_changeattr(dir, &res->cinfo,
4567 res->dir_attr->time_start, 0);
4571 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4572 struct dentry *old_dentry,
4573 struct dentry *new_dentry)
4575 struct nfs_renameargs *arg = msg->rpc_argp;
4576 struct nfs_renameres *res = msg->rpc_resp;
4577 struct inode *old_inode = d_inode(old_dentry);
4578 struct inode *new_inode = d_inode(new_dentry);
4581 nfs4_inode_make_writeable(old_inode);
4583 nfs4_inode_return_delegation(new_inode);
4584 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4585 res->server = NFS_SB(old_dentry->d_sb);
4586 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4589 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4591 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4592 &data->args.seq_args,
4597 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4598 struct inode *new_dir)
4600 struct nfs_renamedata *data = task->tk_calldata;
4601 struct nfs_renameres *res = &data->res;
4603 if (!nfs4_sequence_done(task, &res->seq_res))
4605 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4608 if (task->tk_status == 0) {
4609 if (new_dir != old_dir) {
4610 /* Note: If we moved a directory, nlink will change */
4611 update_changeattr(old_dir, &res->old_cinfo,
4612 res->old_fattr->time_start,
4613 NFS_INO_INVALID_OTHER);
4614 update_changeattr(new_dir, &res->new_cinfo,
4615 res->new_fattr->time_start,
4616 NFS_INO_INVALID_OTHER);
4618 update_changeattr(old_dir, &res->old_cinfo,
4619 res->old_fattr->time_start,
4625 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4627 struct nfs_server *server = NFS_SERVER(inode);
4628 __u32 bitmask[NFS4_BITMASK_SZ];
4629 struct nfs4_link_arg arg = {
4630 .fh = NFS_FH(inode),
4631 .dir_fh = NFS_FH(dir),
4635 struct nfs4_link_res res = {
4639 struct rpc_message msg = {
4640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4644 int status = -ENOMEM;
4646 res.fattr = nfs_alloc_fattr();
4647 if (res.fattr == NULL)
4650 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4651 if (IS_ERR(res.label)) {
4652 status = PTR_ERR(res.label);
4656 nfs4_inode_make_writeable(inode);
4657 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4659 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4661 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
4662 status = nfs_post_op_update_inode(inode, res.fattr);
4664 nfs_setsecurity(inode, res.fattr, res.label);
4668 nfs4_label_free(res.label);
4671 nfs_free_fattr(res.fattr);
4675 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4677 struct nfs4_exception exception = {
4678 .interruptible = true,
4682 err = nfs4_handle_exception(NFS_SERVER(inode),
4683 _nfs4_proc_link(inode, dir, name),
4685 } while (exception.retry);
4689 struct nfs4_createdata {
4690 struct rpc_message msg;
4691 struct nfs4_create_arg arg;
4692 struct nfs4_create_res res;
4694 struct nfs_fattr fattr;
4695 struct nfs4_label *label;
4698 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4699 const struct qstr *name, struct iattr *sattr, u32 ftype)
4701 struct nfs4_createdata *data;
4703 data = kzalloc(sizeof(*data), GFP_KERNEL);
4705 struct nfs_server *server = NFS_SERVER(dir);
4707 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4708 if (IS_ERR(data->label))
4711 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4712 data->msg.rpc_argp = &data->arg;
4713 data->msg.rpc_resp = &data->res;
4714 data->arg.dir_fh = NFS_FH(dir);
4715 data->arg.server = server;
4716 data->arg.name = name;
4717 data->arg.attrs = sattr;
4718 data->arg.ftype = ftype;
4719 data->arg.bitmask = nfs4_bitmask(server, data->label);
4720 data->arg.umask = current_umask();
4721 data->res.server = server;
4722 data->res.fh = &data->fh;
4723 data->res.fattr = &data->fattr;
4724 data->res.label = data->label;
4725 nfs_fattr_init(data->res.fattr);
4733 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4735 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4736 &data->arg.seq_args, &data->res.seq_res, 1);
4738 spin_lock(&dir->i_lock);
4739 update_changeattr_locked(dir, &data->res.dir_cinfo,
4740 data->res.fattr->time_start, 0);
4741 /* Creating a directory bumps nlink in the parent */
4742 if (data->arg.ftype == NF4DIR)
4743 nfs4_inc_nlink_locked(dir);
4744 spin_unlock(&dir->i_lock);
4745 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4750 static void nfs4_free_createdata(struct nfs4_createdata *data)
4752 nfs4_label_free(data->label);
4756 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4757 struct page *page, unsigned int len, struct iattr *sattr,
4758 struct nfs4_label *label)
4760 struct nfs4_createdata *data;
4761 int status = -ENAMETOOLONG;
4763 if (len > NFS4_MAXPATHLEN)
4767 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4771 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4772 data->arg.u.symlink.pages = &page;
4773 data->arg.u.symlink.len = len;
4774 data->arg.label = label;
4776 status = nfs4_do_create(dir, dentry, data);
4778 nfs4_free_createdata(data);
4783 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4784 struct page *page, unsigned int len, struct iattr *sattr)
4786 struct nfs4_exception exception = {
4787 .interruptible = true,
4789 struct nfs4_label l, *label = NULL;
4792 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4795 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4796 trace_nfs4_symlink(dir, &dentry->d_name, err);
4797 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4799 } while (exception.retry);
4801 nfs4_label_release_security(label);
4805 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4806 struct iattr *sattr, struct nfs4_label *label)
4808 struct nfs4_createdata *data;
4809 int status = -ENOMEM;
4811 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4815 data->arg.label = label;
4816 status = nfs4_do_create(dir, dentry, data);
4818 nfs4_free_createdata(data);
4823 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4824 struct iattr *sattr)
4826 struct nfs_server *server = NFS_SERVER(dir);
4827 struct nfs4_exception exception = {
4828 .interruptible = true,
4830 struct nfs4_label l, *label = NULL;
4833 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4835 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4836 sattr->ia_mode &= ~current_umask();
4838 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4839 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4840 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4842 } while (exception.retry);
4843 nfs4_label_release_security(label);
4848 static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4849 u64 cookie, struct page **pages, unsigned int count, bool plus)
4851 struct inode *dir = d_inode(dentry);
4852 struct nfs4_readdir_arg args = {
4857 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4860 struct nfs4_readdir_res res;
4861 struct rpc_message msg = {
4862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4869 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4871 (unsigned long long)cookie);
4872 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4873 res.pgbase = args.pgbase;
4874 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4876 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4877 status += args.pgbase;
4880 nfs_invalidate_atime(dir);
4882 dprintk("%s: returns %d\n", __func__, status);
4886 static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4887 u64 cookie, struct page **pages, unsigned int count, bool plus)
4889 struct nfs4_exception exception = {
4890 .interruptible = true,
4894 err = _nfs4_proc_readdir(dentry, cred, cookie,
4895 pages, count, plus);
4896 trace_nfs4_readdir(d_inode(dentry), err);
4897 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4899 } while (exception.retry);
4903 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4904 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4906 struct nfs4_createdata *data;
4907 int mode = sattr->ia_mode;
4908 int status = -ENOMEM;
4910 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4915 data->arg.ftype = NF4FIFO;
4916 else if (S_ISBLK(mode)) {
4917 data->arg.ftype = NF4BLK;
4918 data->arg.u.device.specdata1 = MAJOR(rdev);
4919 data->arg.u.device.specdata2 = MINOR(rdev);
4921 else if (S_ISCHR(mode)) {
4922 data->arg.ftype = NF4CHR;
4923 data->arg.u.device.specdata1 = MAJOR(rdev);
4924 data->arg.u.device.specdata2 = MINOR(rdev);
4925 } else if (!S_ISSOCK(mode)) {
4930 data->arg.label = label;
4931 status = nfs4_do_create(dir, dentry, data);
4933 nfs4_free_createdata(data);
4938 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4939 struct iattr *sattr, dev_t rdev)
4941 struct nfs_server *server = NFS_SERVER(dir);
4942 struct nfs4_exception exception = {
4943 .interruptible = true,
4945 struct nfs4_label l, *label = NULL;
4948 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4950 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4951 sattr->ia_mode &= ~current_umask();
4953 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4954 trace_nfs4_mknod(dir, &dentry->d_name, err);
4955 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4957 } while (exception.retry);
4959 nfs4_label_release_security(label);
4964 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4965 struct nfs_fsstat *fsstat)
4967 struct nfs4_statfs_arg args = {
4969 .bitmask = server->attr_bitmask,
4971 struct nfs4_statfs_res res = {
4974 struct rpc_message msg = {
4975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4980 nfs_fattr_init(fsstat->fattr);
4981 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4984 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4986 struct nfs4_exception exception = {
4987 .interruptible = true,
4991 err = nfs4_handle_exception(server,
4992 _nfs4_proc_statfs(server, fhandle, fsstat),
4994 } while (exception.retry);
4998 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4999 struct nfs_fsinfo *fsinfo)
5001 struct nfs4_fsinfo_arg args = {
5003 .bitmask = server->attr_bitmask,
5005 struct nfs4_fsinfo_res res = {
5008 struct rpc_message msg = {
5009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5014 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5017 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5019 struct nfs4_exception exception = {
5020 .interruptible = true,
5022 unsigned long now = jiffies;
5026 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5027 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5029 nfs4_set_lease_period(server->nfs_client,
5030 fsinfo->lease_time * HZ,
5034 err = nfs4_handle_exception(server, err, &exception);
5035 } while (exception.retry);
5039 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5043 nfs_fattr_init(fsinfo->fattr);
5044 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5046 /* block layout checks this! */
5047 server->pnfs_blksize = fsinfo->blksize;
5048 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5054 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5055 struct nfs_pathconf *pathconf)
5057 struct nfs4_pathconf_arg args = {
5059 .bitmask = server->attr_bitmask,
5061 struct nfs4_pathconf_res res = {
5062 .pathconf = pathconf,
5064 struct rpc_message msg = {
5065 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5070 /* None of the pathconf attributes are mandatory to implement */
5071 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5072 memset(pathconf, 0, sizeof(*pathconf));
5076 nfs_fattr_init(pathconf->fattr);
5077 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5080 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5081 struct nfs_pathconf *pathconf)
5083 struct nfs4_exception exception = {
5084 .interruptible = true,
5089 err = nfs4_handle_exception(server,
5090 _nfs4_proc_pathconf(server, fhandle, pathconf),
5092 } while (exception.retry);
5096 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5097 const struct nfs_open_context *ctx,
5098 const struct nfs_lock_context *l_ctx,
5101 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5103 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5105 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5106 const struct nfs_open_context *ctx,
5107 const struct nfs_lock_context *l_ctx,
5110 nfs4_stateid _current_stateid;
5112 /* If the current stateid represents a lost lock, then exit */
5113 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5115 return nfs4_stateid_match(stateid, &_current_stateid);
5118 static bool nfs4_error_stateid_expired(int err)
5121 case -NFS4ERR_DELEG_REVOKED:
5122 case -NFS4ERR_ADMIN_REVOKED:
5123 case -NFS4ERR_BAD_STATEID:
5124 case -NFS4ERR_STALE_STATEID:
5125 case -NFS4ERR_OLD_STATEID:
5126 case -NFS4ERR_OPENMODE:
5127 case -NFS4ERR_EXPIRED:
5133 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5135 struct nfs_server *server = NFS_SERVER(hdr->inode);
5137 trace_nfs4_read(hdr, task->tk_status);
5138 if (task->tk_status < 0) {
5139 struct nfs4_exception exception = {
5140 .inode = hdr->inode,
5141 .state = hdr->args.context->state,
5142 .stateid = &hdr->args.stateid,
5144 task->tk_status = nfs4_async_handle_exception(task,
5145 server, task->tk_status, &exception);
5146 if (exception.retry) {
5147 rpc_restart_call_prepare(task);
5152 if (task->tk_status > 0)
5153 renew_lease(server, hdr->timestamp);
5157 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5158 struct nfs_pgio_args *args)
5161 if (!nfs4_error_stateid_expired(task->tk_status) ||
5162 nfs4_stateid_is_current(&args->stateid,
5167 rpc_restart_call_prepare(task);
5171 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5174 dprintk("--> %s\n", __func__);
5176 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5178 if (nfs4_read_stateid_changed(task, &hdr->args))
5180 if (task->tk_status > 0)
5181 nfs_invalidate_atime(hdr->inode);
5182 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5183 nfs4_read_done_cb(task, hdr);
5186 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5187 struct rpc_message *msg)
5189 hdr->timestamp = jiffies;
5190 if (!hdr->pgio_done_cb)
5191 hdr->pgio_done_cb = nfs4_read_done_cb;
5192 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5193 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5196 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5197 struct nfs_pgio_header *hdr)
5199 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5200 &hdr->args.seq_args,
5204 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5205 hdr->args.lock_context,
5206 hdr->rw_mode) == -EIO)
5208 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5213 static int nfs4_write_done_cb(struct rpc_task *task,
5214 struct nfs_pgio_header *hdr)
5216 struct inode *inode = hdr->inode;
5218 trace_nfs4_write(hdr, task->tk_status);
5219 if (task->tk_status < 0) {
5220 struct nfs4_exception exception = {
5221 .inode = hdr->inode,
5222 .state = hdr->args.context->state,
5223 .stateid = &hdr->args.stateid,
5225 task->tk_status = nfs4_async_handle_exception(task,
5226 NFS_SERVER(inode), task->tk_status,
5228 if (exception.retry) {
5229 rpc_restart_call_prepare(task);
5233 if (task->tk_status >= 0) {
5234 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5235 nfs_writeback_update_inode(hdr);
5240 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5241 struct nfs_pgio_args *args)
5244 if (!nfs4_error_stateid_expired(task->tk_status) ||
5245 nfs4_stateid_is_current(&args->stateid,
5250 rpc_restart_call_prepare(task);
5254 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5256 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5258 if (nfs4_write_stateid_changed(task, &hdr->args))
5260 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5261 nfs4_write_done_cb(task, hdr);
5265 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5267 /* Don't request attributes for pNFS or O_DIRECT writes */
5268 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5270 /* Otherwise, request attributes if and only if we don't hold
5273 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5276 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5277 struct rpc_message *msg,
5278 struct rpc_clnt **clnt)
5280 struct nfs_server *server = NFS_SERVER(hdr->inode);
5282 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5283 hdr->args.bitmask = NULL;
5284 hdr->res.fattr = NULL;
5286 hdr->args.bitmask = server->cache_consistency_bitmask;
5288 if (!hdr->pgio_done_cb)
5289 hdr->pgio_done_cb = nfs4_write_done_cb;
5290 hdr->res.server = server;
5291 hdr->timestamp = jiffies;
5293 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5294 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
5295 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5298 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5300 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5301 &data->args.seq_args,
5306 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5308 struct inode *inode = data->inode;
5310 trace_nfs4_commit(data, task->tk_status);
5311 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5312 NULL, NULL) == -EAGAIN) {
5313 rpc_restart_call_prepare(task);
5319 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5321 if (!nfs4_sequence_done(task, &data->res.seq_res))
5323 return data->commit_done_cb(task, data);
5326 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5327 struct rpc_clnt **clnt)
5329 struct nfs_server *server = NFS_SERVER(data->inode);
5331 if (data->commit_done_cb == NULL)
5332 data->commit_done_cb = nfs4_commit_done_cb;
5333 data->res.server = server;
5334 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5335 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5336 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5339 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5340 struct nfs_commitres *res)
5342 struct inode *dst_inode = file_inode(dst);
5343 struct nfs_server *server = NFS_SERVER(dst_inode);
5344 struct rpc_message msg = {
5345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5350 args->fh = NFS_FH(dst_inode);
5351 return nfs4_call_sync(server->client, server, &msg,
5352 &args->seq_args, &res->seq_res, 1);
5355 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5357 struct nfs_commitargs args = {
5361 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5362 struct nfs4_exception exception = { };
5366 status = _nfs4_proc_commit(dst, &args, res);
5367 status = nfs4_handle_exception(dst_server, status, &exception);
5368 } while (exception.retry);
5373 struct nfs4_renewdata {
5374 struct nfs_client *client;
5375 unsigned long timestamp;
5379 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5380 * standalone procedure for queueing an asynchronous RENEW.
5382 static void nfs4_renew_release(void *calldata)
5384 struct nfs4_renewdata *data = calldata;
5385 struct nfs_client *clp = data->client;
5387 if (refcount_read(&clp->cl_count) > 1)
5388 nfs4_schedule_state_renewal(clp);
5389 nfs_put_client(clp);
5393 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5395 struct nfs4_renewdata *data = calldata;
5396 struct nfs_client *clp = data->client;
5397 unsigned long timestamp = data->timestamp;
5399 trace_nfs4_renew_async(clp, task->tk_status);
5400 switch (task->tk_status) {
5403 case -NFS4ERR_LEASE_MOVED:
5404 nfs4_schedule_lease_moved_recovery(clp);
5407 /* Unless we're shutting down, schedule state recovery! */
5408 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5410 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5411 nfs4_schedule_lease_recovery(clp);
5414 nfs4_schedule_path_down_recovery(clp);
5416 do_renew_lease(clp, timestamp);
5419 static const struct rpc_call_ops nfs4_renew_ops = {
5420 .rpc_call_done = nfs4_renew_done,
5421 .rpc_release = nfs4_renew_release,
5424 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5426 struct rpc_message msg = {
5427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5431 struct nfs4_renewdata *data;
5433 if (renew_flags == 0)
5435 if (!refcount_inc_not_zero(&clp->cl_count))
5437 data = kmalloc(sizeof(*data), GFP_NOFS);
5439 nfs_put_client(clp);
5443 data->timestamp = jiffies;
5444 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5445 &nfs4_renew_ops, data);
5448 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5450 struct rpc_message msg = {
5451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5455 unsigned long now = jiffies;
5458 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5461 do_renew_lease(clp, now);
5465 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5467 return server->caps & NFS_CAP_ACLS;
5470 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5471 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5474 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5476 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5477 struct page **pages)
5479 struct page *newpage, **spages;
5485 len = min_t(size_t, PAGE_SIZE, buflen);
5486 newpage = alloc_page(GFP_KERNEL);
5488 if (newpage == NULL)
5490 memcpy(page_address(newpage), buf, len);
5495 } while (buflen != 0);
5501 __free_page(spages[rc-1]);
5505 struct nfs4_cached_acl {
5511 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5513 struct nfs_inode *nfsi = NFS_I(inode);
5515 spin_lock(&inode->i_lock);
5516 kfree(nfsi->nfs4_acl);
5517 nfsi->nfs4_acl = acl;
5518 spin_unlock(&inode->i_lock);
5521 static void nfs4_zap_acl_attr(struct inode *inode)
5523 nfs4_set_cached_acl(inode, NULL);
5526 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5528 struct nfs_inode *nfsi = NFS_I(inode);
5529 struct nfs4_cached_acl *acl;
5532 spin_lock(&inode->i_lock);
5533 acl = nfsi->nfs4_acl;
5536 if (buf == NULL) /* user is just asking for length */
5538 if (acl->cached == 0)
5540 ret = -ERANGE; /* see getxattr(2) man page */
5541 if (acl->len > buflen)
5543 memcpy(buf, acl->data, acl->len);
5547 spin_unlock(&inode->i_lock);
5551 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5553 struct nfs4_cached_acl *acl;
5554 size_t buflen = sizeof(*acl) + acl_len;
5556 if (buflen <= PAGE_SIZE) {
5557 acl = kmalloc(buflen, GFP_KERNEL);
5561 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5563 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5570 nfs4_set_cached_acl(inode, acl);
5574 * The getxattr API returns the required buffer length when called with a
5575 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5576 * the required buf. On a NULL buf, we send a page of data to the server
5577 * guessing that the ACL request can be serviced by a page. If so, we cache
5578 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5579 * the cache. If not so, we throw away the page, and cache the required
5580 * length. The next getxattr call will then produce another round trip to
5581 * the server, this time with the input buf of the required size.
5583 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5585 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5586 struct nfs_getaclargs args = {
5587 .fh = NFS_FH(inode),
5591 struct nfs_getaclres res = {
5594 struct rpc_message msg = {
5595 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5599 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5600 int ret = -ENOMEM, i;
5602 if (npages > ARRAY_SIZE(pages))
5605 for (i = 0; i < npages; i++) {
5606 pages[i] = alloc_page(GFP_KERNEL);
5611 /* for decoding across pages */
5612 res.acl_scratch = alloc_page(GFP_KERNEL);
5613 if (!res.acl_scratch)
5616 args.acl_len = npages * PAGE_SIZE;
5618 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5619 __func__, buf, buflen, npages, args.acl_len);
5620 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5621 &msg, &args.seq_args, &res.seq_res, 0);
5625 /* Handle the case where the passed-in buffer is too short */
5626 if (res.acl_flags & NFS4_ACL_TRUNC) {
5627 /* Did the user only issue a request for the acl length? */
5633 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5635 if (res.acl_len > buflen) {
5639 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5644 for (i = 0; i < npages; i++)
5646 __free_page(pages[i]);
5647 if (res.acl_scratch)
5648 __free_page(res.acl_scratch);
5652 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5654 struct nfs4_exception exception = {
5655 .interruptible = true,
5659 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5660 trace_nfs4_get_acl(inode, ret);
5663 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5664 } while (exception.retry);
5668 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5670 struct nfs_server *server = NFS_SERVER(inode);
5673 if (!nfs4_server_supports_acls(server))
5675 ret = nfs_revalidate_inode(server, inode);
5678 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5679 nfs_zap_acl_cache(inode);
5680 ret = nfs4_read_cached_acl(inode, buf, buflen);
5682 /* -ENOENT is returned if there is no ACL or if there is an ACL
5683 * but no cached acl data, just the acl length */
5685 return nfs4_get_acl_uncached(inode, buf, buflen);
5688 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5690 struct nfs_server *server = NFS_SERVER(inode);
5691 struct page *pages[NFS4ACL_MAXPAGES];
5692 struct nfs_setaclargs arg = {
5693 .fh = NFS_FH(inode),
5697 struct nfs_setaclres res;
5698 struct rpc_message msg = {
5699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5703 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5706 if (!nfs4_server_supports_acls(server))
5708 if (npages > ARRAY_SIZE(pages))
5710 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5713 nfs4_inode_make_writeable(inode);
5714 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5717 * Free each page after tx, so the only ref left is
5718 * held by the network stack
5721 put_page(pages[i-1]);
5724 * Acl update can result in inode attribute update.
5725 * so mark the attribute cache invalid.
5727 spin_lock(&inode->i_lock);
5728 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5729 | NFS_INO_INVALID_CTIME
5730 | NFS_INO_REVAL_FORCED;
5731 spin_unlock(&inode->i_lock);
5732 nfs_access_zap_cache(inode);
5733 nfs_zap_acl_cache(inode);
5737 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5739 struct nfs4_exception exception = { };
5742 err = __nfs4_proc_set_acl(inode, buf, buflen);
5743 trace_nfs4_set_acl(inode, err);
5744 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5746 } while (exception.retry);
5750 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5751 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5754 struct nfs_server *server = NFS_SERVER(inode);
5755 struct nfs_fattr fattr;
5756 struct nfs4_label label = {0, 0, buflen, buf};
5758 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5759 struct nfs4_getattr_arg arg = {
5760 .fh = NFS_FH(inode),
5763 struct nfs4_getattr_res res = {
5768 struct rpc_message msg = {
5769 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5775 nfs_fattr_init(&fattr);
5777 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5780 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5782 if (buflen < label.len)
5787 static int nfs4_get_security_label(struct inode *inode, void *buf,
5790 struct nfs4_exception exception = {
5791 .interruptible = true,
5795 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5799 err = _nfs4_get_security_label(inode, buf, buflen);
5800 trace_nfs4_get_security_label(inode, err);
5801 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5803 } while (exception.retry);
5807 static int _nfs4_do_set_security_label(struct inode *inode,
5808 struct nfs4_label *ilabel,
5809 struct nfs_fattr *fattr,
5810 struct nfs4_label *olabel)
5813 struct iattr sattr = {0};
5814 struct nfs_server *server = NFS_SERVER(inode);
5815 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5816 struct nfs_setattrargs arg = {
5817 .fh = NFS_FH(inode),
5823 struct nfs_setattrres res = {
5828 struct rpc_message msg = {
5829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5835 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5837 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5839 dprintk("%s failed: %d\n", __func__, status);
5844 static int nfs4_do_set_security_label(struct inode *inode,
5845 struct nfs4_label *ilabel,
5846 struct nfs_fattr *fattr,
5847 struct nfs4_label *olabel)
5849 struct nfs4_exception exception = { };
5853 err = _nfs4_do_set_security_label(inode, ilabel,
5855 trace_nfs4_set_security_label(inode, err);
5856 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5858 } while (exception.retry);
5863 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5865 struct nfs4_label ilabel, *olabel = NULL;
5866 struct nfs_fattr fattr;
5869 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5872 nfs_fattr_init(&fattr);
5876 ilabel.label = (char *)buf;
5877 ilabel.len = buflen;
5879 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5880 if (IS_ERR(olabel)) {
5881 status = -PTR_ERR(olabel);
5885 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5887 nfs_setsecurity(inode, &fattr, olabel);
5889 nfs4_label_free(olabel);
5893 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5896 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5897 nfs4_verifier *bootverf)
5901 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5902 /* An impossible timestamp guarantees this value
5903 * will never match a generated boot time. */
5904 verf[0] = cpu_to_be32(U32_MAX);
5905 verf[1] = cpu_to_be32(U32_MAX);
5907 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5908 u64 ns = ktime_to_ns(nn->boot_time);
5910 verf[0] = cpu_to_be32(ns >> 32);
5911 verf[1] = cpu_to_be32(ns);
5913 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5917 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5922 if (clp->cl_owner_id != NULL)
5927 strlen(clp->cl_rpcclient->cl_nodename) +
5929 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5932 if (nfs4_client_id_uniquifier[0] != '\0')
5933 len += strlen(nfs4_client_id_uniquifier) + 1;
5934 if (len > NFS4_OPAQUE_LIMIT + 1)
5938 * Since this string is allocated at mount time, and held until the
5939 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5940 * about a memory-reclaim deadlock.
5942 str = kmalloc(len, GFP_KERNEL);
5947 if (nfs4_client_id_uniquifier[0] != '\0')
5948 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
5949 clp->cl_rpcclient->cl_nodename,
5950 nfs4_client_id_uniquifier,
5951 rpc_peeraddr2str(clp->cl_rpcclient,
5954 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
5955 clp->cl_rpcclient->cl_nodename,
5956 rpc_peeraddr2str(clp->cl_rpcclient,
5960 clp->cl_owner_id = str;
5965 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5970 len = 10 + 10 + 1 + 10 + 1 +
5971 strlen(nfs4_client_id_uniquifier) + 1 +
5972 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5974 if (len > NFS4_OPAQUE_LIMIT + 1)
5978 * Since this string is allocated at mount time, and held until the
5979 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5980 * about a memory-reclaim deadlock.
5982 str = kmalloc(len, GFP_KERNEL);
5986 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5987 clp->rpc_ops->version, clp->cl_minorversion,
5988 nfs4_client_id_uniquifier,
5989 clp->cl_rpcclient->cl_nodename);
5990 clp->cl_owner_id = str;
5995 nfs4_init_uniform_client_string(struct nfs_client *clp)
6000 if (clp->cl_owner_id != NULL)
6003 if (nfs4_client_id_uniquifier[0] != '\0')
6004 return nfs4_init_uniquifier_client_string(clp);
6006 len = 10 + 10 + 1 + 10 + 1 +
6007 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6009 if (len > NFS4_OPAQUE_LIMIT + 1)
6013 * Since this string is allocated at mount time, and held until the
6014 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6015 * about a memory-reclaim deadlock.
6017 str = kmalloc(len, GFP_KERNEL);
6021 scnprintf(str, len, "Linux NFSv%u.%u %s",
6022 clp->rpc_ops->version, clp->cl_minorversion,
6023 clp->cl_rpcclient->cl_nodename);
6024 clp->cl_owner_id = str;
6029 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6030 * services. Advertise one based on the address family of the
6034 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6036 if (strchr(clp->cl_ipaddr, ':') != NULL)
6037 return scnprintf(buf, len, "tcp6");
6039 return scnprintf(buf, len, "tcp");
6042 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6044 struct nfs4_setclientid *sc = calldata;
6046 if (task->tk_status == 0)
6047 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6050 static const struct rpc_call_ops nfs4_setclientid_ops = {
6051 .rpc_call_done = nfs4_setclientid_done,
6055 * nfs4_proc_setclientid - Negotiate client ID
6056 * @clp: state data structure
6057 * @program: RPC program for NFSv4 callback service
6058 * @port: IP port number for NFS4 callback service
6059 * @cred: credential to use for this call
6060 * @res: where to place the result
6062 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6064 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6065 unsigned short port, const struct cred *cred,
6066 struct nfs4_setclientid_res *res)
6068 nfs4_verifier sc_verifier;
6069 struct nfs4_setclientid setclientid = {
6070 .sc_verifier = &sc_verifier,
6074 struct rpc_message msg = {
6075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6076 .rpc_argp = &setclientid,
6080 struct rpc_task_setup task_setup_data = {
6081 .rpc_client = clp->cl_rpcclient,
6082 .rpc_message = &msg,
6083 .callback_ops = &nfs4_setclientid_ops,
6084 .callback_data = &setclientid,
6085 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6089 /* nfs_client_id4 */
6090 nfs4_init_boot_verifier(clp, &sc_verifier);
6092 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6093 status = nfs4_init_uniform_client_string(clp);
6095 status = nfs4_init_nonuniform_client_string(clp);
6101 setclientid.sc_netid_len =
6102 nfs4_init_callback_netid(clp,
6103 setclientid.sc_netid,
6104 sizeof(setclientid.sc_netid));
6105 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6106 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6107 clp->cl_ipaddr, port >> 8, port & 255);
6109 dprintk("NFS call setclientid auth=%s, '%s'\n",
6110 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6113 status = nfs4_call_sync_custom(&task_setup_data);
6114 if (setclientid.sc_cred) {
6115 kfree(clp->cl_acceptor);
6116 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6117 put_rpccred(setclientid.sc_cred);
6120 trace_nfs4_setclientid(clp, status);
6121 dprintk("NFS reply setclientid: %d\n", status);
6126 * nfs4_proc_setclientid_confirm - Confirm client ID
6127 * @clp: state data structure
6128 * @arg: result of a previous SETCLIENTID
6129 * @cred: credential to use for this call
6131 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6133 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6134 struct nfs4_setclientid_res *arg,
6135 const struct cred *cred)
6137 struct rpc_message msg = {
6138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6144 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6145 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6147 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6148 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6149 trace_nfs4_setclientid_confirm(clp, status);
6150 dprintk("NFS reply setclientid_confirm: %d\n", status);
6154 struct nfs4_delegreturndata {
6155 struct nfs4_delegreturnargs args;
6156 struct nfs4_delegreturnres res;
6158 nfs4_stateid stateid;
6159 unsigned long timestamp;
6161 struct nfs4_layoutreturn_args arg;
6162 struct nfs4_layoutreturn_res res;
6163 struct nfs4_xdr_opaque_data ld_private;
6167 struct nfs_fattr fattr;
6169 struct inode *inode;
6172 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6174 struct nfs4_delegreturndata *data = calldata;
6175 struct nfs4_exception exception = {
6176 .inode = data->inode,
6177 .stateid = &data->stateid,
6180 if (!nfs4_sequence_done(task, &data->res.seq_res))
6183 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6185 /* Handle Layoutreturn errors */
6186 if (pnfs_roc_done(task, data->inode,
6187 &data->args.lr_args,
6189 &data->res.lr_ret) == -EAGAIN)
6192 switch (task->tk_status) {
6194 renew_lease(data->res.server, data->timestamp);
6196 case -NFS4ERR_ADMIN_REVOKED:
6197 case -NFS4ERR_DELEG_REVOKED:
6198 case -NFS4ERR_EXPIRED:
6199 nfs4_free_revoked_stateid(data->res.server,
6201 task->tk_msg.rpc_cred);
6203 case -NFS4ERR_BAD_STATEID:
6204 case -NFS4ERR_STALE_STATEID:
6205 task->tk_status = 0;
6207 case -NFS4ERR_OLD_STATEID:
6208 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6209 nfs4_stateid_seqid_inc(&data->stateid);
6210 if (data->args.bitmask) {
6211 data->args.bitmask = NULL;
6212 data->res.fattr = NULL;
6215 case -NFS4ERR_ACCESS:
6216 if (data->args.bitmask) {
6217 data->args.bitmask = NULL;
6218 data->res.fattr = NULL;
6223 task->tk_status = nfs4_async_handle_exception(task,
6224 data->res.server, task->tk_status,
6226 if (exception.retry)
6229 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6230 data->rpc_status = task->tk_status;
6233 task->tk_status = 0;
6234 rpc_restart_call_prepare(task);
6237 static void nfs4_delegreturn_release(void *calldata)
6239 struct nfs4_delegreturndata *data = calldata;
6240 struct inode *inode = data->inode;
6244 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6246 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6247 nfs_iput_and_deactive(inode);
6252 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6254 struct nfs4_delegreturndata *d_data;
6255 struct pnfs_layout_hdr *lo;
6257 d_data = (struct nfs4_delegreturndata *)data;
6259 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6260 nfs4_sequence_done(task, &d_data->res.seq_res);
6264 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6265 if (lo && !pnfs_layout_is_valid(lo)) {
6266 d_data->args.lr_args = NULL;
6267 d_data->res.lr_res = NULL;
6270 nfs4_setup_sequence(d_data->res.server->nfs_client,
6271 &d_data->args.seq_args,
6272 &d_data->res.seq_res,
6276 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6277 .rpc_call_prepare = nfs4_delegreturn_prepare,
6278 .rpc_call_done = nfs4_delegreturn_done,
6279 .rpc_release = nfs4_delegreturn_release,
6282 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6284 struct nfs4_delegreturndata *data;
6285 struct nfs_server *server = NFS_SERVER(inode);
6286 struct rpc_task *task;
6287 struct rpc_message msg = {
6288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6291 struct rpc_task_setup task_setup_data = {
6292 .rpc_client = server->client,
6293 .rpc_message = &msg,
6294 .callback_ops = &nfs4_delegreturn_ops,
6295 .flags = RPC_TASK_ASYNC,
6299 data = kzalloc(sizeof(*data), GFP_NOFS);
6302 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6304 nfs4_state_protect(server->nfs_client,
6305 NFS_SP4_MACH_CRED_CLEANUP,
6306 &task_setup_data.rpc_client, &msg);
6308 data->args.fhandle = &data->fh;
6309 data->args.stateid = &data->stateid;
6310 data->args.bitmask = server->cache_consistency_bitmask;
6311 nfs_copy_fh(&data->fh, NFS_FH(inode));
6312 nfs4_stateid_copy(&data->stateid, stateid);
6313 data->res.fattr = &data->fattr;
6314 data->res.server = server;
6315 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6316 data->lr.arg.ld_private = &data->lr.ld_private;
6317 nfs_fattr_init(data->res.fattr);
6318 data->timestamp = jiffies;
6319 data->rpc_status = 0;
6320 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
6321 data->inode = nfs_igrab_and_active(inode);
6324 data->args.lr_args = &data->lr.arg;
6325 data->res.lr_res = &data->lr.res;
6327 } else if (data->lr.roc) {
6328 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6329 data->lr.roc = false;
6332 task_setup_data.callback_data = data;
6333 msg.rpc_argp = &data->args;
6334 msg.rpc_resp = &data->res;
6335 task = rpc_run_task(&task_setup_data);
6337 return PTR_ERR(task);
6340 status = rpc_wait_for_completion_task(task);
6343 status = data->rpc_status;
6349 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6351 struct nfs_server *server = NFS_SERVER(inode);
6352 struct nfs4_exception exception = { };
6355 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6356 trace_nfs4_delegreturn(inode, stateid, err);
6358 case -NFS4ERR_STALE_STATEID:
6359 case -NFS4ERR_EXPIRED:
6363 err = nfs4_handle_exception(server, err, &exception);
6364 } while (exception.retry);
6368 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6370 struct inode *inode = state->inode;
6371 struct nfs_server *server = NFS_SERVER(inode);
6372 struct nfs_client *clp = server->nfs_client;
6373 struct nfs_lockt_args arg = {
6374 .fh = NFS_FH(inode),
6377 struct nfs_lockt_res res = {
6380 struct rpc_message msg = {
6381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6384 .rpc_cred = state->owner->so_cred,
6386 struct nfs4_lock_state *lsp;
6389 arg.lock_owner.clientid = clp->cl_clientid;
6390 status = nfs4_set_lock_state(state, request);
6393 lsp = request->fl_u.nfs4_fl.owner;
6394 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6395 arg.lock_owner.s_dev = server->s_dev;
6396 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6399 request->fl_type = F_UNLCK;
6401 case -NFS4ERR_DENIED:
6404 request->fl_ops->fl_release_private(request);
6405 request->fl_ops = NULL;
6410 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6412 struct nfs4_exception exception = {
6413 .interruptible = true,
6418 err = _nfs4_proc_getlk(state, cmd, request);
6419 trace_nfs4_get_lock(request, state, cmd, err);
6420 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6422 } while (exception.retry);
6427 * Update the seqid of a lock stateid after receiving
6428 * NFS4ERR_OLD_STATEID
6430 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6431 struct nfs4_lock_state *lsp)
6433 struct nfs4_state *state = lsp->ls_state;
6436 spin_lock(&state->state_lock);
6437 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6439 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6440 nfs4_stateid_seqid_inc(dst);
6442 dst->seqid = lsp->ls_stateid.seqid;
6445 spin_unlock(&state->state_lock);
6449 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6450 struct nfs4_lock_state *lsp)
6452 struct nfs4_state *state = lsp->ls_state;
6455 spin_lock(&state->state_lock);
6456 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6457 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6458 spin_unlock(&state->state_lock);
6462 struct nfs4_unlockdata {
6463 struct nfs_locku_args arg;
6464 struct nfs_locku_res res;
6465 struct nfs4_lock_state *lsp;
6466 struct nfs_open_context *ctx;
6467 struct nfs_lock_context *l_ctx;
6468 struct file_lock fl;
6469 struct nfs_server *server;
6470 unsigned long timestamp;
6473 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6474 struct nfs_open_context *ctx,
6475 struct nfs4_lock_state *lsp,
6476 struct nfs_seqid *seqid)
6478 struct nfs4_unlockdata *p;
6479 struct nfs4_state *state = lsp->ls_state;
6480 struct inode *inode = state->inode;
6482 p = kzalloc(sizeof(*p), GFP_NOFS);
6485 p->arg.fh = NFS_FH(inode);
6487 p->arg.seqid = seqid;
6488 p->res.seqid = seqid;
6490 /* Ensure we don't close file until we're done freeing locks! */
6491 p->ctx = get_nfs_open_context(ctx);
6492 p->l_ctx = nfs_get_lock_context(ctx);
6493 locks_init_lock(&p->fl);
6494 locks_copy_lock(&p->fl, fl);
6495 p->server = NFS_SERVER(inode);
6496 spin_lock(&state->state_lock);
6497 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6498 spin_unlock(&state->state_lock);
6502 static void nfs4_locku_release_calldata(void *data)
6504 struct nfs4_unlockdata *calldata = data;
6505 nfs_free_seqid(calldata->arg.seqid);
6506 nfs4_put_lock_state(calldata->lsp);
6507 nfs_put_lock_context(calldata->l_ctx);
6508 put_nfs_open_context(calldata->ctx);
6512 static void nfs4_locku_done(struct rpc_task *task, void *data)
6514 struct nfs4_unlockdata *calldata = data;
6515 struct nfs4_exception exception = {
6516 .inode = calldata->lsp->ls_state->inode,
6517 .stateid = &calldata->arg.stateid,
6520 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6522 switch (task->tk_status) {
6524 renew_lease(calldata->server, calldata->timestamp);
6525 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6526 if (nfs4_update_lock_stateid(calldata->lsp,
6527 &calldata->res.stateid))
6530 case -NFS4ERR_ADMIN_REVOKED:
6531 case -NFS4ERR_EXPIRED:
6532 nfs4_free_revoked_stateid(calldata->server,
6533 &calldata->arg.stateid,
6534 task->tk_msg.rpc_cred);
6536 case -NFS4ERR_BAD_STATEID:
6537 case -NFS4ERR_STALE_STATEID:
6538 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6540 rpc_restart_call_prepare(task);
6542 case -NFS4ERR_OLD_STATEID:
6543 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6545 rpc_restart_call_prepare(task);
6548 task->tk_status = nfs4_async_handle_exception(task,
6549 calldata->server, task->tk_status,
6551 if (exception.retry)
6552 rpc_restart_call_prepare(task);
6554 nfs_release_seqid(calldata->arg.seqid);
6557 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6559 struct nfs4_unlockdata *calldata = data;
6561 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6562 nfs_async_iocounter_wait(task, calldata->l_ctx))
6565 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6567 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6568 /* Note: exit _without_ running nfs4_locku_done */
6571 calldata->timestamp = jiffies;
6572 if (nfs4_setup_sequence(calldata->server->nfs_client,
6573 &calldata->arg.seq_args,
6574 &calldata->res.seq_res,
6576 nfs_release_seqid(calldata->arg.seqid);
6579 task->tk_action = NULL;
6581 nfs4_sequence_done(task, &calldata->res.seq_res);
6584 static const struct rpc_call_ops nfs4_locku_ops = {
6585 .rpc_call_prepare = nfs4_locku_prepare,
6586 .rpc_call_done = nfs4_locku_done,
6587 .rpc_release = nfs4_locku_release_calldata,
6590 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6591 struct nfs_open_context *ctx,
6592 struct nfs4_lock_state *lsp,
6593 struct nfs_seqid *seqid)
6595 struct nfs4_unlockdata *data;
6596 struct rpc_message msg = {
6597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6598 .rpc_cred = ctx->cred,
6600 struct rpc_task_setup task_setup_data = {
6601 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6602 .rpc_message = &msg,
6603 .callback_ops = &nfs4_locku_ops,
6604 .workqueue = nfsiod_workqueue,
6605 .flags = RPC_TASK_ASYNC,
6608 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6609 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6611 /* Ensure this is an unlock - when canceling a lock, the
6612 * canceled lock is passed in, and it won't be an unlock.
6614 fl->fl_type = F_UNLCK;
6615 if (fl->fl_flags & FL_CLOSE)
6616 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6618 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6620 nfs_free_seqid(seqid);
6621 return ERR_PTR(-ENOMEM);
6624 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6625 msg.rpc_argp = &data->arg;
6626 msg.rpc_resp = &data->res;
6627 task_setup_data.callback_data = data;
6628 return rpc_run_task(&task_setup_data);
6631 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6633 struct inode *inode = state->inode;
6634 struct nfs4_state_owner *sp = state->owner;
6635 struct nfs_inode *nfsi = NFS_I(inode);
6636 struct nfs_seqid *seqid;
6637 struct nfs4_lock_state *lsp;
6638 struct rpc_task *task;
6639 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6641 unsigned char fl_flags = request->fl_flags;
6643 status = nfs4_set_lock_state(state, request);
6644 /* Unlock _before_ we do the RPC call */
6645 request->fl_flags |= FL_EXISTS;
6646 /* Exclude nfs_delegation_claim_locks() */
6647 mutex_lock(&sp->so_delegreturn_mutex);
6648 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6649 down_read(&nfsi->rwsem);
6650 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6651 up_read(&nfsi->rwsem);
6652 mutex_unlock(&sp->so_delegreturn_mutex);
6655 up_read(&nfsi->rwsem);
6656 mutex_unlock(&sp->so_delegreturn_mutex);
6659 /* Is this a delegated lock? */
6660 lsp = request->fl_u.nfs4_fl.owner;
6661 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6663 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6664 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6668 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6669 status = PTR_ERR(task);
6672 status = rpc_wait_for_completion_task(task);
6675 request->fl_flags = fl_flags;
6676 trace_nfs4_unlock(request, state, F_SETLK, status);
6680 struct nfs4_lockdata {
6681 struct nfs_lock_args arg;
6682 struct nfs_lock_res res;
6683 struct nfs4_lock_state *lsp;
6684 struct nfs_open_context *ctx;
6685 struct file_lock fl;
6686 unsigned long timestamp;
6689 struct nfs_server *server;
6692 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6693 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6696 struct nfs4_lockdata *p;
6697 struct inode *inode = lsp->ls_state->inode;
6698 struct nfs_server *server = NFS_SERVER(inode);
6699 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6701 p = kzalloc(sizeof(*p), gfp_mask);
6705 p->arg.fh = NFS_FH(inode);
6707 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6708 if (IS_ERR(p->arg.open_seqid))
6710 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6711 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6712 if (IS_ERR(p->arg.lock_seqid))
6713 goto out_free_seqid;
6714 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6715 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6716 p->arg.lock_owner.s_dev = server->s_dev;
6717 p->res.lock_seqid = p->arg.lock_seqid;
6720 p->ctx = get_nfs_open_context(ctx);
6721 locks_init_lock(&p->fl);
6722 locks_copy_lock(&p->fl, fl);
6725 nfs_free_seqid(p->arg.open_seqid);
6731 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6733 struct nfs4_lockdata *data = calldata;
6734 struct nfs4_state *state = data->lsp->ls_state;
6736 dprintk("%s: begin!\n", __func__);
6737 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6739 /* Do we need to do an open_to_lock_owner? */
6740 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6741 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6742 goto out_release_lock_seqid;
6744 nfs4_stateid_copy(&data->arg.open_stateid,
6745 &state->open_stateid);
6746 data->arg.new_lock_owner = 1;
6747 data->res.open_seqid = data->arg.open_seqid;
6749 data->arg.new_lock_owner = 0;
6750 nfs4_stateid_copy(&data->arg.lock_stateid,
6751 &data->lsp->ls_stateid);
6753 if (!nfs4_valid_open_stateid(state)) {
6754 data->rpc_status = -EBADF;
6755 task->tk_action = NULL;
6756 goto out_release_open_seqid;
6758 data->timestamp = jiffies;
6759 if (nfs4_setup_sequence(data->server->nfs_client,
6760 &data->arg.seq_args,
6764 out_release_open_seqid:
6765 nfs_release_seqid(data->arg.open_seqid);
6766 out_release_lock_seqid:
6767 nfs_release_seqid(data->arg.lock_seqid);
6769 nfs4_sequence_done(task, &data->res.seq_res);
6770 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6773 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6775 struct nfs4_lockdata *data = calldata;
6776 struct nfs4_lock_state *lsp = data->lsp;
6778 dprintk("%s: begin!\n", __func__);
6780 if (!nfs4_sequence_done(task, &data->res.seq_res))
6783 data->rpc_status = task->tk_status;
6784 switch (task->tk_status) {
6786 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6788 if (data->arg.new_lock && !data->cancelled) {
6789 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6790 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6793 if (data->arg.new_lock_owner != 0) {
6794 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6795 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6796 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6797 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6800 case -NFS4ERR_BAD_STATEID:
6801 case -NFS4ERR_OLD_STATEID:
6802 case -NFS4ERR_STALE_STATEID:
6803 case -NFS4ERR_EXPIRED:
6804 if (data->arg.new_lock_owner != 0) {
6805 if (!nfs4_stateid_match(&data->arg.open_stateid,
6806 &lsp->ls_state->open_stateid))
6808 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6813 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6816 if (!data->cancelled)
6817 rpc_restart_call_prepare(task);
6821 static void nfs4_lock_release(void *calldata)
6823 struct nfs4_lockdata *data = calldata;
6825 dprintk("%s: begin!\n", __func__);
6826 nfs_free_seqid(data->arg.open_seqid);
6827 if (data->cancelled && data->rpc_status == 0) {
6828 struct rpc_task *task;
6829 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6830 data->arg.lock_seqid);
6832 rpc_put_task_async(task);
6833 dprintk("%s: cancelling lock!\n", __func__);
6835 nfs_free_seqid(data->arg.lock_seqid);
6836 nfs4_put_lock_state(data->lsp);
6837 put_nfs_open_context(data->ctx);
6839 dprintk("%s: done!\n", __func__);
6842 static const struct rpc_call_ops nfs4_lock_ops = {
6843 .rpc_call_prepare = nfs4_lock_prepare,
6844 .rpc_call_done = nfs4_lock_done,
6845 .rpc_release = nfs4_lock_release,
6848 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6851 case -NFS4ERR_ADMIN_REVOKED:
6852 case -NFS4ERR_EXPIRED:
6853 case -NFS4ERR_BAD_STATEID:
6854 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6855 if (new_lock_owner != 0 ||
6856 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6857 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6859 case -NFS4ERR_STALE_STATEID:
6860 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6861 nfs4_schedule_lease_recovery(server->nfs_client);
6865 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6867 struct nfs4_lockdata *data;
6868 struct rpc_task *task;
6869 struct rpc_message msg = {
6870 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6871 .rpc_cred = state->owner->so_cred,
6873 struct rpc_task_setup task_setup_data = {
6874 .rpc_client = NFS_CLIENT(state->inode),
6875 .rpc_message = &msg,
6876 .callback_ops = &nfs4_lock_ops,
6877 .workqueue = nfsiod_workqueue,
6878 .flags = RPC_TASK_ASYNC,
6882 dprintk("%s: begin!\n", __func__);
6883 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6884 fl->fl_u.nfs4_fl.owner,
6885 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6889 data->arg.block = 1;
6890 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6891 recovery_type > NFS_LOCK_NEW);
6892 msg.rpc_argp = &data->arg;
6893 msg.rpc_resp = &data->res;
6894 task_setup_data.callback_data = data;
6895 if (recovery_type > NFS_LOCK_NEW) {
6896 if (recovery_type == NFS_LOCK_RECLAIM)
6897 data->arg.reclaim = NFS_LOCK_RECLAIM;
6899 data->arg.new_lock = 1;
6900 task = rpc_run_task(&task_setup_data);
6902 return PTR_ERR(task);
6903 ret = rpc_wait_for_completion_task(task);
6905 ret = data->rpc_status;
6907 nfs4_handle_setlk_error(data->server, data->lsp,
6908 data->arg.new_lock_owner, ret);
6910 data->cancelled = true;
6912 dprintk("%s: done, ret = %d!\n", __func__, ret);
6913 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6917 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6919 struct nfs_server *server = NFS_SERVER(state->inode);
6920 struct nfs4_exception exception = {
6921 .inode = state->inode,
6926 /* Cache the lock if possible... */
6927 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6929 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6930 if (err != -NFS4ERR_DELAY)
6932 nfs4_handle_exception(server, err, &exception);
6933 } while (exception.retry);
6937 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6939 struct nfs_server *server = NFS_SERVER(state->inode);
6940 struct nfs4_exception exception = {
6941 .inode = state->inode,
6945 err = nfs4_set_lock_state(state, request);
6948 if (!recover_lost_locks) {
6949 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6953 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6955 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6959 case -NFS4ERR_GRACE:
6960 case -NFS4ERR_DELAY:
6961 nfs4_handle_exception(server, err, &exception);
6964 } while (exception.retry);
6969 #if defined(CONFIG_NFS_V4_1)
6970 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6972 struct nfs4_lock_state *lsp;
6975 status = nfs4_set_lock_state(state, request);
6978 lsp = request->fl_u.nfs4_fl.owner;
6979 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6980 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6982 return nfs4_lock_expired(state, request);
6986 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6988 struct nfs_inode *nfsi = NFS_I(state->inode);
6989 struct nfs4_state_owner *sp = state->owner;
6990 unsigned char fl_flags = request->fl_flags;
6993 request->fl_flags |= FL_ACCESS;
6994 status = locks_lock_inode_wait(state->inode, request);
6997 mutex_lock(&sp->so_delegreturn_mutex);
6998 down_read(&nfsi->rwsem);
6999 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7000 /* Yes: cache locks! */
7001 /* ...but avoid races with delegation recall... */
7002 request->fl_flags = fl_flags & ~FL_SLEEP;
7003 status = locks_lock_inode_wait(state->inode, request);
7004 up_read(&nfsi->rwsem);
7005 mutex_unlock(&sp->so_delegreturn_mutex);
7008 up_read(&nfsi->rwsem);
7009 mutex_unlock(&sp->so_delegreturn_mutex);
7010 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7012 request->fl_flags = fl_flags;
7016 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7018 struct nfs4_exception exception = {
7020 .inode = state->inode,
7021 .interruptible = true,
7026 err = _nfs4_proc_setlk(state, cmd, request);
7027 if (err == -NFS4ERR_DENIED)
7029 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7031 } while (exception.retry);
7035 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7036 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7039 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7040 struct file_lock *request)
7042 int status = -ERESTARTSYS;
7043 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7045 while(!signalled()) {
7046 status = nfs4_proc_setlk(state, cmd, request);
7047 if ((status != -EAGAIN) || IS_SETLK(cmd))
7049 freezable_schedule_timeout_interruptible(timeout);
7051 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7052 status = -ERESTARTSYS;
7057 #ifdef CONFIG_NFS_V4_1
7058 struct nfs4_lock_waiter {
7059 struct task_struct *task;
7060 struct inode *inode;
7061 struct nfs_lowner *owner;
7065 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7068 struct nfs4_lock_waiter *waiter = wait->private;
7070 /* NULL key means to wake up everyone */
7072 struct cb_notify_lock_args *cbnl = key;
7073 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7074 *wowner = waiter->owner;
7076 /* Only wake if the callback was for the same owner. */
7077 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7080 /* Make sure it's for the right inode */
7081 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7085 /* override "private" so we can use default_wake_function */
7086 wait->private = waiter->task;
7087 ret = woken_wake_function(wait, mode, flags, key);
7089 list_del_init(&wait->entry);
7090 wait->private = waiter;
7095 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7097 int status = -ERESTARTSYS;
7098 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7099 struct nfs_server *server = NFS_SERVER(state->inode);
7100 struct nfs_client *clp = server->nfs_client;
7101 wait_queue_head_t *q = &clp->cl_lock_waitq;
7102 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7103 .id = lsp->ls_seqid.owner_id,
7104 .s_dev = server->s_dev };
7105 struct nfs4_lock_waiter waiter = { .task = current,
7106 .inode = state->inode,
7108 wait_queue_entry_t wait;
7110 /* Don't bother with waitqueue if we don't expect a callback */
7111 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7112 return nfs4_retry_setlk_simple(state, cmd, request);
7115 wait.private = &waiter;
7116 wait.func = nfs4_wake_lock_waiter;
7118 while(!signalled()) {
7119 add_wait_queue(q, &wait);
7120 status = nfs4_proc_setlk(state, cmd, request);
7121 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7122 finish_wait(q, &wait);
7126 status = -ERESTARTSYS;
7127 freezer_do_not_count();
7128 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7130 finish_wait(q, &wait);
7135 #else /* !CONFIG_NFS_V4_1 */
7137 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7139 return nfs4_retry_setlk_simple(state, cmd, request);
7144 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7146 struct nfs_open_context *ctx;
7147 struct nfs4_state *state;
7150 /* verify open state */
7151 ctx = nfs_file_open_context(filp);
7154 if (IS_GETLK(cmd)) {
7156 return nfs4_proc_getlk(state, F_GETLK, request);
7160 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7163 if (request->fl_type == F_UNLCK) {
7165 return nfs4_proc_unlck(state, cmd, request);
7172 if ((request->fl_flags & FL_POSIX) &&
7173 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7177 * Don't rely on the VFS having checked the file open mode,
7178 * since it won't do this for flock() locks.
7180 switch (request->fl_type) {
7182 if (!(filp->f_mode & FMODE_READ))
7186 if (!(filp->f_mode & FMODE_WRITE))
7190 status = nfs4_set_lock_state(state, request);
7194 return nfs4_retry_setlk(state, cmd, request);
7197 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7199 struct nfs_server *server = NFS_SERVER(state->inode);
7202 err = nfs4_set_lock_state(state, fl);
7205 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7206 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7209 struct nfs_release_lockowner_data {
7210 struct nfs4_lock_state *lsp;
7211 struct nfs_server *server;
7212 struct nfs_release_lockowner_args args;
7213 struct nfs_release_lockowner_res res;
7214 unsigned long timestamp;
7217 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7219 struct nfs_release_lockowner_data *data = calldata;
7220 struct nfs_server *server = data->server;
7221 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7222 &data->res.seq_res, task);
7223 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7224 data->timestamp = jiffies;
7227 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7229 struct nfs_release_lockowner_data *data = calldata;
7230 struct nfs_server *server = data->server;
7232 nfs40_sequence_done(task, &data->res.seq_res);
7234 switch (task->tk_status) {
7236 renew_lease(server, data->timestamp);
7238 case -NFS4ERR_STALE_CLIENTID:
7239 case -NFS4ERR_EXPIRED:
7240 nfs4_schedule_lease_recovery(server->nfs_client);
7242 case -NFS4ERR_LEASE_MOVED:
7243 case -NFS4ERR_DELAY:
7244 if (nfs4_async_handle_error(task, server,
7245 NULL, NULL) == -EAGAIN)
7246 rpc_restart_call_prepare(task);
7250 static void nfs4_release_lockowner_release(void *calldata)
7252 struct nfs_release_lockowner_data *data = calldata;
7253 nfs4_free_lock_state(data->server, data->lsp);
7257 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7258 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7259 .rpc_call_done = nfs4_release_lockowner_done,
7260 .rpc_release = nfs4_release_lockowner_release,
7264 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7266 struct nfs_release_lockowner_data *data;
7267 struct rpc_message msg = {
7268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7271 if (server->nfs_client->cl_mvops->minor_version != 0)
7274 data = kmalloc(sizeof(*data), GFP_NOFS);
7278 data->server = server;
7279 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7280 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7281 data->args.lock_owner.s_dev = server->s_dev;
7283 msg.rpc_argp = &data->args;
7284 msg.rpc_resp = &data->res;
7285 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7286 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7289 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7291 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7292 struct dentry *unused, struct inode *inode,
7293 const char *key, const void *buf,
7294 size_t buflen, int flags)
7296 return nfs4_proc_set_acl(inode, buf, buflen);
7299 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7300 struct dentry *unused, struct inode *inode,
7301 const char *key, void *buf, size_t buflen)
7303 return nfs4_proc_get_acl(inode, buf, buflen);
7306 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7308 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7311 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7313 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7314 struct dentry *unused, struct inode *inode,
7315 const char *key, const void *buf,
7316 size_t buflen, int flags)
7318 if (security_ismaclabel(key))
7319 return nfs4_set_security_label(inode, buf, buflen);
7324 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7325 struct dentry *unused, struct inode *inode,
7326 const char *key, void *buf, size_t buflen)
7328 if (security_ismaclabel(key))
7329 return nfs4_get_security_label(inode, buf, buflen);
7334 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7338 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7339 len = security_inode_listsecurity(inode, list, list_len);
7340 if (list_len && len > list_len)
7346 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7347 .prefix = XATTR_SECURITY_PREFIX,
7348 .get = nfs4_xattr_get_nfs4_label,
7349 .set = nfs4_xattr_set_nfs4_label,
7355 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7363 * nfs_fhget will use either the mounted_on_fileid or the fileid
7365 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7367 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7368 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7369 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7370 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7373 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7374 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7375 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7379 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7380 const struct qstr *name,
7381 struct nfs4_fs_locations *fs_locations,
7384 struct nfs_server *server = NFS_SERVER(dir);
7386 struct nfs4_fs_locations_arg args = {
7387 .dir_fh = NFS_FH(dir),
7392 struct nfs4_fs_locations_res res = {
7393 .fs_locations = fs_locations,
7395 struct rpc_message msg = {
7396 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7402 dprintk("%s: start\n", __func__);
7404 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7405 bitmask[1] = nfs4_fattr_bitmap[1];
7407 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7408 * is not supported */
7409 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7410 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7412 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7414 nfs_fattr_init(&fs_locations->fattr);
7415 fs_locations->server = server;
7416 fs_locations->nlocations = 0;
7417 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7418 dprintk("%s: returned status = %d\n", __func__, status);
7422 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7423 const struct qstr *name,
7424 struct nfs4_fs_locations *fs_locations,
7427 struct nfs4_exception exception = {
7428 .interruptible = true,
7432 err = _nfs4_proc_fs_locations(client, dir, name,
7433 fs_locations, page);
7434 trace_nfs4_get_fs_locations(dir, name, err);
7435 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7437 } while (exception.retry);
7442 * This operation also signals the server that this client is
7443 * performing migration recovery. The server can stop returning
7444 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7445 * appended to this compound to identify the client ID which is
7446 * performing recovery.
7448 static int _nfs40_proc_get_locations(struct inode *inode,
7449 struct nfs4_fs_locations *locations,
7450 struct page *page, const struct cred *cred)
7452 struct nfs_server *server = NFS_SERVER(inode);
7453 struct rpc_clnt *clnt = server->client;
7455 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7457 struct nfs4_fs_locations_arg args = {
7458 .clientid = server->nfs_client->cl_clientid,
7459 .fh = NFS_FH(inode),
7462 .migration = 1, /* skip LOOKUP */
7463 .renew = 1, /* append RENEW */
7465 struct nfs4_fs_locations_res res = {
7466 .fs_locations = locations,
7470 struct rpc_message msg = {
7471 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7476 unsigned long now = jiffies;
7479 nfs_fattr_init(&locations->fattr);
7480 locations->server = server;
7481 locations->nlocations = 0;
7483 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7484 status = nfs4_call_sync_sequence(clnt, server, &msg,
7485 &args.seq_args, &res.seq_res);
7489 renew_lease(server, now);
7493 #ifdef CONFIG_NFS_V4_1
7496 * This operation also signals the server that this client is
7497 * performing migration recovery. The server can stop asserting
7498 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7499 * performing this operation is identified in the SEQUENCE
7500 * operation in this compound.
7502 * When the client supports GETATTR(fs_locations_info), it can
7503 * be plumbed in here.
7505 static int _nfs41_proc_get_locations(struct inode *inode,
7506 struct nfs4_fs_locations *locations,
7507 struct page *page, const struct cred *cred)
7509 struct nfs_server *server = NFS_SERVER(inode);
7510 struct rpc_clnt *clnt = server->client;
7512 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7514 struct nfs4_fs_locations_arg args = {
7515 .fh = NFS_FH(inode),
7518 .migration = 1, /* skip LOOKUP */
7520 struct nfs4_fs_locations_res res = {
7521 .fs_locations = locations,
7524 struct rpc_message msg = {
7525 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7532 nfs_fattr_init(&locations->fattr);
7533 locations->server = server;
7534 locations->nlocations = 0;
7536 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7537 status = nfs4_call_sync_sequence(clnt, server, &msg,
7538 &args.seq_args, &res.seq_res);
7539 if (status == NFS4_OK &&
7540 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7541 status = -NFS4ERR_LEASE_MOVED;
7545 #endif /* CONFIG_NFS_V4_1 */
7548 * nfs4_proc_get_locations - discover locations for a migrated FSID
7549 * @inode: inode on FSID that is migrating
7550 * @locations: result of query
7552 * @cred: credential to use for this operation
7554 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7555 * operation failed, or a negative errno if a local error occurred.
7557 * On success, "locations" is filled in, but if the server has
7558 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7561 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7562 * from this client that require migration recovery.
7564 int nfs4_proc_get_locations(struct inode *inode,
7565 struct nfs4_fs_locations *locations,
7566 struct page *page, const struct cred *cred)
7568 struct nfs_server *server = NFS_SERVER(inode);
7569 struct nfs_client *clp = server->nfs_client;
7570 const struct nfs4_mig_recovery_ops *ops =
7571 clp->cl_mvops->mig_recovery_ops;
7572 struct nfs4_exception exception = {
7573 .interruptible = true,
7577 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7578 (unsigned long long)server->fsid.major,
7579 (unsigned long long)server->fsid.minor,
7581 nfs_display_fhandle(NFS_FH(inode), __func__);
7584 status = ops->get_locations(inode, locations, page, cred);
7585 if (status != -NFS4ERR_DELAY)
7587 nfs4_handle_exception(server, status, &exception);
7588 } while (exception.retry);
7593 * This operation also signals the server that this client is
7594 * performing "lease moved" recovery. The server can stop
7595 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7596 * is appended to this compound to identify the client ID which is
7597 * performing recovery.
7599 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7601 struct nfs_server *server = NFS_SERVER(inode);
7602 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7603 struct rpc_clnt *clnt = server->client;
7604 struct nfs4_fsid_present_arg args = {
7605 .fh = NFS_FH(inode),
7606 .clientid = clp->cl_clientid,
7607 .renew = 1, /* append RENEW */
7609 struct nfs4_fsid_present_res res = {
7612 struct rpc_message msg = {
7613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7618 unsigned long now = jiffies;
7621 res.fh = nfs_alloc_fhandle();
7625 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7626 status = nfs4_call_sync_sequence(clnt, server, &msg,
7627 &args.seq_args, &res.seq_res);
7628 nfs_free_fhandle(res.fh);
7632 do_renew_lease(clp, now);
7636 #ifdef CONFIG_NFS_V4_1
7639 * This operation also signals the server that this client is
7640 * performing "lease moved" recovery. The server can stop asserting
7641 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7642 * this operation is identified in the SEQUENCE operation in this
7645 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7647 struct nfs_server *server = NFS_SERVER(inode);
7648 struct rpc_clnt *clnt = server->client;
7649 struct nfs4_fsid_present_arg args = {
7650 .fh = NFS_FH(inode),
7652 struct nfs4_fsid_present_res res = {
7654 struct rpc_message msg = {
7655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7662 res.fh = nfs_alloc_fhandle();
7666 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7667 status = nfs4_call_sync_sequence(clnt, server, &msg,
7668 &args.seq_args, &res.seq_res);
7669 nfs_free_fhandle(res.fh);
7670 if (status == NFS4_OK &&
7671 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7672 status = -NFS4ERR_LEASE_MOVED;
7676 #endif /* CONFIG_NFS_V4_1 */
7679 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7680 * @inode: inode on FSID to check
7681 * @cred: credential to use for this operation
7683 * Server indicates whether the FSID is present, moved, or not
7684 * recognized. This operation is necessary to clear a LEASE_MOVED
7685 * condition for this client ID.
7687 * Returns NFS4_OK if the FSID is present on this server,
7688 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7689 * NFS4ERR code if some error occurred on the server, or a
7690 * negative errno if a local failure occurred.
7692 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
7694 struct nfs_server *server = NFS_SERVER(inode);
7695 struct nfs_client *clp = server->nfs_client;
7696 const struct nfs4_mig_recovery_ops *ops =
7697 clp->cl_mvops->mig_recovery_ops;
7698 struct nfs4_exception exception = {
7699 .interruptible = true,
7703 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7704 (unsigned long long)server->fsid.major,
7705 (unsigned long long)server->fsid.minor,
7707 nfs_display_fhandle(NFS_FH(inode), __func__);
7710 status = ops->fsid_present(inode, cred);
7711 if (status != -NFS4ERR_DELAY)
7713 nfs4_handle_exception(server, status, &exception);
7714 } while (exception.retry);
7719 * If 'use_integrity' is true and the state managment nfs_client
7720 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7721 * and the machine credential as per RFC3530bis and RFC5661 Security
7722 * Considerations sections. Otherwise, just use the user cred with the
7723 * filesystem's rpc_client.
7725 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7728 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7729 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
7730 struct nfs4_secinfo_arg args = {
7731 .dir_fh = NFS_FH(dir),
7734 struct nfs4_secinfo_res res = {
7737 struct rpc_message msg = {
7738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7742 struct nfs4_call_sync_data data = {
7743 .seq_server = NFS_SERVER(dir),
7744 .seq_args = &args.seq_args,
7745 .seq_res = &res.seq_res,
7747 struct rpc_task_setup task_setup = {
7749 .rpc_message = &msg,
7750 .callback_ops = clp->cl_mvops->call_sync_ops,
7751 .callback_data = &data,
7752 .flags = RPC_TASK_NO_ROUND_ROBIN,
7754 const struct cred *cred = NULL;
7756 if (use_integrity) {
7757 clnt = clp->cl_rpcclient;
7758 task_setup.rpc_client = clnt;
7760 cred = nfs4_get_clid_cred(clp);
7761 msg.rpc_cred = cred;
7764 dprintk("NFS call secinfo %s\n", name->name);
7766 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7767 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
7768 status = nfs4_call_sync_custom(&task_setup);
7770 dprintk("NFS reply secinfo: %d\n", status);
7776 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7777 struct nfs4_secinfo_flavors *flavors)
7779 struct nfs4_exception exception = {
7780 .interruptible = true,
7784 err = -NFS4ERR_WRONGSEC;
7786 /* try to use integrity protection with machine cred */
7787 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7788 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7791 * if unable to use integrity protection, or SECINFO with
7792 * integrity protection returns NFS4ERR_WRONGSEC (which is
7793 * disallowed by spec, but exists in deployed servers) use
7794 * the current filesystem's rpc_client and the user cred.
7796 if (err == -NFS4ERR_WRONGSEC)
7797 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7799 trace_nfs4_secinfo(dir, name, err);
7800 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7802 } while (exception.retry);
7806 #ifdef CONFIG_NFS_V4_1
7808 * Check the exchange flags returned by the server for invalid flags, having
7809 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7812 static int nfs4_check_cl_exchange_flags(u32 flags)
7814 if (flags & ~EXCHGID4_FLAG_MASK_R)
7816 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7817 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7819 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7823 return -NFS4ERR_INVAL;
7827 nfs41_same_server_scope(struct nfs41_server_scope *a,
7828 struct nfs41_server_scope *b)
7830 if (a->server_scope_sz != b->server_scope_sz)
7832 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7836 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7838 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
7839 struct nfs_client *clp = args->client;
7841 switch (task->tk_status) {
7842 case -NFS4ERR_BADSESSION:
7843 case -NFS4ERR_DEADSESSION:
7844 nfs4_schedule_session_recovery(clp->cl_session,
7849 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7850 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7854 * nfs4_proc_bind_one_conn_to_session()
7856 * The 4.1 client currently uses the same TCP connection for the
7857 * fore and backchannel.
7860 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7861 struct rpc_xprt *xprt,
7862 struct nfs_client *clp,
7863 const struct cred *cred)
7866 struct nfs41_bind_conn_to_session_args args = {
7868 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7870 struct nfs41_bind_conn_to_session_res res;
7871 struct rpc_message msg = {
7873 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7878 struct rpc_task_setup task_setup_data = {
7881 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7882 .rpc_message = &msg,
7883 .flags = RPC_TASK_TIMEOUT,
7885 struct rpc_task *task;
7887 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7888 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7889 args.dir = NFS4_CDFC4_FORE;
7891 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7892 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7893 args.dir = NFS4_CDFC4_FORE;
7895 task = rpc_run_task(&task_setup_data);
7896 if (!IS_ERR(task)) {
7897 status = task->tk_status;
7900 status = PTR_ERR(task);
7901 trace_nfs4_bind_conn_to_session(clp, status);
7903 if (memcmp(res.sessionid.data,
7904 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7905 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7908 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7909 dprintk("NFS: %s: Unexpected direction from server\n",
7913 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7914 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7923 struct rpc_bind_conn_calldata {
7924 struct nfs_client *clp;
7925 const struct cred *cred;
7929 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7930 struct rpc_xprt *xprt,
7933 struct rpc_bind_conn_calldata *p = calldata;
7935 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7938 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
7940 struct rpc_bind_conn_calldata data = {
7944 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7945 nfs4_proc_bind_conn_to_session_callback, &data);
7949 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7950 * and operations we'd like to see to enable certain features in the allow map
7952 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7953 .how = SP4_MACH_CRED,
7954 .enforce.u.words = {
7955 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7956 1 << (OP_EXCHANGE_ID - 32) |
7957 1 << (OP_CREATE_SESSION - 32) |
7958 1 << (OP_DESTROY_SESSION - 32) |
7959 1 << (OP_DESTROY_CLIENTID - 32)
7962 [0] = 1 << (OP_CLOSE) |
7963 1 << (OP_OPEN_DOWNGRADE) |
7965 1 << (OP_DELEGRETURN) |
7967 [1] = 1 << (OP_SECINFO - 32) |
7968 1 << (OP_SECINFO_NO_NAME - 32) |
7969 1 << (OP_LAYOUTRETURN - 32) |
7970 1 << (OP_TEST_STATEID - 32) |
7971 1 << (OP_FREE_STATEID - 32) |
7972 1 << (OP_WRITE - 32)
7977 * Select the state protection mode for client `clp' given the server results
7978 * from exchange_id in `sp'.
7980 * Returns 0 on success, negative errno otherwise.
7982 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7983 struct nfs41_state_protection *sp)
7985 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7986 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7987 1 << (OP_EXCHANGE_ID - 32) |
7988 1 << (OP_CREATE_SESSION - 32) |
7989 1 << (OP_DESTROY_SESSION - 32) |
7990 1 << (OP_DESTROY_CLIENTID - 32)
7992 unsigned long flags = 0;
7996 if (sp->how == SP4_MACH_CRED) {
7997 /* Print state protect result */
7998 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7999 for (i = 0; i <= LAST_NFS4_OP; i++) {
8000 if (test_bit(i, sp->enforce.u.longs))
8001 dfprintk(MOUNT, " enforce op %d\n", i);
8002 if (test_bit(i, sp->allow.u.longs))
8003 dfprintk(MOUNT, " allow op %d\n", i);
8006 /* make sure nothing is on enforce list that isn't supported */
8007 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8008 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8009 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8016 * Minimal mode - state operations are allowed to use machine
8017 * credential. Note this already happens by default, so the
8018 * client doesn't have to do anything more than the negotiation.
8020 * NOTE: we don't care if EXCHANGE_ID is in the list -
8021 * we're already using the machine cred for exchange_id
8022 * and will never use a different cred.
8024 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8025 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8026 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8027 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8028 dfprintk(MOUNT, "sp4_mach_cred:\n");
8029 dfprintk(MOUNT, " minimal mode enabled\n");
8030 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8032 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8037 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8038 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8039 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8040 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8041 dfprintk(MOUNT, " cleanup mode enabled\n");
8042 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8045 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8046 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8047 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8050 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8051 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8052 dfprintk(MOUNT, " secinfo mode enabled\n");
8053 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8056 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8057 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8058 dfprintk(MOUNT, " stateid mode enabled\n");
8059 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8062 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8063 dfprintk(MOUNT, " write mode enabled\n");
8064 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8067 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8068 dfprintk(MOUNT, " commit mode enabled\n");
8069 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8073 clp->cl_sp4_flags = flags;
8077 struct nfs41_exchange_id_data {
8078 struct nfs41_exchange_id_res res;
8079 struct nfs41_exchange_id_args args;
8082 static void nfs4_exchange_id_release(void *data)
8084 struct nfs41_exchange_id_data *cdata =
8085 (struct nfs41_exchange_id_data *)data;
8087 nfs_put_client(cdata->args.client);
8088 kfree(cdata->res.impl_id);
8089 kfree(cdata->res.server_scope);
8090 kfree(cdata->res.server_owner);
8094 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8095 .rpc_release = nfs4_exchange_id_release,
8099 * _nfs4_proc_exchange_id()
8101 * Wrapper for EXCHANGE_ID operation.
8103 static struct rpc_task *
8104 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8105 u32 sp4_how, struct rpc_xprt *xprt)
8107 struct rpc_message msg = {
8108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8111 struct rpc_task_setup task_setup_data = {
8112 .rpc_client = clp->cl_rpcclient,
8113 .callback_ops = &nfs4_exchange_id_call_ops,
8114 .rpc_message = &msg,
8115 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8117 struct nfs41_exchange_id_data *calldata;
8120 if (!refcount_inc_not_zero(&clp->cl_count))
8121 return ERR_PTR(-EIO);
8124 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8128 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8130 status = nfs4_init_uniform_client_string(clp);
8134 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8137 if (unlikely(calldata->res.server_owner == NULL))
8140 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8142 if (unlikely(calldata->res.server_scope == NULL))
8143 goto out_server_owner;
8145 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8146 if (unlikely(calldata->res.impl_id == NULL))
8147 goto out_server_scope;
8151 calldata->args.state_protect.how = SP4_NONE;
8155 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8165 task_setup_data.rpc_xprt = xprt;
8166 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8167 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8168 sizeof(calldata->args.verifier.data));
8170 calldata->args.client = clp;
8171 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8172 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8173 #ifdef CONFIG_NFS_V4_1_MIGRATION
8174 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8176 msg.rpc_argp = &calldata->args;
8177 msg.rpc_resp = &calldata->res;
8178 task_setup_data.callback_data = calldata;
8180 return rpc_run_task(&task_setup_data);
8183 kfree(calldata->res.impl_id);
8185 kfree(calldata->res.server_scope);
8187 kfree(calldata->res.server_owner);
8191 nfs_put_client(clp);
8192 return ERR_PTR(status);
8196 * _nfs4_proc_exchange_id()
8198 * Wrapper for EXCHANGE_ID operation.
8200 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8203 struct rpc_task *task;
8204 struct nfs41_exchange_id_args *argp;
8205 struct nfs41_exchange_id_res *resp;
8208 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8210 return PTR_ERR(task);
8212 argp = task->tk_msg.rpc_argp;
8213 resp = task->tk_msg.rpc_resp;
8214 status = task->tk_status;
8218 status = nfs4_check_cl_exchange_flags(resp->flags);
8222 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8226 clp->cl_clientid = resp->clientid;
8227 clp->cl_exchange_flags = resp->flags;
8228 clp->cl_seqid = resp->seqid;
8229 /* Client ID is not confirmed */
8230 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8231 clear_bit(NFS4_SESSION_ESTABLISHED,
8232 &clp->cl_session->session_state);
8234 if (clp->cl_serverscope != NULL &&
8235 !nfs41_same_server_scope(clp->cl_serverscope,
8236 resp->server_scope)) {
8237 dprintk("%s: server_scope mismatch detected\n",
8239 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8242 swap(clp->cl_serverowner, resp->server_owner);
8243 swap(clp->cl_serverscope, resp->server_scope);
8244 swap(clp->cl_implid, resp->impl_id);
8246 /* Save the EXCHANGE_ID verifier session trunk tests */
8247 memcpy(clp->cl_confirm.data, argp->verifier.data,
8248 sizeof(clp->cl_confirm.data));
8250 trace_nfs4_exchange_id(clp, status);
8256 * nfs4_proc_exchange_id()
8258 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8260 * Since the clientid has expired, all compounds using sessions
8261 * associated with the stale clientid will be returning
8262 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8263 * be in some phase of session reset.
8265 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8267 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8269 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8272 /* try SP4_MACH_CRED if krb5i/p */
8273 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8274 authflavor == RPC_AUTH_GSS_KRB5P) {
8275 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8281 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8285 * nfs4_test_session_trunk
8287 * This is an add_xprt_test() test function called from
8288 * rpc_clnt_setup_test_and_add_xprt.
8290 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8291 * and is dereferrenced in nfs4_exchange_id_release
8293 * Upon success, add the new transport to the rpc_clnt
8295 * @clnt: struct rpc_clnt to get new transport
8296 * @xprt: the rpc_xprt to test
8297 * @data: call data for _nfs4_proc_exchange_id.
8299 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8302 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8303 struct rpc_task *task;
8308 dprintk("--> %s try %s\n", __func__,
8309 xprt->address_strings[RPC_DISPLAY_ADDR]);
8311 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8313 /* Test connection for session trunking. Async exchange_id call */
8314 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8318 status = task->tk_status;
8320 status = nfs4_detect_session_trunking(adata->clp,
8321 task->tk_msg.rpc_resp, xprt);
8324 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8328 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8330 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8331 const struct cred *cred)
8333 struct rpc_message msg = {
8334 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8340 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8341 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8342 trace_nfs4_destroy_clientid(clp, status);
8344 dprintk("NFS: Got error %d from the server %s on "
8345 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8349 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8350 const struct cred *cred)
8355 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8356 ret = _nfs4_proc_destroy_clientid(clp, cred);
8358 case -NFS4ERR_DELAY:
8359 case -NFS4ERR_CLIENTID_BUSY:
8369 int nfs4_destroy_clientid(struct nfs_client *clp)
8371 const struct cred *cred;
8374 if (clp->cl_mvops->minor_version < 1)
8376 if (clp->cl_exchange_flags == 0)
8378 if (clp->cl_preserve_clid)
8380 cred = nfs4_get_clid_cred(clp);
8381 ret = nfs4_proc_destroy_clientid(clp, cred);
8385 case -NFS4ERR_STALE_CLIENTID:
8386 clp->cl_exchange_flags = 0;
8392 #endif /* CONFIG_NFS_V4_1 */
8394 struct nfs4_get_lease_time_data {
8395 struct nfs4_get_lease_time_args *args;
8396 struct nfs4_get_lease_time_res *res;
8397 struct nfs_client *clp;
8400 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8403 struct nfs4_get_lease_time_data *data =
8404 (struct nfs4_get_lease_time_data *)calldata;
8406 dprintk("--> %s\n", __func__);
8407 /* just setup sequence, do not trigger session recovery
8408 since we're invoked within one */
8409 nfs4_setup_sequence(data->clp,
8410 &data->args->la_seq_args,
8411 &data->res->lr_seq_res,
8413 dprintk("<-- %s\n", __func__);
8417 * Called from nfs4_state_manager thread for session setup, so don't recover
8418 * from sequence operation or clientid errors.
8420 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8422 struct nfs4_get_lease_time_data *data =
8423 (struct nfs4_get_lease_time_data *)calldata;
8425 dprintk("--> %s\n", __func__);
8426 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8428 switch (task->tk_status) {
8429 case -NFS4ERR_DELAY:
8430 case -NFS4ERR_GRACE:
8431 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8432 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8433 task->tk_status = 0;
8435 case -NFS4ERR_RETRY_UNCACHED_REP:
8436 rpc_restart_call_prepare(task);
8439 dprintk("<-- %s\n", __func__);
8442 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8443 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8444 .rpc_call_done = nfs4_get_lease_time_done,
8447 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8449 struct nfs4_get_lease_time_args args;
8450 struct nfs4_get_lease_time_res res = {
8451 .lr_fsinfo = fsinfo,
8453 struct nfs4_get_lease_time_data data = {
8458 struct rpc_message msg = {
8459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8463 struct rpc_task_setup task_setup = {
8464 .rpc_client = clp->cl_rpcclient,
8465 .rpc_message = &msg,
8466 .callback_ops = &nfs4_get_lease_time_ops,
8467 .callback_data = &data,
8468 .flags = RPC_TASK_TIMEOUT,
8471 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8472 return nfs4_call_sync_custom(&task_setup);
8475 #ifdef CONFIG_NFS_V4_1
8478 * Initialize the values to be used by the client in CREATE_SESSION
8479 * If nfs4_init_session set the fore channel request and response sizes,
8482 * Set the back channel max_resp_sz_cached to zero to force the client to
8483 * always set csa_cachethis to FALSE because the current implementation
8484 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8486 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8487 struct rpc_clnt *clnt)
8489 unsigned int max_rqst_sz, max_resp_sz;
8490 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8491 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8493 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8494 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8496 /* Fore channel attributes */
8497 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8498 args->fc_attrs.max_resp_sz = max_resp_sz;
8499 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8500 args->fc_attrs.max_reqs = max_session_slots;
8502 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8503 "max_ops=%u max_reqs=%u\n",
8505 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8506 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8508 /* Back channel attributes */
8509 args->bc_attrs.max_rqst_sz = max_bc_payload;
8510 args->bc_attrs.max_resp_sz = max_bc_payload;
8511 args->bc_attrs.max_resp_sz_cached = 0;
8512 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8513 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8514 if (args->bc_attrs.max_reqs > max_bc_slots)
8515 args->bc_attrs.max_reqs = max_bc_slots;
8517 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8518 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8520 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8521 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8522 args->bc_attrs.max_reqs);
8525 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8526 struct nfs41_create_session_res *res)
8528 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8529 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8531 if (rcvd->max_resp_sz > sent->max_resp_sz)
8534 * Our requested max_ops is the minimum we need; we're not
8535 * prepared to break up compounds into smaller pieces than that.
8536 * So, no point even trying to continue if the server won't
8539 if (rcvd->max_ops < sent->max_ops)
8541 if (rcvd->max_reqs == 0)
8543 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8544 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8548 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8549 struct nfs41_create_session_res *res)
8551 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8552 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8554 if (!(res->flags & SESSION4_BACK_CHAN))
8556 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8558 if (rcvd->max_resp_sz < sent->max_resp_sz)
8560 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8562 if (rcvd->max_ops > sent->max_ops)
8564 if (rcvd->max_reqs > sent->max_reqs)
8570 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8571 struct nfs41_create_session_res *res)
8575 ret = nfs4_verify_fore_channel_attrs(args, res);
8578 return nfs4_verify_back_channel_attrs(args, res);
8581 static void nfs4_update_session(struct nfs4_session *session,
8582 struct nfs41_create_session_res *res)
8584 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8585 /* Mark client id and session as being confirmed */
8586 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8587 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8588 session->flags = res->flags;
8589 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8590 if (res->flags & SESSION4_BACK_CHAN)
8591 memcpy(&session->bc_attrs, &res->bc_attrs,
8592 sizeof(session->bc_attrs));
8595 static int _nfs4_proc_create_session(struct nfs_client *clp,
8596 const struct cred *cred)
8598 struct nfs4_session *session = clp->cl_session;
8599 struct nfs41_create_session_args args = {
8601 .clientid = clp->cl_clientid,
8602 .seqid = clp->cl_seqid,
8603 .cb_program = NFS4_CALLBACK,
8605 struct nfs41_create_session_res res;
8607 struct rpc_message msg = {
8608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8615 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8616 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8618 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8619 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8620 trace_nfs4_create_session(clp, status);
8623 case -NFS4ERR_STALE_CLIENTID:
8624 case -NFS4ERR_DELAY:
8633 /* Verify the session's negotiated channel_attrs values */
8634 status = nfs4_verify_channel_attrs(&args, &res);
8635 /* Increment the clientid slot sequence id */
8638 nfs4_update_session(session, &res);
8645 * Issues a CREATE_SESSION operation to the server.
8646 * It is the responsibility of the caller to verify the session is
8647 * expired before calling this routine.
8649 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8653 struct nfs4_session *session = clp->cl_session;
8655 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8657 status = _nfs4_proc_create_session(clp, cred);
8661 /* Init or reset the session slot tables */
8662 status = nfs4_setup_session_slot_tables(session);
8663 dprintk("slot table setup returned %d\n", status);
8667 ptr = (unsigned *)&session->sess_id.data[0];
8668 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8669 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8671 dprintk("<-- %s\n", __func__);
8676 * Issue the over-the-wire RPC DESTROY_SESSION.
8677 * The caller must serialize access to this routine.
8679 int nfs4_proc_destroy_session(struct nfs4_session *session,
8680 const struct cred *cred)
8682 struct rpc_message msg = {
8683 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8684 .rpc_argp = session,
8689 dprintk("--> nfs4_proc_destroy_session\n");
8691 /* session is still being setup */
8692 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8695 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8696 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8697 trace_nfs4_destroy_session(session->clp, status);
8700 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8701 "Session has been destroyed regardless...\n", status);
8703 dprintk("<-- nfs4_proc_destroy_session\n");
8708 * Renew the cl_session lease.
8710 struct nfs4_sequence_data {
8711 struct nfs_client *clp;
8712 struct nfs4_sequence_args args;
8713 struct nfs4_sequence_res res;
8716 static void nfs41_sequence_release(void *data)
8718 struct nfs4_sequence_data *calldata = data;
8719 struct nfs_client *clp = calldata->clp;
8721 if (refcount_read(&clp->cl_count) > 1)
8722 nfs4_schedule_state_renewal(clp);
8723 nfs_put_client(clp);
8727 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8729 switch(task->tk_status) {
8730 case -NFS4ERR_DELAY:
8731 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8734 nfs4_schedule_lease_recovery(clp);
8739 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8741 struct nfs4_sequence_data *calldata = data;
8742 struct nfs_client *clp = calldata->clp;
8744 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8747 trace_nfs4_sequence(clp, task->tk_status);
8748 if (task->tk_status < 0) {
8749 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8750 if (refcount_read(&clp->cl_count) == 1)
8753 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8754 rpc_restart_call_prepare(task);
8758 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8760 dprintk("<-- %s\n", __func__);
8763 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8765 struct nfs4_sequence_data *calldata = data;
8766 struct nfs_client *clp = calldata->clp;
8767 struct nfs4_sequence_args *args;
8768 struct nfs4_sequence_res *res;
8770 args = task->tk_msg.rpc_argp;
8771 res = task->tk_msg.rpc_resp;
8773 nfs4_setup_sequence(clp, args, res, task);
8776 static const struct rpc_call_ops nfs41_sequence_ops = {
8777 .rpc_call_done = nfs41_sequence_call_done,
8778 .rpc_call_prepare = nfs41_sequence_prepare,
8779 .rpc_release = nfs41_sequence_release,
8782 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8783 const struct cred *cred,
8784 struct nfs4_slot *slot,
8787 struct nfs4_sequence_data *calldata;
8788 struct rpc_message msg = {
8789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8792 struct rpc_task_setup task_setup_data = {
8793 .rpc_client = clp->cl_rpcclient,
8794 .rpc_message = &msg,
8795 .callback_ops = &nfs41_sequence_ops,
8796 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8798 struct rpc_task *ret;
8800 ret = ERR_PTR(-EIO);
8801 if (!refcount_inc_not_zero(&clp->cl_count))
8804 ret = ERR_PTR(-ENOMEM);
8805 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8806 if (calldata == NULL)
8808 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
8809 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8810 msg.rpc_argp = &calldata->args;
8811 msg.rpc_resp = &calldata->res;
8812 calldata->clp = clp;
8813 task_setup_data.callback_data = calldata;
8815 ret = rpc_run_task(&task_setup_data);
8820 nfs_put_client(clp);
8822 nfs41_release_slot(slot);
8826 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
8828 struct rpc_task *task;
8831 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8833 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8835 ret = PTR_ERR(task);
8837 rpc_put_task_async(task);
8838 dprintk("<-- %s status=%d\n", __func__, ret);
8842 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
8844 struct rpc_task *task;
8847 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8849 ret = PTR_ERR(task);
8852 ret = rpc_wait_for_completion_task(task);
8854 ret = task->tk_status;
8857 dprintk("<-- %s status=%d\n", __func__, ret);
8861 struct nfs4_reclaim_complete_data {
8862 struct nfs_client *clp;
8863 struct nfs41_reclaim_complete_args arg;
8864 struct nfs41_reclaim_complete_res res;
8867 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8869 struct nfs4_reclaim_complete_data *calldata = data;
8871 nfs4_setup_sequence(calldata->clp,
8872 &calldata->arg.seq_args,
8873 &calldata->res.seq_res,
8877 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8879 switch(task->tk_status) {
8881 wake_up_all(&clp->cl_lock_waitq);
8883 case -NFS4ERR_COMPLETE_ALREADY:
8884 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8886 case -NFS4ERR_DELAY:
8887 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8889 case -NFS4ERR_RETRY_UNCACHED_REP:
8891 case -NFS4ERR_BADSESSION:
8892 case -NFS4ERR_DEADSESSION:
8893 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8896 nfs4_schedule_lease_recovery(clp);
8901 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8903 struct nfs4_reclaim_complete_data *calldata = data;
8904 struct nfs_client *clp = calldata->clp;
8905 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8907 dprintk("--> %s\n", __func__);
8908 if (!nfs41_sequence_done(task, res))
8911 trace_nfs4_reclaim_complete(clp, task->tk_status);
8912 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8913 rpc_restart_call_prepare(task);
8916 dprintk("<-- %s\n", __func__);
8919 static void nfs4_free_reclaim_complete_data(void *data)
8921 struct nfs4_reclaim_complete_data *calldata = data;
8926 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8927 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8928 .rpc_call_done = nfs4_reclaim_complete_done,
8929 .rpc_release = nfs4_free_reclaim_complete_data,
8933 * Issue a global reclaim complete.
8935 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8936 const struct cred *cred)
8938 struct nfs4_reclaim_complete_data *calldata;
8939 struct rpc_message msg = {
8940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8943 struct rpc_task_setup task_setup_data = {
8944 .rpc_client = clp->cl_rpcclient,
8945 .rpc_message = &msg,
8946 .callback_ops = &nfs4_reclaim_complete_call_ops,
8947 .flags = RPC_TASK_NO_ROUND_ROBIN,
8949 int status = -ENOMEM;
8951 dprintk("--> %s\n", __func__);
8952 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8953 if (calldata == NULL)
8955 calldata->clp = clp;
8956 calldata->arg.one_fs = 0;
8958 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
8959 msg.rpc_argp = &calldata->arg;
8960 msg.rpc_resp = &calldata->res;
8961 task_setup_data.callback_data = calldata;
8962 status = nfs4_call_sync_custom(&task_setup_data);
8964 dprintk("<-- %s status=%d\n", __func__, status);
8969 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8971 struct nfs4_layoutget *lgp = calldata;
8972 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8974 dprintk("--> %s\n", __func__);
8975 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8976 &lgp->res.seq_res, task);
8977 dprintk("<-- %s\n", __func__);
8980 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8982 struct nfs4_layoutget *lgp = calldata;
8984 dprintk("--> %s\n", __func__);
8985 nfs41_sequence_process(task, &lgp->res.seq_res);
8986 dprintk("<-- %s\n", __func__);
8990 nfs4_layoutget_handle_exception(struct rpc_task *task,
8991 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8993 struct inode *inode = lgp->args.inode;
8994 struct nfs_server *server = NFS_SERVER(inode);
8995 struct pnfs_layout_hdr *lo;
8996 int nfs4err = task->tk_status;
8997 int err, status = 0;
9000 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9002 nfs4_sequence_free_slot(&lgp->res.seq_res);
9009 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9010 * on the file. set tk_status to -ENODATA to tell upper layer to
9013 case -NFS4ERR_LAYOUTUNAVAILABLE:
9017 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9018 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9020 case -NFS4ERR_BADLAYOUT:
9021 status = -EOVERFLOW;
9024 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9025 * (or clients) writing to the same RAID stripe except when
9026 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9028 * Treat it like we would RECALLCONFLICT -- we retry for a little
9029 * while, and then eventually give up.
9031 case -NFS4ERR_LAYOUTTRYLATER:
9032 if (lgp->args.minlength == 0) {
9033 status = -EOVERFLOW;
9038 case -NFS4ERR_RECALLCONFLICT:
9039 status = -ERECALLCONFLICT;
9041 case -NFS4ERR_DELEG_REVOKED:
9042 case -NFS4ERR_ADMIN_REVOKED:
9043 case -NFS4ERR_EXPIRED:
9044 case -NFS4ERR_BAD_STATEID:
9045 exception->timeout = 0;
9046 spin_lock(&inode->i_lock);
9047 lo = NFS_I(inode)->layout;
9048 /* If the open stateid was bad, then recover it. */
9049 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9050 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9051 spin_unlock(&inode->i_lock);
9052 exception->state = lgp->args.ctx->state;
9053 exception->stateid = &lgp->args.stateid;
9058 * Mark the bad layout state as invalid, then retry
9060 pnfs_mark_layout_stateid_invalid(lo, &head);
9061 spin_unlock(&inode->i_lock);
9062 nfs_commit_inode(inode, 0);
9063 pnfs_free_lseg_list(&head);
9068 err = nfs4_handle_exception(server, nfs4err, exception);
9070 if (exception->retry)
9076 dprintk("<-- %s\n", __func__);
9080 size_t max_response_pages(struct nfs_server *server)
9082 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9083 return nfs_page_array_len(0, max_resp_sz);
9086 static void nfs4_layoutget_release(void *calldata)
9088 struct nfs4_layoutget *lgp = calldata;
9090 dprintk("--> %s\n", __func__);
9091 nfs4_sequence_free_slot(&lgp->res.seq_res);
9092 pnfs_layoutget_free(lgp);
9093 dprintk("<-- %s\n", __func__);
9096 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9097 .rpc_call_prepare = nfs4_layoutget_prepare,
9098 .rpc_call_done = nfs4_layoutget_done,
9099 .rpc_release = nfs4_layoutget_release,
9102 struct pnfs_layout_segment *
9103 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9105 struct inode *inode = lgp->args.inode;
9106 struct nfs_server *server = NFS_SERVER(inode);
9107 struct rpc_task *task;
9108 struct rpc_message msg = {
9109 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9110 .rpc_argp = &lgp->args,
9111 .rpc_resp = &lgp->res,
9112 .rpc_cred = lgp->cred,
9114 struct rpc_task_setup task_setup_data = {
9115 .rpc_client = server->client,
9116 .rpc_message = &msg,
9117 .callback_ops = &nfs4_layoutget_call_ops,
9118 .callback_data = lgp,
9119 .flags = RPC_TASK_ASYNC,
9121 struct pnfs_layout_segment *lseg = NULL;
9122 struct nfs4_exception exception = {
9124 .timeout = *timeout,
9128 dprintk("--> %s\n", __func__);
9130 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9131 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9133 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9135 task = rpc_run_task(&task_setup_data);
9137 return ERR_CAST(task);
9138 status = rpc_wait_for_completion_task(task);
9142 if (task->tk_status < 0) {
9143 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9144 *timeout = exception.timeout;
9145 } else if (lgp->res.layoutp->len == 0) {
9147 *timeout = nfs4_update_delay(&exception.timeout);
9149 lseg = pnfs_layout_process(lgp);
9151 trace_nfs4_layoutget(lgp->args.ctx,
9158 dprintk("<-- %s status=%d\n", __func__, status);
9160 return ERR_PTR(status);
9165 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9167 struct nfs4_layoutreturn *lrp = calldata;
9169 dprintk("--> %s\n", __func__);
9170 nfs4_setup_sequence(lrp->clp,
9171 &lrp->args.seq_args,
9174 if (!pnfs_layout_is_valid(lrp->args.layout))
9178 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9180 struct nfs4_layoutreturn *lrp = calldata;
9181 struct nfs_server *server;
9183 dprintk("--> %s\n", __func__);
9185 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9189 * Was there an RPC level error? Assume the call succeeded,
9190 * and that we need to release the layout
9192 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9193 lrp->res.lrs_present = 0;
9197 server = NFS_SERVER(lrp->args.inode);
9198 switch (task->tk_status) {
9199 case -NFS4ERR_OLD_STATEID:
9200 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9206 task->tk_status = 0;
9210 case -NFS4ERR_DELAY:
9211 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9215 dprintk("<-- %s\n", __func__);
9218 task->tk_status = 0;
9219 nfs4_sequence_free_slot(&lrp->res.seq_res);
9220 rpc_restart_call_prepare(task);
9223 static void nfs4_layoutreturn_release(void *calldata)
9225 struct nfs4_layoutreturn *lrp = calldata;
9226 struct pnfs_layout_hdr *lo = lrp->args.layout;
9228 dprintk("--> %s\n", __func__);
9229 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9230 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9231 nfs4_sequence_free_slot(&lrp->res.seq_res);
9232 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9233 lrp->ld_private.ops->free(&lrp->ld_private);
9234 pnfs_put_layout_hdr(lrp->args.layout);
9235 nfs_iput_and_deactive(lrp->inode);
9237 dprintk("<-- %s\n", __func__);
9240 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9241 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9242 .rpc_call_done = nfs4_layoutreturn_done,
9243 .rpc_release = nfs4_layoutreturn_release,
9246 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9248 struct rpc_task *task;
9249 struct rpc_message msg = {
9250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9251 .rpc_argp = &lrp->args,
9252 .rpc_resp = &lrp->res,
9253 .rpc_cred = lrp->cred,
9255 struct rpc_task_setup task_setup_data = {
9256 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9257 .rpc_message = &msg,
9258 .callback_ops = &nfs4_layoutreturn_call_ops,
9259 .callback_data = lrp,
9263 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9264 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9265 &task_setup_data.rpc_client, &msg);
9267 dprintk("--> %s\n", __func__);
9269 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9271 nfs4_layoutreturn_release(lrp);
9274 task_setup_data.flags |= RPC_TASK_ASYNC;
9276 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9277 task = rpc_run_task(&task_setup_data);
9279 return PTR_ERR(task);
9281 status = task->tk_status;
9282 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9283 dprintk("<-- %s status=%d\n", __func__, status);
9289 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9290 struct pnfs_device *pdev,
9291 const struct cred *cred)
9293 struct nfs4_getdeviceinfo_args args = {
9295 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9296 NOTIFY_DEVICEID4_DELETE,
9298 struct nfs4_getdeviceinfo_res res = {
9301 struct rpc_message msg = {
9302 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9309 dprintk("--> %s\n", __func__);
9310 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9311 if (res.notification & ~args.notify_types)
9312 dprintk("%s: unsupported notification\n", __func__);
9313 if (res.notification != args.notify_types)
9316 dprintk("<-- %s status=%d\n", __func__, status);
9321 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9322 struct pnfs_device *pdev,
9323 const struct cred *cred)
9325 struct nfs4_exception exception = { };
9329 err = nfs4_handle_exception(server,
9330 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9332 } while (exception.retry);
9335 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9337 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9339 struct nfs4_layoutcommit_data *data = calldata;
9340 struct nfs_server *server = NFS_SERVER(data->args.inode);
9342 nfs4_setup_sequence(server->nfs_client,
9343 &data->args.seq_args,
9349 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9351 struct nfs4_layoutcommit_data *data = calldata;
9352 struct nfs_server *server = NFS_SERVER(data->args.inode);
9354 if (!nfs41_sequence_done(task, &data->res.seq_res))
9357 switch (task->tk_status) { /* Just ignore these failures */
9358 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9359 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9360 case -NFS4ERR_BADLAYOUT: /* no layout */
9361 case -NFS4ERR_GRACE: /* loca_recalim always false */
9362 task->tk_status = 0;
9366 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9367 rpc_restart_call_prepare(task);
9373 static void nfs4_layoutcommit_release(void *calldata)
9375 struct nfs4_layoutcommit_data *data = calldata;
9377 pnfs_cleanup_layoutcommit(data);
9378 nfs_post_op_update_inode_force_wcc(data->args.inode,
9380 put_cred(data->cred);
9381 nfs_iput_and_deactive(data->inode);
9385 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9386 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9387 .rpc_call_done = nfs4_layoutcommit_done,
9388 .rpc_release = nfs4_layoutcommit_release,
9392 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9394 struct rpc_message msg = {
9395 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9396 .rpc_argp = &data->args,
9397 .rpc_resp = &data->res,
9398 .rpc_cred = data->cred,
9400 struct rpc_task_setup task_setup_data = {
9401 .task = &data->task,
9402 .rpc_client = NFS_CLIENT(data->args.inode),
9403 .rpc_message = &msg,
9404 .callback_ops = &nfs4_layoutcommit_ops,
9405 .callback_data = data,
9407 struct rpc_task *task;
9410 dprintk("NFS: initiating layoutcommit call. sync %d "
9411 "lbw: %llu inode %lu\n", sync,
9412 data->args.lastbytewritten,
9413 data->args.inode->i_ino);
9416 data->inode = nfs_igrab_and_active(data->args.inode);
9417 if (data->inode == NULL) {
9418 nfs4_layoutcommit_release(data);
9421 task_setup_data.flags = RPC_TASK_ASYNC;
9423 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9424 task = rpc_run_task(&task_setup_data);
9426 return PTR_ERR(task);
9428 status = task->tk_status;
9429 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9430 dprintk("%s: status %d\n", __func__, status);
9436 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9437 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9440 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9441 struct nfs_fsinfo *info,
9442 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9444 struct nfs41_secinfo_no_name_args args = {
9445 .style = SECINFO_STYLE_CURRENT_FH,
9447 struct nfs4_secinfo_res res = {
9450 struct rpc_message msg = {
9451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9455 struct rpc_clnt *clnt = server->client;
9456 struct nfs4_call_sync_data data = {
9457 .seq_server = server,
9458 .seq_args = &args.seq_args,
9459 .seq_res = &res.seq_res,
9461 struct rpc_task_setup task_setup = {
9462 .rpc_client = server->client,
9463 .rpc_message = &msg,
9464 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9465 .callback_data = &data,
9466 .flags = RPC_TASK_NO_ROUND_ROBIN,
9468 const struct cred *cred = NULL;
9471 if (use_integrity) {
9472 clnt = server->nfs_client->cl_rpcclient;
9473 task_setup.rpc_client = clnt;
9475 cred = nfs4_get_clid_cred(server->nfs_client);
9476 msg.rpc_cred = cred;
9479 dprintk("--> %s\n", __func__);
9480 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9481 status = nfs4_call_sync_custom(&task_setup);
9482 dprintk("<-- %s status=%d\n", __func__, status);
9490 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9491 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9493 struct nfs4_exception exception = {
9494 .interruptible = true,
9498 /* first try using integrity protection */
9499 err = -NFS4ERR_WRONGSEC;
9501 /* try to use integrity protection with machine cred */
9502 if (_nfs4_is_integrity_protected(server->nfs_client))
9503 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9507 * if unable to use integrity protection, or SECINFO with
9508 * integrity protection returns NFS4ERR_WRONGSEC (which is
9509 * disallowed by spec, but exists in deployed servers) use
9510 * the current filesystem's rpc_client and the user cred.
9512 if (err == -NFS4ERR_WRONGSEC)
9513 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9518 case -NFS4ERR_WRONGSEC:
9522 err = nfs4_handle_exception(server, err, &exception);
9524 } while (exception.retry);
9530 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9531 struct nfs_fsinfo *info)
9535 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9536 struct nfs4_secinfo_flavors *flavors;
9537 struct nfs4_secinfo4 *secinfo;
9540 page = alloc_page(GFP_KERNEL);
9546 flavors = page_address(page);
9547 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9550 * Fall back on "guess and check" method if
9551 * the server doesn't support SECINFO_NO_NAME
9553 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9554 err = nfs4_find_root_sec(server, fhandle, info);
9560 for (i = 0; i < flavors->num_flavors; i++) {
9561 secinfo = &flavors->flavors[i];
9563 switch (secinfo->flavor) {
9567 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9568 &secinfo->flavor_info);
9571 flavor = RPC_AUTH_MAXFLAVOR;
9575 if (!nfs_auth_info_match(&server->auth_info, flavor))
9576 flavor = RPC_AUTH_MAXFLAVOR;
9578 if (flavor != RPC_AUTH_MAXFLAVOR) {
9579 err = nfs4_lookup_root_sec(server, fhandle,
9586 if (flavor == RPC_AUTH_MAXFLAVOR)
9597 static int _nfs41_test_stateid(struct nfs_server *server,
9598 nfs4_stateid *stateid,
9599 const struct cred *cred)
9602 struct nfs41_test_stateid_args args = {
9605 struct nfs41_test_stateid_res res;
9606 struct rpc_message msg = {
9607 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9612 struct rpc_clnt *rpc_client = server->client;
9614 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9617 dprintk("NFS call test_stateid %p\n", stateid);
9618 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9619 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9620 &args.seq_args, &res.seq_res);
9621 if (status != NFS_OK) {
9622 dprintk("NFS reply test_stateid: failed, %d\n", status);
9625 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9629 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9630 int err, struct nfs4_exception *exception)
9632 exception->retry = 0;
9634 case -NFS4ERR_DELAY:
9635 case -NFS4ERR_RETRY_UNCACHED_REP:
9636 nfs4_handle_exception(server, err, exception);
9638 case -NFS4ERR_BADSESSION:
9639 case -NFS4ERR_BADSLOT:
9640 case -NFS4ERR_BAD_HIGH_SLOT:
9641 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9642 case -NFS4ERR_DEADSESSION:
9643 nfs4_do_handle_exception(server, err, exception);
9648 * nfs41_test_stateid - perform a TEST_STATEID operation
9650 * @server: server / transport on which to perform the operation
9651 * @stateid: state ID to test
9654 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9655 * Otherwise a negative NFS4ERR value is returned if the operation
9656 * failed or the state ID is not currently valid.
9658 static int nfs41_test_stateid(struct nfs_server *server,
9659 nfs4_stateid *stateid,
9660 const struct cred *cred)
9662 struct nfs4_exception exception = {
9663 .interruptible = true,
9667 err = _nfs41_test_stateid(server, stateid, cred);
9668 nfs4_handle_delay_or_session_error(server, err, &exception);
9669 } while (exception.retry);
9673 struct nfs_free_stateid_data {
9674 struct nfs_server *server;
9675 struct nfs41_free_stateid_args args;
9676 struct nfs41_free_stateid_res res;
9679 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9681 struct nfs_free_stateid_data *data = calldata;
9682 nfs4_setup_sequence(data->server->nfs_client,
9683 &data->args.seq_args,
9688 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9690 struct nfs_free_stateid_data *data = calldata;
9692 nfs41_sequence_done(task, &data->res.seq_res);
9694 switch (task->tk_status) {
9695 case -NFS4ERR_DELAY:
9696 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9697 rpc_restart_call_prepare(task);
9701 static void nfs41_free_stateid_release(void *calldata)
9706 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9707 .rpc_call_prepare = nfs41_free_stateid_prepare,
9708 .rpc_call_done = nfs41_free_stateid_done,
9709 .rpc_release = nfs41_free_stateid_release,
9713 * nfs41_free_stateid - perform a FREE_STATEID operation
9715 * @server: server / transport on which to perform the operation
9716 * @stateid: state ID to release
9718 * @privileged: set to true if this call needs to be privileged
9720 * Note: this function is always asynchronous.
9722 static int nfs41_free_stateid(struct nfs_server *server,
9723 const nfs4_stateid *stateid,
9724 const struct cred *cred,
9727 struct rpc_message msg = {
9728 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9731 struct rpc_task_setup task_setup = {
9732 .rpc_client = server->client,
9733 .rpc_message = &msg,
9734 .callback_ops = &nfs41_free_stateid_ops,
9735 .flags = RPC_TASK_ASYNC,
9737 struct nfs_free_stateid_data *data;
9738 struct rpc_task *task;
9740 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9741 &task_setup.rpc_client, &msg);
9743 dprintk("NFS call free_stateid %p\n", stateid);
9744 data = kmalloc(sizeof(*data), GFP_NOFS);
9747 data->server = server;
9748 nfs4_stateid_copy(&data->args.stateid, stateid);
9750 task_setup.callback_data = data;
9752 msg.rpc_argp = &data->args;
9753 msg.rpc_resp = &data->res;
9754 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
9755 task = rpc_run_task(&task_setup);
9757 return PTR_ERR(task);
9763 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9765 const struct cred *cred = lsp->ls_state->owner->so_cred;
9767 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9768 nfs4_free_lock_state(server, lsp);
9771 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9772 const nfs4_stateid *s2)
9774 if (s1->type != s2->type)
9777 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9780 if (s1->seqid == s2->seqid)
9783 return s1->seqid == 0 || s2->seqid == 0;
9786 #endif /* CONFIG_NFS_V4_1 */
9788 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9789 const nfs4_stateid *s2)
9791 return nfs4_stateid_match(s1, s2);
9795 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9796 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9797 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9798 .recover_open = nfs4_open_reclaim,
9799 .recover_lock = nfs4_lock_reclaim,
9800 .establish_clid = nfs4_init_clientid,
9801 .detect_trunking = nfs40_discover_server_trunking,
9804 #if defined(CONFIG_NFS_V4_1)
9805 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9806 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9807 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9808 .recover_open = nfs4_open_reclaim,
9809 .recover_lock = nfs4_lock_reclaim,
9810 .establish_clid = nfs41_init_clientid,
9811 .reclaim_complete = nfs41_proc_reclaim_complete,
9812 .detect_trunking = nfs41_discover_server_trunking,
9814 #endif /* CONFIG_NFS_V4_1 */
9816 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9817 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9818 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9819 .recover_open = nfs40_open_expired,
9820 .recover_lock = nfs4_lock_expired,
9821 .establish_clid = nfs4_init_clientid,
9824 #if defined(CONFIG_NFS_V4_1)
9825 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9826 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9827 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9828 .recover_open = nfs41_open_expired,
9829 .recover_lock = nfs41_lock_expired,
9830 .establish_clid = nfs41_init_clientid,
9832 #endif /* CONFIG_NFS_V4_1 */
9834 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9835 .sched_state_renewal = nfs4_proc_async_renew,
9836 .get_state_renewal_cred = nfs4_get_renew_cred,
9837 .renew_lease = nfs4_proc_renew,
9840 #if defined(CONFIG_NFS_V4_1)
9841 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9842 .sched_state_renewal = nfs41_proc_async_sequence,
9843 .get_state_renewal_cred = nfs4_get_machine_cred,
9844 .renew_lease = nfs4_proc_sequence,
9848 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9849 .get_locations = _nfs40_proc_get_locations,
9850 .fsid_present = _nfs40_proc_fsid_present,
9853 #if defined(CONFIG_NFS_V4_1)
9854 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9855 .get_locations = _nfs41_proc_get_locations,
9856 .fsid_present = _nfs41_proc_fsid_present,
9858 #endif /* CONFIG_NFS_V4_1 */
9860 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9862 .init_caps = NFS_CAP_READDIRPLUS
9863 | NFS_CAP_ATOMIC_OPEN
9864 | NFS_CAP_POSIX_LOCK,
9865 .init_client = nfs40_init_client,
9866 .shutdown_client = nfs40_shutdown_client,
9867 .match_stateid = nfs4_match_stateid,
9868 .find_root_sec = nfs4_find_root_sec,
9869 .free_lock_state = nfs4_release_lockowner,
9870 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9871 .alloc_seqid = nfs_alloc_seqid,
9872 .call_sync_ops = &nfs40_call_sync_ops,
9873 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9874 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9875 .state_renewal_ops = &nfs40_state_renewal_ops,
9876 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9879 #if defined(CONFIG_NFS_V4_1)
9880 static struct nfs_seqid *
9881 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9886 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9888 .init_caps = NFS_CAP_READDIRPLUS
9889 | NFS_CAP_ATOMIC_OPEN
9890 | NFS_CAP_POSIX_LOCK
9891 | NFS_CAP_STATEID_NFSV41
9892 | NFS_CAP_ATOMIC_OPEN_V1
9894 .init_client = nfs41_init_client,
9895 .shutdown_client = nfs41_shutdown_client,
9896 .match_stateid = nfs41_match_stateid,
9897 .find_root_sec = nfs41_find_root_sec,
9898 .free_lock_state = nfs41_free_lock_state,
9899 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9900 .alloc_seqid = nfs_alloc_no_seqid,
9901 .session_trunk = nfs4_test_session_trunk,
9902 .call_sync_ops = &nfs41_call_sync_ops,
9903 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9904 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9905 .state_renewal_ops = &nfs41_state_renewal_ops,
9906 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9910 #if defined(CONFIG_NFS_V4_2)
9911 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9913 .init_caps = NFS_CAP_READDIRPLUS
9914 | NFS_CAP_ATOMIC_OPEN
9915 | NFS_CAP_POSIX_LOCK
9916 | NFS_CAP_STATEID_NFSV41
9917 | NFS_CAP_ATOMIC_OPEN_V1
9921 | NFS_CAP_OFFLOAD_CANCEL
9922 | NFS_CAP_COPY_NOTIFY
9923 | NFS_CAP_DEALLOCATE
9925 | NFS_CAP_LAYOUTSTATS
9927 | NFS_CAP_LAYOUTERROR,
9928 .init_client = nfs41_init_client,
9929 .shutdown_client = nfs41_shutdown_client,
9930 .match_stateid = nfs41_match_stateid,
9931 .find_root_sec = nfs41_find_root_sec,
9932 .free_lock_state = nfs41_free_lock_state,
9933 .call_sync_ops = &nfs41_call_sync_ops,
9934 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9935 .alloc_seqid = nfs_alloc_no_seqid,
9936 .session_trunk = nfs4_test_session_trunk,
9937 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9938 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9939 .state_renewal_ops = &nfs41_state_renewal_ops,
9940 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9944 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9945 [0] = &nfs_v4_0_minor_ops,
9946 #if defined(CONFIG_NFS_V4_1)
9947 [1] = &nfs_v4_1_minor_ops,
9949 #if defined(CONFIG_NFS_V4_2)
9950 [2] = &nfs_v4_2_minor_ops,
9954 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9956 ssize_t error, error2;
9958 error = generic_listxattr(dentry, list, size);
9966 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9969 return error + error2;
9972 static const struct inode_operations nfs4_dir_inode_operations = {
9973 .create = nfs_create,
9974 .lookup = nfs_lookup,
9975 .atomic_open = nfs_atomic_open,
9977 .unlink = nfs_unlink,
9978 .symlink = nfs_symlink,
9982 .rename = nfs_rename,
9983 .permission = nfs_permission,
9984 .getattr = nfs_getattr,
9985 .setattr = nfs_setattr,
9986 .listxattr = nfs4_listxattr,
9989 static const struct inode_operations nfs4_file_inode_operations = {
9990 .permission = nfs_permission,
9991 .getattr = nfs_getattr,
9992 .setattr = nfs_setattr,
9993 .listxattr = nfs4_listxattr,
9996 const struct nfs_rpc_ops nfs_v4_clientops = {
9997 .version = 4, /* protocol version */
9998 .dentry_ops = &nfs4_dentry_operations,
9999 .dir_inode_ops = &nfs4_dir_inode_operations,
10000 .file_inode_ops = &nfs4_file_inode_operations,
10001 .file_ops = &nfs4_file_operations,
10002 .getroot = nfs4_proc_get_root,
10003 .submount = nfs4_submount,
10004 .try_mount = nfs4_try_mount,
10005 .getattr = nfs4_proc_getattr,
10006 .setattr = nfs4_proc_setattr,
10007 .lookup = nfs4_proc_lookup,
10008 .lookupp = nfs4_proc_lookupp,
10009 .access = nfs4_proc_access,
10010 .readlink = nfs4_proc_readlink,
10011 .create = nfs4_proc_create,
10012 .remove = nfs4_proc_remove,
10013 .unlink_setup = nfs4_proc_unlink_setup,
10014 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10015 .unlink_done = nfs4_proc_unlink_done,
10016 .rename_setup = nfs4_proc_rename_setup,
10017 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10018 .rename_done = nfs4_proc_rename_done,
10019 .link = nfs4_proc_link,
10020 .symlink = nfs4_proc_symlink,
10021 .mkdir = nfs4_proc_mkdir,
10022 .rmdir = nfs4_proc_rmdir,
10023 .readdir = nfs4_proc_readdir,
10024 .mknod = nfs4_proc_mknod,
10025 .statfs = nfs4_proc_statfs,
10026 .fsinfo = nfs4_proc_fsinfo,
10027 .pathconf = nfs4_proc_pathconf,
10028 .set_capabilities = nfs4_server_capabilities,
10029 .decode_dirent = nfs4_decode_dirent,
10030 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10031 .read_setup = nfs4_proc_read_setup,
10032 .read_done = nfs4_read_done,
10033 .write_setup = nfs4_proc_write_setup,
10034 .write_done = nfs4_write_done,
10035 .commit_setup = nfs4_proc_commit_setup,
10036 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10037 .commit_done = nfs4_commit_done,
10038 .lock = nfs4_proc_lock,
10039 .clear_acl_cache = nfs4_zap_acl_attr,
10040 .close_context = nfs4_close_context,
10041 .open_context = nfs4_atomic_open,
10042 .have_delegation = nfs4_have_delegation,
10043 .alloc_client = nfs4_alloc_client,
10044 .init_client = nfs4_init_client,
10045 .free_client = nfs4_free_client,
10046 .create_server = nfs4_create_server,
10047 .clone_server = nfs_clone_server,
10050 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10051 .name = XATTR_NAME_NFSV4_ACL,
10052 .list = nfs4_xattr_list_nfs4_acl,
10053 .get = nfs4_xattr_get_nfs4_acl,
10054 .set = nfs4_xattr_set_nfs4_acl,
10057 const struct xattr_handler *nfs4_xattr_handlers[] = {
10058 &nfs4_xattr_nfs4_acl_handler,
10059 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10060 &nfs4_xattr_nfs4_label_handler,
10067 * c-basic-offset: 8