4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
57 #include <linux/iversion.h>
60 #include "delegation.h"
66 #include "nfs4idmap.h"
67 #include "nfs4session.h"
70 #include "nfs4trace.h"
72 #define NFSDBG_FACILITY NFSDBG_PROC
74 #define NFS4_BITMASK_SZ 3
76 #define NFS4_POLL_RETRY_MIN (HZ/10)
77 #define NFS4_POLL_RETRY_MAX (15*HZ)
79 /* file attributes which can be mapped to nfs attributes */
80 #define NFS4_VALID_ATTRS (ATTR_MODE \
91 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
92 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
93 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
94 static int _nfs4_proc_getattr(struct nfs_server *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 void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
778 struct nfs4_slot *slot)
780 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
782 rpc_put_task_async(task);
785 static int nfs41_sequence_process(struct rpc_task *task,
786 struct nfs4_sequence_res *res)
788 struct nfs4_session *session;
789 struct nfs4_slot *slot = res->sr_slot;
790 struct nfs_client *clp;
796 /* don't increment the sequence number if the task wasn't sent */
797 if (!RPC_WAS_SENT(task) || slot->seq_done)
800 session = slot->table->session;
803 trace_nfs4_sequence_done(session, res);
805 status = res->sr_status;
806 if (task->tk_status == -NFS4ERR_DEADSESSION)
807 status = -NFS4ERR_DEADSESSION;
809 /* Check the SEQUENCE operation status */
812 /* Mark this sequence number as having been acked */
813 nfs4_slot_sequence_acked(slot, slot->seq_nr);
814 /* Update the slot's sequence and clientid lease timer */
816 do_renew_lease(clp, res->sr_timestamp);
817 /* Check sequence flags */
818 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
820 nfs41_update_target_slotid(slot->table, slot, res);
824 * sr_status remains 1 if an RPC level error occurred.
825 * The server may or may not have processed the sequence
828 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
832 /* The server detected a resend of the RPC call and
833 * returned NFS4ERR_DELAY as per Section 2.10.6.2
836 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
840 nfs4_slot_sequence_acked(slot, slot->seq_nr);
842 case -NFS4ERR_RETRY_UNCACHED_REP:
843 case -NFS4ERR_SEQ_FALSE_RETRY:
845 * The server thinks we tried to replay a request.
846 * Retry the call after bumping the sequence ID.
848 nfs4_slot_sequence_acked(slot, slot->seq_nr);
850 case -NFS4ERR_BADSLOT:
852 * The slot id we used was probably retired. Try again
853 * using a different slot id.
855 if (slot->slot_nr < slot->table->target_highest_slotid)
856 goto session_recover;
858 case -NFS4ERR_SEQ_MISORDERED:
859 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
861 * Were one or more calls using this slot interrupted?
862 * If the server never received the request, then our
863 * transmitted slot sequence number may be too high. However,
864 * if the server did receive the request then it might
865 * accidentally give us a reply with a mismatched operation.
866 * We can sort this out by sending a lone sequence operation
867 * to the server on the same slot.
869 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
871 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
872 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
879 * A retry might be sent while the original request is
880 * still in progress on the replier. The replier SHOULD
881 * deal with the issue by returning NFS4ERR_DELAY as the
882 * reply to SEQUENCE or CB_SEQUENCE operation, but
883 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
885 * Restart the search after a delay.
887 slot->seq_nr = slot->seq_nr_highest_sent;
889 case -NFS4ERR_BADSESSION:
890 case -NFS4ERR_DEADSESSION:
891 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
892 goto session_recover;
894 /* Just update the slot sequence no. */
898 /* The session may be reset by one of the error handlers. */
899 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
903 nfs4_schedule_session_recovery(session, status);
904 dprintk("%s ERROR: %d Reset session\n", __func__, status);
905 nfs41_sequence_free_slot(res);
910 if (rpc_restart_call_prepare(task)) {
911 nfs41_sequence_free_slot(res);
917 if (!rpc_restart_call(task))
919 rpc_delay(task, NFS4_POLL_RETRY_MAX);
923 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
925 if (!nfs41_sequence_process(task, res))
927 if (res->sr_slot != NULL)
928 nfs41_sequence_free_slot(res);
932 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
934 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
936 if (res->sr_slot == NULL)
938 if (res->sr_slot->table->session != NULL)
939 return nfs41_sequence_process(task, res);
940 return nfs40_sequence_done(task, res);
943 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
945 if (res->sr_slot != NULL) {
946 if (res->sr_slot->table->session != NULL)
947 nfs41_sequence_free_slot(res);
949 nfs40_sequence_free_slot(res);
953 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
955 if (res->sr_slot == NULL)
957 if (!res->sr_slot->table->session)
958 return nfs40_sequence_done(task, res);
959 return nfs41_sequence_done(task, res);
961 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
963 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
965 struct nfs4_call_sync_data *data = calldata;
967 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
969 nfs4_setup_sequence(data->seq_server->nfs_client,
970 data->seq_args, data->seq_res, task);
973 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
975 struct nfs4_call_sync_data *data = calldata;
977 nfs41_sequence_done(task, data->seq_res);
980 static const struct rpc_call_ops nfs41_call_sync_ops = {
981 .rpc_call_prepare = nfs41_call_sync_prepare,
982 .rpc_call_done = nfs41_call_sync_done,
985 #else /* !CONFIG_NFS_V4_1 */
987 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
989 return nfs40_sequence_done(task, res);
992 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
994 if (res->sr_slot != NULL)
995 nfs40_sequence_free_slot(res);
998 int nfs4_sequence_done(struct rpc_task *task,
999 struct nfs4_sequence_res *res)
1001 return nfs40_sequence_done(task, res);
1003 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1005 #endif /* !CONFIG_NFS_V4_1 */
1007 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1009 res->sr_timestamp = jiffies;
1010 res->sr_status_flags = 0;
1015 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1016 struct nfs4_sequence_res *res,
1017 struct nfs4_slot *slot)
1021 slot->privileged = args->sa_privileged ? 1 : 0;
1022 args->sa_slot = slot;
1024 res->sr_slot = slot;
1027 int nfs4_setup_sequence(struct nfs_client *client,
1028 struct nfs4_sequence_args *args,
1029 struct nfs4_sequence_res *res,
1030 struct rpc_task *task)
1032 struct nfs4_session *session = nfs4_get_session(client);
1033 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1034 struct nfs4_slot *slot;
1036 /* slot already allocated? */
1037 if (res->sr_slot != NULL)
1041 tbl = &session->fc_slot_table;
1043 spin_lock(&tbl->slot_tbl_lock);
1044 /* The state manager will wait until the slot table is empty */
1045 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1048 slot = nfs4_alloc_slot(tbl);
1050 if (slot == ERR_PTR(-ENOMEM))
1051 goto out_sleep_timeout;
1054 spin_unlock(&tbl->slot_tbl_lock);
1056 nfs4_sequence_attach_slot(args, res, slot);
1058 trace_nfs4_setup_sequence(session, args);
1060 nfs41_sequence_res_init(res);
1061 rpc_call_start(task);
1064 /* Try again in 1/4 second */
1065 if (args->sa_privileged)
1066 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1067 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1069 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1070 NULL, jiffies + (HZ >> 2));
1071 spin_unlock(&tbl->slot_tbl_lock);
1074 if (args->sa_privileged)
1075 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1076 RPC_PRIORITY_PRIVILEGED);
1078 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1079 spin_unlock(&tbl->slot_tbl_lock);
1082 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1084 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1086 struct nfs4_call_sync_data *data = calldata;
1087 nfs4_setup_sequence(data->seq_server->nfs_client,
1088 data->seq_args, data->seq_res, task);
1091 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1093 struct nfs4_call_sync_data *data = calldata;
1094 nfs4_sequence_done(task, data->seq_res);
1097 static const struct rpc_call_ops nfs40_call_sync_ops = {
1098 .rpc_call_prepare = nfs40_call_sync_prepare,
1099 .rpc_call_done = nfs40_call_sync_done,
1102 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1105 struct rpc_task *task;
1107 task = rpc_run_task(task_setup);
1109 return PTR_ERR(task);
1111 ret = task->tk_status;
1116 static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1117 struct nfs_server *server,
1118 struct rpc_message *msg,
1119 struct nfs4_sequence_args *args,
1120 struct nfs4_sequence_res *res,
1121 unsigned short task_flags)
1123 struct nfs_client *clp = server->nfs_client;
1124 struct nfs4_call_sync_data data = {
1125 .seq_server = server,
1129 struct rpc_task_setup task_setup = {
1132 .callback_ops = clp->cl_mvops->call_sync_ops,
1133 .callback_data = &data,
1134 .flags = task_flags,
1137 return nfs4_call_sync_custom(&task_setup);
1140 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1141 struct nfs_server *server,
1142 struct rpc_message *msg,
1143 struct nfs4_sequence_args *args,
1144 struct nfs4_sequence_res *res)
1146 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1150 int nfs4_call_sync(struct rpc_clnt *clnt,
1151 struct nfs_server *server,
1152 struct rpc_message *msg,
1153 struct nfs4_sequence_args *args,
1154 struct nfs4_sequence_res *res,
1157 nfs4_init_sequence(args, res, cache_reply, 0);
1158 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1162 nfs4_inc_nlink_locked(struct inode *inode)
1164 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1169 nfs4_dec_nlink_locked(struct inode *inode)
1171 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1176 update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
1177 unsigned long timestamp, unsigned long cache_validity)
1179 struct nfs_inode *nfsi = NFS_I(dir);
1181 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1182 | NFS_INO_INVALID_MTIME
1183 | NFS_INO_INVALID_DATA
1185 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
1186 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1187 nfsi->attrtimeo_timestamp = jiffies;
1189 nfs_force_lookup_revalidate(dir);
1190 if (cinfo->before != inode_peek_iversion_raw(dir))
1191 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1192 NFS_INO_INVALID_ACL;
1194 inode_set_iversion_raw(dir, cinfo->after);
1195 nfsi->read_cache_jiffies = timestamp;
1196 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1197 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1198 nfs_fscache_invalidate(dir);
1202 update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1203 unsigned long timestamp, unsigned long cache_validity)
1205 spin_lock(&dir->i_lock);
1206 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1207 spin_unlock(&dir->i_lock);
1210 struct nfs4_open_createattrs {
1211 struct nfs4_label *label;
1212 struct iattr *sattr;
1213 const __u32 verf[2];
1216 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1217 int err, struct nfs4_exception *exception)
1221 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1223 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1224 exception->retry = 1;
1228 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1230 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1233 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1235 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1237 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1241 nfs4_map_atomic_open_share(struct nfs_server *server,
1242 fmode_t fmode, int openflags)
1246 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1248 res = NFS4_SHARE_ACCESS_READ;
1251 res = NFS4_SHARE_ACCESS_WRITE;
1253 case FMODE_READ|FMODE_WRITE:
1254 res = NFS4_SHARE_ACCESS_BOTH;
1256 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1258 /* Want no delegation if we're using O_DIRECT */
1259 if (openflags & O_DIRECT)
1260 res |= NFS4_SHARE_WANT_NO_DELEG;
1265 static enum open_claim_type4
1266 nfs4_map_atomic_open_claim(struct nfs_server *server,
1267 enum open_claim_type4 claim)
1269 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1274 case NFS4_OPEN_CLAIM_FH:
1275 return NFS4_OPEN_CLAIM_NULL;
1276 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1277 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1278 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1279 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1283 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1285 p->o_res.f_attr = &p->f_attr;
1286 p->o_res.f_label = p->f_label;
1287 p->o_res.seqid = p->o_arg.seqid;
1288 p->c_res.seqid = p->c_arg.seqid;
1289 p->o_res.server = p->o_arg.server;
1290 p->o_res.access_request = p->o_arg.access;
1291 nfs_fattr_init(&p->f_attr);
1292 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1295 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1296 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1297 const struct nfs4_open_createattrs *c,
1298 enum open_claim_type4 claim,
1301 struct dentry *parent = dget_parent(dentry);
1302 struct inode *dir = d_inode(parent);
1303 struct nfs_server *server = NFS_SERVER(dir);
1304 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1305 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1306 struct nfs4_opendata *p;
1308 p = kzalloc(sizeof(*p), gfp_mask);
1312 p->f_label = nfs4_label_alloc(server, gfp_mask);
1313 if (IS_ERR(p->f_label))
1316 p->a_label = nfs4_label_alloc(server, gfp_mask);
1317 if (IS_ERR(p->a_label))
1320 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1321 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1322 if (IS_ERR(p->o_arg.seqid))
1323 goto err_free_label;
1324 nfs_sb_active(dentry->d_sb);
1325 p->dentry = dget(dentry);
1328 atomic_inc(&sp->so_count);
1329 p->o_arg.open_flags = flags;
1330 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1331 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1332 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1334 if (flags & O_CREAT) {
1335 p->o_arg.umask = current_umask();
1336 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1337 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1338 p->o_arg.u.attrs = &p->attrs;
1339 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1341 memcpy(p->o_arg.u.verifier.data, c->verf,
1342 sizeof(p->o_arg.u.verifier.data));
1345 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1346 * will return permission denied for all bits until close */
1347 if (!(flags & O_EXCL)) {
1348 /* ask server to check for all possible rights as results
1350 switch (p->o_arg.claim) {
1353 case NFS4_OPEN_CLAIM_NULL:
1354 case NFS4_OPEN_CLAIM_FH:
1355 p->o_arg.access = NFS4_ACCESS_READ |
1356 NFS4_ACCESS_MODIFY |
1357 NFS4_ACCESS_EXTEND |
1358 NFS4_ACCESS_EXECUTE;
1361 p->o_arg.clientid = server->nfs_client->cl_clientid;
1362 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1363 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1364 p->o_arg.name = &dentry->d_name;
1365 p->o_arg.server = server;
1366 p->o_arg.bitmask = nfs4_bitmask(server, label);
1367 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1368 switch (p->o_arg.claim) {
1369 case NFS4_OPEN_CLAIM_NULL:
1370 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1371 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1372 p->o_arg.fh = NFS_FH(dir);
1374 case NFS4_OPEN_CLAIM_PREVIOUS:
1375 case NFS4_OPEN_CLAIM_FH:
1376 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1377 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1378 p->o_arg.fh = NFS_FH(d_inode(dentry));
1380 p->c_arg.fh = &p->o_res.fh;
1381 p->c_arg.stateid = &p->o_res.stateid;
1382 p->c_arg.seqid = p->o_arg.seqid;
1383 nfs4_init_opendata_res(p);
1384 kref_init(&p->kref);
1388 nfs4_label_free(p->a_label);
1390 nfs4_label_free(p->f_label);
1398 static void nfs4_opendata_free(struct kref *kref)
1400 struct nfs4_opendata *p = container_of(kref,
1401 struct nfs4_opendata, kref);
1402 struct super_block *sb = p->dentry->d_sb;
1404 nfs4_lgopen_release(p->lgp);
1405 nfs_free_seqid(p->o_arg.seqid);
1406 nfs4_sequence_free_slot(&p->o_res.seq_res);
1407 if (p->state != NULL)
1408 nfs4_put_open_state(p->state);
1409 nfs4_put_state_owner(p->owner);
1411 nfs4_label_free(p->a_label);
1412 nfs4_label_free(p->f_label);
1416 nfs_sb_deactive(sb);
1417 nfs_fattr_free_names(&p->f_attr);
1418 kfree(p->f_attr.mdsthreshold);
1422 static void nfs4_opendata_put(struct nfs4_opendata *p)
1425 kref_put(&p->kref, nfs4_opendata_free);
1428 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1431 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1432 case FMODE_READ|FMODE_WRITE:
1433 return state->n_rdwr != 0;
1435 return state->n_wronly != 0;
1437 return state->n_rdonly != 0;
1443 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1444 int open_mode, enum open_claim_type4 claim)
1448 if (open_mode & (O_EXCL|O_TRUNC))
1451 case NFS4_OPEN_CLAIM_NULL:
1452 case NFS4_OPEN_CLAIM_FH:
1457 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1459 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1460 && state->n_rdonly != 0;
1463 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1464 && state->n_wronly != 0;
1466 case FMODE_READ|FMODE_WRITE:
1467 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1468 && state->n_rdwr != 0;
1474 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1475 enum open_claim_type4 claim)
1477 if (delegation == NULL)
1479 if ((delegation->type & fmode) != fmode)
1482 case NFS4_OPEN_CLAIM_NULL:
1483 case NFS4_OPEN_CLAIM_FH:
1485 case NFS4_OPEN_CLAIM_PREVIOUS:
1486 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1492 nfs_mark_delegation_referenced(delegation);
1496 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1505 case FMODE_READ|FMODE_WRITE:
1508 nfs4_state_set_mode_locked(state, state->state | fmode);
1511 #ifdef CONFIG_NFS_V4_1
1512 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1514 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1516 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1518 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1522 #endif /* CONFIG_NFS_V4_1 */
1524 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1526 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1527 wake_up_all(&state->waitq);
1530 static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1531 const nfs4_stateid *stateid)
1533 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1534 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1536 if (stateid_seqid == state_seqid + 1U ||
1537 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1538 nfs_state_log_update_open_stateid(state);
1540 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1543 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1545 struct nfs_client *clp = state->owner->so_server->nfs_client;
1546 bool need_recover = false;
1548 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1549 need_recover = true;
1550 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1551 need_recover = true;
1552 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1553 need_recover = true;
1555 nfs4_state_mark_reclaim_nograce(clp, state);
1559 * Check for whether or not the caller may update the open stateid
1560 * to the value passed in by stateid.
1562 * Note: This function relies heavily on the server implementing
1563 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1565 * i.e. The stateid seqids have to be initialised to 1, and
1566 * are then incremented on every state transition.
1568 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1569 const nfs4_stateid *stateid)
1571 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1572 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1573 if (stateid->seqid == cpu_to_be32(1))
1574 nfs_state_log_update_open_stateid(state);
1576 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1580 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1581 nfs_state_log_out_of_order_open_stateid(state, stateid);
1587 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1589 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1591 if (state->n_wronly)
1592 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1593 if (state->n_rdonly)
1594 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1596 set_bit(NFS_O_RDWR_STATE, &state->flags);
1597 set_bit(NFS_OPEN_STATE, &state->flags);
1600 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1601 nfs4_stateid *stateid, fmode_t fmode)
1603 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1604 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1606 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1609 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1612 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1613 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1614 clear_bit(NFS_OPEN_STATE, &state->flags);
1616 if (stateid == NULL)
1618 /* Handle OPEN+OPEN_DOWNGRADE races */
1619 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1620 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1621 nfs_resync_open_stateid_locked(state);
1624 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1625 nfs4_stateid_copy(&state->stateid, stateid);
1626 nfs4_stateid_copy(&state->open_stateid, stateid);
1627 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1629 nfs_state_log_update_open_stateid(state);
1632 static void nfs_clear_open_stateid(struct nfs4_state *state,
1633 nfs4_stateid *arg_stateid,
1634 nfs4_stateid *stateid, fmode_t fmode)
1636 write_seqlock(&state->seqlock);
1637 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1638 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1639 nfs_clear_open_stateid_locked(state, stateid, fmode);
1640 write_sequnlock(&state->seqlock);
1641 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1642 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1645 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1646 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1647 __must_hold(&state->owner->so_lock)
1648 __must_hold(&state->seqlock)
1656 if (!nfs_need_update_open_stateid(state, stateid))
1658 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1662 /* Rely on seqids for serialisation with NFSv4.0 */
1663 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1666 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1668 * Ensure we process the state changes in the same order
1669 * in which the server processed them by delaying the
1670 * update of the stateid until we are in sequence.
1672 write_sequnlock(&state->seqlock);
1673 spin_unlock(&state->owner->so_lock);
1675 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1676 if (!signal_pending(current)) {
1677 if (schedule_timeout(5*HZ) == 0)
1683 finish_wait(&state->waitq, &wait);
1685 spin_lock(&state->owner->so_lock);
1686 write_seqlock(&state->seqlock);
1689 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1690 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1691 nfs4_stateid_copy(freeme, &state->open_stateid);
1692 nfs_test_and_clear_all_open_stateid(state);
1695 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1696 nfs4_stateid_copy(&state->stateid, stateid);
1697 nfs4_stateid_copy(&state->open_stateid, stateid);
1698 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1699 nfs_state_log_update_open_stateid(state);
1702 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1703 const nfs4_stateid *open_stateid,
1705 nfs4_stateid *freeme)
1708 * Protect the call to nfs4_state_set_mode_locked and
1709 * serialise the stateid update
1711 write_seqlock(&state->seqlock);
1712 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1715 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1718 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1720 case FMODE_READ|FMODE_WRITE:
1721 set_bit(NFS_O_RDWR_STATE, &state->flags);
1723 set_bit(NFS_OPEN_STATE, &state->flags);
1724 write_sequnlock(&state->seqlock);
1727 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1729 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1730 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1731 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1732 clear_bit(NFS_OPEN_STATE, &state->flags);
1735 static void nfs_state_set_delegation(struct nfs4_state *state,
1736 const nfs4_stateid *deleg_stateid,
1740 * Protect the call to nfs4_state_set_mode_locked and
1741 * serialise the stateid update
1743 write_seqlock(&state->seqlock);
1744 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1745 set_bit(NFS_DELEGATED_STATE, &state->flags);
1746 write_sequnlock(&state->seqlock);
1749 static void nfs_state_clear_delegation(struct nfs4_state *state)
1751 write_seqlock(&state->seqlock);
1752 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1753 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1754 write_sequnlock(&state->seqlock);
1757 int update_open_stateid(struct nfs4_state *state,
1758 const nfs4_stateid *open_stateid,
1759 const nfs4_stateid *delegation,
1762 struct nfs_server *server = NFS_SERVER(state->inode);
1763 struct nfs_client *clp = server->nfs_client;
1764 struct nfs_inode *nfsi = NFS_I(state->inode);
1765 struct nfs_delegation *deleg_cur;
1766 nfs4_stateid freeme = { };
1769 fmode &= (FMODE_READ|FMODE_WRITE);
1772 spin_lock(&state->owner->so_lock);
1773 if (open_stateid != NULL) {
1774 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1778 deleg_cur = nfs4_get_valid_delegation(state->inode);
1779 if (deleg_cur == NULL)
1782 spin_lock(&deleg_cur->lock);
1783 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1784 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1785 (deleg_cur->type & fmode) != fmode)
1786 goto no_delegation_unlock;
1788 if (delegation == NULL)
1789 delegation = &deleg_cur->stateid;
1790 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1791 goto no_delegation_unlock;
1793 nfs_mark_delegation_referenced(deleg_cur);
1794 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1796 no_delegation_unlock:
1797 spin_unlock(&deleg_cur->lock);
1800 update_open_stateflags(state, fmode);
1801 spin_unlock(&state->owner->so_lock);
1804 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1805 nfs4_schedule_state_manager(clp);
1806 if (freeme.type != 0)
1807 nfs4_test_and_free_stateid(server, &freeme,
1808 state->owner->so_cred);
1813 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1814 const nfs4_stateid *stateid)
1816 struct nfs4_state *state = lsp->ls_state;
1819 spin_lock(&state->state_lock);
1820 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1822 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1824 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1827 spin_unlock(&state->state_lock);
1831 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1833 struct nfs_delegation *delegation;
1835 fmode &= FMODE_READ|FMODE_WRITE;
1837 delegation = nfs4_get_valid_delegation(inode);
1838 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1843 nfs4_inode_return_delegation(inode);
1846 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1848 struct nfs4_state *state = opendata->state;
1849 struct nfs_delegation *delegation;
1850 int open_mode = opendata->o_arg.open_flags;
1851 fmode_t fmode = opendata->o_arg.fmode;
1852 enum open_claim_type4 claim = opendata->o_arg.claim;
1853 nfs4_stateid stateid;
1857 spin_lock(&state->owner->so_lock);
1858 if (can_open_cached(state, fmode, open_mode, claim)) {
1859 update_open_stateflags(state, fmode);
1860 spin_unlock(&state->owner->so_lock);
1861 goto out_return_state;
1863 spin_unlock(&state->owner->so_lock);
1865 delegation = nfs4_get_valid_delegation(state->inode);
1866 if (!can_open_delegated(delegation, fmode, claim)) {
1870 /* Save the delegation */
1871 nfs4_stateid_copy(&stateid, &delegation->stateid);
1873 nfs_release_seqid(opendata->o_arg.seqid);
1874 if (!opendata->is_recover) {
1875 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1881 /* Try to update the stateid using the delegation */
1882 if (update_open_stateid(state, NULL, &stateid, fmode))
1883 goto out_return_state;
1886 return ERR_PTR(ret);
1888 refcount_inc(&state->count);
1893 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1895 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1896 struct nfs_delegation *delegation;
1897 int delegation_flags = 0;
1900 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1902 delegation_flags = delegation->flags;
1904 switch (data->o_arg.claim) {
1907 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1908 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1909 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1910 "returning a delegation for "
1911 "OPEN(CLAIM_DELEGATE_CUR)\n",
1915 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1916 nfs_inode_set_delegation(state->inode,
1917 data->owner->so_cred,
1918 data->o_res.delegation_type,
1919 &data->o_res.delegation,
1920 data->o_res.pagemod_limit);
1922 nfs_inode_reclaim_delegation(state->inode,
1923 data->owner->so_cred,
1924 data->o_res.delegation_type,
1925 &data->o_res.delegation,
1926 data->o_res.pagemod_limit);
1928 if (data->o_res.do_recall)
1929 nfs_async_inode_return_delegation(state->inode,
1930 &data->o_res.delegation);
1934 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1935 * and update the nfs4_state.
1937 static struct nfs4_state *
1938 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1940 struct inode *inode = data->state->inode;
1941 struct nfs4_state *state = data->state;
1944 if (!data->rpc_done) {
1945 if (data->rpc_status)
1946 return ERR_PTR(data->rpc_status);
1947 /* cached opens have already been processed */
1951 ret = nfs_refresh_inode(inode, &data->f_attr);
1953 return ERR_PTR(ret);
1955 if (data->o_res.delegation_type != 0)
1956 nfs4_opendata_check_deleg(data, state);
1958 if (!update_open_stateid(state, &data->o_res.stateid,
1959 NULL, data->o_arg.fmode))
1960 return ERR_PTR(-EAGAIN);
1961 refcount_inc(&state->count);
1966 static struct inode *
1967 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1969 struct inode *inode;
1971 switch (data->o_arg.claim) {
1972 case NFS4_OPEN_CLAIM_NULL:
1973 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1974 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1975 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1976 return ERR_PTR(-EAGAIN);
1977 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1978 &data->f_attr, data->f_label);
1981 inode = d_inode(data->dentry);
1983 nfs_refresh_inode(inode, &data->f_attr);
1988 static struct nfs4_state *
1989 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1991 struct nfs4_state *state;
1992 struct inode *inode;
1994 inode = nfs4_opendata_get_inode(data);
1996 return ERR_CAST(inode);
1997 if (data->state != NULL && data->state->inode == inode) {
1998 state = data->state;
1999 refcount_inc(&state->count);
2001 state = nfs4_get_open_state(inode, data->owner);
2004 state = ERR_PTR(-ENOMEM);
2008 static struct nfs4_state *
2009 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2011 struct nfs4_state *state;
2013 if (!data->rpc_done) {
2014 state = nfs4_try_open_cached(data);
2015 trace_nfs4_cached_open(data->state);
2019 state = nfs4_opendata_find_nfs4_state(data);
2023 if (data->o_res.delegation_type != 0)
2024 nfs4_opendata_check_deleg(data, state);
2025 if (!update_open_stateid(state, &data->o_res.stateid,
2026 NULL, data->o_arg.fmode)) {
2027 nfs4_put_open_state(state);
2028 state = ERR_PTR(-EAGAIN);
2031 nfs_release_seqid(data->o_arg.seqid);
2035 static struct nfs4_state *
2036 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2038 struct nfs4_state *ret;
2040 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2041 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2043 ret = _nfs4_opendata_to_nfs4_state(data);
2044 nfs4_sequence_free_slot(&data->o_res.seq_res);
2048 static struct nfs_open_context *
2049 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2051 struct nfs_inode *nfsi = NFS_I(state->inode);
2052 struct nfs_open_context *ctx;
2055 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2056 if (ctx->state != state)
2058 if ((ctx->mode & mode) != mode)
2060 if (!get_nfs_open_context(ctx))
2066 return ERR_PTR(-ENOENT);
2069 static struct nfs_open_context *
2070 nfs4_state_find_open_context(struct nfs4_state *state)
2072 struct nfs_open_context *ctx;
2074 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2077 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2080 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2083 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2084 struct nfs4_state *state, enum open_claim_type4 claim)
2086 struct nfs4_opendata *opendata;
2088 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2089 NULL, claim, GFP_NOFS);
2090 if (opendata == NULL)
2091 return ERR_PTR(-ENOMEM);
2092 opendata->state = state;
2093 refcount_inc(&state->count);
2097 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2100 struct nfs4_state *newstate;
2103 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2105 opendata->o_arg.open_flags = 0;
2106 opendata->o_arg.fmode = fmode;
2107 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2108 NFS_SB(opendata->dentry->d_sb),
2110 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2111 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2112 nfs4_init_opendata_res(opendata);
2113 ret = _nfs4_recover_proc_open(opendata);
2116 newstate = nfs4_opendata_to_nfs4_state(opendata);
2117 if (IS_ERR(newstate))
2118 return PTR_ERR(newstate);
2119 if (newstate != opendata->state)
2121 nfs4_close_state(newstate, fmode);
2125 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2129 /* memory barrier prior to reading state->n_* */
2131 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2134 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2137 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2141 * We may have performed cached opens for all three recoveries.
2142 * Check if we need to update the current stateid.
2144 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2145 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2146 write_seqlock(&state->seqlock);
2147 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2148 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2149 write_sequnlock(&state->seqlock);
2156 * reclaim state on the server after a reboot.
2158 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2160 struct nfs_delegation *delegation;
2161 struct nfs4_opendata *opendata;
2162 fmode_t delegation_type = 0;
2165 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2166 NFS4_OPEN_CLAIM_PREVIOUS);
2167 if (IS_ERR(opendata))
2168 return PTR_ERR(opendata);
2170 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2171 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2172 delegation_type = delegation->type;
2174 opendata->o_arg.u.delegation_type = delegation_type;
2175 status = nfs4_open_recover(opendata, state);
2176 nfs4_opendata_put(opendata);
2180 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2182 struct nfs_server *server = NFS_SERVER(state->inode);
2183 struct nfs4_exception exception = { };
2186 err = _nfs4_do_open_reclaim(ctx, state);
2187 trace_nfs4_open_reclaim(ctx, 0, err);
2188 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2190 if (err != -NFS4ERR_DELAY)
2192 nfs4_handle_exception(server, err, &exception);
2193 } while (exception.retry);
2197 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2199 struct nfs_open_context *ctx;
2202 ctx = nfs4_state_find_open_context(state);
2205 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2206 nfs_state_clear_open_state_flags(state);
2207 ret = nfs4_do_open_reclaim(ctx, state);
2208 put_nfs_open_context(ctx);
2212 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)
2216 printk(KERN_ERR "NFS: %s: unhandled error "
2217 "%d.\n", __func__, err);
2224 case -NFS4ERR_BADSESSION:
2225 case -NFS4ERR_BADSLOT:
2226 case -NFS4ERR_BAD_HIGH_SLOT:
2227 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2228 case -NFS4ERR_DEADSESSION:
2230 case -NFS4ERR_STALE_CLIENTID:
2231 case -NFS4ERR_STALE_STATEID:
2232 /* Don't recall a delegation if it was lost */
2233 nfs4_schedule_lease_recovery(server->nfs_client);
2235 case -NFS4ERR_MOVED:
2236 nfs4_schedule_migration_recovery(server);
2238 case -NFS4ERR_LEASE_MOVED:
2239 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2241 case -NFS4ERR_DELEG_REVOKED:
2242 case -NFS4ERR_ADMIN_REVOKED:
2243 case -NFS4ERR_EXPIRED:
2244 case -NFS4ERR_BAD_STATEID:
2245 case -NFS4ERR_OPENMODE:
2246 nfs_inode_find_state_and_recover(state->inode,
2248 nfs4_schedule_stateid_recovery(server, state);
2250 case -NFS4ERR_DELAY:
2251 case -NFS4ERR_GRACE:
2255 case -NFS4ERR_DENIED:
2257 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2259 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2266 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2267 struct nfs4_state *state, const nfs4_stateid *stateid)
2269 struct nfs_server *server = NFS_SERVER(state->inode);
2270 struct nfs4_opendata *opendata;
2273 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2274 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2275 if (IS_ERR(opendata))
2276 return PTR_ERR(opendata);
2277 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2278 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2279 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2283 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2284 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2288 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2289 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2293 nfs_state_clear_delegation(state);
2295 nfs4_opendata_put(opendata);
2296 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2299 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2301 struct nfs4_opendata *data = calldata;
2303 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2304 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2307 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2309 struct nfs4_opendata *data = calldata;
2311 nfs40_sequence_done(task, &data->c_res.seq_res);
2313 data->rpc_status = task->tk_status;
2314 if (data->rpc_status == 0) {
2315 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2316 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2317 renew_lease(data->o_res.server, data->timestamp);
2318 data->rpc_done = true;
2322 static void nfs4_open_confirm_release(void *calldata)
2324 struct nfs4_opendata *data = calldata;
2325 struct nfs4_state *state = NULL;
2327 /* If this request hasn't been cancelled, do nothing */
2328 if (!data->cancelled)
2330 /* In case of error, no cleanup! */
2331 if (!data->rpc_done)
2333 state = nfs4_opendata_to_nfs4_state(data);
2335 nfs4_close_state(state, data->o_arg.fmode);
2337 nfs4_opendata_put(data);
2340 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2341 .rpc_call_prepare = nfs4_open_confirm_prepare,
2342 .rpc_call_done = nfs4_open_confirm_done,
2343 .rpc_release = nfs4_open_confirm_release,
2347 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2349 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2351 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2352 struct rpc_task *task;
2353 struct rpc_message msg = {
2354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2355 .rpc_argp = &data->c_arg,
2356 .rpc_resp = &data->c_res,
2357 .rpc_cred = data->owner->so_cred,
2359 struct rpc_task_setup task_setup_data = {
2360 .rpc_client = server->client,
2361 .rpc_message = &msg,
2362 .callback_ops = &nfs4_open_confirm_ops,
2363 .callback_data = data,
2364 .workqueue = nfsiod_workqueue,
2365 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2369 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2371 kref_get(&data->kref);
2372 data->rpc_done = false;
2373 data->rpc_status = 0;
2374 data->timestamp = jiffies;
2375 task = rpc_run_task(&task_setup_data);
2377 return PTR_ERR(task);
2378 status = rpc_wait_for_completion_task(task);
2380 data->cancelled = true;
2383 status = data->rpc_status;
2388 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2390 struct nfs4_opendata *data = calldata;
2391 struct nfs4_state_owner *sp = data->owner;
2392 struct nfs_client *clp = sp->so_server->nfs_client;
2393 enum open_claim_type4 claim = data->o_arg.claim;
2395 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2398 * Check if we still need to send an OPEN call, or if we can use
2399 * a delegation instead.
2401 if (data->state != NULL) {
2402 struct nfs_delegation *delegation;
2404 if (can_open_cached(data->state, data->o_arg.fmode,
2405 data->o_arg.open_flags, claim))
2408 delegation = nfs4_get_valid_delegation(data->state->inode);
2409 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2410 goto unlock_no_action;
2413 /* Update client id. */
2414 data->o_arg.clientid = clp->cl_clientid;
2418 case NFS4_OPEN_CLAIM_PREVIOUS:
2419 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2420 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2421 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2423 case NFS4_OPEN_CLAIM_FH:
2424 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2426 data->timestamp = jiffies;
2427 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2428 &data->o_arg.seq_args,
2429 &data->o_res.seq_res,
2431 nfs_release_seqid(data->o_arg.seqid);
2433 /* Set the create mode (note dependency on the session type) */
2434 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2435 if (data->o_arg.open_flags & O_EXCL) {
2436 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2437 if (nfs4_has_persistent_session(clp))
2438 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2439 else if (clp->cl_mvops->minor_version > 0)
2440 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2444 trace_nfs4_cached_open(data->state);
2447 task->tk_action = NULL;
2449 nfs4_sequence_done(task, &data->o_res.seq_res);
2452 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2454 struct nfs4_opendata *data = calldata;
2456 data->rpc_status = task->tk_status;
2458 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2461 if (task->tk_status == 0) {
2462 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2463 switch (data->o_res.f_attr->mode & S_IFMT) {
2467 data->rpc_status = -ELOOP;
2470 data->rpc_status = -EISDIR;
2473 data->rpc_status = -ENOTDIR;
2476 renew_lease(data->o_res.server, data->timestamp);
2477 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2478 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2480 data->rpc_done = true;
2483 static void nfs4_open_release(void *calldata)
2485 struct nfs4_opendata *data = calldata;
2486 struct nfs4_state *state = NULL;
2488 /* If this request hasn't been cancelled, do nothing */
2489 if (!data->cancelled)
2491 /* In case of error, no cleanup! */
2492 if (data->rpc_status != 0 || !data->rpc_done)
2494 /* In case we need an open_confirm, no cleanup! */
2495 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2497 state = nfs4_opendata_to_nfs4_state(data);
2499 nfs4_close_state(state, data->o_arg.fmode);
2501 nfs4_opendata_put(data);
2504 static const struct rpc_call_ops nfs4_open_ops = {
2505 .rpc_call_prepare = nfs4_open_prepare,
2506 .rpc_call_done = nfs4_open_done,
2507 .rpc_release = nfs4_open_release,
2510 static int nfs4_run_open_task(struct nfs4_opendata *data,
2511 struct nfs_open_context *ctx)
2513 struct inode *dir = d_inode(data->dir);
2514 struct nfs_server *server = NFS_SERVER(dir);
2515 struct nfs_openargs *o_arg = &data->o_arg;
2516 struct nfs_openres *o_res = &data->o_res;
2517 struct rpc_task *task;
2518 struct rpc_message msg = {
2519 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2522 .rpc_cred = data->owner->so_cred,
2524 struct rpc_task_setup task_setup_data = {
2525 .rpc_client = server->client,
2526 .rpc_message = &msg,
2527 .callback_ops = &nfs4_open_ops,
2528 .callback_data = data,
2529 .workqueue = nfsiod_workqueue,
2530 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2534 kref_get(&data->kref);
2535 data->rpc_done = false;
2536 data->rpc_status = 0;
2537 data->cancelled = false;
2538 data->is_recover = false;
2540 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2541 data->is_recover = true;
2542 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2544 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2545 pnfs_lgopen_prepare(data, ctx);
2547 task = rpc_run_task(&task_setup_data);
2549 return PTR_ERR(task);
2550 status = rpc_wait_for_completion_task(task);
2552 data->cancelled = true;
2555 status = data->rpc_status;
2561 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2563 struct inode *dir = d_inode(data->dir);
2564 struct nfs_openres *o_res = &data->o_res;
2567 status = nfs4_run_open_task(data, NULL);
2568 if (status != 0 || !data->rpc_done)
2571 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2573 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2574 status = _nfs4_proc_open_confirm(data);
2580 * Additional permission checks in order to distinguish between an
2581 * open for read, and an open for execute. This works around the
2582 * fact that NFSv4 OPEN treats read and execute permissions as being
2584 * Note that in the non-execute case, we want to turn off permission
2585 * checking if we just created a new file (POSIX open() semantics).
2587 static int nfs4_opendata_access(const struct cred *cred,
2588 struct nfs4_opendata *opendata,
2589 struct nfs4_state *state, fmode_t fmode,
2592 struct nfs_access_entry cache;
2595 /* access call failed or for some reason the server doesn't
2596 * support any access modes -- defer access call until later */
2597 if (opendata->o_res.access_supported == 0)
2602 * Use openflags to check for exec, because fmode won't
2603 * always have FMODE_EXEC set when file open for exec.
2605 if (openflags & __FMODE_EXEC) {
2606 /* ONLY check for exec rights */
2607 if (S_ISDIR(state->inode->i_mode))
2608 mask = NFS4_ACCESS_LOOKUP;
2610 mask = NFS4_ACCESS_EXECUTE;
2611 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2612 mask = NFS4_ACCESS_READ;
2615 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2616 nfs_access_add_cache(state->inode, &cache);
2618 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2619 if ((mask & ~cache.mask & flags) == 0)
2626 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2628 static int _nfs4_proc_open(struct nfs4_opendata *data,
2629 struct nfs_open_context *ctx)
2631 struct inode *dir = d_inode(data->dir);
2632 struct nfs_server *server = NFS_SERVER(dir);
2633 struct nfs_openargs *o_arg = &data->o_arg;
2634 struct nfs_openres *o_res = &data->o_res;
2637 status = nfs4_run_open_task(data, ctx);
2638 if (!data->rpc_done)
2641 if (status == -NFS4ERR_BADNAME &&
2642 !(o_arg->open_flags & O_CREAT))
2647 nfs_fattr_map_and_free_names(server, &data->f_attr);
2649 if (o_arg->open_flags & O_CREAT) {
2650 if (o_arg->open_flags & O_EXCL)
2651 data->file_created = true;
2652 else if (o_res->cinfo.before != o_res->cinfo.after)
2653 data->file_created = true;
2654 if (data->file_created ||
2655 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2656 update_changeattr(dir, &o_res->cinfo,
2657 o_res->f_attr->time_start, 0);
2659 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2660 server->caps &= ~NFS_CAP_POSIX_LOCK;
2661 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2662 status = _nfs4_proc_open_confirm(data);
2666 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2667 nfs4_sequence_free_slot(&o_res->seq_res);
2668 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2669 o_res->f_label, NULL);
2676 * reclaim state on the server after a network partition.
2677 * Assumes caller holds the appropriate lock
2679 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2681 struct nfs4_opendata *opendata;
2684 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2685 NFS4_OPEN_CLAIM_FH);
2686 if (IS_ERR(opendata))
2687 return PTR_ERR(opendata);
2688 ret = nfs4_open_recover(opendata, state);
2690 d_drop(ctx->dentry);
2691 nfs4_opendata_put(opendata);
2695 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2697 struct nfs_server *server = NFS_SERVER(state->inode);
2698 struct nfs4_exception exception = { };
2702 err = _nfs4_open_expired(ctx, state);
2703 trace_nfs4_open_expired(ctx, 0, err);
2704 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2709 case -NFS4ERR_GRACE:
2710 case -NFS4ERR_DELAY:
2711 nfs4_handle_exception(server, err, &exception);
2714 } while (exception.retry);
2719 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2721 struct nfs_open_context *ctx;
2724 ctx = nfs4_state_find_open_context(state);
2727 ret = nfs4_do_open_expired(ctx, state);
2728 put_nfs_open_context(ctx);
2732 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2733 const nfs4_stateid *stateid)
2735 nfs_remove_bad_delegation(state->inode, stateid);
2736 nfs_state_clear_delegation(state);
2739 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2741 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2742 nfs_finish_clear_delegation_stateid(state, NULL);
2745 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2747 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2748 nfs40_clear_delegation_stateid(state);
2749 nfs_state_clear_open_state_flags(state);
2750 return nfs4_open_expired(sp, state);
2753 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2754 nfs4_stateid *stateid,
2755 const struct cred *cred)
2757 return -NFS4ERR_BAD_STATEID;
2760 #if defined(CONFIG_NFS_V4_1)
2761 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2762 nfs4_stateid *stateid,
2763 const struct cred *cred)
2767 switch (stateid->type) {
2770 case NFS4_INVALID_STATEID_TYPE:
2771 case NFS4_SPECIAL_STATEID_TYPE:
2772 return -NFS4ERR_BAD_STATEID;
2773 case NFS4_REVOKED_STATEID_TYPE:
2777 status = nfs41_test_stateid(server, stateid, cred);
2779 case -NFS4ERR_EXPIRED:
2780 case -NFS4ERR_ADMIN_REVOKED:
2781 case -NFS4ERR_DELEG_REVOKED:
2787 /* Ack the revoked state to the server */
2788 nfs41_free_stateid(server, stateid, cred, true);
2789 return -NFS4ERR_EXPIRED;
2792 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2794 struct nfs_server *server = NFS_SERVER(state->inode);
2795 nfs4_stateid stateid;
2796 struct nfs_delegation *delegation;
2797 const struct cred *cred = NULL;
2798 int status, ret = NFS_OK;
2800 /* Get the delegation credential for use by test/free_stateid */
2802 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2803 if (delegation == NULL) {
2805 nfs_state_clear_delegation(state);
2809 spin_lock(&delegation->lock);
2810 nfs4_stateid_copy(&stateid, &delegation->stateid);
2812 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2813 &delegation->flags)) {
2814 spin_unlock(&delegation->lock);
2819 if (delegation->cred)
2820 cred = get_cred(delegation->cred);
2821 spin_unlock(&delegation->lock);
2823 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2824 trace_nfs4_test_delegation_stateid(state, NULL, status);
2825 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2826 nfs_finish_clear_delegation_stateid(state, &stateid);
2834 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2838 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2839 nfs4_copy_delegation_stateid(state->inode, state->state,
2841 nfs4_stateid_match_other(&state->stateid, &tmp))
2842 nfs_state_set_delegation(state, &tmp, state->state);
2844 nfs_state_clear_delegation(state);
2848 * nfs41_check_expired_locks - possibly free a lock stateid
2850 * @state: NFSv4 state for an inode
2852 * Returns NFS_OK if recovery for this stateid is now finished.
2853 * Otherwise a negative NFS4ERR value is returned.
2855 static int nfs41_check_expired_locks(struct nfs4_state *state)
2857 int status, ret = NFS_OK;
2858 struct nfs4_lock_state *lsp, *prev = NULL;
2859 struct nfs_server *server = NFS_SERVER(state->inode);
2861 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2864 spin_lock(&state->state_lock);
2865 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2866 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2867 const struct cred *cred = lsp->ls_state->owner->so_cred;
2869 refcount_inc(&lsp->ls_count);
2870 spin_unlock(&state->state_lock);
2872 nfs4_put_lock_state(prev);
2875 status = nfs41_test_and_free_expired_stateid(server,
2878 trace_nfs4_test_lock_stateid(state, lsp, status);
2879 if (status == -NFS4ERR_EXPIRED ||
2880 status == -NFS4ERR_BAD_STATEID) {
2881 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2882 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2883 if (!recover_lost_locks)
2884 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2885 } else if (status != NFS_OK) {
2887 nfs4_put_lock_state(prev);
2890 spin_lock(&state->state_lock);
2893 spin_unlock(&state->state_lock);
2894 nfs4_put_lock_state(prev);
2900 * nfs41_check_open_stateid - possibly free an open stateid
2902 * @state: NFSv4 state for an inode
2904 * Returns NFS_OK if recovery for this stateid is now finished.
2905 * Otherwise a negative NFS4ERR value is returned.
2907 static int nfs41_check_open_stateid(struct nfs4_state *state)
2909 struct nfs_server *server = NFS_SERVER(state->inode);
2910 nfs4_stateid *stateid = &state->open_stateid;
2911 const struct cred *cred = state->owner->so_cred;
2914 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2915 return -NFS4ERR_BAD_STATEID;
2916 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2917 trace_nfs4_test_open_stateid(state, NULL, status);
2918 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2919 nfs_state_clear_open_state_flags(state);
2920 stateid->type = NFS4_INVALID_STATEID_TYPE;
2923 if (nfs_open_stateid_recover_openmode(state))
2924 return -NFS4ERR_OPENMODE;
2928 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2932 status = nfs41_check_delegation_stateid(state);
2933 if (status != NFS_OK)
2935 nfs41_delegation_recover_stateid(state);
2937 status = nfs41_check_expired_locks(state);
2938 if (status != NFS_OK)
2940 status = nfs41_check_open_stateid(state);
2941 if (status != NFS_OK)
2942 status = nfs4_open_expired(sp, state);
2948 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2949 * fields corresponding to attributes that were used to store the verifier.
2950 * Make sure we clobber those fields in the later setattr call
2952 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2953 struct iattr *sattr, struct nfs4_label **label)
2955 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2960 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2961 attrset[i] = opendata->o_res.attrset[i];
2962 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2963 attrset[i] &= ~bitmask[i];
2966 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2967 sattr->ia_valid : 0;
2969 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2970 if (sattr->ia_valid & ATTR_ATIME_SET)
2971 ret |= ATTR_ATIME_SET;
2976 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2977 if (sattr->ia_valid & ATTR_MTIME_SET)
2978 ret |= ATTR_MTIME_SET;
2983 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
2988 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2989 int flags, struct nfs_open_context *ctx)
2991 struct nfs4_state_owner *sp = opendata->owner;
2992 struct nfs_server *server = sp->so_server;
2993 struct dentry *dentry;
2994 struct nfs4_state *state;
2995 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
2996 struct inode *dir = d_inode(opendata->dir);
2997 unsigned long dir_verifier;
3001 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3002 dir_verifier = nfs_save_change_attribute(dir);
3004 ret = _nfs4_proc_open(opendata, ctx);
3008 state = _nfs4_opendata_to_nfs4_state(opendata);
3009 ret = PTR_ERR(state);
3013 if (server->caps & NFS_CAP_POSIX_LOCK)
3014 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3015 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3016 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3018 dentry = opendata->dentry;
3019 if (d_really_is_negative(dentry)) {
3020 struct dentry *alias;
3022 alias = d_exact_alias(dentry, state->inode);
3024 alias = d_splice_alias(igrab(state->inode), dentry);
3025 /* d_splice_alias() can't fail here - it's a non-directory */
3028 ctx->dentry = dentry = alias;
3032 switch(opendata->o_arg.claim) {
3035 case NFS4_OPEN_CLAIM_NULL:
3036 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3037 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3038 if (!opendata->rpc_done)
3040 if (opendata->o_res.delegation_type != 0)
3041 dir_verifier = nfs_save_change_attribute(dir);
3042 nfs_set_verifier(dentry, dir_verifier);
3045 /* Parse layoutget results before we check for access */
3046 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3048 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3053 if (d_inode(dentry) == state->inode) {
3054 nfs_inode_attach_open_context(ctx);
3055 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3056 nfs4_schedule_stateid_recovery(server, state);
3060 if (!opendata->cancelled)
3061 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3066 * Returns a referenced nfs4_state
3068 static int _nfs4_do_open(struct inode *dir,
3069 struct nfs_open_context *ctx,
3071 const struct nfs4_open_createattrs *c,
3074 struct nfs4_state_owner *sp;
3075 struct nfs4_state *state = NULL;
3076 struct nfs_server *server = NFS_SERVER(dir);
3077 struct nfs4_opendata *opendata;
3078 struct dentry *dentry = ctx->dentry;
3079 const struct cred *cred = ctx->cred;
3080 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3081 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3082 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3083 struct iattr *sattr = c->sattr;
3084 struct nfs4_label *label = c->label;
3085 struct nfs4_label *olabel = NULL;
3088 /* Protect against reboot recovery conflicts */
3090 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3092 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3095 status = nfs4_client_recover_expired_lease(server->nfs_client);
3097 goto err_put_state_owner;
3098 if (d_really_is_positive(dentry))
3099 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3101 if (d_really_is_positive(dentry))
3102 claim = NFS4_OPEN_CLAIM_FH;
3103 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3104 c, claim, GFP_KERNEL);
3105 if (opendata == NULL)
3106 goto err_put_state_owner;
3109 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3110 if (IS_ERR(olabel)) {
3111 status = PTR_ERR(olabel);
3112 goto err_opendata_put;
3116 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3117 if (!opendata->f_attr.mdsthreshold) {
3118 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3119 if (!opendata->f_attr.mdsthreshold)
3120 goto err_free_label;
3122 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3124 if (d_really_is_positive(dentry))
3125 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3127 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3129 goto err_free_label;
3132 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3133 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3134 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3136 * send create attributes which was not set by open
3137 * with an extra setattr.
3139 if (attrs || label) {
3140 unsigned ia_old = sattr->ia_valid;
3142 sattr->ia_valid = attrs;
3143 nfs_fattr_init(opendata->o_res.f_attr);
3144 status = nfs4_do_setattr(state->inode, cred,
3145 opendata->o_res.f_attr, sattr,
3146 ctx, label, olabel);
3148 nfs_setattr_update_inode(state->inode, sattr,
3149 opendata->o_res.f_attr);
3150 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3152 sattr->ia_valid = ia_old;
3155 if (opened && opendata->file_created)
3158 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3159 *ctx_th = opendata->f_attr.mdsthreshold;
3160 opendata->f_attr.mdsthreshold = NULL;
3163 nfs4_label_free(olabel);
3165 nfs4_opendata_put(opendata);
3166 nfs4_put_state_owner(sp);
3169 nfs4_label_free(olabel);
3171 nfs4_opendata_put(opendata);
3172 err_put_state_owner:
3173 nfs4_put_state_owner(sp);
3179 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3180 struct nfs_open_context *ctx,
3182 struct iattr *sattr,
3183 struct nfs4_label *label,
3186 struct nfs_server *server = NFS_SERVER(dir);
3187 struct nfs4_exception exception = {
3188 .interruptible = true,
3190 struct nfs4_state *res;
3191 struct nfs4_open_createattrs c = {
3195 [0] = (__u32)jiffies,
3196 [1] = (__u32)current->pid,
3202 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3204 trace_nfs4_open_file(ctx, flags, status);
3207 /* NOTE: BAD_SEQID means the server and client disagree about the
3208 * book-keeping w.r.t. state-changing operations
3209 * (OPEN/CLOSE/LOCK/LOCKU...)
3210 * It is actually a sign of a bug on the client or on the server.
3212 * If we receive a BAD_SEQID error in the particular case of
3213 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3214 * have unhashed the old state_owner for us, and that we can
3215 * therefore safely retry using a new one. We should still warn
3216 * the user though...
3218 if (status == -NFS4ERR_BAD_SEQID) {
3219 pr_warn_ratelimited("NFS: v4 server %s "
3220 " returned a bad sequence-id error!\n",
3221 NFS_SERVER(dir)->nfs_client->cl_hostname);
3222 exception.retry = 1;
3226 * BAD_STATEID on OPEN means that the server cancelled our
3227 * state before it received the OPEN_CONFIRM.
3228 * Recover by retrying the request as per the discussion
3229 * on Page 181 of RFC3530.
3231 if (status == -NFS4ERR_BAD_STATEID) {
3232 exception.retry = 1;
3235 if (status == -NFS4ERR_EXPIRED) {
3236 nfs4_schedule_lease_recovery(server->nfs_client);
3237 exception.retry = 1;
3240 if (status == -EAGAIN) {
3241 /* We must have found a delegation */
3242 exception.retry = 1;
3245 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3247 res = ERR_PTR(nfs4_handle_exception(server,
3248 status, &exception));
3249 } while (exception.retry);
3253 static int _nfs4_do_setattr(struct inode *inode,
3254 struct nfs_setattrargs *arg,
3255 struct nfs_setattrres *res,
3256 const struct cred *cred,
3257 struct nfs_open_context *ctx)
3259 struct nfs_server *server = NFS_SERVER(inode);
3260 struct rpc_message msg = {
3261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3266 const struct cred *delegation_cred = NULL;
3267 unsigned long timestamp = jiffies;
3271 nfs_fattr_init(res->fattr);
3273 /* Servers should only apply open mode checks for file size changes */
3274 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3278 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3279 /* Use that stateid */
3280 } else if (ctx != NULL && ctx->state) {
3281 struct nfs_lock_context *l_ctx;
3282 if (!nfs4_valid_open_stateid(ctx->state))
3284 l_ctx = nfs_get_lock_context(ctx);
3286 return PTR_ERR(l_ctx);
3287 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3288 &arg->stateid, &delegation_cred);
3289 nfs_put_lock_context(l_ctx);
3292 else if (status == -EAGAIN)
3296 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3298 if (delegation_cred)
3299 msg.rpc_cred = delegation_cred;
3301 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3303 put_cred(delegation_cred);
3304 if (status == 0 && ctx != NULL)
3305 renew_lease(server, timestamp);
3306 trace_nfs4_setattr(inode, &arg->stateid, status);
3310 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3311 struct nfs_fattr *fattr, struct iattr *sattr,
3312 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3313 struct nfs4_label *olabel)
3315 struct nfs_server *server = NFS_SERVER(inode);
3316 __u32 bitmask[NFS4_BITMASK_SZ];
3317 struct nfs4_state *state = ctx ? ctx->state : NULL;
3318 struct nfs_setattrargs arg = {
3319 .fh = NFS_FH(inode),
3325 struct nfs_setattrres res = {
3330 struct nfs4_exception exception = {
3333 .stateid = &arg.stateid,
3338 nfs4_bitmap_copy_adjust_setattr(bitmask,
3339 nfs4_bitmask(server, olabel),
3342 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3344 case -NFS4ERR_OPENMODE:
3345 if (!(sattr->ia_valid & ATTR_SIZE)) {
3346 pr_warn_once("NFSv4: server %s is incorrectly "
3347 "applying open mode checks to "
3348 "a SETATTR that is not "
3349 "changing file size.\n",
3350 server->nfs_client->cl_hostname);
3352 if (state && !(state->state & FMODE_WRITE)) {
3354 if (sattr->ia_valid & ATTR_OPEN)
3359 err = nfs4_handle_exception(server, err, &exception);
3360 } while (exception.retry);
3366 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3368 if (inode == NULL || !nfs_have_layout(inode))
3371 return pnfs_wait_on_layoutreturn(inode, task);
3375 * Update the seqid of an open stateid
3377 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3378 struct nfs4_state *state)
3385 if (!nfs4_valid_open_stateid(state))
3387 seq = read_seqbegin(&state->seqlock);
3388 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3389 nfs4_stateid_copy(dst, &state->open_stateid);
3390 if (read_seqretry(&state->seqlock, seq))
3394 seqid_open = state->open_stateid.seqid;
3395 if (read_seqretry(&state->seqlock, seq))
3398 dst_seqid = be32_to_cpu(dst->seqid);
3399 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3400 dst->seqid = seqid_open;
3406 * Update the seqid of an open stateid after receiving
3407 * NFS4ERR_OLD_STATEID
3409 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3410 struct nfs4_state *state)
3419 if (!nfs4_valid_open_stateid(state))
3421 seq = read_seqbegin(&state->seqlock);
3422 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3423 if (read_seqretry(&state->seqlock, seq))
3427 seqid_open = state->open_stateid.seqid;
3428 if (read_seqretry(&state->seqlock, seq))
3431 dst_seqid = be32_to_cpu(dst->seqid);
3432 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) >= 0)
3433 dst->seqid = cpu_to_be32(dst_seqid + 1);
3435 dst->seqid = seqid_open;
3443 struct nfs4_closedata {
3444 struct inode *inode;
3445 struct nfs4_state *state;
3446 struct nfs_closeargs arg;
3447 struct nfs_closeres res;
3449 struct nfs4_layoutreturn_args arg;
3450 struct nfs4_layoutreturn_res res;
3451 struct nfs4_xdr_opaque_data ld_private;
3455 struct nfs_fattr fattr;
3456 unsigned long timestamp;
3459 static void nfs4_free_closedata(void *data)
3461 struct nfs4_closedata *calldata = data;
3462 struct nfs4_state_owner *sp = calldata->state->owner;
3463 struct super_block *sb = calldata->state->inode->i_sb;
3465 if (calldata->lr.roc)
3466 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3467 calldata->res.lr_ret);
3468 nfs4_put_open_state(calldata->state);
3469 nfs_free_seqid(calldata->arg.seqid);
3470 nfs4_put_state_owner(sp);
3471 nfs_sb_deactive(sb);
3475 static void nfs4_close_done(struct rpc_task *task, void *data)
3477 struct nfs4_closedata *calldata = data;
3478 struct nfs4_state *state = calldata->state;
3479 struct nfs_server *server = NFS_SERVER(calldata->inode);
3480 nfs4_stateid *res_stateid = NULL;
3481 struct nfs4_exception exception = {
3483 .inode = calldata->inode,
3484 .stateid = &calldata->arg.stateid,
3487 dprintk("%s: begin!\n", __func__);
3488 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3490 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3492 /* Handle Layoutreturn errors */
3493 if (pnfs_roc_done(task, calldata->inode,
3494 &calldata->arg.lr_args,
3495 &calldata->res.lr_res,
3496 &calldata->res.lr_ret) == -EAGAIN)
3499 /* hmm. we are done with the inode, and in the process of freeing
3500 * the state_owner. we keep this around to process errors
3502 switch (task->tk_status) {
3504 res_stateid = &calldata->res.stateid;
3505 renew_lease(server, calldata->timestamp);
3507 case -NFS4ERR_ACCESS:
3508 if (calldata->arg.bitmask != NULL) {
3509 calldata->arg.bitmask = NULL;
3510 calldata->res.fattr = NULL;
3515 case -NFS4ERR_OLD_STATEID:
3516 /* Did we race with OPEN? */
3517 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3521 case -NFS4ERR_ADMIN_REVOKED:
3522 case -NFS4ERR_STALE_STATEID:
3523 case -NFS4ERR_EXPIRED:
3524 nfs4_free_revoked_stateid(server,
3525 &calldata->arg.stateid,
3526 task->tk_msg.rpc_cred);
3528 case -NFS4ERR_BAD_STATEID:
3529 if (calldata->arg.fmode == 0)
3533 task->tk_status = nfs4_async_handle_exception(task,
3534 server, task->tk_status, &exception);
3535 if (exception.retry)
3538 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3539 res_stateid, calldata->arg.fmode);
3541 task->tk_status = 0;
3542 nfs_release_seqid(calldata->arg.seqid);
3543 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3544 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3547 task->tk_status = 0;
3548 rpc_restart_call_prepare(task);
3552 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3554 struct nfs4_closedata *calldata = data;
3555 struct nfs4_state *state = calldata->state;
3556 struct inode *inode = calldata->inode;
3557 struct pnfs_layout_hdr *lo;
3558 bool is_rdonly, is_wronly, is_rdwr;
3561 dprintk("%s: begin!\n", __func__);
3562 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3565 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3566 spin_lock(&state->owner->so_lock);
3567 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3568 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3569 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3570 /* Calculate the change in open mode */
3571 calldata->arg.fmode = 0;
3572 if (state->n_rdwr == 0) {
3573 if (state->n_rdonly == 0)
3574 call_close |= is_rdonly;
3576 calldata->arg.fmode |= FMODE_READ;
3577 if (state->n_wronly == 0)
3578 call_close |= is_wronly;
3580 calldata->arg.fmode |= FMODE_WRITE;
3581 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3582 call_close |= is_rdwr;
3584 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3586 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3587 if (!nfs4_valid_open_stateid(state))
3589 spin_unlock(&state->owner->so_lock);
3592 /* Note: exit _without_ calling nfs4_close_done */
3596 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3597 nfs_release_seqid(calldata->arg.seqid);
3601 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3602 if (lo && !pnfs_layout_is_valid(lo)) {
3603 calldata->arg.lr_args = NULL;
3604 calldata->res.lr_res = NULL;
3607 if (calldata->arg.fmode == 0)
3608 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3610 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3611 /* Close-to-open cache consistency revalidation */
3612 if (!nfs4_have_delegation(inode, FMODE_READ))
3613 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3615 calldata->arg.bitmask = NULL;
3618 calldata->arg.share_access =
3619 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3620 calldata->arg.fmode, 0);
3622 if (calldata->res.fattr == NULL)
3623 calldata->arg.bitmask = NULL;
3624 else if (calldata->arg.bitmask == NULL)
3625 calldata->res.fattr = NULL;
3626 calldata->timestamp = jiffies;
3627 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3628 &calldata->arg.seq_args,
3629 &calldata->res.seq_res,
3631 nfs_release_seqid(calldata->arg.seqid);
3632 dprintk("%s: done!\n", __func__);
3635 task->tk_action = NULL;
3637 nfs4_sequence_done(task, &calldata->res.seq_res);
3640 static const struct rpc_call_ops nfs4_close_ops = {
3641 .rpc_call_prepare = nfs4_close_prepare,
3642 .rpc_call_done = nfs4_close_done,
3643 .rpc_release = nfs4_free_closedata,
3647 * It is possible for data to be read/written from a mem-mapped file
3648 * after the sys_close call (which hits the vfs layer as a flush).
3649 * This means that we can't safely call nfsv4 close on a file until
3650 * the inode is cleared. This in turn means that we are not good
3651 * NFSv4 citizens - we do not indicate to the server to update the file's
3652 * share state even when we are done with one of the three share
3653 * stateid's in the inode.
3655 * NOTE: Caller must be holding the sp->so_owner semaphore!
3657 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3659 struct nfs_server *server = NFS_SERVER(state->inode);
3660 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3661 struct nfs4_closedata *calldata;
3662 struct nfs4_state_owner *sp = state->owner;
3663 struct rpc_task *task;
3664 struct rpc_message msg = {
3665 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3666 .rpc_cred = state->owner->so_cred,
3668 struct rpc_task_setup task_setup_data = {
3669 .rpc_client = server->client,
3670 .rpc_message = &msg,
3671 .callback_ops = &nfs4_close_ops,
3672 .workqueue = nfsiod_workqueue,
3673 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3675 int status = -ENOMEM;
3677 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3678 &task_setup_data.rpc_client, &msg);
3680 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3681 if (calldata == NULL)
3683 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3684 calldata->inode = state->inode;
3685 calldata->state = state;
3686 calldata->arg.fh = NFS_FH(state->inode);
3687 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3688 goto out_free_calldata;
3689 /* Serialization for the sequence id */
3690 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3691 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3692 if (IS_ERR(calldata->arg.seqid))
3693 goto out_free_calldata;
3694 nfs_fattr_init(&calldata->fattr);
3695 calldata->arg.fmode = 0;
3696 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3697 calldata->res.fattr = &calldata->fattr;
3698 calldata->res.seqid = calldata->arg.seqid;
3699 calldata->res.server = server;
3700 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3701 calldata->lr.roc = pnfs_roc(state->inode,
3702 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3703 if (calldata->lr.roc) {
3704 calldata->arg.lr_args = &calldata->lr.arg;
3705 calldata->res.lr_res = &calldata->lr.res;
3707 nfs_sb_active(calldata->inode->i_sb);
3709 msg.rpc_argp = &calldata->arg;
3710 msg.rpc_resp = &calldata->res;
3711 task_setup_data.callback_data = calldata;
3712 task = rpc_run_task(&task_setup_data);
3714 return PTR_ERR(task);
3717 status = rpc_wait_for_completion_task(task);
3723 nfs4_put_open_state(state);
3724 nfs4_put_state_owner(sp);
3728 static struct inode *
3729 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3730 int open_flags, struct iattr *attr, int *opened)
3732 struct nfs4_state *state;
3733 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3735 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3737 /* Protect against concurrent sillydeletes */
3738 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3740 nfs4_label_release_security(label);
3743 return ERR_CAST(state);
3744 return state->inode;
3747 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3749 if (ctx->state == NULL)
3752 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3754 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3757 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3758 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3759 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3761 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3763 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3764 struct nfs4_server_caps_arg args = {
3768 struct nfs4_server_caps_res res = {};
3769 struct rpc_message msg = {
3770 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3777 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3778 FATTR4_WORD0_FH_EXPIRE_TYPE |
3779 FATTR4_WORD0_LINK_SUPPORT |
3780 FATTR4_WORD0_SYMLINK_SUPPORT |
3781 FATTR4_WORD0_ACLSUPPORT;
3783 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3785 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3787 /* Sanity check the server answers */
3788 switch (minorversion) {
3790 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3791 res.attr_bitmask[2] = 0;
3794 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3797 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3799 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3800 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3801 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3802 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3803 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3804 NFS_CAP_CTIME|NFS_CAP_MTIME|
3805 NFS_CAP_SECURITY_LABEL);
3806 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3807 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3808 server->caps |= NFS_CAP_ACLS;
3809 if (res.has_links != 0)
3810 server->caps |= NFS_CAP_HARDLINKS;
3811 if (res.has_symlinks != 0)
3812 server->caps |= NFS_CAP_SYMLINKS;
3813 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3814 server->caps |= NFS_CAP_FILEID;
3815 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3816 server->caps |= NFS_CAP_MODE;
3817 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3818 server->caps |= NFS_CAP_NLINK;
3819 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3820 server->caps |= NFS_CAP_OWNER;
3821 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3822 server->caps |= NFS_CAP_OWNER_GROUP;
3823 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3824 server->caps |= NFS_CAP_ATIME;
3825 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3826 server->caps |= NFS_CAP_CTIME;
3827 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3828 server->caps |= NFS_CAP_MTIME;
3829 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3830 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3831 server->caps |= NFS_CAP_SECURITY_LABEL;
3833 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3834 sizeof(server->attr_bitmask));
3835 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3837 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3838 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3839 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3840 server->cache_consistency_bitmask[2] = 0;
3842 /* Avoid a regression due to buggy server */
3843 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3844 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3845 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3846 sizeof(server->exclcreat_bitmask));
3848 server->acl_bitmask = res.acl_bitmask;
3849 server->fh_expire_type = res.fh_expire_type;
3855 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3857 struct nfs4_exception exception = {
3858 .interruptible = true,
3862 err = nfs4_handle_exception(server,
3863 _nfs4_server_capabilities(server, fhandle),
3865 } while (exception.retry);
3869 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3870 struct nfs_fsinfo *info)
3873 struct nfs4_lookup_root_arg args = {
3876 struct nfs4_lookup_res res = {
3878 .fattr = info->fattr,
3881 struct rpc_message msg = {
3882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3887 bitmask[0] = nfs4_fattr_bitmap[0];
3888 bitmask[1] = nfs4_fattr_bitmap[1];
3890 * Process the label in the upcoming getfattr
3892 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3894 nfs_fattr_init(info->fattr);
3895 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3898 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3899 struct nfs_fsinfo *info)
3901 struct nfs4_exception exception = {
3902 .interruptible = true,
3906 err = _nfs4_lookup_root(server, fhandle, info);
3907 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3910 case -NFS4ERR_WRONGSEC:
3913 err = nfs4_handle_exception(server, err, &exception);
3915 } while (exception.retry);
3920 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3921 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3923 struct rpc_auth_create_args auth_args = {
3924 .pseudoflavor = flavor,
3926 struct rpc_auth *auth;
3928 auth = rpcauth_create(&auth_args, server->client);
3931 return nfs4_lookup_root(server, fhandle, info);
3935 * Retry pseudoroot lookup with various security flavors. We do this when:
3937 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3938 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3940 * Returns zero on success, or a negative NFS4ERR value, or a
3941 * negative errno value.
3943 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3944 struct nfs_fsinfo *info)
3946 /* Per 3530bis 15.33.5 */
3947 static const rpc_authflavor_t flav_array[] = {
3951 RPC_AUTH_UNIX, /* courtesy */
3954 int status = -EPERM;
3957 if (server->auth_info.flavor_len > 0) {
3958 /* try each flavor specified by user */
3959 for (i = 0; i < server->auth_info.flavor_len; i++) {
3960 status = nfs4_lookup_root_sec(server, fhandle, info,
3961 server->auth_info.flavors[i]);
3962 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3967 /* no flavors specified by user, try default list */
3968 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3969 status = nfs4_lookup_root_sec(server, fhandle, info,
3971 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3978 * -EACCES could mean that the user doesn't have correct permissions
3979 * to access the mount. It could also mean that we tried to mount
3980 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3981 * existing mount programs don't handle -EACCES very well so it should
3982 * be mapped to -EPERM instead.
3984 if (status == -EACCES)
3990 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3991 * @server: initialized nfs_server handle
3992 * @fhandle: we fill in the pseudo-fs root file handle
3993 * @info: we fill in an FSINFO struct
3994 * @auth_probe: probe the auth flavours
3996 * Returns zero on success, or a negative errno.
3998 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3999 struct nfs_fsinfo *info,
4005 status = nfs4_lookup_root(server, fhandle, info);
4007 if (auth_probe || status == NFS4ERR_WRONGSEC)
4008 status = server->nfs_client->cl_mvops->find_root_sec(server,
4012 status = nfs4_server_capabilities(server, fhandle);
4014 status = nfs4_do_fsinfo(server, fhandle, info);
4016 return nfs4_map_errors(status);
4019 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4020 struct nfs_fsinfo *info)
4023 struct nfs_fattr *fattr = info->fattr;
4024 struct nfs4_label *label = fattr->label;
4026 error = nfs4_server_capabilities(server, mntfh);
4028 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4032 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
4034 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4038 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4039 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4040 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4047 * Get locations and (maybe) other attributes of a referral.
4048 * Note that we'll actually follow the referral later when
4049 * we detect fsid mismatch in inode revalidation
4051 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4052 const struct qstr *name, struct nfs_fattr *fattr,
4053 struct nfs_fh *fhandle)
4055 int status = -ENOMEM;
4056 struct page *page = NULL;
4057 struct nfs4_fs_locations *locations = NULL;
4059 page = alloc_page(GFP_KERNEL);
4062 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4063 if (locations == NULL)
4066 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4071 * If the fsid didn't change, this is a migration event, not a
4072 * referral. Cause us to drop into the exception handler, which
4073 * will kick off migration recovery.
4075 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4076 dprintk("%s: server did not return a different fsid for"
4077 " a referral at %s\n", __func__, name->name);
4078 status = -NFS4ERR_MOVED;
4081 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4082 nfs_fixup_referral_attributes(&locations->fattr);
4084 /* replace the lookup nfs_fattr with the locations nfs_fattr */
4085 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4086 memset(fhandle, 0, sizeof(struct nfs_fh));
4094 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4095 struct nfs_fattr *fattr, struct nfs4_label *label,
4096 struct inode *inode)
4098 __u32 bitmask[NFS4_BITMASK_SZ];
4099 struct nfs4_getattr_arg args = {
4103 struct nfs4_getattr_res res = {
4108 struct rpc_message msg = {
4109 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4113 unsigned short task_flags = 0;
4115 /* Is this is an attribute revalidation, subject to softreval? */
4116 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4117 task_flags |= RPC_TASK_TIMEOUT;
4119 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
4121 nfs_fattr_init(fattr);
4122 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4123 return nfs4_do_call_sync(server->client, server, &msg,
4124 &args.seq_args, &res.seq_res, task_flags);
4127 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4128 struct nfs_fattr *fattr, struct nfs4_label *label,
4129 struct inode *inode)
4131 struct nfs4_exception exception = {
4132 .interruptible = true,
4136 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4137 trace_nfs4_getattr(server, fhandle, fattr, err);
4138 err = nfs4_handle_exception(server, err,
4140 } while (exception.retry);
4145 * The file is not closed if it is opened due to the a request to change
4146 * the size of the file. The open call will not be needed once the
4147 * VFS layer lookup-intents are implemented.
4149 * Close is called when the inode is destroyed.
4150 * If we haven't opened the file for O_WRONLY, we
4151 * need to in the size_change case to obtain a stateid.
4154 * Because OPEN is always done by name in nfsv4, it is
4155 * possible that we opened a different file by the same
4156 * name. We can recognize this race condition, but we
4157 * can't do anything about it besides returning an error.
4159 * This will be fixed with VFS changes (lookup-intent).
4162 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4163 struct iattr *sattr)
4165 struct inode *inode = d_inode(dentry);
4166 const struct cred *cred = NULL;
4167 struct nfs_open_context *ctx = NULL;
4168 struct nfs4_label *label = NULL;
4171 if (pnfs_ld_layoutret_on_setattr(inode) &&
4172 sattr->ia_valid & ATTR_SIZE &&
4173 sattr->ia_size < i_size_read(inode))
4174 pnfs_commit_and_return_layout(inode);
4176 nfs_fattr_init(fattr);
4178 /* Deal with open(O_TRUNC) */
4179 if (sattr->ia_valid & ATTR_OPEN)
4180 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4182 /* Optimization: if the end result is no change, don't RPC */
4183 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4186 /* Search for an existing open(O_WRITE) file */
4187 if (sattr->ia_valid & ATTR_FILE) {
4189 ctx = nfs_file_open_context(sattr->ia_file);
4194 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4196 return PTR_ERR(label);
4198 /* Return any delegations if we're going to change ACLs */
4199 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4200 nfs4_inode_make_writeable(inode);
4202 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4204 nfs_setattr_update_inode(inode, sattr, fattr);
4205 nfs_setsecurity(inode, fattr, label);
4207 nfs4_label_free(label);
4211 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4212 struct dentry *dentry, struct nfs_fh *fhandle,
4213 struct nfs_fattr *fattr, struct nfs4_label *label)
4215 struct nfs_server *server = NFS_SERVER(dir);
4217 struct nfs4_lookup_arg args = {
4218 .bitmask = server->attr_bitmask,
4219 .dir_fh = NFS_FH(dir),
4220 .name = &dentry->d_name,
4222 struct nfs4_lookup_res res = {
4228 struct rpc_message msg = {
4229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4233 unsigned short task_flags = 0;
4235 /* Is this is an attribute revalidation, subject to softreval? */
4236 if (nfs_lookup_is_soft_revalidate(dentry))
4237 task_flags |= RPC_TASK_TIMEOUT;
4239 args.bitmask = nfs4_bitmask(server, label);
4241 nfs_fattr_init(fattr);
4243 dprintk("NFS call lookup %pd2\n", dentry);
4244 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4245 status = nfs4_do_call_sync(clnt, server, &msg,
4246 &args.seq_args, &res.seq_res, task_flags);
4247 dprintk("NFS reply lookup: %d\n", status);
4251 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4253 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4254 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4255 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4259 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4260 struct dentry *dentry, struct nfs_fh *fhandle,
4261 struct nfs_fattr *fattr, struct nfs4_label *label)
4263 struct nfs4_exception exception = {
4264 .interruptible = true,
4266 struct rpc_clnt *client = *clnt;
4267 const struct qstr *name = &dentry->d_name;
4270 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
4271 trace_nfs4_lookup(dir, name, err);
4273 case -NFS4ERR_BADNAME:
4276 case -NFS4ERR_MOVED:
4277 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4278 if (err == -NFS4ERR_MOVED)
4279 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4281 case -NFS4ERR_WRONGSEC:
4283 if (client != *clnt)
4285 client = nfs4_negotiate_security(client, dir, name);
4287 return PTR_ERR(client);
4289 exception.retry = 1;
4292 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4294 } while (exception.retry);
4299 else if (client != *clnt)
4300 rpc_shutdown_client(client);
4305 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4306 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4307 struct nfs4_label *label)
4310 struct rpc_clnt *client = NFS_CLIENT(dir);
4312 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
4313 if (client != NFS_CLIENT(dir)) {
4314 rpc_shutdown_client(client);
4315 nfs_fixup_secinfo_attributes(fattr);
4321 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4322 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4324 struct rpc_clnt *client = NFS_CLIENT(dir);
4327 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
4329 return ERR_PTR(status);
4330 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4333 static int _nfs4_proc_lookupp(struct inode *inode,
4334 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4335 struct nfs4_label *label)
4337 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4338 struct nfs_server *server = NFS_SERVER(inode);
4340 struct nfs4_lookupp_arg args = {
4341 .bitmask = server->attr_bitmask,
4342 .fh = NFS_FH(inode),
4344 struct nfs4_lookupp_res res = {
4350 struct rpc_message msg = {
4351 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4356 args.bitmask = nfs4_bitmask(server, label);
4358 nfs_fattr_init(fattr);
4360 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4361 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4363 dprintk("NFS reply lookupp: %d\n", status);
4367 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4368 struct nfs_fattr *fattr, struct nfs4_label *label)
4370 struct nfs4_exception exception = {
4371 .interruptible = true,
4375 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4376 trace_nfs4_lookupp(inode, err);
4377 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4379 } while (exception.retry);
4383 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4385 struct nfs_server *server = NFS_SERVER(inode);
4386 struct nfs4_accessargs args = {
4387 .fh = NFS_FH(inode),
4388 .access = entry->mask,
4390 struct nfs4_accessres res = {
4393 struct rpc_message msg = {
4394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4397 .rpc_cred = entry->cred,
4401 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4402 res.fattr = nfs_alloc_fattr();
4403 if (res.fattr == NULL)
4405 args.bitmask = server->cache_consistency_bitmask;
4407 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4409 nfs_access_set_mask(entry, res.access);
4411 nfs_refresh_inode(inode, res.fattr);
4413 nfs_free_fattr(res.fattr);
4417 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4419 struct nfs4_exception exception = {
4420 .interruptible = true,
4424 err = _nfs4_proc_access(inode, entry);
4425 trace_nfs4_access(inode, err);
4426 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4428 } while (exception.retry);
4433 * TODO: For the time being, we don't try to get any attributes
4434 * along with any of the zero-copy operations READ, READDIR,
4437 * In the case of the first three, we want to put the GETATTR
4438 * after the read-type operation -- this is because it is hard
4439 * to predict the length of a GETATTR response in v4, and thus
4440 * align the READ data correctly. This means that the GETATTR
4441 * may end up partially falling into the page cache, and we should
4442 * shift it into the 'tail' of the xdr_buf before processing.
4443 * To do this efficiently, we need to know the total length
4444 * of data received, which doesn't seem to be available outside
4447 * In the case of WRITE, we also want to put the GETATTR after
4448 * the operation -- in this case because we want to make sure
4449 * we get the post-operation mtime and size.
4451 * Both of these changes to the XDR layer would in fact be quite
4452 * minor, but I decided to leave them for a subsequent patch.
4454 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4455 unsigned int pgbase, unsigned int pglen)
4457 struct nfs4_readlink args = {
4458 .fh = NFS_FH(inode),
4463 struct nfs4_readlink_res res;
4464 struct rpc_message msg = {
4465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4470 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4473 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4474 unsigned int pgbase, unsigned int pglen)
4476 struct nfs4_exception exception = {
4477 .interruptible = true,
4481 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4482 trace_nfs4_readlink(inode, err);
4483 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4485 } while (exception.retry);
4490 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4493 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4496 struct nfs_server *server = NFS_SERVER(dir);
4497 struct nfs4_label l, *ilabel = NULL;
4498 struct nfs_open_context *ctx;
4499 struct nfs4_state *state;
4502 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4504 return PTR_ERR(ctx);
4506 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4508 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4509 sattr->ia_mode &= ~current_umask();
4510 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4511 if (IS_ERR(state)) {
4512 status = PTR_ERR(state);
4516 nfs4_label_release_security(ilabel);
4517 put_nfs_open_context(ctx);
4522 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4524 struct nfs_server *server = NFS_SERVER(dir);
4525 struct nfs_removeargs args = {
4529 struct nfs_removeres res = {
4532 struct rpc_message msg = {
4533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4537 unsigned long timestamp = jiffies;
4540 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4542 spin_lock(&dir->i_lock);
4543 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
4544 /* Removing a directory decrements nlink in the parent */
4545 if (ftype == NF4DIR && dir->i_nlink > 2)
4546 nfs4_dec_nlink_locked(dir);
4547 spin_unlock(&dir->i_lock);
4552 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4554 struct nfs4_exception exception = {
4555 .interruptible = true,
4557 struct inode *inode = d_inode(dentry);
4561 if (inode->i_nlink == 1)
4562 nfs4_inode_return_delegation(inode);
4564 nfs4_inode_make_writeable(inode);
4567 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4568 trace_nfs4_remove(dir, &dentry->d_name, err);
4569 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4571 } while (exception.retry);
4575 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4577 struct nfs4_exception exception = {
4578 .interruptible = true,
4583 err = _nfs4_proc_remove(dir, name, NF4DIR);
4584 trace_nfs4_remove(dir, name, err);
4585 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4587 } while (exception.retry);
4591 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4592 struct dentry *dentry,
4593 struct inode *inode)
4595 struct nfs_removeargs *args = msg->rpc_argp;
4596 struct nfs_removeres *res = msg->rpc_resp;
4598 res->server = NFS_SB(dentry->d_sb);
4599 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4600 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4602 nfs_fattr_init(res->dir_attr);
4605 nfs4_inode_return_delegation(inode);
4608 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4610 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4611 &data->args.seq_args,
4616 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4618 struct nfs_unlinkdata *data = task->tk_calldata;
4619 struct nfs_removeres *res = &data->res;
4621 if (!nfs4_sequence_done(task, &res->seq_res))
4623 if (nfs4_async_handle_error(task, res->server, NULL,
4624 &data->timeout) == -EAGAIN)
4626 if (task->tk_status == 0)
4627 update_changeattr(dir, &res->cinfo,
4628 res->dir_attr->time_start, 0);
4632 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4633 struct dentry *old_dentry,
4634 struct dentry *new_dentry)
4636 struct nfs_renameargs *arg = msg->rpc_argp;
4637 struct nfs_renameres *res = msg->rpc_resp;
4638 struct inode *old_inode = d_inode(old_dentry);
4639 struct inode *new_inode = d_inode(new_dentry);
4642 nfs4_inode_make_writeable(old_inode);
4644 nfs4_inode_return_delegation(new_inode);
4645 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4646 res->server = NFS_SB(old_dentry->d_sb);
4647 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4650 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4652 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4653 &data->args.seq_args,
4658 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4659 struct inode *new_dir)
4661 struct nfs_renamedata *data = task->tk_calldata;
4662 struct nfs_renameres *res = &data->res;
4664 if (!nfs4_sequence_done(task, &res->seq_res))
4666 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4669 if (task->tk_status == 0) {
4670 if (new_dir != old_dir) {
4671 /* Note: If we moved a directory, nlink will change */
4672 update_changeattr(old_dir, &res->old_cinfo,
4673 res->old_fattr->time_start,
4674 NFS_INO_INVALID_OTHER);
4675 update_changeattr(new_dir, &res->new_cinfo,
4676 res->new_fattr->time_start,
4677 NFS_INO_INVALID_OTHER);
4679 update_changeattr(old_dir, &res->old_cinfo,
4680 res->old_fattr->time_start,
4686 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4688 struct nfs_server *server = NFS_SERVER(inode);
4689 __u32 bitmask[NFS4_BITMASK_SZ];
4690 struct nfs4_link_arg arg = {
4691 .fh = NFS_FH(inode),
4692 .dir_fh = NFS_FH(dir),
4696 struct nfs4_link_res res = {
4700 struct rpc_message msg = {
4701 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4705 int status = -ENOMEM;
4707 res.fattr = nfs_alloc_fattr();
4708 if (res.fattr == NULL)
4711 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4712 if (IS_ERR(res.label)) {
4713 status = PTR_ERR(res.label);
4717 nfs4_inode_make_writeable(inode);
4718 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4720 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4722 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
4723 status = nfs_post_op_update_inode(inode, res.fattr);
4725 nfs_setsecurity(inode, res.fattr, res.label);
4729 nfs4_label_free(res.label);
4732 nfs_free_fattr(res.fattr);
4736 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4738 struct nfs4_exception exception = {
4739 .interruptible = true,
4743 err = nfs4_handle_exception(NFS_SERVER(inode),
4744 _nfs4_proc_link(inode, dir, name),
4746 } while (exception.retry);
4750 struct nfs4_createdata {
4751 struct rpc_message msg;
4752 struct nfs4_create_arg arg;
4753 struct nfs4_create_res res;
4755 struct nfs_fattr fattr;
4756 struct nfs4_label *label;
4759 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4760 const struct qstr *name, struct iattr *sattr, u32 ftype)
4762 struct nfs4_createdata *data;
4764 data = kzalloc(sizeof(*data), GFP_KERNEL);
4766 struct nfs_server *server = NFS_SERVER(dir);
4768 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4769 if (IS_ERR(data->label))
4772 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4773 data->msg.rpc_argp = &data->arg;
4774 data->msg.rpc_resp = &data->res;
4775 data->arg.dir_fh = NFS_FH(dir);
4776 data->arg.server = server;
4777 data->arg.name = name;
4778 data->arg.attrs = sattr;
4779 data->arg.ftype = ftype;
4780 data->arg.bitmask = nfs4_bitmask(server, data->label);
4781 data->arg.umask = current_umask();
4782 data->res.server = server;
4783 data->res.fh = &data->fh;
4784 data->res.fattr = &data->fattr;
4785 data->res.label = data->label;
4786 nfs_fattr_init(data->res.fattr);
4794 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4796 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4797 &data->arg.seq_args, &data->res.seq_res, 1);
4799 spin_lock(&dir->i_lock);
4800 update_changeattr_locked(dir, &data->res.dir_cinfo,
4801 data->res.fattr->time_start, 0);
4802 /* Creating a directory bumps nlink in the parent */
4803 if (data->arg.ftype == NF4DIR)
4804 nfs4_inc_nlink_locked(dir);
4805 spin_unlock(&dir->i_lock);
4806 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4811 static void nfs4_free_createdata(struct nfs4_createdata *data)
4813 nfs4_label_free(data->label);
4817 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4818 struct page *page, unsigned int len, struct iattr *sattr,
4819 struct nfs4_label *label)
4821 struct nfs4_createdata *data;
4822 int status = -ENAMETOOLONG;
4824 if (len > NFS4_MAXPATHLEN)
4828 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4832 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4833 data->arg.u.symlink.pages = &page;
4834 data->arg.u.symlink.len = len;
4835 data->arg.label = label;
4837 status = nfs4_do_create(dir, dentry, data);
4839 nfs4_free_createdata(data);
4844 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4845 struct page *page, unsigned int len, struct iattr *sattr)
4847 struct nfs4_exception exception = {
4848 .interruptible = true,
4850 struct nfs4_label l, *label = NULL;
4853 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4856 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4857 trace_nfs4_symlink(dir, &dentry->d_name, err);
4858 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4860 } while (exception.retry);
4862 nfs4_label_release_security(label);
4866 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4867 struct iattr *sattr, struct nfs4_label *label)
4869 struct nfs4_createdata *data;
4870 int status = -ENOMEM;
4872 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4876 data->arg.label = label;
4877 status = nfs4_do_create(dir, dentry, data);
4879 nfs4_free_createdata(data);
4884 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4885 struct iattr *sattr)
4887 struct nfs_server *server = NFS_SERVER(dir);
4888 struct nfs4_exception exception = {
4889 .interruptible = true,
4891 struct nfs4_label l, *label = NULL;
4894 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4896 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4897 sattr->ia_mode &= ~current_umask();
4899 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4900 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4901 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4903 } while (exception.retry);
4904 nfs4_label_release_security(label);
4909 static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4910 u64 cookie, struct page **pages, unsigned int count, bool plus)
4912 struct inode *dir = d_inode(dentry);
4913 struct nfs4_readdir_arg args = {
4918 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4921 struct nfs4_readdir_res res;
4922 struct rpc_message msg = {
4923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4930 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4932 (unsigned long long)cookie);
4933 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4934 res.pgbase = args.pgbase;
4935 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4937 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4938 status += args.pgbase;
4941 nfs_invalidate_atime(dir);
4943 dprintk("%s: returns %d\n", __func__, status);
4947 static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4948 u64 cookie, struct page **pages, unsigned int count, bool plus)
4950 struct nfs4_exception exception = {
4951 .interruptible = true,
4955 err = _nfs4_proc_readdir(dentry, cred, cookie,
4956 pages, count, plus);
4957 trace_nfs4_readdir(d_inode(dentry), err);
4958 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4960 } while (exception.retry);
4964 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4965 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4967 struct nfs4_createdata *data;
4968 int mode = sattr->ia_mode;
4969 int status = -ENOMEM;
4971 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4976 data->arg.ftype = NF4FIFO;
4977 else if (S_ISBLK(mode)) {
4978 data->arg.ftype = NF4BLK;
4979 data->arg.u.device.specdata1 = MAJOR(rdev);
4980 data->arg.u.device.specdata2 = MINOR(rdev);
4982 else if (S_ISCHR(mode)) {
4983 data->arg.ftype = NF4CHR;
4984 data->arg.u.device.specdata1 = MAJOR(rdev);
4985 data->arg.u.device.specdata2 = MINOR(rdev);
4986 } else if (!S_ISSOCK(mode)) {
4991 data->arg.label = label;
4992 status = nfs4_do_create(dir, dentry, data);
4994 nfs4_free_createdata(data);
4999 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5000 struct iattr *sattr, dev_t rdev)
5002 struct nfs_server *server = NFS_SERVER(dir);
5003 struct nfs4_exception exception = {
5004 .interruptible = true,
5006 struct nfs4_label l, *label = NULL;
5009 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5011 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5012 sattr->ia_mode &= ~current_umask();
5014 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5015 trace_nfs4_mknod(dir, &dentry->d_name, err);
5016 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5018 } while (exception.retry);
5020 nfs4_label_release_security(label);
5025 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5026 struct nfs_fsstat *fsstat)
5028 struct nfs4_statfs_arg args = {
5030 .bitmask = server->attr_bitmask,
5032 struct nfs4_statfs_res res = {
5035 struct rpc_message msg = {
5036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5041 nfs_fattr_init(fsstat->fattr);
5042 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5045 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5047 struct nfs4_exception exception = {
5048 .interruptible = true,
5052 err = nfs4_handle_exception(server,
5053 _nfs4_proc_statfs(server, fhandle, fsstat),
5055 } while (exception.retry);
5059 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5060 struct nfs_fsinfo *fsinfo)
5062 struct nfs4_fsinfo_arg args = {
5064 .bitmask = server->attr_bitmask,
5066 struct nfs4_fsinfo_res res = {
5069 struct rpc_message msg = {
5070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5075 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5078 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5080 struct nfs4_exception exception = {
5081 .interruptible = true,
5086 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5087 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5089 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5092 err = nfs4_handle_exception(server, err, &exception);
5093 } while (exception.retry);
5097 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5101 nfs_fattr_init(fsinfo->fattr);
5102 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5104 /* block layout checks this! */
5105 server->pnfs_blksize = fsinfo->blksize;
5106 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5112 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5113 struct nfs_pathconf *pathconf)
5115 struct nfs4_pathconf_arg args = {
5117 .bitmask = server->attr_bitmask,
5119 struct nfs4_pathconf_res res = {
5120 .pathconf = pathconf,
5122 struct rpc_message msg = {
5123 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5128 /* None of the pathconf attributes are mandatory to implement */
5129 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5130 memset(pathconf, 0, sizeof(*pathconf));
5134 nfs_fattr_init(pathconf->fattr);
5135 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5138 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5139 struct nfs_pathconf *pathconf)
5141 struct nfs4_exception exception = {
5142 .interruptible = true,
5147 err = nfs4_handle_exception(server,
5148 _nfs4_proc_pathconf(server, fhandle, pathconf),
5150 } while (exception.retry);
5154 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5155 const struct nfs_open_context *ctx,
5156 const struct nfs_lock_context *l_ctx,
5159 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5161 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5163 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5164 const struct nfs_open_context *ctx,
5165 const struct nfs_lock_context *l_ctx,
5168 nfs4_stateid _current_stateid;
5170 /* If the current stateid represents a lost lock, then exit */
5171 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5173 return nfs4_stateid_match(stateid, &_current_stateid);
5176 static bool nfs4_error_stateid_expired(int err)
5179 case -NFS4ERR_DELEG_REVOKED:
5180 case -NFS4ERR_ADMIN_REVOKED:
5181 case -NFS4ERR_BAD_STATEID:
5182 case -NFS4ERR_STALE_STATEID:
5183 case -NFS4ERR_OLD_STATEID:
5184 case -NFS4ERR_OPENMODE:
5185 case -NFS4ERR_EXPIRED:
5191 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5193 struct nfs_server *server = NFS_SERVER(hdr->inode);
5195 trace_nfs4_read(hdr, task->tk_status);
5196 if (task->tk_status < 0) {
5197 struct nfs4_exception exception = {
5198 .inode = hdr->inode,
5199 .state = hdr->args.context->state,
5200 .stateid = &hdr->args.stateid,
5202 task->tk_status = nfs4_async_handle_exception(task,
5203 server, task->tk_status, &exception);
5204 if (exception.retry) {
5205 rpc_restart_call_prepare(task);
5210 if (task->tk_status > 0)
5211 renew_lease(server, hdr->timestamp);
5215 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5216 struct nfs_pgio_args *args)
5219 if (!nfs4_error_stateid_expired(task->tk_status) ||
5220 nfs4_stateid_is_current(&args->stateid,
5225 rpc_restart_call_prepare(task);
5229 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5232 dprintk("--> %s\n", __func__);
5234 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5236 if (nfs4_read_stateid_changed(task, &hdr->args))
5238 if (task->tk_status > 0)
5239 nfs_invalidate_atime(hdr->inode);
5240 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5241 nfs4_read_done_cb(task, hdr);
5244 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5245 struct rpc_message *msg)
5247 hdr->timestamp = jiffies;
5248 if (!hdr->pgio_done_cb)
5249 hdr->pgio_done_cb = nfs4_read_done_cb;
5250 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5251 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5254 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5255 struct nfs_pgio_header *hdr)
5257 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5258 &hdr->args.seq_args,
5262 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5263 hdr->args.lock_context,
5264 hdr->rw_mode) == -EIO)
5266 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5271 static int nfs4_write_done_cb(struct rpc_task *task,
5272 struct nfs_pgio_header *hdr)
5274 struct inode *inode = hdr->inode;
5276 trace_nfs4_write(hdr, task->tk_status);
5277 if (task->tk_status < 0) {
5278 struct nfs4_exception exception = {
5279 .inode = hdr->inode,
5280 .state = hdr->args.context->state,
5281 .stateid = &hdr->args.stateid,
5283 task->tk_status = nfs4_async_handle_exception(task,
5284 NFS_SERVER(inode), task->tk_status,
5286 if (exception.retry) {
5287 rpc_restart_call_prepare(task);
5291 if (task->tk_status >= 0) {
5292 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5293 nfs_writeback_update_inode(hdr);
5298 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5299 struct nfs_pgio_args *args)
5302 if (!nfs4_error_stateid_expired(task->tk_status) ||
5303 nfs4_stateid_is_current(&args->stateid,
5308 rpc_restart_call_prepare(task);
5312 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5314 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5316 if (nfs4_write_stateid_changed(task, &hdr->args))
5318 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5319 nfs4_write_done_cb(task, hdr);
5323 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5325 /* Don't request attributes for pNFS or O_DIRECT writes */
5326 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5328 /* Otherwise, request attributes if and only if we don't hold
5331 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5334 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5335 struct rpc_message *msg,
5336 struct rpc_clnt **clnt)
5338 struct nfs_server *server = NFS_SERVER(hdr->inode);
5340 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5341 hdr->args.bitmask = NULL;
5342 hdr->res.fattr = NULL;
5344 hdr->args.bitmask = server->cache_consistency_bitmask;
5346 if (!hdr->pgio_done_cb)
5347 hdr->pgio_done_cb = nfs4_write_done_cb;
5348 hdr->res.server = server;
5349 hdr->timestamp = jiffies;
5351 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5352 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5353 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5356 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5358 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5359 &data->args.seq_args,
5364 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5366 struct inode *inode = data->inode;
5368 trace_nfs4_commit(data, task->tk_status);
5369 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5370 NULL, NULL) == -EAGAIN) {
5371 rpc_restart_call_prepare(task);
5377 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5379 if (!nfs4_sequence_done(task, &data->res.seq_res))
5381 return data->commit_done_cb(task, data);
5384 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5385 struct rpc_clnt **clnt)
5387 struct nfs_server *server = NFS_SERVER(data->inode);
5389 if (data->commit_done_cb == NULL)
5390 data->commit_done_cb = nfs4_commit_done_cb;
5391 data->res.server = server;
5392 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5393 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5394 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5397 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5398 struct nfs_commitres *res)
5400 struct inode *dst_inode = file_inode(dst);
5401 struct nfs_server *server = NFS_SERVER(dst_inode);
5402 struct rpc_message msg = {
5403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5408 args->fh = NFS_FH(dst_inode);
5409 return nfs4_call_sync(server->client, server, &msg,
5410 &args->seq_args, &res->seq_res, 1);
5413 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5415 struct nfs_commitargs args = {
5419 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5420 struct nfs4_exception exception = { };
5424 status = _nfs4_proc_commit(dst, &args, res);
5425 status = nfs4_handle_exception(dst_server, status, &exception);
5426 } while (exception.retry);
5431 struct nfs4_renewdata {
5432 struct nfs_client *client;
5433 unsigned long timestamp;
5437 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5438 * standalone procedure for queueing an asynchronous RENEW.
5440 static void nfs4_renew_release(void *calldata)
5442 struct nfs4_renewdata *data = calldata;
5443 struct nfs_client *clp = data->client;
5445 if (refcount_read(&clp->cl_count) > 1)
5446 nfs4_schedule_state_renewal(clp);
5447 nfs_put_client(clp);
5451 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5453 struct nfs4_renewdata *data = calldata;
5454 struct nfs_client *clp = data->client;
5455 unsigned long timestamp = data->timestamp;
5457 trace_nfs4_renew_async(clp, task->tk_status);
5458 switch (task->tk_status) {
5461 case -NFS4ERR_LEASE_MOVED:
5462 nfs4_schedule_lease_moved_recovery(clp);
5465 /* Unless we're shutting down, schedule state recovery! */
5466 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5468 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5469 nfs4_schedule_lease_recovery(clp);
5472 nfs4_schedule_path_down_recovery(clp);
5474 do_renew_lease(clp, timestamp);
5477 static const struct rpc_call_ops nfs4_renew_ops = {
5478 .rpc_call_done = nfs4_renew_done,
5479 .rpc_release = nfs4_renew_release,
5482 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5484 struct rpc_message msg = {
5485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5489 struct nfs4_renewdata *data;
5491 if (renew_flags == 0)
5493 if (!refcount_inc_not_zero(&clp->cl_count))
5495 data = kmalloc(sizeof(*data), GFP_NOFS);
5497 nfs_put_client(clp);
5501 data->timestamp = jiffies;
5502 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5503 &nfs4_renew_ops, data);
5506 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5508 struct rpc_message msg = {
5509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5513 unsigned long now = jiffies;
5516 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5519 do_renew_lease(clp, now);
5523 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5525 return server->caps & NFS_CAP_ACLS;
5528 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5529 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5532 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5534 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5535 struct page **pages)
5537 struct page *newpage, **spages;
5543 len = min_t(size_t, PAGE_SIZE, buflen);
5544 newpage = alloc_page(GFP_KERNEL);
5546 if (newpage == NULL)
5548 memcpy(page_address(newpage), buf, len);
5553 } while (buflen != 0);
5559 __free_page(spages[rc-1]);
5563 struct nfs4_cached_acl {
5569 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5571 struct nfs_inode *nfsi = NFS_I(inode);
5573 spin_lock(&inode->i_lock);
5574 kfree(nfsi->nfs4_acl);
5575 nfsi->nfs4_acl = acl;
5576 spin_unlock(&inode->i_lock);
5579 static void nfs4_zap_acl_attr(struct inode *inode)
5581 nfs4_set_cached_acl(inode, NULL);
5584 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5586 struct nfs_inode *nfsi = NFS_I(inode);
5587 struct nfs4_cached_acl *acl;
5590 spin_lock(&inode->i_lock);
5591 acl = nfsi->nfs4_acl;
5594 if (buf == NULL) /* user is just asking for length */
5596 if (acl->cached == 0)
5598 ret = -ERANGE; /* see getxattr(2) man page */
5599 if (acl->len > buflen)
5601 memcpy(buf, acl->data, acl->len);
5605 spin_unlock(&inode->i_lock);
5609 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5611 struct nfs4_cached_acl *acl;
5612 size_t buflen = sizeof(*acl) + acl_len;
5614 if (buflen <= PAGE_SIZE) {
5615 acl = kmalloc(buflen, GFP_KERNEL);
5619 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5621 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5628 nfs4_set_cached_acl(inode, acl);
5632 * The getxattr API returns the required buffer length when called with a
5633 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5634 * the required buf. On a NULL buf, we send a page of data to the server
5635 * guessing that the ACL request can be serviced by a page. If so, we cache
5636 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5637 * the cache. If not so, we throw away the page, and cache the required
5638 * length. The next getxattr call will then produce another round trip to
5639 * the server, this time with the input buf of the required size.
5641 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5643 struct page **pages;
5644 struct nfs_getaclargs args = {
5645 .fh = NFS_FH(inode),
5648 struct nfs_getaclres res = {
5651 struct rpc_message msg = {
5652 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5656 unsigned int npages;
5657 int ret = -ENOMEM, i;
5658 struct nfs_server *server = NFS_SERVER(inode);
5661 buflen = server->rsize;
5663 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5664 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5668 args.acl_pages = pages;
5670 for (i = 0; i < npages; i++) {
5671 pages[i] = alloc_page(GFP_KERNEL);
5676 /* for decoding across pages */
5677 res.acl_scratch = alloc_page(GFP_KERNEL);
5678 if (!res.acl_scratch)
5681 args.acl_len = npages * PAGE_SIZE;
5683 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5684 __func__, buf, buflen, npages, args.acl_len);
5685 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5686 &msg, &args.seq_args, &res.seq_res, 0);
5690 /* Handle the case where the passed-in buffer is too short */
5691 if (res.acl_flags & NFS4_ACL_TRUNC) {
5692 /* Did the user only issue a request for the acl length? */
5698 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5700 if (res.acl_len > buflen) {
5704 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5709 for (i = 0; i < npages; i++)
5711 __free_page(pages[i]);
5712 if (res.acl_scratch)
5713 __free_page(res.acl_scratch);
5718 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5720 struct nfs4_exception exception = {
5721 .interruptible = true,
5725 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5726 trace_nfs4_get_acl(inode, ret);
5729 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5730 } while (exception.retry);
5734 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5736 struct nfs_server *server = NFS_SERVER(inode);
5739 if (!nfs4_server_supports_acls(server))
5741 ret = nfs_revalidate_inode(server, inode);
5744 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5745 nfs_zap_acl_cache(inode);
5746 ret = nfs4_read_cached_acl(inode, buf, buflen);
5748 /* -ENOENT is returned if there is no ACL or if there is an ACL
5749 * but no cached acl data, just the acl length */
5751 return nfs4_get_acl_uncached(inode, buf, buflen);
5754 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5756 struct nfs_server *server = NFS_SERVER(inode);
5757 struct page *pages[NFS4ACL_MAXPAGES];
5758 struct nfs_setaclargs arg = {
5759 .fh = NFS_FH(inode),
5763 struct nfs_setaclres res;
5764 struct rpc_message msg = {
5765 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5769 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5772 if (!nfs4_server_supports_acls(server))
5774 if (npages > ARRAY_SIZE(pages))
5776 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5779 nfs4_inode_make_writeable(inode);
5780 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5783 * Free each page after tx, so the only ref left is
5784 * held by the network stack
5787 put_page(pages[i-1]);
5790 * Acl update can result in inode attribute update.
5791 * so mark the attribute cache invalid.
5793 spin_lock(&inode->i_lock);
5794 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5795 | NFS_INO_INVALID_CTIME
5796 | NFS_INO_REVAL_FORCED;
5797 spin_unlock(&inode->i_lock);
5798 nfs_access_zap_cache(inode);
5799 nfs_zap_acl_cache(inode);
5803 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5805 struct nfs4_exception exception = { };
5808 err = __nfs4_proc_set_acl(inode, buf, buflen);
5809 trace_nfs4_set_acl(inode, err);
5810 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5812 } while (exception.retry);
5816 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5817 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5820 struct nfs_server *server = NFS_SERVER(inode);
5821 struct nfs_fattr fattr;
5822 struct nfs4_label label = {0, 0, buflen, buf};
5824 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5825 struct nfs4_getattr_arg arg = {
5826 .fh = NFS_FH(inode),
5829 struct nfs4_getattr_res res = {
5834 struct rpc_message msg = {
5835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5841 nfs_fattr_init(&fattr);
5843 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5846 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5848 if (buflen < label.len)
5853 static int nfs4_get_security_label(struct inode *inode, void *buf,
5856 struct nfs4_exception exception = {
5857 .interruptible = true,
5861 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5865 err = _nfs4_get_security_label(inode, buf, buflen);
5866 trace_nfs4_get_security_label(inode, err);
5867 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5869 } while (exception.retry);
5873 static int _nfs4_do_set_security_label(struct inode *inode,
5874 struct nfs4_label *ilabel,
5875 struct nfs_fattr *fattr,
5876 struct nfs4_label *olabel)
5879 struct iattr sattr = {0};
5880 struct nfs_server *server = NFS_SERVER(inode);
5881 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5882 struct nfs_setattrargs arg = {
5883 .fh = NFS_FH(inode),
5889 struct nfs_setattrres res = {
5894 struct rpc_message msg = {
5895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5901 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5903 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5905 dprintk("%s failed: %d\n", __func__, status);
5910 static int nfs4_do_set_security_label(struct inode *inode,
5911 struct nfs4_label *ilabel,
5912 struct nfs_fattr *fattr,
5913 struct nfs4_label *olabel)
5915 struct nfs4_exception exception = { };
5919 err = _nfs4_do_set_security_label(inode, ilabel,
5921 trace_nfs4_set_security_label(inode, err);
5922 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5924 } while (exception.retry);
5929 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5931 struct nfs4_label ilabel, *olabel = NULL;
5932 struct nfs_fattr fattr;
5935 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5938 nfs_fattr_init(&fattr);
5942 ilabel.label = (char *)buf;
5943 ilabel.len = buflen;
5945 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5946 if (IS_ERR(olabel)) {
5947 status = -PTR_ERR(olabel);
5951 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5953 nfs_setsecurity(inode, &fattr, olabel);
5955 nfs4_label_free(olabel);
5959 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5962 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5963 nfs4_verifier *bootverf)
5967 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5968 /* An impossible timestamp guarantees this value
5969 * will never match a generated boot time. */
5970 verf[0] = cpu_to_be32(U32_MAX);
5971 verf[1] = cpu_to_be32(U32_MAX);
5973 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5974 u64 ns = ktime_to_ns(nn->boot_time);
5976 verf[0] = cpu_to_be32(ns >> 32);
5977 verf[1] = cpu_to_be32(ns);
5979 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5983 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5988 if (clp->cl_owner_id != NULL)
5993 strlen(clp->cl_rpcclient->cl_nodename) +
5995 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5998 if (nfs4_client_id_uniquifier[0] != '\0')
5999 len += strlen(nfs4_client_id_uniquifier) + 1;
6000 if (len > NFS4_OPAQUE_LIMIT + 1)
6004 * Since this string is allocated at mount time, and held until the
6005 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6006 * about a memory-reclaim deadlock.
6008 str = kmalloc(len, GFP_KERNEL);
6013 if (nfs4_client_id_uniquifier[0] != '\0')
6014 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6015 clp->cl_rpcclient->cl_nodename,
6016 nfs4_client_id_uniquifier,
6017 rpc_peeraddr2str(clp->cl_rpcclient,
6020 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6021 clp->cl_rpcclient->cl_nodename,
6022 rpc_peeraddr2str(clp->cl_rpcclient,
6026 clp->cl_owner_id = str;
6031 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
6036 len = 10 + 10 + 1 + 10 + 1 +
6037 strlen(nfs4_client_id_uniquifier) + 1 +
6038 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6040 if (len > NFS4_OPAQUE_LIMIT + 1)
6044 * Since this string is allocated at mount time, and held until the
6045 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6046 * about a memory-reclaim deadlock.
6048 str = kmalloc(len, GFP_KERNEL);
6052 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6053 clp->rpc_ops->version, clp->cl_minorversion,
6054 nfs4_client_id_uniquifier,
6055 clp->cl_rpcclient->cl_nodename);
6056 clp->cl_owner_id = str;
6061 nfs4_init_uniform_client_string(struct nfs_client *clp)
6066 if (clp->cl_owner_id != NULL)
6069 if (nfs4_client_id_uniquifier[0] != '\0')
6070 return nfs4_init_uniquifier_client_string(clp);
6072 len = 10 + 10 + 1 + 10 + 1 +
6073 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6075 if (len > NFS4_OPAQUE_LIMIT + 1)
6079 * Since this string is allocated at mount time, and held until the
6080 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6081 * about a memory-reclaim deadlock.
6083 str = kmalloc(len, GFP_KERNEL);
6087 scnprintf(str, len, "Linux NFSv%u.%u %s",
6088 clp->rpc_ops->version, clp->cl_minorversion,
6089 clp->cl_rpcclient->cl_nodename);
6090 clp->cl_owner_id = str;
6095 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6096 * services. Advertise one based on the address family of the
6100 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6102 if (strchr(clp->cl_ipaddr, ':') != NULL)
6103 return scnprintf(buf, len, "tcp6");
6105 return scnprintf(buf, len, "tcp");
6108 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6110 struct nfs4_setclientid *sc = calldata;
6112 if (task->tk_status == 0)
6113 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6116 static const struct rpc_call_ops nfs4_setclientid_ops = {
6117 .rpc_call_done = nfs4_setclientid_done,
6121 * nfs4_proc_setclientid - Negotiate client ID
6122 * @clp: state data structure
6123 * @program: RPC program for NFSv4 callback service
6124 * @port: IP port number for NFS4 callback service
6125 * @cred: credential to use for this call
6126 * @res: where to place the result
6128 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6130 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6131 unsigned short port, const struct cred *cred,
6132 struct nfs4_setclientid_res *res)
6134 nfs4_verifier sc_verifier;
6135 struct nfs4_setclientid setclientid = {
6136 .sc_verifier = &sc_verifier,
6140 struct rpc_message msg = {
6141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6142 .rpc_argp = &setclientid,
6146 struct rpc_task_setup task_setup_data = {
6147 .rpc_client = clp->cl_rpcclient,
6148 .rpc_message = &msg,
6149 .callback_ops = &nfs4_setclientid_ops,
6150 .callback_data = &setclientid,
6151 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6153 unsigned long now = jiffies;
6156 /* nfs_client_id4 */
6157 nfs4_init_boot_verifier(clp, &sc_verifier);
6159 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6160 status = nfs4_init_uniform_client_string(clp);
6162 status = nfs4_init_nonuniform_client_string(clp);
6168 setclientid.sc_netid_len =
6169 nfs4_init_callback_netid(clp,
6170 setclientid.sc_netid,
6171 sizeof(setclientid.sc_netid));
6172 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6173 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6174 clp->cl_ipaddr, port >> 8, port & 255);
6176 dprintk("NFS call setclientid auth=%s, '%s'\n",
6177 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6180 status = nfs4_call_sync_custom(&task_setup_data);
6181 if (setclientid.sc_cred) {
6182 kfree(clp->cl_acceptor);
6183 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6184 put_rpccred(setclientid.sc_cred);
6188 do_renew_lease(clp, now);
6190 trace_nfs4_setclientid(clp, status);
6191 dprintk("NFS reply setclientid: %d\n", status);
6196 * nfs4_proc_setclientid_confirm - Confirm client ID
6197 * @clp: state data structure
6198 * @arg: result of a previous SETCLIENTID
6199 * @cred: credential to use for this call
6201 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6203 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6204 struct nfs4_setclientid_res *arg,
6205 const struct cred *cred)
6207 struct rpc_message msg = {
6208 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6214 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6215 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6217 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6218 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6219 trace_nfs4_setclientid_confirm(clp, status);
6220 dprintk("NFS reply setclientid_confirm: %d\n", status);
6224 struct nfs4_delegreturndata {
6225 struct nfs4_delegreturnargs args;
6226 struct nfs4_delegreturnres res;
6228 nfs4_stateid stateid;
6229 unsigned long timestamp;
6231 struct nfs4_layoutreturn_args arg;
6232 struct nfs4_layoutreturn_res res;
6233 struct nfs4_xdr_opaque_data ld_private;
6237 struct nfs_fattr fattr;
6239 struct inode *inode;
6242 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6244 struct nfs4_delegreturndata *data = calldata;
6245 struct nfs4_exception exception = {
6246 .inode = data->inode,
6247 .stateid = &data->stateid,
6250 if (!nfs4_sequence_done(task, &data->res.seq_res))
6253 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6255 /* Handle Layoutreturn errors */
6256 if (pnfs_roc_done(task, data->inode,
6257 &data->args.lr_args,
6259 &data->res.lr_ret) == -EAGAIN)
6262 switch (task->tk_status) {
6264 renew_lease(data->res.server, data->timestamp);
6266 case -NFS4ERR_ADMIN_REVOKED:
6267 case -NFS4ERR_DELEG_REVOKED:
6268 case -NFS4ERR_EXPIRED:
6269 nfs4_free_revoked_stateid(data->res.server,
6271 task->tk_msg.rpc_cred);
6273 case -NFS4ERR_BAD_STATEID:
6274 case -NFS4ERR_STALE_STATEID:
6276 task->tk_status = 0;
6278 case -NFS4ERR_OLD_STATEID:
6279 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6280 nfs4_stateid_seqid_inc(&data->stateid);
6281 if (data->args.bitmask) {
6282 data->args.bitmask = NULL;
6283 data->res.fattr = NULL;
6286 case -NFS4ERR_ACCESS:
6287 if (data->args.bitmask) {
6288 data->args.bitmask = NULL;
6289 data->res.fattr = NULL;
6294 task->tk_status = nfs4_async_handle_exception(task,
6295 data->res.server, task->tk_status,
6297 if (exception.retry)
6300 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6301 data->rpc_status = task->tk_status;
6304 task->tk_status = 0;
6305 rpc_restart_call_prepare(task);
6308 static void nfs4_delegreturn_release(void *calldata)
6310 struct nfs4_delegreturndata *data = calldata;
6311 struct inode *inode = data->inode;
6315 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6317 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6318 nfs_iput_and_deactive(inode);
6323 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6325 struct nfs4_delegreturndata *d_data;
6326 struct pnfs_layout_hdr *lo;
6328 d_data = (struct nfs4_delegreturndata *)data;
6330 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6331 nfs4_sequence_done(task, &d_data->res.seq_res);
6335 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6336 if (lo && !pnfs_layout_is_valid(lo)) {
6337 d_data->args.lr_args = NULL;
6338 d_data->res.lr_res = NULL;
6341 nfs4_setup_sequence(d_data->res.server->nfs_client,
6342 &d_data->args.seq_args,
6343 &d_data->res.seq_res,
6347 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6348 .rpc_call_prepare = nfs4_delegreturn_prepare,
6349 .rpc_call_done = nfs4_delegreturn_done,
6350 .rpc_release = nfs4_delegreturn_release,
6353 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6355 struct nfs4_delegreturndata *data;
6356 struct nfs_server *server = NFS_SERVER(inode);
6357 struct rpc_task *task;
6358 struct rpc_message msg = {
6359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6362 struct rpc_task_setup task_setup_data = {
6363 .rpc_client = server->client,
6364 .rpc_message = &msg,
6365 .callback_ops = &nfs4_delegreturn_ops,
6366 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6370 data = kzalloc(sizeof(*data), GFP_NOFS);
6373 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6375 nfs4_state_protect(server->nfs_client,
6376 NFS_SP4_MACH_CRED_CLEANUP,
6377 &task_setup_data.rpc_client, &msg);
6379 data->args.fhandle = &data->fh;
6380 data->args.stateid = &data->stateid;
6381 data->args.bitmask = server->cache_consistency_bitmask;
6382 nfs_copy_fh(&data->fh, NFS_FH(inode));
6383 nfs4_stateid_copy(&data->stateid, stateid);
6384 data->res.fattr = &data->fattr;
6385 data->res.server = server;
6386 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6387 data->lr.arg.ld_private = &data->lr.ld_private;
6388 nfs_fattr_init(data->res.fattr);
6389 data->timestamp = jiffies;
6390 data->rpc_status = 0;
6391 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
6392 data->inode = nfs_igrab_and_active(inode);
6395 data->args.lr_args = &data->lr.arg;
6396 data->res.lr_res = &data->lr.res;
6398 } else if (data->lr.roc) {
6399 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6400 data->lr.roc = false;
6403 task_setup_data.callback_data = data;
6404 msg.rpc_argp = &data->args;
6405 msg.rpc_resp = &data->res;
6406 task = rpc_run_task(&task_setup_data);
6408 return PTR_ERR(task);
6411 status = rpc_wait_for_completion_task(task);
6414 status = data->rpc_status;
6420 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6422 struct nfs_server *server = NFS_SERVER(inode);
6423 struct nfs4_exception exception = { };
6426 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6427 trace_nfs4_delegreturn(inode, stateid, err);
6429 case -NFS4ERR_STALE_STATEID:
6430 case -NFS4ERR_EXPIRED:
6434 err = nfs4_handle_exception(server, err, &exception);
6435 } while (exception.retry);
6439 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6441 struct inode *inode = state->inode;
6442 struct nfs_server *server = NFS_SERVER(inode);
6443 struct nfs_client *clp = server->nfs_client;
6444 struct nfs_lockt_args arg = {
6445 .fh = NFS_FH(inode),
6448 struct nfs_lockt_res res = {
6451 struct rpc_message msg = {
6452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6455 .rpc_cred = state->owner->so_cred,
6457 struct nfs4_lock_state *lsp;
6460 arg.lock_owner.clientid = clp->cl_clientid;
6461 status = nfs4_set_lock_state(state, request);
6464 lsp = request->fl_u.nfs4_fl.owner;
6465 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6466 arg.lock_owner.s_dev = server->s_dev;
6467 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6470 request->fl_type = F_UNLCK;
6472 case -NFS4ERR_DENIED:
6475 request->fl_ops->fl_release_private(request);
6476 request->fl_ops = NULL;
6481 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6483 struct nfs4_exception exception = {
6484 .interruptible = true,
6489 err = _nfs4_proc_getlk(state, cmd, request);
6490 trace_nfs4_get_lock(request, state, cmd, err);
6491 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6493 } while (exception.retry);
6498 * Update the seqid of a lock stateid after receiving
6499 * NFS4ERR_OLD_STATEID
6501 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6502 struct nfs4_lock_state *lsp)
6504 struct nfs4_state *state = lsp->ls_state;
6507 spin_lock(&state->state_lock);
6508 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6510 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6511 nfs4_stateid_seqid_inc(dst);
6513 dst->seqid = lsp->ls_stateid.seqid;
6516 spin_unlock(&state->state_lock);
6520 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6521 struct nfs4_lock_state *lsp)
6523 struct nfs4_state *state = lsp->ls_state;
6526 spin_lock(&state->state_lock);
6527 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6528 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6529 spin_unlock(&state->state_lock);
6533 struct nfs4_unlockdata {
6534 struct nfs_locku_args arg;
6535 struct nfs_locku_res res;
6536 struct nfs4_lock_state *lsp;
6537 struct nfs_open_context *ctx;
6538 struct nfs_lock_context *l_ctx;
6539 struct file_lock fl;
6540 struct nfs_server *server;
6541 unsigned long timestamp;
6544 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6545 struct nfs_open_context *ctx,
6546 struct nfs4_lock_state *lsp,
6547 struct nfs_seqid *seqid)
6549 struct nfs4_unlockdata *p;
6550 struct nfs4_state *state = lsp->ls_state;
6551 struct inode *inode = state->inode;
6553 p = kzalloc(sizeof(*p), GFP_NOFS);
6556 p->arg.fh = NFS_FH(inode);
6558 p->arg.seqid = seqid;
6559 p->res.seqid = seqid;
6561 /* Ensure we don't close file until we're done freeing locks! */
6562 p->ctx = get_nfs_open_context(ctx);
6563 p->l_ctx = nfs_get_lock_context(ctx);
6564 locks_init_lock(&p->fl);
6565 locks_copy_lock(&p->fl, fl);
6566 p->server = NFS_SERVER(inode);
6567 spin_lock(&state->state_lock);
6568 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6569 spin_unlock(&state->state_lock);
6573 static void nfs4_locku_release_calldata(void *data)
6575 struct nfs4_unlockdata *calldata = data;
6576 nfs_free_seqid(calldata->arg.seqid);
6577 nfs4_put_lock_state(calldata->lsp);
6578 nfs_put_lock_context(calldata->l_ctx);
6579 put_nfs_open_context(calldata->ctx);
6583 static void nfs4_locku_done(struct rpc_task *task, void *data)
6585 struct nfs4_unlockdata *calldata = data;
6586 struct nfs4_exception exception = {
6587 .inode = calldata->lsp->ls_state->inode,
6588 .stateid = &calldata->arg.stateid,
6591 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6593 switch (task->tk_status) {
6595 renew_lease(calldata->server, calldata->timestamp);
6596 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6597 if (nfs4_update_lock_stateid(calldata->lsp,
6598 &calldata->res.stateid))
6601 case -NFS4ERR_ADMIN_REVOKED:
6602 case -NFS4ERR_EXPIRED:
6603 nfs4_free_revoked_stateid(calldata->server,
6604 &calldata->arg.stateid,
6605 task->tk_msg.rpc_cred);
6607 case -NFS4ERR_BAD_STATEID:
6608 case -NFS4ERR_STALE_STATEID:
6609 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6611 rpc_restart_call_prepare(task);
6613 case -NFS4ERR_OLD_STATEID:
6614 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6616 rpc_restart_call_prepare(task);
6619 task->tk_status = nfs4_async_handle_exception(task,
6620 calldata->server, task->tk_status,
6622 if (exception.retry)
6623 rpc_restart_call_prepare(task);
6625 nfs_release_seqid(calldata->arg.seqid);
6628 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6630 struct nfs4_unlockdata *calldata = data;
6632 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6633 nfs_async_iocounter_wait(task, calldata->l_ctx))
6636 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6638 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6639 /* Note: exit _without_ running nfs4_locku_done */
6642 calldata->timestamp = jiffies;
6643 if (nfs4_setup_sequence(calldata->server->nfs_client,
6644 &calldata->arg.seq_args,
6645 &calldata->res.seq_res,
6647 nfs_release_seqid(calldata->arg.seqid);
6650 task->tk_action = NULL;
6652 nfs4_sequence_done(task, &calldata->res.seq_res);
6655 static const struct rpc_call_ops nfs4_locku_ops = {
6656 .rpc_call_prepare = nfs4_locku_prepare,
6657 .rpc_call_done = nfs4_locku_done,
6658 .rpc_release = nfs4_locku_release_calldata,
6661 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6662 struct nfs_open_context *ctx,
6663 struct nfs4_lock_state *lsp,
6664 struct nfs_seqid *seqid)
6666 struct nfs4_unlockdata *data;
6667 struct rpc_message msg = {
6668 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6669 .rpc_cred = ctx->cred,
6671 struct rpc_task_setup task_setup_data = {
6672 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6673 .rpc_message = &msg,
6674 .callback_ops = &nfs4_locku_ops,
6675 .workqueue = nfsiod_workqueue,
6676 .flags = RPC_TASK_ASYNC,
6679 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6680 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6682 /* Ensure this is an unlock - when canceling a lock, the
6683 * canceled lock is passed in, and it won't be an unlock.
6685 fl->fl_type = F_UNLCK;
6686 if (fl->fl_flags & FL_CLOSE)
6687 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6689 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6691 nfs_free_seqid(seqid);
6692 return ERR_PTR(-ENOMEM);
6695 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6696 msg.rpc_argp = &data->arg;
6697 msg.rpc_resp = &data->res;
6698 task_setup_data.callback_data = data;
6699 return rpc_run_task(&task_setup_data);
6702 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6704 struct inode *inode = state->inode;
6705 struct nfs4_state_owner *sp = state->owner;
6706 struct nfs_inode *nfsi = NFS_I(inode);
6707 struct nfs_seqid *seqid;
6708 struct nfs4_lock_state *lsp;
6709 struct rpc_task *task;
6710 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6712 unsigned char fl_flags = request->fl_flags;
6714 status = nfs4_set_lock_state(state, request);
6715 /* Unlock _before_ we do the RPC call */
6716 request->fl_flags |= FL_EXISTS;
6717 /* Exclude nfs_delegation_claim_locks() */
6718 mutex_lock(&sp->so_delegreturn_mutex);
6719 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6720 down_read(&nfsi->rwsem);
6721 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6722 up_read(&nfsi->rwsem);
6723 mutex_unlock(&sp->so_delegreturn_mutex);
6726 up_read(&nfsi->rwsem);
6727 mutex_unlock(&sp->so_delegreturn_mutex);
6730 /* Is this a delegated lock? */
6731 lsp = request->fl_u.nfs4_fl.owner;
6732 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6734 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6735 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6739 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6740 status = PTR_ERR(task);
6743 status = rpc_wait_for_completion_task(task);
6746 request->fl_flags = fl_flags;
6747 trace_nfs4_unlock(request, state, F_SETLK, status);
6751 struct nfs4_lockdata {
6752 struct nfs_lock_args arg;
6753 struct nfs_lock_res res;
6754 struct nfs4_lock_state *lsp;
6755 struct nfs_open_context *ctx;
6756 struct file_lock fl;
6757 unsigned long timestamp;
6760 struct nfs_server *server;
6763 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6764 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6767 struct nfs4_lockdata *p;
6768 struct inode *inode = lsp->ls_state->inode;
6769 struct nfs_server *server = NFS_SERVER(inode);
6770 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6772 p = kzalloc(sizeof(*p), gfp_mask);
6776 p->arg.fh = NFS_FH(inode);
6778 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6779 if (IS_ERR(p->arg.open_seqid))
6781 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6782 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6783 if (IS_ERR(p->arg.lock_seqid))
6784 goto out_free_seqid;
6785 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6786 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6787 p->arg.lock_owner.s_dev = server->s_dev;
6788 p->res.lock_seqid = p->arg.lock_seqid;
6791 p->ctx = get_nfs_open_context(ctx);
6792 locks_init_lock(&p->fl);
6793 locks_copy_lock(&p->fl, fl);
6796 nfs_free_seqid(p->arg.open_seqid);
6802 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6804 struct nfs4_lockdata *data = calldata;
6805 struct nfs4_state *state = data->lsp->ls_state;
6807 dprintk("%s: begin!\n", __func__);
6808 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6810 /* Do we need to do an open_to_lock_owner? */
6811 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6812 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6813 goto out_release_lock_seqid;
6815 nfs4_stateid_copy(&data->arg.open_stateid,
6816 &state->open_stateid);
6817 data->arg.new_lock_owner = 1;
6818 data->res.open_seqid = data->arg.open_seqid;
6820 data->arg.new_lock_owner = 0;
6821 nfs4_stateid_copy(&data->arg.lock_stateid,
6822 &data->lsp->ls_stateid);
6824 if (!nfs4_valid_open_stateid(state)) {
6825 data->rpc_status = -EBADF;
6826 task->tk_action = NULL;
6827 goto out_release_open_seqid;
6829 data->timestamp = jiffies;
6830 if (nfs4_setup_sequence(data->server->nfs_client,
6831 &data->arg.seq_args,
6835 out_release_open_seqid:
6836 nfs_release_seqid(data->arg.open_seqid);
6837 out_release_lock_seqid:
6838 nfs_release_seqid(data->arg.lock_seqid);
6840 nfs4_sequence_done(task, &data->res.seq_res);
6841 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6844 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6846 struct nfs4_lockdata *data = calldata;
6847 struct nfs4_lock_state *lsp = data->lsp;
6849 dprintk("%s: begin!\n", __func__);
6851 if (!nfs4_sequence_done(task, &data->res.seq_res))
6854 data->rpc_status = task->tk_status;
6855 switch (task->tk_status) {
6857 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6859 if (data->arg.new_lock && !data->cancelled) {
6860 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6861 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6864 if (data->arg.new_lock_owner != 0) {
6865 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6866 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6867 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6868 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6871 case -NFS4ERR_BAD_STATEID:
6872 case -NFS4ERR_OLD_STATEID:
6873 case -NFS4ERR_STALE_STATEID:
6874 case -NFS4ERR_EXPIRED:
6875 if (data->arg.new_lock_owner != 0) {
6876 if (!nfs4_stateid_match(&data->arg.open_stateid,
6877 &lsp->ls_state->open_stateid))
6879 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6884 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6887 if (!data->cancelled)
6888 rpc_restart_call_prepare(task);
6892 static void nfs4_lock_release(void *calldata)
6894 struct nfs4_lockdata *data = calldata;
6896 dprintk("%s: begin!\n", __func__);
6897 nfs_free_seqid(data->arg.open_seqid);
6898 if (data->cancelled && data->rpc_status == 0) {
6899 struct rpc_task *task;
6900 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6901 data->arg.lock_seqid);
6903 rpc_put_task_async(task);
6904 dprintk("%s: cancelling lock!\n", __func__);
6906 nfs_free_seqid(data->arg.lock_seqid);
6907 nfs4_put_lock_state(data->lsp);
6908 put_nfs_open_context(data->ctx);
6910 dprintk("%s: done!\n", __func__);
6913 static const struct rpc_call_ops nfs4_lock_ops = {
6914 .rpc_call_prepare = nfs4_lock_prepare,
6915 .rpc_call_done = nfs4_lock_done,
6916 .rpc_release = nfs4_lock_release,
6919 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6922 case -NFS4ERR_ADMIN_REVOKED:
6923 case -NFS4ERR_EXPIRED:
6924 case -NFS4ERR_BAD_STATEID:
6925 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6926 if (new_lock_owner != 0 ||
6927 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6928 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6930 case -NFS4ERR_STALE_STATEID:
6931 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6932 nfs4_schedule_lease_recovery(server->nfs_client);
6936 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6938 struct nfs4_lockdata *data;
6939 struct rpc_task *task;
6940 struct rpc_message msg = {
6941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6942 .rpc_cred = state->owner->so_cred,
6944 struct rpc_task_setup task_setup_data = {
6945 .rpc_client = NFS_CLIENT(state->inode),
6946 .rpc_message = &msg,
6947 .callback_ops = &nfs4_lock_ops,
6948 .workqueue = nfsiod_workqueue,
6949 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
6953 dprintk("%s: begin!\n", __func__);
6954 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6955 fl->fl_u.nfs4_fl.owner,
6956 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6960 data->arg.block = 1;
6961 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6962 recovery_type > NFS_LOCK_NEW);
6963 msg.rpc_argp = &data->arg;
6964 msg.rpc_resp = &data->res;
6965 task_setup_data.callback_data = data;
6966 if (recovery_type > NFS_LOCK_NEW) {
6967 if (recovery_type == NFS_LOCK_RECLAIM)
6968 data->arg.reclaim = NFS_LOCK_RECLAIM;
6970 data->arg.new_lock = 1;
6971 task = rpc_run_task(&task_setup_data);
6973 return PTR_ERR(task);
6974 ret = rpc_wait_for_completion_task(task);
6976 ret = data->rpc_status;
6978 nfs4_handle_setlk_error(data->server, data->lsp,
6979 data->arg.new_lock_owner, ret);
6981 data->cancelled = true;
6983 dprintk("%s: done, ret = %d!\n", __func__, ret);
6984 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6988 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6990 struct nfs_server *server = NFS_SERVER(state->inode);
6991 struct nfs4_exception exception = {
6992 .inode = state->inode,
6997 /* Cache the lock if possible... */
6998 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7000 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7001 if (err != -NFS4ERR_DELAY)
7003 nfs4_handle_exception(server, err, &exception);
7004 } while (exception.retry);
7008 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7010 struct nfs_server *server = NFS_SERVER(state->inode);
7011 struct nfs4_exception exception = {
7012 .inode = state->inode,
7016 err = nfs4_set_lock_state(state, request);
7019 if (!recover_lost_locks) {
7020 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7024 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7026 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7030 case -NFS4ERR_GRACE:
7031 case -NFS4ERR_DELAY:
7032 nfs4_handle_exception(server, err, &exception);
7035 } while (exception.retry);
7040 #if defined(CONFIG_NFS_V4_1)
7041 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7043 struct nfs4_lock_state *lsp;
7046 status = nfs4_set_lock_state(state, request);
7049 lsp = request->fl_u.nfs4_fl.owner;
7050 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7051 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7053 return nfs4_lock_expired(state, request);
7057 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7059 struct nfs_inode *nfsi = NFS_I(state->inode);
7060 struct nfs4_state_owner *sp = state->owner;
7061 unsigned char fl_flags = request->fl_flags;
7064 request->fl_flags |= FL_ACCESS;
7065 status = locks_lock_inode_wait(state->inode, request);
7068 mutex_lock(&sp->so_delegreturn_mutex);
7069 down_read(&nfsi->rwsem);
7070 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7071 /* Yes: cache locks! */
7072 /* ...but avoid races with delegation recall... */
7073 request->fl_flags = fl_flags & ~FL_SLEEP;
7074 status = locks_lock_inode_wait(state->inode, request);
7075 up_read(&nfsi->rwsem);
7076 mutex_unlock(&sp->so_delegreturn_mutex);
7079 up_read(&nfsi->rwsem);
7080 mutex_unlock(&sp->so_delegreturn_mutex);
7081 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7083 request->fl_flags = fl_flags;
7087 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7089 struct nfs4_exception exception = {
7091 .inode = state->inode,
7092 .interruptible = true,
7097 err = _nfs4_proc_setlk(state, cmd, request);
7098 if (err == -NFS4ERR_DENIED)
7100 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7102 } while (exception.retry);
7106 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7107 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7110 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7111 struct file_lock *request)
7113 int status = -ERESTARTSYS;
7114 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7116 while(!signalled()) {
7117 status = nfs4_proc_setlk(state, cmd, request);
7118 if ((status != -EAGAIN) || IS_SETLK(cmd))
7120 freezable_schedule_timeout_interruptible(timeout);
7122 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7123 status = -ERESTARTSYS;
7128 #ifdef CONFIG_NFS_V4_1
7129 struct nfs4_lock_waiter {
7130 struct task_struct *task;
7131 struct inode *inode;
7132 struct nfs_lowner *owner;
7136 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7139 struct nfs4_lock_waiter *waiter = wait->private;
7141 /* NULL key means to wake up everyone */
7143 struct cb_notify_lock_args *cbnl = key;
7144 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7145 *wowner = waiter->owner;
7147 /* Only wake if the callback was for the same owner. */
7148 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7151 /* Make sure it's for the right inode */
7152 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7156 /* override "private" so we can use default_wake_function */
7157 wait->private = waiter->task;
7158 ret = woken_wake_function(wait, mode, flags, key);
7160 list_del_init(&wait->entry);
7161 wait->private = waiter;
7166 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7168 int status = -ERESTARTSYS;
7169 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7170 struct nfs_server *server = NFS_SERVER(state->inode);
7171 struct nfs_client *clp = server->nfs_client;
7172 wait_queue_head_t *q = &clp->cl_lock_waitq;
7173 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7174 .id = lsp->ls_seqid.owner_id,
7175 .s_dev = server->s_dev };
7176 struct nfs4_lock_waiter waiter = { .task = current,
7177 .inode = state->inode,
7179 wait_queue_entry_t wait;
7181 /* Don't bother with waitqueue if we don't expect a callback */
7182 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7183 return nfs4_retry_setlk_simple(state, cmd, request);
7186 wait.private = &waiter;
7187 wait.func = nfs4_wake_lock_waiter;
7189 while(!signalled()) {
7190 add_wait_queue(q, &wait);
7191 status = nfs4_proc_setlk(state, cmd, request);
7192 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7193 finish_wait(q, &wait);
7197 status = -ERESTARTSYS;
7198 freezer_do_not_count();
7199 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7201 finish_wait(q, &wait);
7206 #else /* !CONFIG_NFS_V4_1 */
7208 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7210 return nfs4_retry_setlk_simple(state, cmd, request);
7215 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7217 struct nfs_open_context *ctx;
7218 struct nfs4_state *state;
7221 /* verify open state */
7222 ctx = nfs_file_open_context(filp);
7225 if (IS_GETLK(cmd)) {
7227 return nfs4_proc_getlk(state, F_GETLK, request);
7231 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7234 if (request->fl_type == F_UNLCK) {
7236 return nfs4_proc_unlck(state, cmd, request);
7243 if ((request->fl_flags & FL_POSIX) &&
7244 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7248 * Don't rely on the VFS having checked the file open mode,
7249 * since it won't do this for flock() locks.
7251 switch (request->fl_type) {
7253 if (!(filp->f_mode & FMODE_READ))
7257 if (!(filp->f_mode & FMODE_WRITE))
7261 status = nfs4_set_lock_state(state, request);
7265 return nfs4_retry_setlk(state, cmd, request);
7268 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7270 struct nfs_server *server = NFS_SERVER(state->inode);
7273 err = nfs4_set_lock_state(state, fl);
7276 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7277 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7280 struct nfs_release_lockowner_data {
7281 struct nfs4_lock_state *lsp;
7282 struct nfs_server *server;
7283 struct nfs_release_lockowner_args args;
7284 struct nfs_release_lockowner_res res;
7285 unsigned long timestamp;
7288 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7290 struct nfs_release_lockowner_data *data = calldata;
7291 struct nfs_server *server = data->server;
7292 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7293 &data->res.seq_res, task);
7294 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7295 data->timestamp = jiffies;
7298 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7300 struct nfs_release_lockowner_data *data = calldata;
7301 struct nfs_server *server = data->server;
7303 nfs40_sequence_done(task, &data->res.seq_res);
7305 switch (task->tk_status) {
7307 renew_lease(server, data->timestamp);
7309 case -NFS4ERR_STALE_CLIENTID:
7310 case -NFS4ERR_EXPIRED:
7311 nfs4_schedule_lease_recovery(server->nfs_client);
7313 case -NFS4ERR_LEASE_MOVED:
7314 case -NFS4ERR_DELAY:
7315 if (nfs4_async_handle_error(task, server,
7316 NULL, NULL) == -EAGAIN)
7317 rpc_restart_call_prepare(task);
7321 static void nfs4_release_lockowner_release(void *calldata)
7323 struct nfs_release_lockowner_data *data = calldata;
7324 nfs4_free_lock_state(data->server, data->lsp);
7328 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7329 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7330 .rpc_call_done = nfs4_release_lockowner_done,
7331 .rpc_release = nfs4_release_lockowner_release,
7335 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7337 struct nfs_release_lockowner_data *data;
7338 struct rpc_message msg = {
7339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7342 if (server->nfs_client->cl_mvops->minor_version != 0)
7345 data = kmalloc(sizeof(*data), GFP_NOFS);
7349 data->server = server;
7350 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7351 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7352 data->args.lock_owner.s_dev = server->s_dev;
7354 msg.rpc_argp = &data->args;
7355 msg.rpc_resp = &data->res;
7356 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7357 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7360 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7362 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7363 struct dentry *unused, struct inode *inode,
7364 const char *key, const void *buf,
7365 size_t buflen, int flags)
7367 return nfs4_proc_set_acl(inode, buf, buflen);
7370 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7371 struct dentry *unused, struct inode *inode,
7372 const char *key, void *buf, size_t buflen)
7374 return nfs4_proc_get_acl(inode, buf, buflen);
7377 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7379 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7382 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7384 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7385 struct dentry *unused, struct inode *inode,
7386 const char *key, const void *buf,
7387 size_t buflen, int flags)
7389 if (security_ismaclabel(key))
7390 return nfs4_set_security_label(inode, buf, buflen);
7395 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7396 struct dentry *unused, struct inode *inode,
7397 const char *key, void *buf, size_t buflen)
7399 if (security_ismaclabel(key))
7400 return nfs4_get_security_label(inode, buf, buflen);
7405 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7409 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7410 len = security_inode_listsecurity(inode, list, list_len);
7411 if (list_len && len > list_len)
7417 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7418 .prefix = XATTR_SECURITY_PREFIX,
7419 .get = nfs4_xattr_get_nfs4_label,
7420 .set = nfs4_xattr_set_nfs4_label,
7426 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7434 * nfs_fhget will use either the mounted_on_fileid or the fileid
7436 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7438 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7439 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7440 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7441 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7444 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7445 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7446 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7450 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7451 const struct qstr *name,
7452 struct nfs4_fs_locations *fs_locations,
7455 struct nfs_server *server = NFS_SERVER(dir);
7457 struct nfs4_fs_locations_arg args = {
7458 .dir_fh = NFS_FH(dir),
7463 struct nfs4_fs_locations_res res = {
7464 .fs_locations = fs_locations,
7466 struct rpc_message msg = {
7467 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7473 dprintk("%s: start\n", __func__);
7475 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7476 bitmask[1] = nfs4_fattr_bitmap[1];
7478 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7479 * is not supported */
7480 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7481 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7483 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7485 nfs_fattr_init(&fs_locations->fattr);
7486 fs_locations->server = server;
7487 fs_locations->nlocations = 0;
7488 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7489 dprintk("%s: returned status = %d\n", __func__, status);
7493 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7494 const struct qstr *name,
7495 struct nfs4_fs_locations *fs_locations,
7498 struct nfs4_exception exception = {
7499 .interruptible = true,
7503 err = _nfs4_proc_fs_locations(client, dir, name,
7504 fs_locations, page);
7505 trace_nfs4_get_fs_locations(dir, name, err);
7506 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7508 } while (exception.retry);
7513 * This operation also signals the server that this client is
7514 * performing migration recovery. The server can stop returning
7515 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7516 * appended to this compound to identify the client ID which is
7517 * performing recovery.
7519 static int _nfs40_proc_get_locations(struct inode *inode,
7520 struct nfs4_fs_locations *locations,
7521 struct page *page, const struct cred *cred)
7523 struct nfs_server *server = NFS_SERVER(inode);
7524 struct rpc_clnt *clnt = server->client;
7526 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7528 struct nfs4_fs_locations_arg args = {
7529 .clientid = server->nfs_client->cl_clientid,
7530 .fh = NFS_FH(inode),
7533 .migration = 1, /* skip LOOKUP */
7534 .renew = 1, /* append RENEW */
7536 struct nfs4_fs_locations_res res = {
7537 .fs_locations = locations,
7541 struct rpc_message msg = {
7542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7547 unsigned long now = jiffies;
7550 nfs_fattr_init(&locations->fattr);
7551 locations->server = server;
7552 locations->nlocations = 0;
7554 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7555 status = nfs4_call_sync_sequence(clnt, server, &msg,
7556 &args.seq_args, &res.seq_res);
7560 renew_lease(server, now);
7564 #ifdef CONFIG_NFS_V4_1
7567 * This operation also signals the server that this client is
7568 * performing migration recovery. The server can stop asserting
7569 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7570 * performing this operation is identified in the SEQUENCE
7571 * operation in this compound.
7573 * When the client supports GETATTR(fs_locations_info), it can
7574 * be plumbed in here.
7576 static int _nfs41_proc_get_locations(struct inode *inode,
7577 struct nfs4_fs_locations *locations,
7578 struct page *page, const struct cred *cred)
7580 struct nfs_server *server = NFS_SERVER(inode);
7581 struct rpc_clnt *clnt = server->client;
7583 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7585 struct nfs4_fs_locations_arg args = {
7586 .fh = NFS_FH(inode),
7589 .migration = 1, /* skip LOOKUP */
7591 struct nfs4_fs_locations_res res = {
7592 .fs_locations = locations,
7595 struct rpc_message msg = {
7596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7603 nfs_fattr_init(&locations->fattr);
7604 locations->server = server;
7605 locations->nlocations = 0;
7607 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7608 status = nfs4_call_sync_sequence(clnt, server, &msg,
7609 &args.seq_args, &res.seq_res);
7610 if (status == NFS4_OK &&
7611 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7612 status = -NFS4ERR_LEASE_MOVED;
7616 #endif /* CONFIG_NFS_V4_1 */
7619 * nfs4_proc_get_locations - discover locations for a migrated FSID
7620 * @inode: inode on FSID that is migrating
7621 * @locations: result of query
7623 * @cred: credential to use for this operation
7625 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7626 * operation failed, or a negative errno if a local error occurred.
7628 * On success, "locations" is filled in, but if the server has
7629 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7632 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7633 * from this client that require migration recovery.
7635 int nfs4_proc_get_locations(struct inode *inode,
7636 struct nfs4_fs_locations *locations,
7637 struct page *page, const struct cred *cred)
7639 struct nfs_server *server = NFS_SERVER(inode);
7640 struct nfs_client *clp = server->nfs_client;
7641 const struct nfs4_mig_recovery_ops *ops =
7642 clp->cl_mvops->mig_recovery_ops;
7643 struct nfs4_exception exception = {
7644 .interruptible = true,
7648 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7649 (unsigned long long)server->fsid.major,
7650 (unsigned long long)server->fsid.minor,
7652 nfs_display_fhandle(NFS_FH(inode), __func__);
7655 status = ops->get_locations(inode, locations, page, cred);
7656 if (status != -NFS4ERR_DELAY)
7658 nfs4_handle_exception(server, status, &exception);
7659 } while (exception.retry);
7664 * This operation also signals the server that this client is
7665 * performing "lease moved" recovery. The server can stop
7666 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7667 * is appended to this compound to identify the client ID which is
7668 * performing recovery.
7670 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7672 struct nfs_server *server = NFS_SERVER(inode);
7673 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7674 struct rpc_clnt *clnt = server->client;
7675 struct nfs4_fsid_present_arg args = {
7676 .fh = NFS_FH(inode),
7677 .clientid = clp->cl_clientid,
7678 .renew = 1, /* append RENEW */
7680 struct nfs4_fsid_present_res res = {
7683 struct rpc_message msg = {
7684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7689 unsigned long now = jiffies;
7692 res.fh = nfs_alloc_fhandle();
7696 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7697 status = nfs4_call_sync_sequence(clnt, server, &msg,
7698 &args.seq_args, &res.seq_res);
7699 nfs_free_fhandle(res.fh);
7703 do_renew_lease(clp, now);
7707 #ifdef CONFIG_NFS_V4_1
7710 * This operation also signals the server that this client is
7711 * performing "lease moved" recovery. The server can stop asserting
7712 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7713 * this operation is identified in the SEQUENCE operation in this
7716 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7718 struct nfs_server *server = NFS_SERVER(inode);
7719 struct rpc_clnt *clnt = server->client;
7720 struct nfs4_fsid_present_arg args = {
7721 .fh = NFS_FH(inode),
7723 struct nfs4_fsid_present_res res = {
7725 struct rpc_message msg = {
7726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7733 res.fh = nfs_alloc_fhandle();
7737 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7738 status = nfs4_call_sync_sequence(clnt, server, &msg,
7739 &args.seq_args, &res.seq_res);
7740 nfs_free_fhandle(res.fh);
7741 if (status == NFS4_OK &&
7742 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7743 status = -NFS4ERR_LEASE_MOVED;
7747 #endif /* CONFIG_NFS_V4_1 */
7750 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7751 * @inode: inode on FSID to check
7752 * @cred: credential to use for this operation
7754 * Server indicates whether the FSID is present, moved, or not
7755 * recognized. This operation is necessary to clear a LEASE_MOVED
7756 * condition for this client ID.
7758 * Returns NFS4_OK if the FSID is present on this server,
7759 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7760 * NFS4ERR code if some error occurred on the server, or a
7761 * negative errno if a local failure occurred.
7763 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
7765 struct nfs_server *server = NFS_SERVER(inode);
7766 struct nfs_client *clp = server->nfs_client;
7767 const struct nfs4_mig_recovery_ops *ops =
7768 clp->cl_mvops->mig_recovery_ops;
7769 struct nfs4_exception exception = {
7770 .interruptible = true,
7774 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7775 (unsigned long long)server->fsid.major,
7776 (unsigned long long)server->fsid.minor,
7778 nfs_display_fhandle(NFS_FH(inode), __func__);
7781 status = ops->fsid_present(inode, cred);
7782 if (status != -NFS4ERR_DELAY)
7784 nfs4_handle_exception(server, status, &exception);
7785 } while (exception.retry);
7790 * If 'use_integrity' is true and the state managment nfs_client
7791 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7792 * and the machine credential as per RFC3530bis and RFC5661 Security
7793 * Considerations sections. Otherwise, just use the user cred with the
7794 * filesystem's rpc_client.
7796 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7799 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7800 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
7801 struct nfs4_secinfo_arg args = {
7802 .dir_fh = NFS_FH(dir),
7805 struct nfs4_secinfo_res res = {
7808 struct rpc_message msg = {
7809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7813 struct nfs4_call_sync_data data = {
7814 .seq_server = NFS_SERVER(dir),
7815 .seq_args = &args.seq_args,
7816 .seq_res = &res.seq_res,
7818 struct rpc_task_setup task_setup = {
7820 .rpc_message = &msg,
7821 .callback_ops = clp->cl_mvops->call_sync_ops,
7822 .callback_data = &data,
7823 .flags = RPC_TASK_NO_ROUND_ROBIN,
7825 const struct cred *cred = NULL;
7827 if (use_integrity) {
7828 clnt = clp->cl_rpcclient;
7829 task_setup.rpc_client = clnt;
7831 cred = nfs4_get_clid_cred(clp);
7832 msg.rpc_cred = cred;
7835 dprintk("NFS call secinfo %s\n", name->name);
7837 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7838 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
7839 status = nfs4_call_sync_custom(&task_setup);
7841 dprintk("NFS reply secinfo: %d\n", status);
7847 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7848 struct nfs4_secinfo_flavors *flavors)
7850 struct nfs4_exception exception = {
7851 .interruptible = true,
7855 err = -NFS4ERR_WRONGSEC;
7857 /* try to use integrity protection with machine cred */
7858 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7859 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7862 * if unable to use integrity protection, or SECINFO with
7863 * integrity protection returns NFS4ERR_WRONGSEC (which is
7864 * disallowed by spec, but exists in deployed servers) use
7865 * the current filesystem's rpc_client and the user cred.
7867 if (err == -NFS4ERR_WRONGSEC)
7868 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7870 trace_nfs4_secinfo(dir, name, err);
7871 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7873 } while (exception.retry);
7877 #ifdef CONFIG_NFS_V4_1
7879 * Check the exchange flags returned by the server for invalid flags, having
7880 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7883 static int nfs4_check_cl_exchange_flags(u32 flags)
7885 if (flags & ~EXCHGID4_FLAG_MASK_R)
7887 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7888 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7890 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7894 return -NFS4ERR_INVAL;
7898 nfs41_same_server_scope(struct nfs41_server_scope *a,
7899 struct nfs41_server_scope *b)
7901 if (a->server_scope_sz != b->server_scope_sz)
7903 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7907 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7909 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
7910 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
7911 struct nfs_client *clp = args->client;
7913 switch (task->tk_status) {
7914 case -NFS4ERR_BADSESSION:
7915 case -NFS4ERR_DEADSESSION:
7916 nfs4_schedule_session_recovery(clp->cl_session,
7919 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
7920 res->dir != NFS4_CDFS4_BOTH) {
7921 rpc_task_close_connection(task);
7922 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
7923 rpc_restart_call(task);
7927 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7928 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
7932 * nfs4_proc_bind_one_conn_to_session()
7934 * The 4.1 client currently uses the same TCP connection for the
7935 * fore and backchannel.
7938 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7939 struct rpc_xprt *xprt,
7940 struct nfs_client *clp,
7941 const struct cred *cred)
7944 struct nfs41_bind_conn_to_session_args args = {
7946 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7949 struct nfs41_bind_conn_to_session_res res;
7950 struct rpc_message msg = {
7952 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7957 struct rpc_task_setup task_setup_data = {
7960 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7961 .rpc_message = &msg,
7962 .flags = RPC_TASK_TIMEOUT,
7964 struct rpc_task *task;
7966 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7967 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7968 args.dir = NFS4_CDFC4_FORE;
7970 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7971 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7972 args.dir = NFS4_CDFC4_FORE;
7974 task = rpc_run_task(&task_setup_data);
7975 if (!IS_ERR(task)) {
7976 status = task->tk_status;
7979 status = PTR_ERR(task);
7980 trace_nfs4_bind_conn_to_session(clp, status);
7982 if (memcmp(res.sessionid.data,
7983 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7984 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7987 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7988 dprintk("NFS: %s: Unexpected direction from server\n",
7992 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7993 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8002 struct rpc_bind_conn_calldata {
8003 struct nfs_client *clp;
8004 const struct cred *cred;
8008 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8009 struct rpc_xprt *xprt,
8012 struct rpc_bind_conn_calldata *p = calldata;
8014 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8017 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8019 struct rpc_bind_conn_calldata data = {
8023 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8024 nfs4_proc_bind_conn_to_session_callback, &data);
8028 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8029 * and operations we'd like to see to enable certain features in the allow map
8031 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8032 .how = SP4_MACH_CRED,
8033 .enforce.u.words = {
8034 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8035 1 << (OP_EXCHANGE_ID - 32) |
8036 1 << (OP_CREATE_SESSION - 32) |
8037 1 << (OP_DESTROY_SESSION - 32) |
8038 1 << (OP_DESTROY_CLIENTID - 32)
8041 [0] = 1 << (OP_CLOSE) |
8042 1 << (OP_OPEN_DOWNGRADE) |
8044 1 << (OP_DELEGRETURN) |
8046 [1] = 1 << (OP_SECINFO - 32) |
8047 1 << (OP_SECINFO_NO_NAME - 32) |
8048 1 << (OP_LAYOUTRETURN - 32) |
8049 1 << (OP_TEST_STATEID - 32) |
8050 1 << (OP_FREE_STATEID - 32) |
8051 1 << (OP_WRITE - 32)
8056 * Select the state protection mode for client `clp' given the server results
8057 * from exchange_id in `sp'.
8059 * Returns 0 on success, negative errno otherwise.
8061 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8062 struct nfs41_state_protection *sp)
8064 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8065 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8066 1 << (OP_EXCHANGE_ID - 32) |
8067 1 << (OP_CREATE_SESSION - 32) |
8068 1 << (OP_DESTROY_SESSION - 32) |
8069 1 << (OP_DESTROY_CLIENTID - 32)
8071 unsigned long flags = 0;
8075 if (sp->how == SP4_MACH_CRED) {
8076 /* Print state protect result */
8077 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8078 for (i = 0; i <= LAST_NFS4_OP; i++) {
8079 if (test_bit(i, sp->enforce.u.longs))
8080 dfprintk(MOUNT, " enforce op %d\n", i);
8081 if (test_bit(i, sp->allow.u.longs))
8082 dfprintk(MOUNT, " allow op %d\n", i);
8085 /* make sure nothing is on enforce list that isn't supported */
8086 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8087 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8088 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8095 * Minimal mode - state operations are allowed to use machine
8096 * credential. Note this already happens by default, so the
8097 * client doesn't have to do anything more than the negotiation.
8099 * NOTE: we don't care if EXCHANGE_ID is in the list -
8100 * we're already using the machine cred for exchange_id
8101 * and will never use a different cred.
8103 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8104 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8105 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8106 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8107 dfprintk(MOUNT, "sp4_mach_cred:\n");
8108 dfprintk(MOUNT, " minimal mode enabled\n");
8109 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8111 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8116 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8117 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8118 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8119 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8120 dfprintk(MOUNT, " cleanup mode enabled\n");
8121 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8124 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8125 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8126 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8129 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8130 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8131 dfprintk(MOUNT, " secinfo mode enabled\n");
8132 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8135 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8136 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8137 dfprintk(MOUNT, " stateid mode enabled\n");
8138 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8141 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8142 dfprintk(MOUNT, " write mode enabled\n");
8143 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8146 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8147 dfprintk(MOUNT, " commit mode enabled\n");
8148 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8152 clp->cl_sp4_flags = flags;
8156 struct nfs41_exchange_id_data {
8157 struct nfs41_exchange_id_res res;
8158 struct nfs41_exchange_id_args args;
8161 static void nfs4_exchange_id_release(void *data)
8163 struct nfs41_exchange_id_data *cdata =
8164 (struct nfs41_exchange_id_data *)data;
8166 nfs_put_client(cdata->args.client);
8167 kfree(cdata->res.impl_id);
8168 kfree(cdata->res.server_scope);
8169 kfree(cdata->res.server_owner);
8173 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8174 .rpc_release = nfs4_exchange_id_release,
8178 * _nfs4_proc_exchange_id()
8180 * Wrapper for EXCHANGE_ID operation.
8182 static struct rpc_task *
8183 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8184 u32 sp4_how, struct rpc_xprt *xprt)
8186 struct rpc_message msg = {
8187 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8190 struct rpc_task_setup task_setup_data = {
8191 .rpc_client = clp->cl_rpcclient,
8192 .callback_ops = &nfs4_exchange_id_call_ops,
8193 .rpc_message = &msg,
8194 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8196 struct nfs41_exchange_id_data *calldata;
8199 if (!refcount_inc_not_zero(&clp->cl_count))
8200 return ERR_PTR(-EIO);
8203 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8207 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8209 status = nfs4_init_uniform_client_string(clp);
8213 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8216 if (unlikely(calldata->res.server_owner == NULL))
8219 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8221 if (unlikely(calldata->res.server_scope == NULL))
8222 goto out_server_owner;
8224 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8225 if (unlikely(calldata->res.impl_id == NULL))
8226 goto out_server_scope;
8230 calldata->args.state_protect.how = SP4_NONE;
8234 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8244 task_setup_data.rpc_xprt = xprt;
8245 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8246 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8247 sizeof(calldata->args.verifier.data));
8249 calldata->args.client = clp;
8250 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8251 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8252 #ifdef CONFIG_NFS_V4_1_MIGRATION
8253 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8255 msg.rpc_argp = &calldata->args;
8256 msg.rpc_resp = &calldata->res;
8257 task_setup_data.callback_data = calldata;
8259 return rpc_run_task(&task_setup_data);
8262 kfree(calldata->res.impl_id);
8264 kfree(calldata->res.server_scope);
8266 kfree(calldata->res.server_owner);
8270 nfs_put_client(clp);
8271 return ERR_PTR(status);
8275 * _nfs4_proc_exchange_id()
8277 * Wrapper for EXCHANGE_ID operation.
8279 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8282 struct rpc_task *task;
8283 struct nfs41_exchange_id_args *argp;
8284 struct nfs41_exchange_id_res *resp;
8285 unsigned long now = jiffies;
8288 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8290 return PTR_ERR(task);
8292 argp = task->tk_msg.rpc_argp;
8293 resp = task->tk_msg.rpc_resp;
8294 status = task->tk_status;
8298 status = nfs4_check_cl_exchange_flags(resp->flags);
8302 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8306 do_renew_lease(clp, now);
8308 clp->cl_clientid = resp->clientid;
8309 clp->cl_exchange_flags = resp->flags;
8310 clp->cl_seqid = resp->seqid;
8311 /* Client ID is not confirmed */
8312 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8313 clear_bit(NFS4_SESSION_ESTABLISHED,
8314 &clp->cl_session->session_state);
8316 if (clp->cl_serverscope != NULL &&
8317 !nfs41_same_server_scope(clp->cl_serverscope,
8318 resp->server_scope)) {
8319 dprintk("%s: server_scope mismatch detected\n",
8321 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8324 swap(clp->cl_serverowner, resp->server_owner);
8325 swap(clp->cl_serverscope, resp->server_scope);
8326 swap(clp->cl_implid, resp->impl_id);
8328 /* Save the EXCHANGE_ID verifier session trunk tests */
8329 memcpy(clp->cl_confirm.data, argp->verifier.data,
8330 sizeof(clp->cl_confirm.data));
8332 trace_nfs4_exchange_id(clp, status);
8338 * nfs4_proc_exchange_id()
8340 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8342 * Since the clientid has expired, all compounds using sessions
8343 * associated with the stale clientid will be returning
8344 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8345 * be in some phase of session reset.
8347 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8349 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8351 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8354 /* try SP4_MACH_CRED if krb5i/p */
8355 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8356 authflavor == RPC_AUTH_GSS_KRB5P) {
8357 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8363 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8367 * nfs4_test_session_trunk
8369 * This is an add_xprt_test() test function called from
8370 * rpc_clnt_setup_test_and_add_xprt.
8372 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8373 * and is dereferrenced in nfs4_exchange_id_release
8375 * Upon success, add the new transport to the rpc_clnt
8377 * @clnt: struct rpc_clnt to get new transport
8378 * @xprt: the rpc_xprt to test
8379 * @data: call data for _nfs4_proc_exchange_id.
8381 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8384 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8385 struct rpc_task *task;
8390 dprintk("--> %s try %s\n", __func__,
8391 xprt->address_strings[RPC_DISPLAY_ADDR]);
8393 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8395 /* Test connection for session trunking. Async exchange_id call */
8396 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8400 status = task->tk_status;
8402 status = nfs4_detect_session_trunking(adata->clp,
8403 task->tk_msg.rpc_resp, xprt);
8406 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8410 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8412 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8413 const struct cred *cred)
8415 struct rpc_message msg = {
8416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8422 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8423 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8424 trace_nfs4_destroy_clientid(clp, status);
8426 dprintk("NFS: Got error %d from the server %s on "
8427 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8431 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8432 const struct cred *cred)
8437 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8438 ret = _nfs4_proc_destroy_clientid(clp, cred);
8440 case -NFS4ERR_DELAY:
8441 case -NFS4ERR_CLIENTID_BUSY:
8451 int nfs4_destroy_clientid(struct nfs_client *clp)
8453 const struct cred *cred;
8456 if (clp->cl_mvops->minor_version < 1)
8458 if (clp->cl_exchange_flags == 0)
8460 if (clp->cl_preserve_clid)
8462 cred = nfs4_get_clid_cred(clp);
8463 ret = nfs4_proc_destroy_clientid(clp, cred);
8467 case -NFS4ERR_STALE_CLIENTID:
8468 clp->cl_exchange_flags = 0;
8474 #endif /* CONFIG_NFS_V4_1 */
8476 struct nfs4_get_lease_time_data {
8477 struct nfs4_get_lease_time_args *args;
8478 struct nfs4_get_lease_time_res *res;
8479 struct nfs_client *clp;
8482 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8485 struct nfs4_get_lease_time_data *data =
8486 (struct nfs4_get_lease_time_data *)calldata;
8488 dprintk("--> %s\n", __func__);
8489 /* just setup sequence, do not trigger session recovery
8490 since we're invoked within one */
8491 nfs4_setup_sequence(data->clp,
8492 &data->args->la_seq_args,
8493 &data->res->lr_seq_res,
8495 dprintk("<-- %s\n", __func__);
8499 * Called from nfs4_state_manager thread for session setup, so don't recover
8500 * from sequence operation or clientid errors.
8502 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8504 struct nfs4_get_lease_time_data *data =
8505 (struct nfs4_get_lease_time_data *)calldata;
8507 dprintk("--> %s\n", __func__);
8508 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8510 switch (task->tk_status) {
8511 case -NFS4ERR_DELAY:
8512 case -NFS4ERR_GRACE:
8513 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8514 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8515 task->tk_status = 0;
8517 case -NFS4ERR_RETRY_UNCACHED_REP:
8518 rpc_restart_call_prepare(task);
8521 dprintk("<-- %s\n", __func__);
8524 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8525 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8526 .rpc_call_done = nfs4_get_lease_time_done,
8529 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8531 struct nfs4_get_lease_time_args args;
8532 struct nfs4_get_lease_time_res res = {
8533 .lr_fsinfo = fsinfo,
8535 struct nfs4_get_lease_time_data data = {
8540 struct rpc_message msg = {
8541 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8545 struct rpc_task_setup task_setup = {
8546 .rpc_client = clp->cl_rpcclient,
8547 .rpc_message = &msg,
8548 .callback_ops = &nfs4_get_lease_time_ops,
8549 .callback_data = &data,
8550 .flags = RPC_TASK_TIMEOUT,
8553 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8554 return nfs4_call_sync_custom(&task_setup);
8557 #ifdef CONFIG_NFS_V4_1
8560 * Initialize the values to be used by the client in CREATE_SESSION
8561 * If nfs4_init_session set the fore channel request and response sizes,
8564 * Set the back channel max_resp_sz_cached to zero to force the client to
8565 * always set csa_cachethis to FALSE because the current implementation
8566 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8568 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8569 struct rpc_clnt *clnt)
8571 unsigned int max_rqst_sz, max_resp_sz;
8572 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8573 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8575 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8576 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8578 /* Fore channel attributes */
8579 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8580 args->fc_attrs.max_resp_sz = max_resp_sz;
8581 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8582 args->fc_attrs.max_reqs = max_session_slots;
8584 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8585 "max_ops=%u max_reqs=%u\n",
8587 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8588 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8590 /* Back channel attributes */
8591 args->bc_attrs.max_rqst_sz = max_bc_payload;
8592 args->bc_attrs.max_resp_sz = max_bc_payload;
8593 args->bc_attrs.max_resp_sz_cached = 0;
8594 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8595 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8596 if (args->bc_attrs.max_reqs > max_bc_slots)
8597 args->bc_attrs.max_reqs = max_bc_slots;
8599 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8600 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8602 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8603 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8604 args->bc_attrs.max_reqs);
8607 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8608 struct nfs41_create_session_res *res)
8610 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8611 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8613 if (rcvd->max_resp_sz > sent->max_resp_sz)
8616 * Our requested max_ops is the minimum we need; we're not
8617 * prepared to break up compounds into smaller pieces than that.
8618 * So, no point even trying to continue if the server won't
8621 if (rcvd->max_ops < sent->max_ops)
8623 if (rcvd->max_reqs == 0)
8625 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8626 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8630 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8631 struct nfs41_create_session_res *res)
8633 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8634 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8636 if (!(res->flags & SESSION4_BACK_CHAN))
8638 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8640 if (rcvd->max_resp_sz < sent->max_resp_sz)
8642 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8644 if (rcvd->max_ops > sent->max_ops)
8646 if (rcvd->max_reqs > sent->max_reqs)
8652 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8653 struct nfs41_create_session_res *res)
8657 ret = nfs4_verify_fore_channel_attrs(args, res);
8660 return nfs4_verify_back_channel_attrs(args, res);
8663 static void nfs4_update_session(struct nfs4_session *session,
8664 struct nfs41_create_session_res *res)
8666 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8667 /* Mark client id and session as being confirmed */
8668 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8669 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8670 session->flags = res->flags;
8671 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8672 if (res->flags & SESSION4_BACK_CHAN)
8673 memcpy(&session->bc_attrs, &res->bc_attrs,
8674 sizeof(session->bc_attrs));
8677 static int _nfs4_proc_create_session(struct nfs_client *clp,
8678 const struct cred *cred)
8680 struct nfs4_session *session = clp->cl_session;
8681 struct nfs41_create_session_args args = {
8683 .clientid = clp->cl_clientid,
8684 .seqid = clp->cl_seqid,
8685 .cb_program = NFS4_CALLBACK,
8687 struct nfs41_create_session_res res;
8689 struct rpc_message msg = {
8690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8697 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8698 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8700 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8701 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8702 trace_nfs4_create_session(clp, status);
8705 case -NFS4ERR_STALE_CLIENTID:
8706 case -NFS4ERR_DELAY:
8715 /* Verify the session's negotiated channel_attrs values */
8716 status = nfs4_verify_channel_attrs(&args, &res);
8717 /* Increment the clientid slot sequence id */
8720 nfs4_update_session(session, &res);
8727 * Issues a CREATE_SESSION operation to the server.
8728 * It is the responsibility of the caller to verify the session is
8729 * expired before calling this routine.
8731 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8735 struct nfs4_session *session = clp->cl_session;
8737 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8739 status = _nfs4_proc_create_session(clp, cred);
8743 /* Init or reset the session slot tables */
8744 status = nfs4_setup_session_slot_tables(session);
8745 dprintk("slot table setup returned %d\n", status);
8749 ptr = (unsigned *)&session->sess_id.data[0];
8750 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8751 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8753 dprintk("<-- %s\n", __func__);
8758 * Issue the over-the-wire RPC DESTROY_SESSION.
8759 * The caller must serialize access to this routine.
8761 int nfs4_proc_destroy_session(struct nfs4_session *session,
8762 const struct cred *cred)
8764 struct rpc_message msg = {
8765 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8766 .rpc_argp = session,
8771 dprintk("--> nfs4_proc_destroy_session\n");
8773 /* session is still being setup */
8774 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8777 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8778 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8779 trace_nfs4_destroy_session(session->clp, status);
8782 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8783 "Session has been destroyed regardless...\n", status);
8785 dprintk("<-- nfs4_proc_destroy_session\n");
8790 * Renew the cl_session lease.
8792 struct nfs4_sequence_data {
8793 struct nfs_client *clp;
8794 struct nfs4_sequence_args args;
8795 struct nfs4_sequence_res res;
8798 static void nfs41_sequence_release(void *data)
8800 struct nfs4_sequence_data *calldata = data;
8801 struct nfs_client *clp = calldata->clp;
8803 if (refcount_read(&clp->cl_count) > 1)
8804 nfs4_schedule_state_renewal(clp);
8805 nfs_put_client(clp);
8809 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8811 switch(task->tk_status) {
8812 case -NFS4ERR_DELAY:
8813 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8816 nfs4_schedule_lease_recovery(clp);
8821 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8823 struct nfs4_sequence_data *calldata = data;
8824 struct nfs_client *clp = calldata->clp;
8826 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8829 trace_nfs4_sequence(clp, task->tk_status);
8830 if (task->tk_status < 0) {
8831 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8832 if (refcount_read(&clp->cl_count) == 1)
8835 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8836 rpc_restart_call_prepare(task);
8840 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8842 dprintk("<-- %s\n", __func__);
8845 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8847 struct nfs4_sequence_data *calldata = data;
8848 struct nfs_client *clp = calldata->clp;
8849 struct nfs4_sequence_args *args;
8850 struct nfs4_sequence_res *res;
8852 args = task->tk_msg.rpc_argp;
8853 res = task->tk_msg.rpc_resp;
8855 nfs4_setup_sequence(clp, args, res, task);
8858 static const struct rpc_call_ops nfs41_sequence_ops = {
8859 .rpc_call_done = nfs41_sequence_call_done,
8860 .rpc_call_prepare = nfs41_sequence_prepare,
8861 .rpc_release = nfs41_sequence_release,
8864 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8865 const struct cred *cred,
8866 struct nfs4_slot *slot,
8869 struct nfs4_sequence_data *calldata;
8870 struct rpc_message msg = {
8871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8874 struct rpc_task_setup task_setup_data = {
8875 .rpc_client = clp->cl_rpcclient,
8876 .rpc_message = &msg,
8877 .callback_ops = &nfs41_sequence_ops,
8878 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8880 struct rpc_task *ret;
8882 ret = ERR_PTR(-EIO);
8883 if (!refcount_inc_not_zero(&clp->cl_count))
8886 ret = ERR_PTR(-ENOMEM);
8887 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8888 if (calldata == NULL)
8890 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
8891 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8892 msg.rpc_argp = &calldata->args;
8893 msg.rpc_resp = &calldata->res;
8894 calldata->clp = clp;
8895 task_setup_data.callback_data = calldata;
8897 ret = rpc_run_task(&task_setup_data);
8902 nfs_put_client(clp);
8904 nfs41_release_slot(slot);
8908 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
8910 struct rpc_task *task;
8913 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8915 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8917 ret = PTR_ERR(task);
8919 rpc_put_task_async(task);
8920 dprintk("<-- %s status=%d\n", __func__, ret);
8924 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
8926 struct rpc_task *task;
8929 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8931 ret = PTR_ERR(task);
8934 ret = rpc_wait_for_completion_task(task);
8936 ret = task->tk_status;
8939 dprintk("<-- %s status=%d\n", __func__, ret);
8943 struct nfs4_reclaim_complete_data {
8944 struct nfs_client *clp;
8945 struct nfs41_reclaim_complete_args arg;
8946 struct nfs41_reclaim_complete_res res;
8949 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8951 struct nfs4_reclaim_complete_data *calldata = data;
8953 nfs4_setup_sequence(calldata->clp,
8954 &calldata->arg.seq_args,
8955 &calldata->res.seq_res,
8959 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8961 switch(task->tk_status) {
8963 wake_up_all(&clp->cl_lock_waitq);
8965 case -NFS4ERR_COMPLETE_ALREADY:
8966 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8968 case -NFS4ERR_DELAY:
8969 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8971 case -NFS4ERR_RETRY_UNCACHED_REP:
8973 case -NFS4ERR_BADSESSION:
8974 case -NFS4ERR_DEADSESSION:
8975 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8978 nfs4_schedule_lease_recovery(clp);
8983 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8985 struct nfs4_reclaim_complete_data *calldata = data;
8986 struct nfs_client *clp = calldata->clp;
8987 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8989 dprintk("--> %s\n", __func__);
8990 if (!nfs41_sequence_done(task, res))
8993 trace_nfs4_reclaim_complete(clp, task->tk_status);
8994 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8995 rpc_restart_call_prepare(task);
8998 dprintk("<-- %s\n", __func__);
9001 static void nfs4_free_reclaim_complete_data(void *data)
9003 struct nfs4_reclaim_complete_data *calldata = data;
9008 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9009 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9010 .rpc_call_done = nfs4_reclaim_complete_done,
9011 .rpc_release = nfs4_free_reclaim_complete_data,
9015 * Issue a global reclaim complete.
9017 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9018 const struct cred *cred)
9020 struct nfs4_reclaim_complete_data *calldata;
9021 struct rpc_message msg = {
9022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9025 struct rpc_task_setup task_setup_data = {
9026 .rpc_client = clp->cl_rpcclient,
9027 .rpc_message = &msg,
9028 .callback_ops = &nfs4_reclaim_complete_call_ops,
9029 .flags = RPC_TASK_NO_ROUND_ROBIN,
9031 int status = -ENOMEM;
9033 dprintk("--> %s\n", __func__);
9034 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9035 if (calldata == NULL)
9037 calldata->clp = clp;
9038 calldata->arg.one_fs = 0;
9040 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9041 msg.rpc_argp = &calldata->arg;
9042 msg.rpc_resp = &calldata->res;
9043 task_setup_data.callback_data = calldata;
9044 status = nfs4_call_sync_custom(&task_setup_data);
9046 dprintk("<-- %s status=%d\n", __func__, status);
9051 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9053 struct nfs4_layoutget *lgp = calldata;
9054 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9056 dprintk("--> %s\n", __func__);
9057 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9058 &lgp->res.seq_res, task);
9059 dprintk("<-- %s\n", __func__);
9062 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9064 struct nfs4_layoutget *lgp = calldata;
9066 dprintk("--> %s\n", __func__);
9067 nfs41_sequence_process(task, &lgp->res.seq_res);
9068 dprintk("<-- %s\n", __func__);
9072 nfs4_layoutget_handle_exception(struct rpc_task *task,
9073 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9075 struct inode *inode = lgp->args.inode;
9076 struct nfs_server *server = NFS_SERVER(inode);
9077 struct pnfs_layout_hdr *lo;
9078 int nfs4err = task->tk_status;
9079 int err, status = 0;
9082 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9084 nfs4_sequence_free_slot(&lgp->res.seq_res);
9091 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9092 * on the file. set tk_status to -ENODATA to tell upper layer to
9095 case -NFS4ERR_LAYOUTUNAVAILABLE:
9099 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9100 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9102 case -NFS4ERR_BADLAYOUT:
9103 status = -EOVERFLOW;
9106 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9107 * (or clients) writing to the same RAID stripe except when
9108 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9110 * Treat it like we would RECALLCONFLICT -- we retry for a little
9111 * while, and then eventually give up.
9113 case -NFS4ERR_LAYOUTTRYLATER:
9114 if (lgp->args.minlength == 0) {
9115 status = -EOVERFLOW;
9120 case -NFS4ERR_RECALLCONFLICT:
9121 status = -ERECALLCONFLICT;
9123 case -NFS4ERR_DELEG_REVOKED:
9124 case -NFS4ERR_ADMIN_REVOKED:
9125 case -NFS4ERR_EXPIRED:
9126 case -NFS4ERR_BAD_STATEID:
9127 exception->timeout = 0;
9128 spin_lock(&inode->i_lock);
9129 lo = NFS_I(inode)->layout;
9130 /* If the open stateid was bad, then recover it. */
9131 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9132 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9133 spin_unlock(&inode->i_lock);
9134 exception->state = lgp->args.ctx->state;
9135 exception->stateid = &lgp->args.stateid;
9140 * Mark the bad layout state as invalid, then retry
9142 pnfs_mark_layout_stateid_invalid(lo, &head);
9143 spin_unlock(&inode->i_lock);
9144 nfs_commit_inode(inode, 0);
9145 pnfs_free_lseg_list(&head);
9150 err = nfs4_handle_exception(server, nfs4err, exception);
9152 if (exception->retry)
9158 dprintk("<-- %s\n", __func__);
9162 size_t max_response_pages(struct nfs_server *server)
9164 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9165 return nfs_page_array_len(0, max_resp_sz);
9168 static void nfs4_layoutget_release(void *calldata)
9170 struct nfs4_layoutget *lgp = calldata;
9172 dprintk("--> %s\n", __func__);
9173 nfs4_sequence_free_slot(&lgp->res.seq_res);
9174 pnfs_layoutget_free(lgp);
9175 dprintk("<-- %s\n", __func__);
9178 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9179 .rpc_call_prepare = nfs4_layoutget_prepare,
9180 .rpc_call_done = nfs4_layoutget_done,
9181 .rpc_release = nfs4_layoutget_release,
9184 struct pnfs_layout_segment *
9185 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9187 struct inode *inode = lgp->args.inode;
9188 struct nfs_server *server = NFS_SERVER(inode);
9189 struct rpc_task *task;
9190 struct rpc_message msg = {
9191 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9192 .rpc_argp = &lgp->args,
9193 .rpc_resp = &lgp->res,
9194 .rpc_cred = lgp->cred,
9196 struct rpc_task_setup task_setup_data = {
9197 .rpc_client = server->client,
9198 .rpc_message = &msg,
9199 .callback_ops = &nfs4_layoutget_call_ops,
9200 .callback_data = lgp,
9201 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
9203 struct pnfs_layout_segment *lseg = NULL;
9204 struct nfs4_exception exception = {
9206 .timeout = *timeout,
9210 dprintk("--> %s\n", __func__);
9212 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9213 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9215 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9217 task = rpc_run_task(&task_setup_data);
9219 status = rpc_wait_for_completion_task(task);
9223 if (task->tk_status < 0) {
9224 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9225 *timeout = exception.timeout;
9226 } else if (lgp->res.layoutp->len == 0) {
9228 *timeout = nfs4_update_delay(&exception.timeout);
9230 lseg = pnfs_layout_process(lgp);
9232 trace_nfs4_layoutget(lgp->args.ctx,
9239 dprintk("<-- %s status=%d\n", __func__, status);
9241 return ERR_PTR(status);
9246 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9248 struct nfs4_layoutreturn *lrp = calldata;
9250 dprintk("--> %s\n", __func__);
9251 nfs4_setup_sequence(lrp->clp,
9252 &lrp->args.seq_args,
9255 if (!pnfs_layout_is_valid(lrp->args.layout))
9259 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9261 struct nfs4_layoutreturn *lrp = calldata;
9262 struct nfs_server *server;
9264 dprintk("--> %s\n", __func__);
9266 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9270 * Was there an RPC level error? Assume the call succeeded,
9271 * and that we need to release the layout
9273 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9274 lrp->res.lrs_present = 0;
9278 server = NFS_SERVER(lrp->args.inode);
9279 switch (task->tk_status) {
9280 case -NFS4ERR_OLD_STATEID:
9281 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9287 task->tk_status = 0;
9291 case -NFS4ERR_DELAY:
9292 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9296 dprintk("<-- %s\n", __func__);
9299 task->tk_status = 0;
9300 nfs4_sequence_free_slot(&lrp->res.seq_res);
9301 rpc_restart_call_prepare(task);
9304 static void nfs4_layoutreturn_release(void *calldata)
9306 struct nfs4_layoutreturn *lrp = calldata;
9307 struct pnfs_layout_hdr *lo = lrp->args.layout;
9309 dprintk("--> %s\n", __func__);
9310 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9311 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9312 nfs4_sequence_free_slot(&lrp->res.seq_res);
9313 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9314 lrp->ld_private.ops->free(&lrp->ld_private);
9315 pnfs_put_layout_hdr(lrp->args.layout);
9316 nfs_iput_and_deactive(lrp->inode);
9317 put_cred(lrp->cred);
9319 dprintk("<-- %s\n", __func__);
9322 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9323 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9324 .rpc_call_done = nfs4_layoutreturn_done,
9325 .rpc_release = nfs4_layoutreturn_release,
9328 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9330 struct rpc_task *task;
9331 struct rpc_message msg = {
9332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9333 .rpc_argp = &lrp->args,
9334 .rpc_resp = &lrp->res,
9335 .rpc_cred = lrp->cred,
9337 struct rpc_task_setup task_setup_data = {
9338 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9339 .rpc_message = &msg,
9340 .callback_ops = &nfs4_layoutreturn_call_ops,
9341 .callback_data = lrp,
9345 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9346 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9347 &task_setup_data.rpc_client, &msg);
9349 dprintk("--> %s\n", __func__);
9351 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9353 nfs4_layoutreturn_release(lrp);
9356 task_setup_data.flags |= RPC_TASK_ASYNC;
9358 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9359 task = rpc_run_task(&task_setup_data);
9361 return PTR_ERR(task);
9363 status = task->tk_status;
9364 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9365 dprintk("<-- %s status=%d\n", __func__, status);
9371 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9372 struct pnfs_device *pdev,
9373 const struct cred *cred)
9375 struct nfs4_getdeviceinfo_args args = {
9377 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9378 NOTIFY_DEVICEID4_DELETE,
9380 struct nfs4_getdeviceinfo_res res = {
9383 struct rpc_message msg = {
9384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9391 dprintk("--> %s\n", __func__);
9392 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9393 if (res.notification & ~args.notify_types)
9394 dprintk("%s: unsupported notification\n", __func__);
9395 if (res.notification != args.notify_types)
9398 dprintk("<-- %s status=%d\n", __func__, status);
9403 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9404 struct pnfs_device *pdev,
9405 const struct cred *cred)
9407 struct nfs4_exception exception = { };
9411 err = nfs4_handle_exception(server,
9412 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9414 } while (exception.retry);
9417 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9419 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9421 struct nfs4_layoutcommit_data *data = calldata;
9422 struct nfs_server *server = NFS_SERVER(data->args.inode);
9424 nfs4_setup_sequence(server->nfs_client,
9425 &data->args.seq_args,
9431 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9433 struct nfs4_layoutcommit_data *data = calldata;
9434 struct nfs_server *server = NFS_SERVER(data->args.inode);
9436 if (!nfs41_sequence_done(task, &data->res.seq_res))
9439 switch (task->tk_status) { /* Just ignore these failures */
9440 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9441 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9442 case -NFS4ERR_BADLAYOUT: /* no layout */
9443 case -NFS4ERR_GRACE: /* loca_recalim always false */
9444 task->tk_status = 0;
9448 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9449 rpc_restart_call_prepare(task);
9455 static void nfs4_layoutcommit_release(void *calldata)
9457 struct nfs4_layoutcommit_data *data = calldata;
9459 pnfs_cleanup_layoutcommit(data);
9460 nfs_post_op_update_inode_force_wcc(data->args.inode,
9462 put_cred(data->cred);
9463 nfs_iput_and_deactive(data->inode);
9467 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9468 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9469 .rpc_call_done = nfs4_layoutcommit_done,
9470 .rpc_release = nfs4_layoutcommit_release,
9474 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9476 struct rpc_message msg = {
9477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9478 .rpc_argp = &data->args,
9479 .rpc_resp = &data->res,
9480 .rpc_cred = data->cred,
9482 struct rpc_task_setup task_setup_data = {
9483 .task = &data->task,
9484 .rpc_client = NFS_CLIENT(data->args.inode),
9485 .rpc_message = &msg,
9486 .callback_ops = &nfs4_layoutcommit_ops,
9487 .callback_data = data,
9489 struct rpc_task *task;
9492 dprintk("NFS: initiating layoutcommit call. sync %d "
9493 "lbw: %llu inode %lu\n", sync,
9494 data->args.lastbytewritten,
9495 data->args.inode->i_ino);
9498 data->inode = nfs_igrab_and_active(data->args.inode);
9499 if (data->inode == NULL) {
9500 nfs4_layoutcommit_release(data);
9503 task_setup_data.flags = RPC_TASK_ASYNC;
9505 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9506 task = rpc_run_task(&task_setup_data);
9508 return PTR_ERR(task);
9510 status = task->tk_status;
9511 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9512 dprintk("%s: status %d\n", __func__, status);
9518 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9519 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9522 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9523 struct nfs_fsinfo *info,
9524 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9526 struct nfs41_secinfo_no_name_args args = {
9527 .style = SECINFO_STYLE_CURRENT_FH,
9529 struct nfs4_secinfo_res res = {
9532 struct rpc_message msg = {
9533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9537 struct rpc_clnt *clnt = server->client;
9538 struct nfs4_call_sync_data data = {
9539 .seq_server = server,
9540 .seq_args = &args.seq_args,
9541 .seq_res = &res.seq_res,
9543 struct rpc_task_setup task_setup = {
9544 .rpc_client = server->client,
9545 .rpc_message = &msg,
9546 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9547 .callback_data = &data,
9548 .flags = RPC_TASK_NO_ROUND_ROBIN,
9550 const struct cred *cred = NULL;
9553 if (use_integrity) {
9554 clnt = server->nfs_client->cl_rpcclient;
9555 task_setup.rpc_client = clnt;
9557 cred = nfs4_get_clid_cred(server->nfs_client);
9558 msg.rpc_cred = cred;
9561 dprintk("--> %s\n", __func__);
9562 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9563 status = nfs4_call_sync_custom(&task_setup);
9564 dprintk("<-- %s status=%d\n", __func__, status);
9572 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9573 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9575 struct nfs4_exception exception = {
9576 .interruptible = true,
9580 /* first try using integrity protection */
9581 err = -NFS4ERR_WRONGSEC;
9583 /* try to use integrity protection with machine cred */
9584 if (_nfs4_is_integrity_protected(server->nfs_client))
9585 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9589 * if unable to use integrity protection, or SECINFO with
9590 * integrity protection returns NFS4ERR_WRONGSEC (which is
9591 * disallowed by spec, but exists in deployed servers) use
9592 * the current filesystem's rpc_client and the user cred.
9594 if (err == -NFS4ERR_WRONGSEC)
9595 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9600 case -NFS4ERR_WRONGSEC:
9604 err = nfs4_handle_exception(server, err, &exception);
9606 } while (exception.retry);
9612 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9613 struct nfs_fsinfo *info)
9617 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9618 struct nfs4_secinfo_flavors *flavors;
9619 struct nfs4_secinfo4 *secinfo;
9622 page = alloc_page(GFP_KERNEL);
9628 flavors = page_address(page);
9629 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9632 * Fall back on "guess and check" method if
9633 * the server doesn't support SECINFO_NO_NAME
9635 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9636 err = nfs4_find_root_sec(server, fhandle, info);
9642 for (i = 0; i < flavors->num_flavors; i++) {
9643 secinfo = &flavors->flavors[i];
9645 switch (secinfo->flavor) {
9649 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9650 &secinfo->flavor_info);
9653 flavor = RPC_AUTH_MAXFLAVOR;
9657 if (!nfs_auth_info_match(&server->auth_info, flavor))
9658 flavor = RPC_AUTH_MAXFLAVOR;
9660 if (flavor != RPC_AUTH_MAXFLAVOR) {
9661 err = nfs4_lookup_root_sec(server, fhandle,
9668 if (flavor == RPC_AUTH_MAXFLAVOR)
9679 static int _nfs41_test_stateid(struct nfs_server *server,
9680 nfs4_stateid *stateid,
9681 const struct cred *cred)
9684 struct nfs41_test_stateid_args args = {
9687 struct nfs41_test_stateid_res res;
9688 struct rpc_message msg = {
9689 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9694 struct rpc_clnt *rpc_client = server->client;
9696 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9699 dprintk("NFS call test_stateid %p\n", stateid);
9700 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9701 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9702 &args.seq_args, &res.seq_res);
9703 if (status != NFS_OK) {
9704 dprintk("NFS reply test_stateid: failed, %d\n", status);
9707 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9711 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9712 int err, struct nfs4_exception *exception)
9714 exception->retry = 0;
9716 case -NFS4ERR_DELAY:
9717 case -NFS4ERR_RETRY_UNCACHED_REP:
9718 nfs4_handle_exception(server, err, exception);
9720 case -NFS4ERR_BADSESSION:
9721 case -NFS4ERR_BADSLOT:
9722 case -NFS4ERR_BAD_HIGH_SLOT:
9723 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9724 case -NFS4ERR_DEADSESSION:
9725 nfs4_do_handle_exception(server, err, exception);
9730 * nfs41_test_stateid - perform a TEST_STATEID operation
9732 * @server: server / transport on which to perform the operation
9733 * @stateid: state ID to test
9736 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9737 * Otherwise a negative NFS4ERR value is returned if the operation
9738 * failed or the state ID is not currently valid.
9740 static int nfs41_test_stateid(struct nfs_server *server,
9741 nfs4_stateid *stateid,
9742 const struct cred *cred)
9744 struct nfs4_exception exception = {
9745 .interruptible = true,
9749 err = _nfs41_test_stateid(server, stateid, cred);
9750 nfs4_handle_delay_or_session_error(server, err, &exception);
9751 } while (exception.retry);
9755 struct nfs_free_stateid_data {
9756 struct nfs_server *server;
9757 struct nfs41_free_stateid_args args;
9758 struct nfs41_free_stateid_res res;
9761 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9763 struct nfs_free_stateid_data *data = calldata;
9764 nfs4_setup_sequence(data->server->nfs_client,
9765 &data->args.seq_args,
9770 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9772 struct nfs_free_stateid_data *data = calldata;
9774 nfs41_sequence_done(task, &data->res.seq_res);
9776 switch (task->tk_status) {
9777 case -NFS4ERR_DELAY:
9778 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9779 rpc_restart_call_prepare(task);
9783 static void nfs41_free_stateid_release(void *calldata)
9788 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9789 .rpc_call_prepare = nfs41_free_stateid_prepare,
9790 .rpc_call_done = nfs41_free_stateid_done,
9791 .rpc_release = nfs41_free_stateid_release,
9795 * nfs41_free_stateid - perform a FREE_STATEID operation
9797 * @server: server / transport on which to perform the operation
9798 * @stateid: state ID to release
9800 * @privileged: set to true if this call needs to be privileged
9802 * Note: this function is always asynchronous.
9804 static int nfs41_free_stateid(struct nfs_server *server,
9805 const nfs4_stateid *stateid,
9806 const struct cred *cred,
9809 struct rpc_message msg = {
9810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9813 struct rpc_task_setup task_setup = {
9814 .rpc_client = server->client,
9815 .rpc_message = &msg,
9816 .callback_ops = &nfs41_free_stateid_ops,
9817 .flags = RPC_TASK_ASYNC,
9819 struct nfs_free_stateid_data *data;
9820 struct rpc_task *task;
9822 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9823 &task_setup.rpc_client, &msg);
9825 dprintk("NFS call free_stateid %p\n", stateid);
9826 data = kmalloc(sizeof(*data), GFP_NOFS);
9829 data->server = server;
9830 nfs4_stateid_copy(&data->args.stateid, stateid);
9832 task_setup.callback_data = data;
9834 msg.rpc_argp = &data->args;
9835 msg.rpc_resp = &data->res;
9836 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
9837 task = rpc_run_task(&task_setup);
9839 return PTR_ERR(task);
9845 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9847 const struct cred *cred = lsp->ls_state->owner->so_cred;
9849 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9850 nfs4_free_lock_state(server, lsp);
9853 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9854 const nfs4_stateid *s2)
9856 if (s1->type != s2->type)
9859 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9862 if (s1->seqid == s2->seqid)
9865 return s1->seqid == 0 || s2->seqid == 0;
9868 #endif /* CONFIG_NFS_V4_1 */
9870 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9871 const nfs4_stateid *s2)
9873 return nfs4_stateid_match(s1, s2);
9877 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9878 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9879 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9880 .recover_open = nfs4_open_reclaim,
9881 .recover_lock = nfs4_lock_reclaim,
9882 .establish_clid = nfs4_init_clientid,
9883 .detect_trunking = nfs40_discover_server_trunking,
9886 #if defined(CONFIG_NFS_V4_1)
9887 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9888 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9889 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9890 .recover_open = nfs4_open_reclaim,
9891 .recover_lock = nfs4_lock_reclaim,
9892 .establish_clid = nfs41_init_clientid,
9893 .reclaim_complete = nfs41_proc_reclaim_complete,
9894 .detect_trunking = nfs41_discover_server_trunking,
9896 #endif /* CONFIG_NFS_V4_1 */
9898 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9899 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9900 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9901 .recover_open = nfs40_open_expired,
9902 .recover_lock = nfs4_lock_expired,
9903 .establish_clid = nfs4_init_clientid,
9906 #if defined(CONFIG_NFS_V4_1)
9907 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9908 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9909 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9910 .recover_open = nfs41_open_expired,
9911 .recover_lock = nfs41_lock_expired,
9912 .establish_clid = nfs41_init_clientid,
9914 #endif /* CONFIG_NFS_V4_1 */
9916 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9917 .sched_state_renewal = nfs4_proc_async_renew,
9918 .get_state_renewal_cred = nfs4_get_renew_cred,
9919 .renew_lease = nfs4_proc_renew,
9922 #if defined(CONFIG_NFS_V4_1)
9923 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9924 .sched_state_renewal = nfs41_proc_async_sequence,
9925 .get_state_renewal_cred = nfs4_get_machine_cred,
9926 .renew_lease = nfs4_proc_sequence,
9930 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9931 .get_locations = _nfs40_proc_get_locations,
9932 .fsid_present = _nfs40_proc_fsid_present,
9935 #if defined(CONFIG_NFS_V4_1)
9936 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9937 .get_locations = _nfs41_proc_get_locations,
9938 .fsid_present = _nfs41_proc_fsid_present,
9940 #endif /* CONFIG_NFS_V4_1 */
9942 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9944 .init_caps = NFS_CAP_READDIRPLUS
9945 | NFS_CAP_ATOMIC_OPEN
9946 | NFS_CAP_POSIX_LOCK,
9947 .init_client = nfs40_init_client,
9948 .shutdown_client = nfs40_shutdown_client,
9949 .match_stateid = nfs4_match_stateid,
9950 .find_root_sec = nfs4_find_root_sec,
9951 .free_lock_state = nfs4_release_lockowner,
9952 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9953 .alloc_seqid = nfs_alloc_seqid,
9954 .call_sync_ops = &nfs40_call_sync_ops,
9955 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9956 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9957 .state_renewal_ops = &nfs40_state_renewal_ops,
9958 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9961 #if defined(CONFIG_NFS_V4_1)
9962 static struct nfs_seqid *
9963 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9968 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9970 .init_caps = NFS_CAP_READDIRPLUS
9971 | NFS_CAP_ATOMIC_OPEN
9972 | NFS_CAP_POSIX_LOCK
9973 | NFS_CAP_STATEID_NFSV41
9974 | NFS_CAP_ATOMIC_OPEN_V1
9976 .init_client = nfs41_init_client,
9977 .shutdown_client = nfs41_shutdown_client,
9978 .match_stateid = nfs41_match_stateid,
9979 .find_root_sec = nfs41_find_root_sec,
9980 .free_lock_state = nfs41_free_lock_state,
9981 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9982 .alloc_seqid = nfs_alloc_no_seqid,
9983 .session_trunk = nfs4_test_session_trunk,
9984 .call_sync_ops = &nfs41_call_sync_ops,
9985 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9986 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9987 .state_renewal_ops = &nfs41_state_renewal_ops,
9988 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9992 #if defined(CONFIG_NFS_V4_2)
9993 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9995 .init_caps = NFS_CAP_READDIRPLUS
9996 | NFS_CAP_ATOMIC_OPEN
9997 | NFS_CAP_POSIX_LOCK
9998 | NFS_CAP_STATEID_NFSV41
9999 | NFS_CAP_ATOMIC_OPEN_V1
10003 | NFS_CAP_OFFLOAD_CANCEL
10004 | NFS_CAP_COPY_NOTIFY
10005 | NFS_CAP_DEALLOCATE
10007 | NFS_CAP_LAYOUTSTATS
10009 | NFS_CAP_LAYOUTERROR,
10010 .init_client = nfs41_init_client,
10011 .shutdown_client = nfs41_shutdown_client,
10012 .match_stateid = nfs41_match_stateid,
10013 .find_root_sec = nfs41_find_root_sec,
10014 .free_lock_state = nfs41_free_lock_state,
10015 .call_sync_ops = &nfs41_call_sync_ops,
10016 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10017 .alloc_seqid = nfs_alloc_no_seqid,
10018 .session_trunk = nfs4_test_session_trunk,
10019 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10020 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10021 .state_renewal_ops = &nfs41_state_renewal_ops,
10022 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10026 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10027 [0] = &nfs_v4_0_minor_ops,
10028 #if defined(CONFIG_NFS_V4_1)
10029 [1] = &nfs_v4_1_minor_ops,
10031 #if defined(CONFIG_NFS_V4_2)
10032 [2] = &nfs_v4_2_minor_ops,
10036 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10038 ssize_t error, error2;
10040 error = generic_listxattr(dentry, list, size);
10048 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10051 return error + error2;
10054 static const struct inode_operations nfs4_dir_inode_operations = {
10055 .create = nfs_create,
10056 .lookup = nfs_lookup,
10057 .atomic_open = nfs_atomic_open,
10059 .unlink = nfs_unlink,
10060 .symlink = nfs_symlink,
10061 .mkdir = nfs_mkdir,
10062 .rmdir = nfs_rmdir,
10063 .mknod = nfs_mknod,
10064 .rename = nfs_rename,
10065 .permission = nfs_permission,
10066 .getattr = nfs_getattr,
10067 .setattr = nfs_setattr,
10068 .listxattr = nfs4_listxattr,
10071 static const struct inode_operations nfs4_file_inode_operations = {
10072 .permission = nfs_permission,
10073 .getattr = nfs_getattr,
10074 .setattr = nfs_setattr,
10075 .listxattr = nfs4_listxattr,
10078 const struct nfs_rpc_ops nfs_v4_clientops = {
10079 .version = 4, /* protocol version */
10080 .dentry_ops = &nfs4_dentry_operations,
10081 .dir_inode_ops = &nfs4_dir_inode_operations,
10082 .file_inode_ops = &nfs4_file_inode_operations,
10083 .file_ops = &nfs4_file_operations,
10084 .getroot = nfs4_proc_get_root,
10085 .submount = nfs4_submount,
10086 .try_get_tree = nfs4_try_get_tree,
10087 .getattr = nfs4_proc_getattr,
10088 .setattr = nfs4_proc_setattr,
10089 .lookup = nfs4_proc_lookup,
10090 .lookupp = nfs4_proc_lookupp,
10091 .access = nfs4_proc_access,
10092 .readlink = nfs4_proc_readlink,
10093 .create = nfs4_proc_create,
10094 .remove = nfs4_proc_remove,
10095 .unlink_setup = nfs4_proc_unlink_setup,
10096 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10097 .unlink_done = nfs4_proc_unlink_done,
10098 .rename_setup = nfs4_proc_rename_setup,
10099 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10100 .rename_done = nfs4_proc_rename_done,
10101 .link = nfs4_proc_link,
10102 .symlink = nfs4_proc_symlink,
10103 .mkdir = nfs4_proc_mkdir,
10104 .rmdir = nfs4_proc_rmdir,
10105 .readdir = nfs4_proc_readdir,
10106 .mknod = nfs4_proc_mknod,
10107 .statfs = nfs4_proc_statfs,
10108 .fsinfo = nfs4_proc_fsinfo,
10109 .pathconf = nfs4_proc_pathconf,
10110 .set_capabilities = nfs4_server_capabilities,
10111 .decode_dirent = nfs4_decode_dirent,
10112 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10113 .read_setup = nfs4_proc_read_setup,
10114 .read_done = nfs4_read_done,
10115 .write_setup = nfs4_proc_write_setup,
10116 .write_done = nfs4_write_done,
10117 .commit_setup = nfs4_proc_commit_setup,
10118 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10119 .commit_done = nfs4_commit_done,
10120 .lock = nfs4_proc_lock,
10121 .clear_acl_cache = nfs4_zap_acl_attr,
10122 .close_context = nfs4_close_context,
10123 .open_context = nfs4_atomic_open,
10124 .have_delegation = nfs4_have_delegation,
10125 .alloc_client = nfs4_alloc_client,
10126 .init_client = nfs4_init_client,
10127 .free_client = nfs4_free_client,
10128 .create_server = nfs4_create_server,
10129 .clone_server = nfs_clone_server,
10132 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10133 .name = XATTR_NAME_NFSV4_ACL,
10134 .list = nfs4_xattr_list_nfs4_acl,
10135 .get = nfs4_xattr_get_nfs4_acl,
10136 .set = nfs4_xattr_set_nfs4_acl,
10139 const struct xattr_handler *nfs4_xattr_handlers[] = {
10140 &nfs4_xattr_nfs4_acl_handler,
10141 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10142 &nfs4_xattr_nfs4_label_handler,
10149 * c-basic-offset: 8