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 int nfs41_sequence_process(struct rpc_task *task,
778 struct nfs4_sequence_res *res)
780 struct nfs4_session *session;
781 struct nfs4_slot *slot = res->sr_slot;
782 struct nfs_client *clp;
788 /* don't increment the sequence number if the task wasn't sent */
789 if (!RPC_WAS_SENT(task) || slot->seq_done)
792 session = slot->table->session;
794 trace_nfs4_sequence_done(session, res);
796 status = res->sr_status;
797 if (task->tk_status == -NFS4ERR_DEADSESSION)
798 status = -NFS4ERR_DEADSESSION;
800 /* Check the SEQUENCE operation status */
803 /* Mark this sequence number as having been acked */
804 nfs4_slot_sequence_acked(slot, slot->seq_nr);
805 /* Update the slot's sequence and clientid lease timer */
808 do_renew_lease(clp, res->sr_timestamp);
809 /* Check sequence flags */
810 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
812 nfs41_update_target_slotid(slot->table, slot, res);
816 * sr_status remains 1 if an RPC level error occurred.
817 * The server may or may not have processed the sequence
820 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
824 /* The server detected a resend of the RPC call and
825 * returned NFS4ERR_DELAY as per Section 2.10.6.2
828 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
832 nfs4_slot_sequence_acked(slot, slot->seq_nr);
834 case -NFS4ERR_RETRY_UNCACHED_REP:
835 case -NFS4ERR_SEQ_FALSE_RETRY:
837 * The server thinks we tried to replay a request.
838 * Retry the call after bumping the sequence ID.
840 nfs4_slot_sequence_acked(slot, slot->seq_nr);
842 case -NFS4ERR_BADSLOT:
844 * The slot id we used was probably retired. Try again
845 * using a different slot id.
847 if (slot->slot_nr < slot->table->target_highest_slotid)
848 goto session_recover;
850 case -NFS4ERR_SEQ_MISORDERED:
851 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
853 * Were one or more calls using this slot interrupted?
854 * If the server never received the request, then our
855 * transmitted slot sequence number may be too high.
857 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
863 * A retry might be sent while the original request is
864 * still in progress on the replier. The replier SHOULD
865 * deal with the issue by returning NFS4ERR_DELAY as the
866 * reply to SEQUENCE or CB_SEQUENCE operation, but
867 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
869 * Restart the search after a delay.
871 slot->seq_nr = slot->seq_nr_highest_sent;
873 case -NFS4ERR_BADSESSION:
874 case -NFS4ERR_DEADSESSION:
875 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
876 goto session_recover;
878 /* Just update the slot sequence no. */
882 /* The session may be reset by one of the error handlers. */
883 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
887 nfs4_schedule_session_recovery(session, status);
888 dprintk("%s ERROR: %d Reset session\n", __func__, status);
889 nfs41_sequence_free_slot(res);
894 if (rpc_restart_call_prepare(task)) {
895 nfs41_sequence_free_slot(res);
901 if (!rpc_restart_call(task))
903 rpc_delay(task, NFS4_POLL_RETRY_MAX);
907 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
909 if (!nfs41_sequence_process(task, res))
911 if (res->sr_slot != NULL)
912 nfs41_sequence_free_slot(res);
916 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
918 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
920 if (res->sr_slot == NULL)
922 if (res->sr_slot->table->session != NULL)
923 return nfs41_sequence_process(task, res);
924 return nfs40_sequence_done(task, res);
927 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
929 if (res->sr_slot != NULL) {
930 if (res->sr_slot->table->session != NULL)
931 nfs41_sequence_free_slot(res);
933 nfs40_sequence_free_slot(res);
937 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
939 if (res->sr_slot == NULL)
941 if (!res->sr_slot->table->session)
942 return nfs40_sequence_done(task, res);
943 return nfs41_sequence_done(task, res);
945 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
947 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
949 struct nfs4_call_sync_data *data = calldata;
951 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
953 nfs4_setup_sequence(data->seq_server->nfs_client,
954 data->seq_args, data->seq_res, task);
957 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
959 struct nfs4_call_sync_data *data = calldata;
961 nfs41_sequence_done(task, data->seq_res);
964 static const struct rpc_call_ops nfs41_call_sync_ops = {
965 .rpc_call_prepare = nfs41_call_sync_prepare,
966 .rpc_call_done = nfs41_call_sync_done,
969 #else /* !CONFIG_NFS_V4_1 */
971 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
973 return nfs40_sequence_done(task, res);
976 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
978 if (res->sr_slot != NULL)
979 nfs40_sequence_free_slot(res);
982 int nfs4_sequence_done(struct rpc_task *task,
983 struct nfs4_sequence_res *res)
985 return nfs40_sequence_done(task, res);
987 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
989 #endif /* !CONFIG_NFS_V4_1 */
991 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
993 res->sr_timestamp = jiffies;
994 res->sr_status_flags = 0;
999 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1000 struct nfs4_sequence_res *res,
1001 struct nfs4_slot *slot)
1005 slot->privileged = args->sa_privileged ? 1 : 0;
1006 args->sa_slot = slot;
1008 res->sr_slot = slot;
1011 int nfs4_setup_sequence(struct nfs_client *client,
1012 struct nfs4_sequence_args *args,
1013 struct nfs4_sequence_res *res,
1014 struct rpc_task *task)
1016 struct nfs4_session *session = nfs4_get_session(client);
1017 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1018 struct nfs4_slot *slot;
1020 /* slot already allocated? */
1021 if (res->sr_slot != NULL)
1025 tbl = &session->fc_slot_table;
1027 spin_lock(&tbl->slot_tbl_lock);
1028 /* The state manager will wait until the slot table is empty */
1029 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1032 slot = nfs4_alloc_slot(tbl);
1034 if (slot == ERR_PTR(-ENOMEM))
1035 goto out_sleep_timeout;
1038 spin_unlock(&tbl->slot_tbl_lock);
1040 nfs4_sequence_attach_slot(args, res, slot);
1042 trace_nfs4_setup_sequence(session, args);
1044 nfs41_sequence_res_init(res);
1045 rpc_call_start(task);
1048 /* Try again in 1/4 second */
1049 if (args->sa_privileged)
1050 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1051 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1053 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1054 NULL, jiffies + (HZ >> 2));
1055 spin_unlock(&tbl->slot_tbl_lock);
1058 if (args->sa_privileged)
1059 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1060 RPC_PRIORITY_PRIVILEGED);
1062 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1063 spin_unlock(&tbl->slot_tbl_lock);
1066 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1068 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1070 struct nfs4_call_sync_data *data = calldata;
1071 nfs4_setup_sequence(data->seq_server->nfs_client,
1072 data->seq_args, data->seq_res, task);
1075 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1077 struct nfs4_call_sync_data *data = calldata;
1078 nfs4_sequence_done(task, data->seq_res);
1081 static const struct rpc_call_ops nfs40_call_sync_ops = {
1082 .rpc_call_prepare = nfs40_call_sync_prepare,
1083 .rpc_call_done = nfs40_call_sync_done,
1086 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1089 struct rpc_task *task;
1091 task = rpc_run_task(task_setup);
1093 return PTR_ERR(task);
1095 ret = task->tk_status;
1100 static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1101 struct nfs_server *server,
1102 struct rpc_message *msg,
1103 struct nfs4_sequence_args *args,
1104 struct nfs4_sequence_res *res,
1105 unsigned short task_flags)
1107 struct nfs_client *clp = server->nfs_client;
1108 struct nfs4_call_sync_data data = {
1109 .seq_server = server,
1113 struct rpc_task_setup task_setup = {
1116 .callback_ops = clp->cl_mvops->call_sync_ops,
1117 .callback_data = &data,
1118 .flags = task_flags,
1121 return nfs4_call_sync_custom(&task_setup);
1124 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1125 struct nfs_server *server,
1126 struct rpc_message *msg,
1127 struct nfs4_sequence_args *args,
1128 struct nfs4_sequence_res *res)
1130 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1134 int nfs4_call_sync(struct rpc_clnt *clnt,
1135 struct nfs_server *server,
1136 struct rpc_message *msg,
1137 struct nfs4_sequence_args *args,
1138 struct nfs4_sequence_res *res,
1141 nfs4_init_sequence(args, res, cache_reply, 0);
1142 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1146 nfs4_inc_nlink_locked(struct inode *inode)
1148 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1153 nfs4_dec_nlink_locked(struct inode *inode)
1155 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1160 update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
1161 unsigned long timestamp, unsigned long cache_validity)
1163 struct nfs_inode *nfsi = NFS_I(dir);
1165 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1166 | NFS_INO_INVALID_MTIME
1167 | NFS_INO_INVALID_DATA
1169 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
1170 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1171 nfsi->attrtimeo_timestamp = jiffies;
1173 nfs_force_lookup_revalidate(dir);
1174 if (cinfo->before != inode_peek_iversion_raw(dir))
1175 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1176 NFS_INO_INVALID_ACL;
1178 inode_set_iversion_raw(dir, cinfo->after);
1179 nfsi->read_cache_jiffies = timestamp;
1180 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1181 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1182 nfs_fscache_invalidate(dir);
1186 update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1187 unsigned long timestamp, unsigned long cache_validity)
1189 spin_lock(&dir->i_lock);
1190 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1191 spin_unlock(&dir->i_lock);
1194 struct nfs4_open_createattrs {
1195 struct nfs4_label *label;
1196 struct iattr *sattr;
1197 const __u32 verf[2];
1200 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1201 int err, struct nfs4_exception *exception)
1205 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1207 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1208 exception->retry = 1;
1212 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1214 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1217 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1219 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1221 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1225 nfs4_map_atomic_open_share(struct nfs_server *server,
1226 fmode_t fmode, int openflags)
1230 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1232 res = NFS4_SHARE_ACCESS_READ;
1235 res = NFS4_SHARE_ACCESS_WRITE;
1237 case FMODE_READ|FMODE_WRITE:
1238 res = NFS4_SHARE_ACCESS_BOTH;
1240 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1242 /* Want no delegation if we're using O_DIRECT */
1243 if (openflags & O_DIRECT)
1244 res |= NFS4_SHARE_WANT_NO_DELEG;
1249 static enum open_claim_type4
1250 nfs4_map_atomic_open_claim(struct nfs_server *server,
1251 enum open_claim_type4 claim)
1253 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1258 case NFS4_OPEN_CLAIM_FH:
1259 return NFS4_OPEN_CLAIM_NULL;
1260 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1261 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1262 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1263 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1267 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1269 p->o_res.f_attr = &p->f_attr;
1270 p->o_res.f_label = p->f_label;
1271 p->o_res.seqid = p->o_arg.seqid;
1272 p->c_res.seqid = p->c_arg.seqid;
1273 p->o_res.server = p->o_arg.server;
1274 p->o_res.access_request = p->o_arg.access;
1275 nfs_fattr_init(&p->f_attr);
1276 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1279 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1280 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1281 const struct nfs4_open_createattrs *c,
1282 enum open_claim_type4 claim,
1285 struct dentry *parent = dget_parent(dentry);
1286 struct inode *dir = d_inode(parent);
1287 struct nfs_server *server = NFS_SERVER(dir);
1288 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1289 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1290 struct nfs4_opendata *p;
1292 p = kzalloc(sizeof(*p), gfp_mask);
1296 p->f_label = nfs4_label_alloc(server, gfp_mask);
1297 if (IS_ERR(p->f_label))
1300 p->a_label = nfs4_label_alloc(server, gfp_mask);
1301 if (IS_ERR(p->a_label))
1304 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1305 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1306 if (IS_ERR(p->o_arg.seqid))
1307 goto err_free_label;
1308 nfs_sb_active(dentry->d_sb);
1309 p->dentry = dget(dentry);
1312 atomic_inc(&sp->so_count);
1313 p->o_arg.open_flags = flags;
1314 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1315 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1316 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1318 if (flags & O_CREAT) {
1319 p->o_arg.umask = current_umask();
1320 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1321 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1322 p->o_arg.u.attrs = &p->attrs;
1323 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1325 memcpy(p->o_arg.u.verifier.data, c->verf,
1326 sizeof(p->o_arg.u.verifier.data));
1329 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1330 * will return permission denied for all bits until close */
1331 if (!(flags & O_EXCL)) {
1332 /* ask server to check for all possible rights as results
1334 switch (p->o_arg.claim) {
1337 case NFS4_OPEN_CLAIM_NULL:
1338 case NFS4_OPEN_CLAIM_FH:
1339 p->o_arg.access = NFS4_ACCESS_READ |
1340 NFS4_ACCESS_MODIFY |
1341 NFS4_ACCESS_EXTEND |
1342 NFS4_ACCESS_EXECUTE;
1345 p->o_arg.clientid = server->nfs_client->cl_clientid;
1346 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1347 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1348 p->o_arg.name = &dentry->d_name;
1349 p->o_arg.server = server;
1350 p->o_arg.bitmask = nfs4_bitmask(server, label);
1351 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1352 switch (p->o_arg.claim) {
1353 case NFS4_OPEN_CLAIM_NULL:
1354 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1355 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1356 p->o_arg.fh = NFS_FH(dir);
1358 case NFS4_OPEN_CLAIM_PREVIOUS:
1359 case NFS4_OPEN_CLAIM_FH:
1360 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1361 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1362 p->o_arg.fh = NFS_FH(d_inode(dentry));
1364 p->c_arg.fh = &p->o_res.fh;
1365 p->c_arg.stateid = &p->o_res.stateid;
1366 p->c_arg.seqid = p->o_arg.seqid;
1367 nfs4_init_opendata_res(p);
1368 kref_init(&p->kref);
1372 nfs4_label_free(p->a_label);
1374 nfs4_label_free(p->f_label);
1382 static void nfs4_opendata_free(struct kref *kref)
1384 struct nfs4_opendata *p = container_of(kref,
1385 struct nfs4_opendata, kref);
1386 struct super_block *sb = p->dentry->d_sb;
1388 nfs4_lgopen_release(p->lgp);
1389 nfs_free_seqid(p->o_arg.seqid);
1390 nfs4_sequence_free_slot(&p->o_res.seq_res);
1391 if (p->state != NULL)
1392 nfs4_put_open_state(p->state);
1393 nfs4_put_state_owner(p->owner);
1395 nfs4_label_free(p->a_label);
1396 nfs4_label_free(p->f_label);
1400 nfs_sb_deactive(sb);
1401 nfs_fattr_free_names(&p->f_attr);
1402 kfree(p->f_attr.mdsthreshold);
1406 static void nfs4_opendata_put(struct nfs4_opendata *p)
1409 kref_put(&p->kref, nfs4_opendata_free);
1412 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1415 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1416 case FMODE_READ|FMODE_WRITE:
1417 return state->n_rdwr != 0;
1419 return state->n_wronly != 0;
1421 return state->n_rdonly != 0;
1427 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1428 int open_mode, enum open_claim_type4 claim)
1432 if (open_mode & (O_EXCL|O_TRUNC))
1435 case NFS4_OPEN_CLAIM_NULL:
1436 case NFS4_OPEN_CLAIM_FH:
1441 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1443 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1444 && state->n_rdonly != 0;
1447 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1448 && state->n_wronly != 0;
1450 case FMODE_READ|FMODE_WRITE:
1451 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1452 && state->n_rdwr != 0;
1458 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1459 enum open_claim_type4 claim)
1461 if (delegation == NULL)
1463 if ((delegation->type & fmode) != fmode)
1466 case NFS4_OPEN_CLAIM_NULL:
1467 case NFS4_OPEN_CLAIM_FH:
1469 case NFS4_OPEN_CLAIM_PREVIOUS:
1470 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1476 nfs_mark_delegation_referenced(delegation);
1480 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1489 case FMODE_READ|FMODE_WRITE:
1492 nfs4_state_set_mode_locked(state, state->state | fmode);
1495 #ifdef CONFIG_NFS_V4_1
1496 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1498 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1500 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1502 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1506 #endif /* CONFIG_NFS_V4_1 */
1508 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1510 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1511 wake_up_all(&state->waitq);
1514 static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1515 const nfs4_stateid *stateid)
1517 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1518 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1520 if (stateid_seqid == state_seqid + 1U ||
1521 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1522 nfs_state_log_update_open_stateid(state);
1524 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1527 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1529 struct nfs_client *clp = state->owner->so_server->nfs_client;
1530 bool need_recover = false;
1532 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1533 need_recover = true;
1534 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1535 need_recover = true;
1536 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1537 need_recover = true;
1539 nfs4_state_mark_reclaim_nograce(clp, state);
1543 * Check for whether or not the caller may update the open stateid
1544 * to the value passed in by stateid.
1546 * Note: This function relies heavily on the server implementing
1547 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1549 * i.e. The stateid seqids have to be initialised to 1, and
1550 * are then incremented on every state transition.
1552 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1553 const nfs4_stateid *stateid)
1555 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1556 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1557 if (stateid->seqid == cpu_to_be32(1))
1558 nfs_state_log_update_open_stateid(state);
1560 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1564 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1565 nfs_state_log_out_of_order_open_stateid(state, stateid);
1571 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1573 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1575 if (state->n_wronly)
1576 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1577 if (state->n_rdonly)
1578 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1580 set_bit(NFS_O_RDWR_STATE, &state->flags);
1581 set_bit(NFS_OPEN_STATE, &state->flags);
1584 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1585 nfs4_stateid *stateid, fmode_t fmode)
1587 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1588 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1590 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1593 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1596 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1597 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1598 clear_bit(NFS_OPEN_STATE, &state->flags);
1600 if (stateid == NULL)
1602 /* Handle OPEN+OPEN_DOWNGRADE races */
1603 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1604 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1605 nfs_resync_open_stateid_locked(state);
1608 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1609 nfs4_stateid_copy(&state->stateid, stateid);
1610 nfs4_stateid_copy(&state->open_stateid, stateid);
1611 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1613 nfs_state_log_update_open_stateid(state);
1616 static void nfs_clear_open_stateid(struct nfs4_state *state,
1617 nfs4_stateid *arg_stateid,
1618 nfs4_stateid *stateid, fmode_t fmode)
1620 write_seqlock(&state->seqlock);
1621 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1622 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1623 nfs_clear_open_stateid_locked(state, stateid, fmode);
1624 write_sequnlock(&state->seqlock);
1625 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1626 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1629 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1630 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1631 __must_hold(&state->owner->so_lock)
1632 __must_hold(&state->seqlock)
1640 if (!nfs_need_update_open_stateid(state, stateid))
1642 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1646 /* Rely on seqids for serialisation with NFSv4.0 */
1647 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1650 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1652 * Ensure we process the state changes in the same order
1653 * in which the server processed them by delaying the
1654 * update of the stateid until we are in sequence.
1656 write_sequnlock(&state->seqlock);
1657 spin_unlock(&state->owner->so_lock);
1659 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1660 if (!signal_pending(current)) {
1661 if (schedule_timeout(5*HZ) == 0)
1667 finish_wait(&state->waitq, &wait);
1669 spin_lock(&state->owner->so_lock);
1670 write_seqlock(&state->seqlock);
1673 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1674 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1675 nfs4_stateid_copy(freeme, &state->open_stateid);
1676 nfs_test_and_clear_all_open_stateid(state);
1679 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1680 nfs4_stateid_copy(&state->stateid, stateid);
1681 nfs4_stateid_copy(&state->open_stateid, stateid);
1682 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1683 nfs_state_log_update_open_stateid(state);
1686 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1687 const nfs4_stateid *open_stateid,
1689 nfs4_stateid *freeme)
1692 * Protect the call to nfs4_state_set_mode_locked and
1693 * serialise the stateid update
1695 write_seqlock(&state->seqlock);
1696 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1699 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1702 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1704 case FMODE_READ|FMODE_WRITE:
1705 set_bit(NFS_O_RDWR_STATE, &state->flags);
1707 set_bit(NFS_OPEN_STATE, &state->flags);
1708 write_sequnlock(&state->seqlock);
1711 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1713 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1714 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1715 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1716 clear_bit(NFS_OPEN_STATE, &state->flags);
1719 static void nfs_state_set_delegation(struct nfs4_state *state,
1720 const nfs4_stateid *deleg_stateid,
1724 * Protect the call to nfs4_state_set_mode_locked and
1725 * serialise the stateid update
1727 write_seqlock(&state->seqlock);
1728 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1729 set_bit(NFS_DELEGATED_STATE, &state->flags);
1730 write_sequnlock(&state->seqlock);
1733 static void nfs_state_clear_delegation(struct nfs4_state *state)
1735 write_seqlock(&state->seqlock);
1736 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1737 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1738 write_sequnlock(&state->seqlock);
1741 int update_open_stateid(struct nfs4_state *state,
1742 const nfs4_stateid *open_stateid,
1743 const nfs4_stateid *delegation,
1746 struct nfs_server *server = NFS_SERVER(state->inode);
1747 struct nfs_client *clp = server->nfs_client;
1748 struct nfs_inode *nfsi = NFS_I(state->inode);
1749 struct nfs_delegation *deleg_cur;
1750 nfs4_stateid freeme = { };
1753 fmode &= (FMODE_READ|FMODE_WRITE);
1756 spin_lock(&state->owner->so_lock);
1757 if (open_stateid != NULL) {
1758 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1762 deleg_cur = nfs4_get_valid_delegation(state->inode);
1763 if (deleg_cur == NULL)
1766 spin_lock(&deleg_cur->lock);
1767 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1768 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1769 (deleg_cur->type & fmode) != fmode)
1770 goto no_delegation_unlock;
1772 if (delegation == NULL)
1773 delegation = &deleg_cur->stateid;
1774 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1775 goto no_delegation_unlock;
1777 nfs_mark_delegation_referenced(deleg_cur);
1778 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1780 no_delegation_unlock:
1781 spin_unlock(&deleg_cur->lock);
1784 update_open_stateflags(state, fmode);
1785 spin_unlock(&state->owner->so_lock);
1788 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1789 nfs4_schedule_state_manager(clp);
1790 if (freeme.type != 0)
1791 nfs4_test_and_free_stateid(server, &freeme,
1792 state->owner->so_cred);
1797 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1798 const nfs4_stateid *stateid)
1800 struct nfs4_state *state = lsp->ls_state;
1803 spin_lock(&state->state_lock);
1804 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1806 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1808 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1811 spin_unlock(&state->state_lock);
1815 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1817 struct nfs_delegation *delegation;
1819 fmode &= FMODE_READ|FMODE_WRITE;
1821 delegation = nfs4_get_valid_delegation(inode);
1822 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1827 nfs4_inode_return_delegation(inode);
1830 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1832 struct nfs4_state *state = opendata->state;
1833 struct nfs_delegation *delegation;
1834 int open_mode = opendata->o_arg.open_flags;
1835 fmode_t fmode = opendata->o_arg.fmode;
1836 enum open_claim_type4 claim = opendata->o_arg.claim;
1837 nfs4_stateid stateid;
1841 spin_lock(&state->owner->so_lock);
1842 if (can_open_cached(state, fmode, open_mode, claim)) {
1843 update_open_stateflags(state, fmode);
1844 spin_unlock(&state->owner->so_lock);
1845 goto out_return_state;
1847 spin_unlock(&state->owner->so_lock);
1849 delegation = nfs4_get_valid_delegation(state->inode);
1850 if (!can_open_delegated(delegation, fmode, claim)) {
1854 /* Save the delegation */
1855 nfs4_stateid_copy(&stateid, &delegation->stateid);
1857 nfs_release_seqid(opendata->o_arg.seqid);
1858 if (!opendata->is_recover) {
1859 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1865 /* Try to update the stateid using the delegation */
1866 if (update_open_stateid(state, NULL, &stateid, fmode))
1867 goto out_return_state;
1870 return ERR_PTR(ret);
1872 refcount_inc(&state->count);
1877 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1879 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1880 struct nfs_delegation *delegation;
1881 int delegation_flags = 0;
1884 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1886 delegation_flags = delegation->flags;
1888 switch (data->o_arg.claim) {
1891 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1892 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1893 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1894 "returning a delegation for "
1895 "OPEN(CLAIM_DELEGATE_CUR)\n",
1899 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1900 nfs_inode_set_delegation(state->inode,
1901 data->owner->so_cred,
1902 data->o_res.delegation_type,
1903 &data->o_res.delegation,
1904 data->o_res.pagemod_limit);
1906 nfs_inode_reclaim_delegation(state->inode,
1907 data->owner->so_cred,
1908 data->o_res.delegation_type,
1909 &data->o_res.delegation,
1910 data->o_res.pagemod_limit);
1912 if (data->o_res.do_recall)
1913 nfs_async_inode_return_delegation(state->inode,
1914 &data->o_res.delegation);
1918 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1919 * and update the nfs4_state.
1921 static struct nfs4_state *
1922 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1924 struct inode *inode = data->state->inode;
1925 struct nfs4_state *state = data->state;
1928 if (!data->rpc_done) {
1929 if (data->rpc_status)
1930 return ERR_PTR(data->rpc_status);
1931 /* cached opens have already been processed */
1935 ret = nfs_refresh_inode(inode, &data->f_attr);
1937 return ERR_PTR(ret);
1939 if (data->o_res.delegation_type != 0)
1940 nfs4_opendata_check_deleg(data, state);
1942 if (!update_open_stateid(state, &data->o_res.stateid,
1943 NULL, data->o_arg.fmode))
1944 return ERR_PTR(-EAGAIN);
1945 refcount_inc(&state->count);
1950 static struct inode *
1951 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1953 struct inode *inode;
1955 switch (data->o_arg.claim) {
1956 case NFS4_OPEN_CLAIM_NULL:
1957 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1958 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1959 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1960 return ERR_PTR(-EAGAIN);
1961 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1962 &data->f_attr, data->f_label);
1965 inode = d_inode(data->dentry);
1967 nfs_refresh_inode(inode, &data->f_attr);
1972 static struct nfs4_state *
1973 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1975 struct nfs4_state *state;
1976 struct inode *inode;
1978 inode = nfs4_opendata_get_inode(data);
1980 return ERR_CAST(inode);
1981 if (data->state != NULL && data->state->inode == inode) {
1982 state = data->state;
1983 refcount_inc(&state->count);
1985 state = nfs4_get_open_state(inode, data->owner);
1988 state = ERR_PTR(-ENOMEM);
1992 static struct nfs4_state *
1993 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1995 struct nfs4_state *state;
1997 if (!data->rpc_done) {
1998 state = nfs4_try_open_cached(data);
1999 trace_nfs4_cached_open(data->state);
2003 state = nfs4_opendata_find_nfs4_state(data);
2007 if (data->o_res.delegation_type != 0)
2008 nfs4_opendata_check_deleg(data, state);
2009 if (!update_open_stateid(state, &data->o_res.stateid,
2010 NULL, data->o_arg.fmode)) {
2011 nfs4_put_open_state(state);
2012 state = ERR_PTR(-EAGAIN);
2015 nfs_release_seqid(data->o_arg.seqid);
2019 static struct nfs4_state *
2020 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2022 struct nfs4_state *ret;
2024 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2025 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2027 ret = _nfs4_opendata_to_nfs4_state(data);
2028 nfs4_sequence_free_slot(&data->o_res.seq_res);
2032 static struct nfs_open_context *
2033 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2035 struct nfs_inode *nfsi = NFS_I(state->inode);
2036 struct nfs_open_context *ctx;
2039 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2040 if (ctx->state != state)
2042 if ((ctx->mode & mode) != mode)
2044 if (!get_nfs_open_context(ctx))
2050 return ERR_PTR(-ENOENT);
2053 static struct nfs_open_context *
2054 nfs4_state_find_open_context(struct nfs4_state *state)
2056 struct nfs_open_context *ctx;
2058 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2061 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2064 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2067 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2068 struct nfs4_state *state, enum open_claim_type4 claim)
2070 struct nfs4_opendata *opendata;
2072 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2073 NULL, claim, GFP_NOFS);
2074 if (opendata == NULL)
2075 return ERR_PTR(-ENOMEM);
2076 opendata->state = state;
2077 refcount_inc(&state->count);
2081 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2084 struct nfs4_state *newstate;
2087 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2089 opendata->o_arg.open_flags = 0;
2090 opendata->o_arg.fmode = fmode;
2091 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2092 NFS_SB(opendata->dentry->d_sb),
2094 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2095 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2096 nfs4_init_opendata_res(opendata);
2097 ret = _nfs4_recover_proc_open(opendata);
2100 newstate = nfs4_opendata_to_nfs4_state(opendata);
2101 if (IS_ERR(newstate))
2102 return PTR_ERR(newstate);
2103 if (newstate != opendata->state)
2105 nfs4_close_state(newstate, fmode);
2109 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2113 /* memory barrier prior to reading state->n_* */
2115 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2118 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2121 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2125 * We may have performed cached opens for all three recoveries.
2126 * Check if we need to update the current stateid.
2128 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2129 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2130 write_seqlock(&state->seqlock);
2131 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2132 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2133 write_sequnlock(&state->seqlock);
2140 * reclaim state on the server after a reboot.
2142 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2144 struct nfs_delegation *delegation;
2145 struct nfs4_opendata *opendata;
2146 fmode_t delegation_type = 0;
2149 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2150 NFS4_OPEN_CLAIM_PREVIOUS);
2151 if (IS_ERR(opendata))
2152 return PTR_ERR(opendata);
2154 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2155 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2156 delegation_type = delegation->type;
2158 opendata->o_arg.u.delegation_type = delegation_type;
2159 status = nfs4_open_recover(opendata, state);
2160 nfs4_opendata_put(opendata);
2164 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2166 struct nfs_server *server = NFS_SERVER(state->inode);
2167 struct nfs4_exception exception = { };
2170 err = _nfs4_do_open_reclaim(ctx, state);
2171 trace_nfs4_open_reclaim(ctx, 0, err);
2172 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2174 if (err != -NFS4ERR_DELAY)
2176 nfs4_handle_exception(server, err, &exception);
2177 } while (exception.retry);
2181 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2183 struct nfs_open_context *ctx;
2186 ctx = nfs4_state_find_open_context(state);
2189 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2190 nfs_state_clear_open_state_flags(state);
2191 ret = nfs4_do_open_reclaim(ctx, state);
2192 put_nfs_open_context(ctx);
2196 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)
2200 printk(KERN_ERR "NFS: %s: unhandled error "
2201 "%d.\n", __func__, err);
2208 case -NFS4ERR_BADSESSION:
2209 case -NFS4ERR_BADSLOT:
2210 case -NFS4ERR_BAD_HIGH_SLOT:
2211 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2212 case -NFS4ERR_DEADSESSION:
2214 case -NFS4ERR_STALE_CLIENTID:
2215 case -NFS4ERR_STALE_STATEID:
2216 /* Don't recall a delegation if it was lost */
2217 nfs4_schedule_lease_recovery(server->nfs_client);
2219 case -NFS4ERR_MOVED:
2220 nfs4_schedule_migration_recovery(server);
2222 case -NFS4ERR_LEASE_MOVED:
2223 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2225 case -NFS4ERR_DELEG_REVOKED:
2226 case -NFS4ERR_ADMIN_REVOKED:
2227 case -NFS4ERR_EXPIRED:
2228 case -NFS4ERR_BAD_STATEID:
2229 case -NFS4ERR_OPENMODE:
2230 nfs_inode_find_state_and_recover(state->inode,
2232 nfs4_schedule_stateid_recovery(server, state);
2234 case -NFS4ERR_DELAY:
2235 case -NFS4ERR_GRACE:
2239 case -NFS4ERR_DENIED:
2241 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2243 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2250 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2251 struct nfs4_state *state, const nfs4_stateid *stateid)
2253 struct nfs_server *server = NFS_SERVER(state->inode);
2254 struct nfs4_opendata *opendata;
2257 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2258 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2259 if (IS_ERR(opendata))
2260 return PTR_ERR(opendata);
2261 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2262 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2263 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2267 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2268 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2272 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2273 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2277 nfs_state_clear_delegation(state);
2279 nfs4_opendata_put(opendata);
2280 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2283 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2285 struct nfs4_opendata *data = calldata;
2287 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2288 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2291 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2293 struct nfs4_opendata *data = calldata;
2295 nfs40_sequence_done(task, &data->c_res.seq_res);
2297 data->rpc_status = task->tk_status;
2298 if (data->rpc_status == 0) {
2299 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2300 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2301 renew_lease(data->o_res.server, data->timestamp);
2302 data->rpc_done = true;
2306 static void nfs4_open_confirm_release(void *calldata)
2308 struct nfs4_opendata *data = calldata;
2309 struct nfs4_state *state = NULL;
2311 /* If this request hasn't been cancelled, do nothing */
2312 if (!data->cancelled)
2314 /* In case of error, no cleanup! */
2315 if (!data->rpc_done)
2317 state = nfs4_opendata_to_nfs4_state(data);
2319 nfs4_close_state(state, data->o_arg.fmode);
2321 nfs4_opendata_put(data);
2324 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2325 .rpc_call_prepare = nfs4_open_confirm_prepare,
2326 .rpc_call_done = nfs4_open_confirm_done,
2327 .rpc_release = nfs4_open_confirm_release,
2331 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2333 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2335 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2336 struct rpc_task *task;
2337 struct rpc_message msg = {
2338 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2339 .rpc_argp = &data->c_arg,
2340 .rpc_resp = &data->c_res,
2341 .rpc_cred = data->owner->so_cred,
2343 struct rpc_task_setup task_setup_data = {
2344 .rpc_client = server->client,
2345 .rpc_message = &msg,
2346 .callback_ops = &nfs4_open_confirm_ops,
2347 .callback_data = data,
2348 .workqueue = nfsiod_workqueue,
2349 .flags = RPC_TASK_ASYNC,
2353 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2355 kref_get(&data->kref);
2356 data->rpc_done = false;
2357 data->rpc_status = 0;
2358 data->timestamp = jiffies;
2359 task = rpc_run_task(&task_setup_data);
2361 return PTR_ERR(task);
2362 status = rpc_wait_for_completion_task(task);
2364 data->cancelled = true;
2367 status = data->rpc_status;
2372 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2374 struct nfs4_opendata *data = calldata;
2375 struct nfs4_state_owner *sp = data->owner;
2376 struct nfs_client *clp = sp->so_server->nfs_client;
2377 enum open_claim_type4 claim = data->o_arg.claim;
2379 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2382 * Check if we still need to send an OPEN call, or if we can use
2383 * a delegation instead.
2385 if (data->state != NULL) {
2386 struct nfs_delegation *delegation;
2388 if (can_open_cached(data->state, data->o_arg.fmode,
2389 data->o_arg.open_flags, claim))
2392 delegation = nfs4_get_valid_delegation(data->state->inode);
2393 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2394 goto unlock_no_action;
2397 /* Update client id. */
2398 data->o_arg.clientid = clp->cl_clientid;
2402 case NFS4_OPEN_CLAIM_PREVIOUS:
2403 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2404 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2405 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2407 case NFS4_OPEN_CLAIM_FH:
2408 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2410 data->timestamp = jiffies;
2411 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2412 &data->o_arg.seq_args,
2413 &data->o_res.seq_res,
2415 nfs_release_seqid(data->o_arg.seqid);
2417 /* Set the create mode (note dependency on the session type) */
2418 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2419 if (data->o_arg.open_flags & O_EXCL) {
2420 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2421 if (nfs4_has_persistent_session(clp))
2422 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2423 else if (clp->cl_mvops->minor_version > 0)
2424 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2428 trace_nfs4_cached_open(data->state);
2431 task->tk_action = NULL;
2433 nfs4_sequence_done(task, &data->o_res.seq_res);
2436 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2438 struct nfs4_opendata *data = calldata;
2440 data->rpc_status = task->tk_status;
2442 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2445 if (task->tk_status == 0) {
2446 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2447 switch (data->o_res.f_attr->mode & S_IFMT) {
2451 data->rpc_status = -ELOOP;
2454 data->rpc_status = -EISDIR;
2457 data->rpc_status = -ENOTDIR;
2460 renew_lease(data->o_res.server, data->timestamp);
2461 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2462 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2464 data->rpc_done = true;
2467 static void nfs4_open_release(void *calldata)
2469 struct nfs4_opendata *data = calldata;
2470 struct nfs4_state *state = NULL;
2472 /* If this request hasn't been cancelled, do nothing */
2473 if (!data->cancelled)
2475 /* In case of error, no cleanup! */
2476 if (data->rpc_status != 0 || !data->rpc_done)
2478 /* In case we need an open_confirm, no cleanup! */
2479 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2481 state = nfs4_opendata_to_nfs4_state(data);
2483 nfs4_close_state(state, data->o_arg.fmode);
2485 nfs4_opendata_put(data);
2488 static const struct rpc_call_ops nfs4_open_ops = {
2489 .rpc_call_prepare = nfs4_open_prepare,
2490 .rpc_call_done = nfs4_open_done,
2491 .rpc_release = nfs4_open_release,
2494 static int nfs4_run_open_task(struct nfs4_opendata *data,
2495 struct nfs_open_context *ctx)
2497 struct inode *dir = d_inode(data->dir);
2498 struct nfs_server *server = NFS_SERVER(dir);
2499 struct nfs_openargs *o_arg = &data->o_arg;
2500 struct nfs_openres *o_res = &data->o_res;
2501 struct rpc_task *task;
2502 struct rpc_message msg = {
2503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2506 .rpc_cred = data->owner->so_cred,
2508 struct rpc_task_setup task_setup_data = {
2509 .rpc_client = server->client,
2510 .rpc_message = &msg,
2511 .callback_ops = &nfs4_open_ops,
2512 .callback_data = data,
2513 .workqueue = nfsiod_workqueue,
2514 .flags = RPC_TASK_ASYNC,
2518 kref_get(&data->kref);
2519 data->rpc_done = false;
2520 data->rpc_status = 0;
2521 data->cancelled = false;
2522 data->is_recover = false;
2524 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2525 data->is_recover = true;
2526 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2528 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2529 pnfs_lgopen_prepare(data, ctx);
2531 task = rpc_run_task(&task_setup_data);
2533 return PTR_ERR(task);
2534 status = rpc_wait_for_completion_task(task);
2536 data->cancelled = true;
2539 status = data->rpc_status;
2545 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2547 struct inode *dir = d_inode(data->dir);
2548 struct nfs_openres *o_res = &data->o_res;
2551 status = nfs4_run_open_task(data, NULL);
2552 if (status != 0 || !data->rpc_done)
2555 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2557 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2558 status = _nfs4_proc_open_confirm(data);
2564 * Additional permission checks in order to distinguish between an
2565 * open for read, and an open for execute. This works around the
2566 * fact that NFSv4 OPEN treats read and execute permissions as being
2568 * Note that in the non-execute case, we want to turn off permission
2569 * checking if we just created a new file (POSIX open() semantics).
2571 static int nfs4_opendata_access(const struct cred *cred,
2572 struct nfs4_opendata *opendata,
2573 struct nfs4_state *state, fmode_t fmode,
2576 struct nfs_access_entry cache;
2579 /* access call failed or for some reason the server doesn't
2580 * support any access modes -- defer access call until later */
2581 if (opendata->o_res.access_supported == 0)
2586 * Use openflags to check for exec, because fmode won't
2587 * always have FMODE_EXEC set when file open for exec.
2589 if (openflags & __FMODE_EXEC) {
2590 /* ONLY check for exec rights */
2591 if (S_ISDIR(state->inode->i_mode))
2592 mask = NFS4_ACCESS_LOOKUP;
2594 mask = NFS4_ACCESS_EXECUTE;
2595 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2596 mask = NFS4_ACCESS_READ;
2599 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2600 nfs_access_add_cache(state->inode, &cache);
2602 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2603 if ((mask & ~cache.mask & flags) == 0)
2610 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2612 static int _nfs4_proc_open(struct nfs4_opendata *data,
2613 struct nfs_open_context *ctx)
2615 struct inode *dir = d_inode(data->dir);
2616 struct nfs_server *server = NFS_SERVER(dir);
2617 struct nfs_openargs *o_arg = &data->o_arg;
2618 struct nfs_openres *o_res = &data->o_res;
2621 status = nfs4_run_open_task(data, ctx);
2622 if (!data->rpc_done)
2625 if (status == -NFS4ERR_BADNAME &&
2626 !(o_arg->open_flags & O_CREAT))
2631 nfs_fattr_map_and_free_names(server, &data->f_attr);
2633 if (o_arg->open_flags & O_CREAT) {
2634 if (o_arg->open_flags & O_EXCL)
2635 data->file_created = true;
2636 else if (o_res->cinfo.before != o_res->cinfo.after)
2637 data->file_created = true;
2638 if (data->file_created ||
2639 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2640 update_changeattr(dir, &o_res->cinfo,
2641 o_res->f_attr->time_start, 0);
2643 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2644 server->caps &= ~NFS_CAP_POSIX_LOCK;
2645 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2646 status = _nfs4_proc_open_confirm(data);
2650 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2651 nfs4_sequence_free_slot(&o_res->seq_res);
2652 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2653 o_res->f_label, NULL);
2660 * reclaim state on the server after a network partition.
2661 * Assumes caller holds the appropriate lock
2663 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2665 struct nfs4_opendata *opendata;
2668 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2669 NFS4_OPEN_CLAIM_FH);
2670 if (IS_ERR(opendata))
2671 return PTR_ERR(opendata);
2672 ret = nfs4_open_recover(opendata, state);
2674 d_drop(ctx->dentry);
2675 nfs4_opendata_put(opendata);
2679 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2681 struct nfs_server *server = NFS_SERVER(state->inode);
2682 struct nfs4_exception exception = { };
2686 err = _nfs4_open_expired(ctx, state);
2687 trace_nfs4_open_expired(ctx, 0, err);
2688 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2693 case -NFS4ERR_GRACE:
2694 case -NFS4ERR_DELAY:
2695 nfs4_handle_exception(server, err, &exception);
2698 } while (exception.retry);
2703 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2705 struct nfs_open_context *ctx;
2708 ctx = nfs4_state_find_open_context(state);
2711 ret = nfs4_do_open_expired(ctx, state);
2712 put_nfs_open_context(ctx);
2716 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2717 const nfs4_stateid *stateid)
2719 nfs_remove_bad_delegation(state->inode, stateid);
2720 nfs_state_clear_delegation(state);
2723 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2725 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2726 nfs_finish_clear_delegation_stateid(state, NULL);
2729 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2731 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2732 nfs40_clear_delegation_stateid(state);
2733 nfs_state_clear_open_state_flags(state);
2734 return nfs4_open_expired(sp, state);
2737 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2738 nfs4_stateid *stateid,
2739 const struct cred *cred)
2741 return -NFS4ERR_BAD_STATEID;
2744 #if defined(CONFIG_NFS_V4_1)
2745 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2746 nfs4_stateid *stateid,
2747 const struct cred *cred)
2751 switch (stateid->type) {
2754 case NFS4_INVALID_STATEID_TYPE:
2755 case NFS4_SPECIAL_STATEID_TYPE:
2756 return -NFS4ERR_BAD_STATEID;
2757 case NFS4_REVOKED_STATEID_TYPE:
2761 status = nfs41_test_stateid(server, stateid, cred);
2763 case -NFS4ERR_EXPIRED:
2764 case -NFS4ERR_ADMIN_REVOKED:
2765 case -NFS4ERR_DELEG_REVOKED:
2771 /* Ack the revoked state to the server */
2772 nfs41_free_stateid(server, stateid, cred, true);
2773 return -NFS4ERR_EXPIRED;
2776 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2778 struct nfs_server *server = NFS_SERVER(state->inode);
2779 nfs4_stateid stateid;
2780 struct nfs_delegation *delegation;
2781 const struct cred *cred = NULL;
2782 int status, ret = NFS_OK;
2784 /* Get the delegation credential for use by test/free_stateid */
2786 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2787 if (delegation == NULL) {
2789 nfs_state_clear_delegation(state);
2793 nfs4_stateid_copy(&stateid, &delegation->stateid);
2795 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2796 &delegation->flags)) {
2801 if (delegation->cred)
2802 cred = get_cred(delegation->cred);
2804 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2805 trace_nfs4_test_delegation_stateid(state, NULL, status);
2806 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2807 nfs_finish_clear_delegation_stateid(state, &stateid);
2815 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2819 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2820 nfs4_copy_delegation_stateid(state->inode, state->state,
2822 nfs4_stateid_match_other(&state->stateid, &tmp))
2823 nfs_state_set_delegation(state, &tmp, state->state);
2825 nfs_state_clear_delegation(state);
2829 * nfs41_check_expired_locks - possibly free a lock stateid
2831 * @state: NFSv4 state for an inode
2833 * Returns NFS_OK if recovery for this stateid is now finished.
2834 * Otherwise a negative NFS4ERR value is returned.
2836 static int nfs41_check_expired_locks(struct nfs4_state *state)
2838 int status, ret = NFS_OK;
2839 struct nfs4_lock_state *lsp, *prev = NULL;
2840 struct nfs_server *server = NFS_SERVER(state->inode);
2842 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2845 spin_lock(&state->state_lock);
2846 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2847 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2848 const struct cred *cred = lsp->ls_state->owner->so_cred;
2850 refcount_inc(&lsp->ls_count);
2851 spin_unlock(&state->state_lock);
2853 nfs4_put_lock_state(prev);
2856 status = nfs41_test_and_free_expired_stateid(server,
2859 trace_nfs4_test_lock_stateid(state, lsp, status);
2860 if (status == -NFS4ERR_EXPIRED ||
2861 status == -NFS4ERR_BAD_STATEID) {
2862 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2863 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2864 if (!recover_lost_locks)
2865 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2866 } else if (status != NFS_OK) {
2868 nfs4_put_lock_state(prev);
2871 spin_lock(&state->state_lock);
2874 spin_unlock(&state->state_lock);
2875 nfs4_put_lock_state(prev);
2881 * nfs41_check_open_stateid - possibly free an open stateid
2883 * @state: NFSv4 state for an inode
2885 * Returns NFS_OK if recovery for this stateid is now finished.
2886 * Otherwise a negative NFS4ERR value is returned.
2888 static int nfs41_check_open_stateid(struct nfs4_state *state)
2890 struct nfs_server *server = NFS_SERVER(state->inode);
2891 nfs4_stateid *stateid = &state->open_stateid;
2892 const struct cred *cred = state->owner->so_cred;
2895 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2896 return -NFS4ERR_BAD_STATEID;
2897 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2898 trace_nfs4_test_open_stateid(state, NULL, status);
2899 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2900 nfs_state_clear_open_state_flags(state);
2901 stateid->type = NFS4_INVALID_STATEID_TYPE;
2904 if (nfs_open_stateid_recover_openmode(state))
2905 return -NFS4ERR_OPENMODE;
2909 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2913 status = nfs41_check_delegation_stateid(state);
2914 if (status != NFS_OK)
2916 nfs41_delegation_recover_stateid(state);
2918 status = nfs41_check_expired_locks(state);
2919 if (status != NFS_OK)
2921 status = nfs41_check_open_stateid(state);
2922 if (status != NFS_OK)
2923 status = nfs4_open_expired(sp, state);
2929 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2930 * fields corresponding to attributes that were used to store the verifier.
2931 * Make sure we clobber those fields in the later setattr call
2933 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2934 struct iattr *sattr, struct nfs4_label **label)
2936 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2941 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2942 attrset[i] = opendata->o_res.attrset[i];
2943 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2944 attrset[i] &= ~bitmask[i];
2947 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2948 sattr->ia_valid : 0;
2950 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2951 if (sattr->ia_valid & ATTR_ATIME_SET)
2952 ret |= ATTR_ATIME_SET;
2957 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2958 if (sattr->ia_valid & ATTR_MTIME_SET)
2959 ret |= ATTR_MTIME_SET;
2964 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
2969 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2970 int flags, struct nfs_open_context *ctx)
2972 struct nfs4_state_owner *sp = opendata->owner;
2973 struct nfs_server *server = sp->so_server;
2974 struct dentry *dentry;
2975 struct nfs4_state *state;
2976 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
2980 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2982 ret = _nfs4_proc_open(opendata, ctx);
2986 state = _nfs4_opendata_to_nfs4_state(opendata);
2987 ret = PTR_ERR(state);
2991 if (server->caps & NFS_CAP_POSIX_LOCK)
2992 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2993 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2994 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2996 dentry = opendata->dentry;
2997 if (d_really_is_negative(dentry)) {
2998 struct dentry *alias;
3000 alias = d_exact_alias(dentry, state->inode);
3002 alias = d_splice_alias(igrab(state->inode), dentry);
3003 /* d_splice_alias() can't fail here - it's a non-directory */
3006 ctx->dentry = dentry = alias;
3008 nfs_set_verifier(dentry,
3009 nfs_save_change_attribute(d_inode(opendata->dir)));
3012 /* Parse layoutget results before we check for access */
3013 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3015 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3020 if (d_inode(dentry) == state->inode) {
3021 nfs_inode_attach_open_context(ctx);
3022 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3023 nfs4_schedule_stateid_recovery(server, state);
3027 if (!opendata->cancelled)
3028 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3033 * Returns a referenced nfs4_state
3035 static int _nfs4_do_open(struct inode *dir,
3036 struct nfs_open_context *ctx,
3038 const struct nfs4_open_createattrs *c,
3041 struct nfs4_state_owner *sp;
3042 struct nfs4_state *state = NULL;
3043 struct nfs_server *server = NFS_SERVER(dir);
3044 struct nfs4_opendata *opendata;
3045 struct dentry *dentry = ctx->dentry;
3046 const struct cred *cred = ctx->cred;
3047 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3048 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3049 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3050 struct iattr *sattr = c->sattr;
3051 struct nfs4_label *label = c->label;
3052 struct nfs4_label *olabel = NULL;
3055 /* Protect against reboot recovery conflicts */
3057 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3059 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3062 status = nfs4_client_recover_expired_lease(server->nfs_client);
3064 goto err_put_state_owner;
3065 if (d_really_is_positive(dentry))
3066 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3068 if (d_really_is_positive(dentry))
3069 claim = NFS4_OPEN_CLAIM_FH;
3070 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3071 c, claim, GFP_KERNEL);
3072 if (opendata == NULL)
3073 goto err_put_state_owner;
3076 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3077 if (IS_ERR(olabel)) {
3078 status = PTR_ERR(olabel);
3079 goto err_opendata_put;
3083 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3084 if (!opendata->f_attr.mdsthreshold) {
3085 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3086 if (!opendata->f_attr.mdsthreshold)
3087 goto err_free_label;
3089 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3091 if (d_really_is_positive(dentry))
3092 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3094 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3096 goto err_free_label;
3099 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3100 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3101 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3103 * send create attributes which was not set by open
3104 * with an extra setattr.
3106 if (attrs || label) {
3107 unsigned ia_old = sattr->ia_valid;
3109 sattr->ia_valid = attrs;
3110 nfs_fattr_init(opendata->o_res.f_attr);
3111 status = nfs4_do_setattr(state->inode, cred,
3112 opendata->o_res.f_attr, sattr,
3113 ctx, label, olabel);
3115 nfs_setattr_update_inode(state->inode, sattr,
3116 opendata->o_res.f_attr);
3117 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3119 sattr->ia_valid = ia_old;
3122 if (opened && opendata->file_created)
3125 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3126 *ctx_th = opendata->f_attr.mdsthreshold;
3127 opendata->f_attr.mdsthreshold = NULL;
3130 nfs4_label_free(olabel);
3132 nfs4_opendata_put(opendata);
3133 nfs4_put_state_owner(sp);
3136 nfs4_label_free(olabel);
3138 nfs4_opendata_put(opendata);
3139 err_put_state_owner:
3140 nfs4_put_state_owner(sp);
3146 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3147 struct nfs_open_context *ctx,
3149 struct iattr *sattr,
3150 struct nfs4_label *label,
3153 struct nfs_server *server = NFS_SERVER(dir);
3154 struct nfs4_exception exception = {
3155 .interruptible = true,
3157 struct nfs4_state *res;
3158 struct nfs4_open_createattrs c = {
3162 [0] = (__u32)jiffies,
3163 [1] = (__u32)current->pid,
3169 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3171 trace_nfs4_open_file(ctx, flags, status);
3174 /* NOTE: BAD_SEQID means the server and client disagree about the
3175 * book-keeping w.r.t. state-changing operations
3176 * (OPEN/CLOSE/LOCK/LOCKU...)
3177 * It is actually a sign of a bug on the client or on the server.
3179 * If we receive a BAD_SEQID error in the particular case of
3180 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3181 * have unhashed the old state_owner for us, and that we can
3182 * therefore safely retry using a new one. We should still warn
3183 * the user though...
3185 if (status == -NFS4ERR_BAD_SEQID) {
3186 pr_warn_ratelimited("NFS: v4 server %s "
3187 " returned a bad sequence-id error!\n",
3188 NFS_SERVER(dir)->nfs_client->cl_hostname);
3189 exception.retry = 1;
3193 * BAD_STATEID on OPEN means that the server cancelled our
3194 * state before it received the OPEN_CONFIRM.
3195 * Recover by retrying the request as per the discussion
3196 * on Page 181 of RFC3530.
3198 if (status == -NFS4ERR_BAD_STATEID) {
3199 exception.retry = 1;
3202 if (status == -EAGAIN) {
3203 /* We must have found a delegation */
3204 exception.retry = 1;
3207 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3209 res = ERR_PTR(nfs4_handle_exception(server,
3210 status, &exception));
3211 } while (exception.retry);
3215 static int _nfs4_do_setattr(struct inode *inode,
3216 struct nfs_setattrargs *arg,
3217 struct nfs_setattrres *res,
3218 const struct cred *cred,
3219 struct nfs_open_context *ctx)
3221 struct nfs_server *server = NFS_SERVER(inode);
3222 struct rpc_message msg = {
3223 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3228 const struct cred *delegation_cred = NULL;
3229 unsigned long timestamp = jiffies;
3233 nfs_fattr_init(res->fattr);
3235 /* Servers should only apply open mode checks for file size changes */
3236 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3240 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3241 /* Use that stateid */
3242 } else if (ctx != NULL && ctx->state) {
3243 struct nfs_lock_context *l_ctx;
3244 if (!nfs4_valid_open_stateid(ctx->state))
3246 l_ctx = nfs_get_lock_context(ctx);
3248 return PTR_ERR(l_ctx);
3249 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3250 &arg->stateid, &delegation_cred);
3251 nfs_put_lock_context(l_ctx);
3256 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3258 if (delegation_cred)
3259 msg.rpc_cred = delegation_cred;
3261 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3263 put_cred(delegation_cred);
3264 if (status == 0 && ctx != NULL)
3265 renew_lease(server, timestamp);
3266 trace_nfs4_setattr(inode, &arg->stateid, status);
3270 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3271 struct nfs_fattr *fattr, struct iattr *sattr,
3272 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3273 struct nfs4_label *olabel)
3275 struct nfs_server *server = NFS_SERVER(inode);
3276 __u32 bitmask[NFS4_BITMASK_SZ];
3277 struct nfs4_state *state = ctx ? ctx->state : NULL;
3278 struct nfs_setattrargs arg = {
3279 .fh = NFS_FH(inode),
3285 struct nfs_setattrres res = {
3290 struct nfs4_exception exception = {
3293 .stateid = &arg.stateid,
3298 nfs4_bitmap_copy_adjust_setattr(bitmask,
3299 nfs4_bitmask(server, olabel),
3302 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3304 case -NFS4ERR_OPENMODE:
3305 if (!(sattr->ia_valid & ATTR_SIZE)) {
3306 pr_warn_once("NFSv4: server %s is incorrectly "
3307 "applying open mode checks to "
3308 "a SETATTR that is not "
3309 "changing file size.\n",
3310 server->nfs_client->cl_hostname);
3312 if (state && !(state->state & FMODE_WRITE)) {
3314 if (sattr->ia_valid & ATTR_OPEN)
3319 err = nfs4_handle_exception(server, err, &exception);
3320 } while (exception.retry);
3326 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3328 if (inode == NULL || !nfs_have_layout(inode))
3331 return pnfs_wait_on_layoutreturn(inode, task);
3335 * Update the seqid of an open stateid
3337 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3338 struct nfs4_state *state)
3345 if (!nfs4_valid_open_stateid(state))
3347 seq = read_seqbegin(&state->seqlock);
3348 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3349 nfs4_stateid_copy(dst, &state->open_stateid);
3350 if (read_seqretry(&state->seqlock, seq))
3354 seqid_open = state->open_stateid.seqid;
3355 if (read_seqretry(&state->seqlock, seq))
3358 dst_seqid = be32_to_cpu(dst->seqid);
3359 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3360 dst->seqid = seqid_open;
3366 * Update the seqid of an open stateid after receiving
3367 * NFS4ERR_OLD_STATEID
3369 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3370 struct nfs4_state *state)
3379 if (!nfs4_valid_open_stateid(state))
3381 seq = read_seqbegin(&state->seqlock);
3382 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3383 if (read_seqretry(&state->seqlock, seq))
3387 seqid_open = state->open_stateid.seqid;
3388 if (read_seqretry(&state->seqlock, seq))
3391 dst_seqid = be32_to_cpu(dst->seqid);
3392 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) >= 0)
3393 dst->seqid = cpu_to_be32(dst_seqid + 1);
3395 dst->seqid = seqid_open;
3403 struct nfs4_closedata {
3404 struct inode *inode;
3405 struct nfs4_state *state;
3406 struct nfs_closeargs arg;
3407 struct nfs_closeres res;
3409 struct nfs4_layoutreturn_args arg;
3410 struct nfs4_layoutreturn_res res;
3411 struct nfs4_xdr_opaque_data ld_private;
3415 struct nfs_fattr fattr;
3416 unsigned long timestamp;
3419 static void nfs4_free_closedata(void *data)
3421 struct nfs4_closedata *calldata = data;
3422 struct nfs4_state_owner *sp = calldata->state->owner;
3423 struct super_block *sb = calldata->state->inode->i_sb;
3425 if (calldata->lr.roc)
3426 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3427 calldata->res.lr_ret);
3428 nfs4_put_open_state(calldata->state);
3429 nfs_free_seqid(calldata->arg.seqid);
3430 nfs4_put_state_owner(sp);
3431 nfs_sb_deactive(sb);
3435 static void nfs4_close_done(struct rpc_task *task, void *data)
3437 struct nfs4_closedata *calldata = data;
3438 struct nfs4_state *state = calldata->state;
3439 struct nfs_server *server = NFS_SERVER(calldata->inode);
3440 nfs4_stateid *res_stateid = NULL;
3441 struct nfs4_exception exception = {
3443 .inode = calldata->inode,
3444 .stateid = &calldata->arg.stateid,
3447 dprintk("%s: begin!\n", __func__);
3448 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3450 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3452 /* Handle Layoutreturn errors */
3453 if (pnfs_roc_done(task, calldata->inode,
3454 &calldata->arg.lr_args,
3455 &calldata->res.lr_res,
3456 &calldata->res.lr_ret) == -EAGAIN)
3459 /* hmm. we are done with the inode, and in the process of freeing
3460 * the state_owner. we keep this around to process errors
3462 switch (task->tk_status) {
3464 res_stateid = &calldata->res.stateid;
3465 renew_lease(server, calldata->timestamp);
3467 case -NFS4ERR_ACCESS:
3468 if (calldata->arg.bitmask != NULL) {
3469 calldata->arg.bitmask = NULL;
3470 calldata->res.fattr = NULL;
3475 case -NFS4ERR_OLD_STATEID:
3476 /* Did we race with OPEN? */
3477 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3481 case -NFS4ERR_ADMIN_REVOKED:
3482 case -NFS4ERR_STALE_STATEID:
3483 case -NFS4ERR_EXPIRED:
3484 nfs4_free_revoked_stateid(server,
3485 &calldata->arg.stateid,
3486 task->tk_msg.rpc_cred);
3488 case -NFS4ERR_BAD_STATEID:
3489 if (calldata->arg.fmode == 0)
3493 task->tk_status = nfs4_async_handle_exception(task,
3494 server, task->tk_status, &exception);
3495 if (exception.retry)
3498 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3499 res_stateid, calldata->arg.fmode);
3501 task->tk_status = 0;
3502 nfs_release_seqid(calldata->arg.seqid);
3503 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3504 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3507 task->tk_status = 0;
3508 rpc_restart_call_prepare(task);
3512 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3514 struct nfs4_closedata *calldata = data;
3515 struct nfs4_state *state = calldata->state;
3516 struct inode *inode = calldata->inode;
3517 struct pnfs_layout_hdr *lo;
3518 bool is_rdonly, is_wronly, is_rdwr;
3521 dprintk("%s: begin!\n", __func__);
3522 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3525 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3526 spin_lock(&state->owner->so_lock);
3527 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3528 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3529 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3530 /* Calculate the change in open mode */
3531 calldata->arg.fmode = 0;
3532 if (state->n_rdwr == 0) {
3533 if (state->n_rdonly == 0)
3534 call_close |= is_rdonly;
3536 calldata->arg.fmode |= FMODE_READ;
3537 if (state->n_wronly == 0)
3538 call_close |= is_wronly;
3540 calldata->arg.fmode |= FMODE_WRITE;
3541 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3542 call_close |= is_rdwr;
3544 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3546 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3547 if (!nfs4_valid_open_stateid(state))
3549 spin_unlock(&state->owner->so_lock);
3552 /* Note: exit _without_ calling nfs4_close_done */
3556 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3557 nfs_release_seqid(calldata->arg.seqid);
3561 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3562 if (lo && !pnfs_layout_is_valid(lo)) {
3563 calldata->arg.lr_args = NULL;
3564 calldata->res.lr_res = NULL;
3567 if (calldata->arg.fmode == 0)
3568 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3570 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3571 /* Close-to-open cache consistency revalidation */
3572 if (!nfs4_have_delegation(inode, FMODE_READ))
3573 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3575 calldata->arg.bitmask = NULL;
3578 calldata->arg.share_access =
3579 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3580 calldata->arg.fmode, 0);
3582 if (calldata->res.fattr == NULL)
3583 calldata->arg.bitmask = NULL;
3584 else if (calldata->arg.bitmask == NULL)
3585 calldata->res.fattr = NULL;
3586 calldata->timestamp = jiffies;
3587 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3588 &calldata->arg.seq_args,
3589 &calldata->res.seq_res,
3591 nfs_release_seqid(calldata->arg.seqid);
3592 dprintk("%s: done!\n", __func__);
3595 task->tk_action = NULL;
3597 nfs4_sequence_done(task, &calldata->res.seq_res);
3600 static const struct rpc_call_ops nfs4_close_ops = {
3601 .rpc_call_prepare = nfs4_close_prepare,
3602 .rpc_call_done = nfs4_close_done,
3603 .rpc_release = nfs4_free_closedata,
3607 * It is possible for data to be read/written from a mem-mapped file
3608 * after the sys_close call (which hits the vfs layer as a flush).
3609 * This means that we can't safely call nfsv4 close on a file until
3610 * the inode is cleared. This in turn means that we are not good
3611 * NFSv4 citizens - we do not indicate to the server to update the file's
3612 * share state even when we are done with one of the three share
3613 * stateid's in the inode.
3615 * NOTE: Caller must be holding the sp->so_owner semaphore!
3617 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3619 struct nfs_server *server = NFS_SERVER(state->inode);
3620 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3621 struct nfs4_closedata *calldata;
3622 struct nfs4_state_owner *sp = state->owner;
3623 struct rpc_task *task;
3624 struct rpc_message msg = {
3625 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3626 .rpc_cred = state->owner->so_cred,
3628 struct rpc_task_setup task_setup_data = {
3629 .rpc_client = server->client,
3630 .rpc_message = &msg,
3631 .callback_ops = &nfs4_close_ops,
3632 .workqueue = nfsiod_workqueue,
3633 .flags = RPC_TASK_ASYNC,
3635 int status = -ENOMEM;
3637 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3638 &task_setup_data.rpc_client, &msg);
3640 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3641 if (calldata == NULL)
3643 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3644 calldata->inode = state->inode;
3645 calldata->state = state;
3646 calldata->arg.fh = NFS_FH(state->inode);
3647 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3648 goto out_free_calldata;
3649 /* Serialization for the sequence id */
3650 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3651 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3652 if (IS_ERR(calldata->arg.seqid))
3653 goto out_free_calldata;
3654 nfs_fattr_init(&calldata->fattr);
3655 calldata->arg.fmode = 0;
3656 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3657 calldata->res.fattr = &calldata->fattr;
3658 calldata->res.seqid = calldata->arg.seqid;
3659 calldata->res.server = server;
3660 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3661 calldata->lr.roc = pnfs_roc(state->inode,
3662 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3663 if (calldata->lr.roc) {
3664 calldata->arg.lr_args = &calldata->lr.arg;
3665 calldata->res.lr_res = &calldata->lr.res;
3667 nfs_sb_active(calldata->inode->i_sb);
3669 msg.rpc_argp = &calldata->arg;
3670 msg.rpc_resp = &calldata->res;
3671 task_setup_data.callback_data = calldata;
3672 task = rpc_run_task(&task_setup_data);
3674 return PTR_ERR(task);
3677 status = rpc_wait_for_completion_task(task);
3683 nfs4_put_open_state(state);
3684 nfs4_put_state_owner(sp);
3688 static struct inode *
3689 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3690 int open_flags, struct iattr *attr, int *opened)
3692 struct nfs4_state *state;
3693 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3695 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3697 /* Protect against concurrent sillydeletes */
3698 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3700 nfs4_label_release_security(label);
3703 return ERR_CAST(state);
3704 return state->inode;
3707 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3709 if (ctx->state == NULL)
3712 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3714 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3717 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3718 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3719 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3721 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3723 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3724 struct nfs4_server_caps_arg args = {
3728 struct nfs4_server_caps_res res = {};
3729 struct rpc_message msg = {
3730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3737 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3738 FATTR4_WORD0_FH_EXPIRE_TYPE |
3739 FATTR4_WORD0_LINK_SUPPORT |
3740 FATTR4_WORD0_SYMLINK_SUPPORT |
3741 FATTR4_WORD0_ACLSUPPORT;
3743 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3745 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3747 /* Sanity check the server answers */
3748 switch (minorversion) {
3750 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3751 res.attr_bitmask[2] = 0;
3754 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3757 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3759 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3760 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3761 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3762 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3763 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3764 NFS_CAP_CTIME|NFS_CAP_MTIME|
3765 NFS_CAP_SECURITY_LABEL);
3766 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3767 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3768 server->caps |= NFS_CAP_ACLS;
3769 if (res.has_links != 0)
3770 server->caps |= NFS_CAP_HARDLINKS;
3771 if (res.has_symlinks != 0)
3772 server->caps |= NFS_CAP_SYMLINKS;
3773 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3774 server->caps |= NFS_CAP_FILEID;
3775 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3776 server->caps |= NFS_CAP_MODE;
3777 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3778 server->caps |= NFS_CAP_NLINK;
3779 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3780 server->caps |= NFS_CAP_OWNER;
3781 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3782 server->caps |= NFS_CAP_OWNER_GROUP;
3783 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3784 server->caps |= NFS_CAP_ATIME;
3785 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3786 server->caps |= NFS_CAP_CTIME;
3787 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3788 server->caps |= NFS_CAP_MTIME;
3789 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3790 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3791 server->caps |= NFS_CAP_SECURITY_LABEL;
3793 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3794 sizeof(server->attr_bitmask));
3795 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3797 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3798 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3799 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3800 server->cache_consistency_bitmask[2] = 0;
3802 /* Avoid a regression due to buggy server */
3803 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3804 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3805 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3806 sizeof(server->exclcreat_bitmask));
3808 server->acl_bitmask = res.acl_bitmask;
3809 server->fh_expire_type = res.fh_expire_type;
3815 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3817 struct nfs4_exception exception = {
3818 .interruptible = true,
3822 err = nfs4_handle_exception(server,
3823 _nfs4_server_capabilities(server, fhandle),
3825 } while (exception.retry);
3829 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3830 struct nfs_fsinfo *info)
3833 struct nfs4_lookup_root_arg args = {
3836 struct nfs4_lookup_res res = {
3838 .fattr = info->fattr,
3841 struct rpc_message msg = {
3842 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3847 bitmask[0] = nfs4_fattr_bitmap[0];
3848 bitmask[1] = nfs4_fattr_bitmap[1];
3850 * Process the label in the upcoming getfattr
3852 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3854 nfs_fattr_init(info->fattr);
3855 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3858 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3859 struct nfs_fsinfo *info)
3861 struct nfs4_exception exception = {
3862 .interruptible = true,
3866 err = _nfs4_lookup_root(server, fhandle, info);
3867 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3870 case -NFS4ERR_WRONGSEC:
3873 err = nfs4_handle_exception(server, err, &exception);
3875 } while (exception.retry);
3880 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3881 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3883 struct rpc_auth_create_args auth_args = {
3884 .pseudoflavor = flavor,
3886 struct rpc_auth *auth;
3888 auth = rpcauth_create(&auth_args, server->client);
3891 return nfs4_lookup_root(server, fhandle, info);
3895 * Retry pseudoroot lookup with various security flavors. We do this when:
3897 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3898 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3900 * Returns zero on success, or a negative NFS4ERR value, or a
3901 * negative errno value.
3903 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3904 struct nfs_fsinfo *info)
3906 /* Per 3530bis 15.33.5 */
3907 static const rpc_authflavor_t flav_array[] = {
3911 RPC_AUTH_UNIX, /* courtesy */
3914 int status = -EPERM;
3917 if (server->auth_info.flavor_len > 0) {
3918 /* try each flavor specified by user */
3919 for (i = 0; i < server->auth_info.flavor_len; i++) {
3920 status = nfs4_lookup_root_sec(server, fhandle, info,
3921 server->auth_info.flavors[i]);
3922 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3927 /* no flavors specified by user, try default list */
3928 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3929 status = nfs4_lookup_root_sec(server, fhandle, info,
3931 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3938 * -EACCES could mean that the user doesn't have correct permissions
3939 * to access the mount. It could also mean that we tried to mount
3940 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3941 * existing mount programs don't handle -EACCES very well so it should
3942 * be mapped to -EPERM instead.
3944 if (status == -EACCES)
3950 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3951 * @server: initialized nfs_server handle
3952 * @fhandle: we fill in the pseudo-fs root file handle
3953 * @info: we fill in an FSINFO struct
3954 * @auth_probe: probe the auth flavours
3956 * Returns zero on success, or a negative errno.
3958 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3959 struct nfs_fsinfo *info,
3965 status = nfs4_lookup_root(server, fhandle, info);
3967 if (auth_probe || status == NFS4ERR_WRONGSEC)
3968 status = server->nfs_client->cl_mvops->find_root_sec(server,
3972 status = nfs4_server_capabilities(server, fhandle);
3974 status = nfs4_do_fsinfo(server, fhandle, info);
3976 return nfs4_map_errors(status);
3979 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3980 struct nfs_fsinfo *info)
3983 struct nfs_fattr *fattr = info->fattr;
3984 struct nfs4_label *label = NULL;
3986 error = nfs4_server_capabilities(server, mntfh);
3988 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3992 label = nfs4_label_alloc(server, GFP_KERNEL);
3994 return PTR_ERR(label);
3996 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
3998 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3999 goto err_free_label;
4002 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4003 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4004 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4007 nfs4_label_free(label);
4013 * Get locations and (maybe) other attributes of a referral.
4014 * Note that we'll actually follow the referral later when
4015 * we detect fsid mismatch in inode revalidation
4017 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4018 const struct qstr *name, struct nfs_fattr *fattr,
4019 struct nfs_fh *fhandle)
4021 int status = -ENOMEM;
4022 struct page *page = NULL;
4023 struct nfs4_fs_locations *locations = NULL;
4025 page = alloc_page(GFP_KERNEL);
4028 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4029 if (locations == NULL)
4032 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4037 * If the fsid didn't change, this is a migration event, not a
4038 * referral. Cause us to drop into the exception handler, which
4039 * will kick off migration recovery.
4041 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4042 dprintk("%s: server did not return a different fsid for"
4043 " a referral at %s\n", __func__, name->name);
4044 status = -NFS4ERR_MOVED;
4047 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4048 nfs_fixup_referral_attributes(&locations->fattr);
4050 /* replace the lookup nfs_fattr with the locations nfs_fattr */
4051 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4052 memset(fhandle, 0, sizeof(struct nfs_fh));
4060 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4061 struct nfs_fattr *fattr, struct nfs4_label *label,
4062 struct inode *inode)
4064 __u32 bitmask[NFS4_BITMASK_SZ];
4065 struct nfs4_getattr_arg args = {
4069 struct nfs4_getattr_res res = {
4074 struct rpc_message msg = {
4075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4079 unsigned short task_flags = 0;
4081 /* Is this is an attribute revalidation, subject to softreval? */
4082 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4083 task_flags |= RPC_TASK_TIMEOUT;
4085 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
4087 nfs_fattr_init(fattr);
4088 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4089 return nfs4_do_call_sync(server->client, server, &msg,
4090 &args.seq_args, &res.seq_res, task_flags);
4093 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4094 struct nfs_fattr *fattr, struct nfs4_label *label,
4095 struct inode *inode)
4097 struct nfs4_exception exception = {
4098 .interruptible = true,
4102 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4103 trace_nfs4_getattr(server, fhandle, fattr, err);
4104 err = nfs4_handle_exception(server, err,
4106 } while (exception.retry);
4111 * The file is not closed if it is opened due to the a request to change
4112 * the size of the file. The open call will not be needed once the
4113 * VFS layer lookup-intents are implemented.
4115 * Close is called when the inode is destroyed.
4116 * If we haven't opened the file for O_WRONLY, we
4117 * need to in the size_change case to obtain a stateid.
4120 * Because OPEN is always done by name in nfsv4, it is
4121 * possible that we opened a different file by the same
4122 * name. We can recognize this race condition, but we
4123 * can't do anything about it besides returning an error.
4125 * This will be fixed with VFS changes (lookup-intent).
4128 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4129 struct iattr *sattr)
4131 struct inode *inode = d_inode(dentry);
4132 const struct cred *cred = NULL;
4133 struct nfs_open_context *ctx = NULL;
4134 struct nfs4_label *label = NULL;
4137 if (pnfs_ld_layoutret_on_setattr(inode) &&
4138 sattr->ia_valid & ATTR_SIZE &&
4139 sattr->ia_size < i_size_read(inode))
4140 pnfs_commit_and_return_layout(inode);
4142 nfs_fattr_init(fattr);
4144 /* Deal with open(O_TRUNC) */
4145 if (sattr->ia_valid & ATTR_OPEN)
4146 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4148 /* Optimization: if the end result is no change, don't RPC */
4149 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4152 /* Search for an existing open(O_WRITE) file */
4153 if (sattr->ia_valid & ATTR_FILE) {
4155 ctx = nfs_file_open_context(sattr->ia_file);
4160 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4162 return PTR_ERR(label);
4164 /* Return any delegations if we're going to change ACLs */
4165 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4166 nfs4_inode_make_writeable(inode);
4168 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4170 nfs_setattr_update_inode(inode, sattr, fattr);
4171 nfs_setsecurity(inode, fattr, label);
4173 nfs4_label_free(label);
4177 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4178 const struct qstr *name, struct nfs_fh *fhandle,
4179 struct nfs_fattr *fattr, struct nfs4_label *label)
4181 struct nfs_server *server = NFS_SERVER(dir);
4183 struct nfs4_lookup_arg args = {
4184 .bitmask = server->attr_bitmask,
4185 .dir_fh = NFS_FH(dir),
4188 struct nfs4_lookup_res res = {
4194 struct rpc_message msg = {
4195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4200 args.bitmask = nfs4_bitmask(server, label);
4202 nfs_fattr_init(fattr);
4204 dprintk("NFS call lookup %s\n", name->name);
4205 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
4206 dprintk("NFS reply lookup: %d\n", status);
4210 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4212 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4213 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4214 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4218 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4219 const struct qstr *name, struct nfs_fh *fhandle,
4220 struct nfs_fattr *fattr, struct nfs4_label *label)
4222 struct nfs4_exception exception = {
4223 .interruptible = true,
4225 struct rpc_clnt *client = *clnt;
4228 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
4229 trace_nfs4_lookup(dir, name, err);
4231 case -NFS4ERR_BADNAME:
4234 case -NFS4ERR_MOVED:
4235 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4236 if (err == -NFS4ERR_MOVED)
4237 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4239 case -NFS4ERR_WRONGSEC:
4241 if (client != *clnt)
4243 client = nfs4_negotiate_security(client, dir, name);
4245 return PTR_ERR(client);
4247 exception.retry = 1;
4250 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4252 } while (exception.retry);
4257 else if (client != *clnt)
4258 rpc_shutdown_client(client);
4263 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
4264 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4265 struct nfs4_label *label)
4268 struct rpc_clnt *client = NFS_CLIENT(dir);
4270 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
4271 if (client != NFS_CLIENT(dir)) {
4272 rpc_shutdown_client(client);
4273 nfs_fixup_secinfo_attributes(fattr);
4279 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
4280 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4282 struct rpc_clnt *client = NFS_CLIENT(dir);
4285 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
4287 return ERR_PTR(status);
4288 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4291 static int _nfs4_proc_lookupp(struct inode *inode,
4292 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4293 struct nfs4_label *label)
4295 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4296 struct nfs_server *server = NFS_SERVER(inode);
4298 struct nfs4_lookupp_arg args = {
4299 .bitmask = server->attr_bitmask,
4300 .fh = NFS_FH(inode),
4302 struct nfs4_lookupp_res res = {
4308 struct rpc_message msg = {
4309 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4314 args.bitmask = nfs4_bitmask(server, label);
4316 nfs_fattr_init(fattr);
4318 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4319 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4321 dprintk("NFS reply lookupp: %d\n", status);
4325 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4326 struct nfs_fattr *fattr, struct nfs4_label *label)
4328 struct nfs4_exception exception = {
4329 .interruptible = true,
4333 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4334 trace_nfs4_lookupp(inode, err);
4335 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4337 } while (exception.retry);
4341 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4343 struct nfs_server *server = NFS_SERVER(inode);
4344 struct nfs4_accessargs args = {
4345 .fh = NFS_FH(inode),
4346 .access = entry->mask,
4348 struct nfs4_accessres res = {
4351 struct rpc_message msg = {
4352 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4355 .rpc_cred = entry->cred,
4359 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4360 res.fattr = nfs_alloc_fattr();
4361 if (res.fattr == NULL)
4363 args.bitmask = server->cache_consistency_bitmask;
4365 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4367 nfs_access_set_mask(entry, res.access);
4369 nfs_refresh_inode(inode, res.fattr);
4371 nfs_free_fattr(res.fattr);
4375 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4377 struct nfs4_exception exception = {
4378 .interruptible = true,
4382 err = _nfs4_proc_access(inode, entry);
4383 trace_nfs4_access(inode, err);
4384 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4386 } while (exception.retry);
4391 * TODO: For the time being, we don't try to get any attributes
4392 * along with any of the zero-copy operations READ, READDIR,
4395 * In the case of the first three, we want to put the GETATTR
4396 * after the read-type operation -- this is because it is hard
4397 * to predict the length of a GETATTR response in v4, and thus
4398 * align the READ data correctly. This means that the GETATTR
4399 * may end up partially falling into the page cache, and we should
4400 * shift it into the 'tail' of the xdr_buf before processing.
4401 * To do this efficiently, we need to know the total length
4402 * of data received, which doesn't seem to be available outside
4405 * In the case of WRITE, we also want to put the GETATTR after
4406 * the operation -- in this case because we want to make sure
4407 * we get the post-operation mtime and size.
4409 * Both of these changes to the XDR layer would in fact be quite
4410 * minor, but I decided to leave them for a subsequent patch.
4412 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4413 unsigned int pgbase, unsigned int pglen)
4415 struct nfs4_readlink args = {
4416 .fh = NFS_FH(inode),
4421 struct nfs4_readlink_res res;
4422 struct rpc_message msg = {
4423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4428 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4431 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4432 unsigned int pgbase, unsigned int pglen)
4434 struct nfs4_exception exception = {
4435 .interruptible = true,
4439 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4440 trace_nfs4_readlink(inode, err);
4441 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4443 } while (exception.retry);
4448 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4451 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4454 struct nfs_server *server = NFS_SERVER(dir);
4455 struct nfs4_label l, *ilabel = NULL;
4456 struct nfs_open_context *ctx;
4457 struct nfs4_state *state;
4460 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4462 return PTR_ERR(ctx);
4464 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4466 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4467 sattr->ia_mode &= ~current_umask();
4468 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4469 if (IS_ERR(state)) {
4470 status = PTR_ERR(state);
4474 nfs4_label_release_security(ilabel);
4475 put_nfs_open_context(ctx);
4480 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4482 struct nfs_server *server = NFS_SERVER(dir);
4483 struct nfs_removeargs args = {
4487 struct nfs_removeres res = {
4490 struct rpc_message msg = {
4491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4495 unsigned long timestamp = jiffies;
4498 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4500 spin_lock(&dir->i_lock);
4501 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
4502 /* Removing a directory decrements nlink in the parent */
4503 if (ftype == NF4DIR && dir->i_nlink > 2)
4504 nfs4_dec_nlink_locked(dir);
4505 spin_unlock(&dir->i_lock);
4510 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4512 struct nfs4_exception exception = {
4513 .interruptible = true,
4515 struct inode *inode = d_inode(dentry);
4519 if (inode->i_nlink == 1)
4520 nfs4_inode_return_delegation(inode);
4522 nfs4_inode_make_writeable(inode);
4525 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4526 trace_nfs4_remove(dir, &dentry->d_name, err);
4527 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4529 } while (exception.retry);
4533 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4535 struct nfs4_exception exception = {
4536 .interruptible = true,
4541 err = _nfs4_proc_remove(dir, name, NF4DIR);
4542 trace_nfs4_remove(dir, name, err);
4543 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4545 } while (exception.retry);
4549 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4550 struct dentry *dentry,
4551 struct inode *inode)
4553 struct nfs_removeargs *args = msg->rpc_argp;
4554 struct nfs_removeres *res = msg->rpc_resp;
4556 res->server = NFS_SB(dentry->d_sb);
4557 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4558 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4560 nfs_fattr_init(res->dir_attr);
4563 nfs4_inode_return_delegation(inode);
4566 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4568 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4569 &data->args.seq_args,
4574 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4576 struct nfs_unlinkdata *data = task->tk_calldata;
4577 struct nfs_removeres *res = &data->res;
4579 if (!nfs4_sequence_done(task, &res->seq_res))
4581 if (nfs4_async_handle_error(task, res->server, NULL,
4582 &data->timeout) == -EAGAIN)
4584 if (task->tk_status == 0)
4585 update_changeattr(dir, &res->cinfo,
4586 res->dir_attr->time_start, 0);
4590 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4591 struct dentry *old_dentry,
4592 struct dentry *new_dentry)
4594 struct nfs_renameargs *arg = msg->rpc_argp;
4595 struct nfs_renameres *res = msg->rpc_resp;
4596 struct inode *old_inode = d_inode(old_dentry);
4597 struct inode *new_inode = d_inode(new_dentry);
4600 nfs4_inode_make_writeable(old_inode);
4602 nfs4_inode_return_delegation(new_inode);
4603 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4604 res->server = NFS_SB(old_dentry->d_sb);
4605 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4608 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4610 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4611 &data->args.seq_args,
4616 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4617 struct inode *new_dir)
4619 struct nfs_renamedata *data = task->tk_calldata;
4620 struct nfs_renameres *res = &data->res;
4622 if (!nfs4_sequence_done(task, &res->seq_res))
4624 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4627 if (task->tk_status == 0) {
4628 if (new_dir != old_dir) {
4629 /* Note: If we moved a directory, nlink will change */
4630 update_changeattr(old_dir, &res->old_cinfo,
4631 res->old_fattr->time_start,
4632 NFS_INO_INVALID_OTHER);
4633 update_changeattr(new_dir, &res->new_cinfo,
4634 res->new_fattr->time_start,
4635 NFS_INO_INVALID_OTHER);
4637 update_changeattr(old_dir, &res->old_cinfo,
4638 res->old_fattr->time_start,
4644 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4646 struct nfs_server *server = NFS_SERVER(inode);
4647 __u32 bitmask[NFS4_BITMASK_SZ];
4648 struct nfs4_link_arg arg = {
4649 .fh = NFS_FH(inode),
4650 .dir_fh = NFS_FH(dir),
4654 struct nfs4_link_res res = {
4658 struct rpc_message msg = {
4659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4663 int status = -ENOMEM;
4665 res.fattr = nfs_alloc_fattr();
4666 if (res.fattr == NULL)
4669 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4670 if (IS_ERR(res.label)) {
4671 status = PTR_ERR(res.label);
4675 nfs4_inode_make_writeable(inode);
4676 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4678 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4680 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
4681 status = nfs_post_op_update_inode(inode, res.fattr);
4683 nfs_setsecurity(inode, res.fattr, res.label);
4687 nfs4_label_free(res.label);
4690 nfs_free_fattr(res.fattr);
4694 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4696 struct nfs4_exception exception = {
4697 .interruptible = true,
4701 err = nfs4_handle_exception(NFS_SERVER(inode),
4702 _nfs4_proc_link(inode, dir, name),
4704 } while (exception.retry);
4708 struct nfs4_createdata {
4709 struct rpc_message msg;
4710 struct nfs4_create_arg arg;
4711 struct nfs4_create_res res;
4713 struct nfs_fattr fattr;
4714 struct nfs4_label *label;
4717 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4718 const struct qstr *name, struct iattr *sattr, u32 ftype)
4720 struct nfs4_createdata *data;
4722 data = kzalloc(sizeof(*data), GFP_KERNEL);
4724 struct nfs_server *server = NFS_SERVER(dir);
4726 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4727 if (IS_ERR(data->label))
4730 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4731 data->msg.rpc_argp = &data->arg;
4732 data->msg.rpc_resp = &data->res;
4733 data->arg.dir_fh = NFS_FH(dir);
4734 data->arg.server = server;
4735 data->arg.name = name;
4736 data->arg.attrs = sattr;
4737 data->arg.ftype = ftype;
4738 data->arg.bitmask = nfs4_bitmask(server, data->label);
4739 data->arg.umask = current_umask();
4740 data->res.server = server;
4741 data->res.fh = &data->fh;
4742 data->res.fattr = &data->fattr;
4743 data->res.label = data->label;
4744 nfs_fattr_init(data->res.fattr);
4752 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4754 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4755 &data->arg.seq_args, &data->res.seq_res, 1);
4757 spin_lock(&dir->i_lock);
4758 update_changeattr_locked(dir, &data->res.dir_cinfo,
4759 data->res.fattr->time_start, 0);
4760 /* Creating a directory bumps nlink in the parent */
4761 if (data->arg.ftype == NF4DIR)
4762 nfs4_inc_nlink_locked(dir);
4763 spin_unlock(&dir->i_lock);
4764 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4769 static void nfs4_free_createdata(struct nfs4_createdata *data)
4771 nfs4_label_free(data->label);
4775 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4776 struct page *page, unsigned int len, struct iattr *sattr,
4777 struct nfs4_label *label)
4779 struct nfs4_createdata *data;
4780 int status = -ENAMETOOLONG;
4782 if (len > NFS4_MAXPATHLEN)
4786 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4790 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4791 data->arg.u.symlink.pages = &page;
4792 data->arg.u.symlink.len = len;
4793 data->arg.label = label;
4795 status = nfs4_do_create(dir, dentry, data);
4797 nfs4_free_createdata(data);
4802 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4803 struct page *page, unsigned int len, struct iattr *sattr)
4805 struct nfs4_exception exception = {
4806 .interruptible = true,
4808 struct nfs4_label l, *label = NULL;
4811 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4814 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4815 trace_nfs4_symlink(dir, &dentry->d_name, err);
4816 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4818 } while (exception.retry);
4820 nfs4_label_release_security(label);
4824 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4825 struct iattr *sattr, struct nfs4_label *label)
4827 struct nfs4_createdata *data;
4828 int status = -ENOMEM;
4830 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4834 data->arg.label = label;
4835 status = nfs4_do_create(dir, dentry, data);
4837 nfs4_free_createdata(data);
4842 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4843 struct iattr *sattr)
4845 struct nfs_server *server = NFS_SERVER(dir);
4846 struct nfs4_exception exception = {
4847 .interruptible = true,
4849 struct nfs4_label l, *label = NULL;
4852 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4854 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4855 sattr->ia_mode &= ~current_umask();
4857 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4858 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4859 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4861 } while (exception.retry);
4862 nfs4_label_release_security(label);
4867 static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4868 u64 cookie, struct page **pages, unsigned int count, bool plus)
4870 struct inode *dir = d_inode(dentry);
4871 struct nfs4_readdir_arg args = {
4876 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4879 struct nfs4_readdir_res res;
4880 struct rpc_message msg = {
4881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4888 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4890 (unsigned long long)cookie);
4891 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4892 res.pgbase = args.pgbase;
4893 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4895 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4896 status += args.pgbase;
4899 nfs_invalidate_atime(dir);
4901 dprintk("%s: returns %d\n", __func__, status);
4905 static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4906 u64 cookie, struct page **pages, unsigned int count, bool plus)
4908 struct nfs4_exception exception = {
4909 .interruptible = true,
4913 err = _nfs4_proc_readdir(dentry, cred, cookie,
4914 pages, count, plus);
4915 trace_nfs4_readdir(d_inode(dentry), err);
4916 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4918 } while (exception.retry);
4922 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4923 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4925 struct nfs4_createdata *data;
4926 int mode = sattr->ia_mode;
4927 int status = -ENOMEM;
4929 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4934 data->arg.ftype = NF4FIFO;
4935 else if (S_ISBLK(mode)) {
4936 data->arg.ftype = NF4BLK;
4937 data->arg.u.device.specdata1 = MAJOR(rdev);
4938 data->arg.u.device.specdata2 = MINOR(rdev);
4940 else if (S_ISCHR(mode)) {
4941 data->arg.ftype = NF4CHR;
4942 data->arg.u.device.specdata1 = MAJOR(rdev);
4943 data->arg.u.device.specdata2 = MINOR(rdev);
4944 } else if (!S_ISSOCK(mode)) {
4949 data->arg.label = label;
4950 status = nfs4_do_create(dir, dentry, data);
4952 nfs4_free_createdata(data);
4957 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4958 struct iattr *sattr, dev_t rdev)
4960 struct nfs_server *server = NFS_SERVER(dir);
4961 struct nfs4_exception exception = {
4962 .interruptible = true,
4964 struct nfs4_label l, *label = NULL;
4967 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4969 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4970 sattr->ia_mode &= ~current_umask();
4972 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4973 trace_nfs4_mknod(dir, &dentry->d_name, err);
4974 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4976 } while (exception.retry);
4978 nfs4_label_release_security(label);
4983 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4984 struct nfs_fsstat *fsstat)
4986 struct nfs4_statfs_arg args = {
4988 .bitmask = server->attr_bitmask,
4990 struct nfs4_statfs_res res = {
4993 struct rpc_message msg = {
4994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4999 nfs_fattr_init(fsstat->fattr);
5000 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5003 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5005 struct nfs4_exception exception = {
5006 .interruptible = true,
5010 err = nfs4_handle_exception(server,
5011 _nfs4_proc_statfs(server, fhandle, fsstat),
5013 } while (exception.retry);
5017 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5018 struct nfs_fsinfo *fsinfo)
5020 struct nfs4_fsinfo_arg args = {
5022 .bitmask = server->attr_bitmask,
5024 struct nfs4_fsinfo_res res = {
5027 struct rpc_message msg = {
5028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5033 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5036 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5038 struct nfs4_exception exception = {
5039 .interruptible = true,
5041 unsigned long now = jiffies;
5045 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5046 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5048 nfs4_set_lease_period(server->nfs_client,
5049 fsinfo->lease_time * HZ,
5053 err = nfs4_handle_exception(server, err, &exception);
5054 } while (exception.retry);
5058 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5062 nfs_fattr_init(fsinfo->fattr);
5063 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5065 /* block layout checks this! */
5066 server->pnfs_blksize = fsinfo->blksize;
5067 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5073 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5074 struct nfs_pathconf *pathconf)
5076 struct nfs4_pathconf_arg args = {
5078 .bitmask = server->attr_bitmask,
5080 struct nfs4_pathconf_res res = {
5081 .pathconf = pathconf,
5083 struct rpc_message msg = {
5084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5089 /* None of the pathconf attributes are mandatory to implement */
5090 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5091 memset(pathconf, 0, sizeof(*pathconf));
5095 nfs_fattr_init(pathconf->fattr);
5096 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5099 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5100 struct nfs_pathconf *pathconf)
5102 struct nfs4_exception exception = {
5103 .interruptible = true,
5108 err = nfs4_handle_exception(server,
5109 _nfs4_proc_pathconf(server, fhandle, pathconf),
5111 } while (exception.retry);
5115 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5116 const struct nfs_open_context *ctx,
5117 const struct nfs_lock_context *l_ctx,
5120 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5122 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5124 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5125 const struct nfs_open_context *ctx,
5126 const struct nfs_lock_context *l_ctx,
5129 nfs4_stateid _current_stateid;
5131 /* If the current stateid represents a lost lock, then exit */
5132 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5134 return nfs4_stateid_match(stateid, &_current_stateid);
5137 static bool nfs4_error_stateid_expired(int err)
5140 case -NFS4ERR_DELEG_REVOKED:
5141 case -NFS4ERR_ADMIN_REVOKED:
5142 case -NFS4ERR_BAD_STATEID:
5143 case -NFS4ERR_STALE_STATEID:
5144 case -NFS4ERR_OLD_STATEID:
5145 case -NFS4ERR_OPENMODE:
5146 case -NFS4ERR_EXPIRED:
5152 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5154 struct nfs_server *server = NFS_SERVER(hdr->inode);
5156 trace_nfs4_read(hdr, task->tk_status);
5157 if (task->tk_status < 0) {
5158 struct nfs4_exception exception = {
5159 .inode = hdr->inode,
5160 .state = hdr->args.context->state,
5161 .stateid = &hdr->args.stateid,
5163 task->tk_status = nfs4_async_handle_exception(task,
5164 server, task->tk_status, &exception);
5165 if (exception.retry) {
5166 rpc_restart_call_prepare(task);
5171 if (task->tk_status > 0)
5172 renew_lease(server, hdr->timestamp);
5176 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5177 struct nfs_pgio_args *args)
5180 if (!nfs4_error_stateid_expired(task->tk_status) ||
5181 nfs4_stateid_is_current(&args->stateid,
5186 rpc_restart_call_prepare(task);
5190 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5193 dprintk("--> %s\n", __func__);
5195 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5197 if (nfs4_read_stateid_changed(task, &hdr->args))
5199 if (task->tk_status > 0)
5200 nfs_invalidate_atime(hdr->inode);
5201 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5202 nfs4_read_done_cb(task, hdr);
5205 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5206 struct rpc_message *msg)
5208 hdr->timestamp = jiffies;
5209 if (!hdr->pgio_done_cb)
5210 hdr->pgio_done_cb = nfs4_read_done_cb;
5211 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5212 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5215 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5216 struct nfs_pgio_header *hdr)
5218 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5219 &hdr->args.seq_args,
5223 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5224 hdr->args.lock_context,
5225 hdr->rw_mode) == -EIO)
5227 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5232 static int nfs4_write_done_cb(struct rpc_task *task,
5233 struct nfs_pgio_header *hdr)
5235 struct inode *inode = hdr->inode;
5237 trace_nfs4_write(hdr, task->tk_status);
5238 if (task->tk_status < 0) {
5239 struct nfs4_exception exception = {
5240 .inode = hdr->inode,
5241 .state = hdr->args.context->state,
5242 .stateid = &hdr->args.stateid,
5244 task->tk_status = nfs4_async_handle_exception(task,
5245 NFS_SERVER(inode), task->tk_status,
5247 if (exception.retry) {
5248 rpc_restart_call_prepare(task);
5252 if (task->tk_status >= 0) {
5253 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5254 nfs_writeback_update_inode(hdr);
5259 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5260 struct nfs_pgio_args *args)
5263 if (!nfs4_error_stateid_expired(task->tk_status) ||
5264 nfs4_stateid_is_current(&args->stateid,
5269 rpc_restart_call_prepare(task);
5273 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5275 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5277 if (nfs4_write_stateid_changed(task, &hdr->args))
5279 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5280 nfs4_write_done_cb(task, hdr);
5284 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5286 /* Don't request attributes for pNFS or O_DIRECT writes */
5287 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5289 /* Otherwise, request attributes if and only if we don't hold
5292 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5295 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5296 struct rpc_message *msg,
5297 struct rpc_clnt **clnt)
5299 struct nfs_server *server = NFS_SERVER(hdr->inode);
5301 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5302 hdr->args.bitmask = NULL;
5303 hdr->res.fattr = NULL;
5305 hdr->args.bitmask = server->cache_consistency_bitmask;
5307 if (!hdr->pgio_done_cb)
5308 hdr->pgio_done_cb = nfs4_write_done_cb;
5309 hdr->res.server = server;
5310 hdr->timestamp = jiffies;
5312 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5313 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
5314 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5317 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5319 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5320 &data->args.seq_args,
5325 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5327 struct inode *inode = data->inode;
5329 trace_nfs4_commit(data, task->tk_status);
5330 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5331 NULL, NULL) == -EAGAIN) {
5332 rpc_restart_call_prepare(task);
5338 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5340 if (!nfs4_sequence_done(task, &data->res.seq_res))
5342 return data->commit_done_cb(task, data);
5345 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5346 struct rpc_clnt **clnt)
5348 struct nfs_server *server = NFS_SERVER(data->inode);
5350 if (data->commit_done_cb == NULL)
5351 data->commit_done_cb = nfs4_commit_done_cb;
5352 data->res.server = server;
5353 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5354 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5355 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5358 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5359 struct nfs_commitres *res)
5361 struct inode *dst_inode = file_inode(dst);
5362 struct nfs_server *server = NFS_SERVER(dst_inode);
5363 struct rpc_message msg = {
5364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5369 args->fh = NFS_FH(dst_inode);
5370 return nfs4_call_sync(server->client, server, &msg,
5371 &args->seq_args, &res->seq_res, 1);
5374 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5376 struct nfs_commitargs args = {
5380 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5381 struct nfs4_exception exception = { };
5385 status = _nfs4_proc_commit(dst, &args, res);
5386 status = nfs4_handle_exception(dst_server, status, &exception);
5387 } while (exception.retry);
5392 struct nfs4_renewdata {
5393 struct nfs_client *client;
5394 unsigned long timestamp;
5398 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5399 * standalone procedure for queueing an asynchronous RENEW.
5401 static void nfs4_renew_release(void *calldata)
5403 struct nfs4_renewdata *data = calldata;
5404 struct nfs_client *clp = data->client;
5406 if (refcount_read(&clp->cl_count) > 1)
5407 nfs4_schedule_state_renewal(clp);
5408 nfs_put_client(clp);
5412 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5414 struct nfs4_renewdata *data = calldata;
5415 struct nfs_client *clp = data->client;
5416 unsigned long timestamp = data->timestamp;
5418 trace_nfs4_renew_async(clp, task->tk_status);
5419 switch (task->tk_status) {
5422 case -NFS4ERR_LEASE_MOVED:
5423 nfs4_schedule_lease_moved_recovery(clp);
5426 /* Unless we're shutting down, schedule state recovery! */
5427 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5429 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5430 nfs4_schedule_lease_recovery(clp);
5433 nfs4_schedule_path_down_recovery(clp);
5435 do_renew_lease(clp, timestamp);
5438 static const struct rpc_call_ops nfs4_renew_ops = {
5439 .rpc_call_done = nfs4_renew_done,
5440 .rpc_release = nfs4_renew_release,
5443 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5445 struct rpc_message msg = {
5446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5450 struct nfs4_renewdata *data;
5452 if (renew_flags == 0)
5454 if (!refcount_inc_not_zero(&clp->cl_count))
5456 data = kmalloc(sizeof(*data), GFP_NOFS);
5458 nfs_put_client(clp);
5462 data->timestamp = jiffies;
5463 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5464 &nfs4_renew_ops, data);
5467 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5469 struct rpc_message msg = {
5470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5474 unsigned long now = jiffies;
5477 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5480 do_renew_lease(clp, now);
5484 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5486 return server->caps & NFS_CAP_ACLS;
5489 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5490 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5493 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5495 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5496 struct page **pages)
5498 struct page *newpage, **spages;
5504 len = min_t(size_t, PAGE_SIZE, buflen);
5505 newpage = alloc_page(GFP_KERNEL);
5507 if (newpage == NULL)
5509 memcpy(page_address(newpage), buf, len);
5514 } while (buflen != 0);
5520 __free_page(spages[rc-1]);
5524 struct nfs4_cached_acl {
5530 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5532 struct nfs_inode *nfsi = NFS_I(inode);
5534 spin_lock(&inode->i_lock);
5535 kfree(nfsi->nfs4_acl);
5536 nfsi->nfs4_acl = acl;
5537 spin_unlock(&inode->i_lock);
5540 static void nfs4_zap_acl_attr(struct inode *inode)
5542 nfs4_set_cached_acl(inode, NULL);
5545 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5547 struct nfs_inode *nfsi = NFS_I(inode);
5548 struct nfs4_cached_acl *acl;
5551 spin_lock(&inode->i_lock);
5552 acl = nfsi->nfs4_acl;
5555 if (buf == NULL) /* user is just asking for length */
5557 if (acl->cached == 0)
5559 ret = -ERANGE; /* see getxattr(2) man page */
5560 if (acl->len > buflen)
5562 memcpy(buf, acl->data, acl->len);
5566 spin_unlock(&inode->i_lock);
5570 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5572 struct nfs4_cached_acl *acl;
5573 size_t buflen = sizeof(*acl) + acl_len;
5575 if (buflen <= PAGE_SIZE) {
5576 acl = kmalloc(buflen, GFP_KERNEL);
5580 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5582 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5589 nfs4_set_cached_acl(inode, acl);
5593 * The getxattr API returns the required buffer length when called with a
5594 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5595 * the required buf. On a NULL buf, we send a page of data to the server
5596 * guessing that the ACL request can be serviced by a page. If so, we cache
5597 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5598 * the cache. If not so, we throw away the page, and cache the required
5599 * length. The next getxattr call will then produce another round trip to
5600 * the server, this time with the input buf of the required size.
5602 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5604 struct page **pages;
5605 struct nfs_getaclargs args = {
5606 .fh = NFS_FH(inode),
5609 struct nfs_getaclres res = {
5612 struct rpc_message msg = {
5613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5617 unsigned int npages;
5618 int ret = -ENOMEM, i;
5619 struct nfs_server *server = NFS_SERVER(inode);
5622 buflen = server->rsize;
5624 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5625 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5629 args.acl_pages = pages;
5631 for (i = 0; i < npages; i++) {
5632 pages[i] = alloc_page(GFP_KERNEL);
5637 /* for decoding across pages */
5638 res.acl_scratch = alloc_page(GFP_KERNEL);
5639 if (!res.acl_scratch)
5642 args.acl_len = npages * PAGE_SIZE;
5644 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5645 __func__, buf, buflen, npages, args.acl_len);
5646 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5647 &msg, &args.seq_args, &res.seq_res, 0);
5651 /* Handle the case where the passed-in buffer is too short */
5652 if (res.acl_flags & NFS4_ACL_TRUNC) {
5653 /* Did the user only issue a request for the acl length? */
5659 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5661 if (res.acl_len > buflen) {
5665 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5670 for (i = 0; i < npages; i++)
5672 __free_page(pages[i]);
5673 if (res.acl_scratch)
5674 __free_page(res.acl_scratch);
5679 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5681 struct nfs4_exception exception = {
5682 .interruptible = true,
5686 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5687 trace_nfs4_get_acl(inode, ret);
5690 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5691 } while (exception.retry);
5695 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5697 struct nfs_server *server = NFS_SERVER(inode);
5700 if (!nfs4_server_supports_acls(server))
5702 ret = nfs_revalidate_inode(server, inode);
5705 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5706 nfs_zap_acl_cache(inode);
5707 ret = nfs4_read_cached_acl(inode, buf, buflen);
5709 /* -ENOENT is returned if there is no ACL or if there is an ACL
5710 * but no cached acl data, just the acl length */
5712 return nfs4_get_acl_uncached(inode, buf, buflen);
5715 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5717 struct nfs_server *server = NFS_SERVER(inode);
5718 struct page *pages[NFS4ACL_MAXPAGES];
5719 struct nfs_setaclargs arg = {
5720 .fh = NFS_FH(inode),
5724 struct nfs_setaclres res;
5725 struct rpc_message msg = {
5726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5730 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5733 if (!nfs4_server_supports_acls(server))
5735 if (npages > ARRAY_SIZE(pages))
5737 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5740 nfs4_inode_make_writeable(inode);
5741 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5744 * Free each page after tx, so the only ref left is
5745 * held by the network stack
5748 put_page(pages[i-1]);
5751 * Acl update can result in inode attribute update.
5752 * so mark the attribute cache invalid.
5754 spin_lock(&inode->i_lock);
5755 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5756 | NFS_INO_INVALID_CTIME
5757 | NFS_INO_REVAL_FORCED;
5758 spin_unlock(&inode->i_lock);
5759 nfs_access_zap_cache(inode);
5760 nfs_zap_acl_cache(inode);
5764 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5766 struct nfs4_exception exception = { };
5769 err = __nfs4_proc_set_acl(inode, buf, buflen);
5770 trace_nfs4_set_acl(inode, err);
5771 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5773 } while (exception.retry);
5777 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5778 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5781 struct nfs_server *server = NFS_SERVER(inode);
5782 struct nfs_fattr fattr;
5783 struct nfs4_label label = {0, 0, buflen, buf};
5785 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5786 struct nfs4_getattr_arg arg = {
5787 .fh = NFS_FH(inode),
5790 struct nfs4_getattr_res res = {
5795 struct rpc_message msg = {
5796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5802 nfs_fattr_init(&fattr);
5804 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5807 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5809 if (buflen < label.len)
5814 static int nfs4_get_security_label(struct inode *inode, void *buf,
5817 struct nfs4_exception exception = {
5818 .interruptible = true,
5822 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5826 err = _nfs4_get_security_label(inode, buf, buflen);
5827 trace_nfs4_get_security_label(inode, err);
5828 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5830 } while (exception.retry);
5834 static int _nfs4_do_set_security_label(struct inode *inode,
5835 struct nfs4_label *ilabel,
5836 struct nfs_fattr *fattr,
5837 struct nfs4_label *olabel)
5840 struct iattr sattr = {0};
5841 struct nfs_server *server = NFS_SERVER(inode);
5842 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5843 struct nfs_setattrargs arg = {
5844 .fh = NFS_FH(inode),
5850 struct nfs_setattrres res = {
5855 struct rpc_message msg = {
5856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5862 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5864 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5866 dprintk("%s failed: %d\n", __func__, status);
5871 static int nfs4_do_set_security_label(struct inode *inode,
5872 struct nfs4_label *ilabel,
5873 struct nfs_fattr *fattr,
5874 struct nfs4_label *olabel)
5876 struct nfs4_exception exception = { };
5880 err = _nfs4_do_set_security_label(inode, ilabel,
5882 trace_nfs4_set_security_label(inode, err);
5883 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5885 } while (exception.retry);
5890 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5892 struct nfs4_label ilabel, *olabel = NULL;
5893 struct nfs_fattr fattr;
5896 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5899 nfs_fattr_init(&fattr);
5903 ilabel.label = (char *)buf;
5904 ilabel.len = buflen;
5906 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5907 if (IS_ERR(olabel)) {
5908 status = -PTR_ERR(olabel);
5912 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5914 nfs_setsecurity(inode, &fattr, olabel);
5916 nfs4_label_free(olabel);
5920 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5923 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5924 nfs4_verifier *bootverf)
5928 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5929 /* An impossible timestamp guarantees this value
5930 * will never match a generated boot time. */
5931 verf[0] = cpu_to_be32(U32_MAX);
5932 verf[1] = cpu_to_be32(U32_MAX);
5934 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5935 u64 ns = ktime_to_ns(nn->boot_time);
5937 verf[0] = cpu_to_be32(ns >> 32);
5938 verf[1] = cpu_to_be32(ns);
5940 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5944 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5949 if (clp->cl_owner_id != NULL)
5954 strlen(clp->cl_rpcclient->cl_nodename) +
5956 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5959 if (nfs4_client_id_uniquifier[0] != '\0')
5960 len += strlen(nfs4_client_id_uniquifier) + 1;
5961 if (len > NFS4_OPAQUE_LIMIT + 1)
5965 * Since this string is allocated at mount time, and held until the
5966 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5967 * about a memory-reclaim deadlock.
5969 str = kmalloc(len, GFP_KERNEL);
5974 if (nfs4_client_id_uniquifier[0] != '\0')
5975 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
5976 clp->cl_rpcclient->cl_nodename,
5977 nfs4_client_id_uniquifier,
5978 rpc_peeraddr2str(clp->cl_rpcclient,
5981 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
5982 clp->cl_rpcclient->cl_nodename,
5983 rpc_peeraddr2str(clp->cl_rpcclient,
5987 clp->cl_owner_id = str;
5992 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5997 len = 10 + 10 + 1 + 10 + 1 +
5998 strlen(nfs4_client_id_uniquifier) + 1 +
5999 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6001 if (len > NFS4_OPAQUE_LIMIT + 1)
6005 * Since this string is allocated at mount time, and held until the
6006 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6007 * about a memory-reclaim deadlock.
6009 str = kmalloc(len, GFP_KERNEL);
6013 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6014 clp->rpc_ops->version, clp->cl_minorversion,
6015 nfs4_client_id_uniquifier,
6016 clp->cl_rpcclient->cl_nodename);
6017 clp->cl_owner_id = str;
6022 nfs4_init_uniform_client_string(struct nfs_client *clp)
6027 if (clp->cl_owner_id != NULL)
6030 if (nfs4_client_id_uniquifier[0] != '\0')
6031 return nfs4_init_uniquifier_client_string(clp);
6033 len = 10 + 10 + 1 + 10 + 1 +
6034 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6036 if (len > NFS4_OPAQUE_LIMIT + 1)
6040 * Since this string is allocated at mount time, and held until the
6041 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6042 * about a memory-reclaim deadlock.
6044 str = kmalloc(len, GFP_KERNEL);
6048 scnprintf(str, len, "Linux NFSv%u.%u %s",
6049 clp->rpc_ops->version, clp->cl_minorversion,
6050 clp->cl_rpcclient->cl_nodename);
6051 clp->cl_owner_id = str;
6056 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6057 * services. Advertise one based on the address family of the
6061 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6063 if (strchr(clp->cl_ipaddr, ':') != NULL)
6064 return scnprintf(buf, len, "tcp6");
6066 return scnprintf(buf, len, "tcp");
6069 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6071 struct nfs4_setclientid *sc = calldata;
6073 if (task->tk_status == 0)
6074 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6077 static const struct rpc_call_ops nfs4_setclientid_ops = {
6078 .rpc_call_done = nfs4_setclientid_done,
6082 * nfs4_proc_setclientid - Negotiate client ID
6083 * @clp: state data structure
6084 * @program: RPC program for NFSv4 callback service
6085 * @port: IP port number for NFS4 callback service
6086 * @cred: credential to use for this call
6087 * @res: where to place the result
6089 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6091 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6092 unsigned short port, const struct cred *cred,
6093 struct nfs4_setclientid_res *res)
6095 nfs4_verifier sc_verifier;
6096 struct nfs4_setclientid setclientid = {
6097 .sc_verifier = &sc_verifier,
6101 struct rpc_message msg = {
6102 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6103 .rpc_argp = &setclientid,
6107 struct rpc_task_setup task_setup_data = {
6108 .rpc_client = clp->cl_rpcclient,
6109 .rpc_message = &msg,
6110 .callback_ops = &nfs4_setclientid_ops,
6111 .callback_data = &setclientid,
6112 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6116 /* nfs_client_id4 */
6117 nfs4_init_boot_verifier(clp, &sc_verifier);
6119 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6120 status = nfs4_init_uniform_client_string(clp);
6122 status = nfs4_init_nonuniform_client_string(clp);
6128 setclientid.sc_netid_len =
6129 nfs4_init_callback_netid(clp,
6130 setclientid.sc_netid,
6131 sizeof(setclientid.sc_netid));
6132 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6133 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6134 clp->cl_ipaddr, port >> 8, port & 255);
6136 dprintk("NFS call setclientid auth=%s, '%s'\n",
6137 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6140 status = nfs4_call_sync_custom(&task_setup_data);
6141 if (setclientid.sc_cred) {
6142 kfree(clp->cl_acceptor);
6143 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6144 put_rpccred(setclientid.sc_cred);
6147 trace_nfs4_setclientid(clp, status);
6148 dprintk("NFS reply setclientid: %d\n", status);
6153 * nfs4_proc_setclientid_confirm - Confirm client ID
6154 * @clp: state data structure
6155 * @arg: result of a previous SETCLIENTID
6156 * @cred: credential to use for this call
6158 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6160 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6161 struct nfs4_setclientid_res *arg,
6162 const struct cred *cred)
6164 struct rpc_message msg = {
6165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6171 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6172 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6174 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6175 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6176 trace_nfs4_setclientid_confirm(clp, status);
6177 dprintk("NFS reply setclientid_confirm: %d\n", status);
6181 struct nfs4_delegreturndata {
6182 struct nfs4_delegreturnargs args;
6183 struct nfs4_delegreturnres res;
6185 nfs4_stateid stateid;
6186 unsigned long timestamp;
6188 struct nfs4_layoutreturn_args arg;
6189 struct nfs4_layoutreturn_res res;
6190 struct nfs4_xdr_opaque_data ld_private;
6194 struct nfs_fattr fattr;
6196 struct inode *inode;
6199 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6201 struct nfs4_delegreturndata *data = calldata;
6202 struct nfs4_exception exception = {
6203 .inode = data->inode,
6204 .stateid = &data->stateid,
6207 if (!nfs4_sequence_done(task, &data->res.seq_res))
6210 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6212 /* Handle Layoutreturn errors */
6213 if (pnfs_roc_done(task, data->inode,
6214 &data->args.lr_args,
6216 &data->res.lr_ret) == -EAGAIN)
6219 switch (task->tk_status) {
6221 renew_lease(data->res.server, data->timestamp);
6223 case -NFS4ERR_ADMIN_REVOKED:
6224 case -NFS4ERR_DELEG_REVOKED:
6225 case -NFS4ERR_EXPIRED:
6226 nfs4_free_revoked_stateid(data->res.server,
6228 task->tk_msg.rpc_cred);
6230 case -NFS4ERR_BAD_STATEID:
6231 case -NFS4ERR_STALE_STATEID:
6232 task->tk_status = 0;
6234 case -NFS4ERR_OLD_STATEID:
6235 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6236 nfs4_stateid_seqid_inc(&data->stateid);
6237 if (data->args.bitmask) {
6238 data->args.bitmask = NULL;
6239 data->res.fattr = NULL;
6242 case -NFS4ERR_ACCESS:
6243 if (data->args.bitmask) {
6244 data->args.bitmask = NULL;
6245 data->res.fattr = NULL;
6250 task->tk_status = nfs4_async_handle_exception(task,
6251 data->res.server, task->tk_status,
6253 if (exception.retry)
6256 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6257 data->rpc_status = task->tk_status;
6260 task->tk_status = 0;
6261 rpc_restart_call_prepare(task);
6264 static void nfs4_delegreturn_release(void *calldata)
6266 struct nfs4_delegreturndata *data = calldata;
6267 struct inode *inode = data->inode;
6271 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6273 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6274 nfs_iput_and_deactive(inode);
6279 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6281 struct nfs4_delegreturndata *d_data;
6282 struct pnfs_layout_hdr *lo;
6284 d_data = (struct nfs4_delegreturndata *)data;
6286 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6287 nfs4_sequence_done(task, &d_data->res.seq_res);
6291 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6292 if (lo && !pnfs_layout_is_valid(lo)) {
6293 d_data->args.lr_args = NULL;
6294 d_data->res.lr_res = NULL;
6297 nfs4_setup_sequence(d_data->res.server->nfs_client,
6298 &d_data->args.seq_args,
6299 &d_data->res.seq_res,
6303 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6304 .rpc_call_prepare = nfs4_delegreturn_prepare,
6305 .rpc_call_done = nfs4_delegreturn_done,
6306 .rpc_release = nfs4_delegreturn_release,
6309 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6311 struct nfs4_delegreturndata *data;
6312 struct nfs_server *server = NFS_SERVER(inode);
6313 struct rpc_task *task;
6314 struct rpc_message msg = {
6315 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6318 struct rpc_task_setup task_setup_data = {
6319 .rpc_client = server->client,
6320 .rpc_message = &msg,
6321 .callback_ops = &nfs4_delegreturn_ops,
6322 .flags = RPC_TASK_ASYNC,
6326 data = kzalloc(sizeof(*data), GFP_NOFS);
6329 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6331 nfs4_state_protect(server->nfs_client,
6332 NFS_SP4_MACH_CRED_CLEANUP,
6333 &task_setup_data.rpc_client, &msg);
6335 data->args.fhandle = &data->fh;
6336 data->args.stateid = &data->stateid;
6337 data->args.bitmask = server->cache_consistency_bitmask;
6338 nfs_copy_fh(&data->fh, NFS_FH(inode));
6339 nfs4_stateid_copy(&data->stateid, stateid);
6340 data->res.fattr = &data->fattr;
6341 data->res.server = server;
6342 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6343 data->lr.arg.ld_private = &data->lr.ld_private;
6344 nfs_fattr_init(data->res.fattr);
6345 data->timestamp = jiffies;
6346 data->rpc_status = 0;
6347 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
6348 data->inode = nfs_igrab_and_active(inode);
6351 data->args.lr_args = &data->lr.arg;
6352 data->res.lr_res = &data->lr.res;
6354 } else if (data->lr.roc) {
6355 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6356 data->lr.roc = false;
6359 task_setup_data.callback_data = data;
6360 msg.rpc_argp = &data->args;
6361 msg.rpc_resp = &data->res;
6362 task = rpc_run_task(&task_setup_data);
6364 return PTR_ERR(task);
6367 status = rpc_wait_for_completion_task(task);
6370 status = data->rpc_status;
6376 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6378 struct nfs_server *server = NFS_SERVER(inode);
6379 struct nfs4_exception exception = { };
6382 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6383 trace_nfs4_delegreturn(inode, stateid, err);
6385 case -NFS4ERR_STALE_STATEID:
6386 case -NFS4ERR_EXPIRED:
6390 err = nfs4_handle_exception(server, err, &exception);
6391 } while (exception.retry);
6395 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6397 struct inode *inode = state->inode;
6398 struct nfs_server *server = NFS_SERVER(inode);
6399 struct nfs_client *clp = server->nfs_client;
6400 struct nfs_lockt_args arg = {
6401 .fh = NFS_FH(inode),
6404 struct nfs_lockt_res res = {
6407 struct rpc_message msg = {
6408 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6411 .rpc_cred = state->owner->so_cred,
6413 struct nfs4_lock_state *lsp;
6416 arg.lock_owner.clientid = clp->cl_clientid;
6417 status = nfs4_set_lock_state(state, request);
6420 lsp = request->fl_u.nfs4_fl.owner;
6421 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6422 arg.lock_owner.s_dev = server->s_dev;
6423 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6426 request->fl_type = F_UNLCK;
6428 case -NFS4ERR_DENIED:
6431 request->fl_ops->fl_release_private(request);
6432 request->fl_ops = NULL;
6437 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6439 struct nfs4_exception exception = {
6440 .interruptible = true,
6445 err = _nfs4_proc_getlk(state, cmd, request);
6446 trace_nfs4_get_lock(request, state, cmd, err);
6447 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6449 } while (exception.retry);
6454 * Update the seqid of a lock stateid after receiving
6455 * NFS4ERR_OLD_STATEID
6457 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6458 struct nfs4_lock_state *lsp)
6460 struct nfs4_state *state = lsp->ls_state;
6463 spin_lock(&state->state_lock);
6464 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6466 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6467 nfs4_stateid_seqid_inc(dst);
6469 dst->seqid = lsp->ls_stateid.seqid;
6472 spin_unlock(&state->state_lock);
6476 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6477 struct nfs4_lock_state *lsp)
6479 struct nfs4_state *state = lsp->ls_state;
6482 spin_lock(&state->state_lock);
6483 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6484 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6485 spin_unlock(&state->state_lock);
6489 struct nfs4_unlockdata {
6490 struct nfs_locku_args arg;
6491 struct nfs_locku_res res;
6492 struct nfs4_lock_state *lsp;
6493 struct nfs_open_context *ctx;
6494 struct nfs_lock_context *l_ctx;
6495 struct file_lock fl;
6496 struct nfs_server *server;
6497 unsigned long timestamp;
6500 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6501 struct nfs_open_context *ctx,
6502 struct nfs4_lock_state *lsp,
6503 struct nfs_seqid *seqid)
6505 struct nfs4_unlockdata *p;
6506 struct nfs4_state *state = lsp->ls_state;
6507 struct inode *inode = state->inode;
6509 p = kzalloc(sizeof(*p), GFP_NOFS);
6512 p->arg.fh = NFS_FH(inode);
6514 p->arg.seqid = seqid;
6515 p->res.seqid = seqid;
6517 /* Ensure we don't close file until we're done freeing locks! */
6518 p->ctx = get_nfs_open_context(ctx);
6519 p->l_ctx = nfs_get_lock_context(ctx);
6520 locks_init_lock(&p->fl);
6521 locks_copy_lock(&p->fl, fl);
6522 p->server = NFS_SERVER(inode);
6523 spin_lock(&state->state_lock);
6524 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6525 spin_unlock(&state->state_lock);
6529 static void nfs4_locku_release_calldata(void *data)
6531 struct nfs4_unlockdata *calldata = data;
6532 nfs_free_seqid(calldata->arg.seqid);
6533 nfs4_put_lock_state(calldata->lsp);
6534 nfs_put_lock_context(calldata->l_ctx);
6535 put_nfs_open_context(calldata->ctx);
6539 static void nfs4_locku_done(struct rpc_task *task, void *data)
6541 struct nfs4_unlockdata *calldata = data;
6542 struct nfs4_exception exception = {
6543 .inode = calldata->lsp->ls_state->inode,
6544 .stateid = &calldata->arg.stateid,
6547 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6549 switch (task->tk_status) {
6551 renew_lease(calldata->server, calldata->timestamp);
6552 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6553 if (nfs4_update_lock_stateid(calldata->lsp,
6554 &calldata->res.stateid))
6557 case -NFS4ERR_ADMIN_REVOKED:
6558 case -NFS4ERR_EXPIRED:
6559 nfs4_free_revoked_stateid(calldata->server,
6560 &calldata->arg.stateid,
6561 task->tk_msg.rpc_cred);
6563 case -NFS4ERR_BAD_STATEID:
6564 case -NFS4ERR_STALE_STATEID:
6565 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6567 rpc_restart_call_prepare(task);
6569 case -NFS4ERR_OLD_STATEID:
6570 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6572 rpc_restart_call_prepare(task);
6575 task->tk_status = nfs4_async_handle_exception(task,
6576 calldata->server, task->tk_status,
6578 if (exception.retry)
6579 rpc_restart_call_prepare(task);
6581 nfs_release_seqid(calldata->arg.seqid);
6584 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6586 struct nfs4_unlockdata *calldata = data;
6588 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6589 nfs_async_iocounter_wait(task, calldata->l_ctx))
6592 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6594 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6595 /* Note: exit _without_ running nfs4_locku_done */
6598 calldata->timestamp = jiffies;
6599 if (nfs4_setup_sequence(calldata->server->nfs_client,
6600 &calldata->arg.seq_args,
6601 &calldata->res.seq_res,
6603 nfs_release_seqid(calldata->arg.seqid);
6606 task->tk_action = NULL;
6608 nfs4_sequence_done(task, &calldata->res.seq_res);
6611 static const struct rpc_call_ops nfs4_locku_ops = {
6612 .rpc_call_prepare = nfs4_locku_prepare,
6613 .rpc_call_done = nfs4_locku_done,
6614 .rpc_release = nfs4_locku_release_calldata,
6617 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6618 struct nfs_open_context *ctx,
6619 struct nfs4_lock_state *lsp,
6620 struct nfs_seqid *seqid)
6622 struct nfs4_unlockdata *data;
6623 struct rpc_message msg = {
6624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6625 .rpc_cred = ctx->cred,
6627 struct rpc_task_setup task_setup_data = {
6628 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6629 .rpc_message = &msg,
6630 .callback_ops = &nfs4_locku_ops,
6631 .workqueue = nfsiod_workqueue,
6632 .flags = RPC_TASK_ASYNC,
6635 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6636 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6638 /* Ensure this is an unlock - when canceling a lock, the
6639 * canceled lock is passed in, and it won't be an unlock.
6641 fl->fl_type = F_UNLCK;
6642 if (fl->fl_flags & FL_CLOSE)
6643 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6645 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6647 nfs_free_seqid(seqid);
6648 return ERR_PTR(-ENOMEM);
6651 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6652 msg.rpc_argp = &data->arg;
6653 msg.rpc_resp = &data->res;
6654 task_setup_data.callback_data = data;
6655 return rpc_run_task(&task_setup_data);
6658 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6660 struct inode *inode = state->inode;
6661 struct nfs4_state_owner *sp = state->owner;
6662 struct nfs_inode *nfsi = NFS_I(inode);
6663 struct nfs_seqid *seqid;
6664 struct nfs4_lock_state *lsp;
6665 struct rpc_task *task;
6666 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6668 unsigned char fl_flags = request->fl_flags;
6670 status = nfs4_set_lock_state(state, request);
6671 /* Unlock _before_ we do the RPC call */
6672 request->fl_flags |= FL_EXISTS;
6673 /* Exclude nfs_delegation_claim_locks() */
6674 mutex_lock(&sp->so_delegreturn_mutex);
6675 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6676 down_read(&nfsi->rwsem);
6677 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6678 up_read(&nfsi->rwsem);
6679 mutex_unlock(&sp->so_delegreturn_mutex);
6682 up_read(&nfsi->rwsem);
6683 mutex_unlock(&sp->so_delegreturn_mutex);
6686 /* Is this a delegated lock? */
6687 lsp = request->fl_u.nfs4_fl.owner;
6688 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6690 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6691 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6695 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6696 status = PTR_ERR(task);
6699 status = rpc_wait_for_completion_task(task);
6702 request->fl_flags = fl_flags;
6703 trace_nfs4_unlock(request, state, F_SETLK, status);
6707 struct nfs4_lockdata {
6708 struct nfs_lock_args arg;
6709 struct nfs_lock_res res;
6710 struct nfs4_lock_state *lsp;
6711 struct nfs_open_context *ctx;
6712 struct file_lock fl;
6713 unsigned long timestamp;
6716 struct nfs_server *server;
6719 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6720 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6723 struct nfs4_lockdata *p;
6724 struct inode *inode = lsp->ls_state->inode;
6725 struct nfs_server *server = NFS_SERVER(inode);
6726 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6728 p = kzalloc(sizeof(*p), gfp_mask);
6732 p->arg.fh = NFS_FH(inode);
6734 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6735 if (IS_ERR(p->arg.open_seqid))
6737 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6738 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6739 if (IS_ERR(p->arg.lock_seqid))
6740 goto out_free_seqid;
6741 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6742 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6743 p->arg.lock_owner.s_dev = server->s_dev;
6744 p->res.lock_seqid = p->arg.lock_seqid;
6747 p->ctx = get_nfs_open_context(ctx);
6748 locks_init_lock(&p->fl);
6749 locks_copy_lock(&p->fl, fl);
6752 nfs_free_seqid(p->arg.open_seqid);
6758 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6760 struct nfs4_lockdata *data = calldata;
6761 struct nfs4_state *state = data->lsp->ls_state;
6763 dprintk("%s: begin!\n", __func__);
6764 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6766 /* Do we need to do an open_to_lock_owner? */
6767 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6768 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6769 goto out_release_lock_seqid;
6771 nfs4_stateid_copy(&data->arg.open_stateid,
6772 &state->open_stateid);
6773 data->arg.new_lock_owner = 1;
6774 data->res.open_seqid = data->arg.open_seqid;
6776 data->arg.new_lock_owner = 0;
6777 nfs4_stateid_copy(&data->arg.lock_stateid,
6778 &data->lsp->ls_stateid);
6780 if (!nfs4_valid_open_stateid(state)) {
6781 data->rpc_status = -EBADF;
6782 task->tk_action = NULL;
6783 goto out_release_open_seqid;
6785 data->timestamp = jiffies;
6786 if (nfs4_setup_sequence(data->server->nfs_client,
6787 &data->arg.seq_args,
6791 out_release_open_seqid:
6792 nfs_release_seqid(data->arg.open_seqid);
6793 out_release_lock_seqid:
6794 nfs_release_seqid(data->arg.lock_seqid);
6796 nfs4_sequence_done(task, &data->res.seq_res);
6797 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6800 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6802 struct nfs4_lockdata *data = calldata;
6803 struct nfs4_lock_state *lsp = data->lsp;
6805 dprintk("%s: begin!\n", __func__);
6807 if (!nfs4_sequence_done(task, &data->res.seq_res))
6810 data->rpc_status = task->tk_status;
6811 switch (task->tk_status) {
6813 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6815 if (data->arg.new_lock && !data->cancelled) {
6816 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6817 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6820 if (data->arg.new_lock_owner != 0) {
6821 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6822 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6823 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6824 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6827 case -NFS4ERR_BAD_STATEID:
6828 case -NFS4ERR_OLD_STATEID:
6829 case -NFS4ERR_STALE_STATEID:
6830 case -NFS4ERR_EXPIRED:
6831 if (data->arg.new_lock_owner != 0) {
6832 if (!nfs4_stateid_match(&data->arg.open_stateid,
6833 &lsp->ls_state->open_stateid))
6835 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6840 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6843 if (!data->cancelled)
6844 rpc_restart_call_prepare(task);
6848 static void nfs4_lock_release(void *calldata)
6850 struct nfs4_lockdata *data = calldata;
6852 dprintk("%s: begin!\n", __func__);
6853 nfs_free_seqid(data->arg.open_seqid);
6854 if (data->cancelled && data->rpc_status == 0) {
6855 struct rpc_task *task;
6856 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6857 data->arg.lock_seqid);
6859 rpc_put_task_async(task);
6860 dprintk("%s: cancelling lock!\n", __func__);
6862 nfs_free_seqid(data->arg.lock_seqid);
6863 nfs4_put_lock_state(data->lsp);
6864 put_nfs_open_context(data->ctx);
6866 dprintk("%s: done!\n", __func__);
6869 static const struct rpc_call_ops nfs4_lock_ops = {
6870 .rpc_call_prepare = nfs4_lock_prepare,
6871 .rpc_call_done = nfs4_lock_done,
6872 .rpc_release = nfs4_lock_release,
6875 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6878 case -NFS4ERR_ADMIN_REVOKED:
6879 case -NFS4ERR_EXPIRED:
6880 case -NFS4ERR_BAD_STATEID:
6881 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6882 if (new_lock_owner != 0 ||
6883 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6884 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6886 case -NFS4ERR_STALE_STATEID:
6887 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6888 nfs4_schedule_lease_recovery(server->nfs_client);
6892 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6894 struct nfs4_lockdata *data;
6895 struct rpc_task *task;
6896 struct rpc_message msg = {
6897 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6898 .rpc_cred = state->owner->so_cred,
6900 struct rpc_task_setup task_setup_data = {
6901 .rpc_client = NFS_CLIENT(state->inode),
6902 .rpc_message = &msg,
6903 .callback_ops = &nfs4_lock_ops,
6904 .workqueue = nfsiod_workqueue,
6905 .flags = RPC_TASK_ASYNC,
6909 dprintk("%s: begin!\n", __func__);
6910 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6911 fl->fl_u.nfs4_fl.owner,
6912 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6916 data->arg.block = 1;
6917 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6918 recovery_type > NFS_LOCK_NEW);
6919 msg.rpc_argp = &data->arg;
6920 msg.rpc_resp = &data->res;
6921 task_setup_data.callback_data = data;
6922 if (recovery_type > NFS_LOCK_NEW) {
6923 if (recovery_type == NFS_LOCK_RECLAIM)
6924 data->arg.reclaim = NFS_LOCK_RECLAIM;
6926 data->arg.new_lock = 1;
6927 task = rpc_run_task(&task_setup_data);
6929 return PTR_ERR(task);
6930 ret = rpc_wait_for_completion_task(task);
6932 ret = data->rpc_status;
6934 nfs4_handle_setlk_error(data->server, data->lsp,
6935 data->arg.new_lock_owner, ret);
6937 data->cancelled = true;
6939 dprintk("%s: done, ret = %d!\n", __func__, ret);
6940 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6944 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6946 struct nfs_server *server = NFS_SERVER(state->inode);
6947 struct nfs4_exception exception = {
6948 .inode = state->inode,
6953 /* Cache the lock if possible... */
6954 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6956 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6957 if (err != -NFS4ERR_DELAY)
6959 nfs4_handle_exception(server, err, &exception);
6960 } while (exception.retry);
6964 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6966 struct nfs_server *server = NFS_SERVER(state->inode);
6967 struct nfs4_exception exception = {
6968 .inode = state->inode,
6972 err = nfs4_set_lock_state(state, request);
6975 if (!recover_lost_locks) {
6976 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6980 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6982 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6986 case -NFS4ERR_GRACE:
6987 case -NFS4ERR_DELAY:
6988 nfs4_handle_exception(server, err, &exception);
6991 } while (exception.retry);
6996 #if defined(CONFIG_NFS_V4_1)
6997 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6999 struct nfs4_lock_state *lsp;
7002 status = nfs4_set_lock_state(state, request);
7005 lsp = request->fl_u.nfs4_fl.owner;
7006 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7007 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7009 return nfs4_lock_expired(state, request);
7013 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7015 struct nfs_inode *nfsi = NFS_I(state->inode);
7016 struct nfs4_state_owner *sp = state->owner;
7017 unsigned char fl_flags = request->fl_flags;
7020 request->fl_flags |= FL_ACCESS;
7021 status = locks_lock_inode_wait(state->inode, request);
7024 mutex_lock(&sp->so_delegreturn_mutex);
7025 down_read(&nfsi->rwsem);
7026 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7027 /* Yes: cache locks! */
7028 /* ...but avoid races with delegation recall... */
7029 request->fl_flags = fl_flags & ~FL_SLEEP;
7030 status = locks_lock_inode_wait(state->inode, request);
7031 up_read(&nfsi->rwsem);
7032 mutex_unlock(&sp->so_delegreturn_mutex);
7035 up_read(&nfsi->rwsem);
7036 mutex_unlock(&sp->so_delegreturn_mutex);
7037 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7039 request->fl_flags = fl_flags;
7043 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7045 struct nfs4_exception exception = {
7047 .inode = state->inode,
7048 .interruptible = true,
7053 err = _nfs4_proc_setlk(state, cmd, request);
7054 if (err == -NFS4ERR_DENIED)
7056 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7058 } while (exception.retry);
7062 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7063 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7066 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7067 struct file_lock *request)
7069 int status = -ERESTARTSYS;
7070 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7072 while(!signalled()) {
7073 status = nfs4_proc_setlk(state, cmd, request);
7074 if ((status != -EAGAIN) || IS_SETLK(cmd))
7076 freezable_schedule_timeout_interruptible(timeout);
7078 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7079 status = -ERESTARTSYS;
7084 #ifdef CONFIG_NFS_V4_1
7085 struct nfs4_lock_waiter {
7086 struct task_struct *task;
7087 struct inode *inode;
7088 struct nfs_lowner *owner;
7092 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7095 struct nfs4_lock_waiter *waiter = wait->private;
7097 /* NULL key means to wake up everyone */
7099 struct cb_notify_lock_args *cbnl = key;
7100 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7101 *wowner = waiter->owner;
7103 /* Only wake if the callback was for the same owner. */
7104 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7107 /* Make sure it's for the right inode */
7108 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7112 /* override "private" so we can use default_wake_function */
7113 wait->private = waiter->task;
7114 ret = woken_wake_function(wait, mode, flags, key);
7116 list_del_init(&wait->entry);
7117 wait->private = waiter;
7122 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7124 int status = -ERESTARTSYS;
7125 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7126 struct nfs_server *server = NFS_SERVER(state->inode);
7127 struct nfs_client *clp = server->nfs_client;
7128 wait_queue_head_t *q = &clp->cl_lock_waitq;
7129 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7130 .id = lsp->ls_seqid.owner_id,
7131 .s_dev = server->s_dev };
7132 struct nfs4_lock_waiter waiter = { .task = current,
7133 .inode = state->inode,
7135 wait_queue_entry_t wait;
7137 /* Don't bother with waitqueue if we don't expect a callback */
7138 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7139 return nfs4_retry_setlk_simple(state, cmd, request);
7142 wait.private = &waiter;
7143 wait.func = nfs4_wake_lock_waiter;
7145 while(!signalled()) {
7146 add_wait_queue(q, &wait);
7147 status = nfs4_proc_setlk(state, cmd, request);
7148 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7149 finish_wait(q, &wait);
7153 status = -ERESTARTSYS;
7154 freezer_do_not_count();
7155 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7157 finish_wait(q, &wait);
7162 #else /* !CONFIG_NFS_V4_1 */
7164 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7166 return nfs4_retry_setlk_simple(state, cmd, request);
7171 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7173 struct nfs_open_context *ctx;
7174 struct nfs4_state *state;
7177 /* verify open state */
7178 ctx = nfs_file_open_context(filp);
7181 if (IS_GETLK(cmd)) {
7183 return nfs4_proc_getlk(state, F_GETLK, request);
7187 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7190 if (request->fl_type == F_UNLCK) {
7192 return nfs4_proc_unlck(state, cmd, request);
7199 if ((request->fl_flags & FL_POSIX) &&
7200 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7204 * Don't rely on the VFS having checked the file open mode,
7205 * since it won't do this for flock() locks.
7207 switch (request->fl_type) {
7209 if (!(filp->f_mode & FMODE_READ))
7213 if (!(filp->f_mode & FMODE_WRITE))
7217 status = nfs4_set_lock_state(state, request);
7221 return nfs4_retry_setlk(state, cmd, request);
7224 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7226 struct nfs_server *server = NFS_SERVER(state->inode);
7229 err = nfs4_set_lock_state(state, fl);
7232 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7233 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7236 struct nfs_release_lockowner_data {
7237 struct nfs4_lock_state *lsp;
7238 struct nfs_server *server;
7239 struct nfs_release_lockowner_args args;
7240 struct nfs_release_lockowner_res res;
7241 unsigned long timestamp;
7244 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7246 struct nfs_release_lockowner_data *data = calldata;
7247 struct nfs_server *server = data->server;
7248 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7249 &data->res.seq_res, task);
7250 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7251 data->timestamp = jiffies;
7254 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7256 struct nfs_release_lockowner_data *data = calldata;
7257 struct nfs_server *server = data->server;
7259 nfs40_sequence_done(task, &data->res.seq_res);
7261 switch (task->tk_status) {
7263 renew_lease(server, data->timestamp);
7265 case -NFS4ERR_STALE_CLIENTID:
7266 case -NFS4ERR_EXPIRED:
7267 nfs4_schedule_lease_recovery(server->nfs_client);
7269 case -NFS4ERR_LEASE_MOVED:
7270 case -NFS4ERR_DELAY:
7271 if (nfs4_async_handle_error(task, server,
7272 NULL, NULL) == -EAGAIN)
7273 rpc_restart_call_prepare(task);
7277 static void nfs4_release_lockowner_release(void *calldata)
7279 struct nfs_release_lockowner_data *data = calldata;
7280 nfs4_free_lock_state(data->server, data->lsp);
7284 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7285 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7286 .rpc_call_done = nfs4_release_lockowner_done,
7287 .rpc_release = nfs4_release_lockowner_release,
7291 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7293 struct nfs_release_lockowner_data *data;
7294 struct rpc_message msg = {
7295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7298 if (server->nfs_client->cl_mvops->minor_version != 0)
7301 data = kmalloc(sizeof(*data), GFP_NOFS);
7305 data->server = server;
7306 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7307 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7308 data->args.lock_owner.s_dev = server->s_dev;
7310 msg.rpc_argp = &data->args;
7311 msg.rpc_resp = &data->res;
7312 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7313 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7316 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7318 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7319 struct dentry *unused, struct inode *inode,
7320 const char *key, const void *buf,
7321 size_t buflen, int flags)
7323 return nfs4_proc_set_acl(inode, buf, buflen);
7326 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7327 struct dentry *unused, struct inode *inode,
7328 const char *key, void *buf, size_t buflen)
7330 return nfs4_proc_get_acl(inode, buf, buflen);
7333 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7335 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7338 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7340 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7341 struct dentry *unused, struct inode *inode,
7342 const char *key, const void *buf,
7343 size_t buflen, int flags)
7345 if (security_ismaclabel(key))
7346 return nfs4_set_security_label(inode, buf, buflen);
7351 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7352 struct dentry *unused, struct inode *inode,
7353 const char *key, void *buf, size_t buflen)
7355 if (security_ismaclabel(key))
7356 return nfs4_get_security_label(inode, buf, buflen);
7361 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7365 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7366 len = security_inode_listsecurity(inode, list, list_len);
7367 if (list_len && len > list_len)
7373 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7374 .prefix = XATTR_SECURITY_PREFIX,
7375 .get = nfs4_xattr_get_nfs4_label,
7376 .set = nfs4_xattr_set_nfs4_label,
7382 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7390 * nfs_fhget will use either the mounted_on_fileid or the fileid
7392 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7394 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7395 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7396 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7397 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7400 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7401 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7402 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7406 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7407 const struct qstr *name,
7408 struct nfs4_fs_locations *fs_locations,
7411 struct nfs_server *server = NFS_SERVER(dir);
7413 struct nfs4_fs_locations_arg args = {
7414 .dir_fh = NFS_FH(dir),
7419 struct nfs4_fs_locations_res res = {
7420 .fs_locations = fs_locations,
7422 struct rpc_message msg = {
7423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7429 dprintk("%s: start\n", __func__);
7431 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7432 bitmask[1] = nfs4_fattr_bitmap[1];
7434 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7435 * is not supported */
7436 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7437 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7439 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7441 nfs_fattr_init(&fs_locations->fattr);
7442 fs_locations->server = server;
7443 fs_locations->nlocations = 0;
7444 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7445 dprintk("%s: returned status = %d\n", __func__, status);
7449 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7450 const struct qstr *name,
7451 struct nfs4_fs_locations *fs_locations,
7454 struct nfs4_exception exception = {
7455 .interruptible = true,
7459 err = _nfs4_proc_fs_locations(client, dir, name,
7460 fs_locations, page);
7461 trace_nfs4_get_fs_locations(dir, name, err);
7462 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7464 } while (exception.retry);
7469 * This operation also signals the server that this client is
7470 * performing migration recovery. The server can stop returning
7471 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7472 * appended to this compound to identify the client ID which is
7473 * performing recovery.
7475 static int _nfs40_proc_get_locations(struct inode *inode,
7476 struct nfs4_fs_locations *locations,
7477 struct page *page, const struct cred *cred)
7479 struct nfs_server *server = NFS_SERVER(inode);
7480 struct rpc_clnt *clnt = server->client;
7482 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7484 struct nfs4_fs_locations_arg args = {
7485 .clientid = server->nfs_client->cl_clientid,
7486 .fh = NFS_FH(inode),
7489 .migration = 1, /* skip LOOKUP */
7490 .renew = 1, /* append RENEW */
7492 struct nfs4_fs_locations_res res = {
7493 .fs_locations = locations,
7497 struct rpc_message msg = {
7498 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7503 unsigned long now = jiffies;
7506 nfs_fattr_init(&locations->fattr);
7507 locations->server = server;
7508 locations->nlocations = 0;
7510 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7511 status = nfs4_call_sync_sequence(clnt, server, &msg,
7512 &args.seq_args, &res.seq_res);
7516 renew_lease(server, now);
7520 #ifdef CONFIG_NFS_V4_1
7523 * This operation also signals the server that this client is
7524 * performing migration recovery. The server can stop asserting
7525 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7526 * performing this operation is identified in the SEQUENCE
7527 * operation in this compound.
7529 * When the client supports GETATTR(fs_locations_info), it can
7530 * be plumbed in here.
7532 static int _nfs41_proc_get_locations(struct inode *inode,
7533 struct nfs4_fs_locations *locations,
7534 struct page *page, const struct cred *cred)
7536 struct nfs_server *server = NFS_SERVER(inode);
7537 struct rpc_clnt *clnt = server->client;
7539 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7541 struct nfs4_fs_locations_arg args = {
7542 .fh = NFS_FH(inode),
7545 .migration = 1, /* skip LOOKUP */
7547 struct nfs4_fs_locations_res res = {
7548 .fs_locations = locations,
7551 struct rpc_message msg = {
7552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7559 nfs_fattr_init(&locations->fattr);
7560 locations->server = server;
7561 locations->nlocations = 0;
7563 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7564 status = nfs4_call_sync_sequence(clnt, server, &msg,
7565 &args.seq_args, &res.seq_res);
7566 if (status == NFS4_OK &&
7567 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7568 status = -NFS4ERR_LEASE_MOVED;
7572 #endif /* CONFIG_NFS_V4_1 */
7575 * nfs4_proc_get_locations - discover locations for a migrated FSID
7576 * @inode: inode on FSID that is migrating
7577 * @locations: result of query
7579 * @cred: credential to use for this operation
7581 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7582 * operation failed, or a negative errno if a local error occurred.
7584 * On success, "locations" is filled in, but if the server has
7585 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7588 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7589 * from this client that require migration recovery.
7591 int nfs4_proc_get_locations(struct inode *inode,
7592 struct nfs4_fs_locations *locations,
7593 struct page *page, const struct cred *cred)
7595 struct nfs_server *server = NFS_SERVER(inode);
7596 struct nfs_client *clp = server->nfs_client;
7597 const struct nfs4_mig_recovery_ops *ops =
7598 clp->cl_mvops->mig_recovery_ops;
7599 struct nfs4_exception exception = {
7600 .interruptible = true,
7604 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7605 (unsigned long long)server->fsid.major,
7606 (unsigned long long)server->fsid.minor,
7608 nfs_display_fhandle(NFS_FH(inode), __func__);
7611 status = ops->get_locations(inode, locations, page, cred);
7612 if (status != -NFS4ERR_DELAY)
7614 nfs4_handle_exception(server, status, &exception);
7615 } while (exception.retry);
7620 * This operation also signals the server that this client is
7621 * performing "lease moved" recovery. The server can stop
7622 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7623 * is appended to this compound to identify the client ID which is
7624 * performing recovery.
7626 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7628 struct nfs_server *server = NFS_SERVER(inode);
7629 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7630 struct rpc_clnt *clnt = server->client;
7631 struct nfs4_fsid_present_arg args = {
7632 .fh = NFS_FH(inode),
7633 .clientid = clp->cl_clientid,
7634 .renew = 1, /* append RENEW */
7636 struct nfs4_fsid_present_res res = {
7639 struct rpc_message msg = {
7640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7645 unsigned long now = jiffies;
7648 res.fh = nfs_alloc_fhandle();
7652 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7653 status = nfs4_call_sync_sequence(clnt, server, &msg,
7654 &args.seq_args, &res.seq_res);
7655 nfs_free_fhandle(res.fh);
7659 do_renew_lease(clp, now);
7663 #ifdef CONFIG_NFS_V4_1
7666 * This operation also signals the server that this client is
7667 * performing "lease moved" recovery. The server can stop asserting
7668 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7669 * this operation is identified in the SEQUENCE operation in this
7672 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7674 struct nfs_server *server = NFS_SERVER(inode);
7675 struct rpc_clnt *clnt = server->client;
7676 struct nfs4_fsid_present_arg args = {
7677 .fh = NFS_FH(inode),
7679 struct nfs4_fsid_present_res res = {
7681 struct rpc_message msg = {
7682 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7689 res.fh = nfs_alloc_fhandle();
7693 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7694 status = nfs4_call_sync_sequence(clnt, server, &msg,
7695 &args.seq_args, &res.seq_res);
7696 nfs_free_fhandle(res.fh);
7697 if (status == NFS4_OK &&
7698 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7699 status = -NFS4ERR_LEASE_MOVED;
7703 #endif /* CONFIG_NFS_V4_1 */
7706 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7707 * @inode: inode on FSID to check
7708 * @cred: credential to use for this operation
7710 * Server indicates whether the FSID is present, moved, or not
7711 * recognized. This operation is necessary to clear a LEASE_MOVED
7712 * condition for this client ID.
7714 * Returns NFS4_OK if the FSID is present on this server,
7715 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7716 * NFS4ERR code if some error occurred on the server, or a
7717 * negative errno if a local failure occurred.
7719 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
7721 struct nfs_server *server = NFS_SERVER(inode);
7722 struct nfs_client *clp = server->nfs_client;
7723 const struct nfs4_mig_recovery_ops *ops =
7724 clp->cl_mvops->mig_recovery_ops;
7725 struct nfs4_exception exception = {
7726 .interruptible = true,
7730 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7731 (unsigned long long)server->fsid.major,
7732 (unsigned long long)server->fsid.minor,
7734 nfs_display_fhandle(NFS_FH(inode), __func__);
7737 status = ops->fsid_present(inode, cred);
7738 if (status != -NFS4ERR_DELAY)
7740 nfs4_handle_exception(server, status, &exception);
7741 } while (exception.retry);
7746 * If 'use_integrity' is true and the state managment nfs_client
7747 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7748 * and the machine credential as per RFC3530bis and RFC5661 Security
7749 * Considerations sections. Otherwise, just use the user cred with the
7750 * filesystem's rpc_client.
7752 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7755 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7756 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
7757 struct nfs4_secinfo_arg args = {
7758 .dir_fh = NFS_FH(dir),
7761 struct nfs4_secinfo_res res = {
7764 struct rpc_message msg = {
7765 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7769 struct nfs4_call_sync_data data = {
7770 .seq_server = NFS_SERVER(dir),
7771 .seq_args = &args.seq_args,
7772 .seq_res = &res.seq_res,
7774 struct rpc_task_setup task_setup = {
7776 .rpc_message = &msg,
7777 .callback_ops = clp->cl_mvops->call_sync_ops,
7778 .callback_data = &data,
7779 .flags = RPC_TASK_NO_ROUND_ROBIN,
7781 const struct cred *cred = NULL;
7783 if (use_integrity) {
7784 clnt = clp->cl_rpcclient;
7785 task_setup.rpc_client = clnt;
7787 cred = nfs4_get_clid_cred(clp);
7788 msg.rpc_cred = cred;
7791 dprintk("NFS call secinfo %s\n", name->name);
7793 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7794 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
7795 status = nfs4_call_sync_custom(&task_setup);
7797 dprintk("NFS reply secinfo: %d\n", status);
7803 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7804 struct nfs4_secinfo_flavors *flavors)
7806 struct nfs4_exception exception = {
7807 .interruptible = true,
7811 err = -NFS4ERR_WRONGSEC;
7813 /* try to use integrity protection with machine cred */
7814 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7815 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7818 * if unable to use integrity protection, or SECINFO with
7819 * integrity protection returns NFS4ERR_WRONGSEC (which is
7820 * disallowed by spec, but exists in deployed servers) use
7821 * the current filesystem's rpc_client and the user cred.
7823 if (err == -NFS4ERR_WRONGSEC)
7824 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7826 trace_nfs4_secinfo(dir, name, err);
7827 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7829 } while (exception.retry);
7833 #ifdef CONFIG_NFS_V4_1
7835 * Check the exchange flags returned by the server for invalid flags, having
7836 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7839 static int nfs4_check_cl_exchange_flags(u32 flags)
7841 if (flags & ~EXCHGID4_FLAG_MASK_R)
7843 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7844 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7846 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7850 return -NFS4ERR_INVAL;
7854 nfs41_same_server_scope(struct nfs41_server_scope *a,
7855 struct nfs41_server_scope *b)
7857 if (a->server_scope_sz != b->server_scope_sz)
7859 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7863 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7865 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
7866 struct nfs_client *clp = args->client;
7868 switch (task->tk_status) {
7869 case -NFS4ERR_BADSESSION:
7870 case -NFS4ERR_DEADSESSION:
7871 nfs4_schedule_session_recovery(clp->cl_session,
7876 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7877 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7881 * nfs4_proc_bind_one_conn_to_session()
7883 * The 4.1 client currently uses the same TCP connection for the
7884 * fore and backchannel.
7887 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7888 struct rpc_xprt *xprt,
7889 struct nfs_client *clp,
7890 const struct cred *cred)
7893 struct nfs41_bind_conn_to_session_args args = {
7895 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7897 struct nfs41_bind_conn_to_session_res res;
7898 struct rpc_message msg = {
7900 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7905 struct rpc_task_setup task_setup_data = {
7908 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7909 .rpc_message = &msg,
7910 .flags = RPC_TASK_TIMEOUT,
7912 struct rpc_task *task;
7914 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7915 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7916 args.dir = NFS4_CDFC4_FORE;
7918 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7919 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7920 args.dir = NFS4_CDFC4_FORE;
7922 task = rpc_run_task(&task_setup_data);
7923 if (!IS_ERR(task)) {
7924 status = task->tk_status;
7927 status = PTR_ERR(task);
7928 trace_nfs4_bind_conn_to_session(clp, status);
7930 if (memcmp(res.sessionid.data,
7931 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7932 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7935 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7936 dprintk("NFS: %s: Unexpected direction from server\n",
7940 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7941 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7950 struct rpc_bind_conn_calldata {
7951 struct nfs_client *clp;
7952 const struct cred *cred;
7956 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7957 struct rpc_xprt *xprt,
7960 struct rpc_bind_conn_calldata *p = calldata;
7962 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7965 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
7967 struct rpc_bind_conn_calldata data = {
7971 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7972 nfs4_proc_bind_conn_to_session_callback, &data);
7976 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7977 * and operations we'd like to see to enable certain features in the allow map
7979 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7980 .how = SP4_MACH_CRED,
7981 .enforce.u.words = {
7982 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7983 1 << (OP_EXCHANGE_ID - 32) |
7984 1 << (OP_CREATE_SESSION - 32) |
7985 1 << (OP_DESTROY_SESSION - 32) |
7986 1 << (OP_DESTROY_CLIENTID - 32)
7989 [0] = 1 << (OP_CLOSE) |
7990 1 << (OP_OPEN_DOWNGRADE) |
7992 1 << (OP_DELEGRETURN) |
7994 [1] = 1 << (OP_SECINFO - 32) |
7995 1 << (OP_SECINFO_NO_NAME - 32) |
7996 1 << (OP_LAYOUTRETURN - 32) |
7997 1 << (OP_TEST_STATEID - 32) |
7998 1 << (OP_FREE_STATEID - 32) |
7999 1 << (OP_WRITE - 32)
8004 * Select the state protection mode for client `clp' given the server results
8005 * from exchange_id in `sp'.
8007 * Returns 0 on success, negative errno otherwise.
8009 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8010 struct nfs41_state_protection *sp)
8012 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8013 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8014 1 << (OP_EXCHANGE_ID - 32) |
8015 1 << (OP_CREATE_SESSION - 32) |
8016 1 << (OP_DESTROY_SESSION - 32) |
8017 1 << (OP_DESTROY_CLIENTID - 32)
8019 unsigned long flags = 0;
8023 if (sp->how == SP4_MACH_CRED) {
8024 /* Print state protect result */
8025 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8026 for (i = 0; i <= LAST_NFS4_OP; i++) {
8027 if (test_bit(i, sp->enforce.u.longs))
8028 dfprintk(MOUNT, " enforce op %d\n", i);
8029 if (test_bit(i, sp->allow.u.longs))
8030 dfprintk(MOUNT, " allow op %d\n", i);
8033 /* make sure nothing is on enforce list that isn't supported */
8034 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8035 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8036 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8043 * Minimal mode - state operations are allowed to use machine
8044 * credential. Note this already happens by default, so the
8045 * client doesn't have to do anything more than the negotiation.
8047 * NOTE: we don't care if EXCHANGE_ID is in the list -
8048 * we're already using the machine cred for exchange_id
8049 * and will never use a different cred.
8051 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8052 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8053 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8054 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8055 dfprintk(MOUNT, "sp4_mach_cred:\n");
8056 dfprintk(MOUNT, " minimal mode enabled\n");
8057 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8059 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8064 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8065 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8066 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8067 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8068 dfprintk(MOUNT, " cleanup mode enabled\n");
8069 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8072 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8073 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8074 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8077 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8078 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8079 dfprintk(MOUNT, " secinfo mode enabled\n");
8080 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8083 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8084 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8085 dfprintk(MOUNT, " stateid mode enabled\n");
8086 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8089 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8090 dfprintk(MOUNT, " write mode enabled\n");
8091 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8094 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8095 dfprintk(MOUNT, " commit mode enabled\n");
8096 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8100 clp->cl_sp4_flags = flags;
8104 struct nfs41_exchange_id_data {
8105 struct nfs41_exchange_id_res res;
8106 struct nfs41_exchange_id_args args;
8109 static void nfs4_exchange_id_release(void *data)
8111 struct nfs41_exchange_id_data *cdata =
8112 (struct nfs41_exchange_id_data *)data;
8114 nfs_put_client(cdata->args.client);
8115 kfree(cdata->res.impl_id);
8116 kfree(cdata->res.server_scope);
8117 kfree(cdata->res.server_owner);
8121 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8122 .rpc_release = nfs4_exchange_id_release,
8126 * _nfs4_proc_exchange_id()
8128 * Wrapper for EXCHANGE_ID operation.
8130 static struct rpc_task *
8131 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8132 u32 sp4_how, struct rpc_xprt *xprt)
8134 struct rpc_message msg = {
8135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8138 struct rpc_task_setup task_setup_data = {
8139 .rpc_client = clp->cl_rpcclient,
8140 .callback_ops = &nfs4_exchange_id_call_ops,
8141 .rpc_message = &msg,
8142 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8144 struct nfs41_exchange_id_data *calldata;
8147 if (!refcount_inc_not_zero(&clp->cl_count))
8148 return ERR_PTR(-EIO);
8151 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8155 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8157 status = nfs4_init_uniform_client_string(clp);
8161 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8164 if (unlikely(calldata->res.server_owner == NULL))
8167 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8169 if (unlikely(calldata->res.server_scope == NULL))
8170 goto out_server_owner;
8172 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8173 if (unlikely(calldata->res.impl_id == NULL))
8174 goto out_server_scope;
8178 calldata->args.state_protect.how = SP4_NONE;
8182 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8192 task_setup_data.rpc_xprt = xprt;
8193 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8194 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8195 sizeof(calldata->args.verifier.data));
8197 calldata->args.client = clp;
8198 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8199 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8200 #ifdef CONFIG_NFS_V4_1_MIGRATION
8201 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8203 msg.rpc_argp = &calldata->args;
8204 msg.rpc_resp = &calldata->res;
8205 task_setup_data.callback_data = calldata;
8207 return rpc_run_task(&task_setup_data);
8210 kfree(calldata->res.impl_id);
8212 kfree(calldata->res.server_scope);
8214 kfree(calldata->res.server_owner);
8218 nfs_put_client(clp);
8219 return ERR_PTR(status);
8223 * _nfs4_proc_exchange_id()
8225 * Wrapper for EXCHANGE_ID operation.
8227 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8230 struct rpc_task *task;
8231 struct nfs41_exchange_id_args *argp;
8232 struct nfs41_exchange_id_res *resp;
8235 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8237 return PTR_ERR(task);
8239 argp = task->tk_msg.rpc_argp;
8240 resp = task->tk_msg.rpc_resp;
8241 status = task->tk_status;
8245 status = nfs4_check_cl_exchange_flags(resp->flags);
8249 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8253 clp->cl_clientid = resp->clientid;
8254 clp->cl_exchange_flags = resp->flags;
8255 clp->cl_seqid = resp->seqid;
8256 /* Client ID is not confirmed */
8257 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8258 clear_bit(NFS4_SESSION_ESTABLISHED,
8259 &clp->cl_session->session_state);
8261 if (clp->cl_serverscope != NULL &&
8262 !nfs41_same_server_scope(clp->cl_serverscope,
8263 resp->server_scope)) {
8264 dprintk("%s: server_scope mismatch detected\n",
8266 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8269 swap(clp->cl_serverowner, resp->server_owner);
8270 swap(clp->cl_serverscope, resp->server_scope);
8271 swap(clp->cl_implid, resp->impl_id);
8273 /* Save the EXCHANGE_ID verifier session trunk tests */
8274 memcpy(clp->cl_confirm.data, argp->verifier.data,
8275 sizeof(clp->cl_confirm.data));
8277 trace_nfs4_exchange_id(clp, status);
8283 * nfs4_proc_exchange_id()
8285 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8287 * Since the clientid has expired, all compounds using sessions
8288 * associated with the stale clientid will be returning
8289 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8290 * be in some phase of session reset.
8292 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8294 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8296 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8299 /* try SP4_MACH_CRED if krb5i/p */
8300 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8301 authflavor == RPC_AUTH_GSS_KRB5P) {
8302 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8308 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8312 * nfs4_test_session_trunk
8314 * This is an add_xprt_test() test function called from
8315 * rpc_clnt_setup_test_and_add_xprt.
8317 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8318 * and is dereferrenced in nfs4_exchange_id_release
8320 * Upon success, add the new transport to the rpc_clnt
8322 * @clnt: struct rpc_clnt to get new transport
8323 * @xprt: the rpc_xprt to test
8324 * @data: call data for _nfs4_proc_exchange_id.
8326 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8329 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8330 struct rpc_task *task;
8335 dprintk("--> %s try %s\n", __func__,
8336 xprt->address_strings[RPC_DISPLAY_ADDR]);
8338 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8340 /* Test connection for session trunking. Async exchange_id call */
8341 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8345 status = task->tk_status;
8347 status = nfs4_detect_session_trunking(adata->clp,
8348 task->tk_msg.rpc_resp, xprt);
8351 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8355 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8357 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8358 const struct cred *cred)
8360 struct rpc_message msg = {
8361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8367 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8368 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8369 trace_nfs4_destroy_clientid(clp, status);
8371 dprintk("NFS: Got error %d from the server %s on "
8372 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8376 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8377 const struct cred *cred)
8382 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8383 ret = _nfs4_proc_destroy_clientid(clp, cred);
8385 case -NFS4ERR_DELAY:
8386 case -NFS4ERR_CLIENTID_BUSY:
8396 int nfs4_destroy_clientid(struct nfs_client *clp)
8398 const struct cred *cred;
8401 if (clp->cl_mvops->minor_version < 1)
8403 if (clp->cl_exchange_flags == 0)
8405 if (clp->cl_preserve_clid)
8407 cred = nfs4_get_clid_cred(clp);
8408 ret = nfs4_proc_destroy_clientid(clp, cred);
8412 case -NFS4ERR_STALE_CLIENTID:
8413 clp->cl_exchange_flags = 0;
8419 #endif /* CONFIG_NFS_V4_1 */
8421 struct nfs4_get_lease_time_data {
8422 struct nfs4_get_lease_time_args *args;
8423 struct nfs4_get_lease_time_res *res;
8424 struct nfs_client *clp;
8427 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8430 struct nfs4_get_lease_time_data *data =
8431 (struct nfs4_get_lease_time_data *)calldata;
8433 dprintk("--> %s\n", __func__);
8434 /* just setup sequence, do not trigger session recovery
8435 since we're invoked within one */
8436 nfs4_setup_sequence(data->clp,
8437 &data->args->la_seq_args,
8438 &data->res->lr_seq_res,
8440 dprintk("<-- %s\n", __func__);
8444 * Called from nfs4_state_manager thread for session setup, so don't recover
8445 * from sequence operation or clientid errors.
8447 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8449 struct nfs4_get_lease_time_data *data =
8450 (struct nfs4_get_lease_time_data *)calldata;
8452 dprintk("--> %s\n", __func__);
8453 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8455 switch (task->tk_status) {
8456 case -NFS4ERR_DELAY:
8457 case -NFS4ERR_GRACE:
8458 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8459 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8460 task->tk_status = 0;
8462 case -NFS4ERR_RETRY_UNCACHED_REP:
8463 rpc_restart_call_prepare(task);
8466 dprintk("<-- %s\n", __func__);
8469 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8470 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8471 .rpc_call_done = nfs4_get_lease_time_done,
8474 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8476 struct nfs4_get_lease_time_args args;
8477 struct nfs4_get_lease_time_res res = {
8478 .lr_fsinfo = fsinfo,
8480 struct nfs4_get_lease_time_data data = {
8485 struct rpc_message msg = {
8486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8490 struct rpc_task_setup task_setup = {
8491 .rpc_client = clp->cl_rpcclient,
8492 .rpc_message = &msg,
8493 .callback_ops = &nfs4_get_lease_time_ops,
8494 .callback_data = &data,
8495 .flags = RPC_TASK_TIMEOUT,
8498 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8499 return nfs4_call_sync_custom(&task_setup);
8502 #ifdef CONFIG_NFS_V4_1
8505 * Initialize the values to be used by the client in CREATE_SESSION
8506 * If nfs4_init_session set the fore channel request and response sizes,
8509 * Set the back channel max_resp_sz_cached to zero to force the client to
8510 * always set csa_cachethis to FALSE because the current implementation
8511 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8513 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8514 struct rpc_clnt *clnt)
8516 unsigned int max_rqst_sz, max_resp_sz;
8517 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8518 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8520 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8521 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8523 /* Fore channel attributes */
8524 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8525 args->fc_attrs.max_resp_sz = max_resp_sz;
8526 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8527 args->fc_attrs.max_reqs = max_session_slots;
8529 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8530 "max_ops=%u max_reqs=%u\n",
8532 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8533 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8535 /* Back channel attributes */
8536 args->bc_attrs.max_rqst_sz = max_bc_payload;
8537 args->bc_attrs.max_resp_sz = max_bc_payload;
8538 args->bc_attrs.max_resp_sz_cached = 0;
8539 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8540 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8541 if (args->bc_attrs.max_reqs > max_bc_slots)
8542 args->bc_attrs.max_reqs = max_bc_slots;
8544 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8545 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8547 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8548 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8549 args->bc_attrs.max_reqs);
8552 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8553 struct nfs41_create_session_res *res)
8555 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8556 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8558 if (rcvd->max_resp_sz > sent->max_resp_sz)
8561 * Our requested max_ops is the minimum we need; we're not
8562 * prepared to break up compounds into smaller pieces than that.
8563 * So, no point even trying to continue if the server won't
8566 if (rcvd->max_ops < sent->max_ops)
8568 if (rcvd->max_reqs == 0)
8570 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8571 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8575 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8576 struct nfs41_create_session_res *res)
8578 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8579 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8581 if (!(res->flags & SESSION4_BACK_CHAN))
8583 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8585 if (rcvd->max_resp_sz < sent->max_resp_sz)
8587 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8589 if (rcvd->max_ops > sent->max_ops)
8591 if (rcvd->max_reqs > sent->max_reqs)
8597 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8598 struct nfs41_create_session_res *res)
8602 ret = nfs4_verify_fore_channel_attrs(args, res);
8605 return nfs4_verify_back_channel_attrs(args, res);
8608 static void nfs4_update_session(struct nfs4_session *session,
8609 struct nfs41_create_session_res *res)
8611 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8612 /* Mark client id and session as being confirmed */
8613 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8614 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8615 session->flags = res->flags;
8616 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8617 if (res->flags & SESSION4_BACK_CHAN)
8618 memcpy(&session->bc_attrs, &res->bc_attrs,
8619 sizeof(session->bc_attrs));
8622 static int _nfs4_proc_create_session(struct nfs_client *clp,
8623 const struct cred *cred)
8625 struct nfs4_session *session = clp->cl_session;
8626 struct nfs41_create_session_args args = {
8628 .clientid = clp->cl_clientid,
8629 .seqid = clp->cl_seqid,
8630 .cb_program = NFS4_CALLBACK,
8632 struct nfs41_create_session_res res;
8634 struct rpc_message msg = {
8635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8642 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8643 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8645 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8646 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8647 trace_nfs4_create_session(clp, status);
8650 case -NFS4ERR_STALE_CLIENTID:
8651 case -NFS4ERR_DELAY:
8660 /* Verify the session's negotiated channel_attrs values */
8661 status = nfs4_verify_channel_attrs(&args, &res);
8662 /* Increment the clientid slot sequence id */
8665 nfs4_update_session(session, &res);
8672 * Issues a CREATE_SESSION operation to the server.
8673 * It is the responsibility of the caller to verify the session is
8674 * expired before calling this routine.
8676 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8680 struct nfs4_session *session = clp->cl_session;
8682 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8684 status = _nfs4_proc_create_session(clp, cred);
8688 /* Init or reset the session slot tables */
8689 status = nfs4_setup_session_slot_tables(session);
8690 dprintk("slot table setup returned %d\n", status);
8694 ptr = (unsigned *)&session->sess_id.data[0];
8695 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8696 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8698 dprintk("<-- %s\n", __func__);
8703 * Issue the over-the-wire RPC DESTROY_SESSION.
8704 * The caller must serialize access to this routine.
8706 int nfs4_proc_destroy_session(struct nfs4_session *session,
8707 const struct cred *cred)
8709 struct rpc_message msg = {
8710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8711 .rpc_argp = session,
8716 dprintk("--> nfs4_proc_destroy_session\n");
8718 /* session is still being setup */
8719 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8722 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8723 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8724 trace_nfs4_destroy_session(session->clp, status);
8727 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8728 "Session has been destroyed regardless...\n", status);
8730 dprintk("<-- nfs4_proc_destroy_session\n");
8735 * Renew the cl_session lease.
8737 struct nfs4_sequence_data {
8738 struct nfs_client *clp;
8739 struct nfs4_sequence_args args;
8740 struct nfs4_sequence_res res;
8743 static void nfs41_sequence_release(void *data)
8745 struct nfs4_sequence_data *calldata = data;
8746 struct nfs_client *clp = calldata->clp;
8748 if (refcount_read(&clp->cl_count) > 1)
8749 nfs4_schedule_state_renewal(clp);
8750 nfs_put_client(clp);
8754 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8756 switch(task->tk_status) {
8757 case -NFS4ERR_DELAY:
8758 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8761 nfs4_schedule_lease_recovery(clp);
8766 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8768 struct nfs4_sequence_data *calldata = data;
8769 struct nfs_client *clp = calldata->clp;
8771 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8774 trace_nfs4_sequence(clp, task->tk_status);
8775 if (task->tk_status < 0) {
8776 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8777 if (refcount_read(&clp->cl_count) == 1)
8780 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8781 rpc_restart_call_prepare(task);
8785 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8787 dprintk("<-- %s\n", __func__);
8790 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8792 struct nfs4_sequence_data *calldata = data;
8793 struct nfs_client *clp = calldata->clp;
8794 struct nfs4_sequence_args *args;
8795 struct nfs4_sequence_res *res;
8797 args = task->tk_msg.rpc_argp;
8798 res = task->tk_msg.rpc_resp;
8800 nfs4_setup_sequence(clp, args, res, task);
8803 static const struct rpc_call_ops nfs41_sequence_ops = {
8804 .rpc_call_done = nfs41_sequence_call_done,
8805 .rpc_call_prepare = nfs41_sequence_prepare,
8806 .rpc_release = nfs41_sequence_release,
8809 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8810 const struct cred *cred,
8811 struct nfs4_slot *slot,
8814 struct nfs4_sequence_data *calldata;
8815 struct rpc_message msg = {
8816 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8819 struct rpc_task_setup task_setup_data = {
8820 .rpc_client = clp->cl_rpcclient,
8821 .rpc_message = &msg,
8822 .callback_ops = &nfs41_sequence_ops,
8823 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8825 struct rpc_task *ret;
8827 ret = ERR_PTR(-EIO);
8828 if (!refcount_inc_not_zero(&clp->cl_count))
8831 ret = ERR_PTR(-ENOMEM);
8832 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8833 if (calldata == NULL)
8835 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
8836 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8837 msg.rpc_argp = &calldata->args;
8838 msg.rpc_resp = &calldata->res;
8839 calldata->clp = clp;
8840 task_setup_data.callback_data = calldata;
8842 ret = rpc_run_task(&task_setup_data);
8847 nfs_put_client(clp);
8849 nfs41_release_slot(slot);
8853 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
8855 struct rpc_task *task;
8858 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8860 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8862 ret = PTR_ERR(task);
8864 rpc_put_task_async(task);
8865 dprintk("<-- %s status=%d\n", __func__, ret);
8869 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
8871 struct rpc_task *task;
8874 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8876 ret = PTR_ERR(task);
8879 ret = rpc_wait_for_completion_task(task);
8881 ret = task->tk_status;
8884 dprintk("<-- %s status=%d\n", __func__, ret);
8888 struct nfs4_reclaim_complete_data {
8889 struct nfs_client *clp;
8890 struct nfs41_reclaim_complete_args arg;
8891 struct nfs41_reclaim_complete_res res;
8894 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8896 struct nfs4_reclaim_complete_data *calldata = data;
8898 nfs4_setup_sequence(calldata->clp,
8899 &calldata->arg.seq_args,
8900 &calldata->res.seq_res,
8904 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8906 switch(task->tk_status) {
8908 wake_up_all(&clp->cl_lock_waitq);
8910 case -NFS4ERR_COMPLETE_ALREADY:
8911 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8913 case -NFS4ERR_DELAY:
8914 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8916 case -NFS4ERR_RETRY_UNCACHED_REP:
8918 case -NFS4ERR_BADSESSION:
8919 case -NFS4ERR_DEADSESSION:
8920 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8923 nfs4_schedule_lease_recovery(clp);
8928 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8930 struct nfs4_reclaim_complete_data *calldata = data;
8931 struct nfs_client *clp = calldata->clp;
8932 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8934 dprintk("--> %s\n", __func__);
8935 if (!nfs41_sequence_done(task, res))
8938 trace_nfs4_reclaim_complete(clp, task->tk_status);
8939 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8940 rpc_restart_call_prepare(task);
8943 dprintk("<-- %s\n", __func__);
8946 static void nfs4_free_reclaim_complete_data(void *data)
8948 struct nfs4_reclaim_complete_data *calldata = data;
8953 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8954 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8955 .rpc_call_done = nfs4_reclaim_complete_done,
8956 .rpc_release = nfs4_free_reclaim_complete_data,
8960 * Issue a global reclaim complete.
8962 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8963 const struct cred *cred)
8965 struct nfs4_reclaim_complete_data *calldata;
8966 struct rpc_message msg = {
8967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8970 struct rpc_task_setup task_setup_data = {
8971 .rpc_client = clp->cl_rpcclient,
8972 .rpc_message = &msg,
8973 .callback_ops = &nfs4_reclaim_complete_call_ops,
8974 .flags = RPC_TASK_NO_ROUND_ROBIN,
8976 int status = -ENOMEM;
8978 dprintk("--> %s\n", __func__);
8979 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8980 if (calldata == NULL)
8982 calldata->clp = clp;
8983 calldata->arg.one_fs = 0;
8985 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
8986 msg.rpc_argp = &calldata->arg;
8987 msg.rpc_resp = &calldata->res;
8988 task_setup_data.callback_data = calldata;
8989 status = nfs4_call_sync_custom(&task_setup_data);
8991 dprintk("<-- %s status=%d\n", __func__, status);
8996 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8998 struct nfs4_layoutget *lgp = calldata;
8999 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9001 dprintk("--> %s\n", __func__);
9002 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9003 &lgp->res.seq_res, task);
9004 dprintk("<-- %s\n", __func__);
9007 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9009 struct nfs4_layoutget *lgp = calldata;
9011 dprintk("--> %s\n", __func__);
9012 nfs41_sequence_process(task, &lgp->res.seq_res);
9013 dprintk("<-- %s\n", __func__);
9017 nfs4_layoutget_handle_exception(struct rpc_task *task,
9018 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9020 struct inode *inode = lgp->args.inode;
9021 struct nfs_server *server = NFS_SERVER(inode);
9022 struct pnfs_layout_hdr *lo;
9023 int nfs4err = task->tk_status;
9024 int err, status = 0;
9027 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9029 nfs4_sequence_free_slot(&lgp->res.seq_res);
9036 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9037 * on the file. set tk_status to -ENODATA to tell upper layer to
9040 case -NFS4ERR_LAYOUTUNAVAILABLE:
9044 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9045 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9047 case -NFS4ERR_BADLAYOUT:
9048 status = -EOVERFLOW;
9051 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9052 * (or clients) writing to the same RAID stripe except when
9053 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9055 * Treat it like we would RECALLCONFLICT -- we retry for a little
9056 * while, and then eventually give up.
9058 case -NFS4ERR_LAYOUTTRYLATER:
9059 if (lgp->args.minlength == 0) {
9060 status = -EOVERFLOW;
9065 case -NFS4ERR_RECALLCONFLICT:
9066 status = -ERECALLCONFLICT;
9068 case -NFS4ERR_DELEG_REVOKED:
9069 case -NFS4ERR_ADMIN_REVOKED:
9070 case -NFS4ERR_EXPIRED:
9071 case -NFS4ERR_BAD_STATEID:
9072 exception->timeout = 0;
9073 spin_lock(&inode->i_lock);
9074 lo = NFS_I(inode)->layout;
9075 /* If the open stateid was bad, then recover it. */
9076 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9077 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9078 spin_unlock(&inode->i_lock);
9079 exception->state = lgp->args.ctx->state;
9080 exception->stateid = &lgp->args.stateid;
9085 * Mark the bad layout state as invalid, then retry
9087 pnfs_mark_layout_stateid_invalid(lo, &head);
9088 spin_unlock(&inode->i_lock);
9089 nfs_commit_inode(inode, 0);
9090 pnfs_free_lseg_list(&head);
9095 err = nfs4_handle_exception(server, nfs4err, exception);
9097 if (exception->retry)
9103 dprintk("<-- %s\n", __func__);
9107 size_t max_response_pages(struct nfs_server *server)
9109 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9110 return nfs_page_array_len(0, max_resp_sz);
9113 static void nfs4_layoutget_release(void *calldata)
9115 struct nfs4_layoutget *lgp = calldata;
9117 dprintk("--> %s\n", __func__);
9118 nfs4_sequence_free_slot(&lgp->res.seq_res);
9119 pnfs_layoutget_free(lgp);
9120 dprintk("<-- %s\n", __func__);
9123 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9124 .rpc_call_prepare = nfs4_layoutget_prepare,
9125 .rpc_call_done = nfs4_layoutget_done,
9126 .rpc_release = nfs4_layoutget_release,
9129 struct pnfs_layout_segment *
9130 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9132 struct inode *inode = lgp->args.inode;
9133 struct nfs_server *server = NFS_SERVER(inode);
9134 struct rpc_task *task;
9135 struct rpc_message msg = {
9136 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9137 .rpc_argp = &lgp->args,
9138 .rpc_resp = &lgp->res,
9139 .rpc_cred = lgp->cred,
9141 struct rpc_task_setup task_setup_data = {
9142 .rpc_client = server->client,
9143 .rpc_message = &msg,
9144 .callback_ops = &nfs4_layoutget_call_ops,
9145 .callback_data = lgp,
9146 .flags = RPC_TASK_ASYNC,
9148 struct pnfs_layout_segment *lseg = NULL;
9149 struct nfs4_exception exception = {
9151 .timeout = *timeout,
9155 dprintk("--> %s\n", __func__);
9157 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9158 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9160 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9162 task = rpc_run_task(&task_setup_data);
9164 return ERR_CAST(task);
9165 status = rpc_wait_for_completion_task(task);
9169 if (task->tk_status < 0) {
9170 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9171 *timeout = exception.timeout;
9172 } else if (lgp->res.layoutp->len == 0) {
9174 *timeout = nfs4_update_delay(&exception.timeout);
9176 lseg = pnfs_layout_process(lgp);
9178 trace_nfs4_layoutget(lgp->args.ctx,
9185 dprintk("<-- %s status=%d\n", __func__, status);
9187 return ERR_PTR(status);
9192 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9194 struct nfs4_layoutreturn *lrp = calldata;
9196 dprintk("--> %s\n", __func__);
9197 nfs4_setup_sequence(lrp->clp,
9198 &lrp->args.seq_args,
9201 if (!pnfs_layout_is_valid(lrp->args.layout))
9205 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9207 struct nfs4_layoutreturn *lrp = calldata;
9208 struct nfs_server *server;
9210 dprintk("--> %s\n", __func__);
9212 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9216 * Was there an RPC level error? Assume the call succeeded,
9217 * and that we need to release the layout
9219 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9220 lrp->res.lrs_present = 0;
9224 server = NFS_SERVER(lrp->args.inode);
9225 switch (task->tk_status) {
9226 case -NFS4ERR_OLD_STATEID:
9227 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9233 task->tk_status = 0;
9237 case -NFS4ERR_DELAY:
9238 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9242 dprintk("<-- %s\n", __func__);
9245 task->tk_status = 0;
9246 nfs4_sequence_free_slot(&lrp->res.seq_res);
9247 rpc_restart_call_prepare(task);
9250 static void nfs4_layoutreturn_release(void *calldata)
9252 struct nfs4_layoutreturn *lrp = calldata;
9253 struct pnfs_layout_hdr *lo = lrp->args.layout;
9255 dprintk("--> %s\n", __func__);
9256 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9257 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9258 nfs4_sequence_free_slot(&lrp->res.seq_res);
9259 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9260 lrp->ld_private.ops->free(&lrp->ld_private);
9261 pnfs_put_layout_hdr(lrp->args.layout);
9262 nfs_iput_and_deactive(lrp->inode);
9264 dprintk("<-- %s\n", __func__);
9267 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9268 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9269 .rpc_call_done = nfs4_layoutreturn_done,
9270 .rpc_release = nfs4_layoutreturn_release,
9273 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9275 struct rpc_task *task;
9276 struct rpc_message msg = {
9277 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9278 .rpc_argp = &lrp->args,
9279 .rpc_resp = &lrp->res,
9280 .rpc_cred = lrp->cred,
9282 struct rpc_task_setup task_setup_data = {
9283 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9284 .rpc_message = &msg,
9285 .callback_ops = &nfs4_layoutreturn_call_ops,
9286 .callback_data = lrp,
9290 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9291 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9292 &task_setup_data.rpc_client, &msg);
9294 dprintk("--> %s\n", __func__);
9296 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9298 nfs4_layoutreturn_release(lrp);
9301 task_setup_data.flags |= RPC_TASK_ASYNC;
9303 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9304 task = rpc_run_task(&task_setup_data);
9306 return PTR_ERR(task);
9308 status = task->tk_status;
9309 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9310 dprintk("<-- %s status=%d\n", __func__, status);
9316 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9317 struct pnfs_device *pdev,
9318 const struct cred *cred)
9320 struct nfs4_getdeviceinfo_args args = {
9322 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9323 NOTIFY_DEVICEID4_DELETE,
9325 struct nfs4_getdeviceinfo_res res = {
9328 struct rpc_message msg = {
9329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9336 dprintk("--> %s\n", __func__);
9337 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9338 if (res.notification & ~args.notify_types)
9339 dprintk("%s: unsupported notification\n", __func__);
9340 if (res.notification != args.notify_types)
9343 dprintk("<-- %s status=%d\n", __func__, status);
9348 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9349 struct pnfs_device *pdev,
9350 const struct cred *cred)
9352 struct nfs4_exception exception = { };
9356 err = nfs4_handle_exception(server,
9357 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9359 } while (exception.retry);
9362 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9364 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9366 struct nfs4_layoutcommit_data *data = calldata;
9367 struct nfs_server *server = NFS_SERVER(data->args.inode);
9369 nfs4_setup_sequence(server->nfs_client,
9370 &data->args.seq_args,
9376 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9378 struct nfs4_layoutcommit_data *data = calldata;
9379 struct nfs_server *server = NFS_SERVER(data->args.inode);
9381 if (!nfs41_sequence_done(task, &data->res.seq_res))
9384 switch (task->tk_status) { /* Just ignore these failures */
9385 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9386 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9387 case -NFS4ERR_BADLAYOUT: /* no layout */
9388 case -NFS4ERR_GRACE: /* loca_recalim always false */
9389 task->tk_status = 0;
9393 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9394 rpc_restart_call_prepare(task);
9400 static void nfs4_layoutcommit_release(void *calldata)
9402 struct nfs4_layoutcommit_data *data = calldata;
9404 pnfs_cleanup_layoutcommit(data);
9405 nfs_post_op_update_inode_force_wcc(data->args.inode,
9407 put_cred(data->cred);
9408 nfs_iput_and_deactive(data->inode);
9412 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9413 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9414 .rpc_call_done = nfs4_layoutcommit_done,
9415 .rpc_release = nfs4_layoutcommit_release,
9419 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9421 struct rpc_message msg = {
9422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9423 .rpc_argp = &data->args,
9424 .rpc_resp = &data->res,
9425 .rpc_cred = data->cred,
9427 struct rpc_task_setup task_setup_data = {
9428 .task = &data->task,
9429 .rpc_client = NFS_CLIENT(data->args.inode),
9430 .rpc_message = &msg,
9431 .callback_ops = &nfs4_layoutcommit_ops,
9432 .callback_data = data,
9434 struct rpc_task *task;
9437 dprintk("NFS: initiating layoutcommit call. sync %d "
9438 "lbw: %llu inode %lu\n", sync,
9439 data->args.lastbytewritten,
9440 data->args.inode->i_ino);
9443 data->inode = nfs_igrab_and_active(data->args.inode);
9444 if (data->inode == NULL) {
9445 nfs4_layoutcommit_release(data);
9448 task_setup_data.flags = RPC_TASK_ASYNC;
9450 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9451 task = rpc_run_task(&task_setup_data);
9453 return PTR_ERR(task);
9455 status = task->tk_status;
9456 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9457 dprintk("%s: status %d\n", __func__, status);
9463 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9464 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9467 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9468 struct nfs_fsinfo *info,
9469 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9471 struct nfs41_secinfo_no_name_args args = {
9472 .style = SECINFO_STYLE_CURRENT_FH,
9474 struct nfs4_secinfo_res res = {
9477 struct rpc_message msg = {
9478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9482 struct rpc_clnt *clnt = server->client;
9483 struct nfs4_call_sync_data data = {
9484 .seq_server = server,
9485 .seq_args = &args.seq_args,
9486 .seq_res = &res.seq_res,
9488 struct rpc_task_setup task_setup = {
9489 .rpc_client = server->client,
9490 .rpc_message = &msg,
9491 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9492 .callback_data = &data,
9493 .flags = RPC_TASK_NO_ROUND_ROBIN,
9495 const struct cred *cred = NULL;
9498 if (use_integrity) {
9499 clnt = server->nfs_client->cl_rpcclient;
9500 task_setup.rpc_client = clnt;
9502 cred = nfs4_get_clid_cred(server->nfs_client);
9503 msg.rpc_cred = cred;
9506 dprintk("--> %s\n", __func__);
9507 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9508 status = nfs4_call_sync_custom(&task_setup);
9509 dprintk("<-- %s status=%d\n", __func__, status);
9517 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9518 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9520 struct nfs4_exception exception = {
9521 .interruptible = true,
9525 /* first try using integrity protection */
9526 err = -NFS4ERR_WRONGSEC;
9528 /* try to use integrity protection with machine cred */
9529 if (_nfs4_is_integrity_protected(server->nfs_client))
9530 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9534 * if unable to use integrity protection, or SECINFO with
9535 * integrity protection returns NFS4ERR_WRONGSEC (which is
9536 * disallowed by spec, but exists in deployed servers) use
9537 * the current filesystem's rpc_client and the user cred.
9539 if (err == -NFS4ERR_WRONGSEC)
9540 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9545 case -NFS4ERR_WRONGSEC:
9549 err = nfs4_handle_exception(server, err, &exception);
9551 } while (exception.retry);
9557 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9558 struct nfs_fsinfo *info)
9562 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9563 struct nfs4_secinfo_flavors *flavors;
9564 struct nfs4_secinfo4 *secinfo;
9567 page = alloc_page(GFP_KERNEL);
9573 flavors = page_address(page);
9574 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9577 * Fall back on "guess and check" method if
9578 * the server doesn't support SECINFO_NO_NAME
9580 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9581 err = nfs4_find_root_sec(server, fhandle, info);
9587 for (i = 0; i < flavors->num_flavors; i++) {
9588 secinfo = &flavors->flavors[i];
9590 switch (secinfo->flavor) {
9594 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9595 &secinfo->flavor_info);
9598 flavor = RPC_AUTH_MAXFLAVOR;
9602 if (!nfs_auth_info_match(&server->auth_info, flavor))
9603 flavor = RPC_AUTH_MAXFLAVOR;
9605 if (flavor != RPC_AUTH_MAXFLAVOR) {
9606 err = nfs4_lookup_root_sec(server, fhandle,
9613 if (flavor == RPC_AUTH_MAXFLAVOR)
9624 static int _nfs41_test_stateid(struct nfs_server *server,
9625 nfs4_stateid *stateid,
9626 const struct cred *cred)
9629 struct nfs41_test_stateid_args args = {
9632 struct nfs41_test_stateid_res res;
9633 struct rpc_message msg = {
9634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9639 struct rpc_clnt *rpc_client = server->client;
9641 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9644 dprintk("NFS call test_stateid %p\n", stateid);
9645 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9646 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9647 &args.seq_args, &res.seq_res);
9648 if (status != NFS_OK) {
9649 dprintk("NFS reply test_stateid: failed, %d\n", status);
9652 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9656 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9657 int err, struct nfs4_exception *exception)
9659 exception->retry = 0;
9661 case -NFS4ERR_DELAY:
9662 case -NFS4ERR_RETRY_UNCACHED_REP:
9663 nfs4_handle_exception(server, err, exception);
9665 case -NFS4ERR_BADSESSION:
9666 case -NFS4ERR_BADSLOT:
9667 case -NFS4ERR_BAD_HIGH_SLOT:
9668 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9669 case -NFS4ERR_DEADSESSION:
9670 nfs4_do_handle_exception(server, err, exception);
9675 * nfs41_test_stateid - perform a TEST_STATEID operation
9677 * @server: server / transport on which to perform the operation
9678 * @stateid: state ID to test
9681 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9682 * Otherwise a negative NFS4ERR value is returned if the operation
9683 * failed or the state ID is not currently valid.
9685 static int nfs41_test_stateid(struct nfs_server *server,
9686 nfs4_stateid *stateid,
9687 const struct cred *cred)
9689 struct nfs4_exception exception = {
9690 .interruptible = true,
9694 err = _nfs41_test_stateid(server, stateid, cred);
9695 nfs4_handle_delay_or_session_error(server, err, &exception);
9696 } while (exception.retry);
9700 struct nfs_free_stateid_data {
9701 struct nfs_server *server;
9702 struct nfs41_free_stateid_args args;
9703 struct nfs41_free_stateid_res res;
9706 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9708 struct nfs_free_stateid_data *data = calldata;
9709 nfs4_setup_sequence(data->server->nfs_client,
9710 &data->args.seq_args,
9715 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9717 struct nfs_free_stateid_data *data = calldata;
9719 nfs41_sequence_done(task, &data->res.seq_res);
9721 switch (task->tk_status) {
9722 case -NFS4ERR_DELAY:
9723 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9724 rpc_restart_call_prepare(task);
9728 static void nfs41_free_stateid_release(void *calldata)
9733 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9734 .rpc_call_prepare = nfs41_free_stateid_prepare,
9735 .rpc_call_done = nfs41_free_stateid_done,
9736 .rpc_release = nfs41_free_stateid_release,
9740 * nfs41_free_stateid - perform a FREE_STATEID operation
9742 * @server: server / transport on which to perform the operation
9743 * @stateid: state ID to release
9745 * @privileged: set to true if this call needs to be privileged
9747 * Note: this function is always asynchronous.
9749 static int nfs41_free_stateid(struct nfs_server *server,
9750 const nfs4_stateid *stateid,
9751 const struct cred *cred,
9754 struct rpc_message msg = {
9755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9758 struct rpc_task_setup task_setup = {
9759 .rpc_client = server->client,
9760 .rpc_message = &msg,
9761 .callback_ops = &nfs41_free_stateid_ops,
9762 .flags = RPC_TASK_ASYNC,
9764 struct nfs_free_stateid_data *data;
9765 struct rpc_task *task;
9767 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9768 &task_setup.rpc_client, &msg);
9770 dprintk("NFS call free_stateid %p\n", stateid);
9771 data = kmalloc(sizeof(*data), GFP_NOFS);
9774 data->server = server;
9775 nfs4_stateid_copy(&data->args.stateid, stateid);
9777 task_setup.callback_data = data;
9779 msg.rpc_argp = &data->args;
9780 msg.rpc_resp = &data->res;
9781 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
9782 task = rpc_run_task(&task_setup);
9784 return PTR_ERR(task);
9790 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9792 const struct cred *cred = lsp->ls_state->owner->so_cred;
9794 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9795 nfs4_free_lock_state(server, lsp);
9798 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9799 const nfs4_stateid *s2)
9801 if (s1->type != s2->type)
9804 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9807 if (s1->seqid == s2->seqid)
9810 return s1->seqid == 0 || s2->seqid == 0;
9813 #endif /* CONFIG_NFS_V4_1 */
9815 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9816 const nfs4_stateid *s2)
9818 return nfs4_stateid_match(s1, s2);
9822 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9823 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9824 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9825 .recover_open = nfs4_open_reclaim,
9826 .recover_lock = nfs4_lock_reclaim,
9827 .establish_clid = nfs4_init_clientid,
9828 .detect_trunking = nfs40_discover_server_trunking,
9831 #if defined(CONFIG_NFS_V4_1)
9832 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9833 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9834 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9835 .recover_open = nfs4_open_reclaim,
9836 .recover_lock = nfs4_lock_reclaim,
9837 .establish_clid = nfs41_init_clientid,
9838 .reclaim_complete = nfs41_proc_reclaim_complete,
9839 .detect_trunking = nfs41_discover_server_trunking,
9841 #endif /* CONFIG_NFS_V4_1 */
9843 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9844 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9845 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9846 .recover_open = nfs40_open_expired,
9847 .recover_lock = nfs4_lock_expired,
9848 .establish_clid = nfs4_init_clientid,
9851 #if defined(CONFIG_NFS_V4_1)
9852 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9853 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9854 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9855 .recover_open = nfs41_open_expired,
9856 .recover_lock = nfs41_lock_expired,
9857 .establish_clid = nfs41_init_clientid,
9859 #endif /* CONFIG_NFS_V4_1 */
9861 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9862 .sched_state_renewal = nfs4_proc_async_renew,
9863 .get_state_renewal_cred = nfs4_get_renew_cred,
9864 .renew_lease = nfs4_proc_renew,
9867 #if defined(CONFIG_NFS_V4_1)
9868 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9869 .sched_state_renewal = nfs41_proc_async_sequence,
9870 .get_state_renewal_cred = nfs4_get_machine_cred,
9871 .renew_lease = nfs4_proc_sequence,
9875 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9876 .get_locations = _nfs40_proc_get_locations,
9877 .fsid_present = _nfs40_proc_fsid_present,
9880 #if defined(CONFIG_NFS_V4_1)
9881 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9882 .get_locations = _nfs41_proc_get_locations,
9883 .fsid_present = _nfs41_proc_fsid_present,
9885 #endif /* CONFIG_NFS_V4_1 */
9887 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9889 .init_caps = NFS_CAP_READDIRPLUS
9890 | NFS_CAP_ATOMIC_OPEN
9891 | NFS_CAP_POSIX_LOCK,
9892 .init_client = nfs40_init_client,
9893 .shutdown_client = nfs40_shutdown_client,
9894 .match_stateid = nfs4_match_stateid,
9895 .find_root_sec = nfs4_find_root_sec,
9896 .free_lock_state = nfs4_release_lockowner,
9897 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9898 .alloc_seqid = nfs_alloc_seqid,
9899 .call_sync_ops = &nfs40_call_sync_ops,
9900 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9901 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9902 .state_renewal_ops = &nfs40_state_renewal_ops,
9903 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9906 #if defined(CONFIG_NFS_V4_1)
9907 static struct nfs_seqid *
9908 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9913 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9915 .init_caps = NFS_CAP_READDIRPLUS
9916 | NFS_CAP_ATOMIC_OPEN
9917 | NFS_CAP_POSIX_LOCK
9918 | NFS_CAP_STATEID_NFSV41
9919 | NFS_CAP_ATOMIC_OPEN_V1
9921 .init_client = nfs41_init_client,
9922 .shutdown_client = nfs41_shutdown_client,
9923 .match_stateid = nfs41_match_stateid,
9924 .find_root_sec = nfs41_find_root_sec,
9925 .free_lock_state = nfs41_free_lock_state,
9926 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9927 .alloc_seqid = nfs_alloc_no_seqid,
9928 .session_trunk = nfs4_test_session_trunk,
9929 .call_sync_ops = &nfs41_call_sync_ops,
9930 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9931 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9932 .state_renewal_ops = &nfs41_state_renewal_ops,
9933 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9937 #if defined(CONFIG_NFS_V4_2)
9938 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9940 .init_caps = NFS_CAP_READDIRPLUS
9941 | NFS_CAP_ATOMIC_OPEN
9942 | NFS_CAP_POSIX_LOCK
9943 | NFS_CAP_STATEID_NFSV41
9944 | NFS_CAP_ATOMIC_OPEN_V1
9948 | NFS_CAP_OFFLOAD_CANCEL
9949 | NFS_CAP_COPY_NOTIFY
9950 | NFS_CAP_DEALLOCATE
9952 | NFS_CAP_LAYOUTSTATS
9954 | NFS_CAP_LAYOUTERROR,
9955 .init_client = nfs41_init_client,
9956 .shutdown_client = nfs41_shutdown_client,
9957 .match_stateid = nfs41_match_stateid,
9958 .find_root_sec = nfs41_find_root_sec,
9959 .free_lock_state = nfs41_free_lock_state,
9960 .call_sync_ops = &nfs41_call_sync_ops,
9961 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9962 .alloc_seqid = nfs_alloc_no_seqid,
9963 .session_trunk = nfs4_test_session_trunk,
9964 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9965 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9966 .state_renewal_ops = &nfs41_state_renewal_ops,
9967 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9971 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9972 [0] = &nfs_v4_0_minor_ops,
9973 #if defined(CONFIG_NFS_V4_1)
9974 [1] = &nfs_v4_1_minor_ops,
9976 #if defined(CONFIG_NFS_V4_2)
9977 [2] = &nfs_v4_2_minor_ops,
9981 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9983 ssize_t error, error2;
9985 error = generic_listxattr(dentry, list, size);
9993 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9996 return error + error2;
9999 static const struct inode_operations nfs4_dir_inode_operations = {
10000 .create = nfs_create,
10001 .lookup = nfs_lookup,
10002 .atomic_open = nfs_atomic_open,
10004 .unlink = nfs_unlink,
10005 .symlink = nfs_symlink,
10006 .mkdir = nfs_mkdir,
10007 .rmdir = nfs_rmdir,
10008 .mknod = nfs_mknod,
10009 .rename = nfs_rename,
10010 .permission = nfs_permission,
10011 .getattr = nfs_getattr,
10012 .setattr = nfs_setattr,
10013 .listxattr = nfs4_listxattr,
10016 static const struct inode_operations nfs4_file_inode_operations = {
10017 .permission = nfs_permission,
10018 .getattr = nfs_getattr,
10019 .setattr = nfs_setattr,
10020 .listxattr = nfs4_listxattr,
10023 const struct nfs_rpc_ops nfs_v4_clientops = {
10024 .version = 4, /* protocol version */
10025 .dentry_ops = &nfs4_dentry_operations,
10026 .dir_inode_ops = &nfs4_dir_inode_operations,
10027 .file_inode_ops = &nfs4_file_inode_operations,
10028 .file_ops = &nfs4_file_operations,
10029 .getroot = nfs4_proc_get_root,
10030 .submount = nfs4_submount,
10031 .try_get_tree = nfs4_try_get_tree,
10032 .getattr = nfs4_proc_getattr,
10033 .setattr = nfs4_proc_setattr,
10034 .lookup = nfs4_proc_lookup,
10035 .lookupp = nfs4_proc_lookupp,
10036 .access = nfs4_proc_access,
10037 .readlink = nfs4_proc_readlink,
10038 .create = nfs4_proc_create,
10039 .remove = nfs4_proc_remove,
10040 .unlink_setup = nfs4_proc_unlink_setup,
10041 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10042 .unlink_done = nfs4_proc_unlink_done,
10043 .rename_setup = nfs4_proc_rename_setup,
10044 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10045 .rename_done = nfs4_proc_rename_done,
10046 .link = nfs4_proc_link,
10047 .symlink = nfs4_proc_symlink,
10048 .mkdir = nfs4_proc_mkdir,
10049 .rmdir = nfs4_proc_rmdir,
10050 .readdir = nfs4_proc_readdir,
10051 .mknod = nfs4_proc_mknod,
10052 .statfs = nfs4_proc_statfs,
10053 .fsinfo = nfs4_proc_fsinfo,
10054 .pathconf = nfs4_proc_pathconf,
10055 .set_capabilities = nfs4_server_capabilities,
10056 .decode_dirent = nfs4_decode_dirent,
10057 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10058 .read_setup = nfs4_proc_read_setup,
10059 .read_done = nfs4_read_done,
10060 .write_setup = nfs4_proc_write_setup,
10061 .write_done = nfs4_write_done,
10062 .commit_setup = nfs4_proc_commit_setup,
10063 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10064 .commit_done = nfs4_commit_done,
10065 .lock = nfs4_proc_lock,
10066 .clear_acl_cache = nfs4_zap_acl_attr,
10067 .close_context = nfs4_close_context,
10068 .open_context = nfs4_atomic_open,
10069 .have_delegation = nfs4_have_delegation,
10070 .alloc_client = nfs4_alloc_client,
10071 .init_client = nfs4_init_client,
10072 .free_client = nfs4_free_client,
10073 .create_server = nfs4_create_server,
10074 .clone_server = nfs_clone_server,
10077 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10078 .name = XATTR_NAME_NFSV4_ACL,
10079 .list = nfs4_xattr_list_nfs4_acl,
10080 .get = nfs4_xattr_get_nfs4_acl,
10081 .set = nfs4_xattr_set_nfs4_acl,
10084 const struct xattr_handler *nfs4_xattr_handlers[] = {
10085 &nfs4_xattr_nfs4_acl_handler,
10086 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10087 &nfs4_xattr_nfs4_label_handler,
10094 * c-basic-offset: 8