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"
67 #include "nfs4idmap.h"
68 #include "nfs4session.h"
72 #include "nfs4trace.h"
74 #define NFSDBG_FACILITY NFSDBG_PROC
76 #define NFS4_BITMASK_SZ 3
78 #define NFS4_POLL_RETRY_MIN (HZ/10)
79 #define NFS4_POLL_RETRY_MAX (15*HZ)
81 /* file attributes which can be mapped to nfs attributes */
82 #define NFS4_VALID_ATTRS (ATTR_MODE \
93 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
94 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
95 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
96 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
97 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
98 struct nfs_fattr *fattr, struct iattr *sattr,
99 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
100 struct nfs4_label *olabel);
101 #ifdef CONFIG_NFS_V4_1
102 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
103 const struct cred *cred,
104 struct nfs4_slot *slot,
106 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
107 const struct cred *);
108 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
109 const struct cred *, bool);
111 static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ],
112 const __u32 *src, struct inode *inode,
113 struct nfs_server *server,
114 struct nfs4_label *label);
116 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
117 static inline struct nfs4_label *
118 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
119 struct iattr *sattr, struct nfs4_label *label)
126 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
134 err = security_dentry_init_security(dentry, sattr->ia_mode,
135 &dentry->d_name, (void **)&label->label, &label->len);
142 nfs4_label_release_security(struct nfs4_label *label)
145 security_release_secctx(label->label, label->len);
147 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
150 return server->attr_bitmask;
152 return server->attr_bitmask_nl;
155 static inline struct nfs4_label *
156 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
157 struct iattr *sattr, struct nfs4_label *l)
160 nfs4_label_release_security(struct nfs4_label *label)
163 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
164 { return server->attr_bitmask; }
167 /* Prevent leaks of NFSv4 errors into userland */
168 static int nfs4_map_errors(int err)
173 case -NFS4ERR_RESOURCE:
174 case -NFS4ERR_LAYOUTTRYLATER:
175 case -NFS4ERR_RECALLCONFLICT:
177 case -NFS4ERR_WRONGSEC:
178 case -NFS4ERR_WRONG_CRED:
180 case -NFS4ERR_BADOWNER:
181 case -NFS4ERR_BADNAME:
183 case -NFS4ERR_SHARE_DENIED:
185 case -NFS4ERR_MINOR_VERS_MISMATCH:
186 return -EPROTONOSUPPORT;
187 case -NFS4ERR_FILE_OPEN:
189 case -NFS4ERR_NOT_SAME:
192 dprintk("%s could not handle NFSv4 error %d\n",
200 * This is our standard bitmap for GETATTR requests.
202 const u32 nfs4_fattr_bitmap[3] = {
204 | FATTR4_WORD0_CHANGE
207 | FATTR4_WORD0_FILEID,
209 | FATTR4_WORD1_NUMLINKS
211 | FATTR4_WORD1_OWNER_GROUP
212 | FATTR4_WORD1_RAWDEV
213 | FATTR4_WORD1_SPACE_USED
214 | FATTR4_WORD1_TIME_ACCESS
215 | FATTR4_WORD1_TIME_METADATA
216 | FATTR4_WORD1_TIME_MODIFY
217 | FATTR4_WORD1_MOUNTED_ON_FILEID,
218 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
219 FATTR4_WORD2_SECURITY_LABEL
223 static const u32 nfs4_pnfs_open_bitmap[3] = {
225 | FATTR4_WORD0_CHANGE
228 | FATTR4_WORD0_FILEID,
230 | FATTR4_WORD1_NUMLINKS
232 | FATTR4_WORD1_OWNER_GROUP
233 | FATTR4_WORD1_RAWDEV
234 | FATTR4_WORD1_SPACE_USED
235 | FATTR4_WORD1_TIME_ACCESS
236 | FATTR4_WORD1_TIME_METADATA
237 | FATTR4_WORD1_TIME_MODIFY,
238 FATTR4_WORD2_MDSTHRESHOLD
239 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
240 | FATTR4_WORD2_SECURITY_LABEL
244 static const u32 nfs4_open_noattr_bitmap[3] = {
246 | FATTR4_WORD0_FILEID,
249 const u32 nfs4_statfs_bitmap[3] = {
250 FATTR4_WORD0_FILES_AVAIL
251 | FATTR4_WORD0_FILES_FREE
252 | FATTR4_WORD0_FILES_TOTAL,
253 FATTR4_WORD1_SPACE_AVAIL
254 | FATTR4_WORD1_SPACE_FREE
255 | FATTR4_WORD1_SPACE_TOTAL
258 const u32 nfs4_pathconf_bitmap[3] = {
260 | FATTR4_WORD0_MAXNAME,
264 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
265 | FATTR4_WORD0_MAXREAD
266 | FATTR4_WORD0_MAXWRITE
267 | FATTR4_WORD0_LEASE_TIME,
268 FATTR4_WORD1_TIME_DELTA
269 | FATTR4_WORD1_FS_LAYOUT_TYPES,
270 FATTR4_WORD2_LAYOUT_BLKSIZE
271 | FATTR4_WORD2_CLONE_BLKSIZE
272 | FATTR4_WORD2_CHANGE_ATTR_TYPE
273 | FATTR4_WORD2_XATTR_SUPPORT
276 const u32 nfs4_fs_locations_bitmap[3] = {
280 | FATTR4_WORD0_FILEID
281 | FATTR4_WORD0_FS_LOCATIONS,
283 | FATTR4_WORD1_OWNER_GROUP
284 | FATTR4_WORD1_RAWDEV
285 | FATTR4_WORD1_SPACE_USED
286 | FATTR4_WORD1_TIME_ACCESS
287 | FATTR4_WORD1_TIME_METADATA
288 | FATTR4_WORD1_TIME_MODIFY
289 | FATTR4_WORD1_MOUNTED_ON_FILEID,
292 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
293 struct inode *inode, unsigned long flags)
295 unsigned long cache_validity;
297 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
298 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
301 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags;
303 /* Remove the attributes over which we have full control */
304 dst[1] &= ~FATTR4_WORD1_RAWDEV;
305 if (!(cache_validity & NFS_INO_INVALID_SIZE))
306 dst[0] &= ~FATTR4_WORD0_SIZE;
308 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
309 dst[0] &= ~FATTR4_WORD0_CHANGE;
311 if (!(cache_validity & NFS_INO_INVALID_MODE))
312 dst[1] &= ~FATTR4_WORD1_MODE;
313 if (!(cache_validity & NFS_INO_INVALID_OTHER))
314 dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP);
317 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
318 struct nfs4_readdir_arg *readdir)
320 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
324 readdir->cookie = cookie;
325 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
330 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
335 * NFSv4 servers do not return entries for '.' and '..'
336 * Therefore, we fake these entries here. We let '.'
337 * have cookie 0 and '..' have cookie 1. Note that
338 * when talking to the server, we always send cookie 0
341 start = p = kmap_atomic(*readdir->pages);
344 *p++ = xdr_one; /* next */
345 *p++ = xdr_zero; /* cookie, first word */
346 *p++ = xdr_one; /* cookie, second word */
347 *p++ = xdr_one; /* entry len */
348 memcpy(p, ".\0\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)));
357 *p++ = xdr_one; /* next */
358 *p++ = xdr_zero; /* cookie, first word */
359 *p++ = xdr_two; /* cookie, second word */
360 *p++ = xdr_two; /* entry len */
361 memcpy(p, "..\0\0", 4); /* entry */
363 *p++ = xdr_one; /* bitmap length */
364 *p++ = htonl(attrs); /* bitmap */
365 *p++ = htonl(12); /* attribute buffer length */
366 *p++ = htonl(NF4DIR);
367 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
369 readdir->pgbase = (char *)p - (char *)start;
370 readdir->count -= readdir->pgbase;
371 kunmap_atomic(start);
374 static void nfs4_fattr_set_prechange(struct nfs_fattr *fattr, u64 version)
376 if (!(fattr->valid & NFS_ATTR_FATTR_PRECHANGE)) {
377 fattr->pre_change_attr = version;
378 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
382 static void nfs4_test_and_free_stateid(struct nfs_server *server,
383 nfs4_stateid *stateid,
384 const struct cred *cred)
386 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
388 ops->test_and_free_expired(server, stateid, cred);
391 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
392 nfs4_stateid *stateid,
393 const struct cred *cred)
395 stateid->type = NFS4_REVOKED_STATEID_TYPE;
396 nfs4_test_and_free_stateid(server, stateid, cred);
399 static void nfs4_free_revoked_stateid(struct nfs_server *server,
400 const nfs4_stateid *stateid,
401 const struct cred *cred)
405 nfs4_stateid_copy(&tmp, stateid);
406 __nfs4_free_revoked_stateid(server, &tmp, cred);
409 static long nfs4_update_delay(long *timeout)
413 return NFS4_POLL_RETRY_MAX;
415 *timeout = NFS4_POLL_RETRY_MIN;
416 if (*timeout > NFS4_POLL_RETRY_MAX)
417 *timeout = NFS4_POLL_RETRY_MAX;
423 static int nfs4_delay_killable(long *timeout)
427 freezable_schedule_timeout_killable_unsafe(
428 nfs4_update_delay(timeout));
429 if (!__fatal_signal_pending(current))
434 static int nfs4_delay_interruptible(long *timeout)
438 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
439 if (!signal_pending(current))
441 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
444 static int nfs4_delay(long *timeout, bool interruptible)
447 return nfs4_delay_interruptible(timeout);
448 return nfs4_delay_killable(timeout);
451 static const nfs4_stateid *
452 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
456 switch (stateid->type) {
457 case NFS4_OPEN_STATEID_TYPE:
458 case NFS4_LOCK_STATEID_TYPE:
459 case NFS4_DELEGATION_STATEID_TYPE:
467 /* This is the error handling routine for processes that are allowed
470 static int nfs4_do_handle_exception(struct nfs_server *server,
471 int errorcode, struct nfs4_exception *exception)
473 struct nfs_client *clp = server->nfs_client;
474 struct nfs4_state *state = exception->state;
475 const nfs4_stateid *stateid;
476 struct inode *inode = exception->inode;
479 exception->delay = 0;
480 exception->recovering = 0;
481 exception->retry = 0;
483 stateid = nfs4_recoverable_stateid(exception->stateid);
484 if (stateid == NULL && state != NULL)
485 stateid = nfs4_recoverable_stateid(&state->stateid);
490 case -NFS4ERR_BADHANDLE:
492 if (inode != NULL && S_ISREG(inode->i_mode))
493 pnfs_destroy_layout(NFS_I(inode));
495 case -NFS4ERR_DELEG_REVOKED:
496 case -NFS4ERR_ADMIN_REVOKED:
497 case -NFS4ERR_EXPIRED:
498 case -NFS4ERR_BAD_STATEID:
499 case -NFS4ERR_PARTNER_NO_AUTH:
500 if (inode != NULL && stateid != NULL) {
501 nfs_inode_find_state_and_recover(inode,
503 goto wait_on_recovery;
506 case -NFS4ERR_OPENMODE:
510 err = nfs_async_inode_return_delegation(inode,
513 goto wait_on_recovery;
514 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
515 exception->retry = 1;
521 ret = nfs4_schedule_stateid_recovery(server, state);
524 goto wait_on_recovery;
525 case -NFS4ERR_STALE_STATEID:
526 case -NFS4ERR_STALE_CLIENTID:
527 nfs4_schedule_lease_recovery(clp);
528 goto wait_on_recovery;
530 ret = nfs4_schedule_migration_recovery(server);
533 goto wait_on_recovery;
534 case -NFS4ERR_LEASE_MOVED:
535 nfs4_schedule_lease_moved_recovery(clp);
536 goto wait_on_recovery;
537 #if defined(CONFIG_NFS_V4_1)
538 case -NFS4ERR_BADSESSION:
539 case -NFS4ERR_BADSLOT:
540 case -NFS4ERR_BAD_HIGH_SLOT:
541 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
542 case -NFS4ERR_DEADSESSION:
543 case -NFS4ERR_SEQ_FALSE_RETRY:
544 case -NFS4ERR_SEQ_MISORDERED:
545 /* Handled in nfs41_sequence_process() */
546 goto wait_on_recovery;
547 #endif /* defined(CONFIG_NFS_V4_1) */
548 case -NFS4ERR_FILE_OPEN:
549 if (exception->timeout > HZ) {
550 /* We have retried a decent amount, time to
558 nfs_inc_server_stats(server, NFSIOS_DELAY);
561 case -NFS4ERR_LAYOUTTRYLATER:
562 case -NFS4ERR_RECALLCONFLICT:
563 exception->delay = 1;
566 case -NFS4ERR_RETRY_UNCACHED_REP:
567 case -NFS4ERR_OLD_STATEID:
568 exception->retry = 1;
570 case -NFS4ERR_BADOWNER:
571 /* The following works around a Linux server bug! */
572 case -NFS4ERR_BADNAME:
573 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
574 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
575 exception->retry = 1;
576 printk(KERN_WARNING "NFS: v4 server %s "
577 "does not accept raw "
579 "Reenabling the idmapper.\n",
580 server->nfs_client->cl_hostname);
583 /* We failed to handle the error */
584 return nfs4_map_errors(ret);
586 exception->recovering = 1;
590 /* This is the error handling routine for processes that are allowed
593 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
595 struct nfs_client *clp = server->nfs_client;
598 ret = nfs4_do_handle_exception(server, errorcode, exception);
599 if (exception->delay) {
600 ret = nfs4_delay(&exception->timeout,
601 exception->interruptible);
604 if (exception->recovering) {
605 if (exception->task_is_privileged)
607 ret = nfs4_wait_clnt_recover(clp);
608 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
615 exception->retry = 1;
620 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
621 int errorcode, struct nfs4_exception *exception)
623 struct nfs_client *clp = server->nfs_client;
626 ret = nfs4_do_handle_exception(server, errorcode, exception);
627 if (exception->delay) {
628 rpc_delay(task, nfs4_update_delay(&exception->timeout));
631 if (exception->recovering) {
632 if (exception->task_is_privileged)
634 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
635 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
636 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
639 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
644 exception->retry = 1;
646 * For NFS4ERR_MOVED, the client transport will need to
647 * be recomputed after migration recovery has completed.
649 if (errorcode == -NFS4ERR_MOVED)
650 rpc_task_release_transport(task);
656 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
657 struct nfs4_state *state, long *timeout)
659 struct nfs4_exception exception = {
663 if (task->tk_status >= 0)
666 exception.timeout = *timeout;
667 task->tk_status = nfs4_async_handle_exception(task, server,
670 if (exception.delay && timeout)
671 *timeout = exception.timeout;
678 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
679 * or 'false' otherwise.
681 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
683 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
684 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
687 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
689 spin_lock(&clp->cl_lock);
690 if (time_before(clp->cl_last_renewal,timestamp))
691 clp->cl_last_renewal = timestamp;
692 spin_unlock(&clp->cl_lock);
695 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
697 struct nfs_client *clp = server->nfs_client;
699 if (!nfs4_has_session(clp))
700 do_renew_lease(clp, timestamp);
703 struct nfs4_call_sync_data {
704 const struct nfs_server *seq_server;
705 struct nfs4_sequence_args *seq_args;
706 struct nfs4_sequence_res *seq_res;
709 void nfs4_init_sequence(struct nfs4_sequence_args *args,
710 struct nfs4_sequence_res *res, int cache_reply,
713 args->sa_slot = NULL;
714 args->sa_cache_this = cache_reply;
715 args->sa_privileged = privileged;
720 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
722 struct nfs4_slot *slot = res->sr_slot;
723 struct nfs4_slot_table *tbl;
726 spin_lock(&tbl->slot_tbl_lock);
727 if (!nfs41_wake_and_assign_slot(tbl, slot))
728 nfs4_free_slot(tbl, slot);
729 spin_unlock(&tbl->slot_tbl_lock);
734 static int nfs40_sequence_done(struct rpc_task *task,
735 struct nfs4_sequence_res *res)
737 if (res->sr_slot != NULL)
738 nfs40_sequence_free_slot(res);
742 #if defined(CONFIG_NFS_V4_1)
744 static void nfs41_release_slot(struct nfs4_slot *slot)
746 struct nfs4_session *session;
747 struct nfs4_slot_table *tbl;
748 bool send_new_highest_used_slotid = false;
753 session = tbl->session;
755 /* Bump the slot sequence number */
760 spin_lock(&tbl->slot_tbl_lock);
761 /* Be nice to the server: try to ensure that the last transmitted
762 * value for highest_user_slotid <= target_highest_slotid
764 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
765 send_new_highest_used_slotid = true;
767 if (nfs41_wake_and_assign_slot(tbl, slot)) {
768 send_new_highest_used_slotid = false;
771 nfs4_free_slot(tbl, slot);
773 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
774 send_new_highest_used_slotid = false;
776 spin_unlock(&tbl->slot_tbl_lock);
777 if (send_new_highest_used_slotid)
778 nfs41_notify_server(session->clp);
779 if (waitqueue_active(&tbl->slot_waitq))
780 wake_up_all(&tbl->slot_waitq);
783 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
785 nfs41_release_slot(res->sr_slot);
789 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
792 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
793 slot->seq_nr_highest_sent = seqnr;
795 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot, u32 seqnr)
797 nfs4_slot_sequence_record_sent(slot, seqnr);
798 slot->seq_nr_last_acked = seqnr;
801 static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
802 struct nfs4_slot *slot)
804 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
806 rpc_put_task_async(task);
809 static int nfs41_sequence_process(struct rpc_task *task,
810 struct nfs4_sequence_res *res)
812 struct nfs4_session *session;
813 struct nfs4_slot *slot = res->sr_slot;
814 struct nfs_client *clp;
820 /* don't increment the sequence number if the task wasn't sent */
821 if (!RPC_WAS_SENT(task) || slot->seq_done)
824 session = slot->table->session;
827 trace_nfs4_sequence_done(session, res);
829 status = res->sr_status;
830 if (task->tk_status == -NFS4ERR_DEADSESSION)
831 status = -NFS4ERR_DEADSESSION;
833 /* Check the SEQUENCE operation status */
836 /* Mark this sequence number as having been acked */
837 nfs4_slot_sequence_acked(slot, slot->seq_nr);
838 /* Update the slot's sequence and clientid lease timer */
840 do_renew_lease(clp, res->sr_timestamp);
841 /* Check sequence flags */
842 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
844 nfs41_update_target_slotid(slot->table, slot, res);
848 * sr_status remains 1 if an RPC level error occurred.
849 * The server may or may not have processed the sequence
852 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
856 /* The server detected a resend of the RPC call and
857 * returned NFS4ERR_DELAY as per Section 2.10.6.2
860 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
865 case -NFS4ERR_RETRY_UNCACHED_REP:
866 case -NFS4ERR_SEQ_FALSE_RETRY:
868 * The server thinks we tried to replay a request.
869 * Retry the call after bumping the sequence ID.
871 nfs4_slot_sequence_acked(slot, slot->seq_nr);
873 case -NFS4ERR_BADSLOT:
875 * The slot id we used was probably retired. Try again
876 * using a different slot id.
878 if (slot->slot_nr < slot->table->target_highest_slotid)
879 goto session_recover;
881 case -NFS4ERR_SEQ_MISORDERED:
882 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
884 * Were one or more calls using this slot interrupted?
885 * If the server never received the request, then our
886 * transmitted slot sequence number may be too high. However,
887 * if the server did receive the request then it might
888 * accidentally give us a reply with a mismatched operation.
889 * We can sort this out by sending a lone sequence operation
890 * to the server on the same slot.
892 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
894 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
895 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
902 * A retry might be sent while the original request is
903 * still in progress on the replier. The replier SHOULD
904 * deal with the issue by returning NFS4ERR_DELAY as the
905 * reply to SEQUENCE or CB_SEQUENCE operation, but
906 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
908 * Restart the search after a delay.
910 slot->seq_nr = slot->seq_nr_highest_sent;
912 case -NFS4ERR_BADSESSION:
913 case -NFS4ERR_DEADSESSION:
914 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
915 goto session_recover;
917 /* Just update the slot sequence no. */
921 /* The session may be reset by one of the error handlers. */
922 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
926 set_bit(NFS4_SLOT_TBL_DRAINING, &session->fc_slot_table.slot_tbl_state);
927 nfs4_schedule_session_recovery(session, status);
928 dprintk("%s ERROR: %d Reset session\n", __func__, status);
929 nfs41_sequence_free_slot(res);
934 if (rpc_restart_call_prepare(task)) {
935 nfs41_sequence_free_slot(res);
941 if (!rpc_restart_call(task))
943 rpc_delay(task, NFS4_POLL_RETRY_MAX);
947 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
949 if (!nfs41_sequence_process(task, res))
951 if (res->sr_slot != NULL)
952 nfs41_sequence_free_slot(res);
956 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
958 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
960 if (res->sr_slot == NULL)
962 if (res->sr_slot->table->session != NULL)
963 return nfs41_sequence_process(task, res);
964 return nfs40_sequence_done(task, res);
967 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
969 if (res->sr_slot != NULL) {
970 if (res->sr_slot->table->session != NULL)
971 nfs41_sequence_free_slot(res);
973 nfs40_sequence_free_slot(res);
977 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
979 if (res->sr_slot == NULL)
981 if (!res->sr_slot->table->session)
982 return nfs40_sequence_done(task, res);
983 return nfs41_sequence_done(task, res);
985 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
987 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
989 struct nfs4_call_sync_data *data = calldata;
991 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
993 nfs4_setup_sequence(data->seq_server->nfs_client,
994 data->seq_args, data->seq_res, task);
997 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
999 struct nfs4_call_sync_data *data = calldata;
1001 nfs41_sequence_done(task, data->seq_res);
1004 static const struct rpc_call_ops nfs41_call_sync_ops = {
1005 .rpc_call_prepare = nfs41_call_sync_prepare,
1006 .rpc_call_done = nfs41_call_sync_done,
1009 #else /* !CONFIG_NFS_V4_1 */
1011 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1013 return nfs40_sequence_done(task, res);
1016 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1018 if (res->sr_slot != NULL)
1019 nfs40_sequence_free_slot(res);
1022 int nfs4_sequence_done(struct rpc_task *task,
1023 struct nfs4_sequence_res *res)
1025 return nfs40_sequence_done(task, res);
1027 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1029 #endif /* !CONFIG_NFS_V4_1 */
1031 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1033 res->sr_timestamp = jiffies;
1034 res->sr_status_flags = 0;
1039 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1040 struct nfs4_sequence_res *res,
1041 struct nfs4_slot *slot)
1045 slot->privileged = args->sa_privileged ? 1 : 0;
1046 args->sa_slot = slot;
1048 res->sr_slot = slot;
1051 int nfs4_setup_sequence(struct nfs_client *client,
1052 struct nfs4_sequence_args *args,
1053 struct nfs4_sequence_res *res,
1054 struct rpc_task *task)
1056 struct nfs4_session *session = nfs4_get_session(client);
1057 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1058 struct nfs4_slot *slot;
1060 /* slot already allocated? */
1061 if (res->sr_slot != NULL)
1065 tbl = &session->fc_slot_table;
1067 spin_lock(&tbl->slot_tbl_lock);
1068 /* The state manager will wait until the slot table is empty */
1069 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1072 slot = nfs4_alloc_slot(tbl);
1074 if (slot == ERR_PTR(-ENOMEM))
1075 goto out_sleep_timeout;
1078 spin_unlock(&tbl->slot_tbl_lock);
1080 nfs4_sequence_attach_slot(args, res, slot);
1082 trace_nfs4_setup_sequence(session, args);
1084 nfs41_sequence_res_init(res);
1085 rpc_call_start(task);
1088 /* Try again in 1/4 second */
1089 if (args->sa_privileged)
1090 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1091 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1093 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1094 NULL, jiffies + (HZ >> 2));
1095 spin_unlock(&tbl->slot_tbl_lock);
1098 if (args->sa_privileged)
1099 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1100 RPC_PRIORITY_PRIVILEGED);
1102 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1103 spin_unlock(&tbl->slot_tbl_lock);
1106 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1108 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1110 struct nfs4_call_sync_data *data = calldata;
1111 nfs4_setup_sequence(data->seq_server->nfs_client,
1112 data->seq_args, data->seq_res, task);
1115 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1117 struct nfs4_call_sync_data *data = calldata;
1118 nfs4_sequence_done(task, data->seq_res);
1121 static const struct rpc_call_ops nfs40_call_sync_ops = {
1122 .rpc_call_prepare = nfs40_call_sync_prepare,
1123 .rpc_call_done = nfs40_call_sync_done,
1126 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1129 struct rpc_task *task;
1131 task = rpc_run_task(task_setup);
1133 return PTR_ERR(task);
1135 ret = task->tk_status;
1140 static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1141 struct nfs_server *server,
1142 struct rpc_message *msg,
1143 struct nfs4_sequence_args *args,
1144 struct nfs4_sequence_res *res,
1145 unsigned short task_flags)
1147 struct nfs_client *clp = server->nfs_client;
1148 struct nfs4_call_sync_data data = {
1149 .seq_server = server,
1153 struct rpc_task_setup task_setup = {
1156 .callback_ops = clp->cl_mvops->call_sync_ops,
1157 .callback_data = &data,
1158 .flags = task_flags,
1161 return nfs4_call_sync_custom(&task_setup);
1164 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1165 struct nfs_server *server,
1166 struct rpc_message *msg,
1167 struct nfs4_sequence_args *args,
1168 struct nfs4_sequence_res *res)
1170 unsigned short task_flags = 0;
1172 if (server->caps & NFS_CAP_MOVEABLE)
1173 task_flags = RPC_TASK_MOVEABLE;
1174 return nfs4_do_call_sync(clnt, server, msg, args, res, task_flags);
1178 int nfs4_call_sync(struct rpc_clnt *clnt,
1179 struct nfs_server *server,
1180 struct rpc_message *msg,
1181 struct nfs4_sequence_args *args,
1182 struct nfs4_sequence_res *res,
1185 nfs4_init_sequence(args, res, cache_reply, 0);
1186 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1190 nfs4_inc_nlink_locked(struct inode *inode)
1192 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1193 NFS_INO_INVALID_CTIME |
1194 NFS_INO_INVALID_NLINK);
1199 nfs4_inc_nlink(struct inode *inode)
1201 spin_lock(&inode->i_lock);
1202 nfs4_inc_nlink_locked(inode);
1203 spin_unlock(&inode->i_lock);
1207 nfs4_dec_nlink_locked(struct inode *inode)
1209 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1210 NFS_INO_INVALID_CTIME |
1211 NFS_INO_INVALID_NLINK);
1216 nfs4_update_changeattr_locked(struct inode *inode,
1217 struct nfs4_change_info *cinfo,
1218 unsigned long timestamp, unsigned long cache_validity)
1220 struct nfs_inode *nfsi = NFS_I(inode);
1221 u64 change_attr = inode_peek_iversion_raw(inode);
1223 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
1224 if (S_ISDIR(inode->i_mode))
1225 cache_validity |= NFS_INO_INVALID_DATA;
1227 switch (NFS_SERVER(inode)->change_attr_type) {
1228 case NFS4_CHANGE_TYPE_IS_UNDEFINED:
1229 if (cinfo->after == change_attr)
1233 if ((s64)(change_attr - cinfo->after) >= 0)
1237 inode_set_iversion_raw(inode, cinfo->after);
1238 if (!cinfo->atomic || cinfo->before != change_attr) {
1239 if (S_ISDIR(inode->i_mode))
1240 nfs_force_lookup_revalidate(inode);
1242 if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1244 NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
1245 NFS_INO_INVALID_SIZE | NFS_INO_INVALID_OTHER |
1246 NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
1247 NFS_INO_INVALID_MODE | NFS_INO_INVALID_XATTR;
1248 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1250 nfsi->attrtimeo_timestamp = jiffies;
1251 nfsi->read_cache_jiffies = timestamp;
1252 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1253 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1255 nfs_set_cache_invalid(inode, cache_validity);
1259 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1260 unsigned long timestamp, unsigned long cache_validity)
1262 spin_lock(&dir->i_lock);
1263 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1264 spin_unlock(&dir->i_lock);
1267 struct nfs4_open_createattrs {
1268 struct nfs4_label *label;
1269 struct iattr *sattr;
1270 const __u32 verf[2];
1273 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1274 int err, struct nfs4_exception *exception)
1278 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1280 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1281 exception->retry = 1;
1285 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1287 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1290 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1292 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1294 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1298 nfs4_map_atomic_open_share(struct nfs_server *server,
1299 fmode_t fmode, int openflags)
1303 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1305 res = NFS4_SHARE_ACCESS_READ;
1308 res = NFS4_SHARE_ACCESS_WRITE;
1310 case FMODE_READ|FMODE_WRITE:
1311 res = NFS4_SHARE_ACCESS_BOTH;
1313 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1315 /* Want no delegation if we're using O_DIRECT */
1316 if (openflags & O_DIRECT)
1317 res |= NFS4_SHARE_WANT_NO_DELEG;
1322 static enum open_claim_type4
1323 nfs4_map_atomic_open_claim(struct nfs_server *server,
1324 enum open_claim_type4 claim)
1326 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1331 case NFS4_OPEN_CLAIM_FH:
1332 return NFS4_OPEN_CLAIM_NULL;
1333 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1334 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1335 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1336 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1340 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1342 p->o_res.f_attr = &p->f_attr;
1343 p->o_res.f_label = p->f_label;
1344 p->o_res.seqid = p->o_arg.seqid;
1345 p->c_res.seqid = p->c_arg.seqid;
1346 p->o_res.server = p->o_arg.server;
1347 p->o_res.access_request = p->o_arg.access;
1348 nfs_fattr_init(&p->f_attr);
1349 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1352 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1353 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1354 const struct nfs4_open_createattrs *c,
1355 enum open_claim_type4 claim,
1358 struct dentry *parent = dget_parent(dentry);
1359 struct inode *dir = d_inode(parent);
1360 struct nfs_server *server = NFS_SERVER(dir);
1361 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1362 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1363 struct nfs4_opendata *p;
1365 p = kzalloc(sizeof(*p), gfp_mask);
1369 p->f_label = nfs4_label_alloc(server, gfp_mask);
1370 if (IS_ERR(p->f_label))
1373 p->a_label = nfs4_label_alloc(server, gfp_mask);
1374 if (IS_ERR(p->a_label))
1377 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1378 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1379 if (IS_ERR(p->o_arg.seqid))
1380 goto err_free_label;
1381 nfs_sb_active(dentry->d_sb);
1382 p->dentry = dget(dentry);
1385 atomic_inc(&sp->so_count);
1386 p->o_arg.open_flags = flags;
1387 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1388 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1389 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1391 if (flags & O_CREAT) {
1392 p->o_arg.umask = current_umask();
1393 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1394 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1395 p->o_arg.u.attrs = &p->attrs;
1396 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1398 memcpy(p->o_arg.u.verifier.data, c->verf,
1399 sizeof(p->o_arg.u.verifier.data));
1402 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1403 * will return permission denied for all bits until close */
1404 if (!(flags & O_EXCL)) {
1405 /* ask server to check for all possible rights as results
1407 switch (p->o_arg.claim) {
1410 case NFS4_OPEN_CLAIM_NULL:
1411 case NFS4_OPEN_CLAIM_FH:
1412 p->o_arg.access = NFS4_ACCESS_READ |
1413 NFS4_ACCESS_MODIFY |
1414 NFS4_ACCESS_EXTEND |
1415 NFS4_ACCESS_EXECUTE;
1416 #ifdef CONFIG_NFS_V4_2
1417 if (server->caps & NFS_CAP_XATTR)
1418 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1419 NFS4_ACCESS_XAWRITE |
1424 p->o_arg.clientid = server->nfs_client->cl_clientid;
1425 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1426 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1427 p->o_arg.name = &dentry->d_name;
1428 p->o_arg.server = server;
1429 p->o_arg.bitmask = nfs4_bitmask(server, label);
1430 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1431 switch (p->o_arg.claim) {
1432 case NFS4_OPEN_CLAIM_NULL:
1433 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1434 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1435 p->o_arg.fh = NFS_FH(dir);
1437 case NFS4_OPEN_CLAIM_PREVIOUS:
1438 case NFS4_OPEN_CLAIM_FH:
1439 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1440 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1441 p->o_arg.fh = NFS_FH(d_inode(dentry));
1443 p->c_arg.fh = &p->o_res.fh;
1444 p->c_arg.stateid = &p->o_res.stateid;
1445 p->c_arg.seqid = p->o_arg.seqid;
1446 nfs4_init_opendata_res(p);
1447 kref_init(&p->kref);
1451 nfs4_label_free(p->a_label);
1453 nfs4_label_free(p->f_label);
1461 static void nfs4_opendata_free(struct kref *kref)
1463 struct nfs4_opendata *p = container_of(kref,
1464 struct nfs4_opendata, kref);
1465 struct super_block *sb = p->dentry->d_sb;
1467 nfs4_lgopen_release(p->lgp);
1468 nfs_free_seqid(p->o_arg.seqid);
1469 nfs4_sequence_free_slot(&p->o_res.seq_res);
1470 if (p->state != NULL)
1471 nfs4_put_open_state(p->state);
1472 nfs4_put_state_owner(p->owner);
1474 nfs4_label_free(p->a_label);
1475 nfs4_label_free(p->f_label);
1479 nfs_sb_deactive(sb);
1480 nfs_fattr_free_names(&p->f_attr);
1481 kfree(p->f_attr.mdsthreshold);
1485 static void nfs4_opendata_put(struct nfs4_opendata *p)
1488 kref_put(&p->kref, nfs4_opendata_free);
1491 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1494 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1495 case FMODE_READ|FMODE_WRITE:
1496 return state->n_rdwr != 0;
1498 return state->n_wronly != 0;
1500 return state->n_rdonly != 0;
1506 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1507 int open_mode, enum open_claim_type4 claim)
1511 if (open_mode & (O_EXCL|O_TRUNC))
1514 case NFS4_OPEN_CLAIM_NULL:
1515 case NFS4_OPEN_CLAIM_FH:
1520 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1522 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1523 && state->n_rdonly != 0;
1526 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1527 && state->n_wronly != 0;
1529 case FMODE_READ|FMODE_WRITE:
1530 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1531 && state->n_rdwr != 0;
1537 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1538 enum open_claim_type4 claim)
1540 if (delegation == NULL)
1542 if ((delegation->type & fmode) != fmode)
1545 case NFS4_OPEN_CLAIM_NULL:
1546 case NFS4_OPEN_CLAIM_FH:
1548 case NFS4_OPEN_CLAIM_PREVIOUS:
1549 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1555 nfs_mark_delegation_referenced(delegation);
1559 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1568 case FMODE_READ|FMODE_WRITE:
1571 nfs4_state_set_mode_locked(state, state->state | fmode);
1574 #ifdef CONFIG_NFS_V4_1
1575 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1577 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1579 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1581 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1585 #endif /* CONFIG_NFS_V4_1 */
1587 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1589 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1590 wake_up_all(&state->waitq);
1593 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1595 struct nfs_client *clp = state->owner->so_server->nfs_client;
1596 bool need_recover = false;
1598 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1599 need_recover = true;
1600 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1601 need_recover = true;
1602 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1603 need_recover = true;
1605 nfs4_state_mark_reclaim_nograce(clp, state);
1609 * Check for whether or not the caller may update the open stateid
1610 * to the value passed in by stateid.
1612 * Note: This function relies heavily on the server implementing
1613 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1615 * i.e. The stateid seqids have to be initialised to 1, and
1616 * are then incremented on every state transition.
1618 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1619 const nfs4_stateid *stateid)
1621 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1622 /* The common case - we're updating to a new sequence number */
1623 if (nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1624 if (nfs4_stateid_is_next(&state->open_stateid, stateid))
1628 /* The server returned a new stateid */
1630 /* This is the first OPEN in this generation */
1631 if (stateid->seqid == cpu_to_be32(1))
1636 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1638 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1640 if (state->n_wronly)
1641 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1642 if (state->n_rdonly)
1643 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1645 set_bit(NFS_O_RDWR_STATE, &state->flags);
1646 set_bit(NFS_OPEN_STATE, &state->flags);
1649 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1650 nfs4_stateid *stateid, fmode_t fmode)
1652 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1653 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1655 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1658 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1661 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1662 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1663 clear_bit(NFS_OPEN_STATE, &state->flags);
1665 if (stateid == NULL)
1667 /* Handle OPEN+OPEN_DOWNGRADE races */
1668 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1669 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1670 nfs_resync_open_stateid_locked(state);
1673 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1674 nfs4_stateid_copy(&state->stateid, stateid);
1675 nfs4_stateid_copy(&state->open_stateid, stateid);
1676 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1678 nfs_state_log_update_open_stateid(state);
1681 static void nfs_clear_open_stateid(struct nfs4_state *state,
1682 nfs4_stateid *arg_stateid,
1683 nfs4_stateid *stateid, fmode_t fmode)
1685 write_seqlock(&state->seqlock);
1686 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1687 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1688 nfs_clear_open_stateid_locked(state, stateid, fmode);
1689 write_sequnlock(&state->seqlock);
1690 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1691 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1694 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1695 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1696 __must_hold(&state->owner->so_lock)
1697 __must_hold(&state->seqlock)
1705 if (nfs_stateid_is_sequential(state, stateid))
1710 /* Rely on seqids for serialisation with NFSv4.0 */
1711 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1714 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1715 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1717 * Ensure we process the state changes in the same order
1718 * in which the server processed them by delaying the
1719 * update of the stateid until we are in sequence.
1721 write_sequnlock(&state->seqlock);
1722 spin_unlock(&state->owner->so_lock);
1724 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1726 if (!fatal_signal_pending(current)) {
1727 if (schedule_timeout(5*HZ) == 0)
1733 finish_wait(&state->waitq, &wait);
1735 spin_lock(&state->owner->so_lock);
1736 write_seqlock(&state->seqlock);
1739 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1740 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1741 nfs4_stateid_copy(freeme, &state->open_stateid);
1742 nfs_test_and_clear_all_open_stateid(state);
1745 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1746 nfs4_stateid_copy(&state->stateid, stateid);
1747 nfs4_stateid_copy(&state->open_stateid, stateid);
1748 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1749 nfs_state_log_update_open_stateid(state);
1752 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1753 const nfs4_stateid *open_stateid,
1755 nfs4_stateid *freeme)
1758 * Protect the call to nfs4_state_set_mode_locked and
1759 * serialise the stateid update
1761 write_seqlock(&state->seqlock);
1762 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1765 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1768 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1770 case FMODE_READ|FMODE_WRITE:
1771 set_bit(NFS_O_RDWR_STATE, &state->flags);
1773 set_bit(NFS_OPEN_STATE, &state->flags);
1774 write_sequnlock(&state->seqlock);
1777 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1779 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1780 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1781 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1782 clear_bit(NFS_OPEN_STATE, &state->flags);
1785 static void nfs_state_set_delegation(struct nfs4_state *state,
1786 const nfs4_stateid *deleg_stateid,
1790 * Protect the call to nfs4_state_set_mode_locked and
1791 * serialise the stateid update
1793 write_seqlock(&state->seqlock);
1794 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1795 set_bit(NFS_DELEGATED_STATE, &state->flags);
1796 write_sequnlock(&state->seqlock);
1799 static void nfs_state_clear_delegation(struct nfs4_state *state)
1801 write_seqlock(&state->seqlock);
1802 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1803 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1804 write_sequnlock(&state->seqlock);
1807 int update_open_stateid(struct nfs4_state *state,
1808 const nfs4_stateid *open_stateid,
1809 const nfs4_stateid *delegation,
1812 struct nfs_server *server = NFS_SERVER(state->inode);
1813 struct nfs_client *clp = server->nfs_client;
1814 struct nfs_inode *nfsi = NFS_I(state->inode);
1815 struct nfs_delegation *deleg_cur;
1816 nfs4_stateid freeme = { };
1819 fmode &= (FMODE_READ|FMODE_WRITE);
1822 spin_lock(&state->owner->so_lock);
1823 if (open_stateid != NULL) {
1824 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1828 deleg_cur = nfs4_get_valid_delegation(state->inode);
1829 if (deleg_cur == NULL)
1832 spin_lock(&deleg_cur->lock);
1833 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1834 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1835 (deleg_cur->type & fmode) != fmode)
1836 goto no_delegation_unlock;
1838 if (delegation == NULL)
1839 delegation = &deleg_cur->stateid;
1840 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1841 goto no_delegation_unlock;
1843 nfs_mark_delegation_referenced(deleg_cur);
1844 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1846 no_delegation_unlock:
1847 spin_unlock(&deleg_cur->lock);
1850 update_open_stateflags(state, fmode);
1851 spin_unlock(&state->owner->so_lock);
1854 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1855 nfs4_schedule_state_manager(clp);
1856 if (freeme.type != 0)
1857 nfs4_test_and_free_stateid(server, &freeme,
1858 state->owner->so_cred);
1863 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1864 const nfs4_stateid *stateid)
1866 struct nfs4_state *state = lsp->ls_state;
1869 spin_lock(&state->state_lock);
1870 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1872 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1874 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1877 spin_unlock(&state->state_lock);
1881 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1883 struct nfs_delegation *delegation;
1885 fmode &= FMODE_READ|FMODE_WRITE;
1887 delegation = nfs4_get_valid_delegation(inode);
1888 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1893 nfs4_inode_return_delegation(inode);
1896 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1898 struct nfs4_state *state = opendata->state;
1899 struct nfs_delegation *delegation;
1900 int open_mode = opendata->o_arg.open_flags;
1901 fmode_t fmode = opendata->o_arg.fmode;
1902 enum open_claim_type4 claim = opendata->o_arg.claim;
1903 nfs4_stateid stateid;
1907 spin_lock(&state->owner->so_lock);
1908 if (can_open_cached(state, fmode, open_mode, claim)) {
1909 update_open_stateflags(state, fmode);
1910 spin_unlock(&state->owner->so_lock);
1911 goto out_return_state;
1913 spin_unlock(&state->owner->so_lock);
1915 delegation = nfs4_get_valid_delegation(state->inode);
1916 if (!can_open_delegated(delegation, fmode, claim)) {
1920 /* Save the delegation */
1921 nfs4_stateid_copy(&stateid, &delegation->stateid);
1923 nfs_release_seqid(opendata->o_arg.seqid);
1924 if (!opendata->is_recover) {
1925 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1931 /* Try to update the stateid using the delegation */
1932 if (update_open_stateid(state, NULL, &stateid, fmode))
1933 goto out_return_state;
1936 return ERR_PTR(ret);
1938 refcount_inc(&state->count);
1943 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1945 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1946 struct nfs_delegation *delegation;
1947 int delegation_flags = 0;
1950 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1952 delegation_flags = delegation->flags;
1954 switch (data->o_arg.claim) {
1957 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1958 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1959 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1960 "returning a delegation for "
1961 "OPEN(CLAIM_DELEGATE_CUR)\n",
1965 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1966 nfs_inode_set_delegation(state->inode,
1967 data->owner->so_cred,
1968 data->o_res.delegation_type,
1969 &data->o_res.delegation,
1970 data->o_res.pagemod_limit);
1972 nfs_inode_reclaim_delegation(state->inode,
1973 data->owner->so_cred,
1974 data->o_res.delegation_type,
1975 &data->o_res.delegation,
1976 data->o_res.pagemod_limit);
1978 if (data->o_res.do_recall)
1979 nfs_async_inode_return_delegation(state->inode,
1980 &data->o_res.delegation);
1984 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1985 * and update the nfs4_state.
1987 static struct nfs4_state *
1988 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1990 struct inode *inode = data->state->inode;
1991 struct nfs4_state *state = data->state;
1994 if (!data->rpc_done) {
1995 if (data->rpc_status)
1996 return ERR_PTR(data->rpc_status);
1997 return nfs4_try_open_cached(data);
2000 ret = nfs_refresh_inode(inode, &data->f_attr);
2002 return ERR_PTR(ret);
2004 if (data->o_res.delegation_type != 0)
2005 nfs4_opendata_check_deleg(data, state);
2007 if (!update_open_stateid(state, &data->o_res.stateid,
2008 NULL, data->o_arg.fmode))
2009 return ERR_PTR(-EAGAIN);
2010 refcount_inc(&state->count);
2015 static struct inode *
2016 nfs4_opendata_get_inode(struct nfs4_opendata *data)
2018 struct inode *inode;
2020 switch (data->o_arg.claim) {
2021 case NFS4_OPEN_CLAIM_NULL:
2022 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2023 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2024 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2025 return ERR_PTR(-EAGAIN);
2026 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2027 &data->f_attr, data->f_label);
2030 inode = d_inode(data->dentry);
2032 nfs_refresh_inode(inode, &data->f_attr);
2037 static struct nfs4_state *
2038 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2040 struct nfs4_state *state;
2041 struct inode *inode;
2043 inode = nfs4_opendata_get_inode(data);
2045 return ERR_CAST(inode);
2046 if (data->state != NULL && data->state->inode == inode) {
2047 state = data->state;
2048 refcount_inc(&state->count);
2050 state = nfs4_get_open_state(inode, data->owner);
2053 state = ERR_PTR(-ENOMEM);
2057 static struct nfs4_state *
2058 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2060 struct nfs4_state *state;
2062 if (!data->rpc_done) {
2063 state = nfs4_try_open_cached(data);
2064 trace_nfs4_cached_open(data->state);
2068 state = nfs4_opendata_find_nfs4_state(data);
2072 if (data->o_res.delegation_type != 0)
2073 nfs4_opendata_check_deleg(data, state);
2074 if (!update_open_stateid(state, &data->o_res.stateid,
2075 NULL, data->o_arg.fmode)) {
2076 nfs4_put_open_state(state);
2077 state = ERR_PTR(-EAGAIN);
2080 nfs_release_seqid(data->o_arg.seqid);
2084 static struct nfs4_state *
2085 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2087 struct nfs4_state *ret;
2089 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2090 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2092 ret = _nfs4_opendata_to_nfs4_state(data);
2093 nfs4_sequence_free_slot(&data->o_res.seq_res);
2097 static struct nfs_open_context *
2098 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2100 struct nfs_inode *nfsi = NFS_I(state->inode);
2101 struct nfs_open_context *ctx;
2104 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2105 if (ctx->state != state)
2107 if ((ctx->mode & mode) != mode)
2109 if (!get_nfs_open_context(ctx))
2115 return ERR_PTR(-ENOENT);
2118 static struct nfs_open_context *
2119 nfs4_state_find_open_context(struct nfs4_state *state)
2121 struct nfs_open_context *ctx;
2123 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2126 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2129 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2132 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2133 struct nfs4_state *state, enum open_claim_type4 claim)
2135 struct nfs4_opendata *opendata;
2137 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2138 NULL, claim, GFP_NOFS);
2139 if (opendata == NULL)
2140 return ERR_PTR(-ENOMEM);
2141 opendata->state = state;
2142 refcount_inc(&state->count);
2146 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2149 struct nfs4_state *newstate;
2150 struct nfs_server *server = NFS_SB(opendata->dentry->d_sb);
2151 int openflags = opendata->o_arg.open_flags;
2154 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2156 opendata->o_arg.fmode = fmode;
2157 opendata->o_arg.share_access =
2158 nfs4_map_atomic_open_share(server, fmode, openflags);
2159 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2160 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2161 nfs4_init_opendata_res(opendata);
2162 ret = _nfs4_recover_proc_open(opendata);
2165 newstate = nfs4_opendata_to_nfs4_state(opendata);
2166 if (IS_ERR(newstate))
2167 return PTR_ERR(newstate);
2168 if (newstate != opendata->state)
2170 nfs4_close_state(newstate, fmode);
2174 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2178 /* memory barrier prior to reading state->n_* */
2180 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2183 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2186 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2190 * We may have performed cached opens for all three recoveries.
2191 * Check if we need to update the current stateid.
2193 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2194 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2195 write_seqlock(&state->seqlock);
2196 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2197 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2198 write_sequnlock(&state->seqlock);
2205 * reclaim state on the server after a reboot.
2207 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2209 struct nfs_delegation *delegation;
2210 struct nfs4_opendata *opendata;
2211 fmode_t delegation_type = 0;
2214 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2215 NFS4_OPEN_CLAIM_PREVIOUS);
2216 if (IS_ERR(opendata))
2217 return PTR_ERR(opendata);
2219 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2220 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2221 delegation_type = delegation->type;
2223 opendata->o_arg.u.delegation_type = delegation_type;
2224 status = nfs4_open_recover(opendata, state);
2225 nfs4_opendata_put(opendata);
2229 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2231 struct nfs_server *server = NFS_SERVER(state->inode);
2232 struct nfs4_exception exception = { };
2235 err = _nfs4_do_open_reclaim(ctx, state);
2236 trace_nfs4_open_reclaim(ctx, 0, err);
2237 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2239 if (err != -NFS4ERR_DELAY)
2241 nfs4_handle_exception(server, err, &exception);
2242 } while (exception.retry);
2246 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2248 struct nfs_open_context *ctx;
2251 ctx = nfs4_state_find_open_context(state);
2254 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2255 nfs_state_clear_open_state_flags(state);
2256 ret = nfs4_do_open_reclaim(ctx, state);
2257 put_nfs_open_context(ctx);
2261 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)
2265 printk(KERN_ERR "NFS: %s: unhandled error "
2266 "%d.\n", __func__, err);
2274 case -NFS4ERR_BADSESSION:
2275 case -NFS4ERR_BADSLOT:
2276 case -NFS4ERR_BAD_HIGH_SLOT:
2277 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2278 case -NFS4ERR_DEADSESSION:
2280 case -NFS4ERR_STALE_CLIENTID:
2281 case -NFS4ERR_STALE_STATEID:
2282 /* Don't recall a delegation if it was lost */
2283 nfs4_schedule_lease_recovery(server->nfs_client);
2285 case -NFS4ERR_MOVED:
2286 nfs4_schedule_migration_recovery(server);
2288 case -NFS4ERR_LEASE_MOVED:
2289 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2291 case -NFS4ERR_DELEG_REVOKED:
2292 case -NFS4ERR_ADMIN_REVOKED:
2293 case -NFS4ERR_EXPIRED:
2294 case -NFS4ERR_BAD_STATEID:
2295 case -NFS4ERR_OPENMODE:
2296 nfs_inode_find_state_and_recover(state->inode,
2298 nfs4_schedule_stateid_recovery(server, state);
2300 case -NFS4ERR_DELAY:
2301 case -NFS4ERR_GRACE:
2305 case -NFS4ERR_DENIED:
2307 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2309 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2316 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2317 struct nfs4_state *state, const nfs4_stateid *stateid)
2319 struct nfs_server *server = NFS_SERVER(state->inode);
2320 struct nfs4_opendata *opendata;
2323 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2324 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2325 if (IS_ERR(opendata))
2326 return PTR_ERR(opendata);
2327 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2328 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2329 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2333 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2334 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2338 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2339 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2343 nfs_state_clear_delegation(state);
2345 nfs4_opendata_put(opendata);
2346 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2349 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2351 struct nfs4_opendata *data = calldata;
2353 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2354 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2357 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2359 struct nfs4_opendata *data = calldata;
2361 nfs40_sequence_done(task, &data->c_res.seq_res);
2363 data->rpc_status = task->tk_status;
2364 if (data->rpc_status == 0) {
2365 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2366 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2367 renew_lease(data->o_res.server, data->timestamp);
2368 data->rpc_done = true;
2372 static void nfs4_open_confirm_release(void *calldata)
2374 struct nfs4_opendata *data = calldata;
2375 struct nfs4_state *state = NULL;
2377 /* If this request hasn't been cancelled, do nothing */
2378 if (!data->cancelled)
2380 /* In case of error, no cleanup! */
2381 if (!data->rpc_done)
2383 state = nfs4_opendata_to_nfs4_state(data);
2385 nfs4_close_state(state, data->o_arg.fmode);
2387 nfs4_opendata_put(data);
2390 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2391 .rpc_call_prepare = nfs4_open_confirm_prepare,
2392 .rpc_call_done = nfs4_open_confirm_done,
2393 .rpc_release = nfs4_open_confirm_release,
2397 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2399 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2401 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2402 struct rpc_task *task;
2403 struct rpc_message msg = {
2404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2405 .rpc_argp = &data->c_arg,
2406 .rpc_resp = &data->c_res,
2407 .rpc_cred = data->owner->so_cred,
2409 struct rpc_task_setup task_setup_data = {
2410 .rpc_client = server->client,
2411 .rpc_message = &msg,
2412 .callback_ops = &nfs4_open_confirm_ops,
2413 .callback_data = data,
2414 .workqueue = nfsiod_workqueue,
2415 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2419 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2421 kref_get(&data->kref);
2422 data->rpc_done = false;
2423 data->rpc_status = 0;
2424 data->timestamp = jiffies;
2425 task = rpc_run_task(&task_setup_data);
2427 return PTR_ERR(task);
2428 status = rpc_wait_for_completion_task(task);
2430 data->cancelled = true;
2433 status = data->rpc_status;
2438 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2440 struct nfs4_opendata *data = calldata;
2441 struct nfs4_state_owner *sp = data->owner;
2442 struct nfs_client *clp = sp->so_server->nfs_client;
2443 enum open_claim_type4 claim = data->o_arg.claim;
2445 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2448 * Check if we still need to send an OPEN call, or if we can use
2449 * a delegation instead.
2451 if (data->state != NULL) {
2452 struct nfs_delegation *delegation;
2454 if (can_open_cached(data->state, data->o_arg.fmode,
2455 data->o_arg.open_flags, claim))
2458 delegation = nfs4_get_valid_delegation(data->state->inode);
2459 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2460 goto unlock_no_action;
2463 /* Update client id. */
2464 data->o_arg.clientid = clp->cl_clientid;
2468 case NFS4_OPEN_CLAIM_PREVIOUS:
2469 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2470 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2471 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2473 case NFS4_OPEN_CLAIM_FH:
2474 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2476 data->timestamp = jiffies;
2477 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2478 &data->o_arg.seq_args,
2479 &data->o_res.seq_res,
2481 nfs_release_seqid(data->o_arg.seqid);
2483 /* Set the create mode (note dependency on the session type) */
2484 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2485 if (data->o_arg.open_flags & O_EXCL) {
2486 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2487 if (nfs4_has_persistent_session(clp))
2488 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2489 else if (clp->cl_mvops->minor_version > 0)
2490 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2494 trace_nfs4_cached_open(data->state);
2497 task->tk_action = NULL;
2499 nfs4_sequence_done(task, &data->o_res.seq_res);
2502 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2504 struct nfs4_opendata *data = calldata;
2506 data->rpc_status = task->tk_status;
2508 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2511 if (task->tk_status == 0) {
2512 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2513 switch (data->o_res.f_attr->mode & S_IFMT) {
2517 data->rpc_status = -ELOOP;
2520 data->rpc_status = -EISDIR;
2523 data->rpc_status = -ENOTDIR;
2526 renew_lease(data->o_res.server, data->timestamp);
2527 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2528 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2530 data->rpc_done = true;
2533 static void nfs4_open_release(void *calldata)
2535 struct nfs4_opendata *data = calldata;
2536 struct nfs4_state *state = NULL;
2538 /* If this request hasn't been cancelled, do nothing */
2539 if (!data->cancelled)
2541 /* In case of error, no cleanup! */
2542 if (data->rpc_status != 0 || !data->rpc_done)
2544 /* In case we need an open_confirm, no cleanup! */
2545 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2547 state = nfs4_opendata_to_nfs4_state(data);
2549 nfs4_close_state(state, data->o_arg.fmode);
2551 nfs4_opendata_put(data);
2554 static const struct rpc_call_ops nfs4_open_ops = {
2555 .rpc_call_prepare = nfs4_open_prepare,
2556 .rpc_call_done = nfs4_open_done,
2557 .rpc_release = nfs4_open_release,
2560 static int nfs4_run_open_task(struct nfs4_opendata *data,
2561 struct nfs_open_context *ctx)
2563 struct inode *dir = d_inode(data->dir);
2564 struct nfs_server *server = NFS_SERVER(dir);
2565 struct nfs_openargs *o_arg = &data->o_arg;
2566 struct nfs_openres *o_res = &data->o_res;
2567 struct rpc_task *task;
2568 struct rpc_message msg = {
2569 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2572 .rpc_cred = data->owner->so_cred,
2574 struct rpc_task_setup task_setup_data = {
2575 .rpc_client = server->client,
2576 .rpc_message = &msg,
2577 .callback_ops = &nfs4_open_ops,
2578 .callback_data = data,
2579 .workqueue = nfsiod_workqueue,
2580 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2584 if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
2585 task_setup_data.flags |= RPC_TASK_MOVEABLE;
2587 kref_get(&data->kref);
2588 data->rpc_done = false;
2589 data->rpc_status = 0;
2590 data->cancelled = false;
2591 data->is_recover = false;
2593 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2594 data->is_recover = true;
2595 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2597 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2598 pnfs_lgopen_prepare(data, ctx);
2600 task = rpc_run_task(&task_setup_data);
2602 return PTR_ERR(task);
2603 status = rpc_wait_for_completion_task(task);
2605 data->cancelled = true;
2608 status = data->rpc_status;
2614 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2616 struct inode *dir = d_inode(data->dir);
2617 struct nfs_openres *o_res = &data->o_res;
2620 status = nfs4_run_open_task(data, NULL);
2621 if (status != 0 || !data->rpc_done)
2624 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2626 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2627 status = _nfs4_proc_open_confirm(data);
2633 * Additional permission checks in order to distinguish between an
2634 * open for read, and an open for execute. This works around the
2635 * fact that NFSv4 OPEN treats read and execute permissions as being
2637 * Note that in the non-execute case, we want to turn off permission
2638 * checking if we just created a new file (POSIX open() semantics).
2640 static int nfs4_opendata_access(const struct cred *cred,
2641 struct nfs4_opendata *opendata,
2642 struct nfs4_state *state, fmode_t fmode,
2645 struct nfs_access_entry cache;
2648 /* access call failed or for some reason the server doesn't
2649 * support any access modes -- defer access call until later */
2650 if (opendata->o_res.access_supported == 0)
2655 * Use openflags to check for exec, because fmode won't
2656 * always have FMODE_EXEC set when file open for exec.
2658 if (openflags & __FMODE_EXEC) {
2659 /* ONLY check for exec rights */
2660 if (S_ISDIR(state->inode->i_mode))
2661 mask = NFS4_ACCESS_LOOKUP;
2663 mask = NFS4_ACCESS_EXECUTE;
2664 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2665 mask = NFS4_ACCESS_READ;
2668 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2669 nfs_access_add_cache(state->inode, &cache);
2671 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2672 if ((mask & ~cache.mask & flags) == 0)
2679 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2681 static int _nfs4_proc_open(struct nfs4_opendata *data,
2682 struct nfs_open_context *ctx)
2684 struct inode *dir = d_inode(data->dir);
2685 struct nfs_server *server = NFS_SERVER(dir);
2686 struct nfs_openargs *o_arg = &data->o_arg;
2687 struct nfs_openres *o_res = &data->o_res;
2690 status = nfs4_run_open_task(data, ctx);
2691 if (!data->rpc_done)
2694 if (status == -NFS4ERR_BADNAME &&
2695 !(o_arg->open_flags & O_CREAT))
2700 nfs_fattr_map_and_free_names(server, &data->f_attr);
2702 if (o_arg->open_flags & O_CREAT) {
2703 if (o_arg->open_flags & O_EXCL)
2704 data->file_created = true;
2705 else if (o_res->cinfo.before != o_res->cinfo.after)
2706 data->file_created = true;
2707 if (data->file_created ||
2708 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2709 nfs4_update_changeattr(dir, &o_res->cinfo,
2710 o_res->f_attr->time_start,
2711 NFS_INO_INVALID_DATA);
2713 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2714 server->caps &= ~NFS_CAP_POSIX_LOCK;
2715 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2716 status = _nfs4_proc_open_confirm(data);
2720 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2721 nfs4_sequence_free_slot(&o_res->seq_res);
2722 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2723 o_res->f_label, NULL);
2730 * reclaim state on the server after a network partition.
2731 * Assumes caller holds the appropriate lock
2733 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2735 struct nfs4_opendata *opendata;
2738 opendata = nfs4_open_recoverdata_alloc(ctx, state, NFS4_OPEN_CLAIM_FH);
2739 if (IS_ERR(opendata))
2740 return PTR_ERR(opendata);
2742 * We're not recovering a delegation, so ask for no delegation.
2743 * Otherwise the recovery thread could deadlock with an outstanding
2744 * delegation return.
2746 opendata->o_arg.open_flags = O_DIRECT;
2747 ret = nfs4_open_recover(opendata, state);
2749 d_drop(ctx->dentry);
2750 nfs4_opendata_put(opendata);
2754 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2756 struct nfs_server *server = NFS_SERVER(state->inode);
2757 struct nfs4_exception exception = { };
2761 err = _nfs4_open_expired(ctx, state);
2762 trace_nfs4_open_expired(ctx, 0, err);
2763 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2768 case -NFS4ERR_GRACE:
2769 case -NFS4ERR_DELAY:
2770 nfs4_handle_exception(server, err, &exception);
2773 } while (exception.retry);
2778 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2780 struct nfs_open_context *ctx;
2783 ctx = nfs4_state_find_open_context(state);
2786 ret = nfs4_do_open_expired(ctx, state);
2787 put_nfs_open_context(ctx);
2791 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2792 const nfs4_stateid *stateid)
2794 nfs_remove_bad_delegation(state->inode, stateid);
2795 nfs_state_clear_delegation(state);
2798 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2800 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2801 nfs_finish_clear_delegation_stateid(state, NULL);
2804 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2806 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2807 nfs40_clear_delegation_stateid(state);
2808 nfs_state_clear_open_state_flags(state);
2809 return nfs4_open_expired(sp, state);
2812 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2813 nfs4_stateid *stateid,
2814 const struct cred *cred)
2816 return -NFS4ERR_BAD_STATEID;
2819 #if defined(CONFIG_NFS_V4_1)
2820 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2821 nfs4_stateid *stateid,
2822 const struct cred *cred)
2826 switch (stateid->type) {
2829 case NFS4_INVALID_STATEID_TYPE:
2830 case NFS4_SPECIAL_STATEID_TYPE:
2831 return -NFS4ERR_BAD_STATEID;
2832 case NFS4_REVOKED_STATEID_TYPE:
2836 status = nfs41_test_stateid(server, stateid, cred);
2838 case -NFS4ERR_EXPIRED:
2839 case -NFS4ERR_ADMIN_REVOKED:
2840 case -NFS4ERR_DELEG_REVOKED:
2846 /* Ack the revoked state to the server */
2847 nfs41_free_stateid(server, stateid, cred, true);
2848 return -NFS4ERR_EXPIRED;
2851 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2853 struct nfs_server *server = NFS_SERVER(state->inode);
2854 nfs4_stateid stateid;
2855 struct nfs_delegation *delegation;
2856 const struct cred *cred = NULL;
2857 int status, ret = NFS_OK;
2859 /* Get the delegation credential for use by test/free_stateid */
2861 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2862 if (delegation == NULL) {
2864 nfs_state_clear_delegation(state);
2868 spin_lock(&delegation->lock);
2869 nfs4_stateid_copy(&stateid, &delegation->stateid);
2871 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2872 &delegation->flags)) {
2873 spin_unlock(&delegation->lock);
2878 if (delegation->cred)
2879 cred = get_cred(delegation->cred);
2880 spin_unlock(&delegation->lock);
2882 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2883 trace_nfs4_test_delegation_stateid(state, NULL, status);
2884 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2885 nfs_finish_clear_delegation_stateid(state, &stateid);
2893 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2897 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2898 nfs4_copy_delegation_stateid(state->inode, state->state,
2900 nfs4_stateid_match_other(&state->stateid, &tmp))
2901 nfs_state_set_delegation(state, &tmp, state->state);
2903 nfs_state_clear_delegation(state);
2907 * nfs41_check_expired_locks - possibly free a lock stateid
2909 * @state: NFSv4 state for an inode
2911 * Returns NFS_OK if recovery for this stateid is now finished.
2912 * Otherwise a negative NFS4ERR value is returned.
2914 static int nfs41_check_expired_locks(struct nfs4_state *state)
2916 int status, ret = NFS_OK;
2917 struct nfs4_lock_state *lsp, *prev = NULL;
2918 struct nfs_server *server = NFS_SERVER(state->inode);
2920 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2923 spin_lock(&state->state_lock);
2924 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2925 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2926 const struct cred *cred = lsp->ls_state->owner->so_cred;
2928 refcount_inc(&lsp->ls_count);
2929 spin_unlock(&state->state_lock);
2931 nfs4_put_lock_state(prev);
2934 status = nfs41_test_and_free_expired_stateid(server,
2937 trace_nfs4_test_lock_stateid(state, lsp, status);
2938 if (status == -NFS4ERR_EXPIRED ||
2939 status == -NFS4ERR_BAD_STATEID) {
2940 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2941 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2942 if (!recover_lost_locks)
2943 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2944 } else if (status != NFS_OK) {
2946 nfs4_put_lock_state(prev);
2949 spin_lock(&state->state_lock);
2952 spin_unlock(&state->state_lock);
2953 nfs4_put_lock_state(prev);
2959 * nfs41_check_open_stateid - possibly free an open stateid
2961 * @state: NFSv4 state for an inode
2963 * Returns NFS_OK if recovery for this stateid is now finished.
2964 * Otherwise a negative NFS4ERR value is returned.
2966 static int nfs41_check_open_stateid(struct nfs4_state *state)
2968 struct nfs_server *server = NFS_SERVER(state->inode);
2969 nfs4_stateid *stateid = &state->open_stateid;
2970 const struct cred *cred = state->owner->so_cred;
2973 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2974 return -NFS4ERR_BAD_STATEID;
2975 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2976 trace_nfs4_test_open_stateid(state, NULL, status);
2977 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2978 nfs_state_clear_open_state_flags(state);
2979 stateid->type = NFS4_INVALID_STATEID_TYPE;
2982 if (nfs_open_stateid_recover_openmode(state))
2983 return -NFS4ERR_OPENMODE;
2987 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2991 status = nfs41_check_delegation_stateid(state);
2992 if (status != NFS_OK)
2994 nfs41_delegation_recover_stateid(state);
2996 status = nfs41_check_expired_locks(state);
2997 if (status != NFS_OK)
2999 status = nfs41_check_open_stateid(state);
3000 if (status != NFS_OK)
3001 status = nfs4_open_expired(sp, state);
3007 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
3008 * fields corresponding to attributes that were used to store the verifier.
3009 * Make sure we clobber those fields in the later setattr call
3011 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
3012 struct iattr *sattr, struct nfs4_label **label)
3014 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
3019 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
3020 attrset[i] = opendata->o_res.attrset[i];
3021 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3022 attrset[i] &= ~bitmask[i];
3025 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3026 sattr->ia_valid : 0;
3028 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3029 if (sattr->ia_valid & ATTR_ATIME_SET)
3030 ret |= ATTR_ATIME_SET;
3035 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3036 if (sattr->ia_valid & ATTR_MTIME_SET)
3037 ret |= ATTR_MTIME_SET;
3042 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3047 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3048 int flags, struct nfs_open_context *ctx)
3050 struct nfs4_state_owner *sp = opendata->owner;
3051 struct nfs_server *server = sp->so_server;
3052 struct dentry *dentry;
3053 struct nfs4_state *state;
3054 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3055 struct inode *dir = d_inode(opendata->dir);
3056 unsigned long dir_verifier;
3060 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3061 dir_verifier = nfs_save_change_attribute(dir);
3063 ret = _nfs4_proc_open(opendata, ctx);
3067 state = _nfs4_opendata_to_nfs4_state(opendata);
3068 ret = PTR_ERR(state);
3072 if (server->caps & NFS_CAP_POSIX_LOCK)
3073 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3074 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3075 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3077 dentry = opendata->dentry;
3078 if (d_really_is_negative(dentry)) {
3079 struct dentry *alias;
3081 alias = d_exact_alias(dentry, state->inode);
3083 alias = d_splice_alias(igrab(state->inode), dentry);
3084 /* d_splice_alias() can't fail here - it's a non-directory */
3087 ctx->dentry = dentry = alias;
3091 switch(opendata->o_arg.claim) {
3094 case NFS4_OPEN_CLAIM_NULL:
3095 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3096 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3097 if (!opendata->rpc_done)
3099 if (opendata->o_res.delegation_type != 0)
3100 dir_verifier = nfs_save_change_attribute(dir);
3101 nfs_set_verifier(dentry, dir_verifier);
3104 /* Parse layoutget results before we check for access */
3105 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3107 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3112 if (d_inode(dentry) == state->inode) {
3113 nfs_inode_attach_open_context(ctx);
3114 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3115 nfs4_schedule_stateid_recovery(server, state);
3119 if (!opendata->cancelled) {
3120 if (opendata->lgp) {
3121 nfs4_lgopen_release(opendata->lgp);
3122 opendata->lgp = NULL;
3124 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3130 * Returns a referenced nfs4_state
3132 static int _nfs4_do_open(struct inode *dir,
3133 struct nfs_open_context *ctx,
3135 const struct nfs4_open_createattrs *c,
3138 struct nfs4_state_owner *sp;
3139 struct nfs4_state *state = NULL;
3140 struct nfs_server *server = NFS_SERVER(dir);
3141 struct nfs4_opendata *opendata;
3142 struct dentry *dentry = ctx->dentry;
3143 const struct cred *cred = ctx->cred;
3144 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3145 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3146 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3147 struct iattr *sattr = c->sattr;
3148 struct nfs4_label *label = c->label;
3149 struct nfs4_label *olabel = NULL;
3152 /* Protect against reboot recovery conflicts */
3154 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3156 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3159 status = nfs4_client_recover_expired_lease(server->nfs_client);
3161 goto err_put_state_owner;
3162 if (d_really_is_positive(dentry))
3163 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3165 if (d_really_is_positive(dentry))
3166 claim = NFS4_OPEN_CLAIM_FH;
3167 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3168 c, claim, GFP_KERNEL);
3169 if (opendata == NULL)
3170 goto err_put_state_owner;
3173 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3174 if (IS_ERR(olabel)) {
3175 status = PTR_ERR(olabel);
3176 goto err_opendata_put;
3180 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3181 if (!opendata->f_attr.mdsthreshold) {
3182 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3183 if (!opendata->f_attr.mdsthreshold)
3184 goto err_free_label;
3186 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3188 if (d_really_is_positive(dentry))
3189 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3191 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3193 goto err_free_label;
3196 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3197 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3198 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3200 * send create attributes which was not set by open
3201 * with an extra setattr.
3203 if (attrs || label) {
3204 unsigned ia_old = sattr->ia_valid;
3206 sattr->ia_valid = attrs;
3207 nfs_fattr_init(opendata->o_res.f_attr);
3208 status = nfs4_do_setattr(state->inode, cred,
3209 opendata->o_res.f_attr, sattr,
3210 ctx, label, olabel);
3212 nfs_setattr_update_inode(state->inode, sattr,
3213 opendata->o_res.f_attr);
3214 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3216 sattr->ia_valid = ia_old;
3219 if (opened && opendata->file_created)
3222 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3223 *ctx_th = opendata->f_attr.mdsthreshold;
3224 opendata->f_attr.mdsthreshold = NULL;
3227 nfs4_label_free(olabel);
3229 nfs4_opendata_put(opendata);
3230 nfs4_put_state_owner(sp);
3233 nfs4_label_free(olabel);
3235 nfs4_opendata_put(opendata);
3236 err_put_state_owner:
3237 nfs4_put_state_owner(sp);
3243 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3244 struct nfs_open_context *ctx,
3246 struct iattr *sattr,
3247 struct nfs4_label *label,
3250 struct nfs_server *server = NFS_SERVER(dir);
3251 struct nfs4_exception exception = {
3252 .interruptible = true,
3254 struct nfs4_state *res;
3255 struct nfs4_open_createattrs c = {
3259 [0] = (__u32)jiffies,
3260 [1] = (__u32)current->pid,
3266 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3268 trace_nfs4_open_file(ctx, flags, status);
3271 /* NOTE: BAD_SEQID means the server and client disagree about the
3272 * book-keeping w.r.t. state-changing operations
3273 * (OPEN/CLOSE/LOCK/LOCKU...)
3274 * It is actually a sign of a bug on the client or on the server.
3276 * If we receive a BAD_SEQID error in the particular case of
3277 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3278 * have unhashed the old state_owner for us, and that we can
3279 * therefore safely retry using a new one. We should still warn
3280 * the user though...
3282 if (status == -NFS4ERR_BAD_SEQID) {
3283 pr_warn_ratelimited("NFS: v4 server %s "
3284 " returned a bad sequence-id error!\n",
3285 NFS_SERVER(dir)->nfs_client->cl_hostname);
3286 exception.retry = 1;
3290 * BAD_STATEID on OPEN means that the server cancelled our
3291 * state before it received the OPEN_CONFIRM.
3292 * Recover by retrying the request as per the discussion
3293 * on Page 181 of RFC3530.
3295 if (status == -NFS4ERR_BAD_STATEID) {
3296 exception.retry = 1;
3299 if (status == -NFS4ERR_EXPIRED) {
3300 nfs4_schedule_lease_recovery(server->nfs_client);
3301 exception.retry = 1;
3304 if (status == -EAGAIN) {
3305 /* We must have found a delegation */
3306 exception.retry = 1;
3309 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3311 res = ERR_PTR(nfs4_handle_exception(server,
3312 status, &exception));
3313 } while (exception.retry);
3317 static int _nfs4_do_setattr(struct inode *inode,
3318 struct nfs_setattrargs *arg,
3319 struct nfs_setattrres *res,
3320 const struct cred *cred,
3321 struct nfs_open_context *ctx)
3323 struct nfs_server *server = NFS_SERVER(inode);
3324 struct rpc_message msg = {
3325 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3330 const struct cred *delegation_cred = NULL;
3331 unsigned long timestamp = jiffies;
3335 nfs_fattr_init(res->fattr);
3337 /* Servers should only apply open mode checks for file size changes */
3338 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3340 nfs4_inode_make_writeable(inode);
3344 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3345 /* Use that stateid */
3346 } else if (ctx != NULL && ctx->state) {
3347 struct nfs_lock_context *l_ctx;
3348 if (!nfs4_valid_open_stateid(ctx->state))
3350 l_ctx = nfs_get_lock_context(ctx);
3352 return PTR_ERR(l_ctx);
3353 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3354 &arg->stateid, &delegation_cred);
3355 nfs_put_lock_context(l_ctx);
3358 else if (status == -EAGAIN)
3362 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3364 if (delegation_cred)
3365 msg.rpc_cred = delegation_cred;
3367 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3369 put_cred(delegation_cred);
3370 if (status == 0 && ctx != NULL)
3371 renew_lease(server, timestamp);
3372 trace_nfs4_setattr(inode, &arg->stateid, status);
3376 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3377 struct nfs_fattr *fattr, struct iattr *sattr,
3378 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3379 struct nfs4_label *olabel)
3381 struct nfs_server *server = NFS_SERVER(inode);
3382 __u32 bitmask[NFS4_BITMASK_SZ];
3383 struct nfs4_state *state = ctx ? ctx->state : NULL;
3384 struct nfs_setattrargs arg = {
3385 .fh = NFS_FH(inode),
3391 struct nfs_setattrres res = {
3396 struct nfs4_exception exception = {
3399 .stateid = &arg.stateid,
3401 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
3404 if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3405 adjust_flags |= NFS_INO_INVALID_MODE;
3406 if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
3407 adjust_flags |= NFS_INO_INVALID_OTHER;
3410 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, olabel),
3411 inode, adjust_flags);
3413 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3415 case -NFS4ERR_OPENMODE:
3416 if (!(sattr->ia_valid & ATTR_SIZE)) {
3417 pr_warn_once("NFSv4: server %s is incorrectly "
3418 "applying open mode checks to "
3419 "a SETATTR that is not "
3420 "changing file size.\n",
3421 server->nfs_client->cl_hostname);
3423 if (state && !(state->state & FMODE_WRITE)) {
3425 if (sattr->ia_valid & ATTR_OPEN)
3430 err = nfs4_handle_exception(server, err, &exception);
3431 } while (exception.retry);
3437 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3439 if (inode == NULL || !nfs_have_layout(inode))
3442 return pnfs_wait_on_layoutreturn(inode, task);
3446 * Update the seqid of an open stateid
3448 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3449 struct nfs4_state *state)
3456 if (!nfs4_valid_open_stateid(state))
3458 seq = read_seqbegin(&state->seqlock);
3459 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3460 nfs4_stateid_copy(dst, &state->open_stateid);
3461 if (read_seqretry(&state->seqlock, seq))
3465 seqid_open = state->open_stateid.seqid;
3466 if (read_seqretry(&state->seqlock, seq))
3469 dst_seqid = be32_to_cpu(dst->seqid);
3470 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3471 dst->seqid = seqid_open;
3477 * Update the seqid of an open stateid after receiving
3478 * NFS4ERR_OLD_STATEID
3480 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3481 struct nfs4_state *state)
3486 int seq, status = -EAGAIN;
3491 if (!nfs4_valid_open_stateid(state))
3493 seq = read_seqbegin(&state->seqlock);
3494 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3495 if (read_seqretry(&state->seqlock, seq))
3500 write_seqlock(&state->seqlock);
3501 seqid_open = state->open_stateid.seqid;
3503 dst_seqid = be32_to_cpu(dst->seqid);
3505 /* Did another OPEN bump the state's seqid? try again: */
3506 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3507 dst->seqid = seqid_open;
3508 write_sequnlock(&state->seqlock);
3513 /* server says we're behind but we haven't seen the update yet */
3514 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3515 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3516 write_sequnlock(&state->seqlock);
3517 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3519 if (fatal_signal_pending(current))
3522 if (schedule_timeout(5*HZ) != 0)
3525 finish_wait(&state->waitq, &wait);
3529 if (status == -EINTR)
3532 /* we slept the whole 5 seconds, we must have lost a seqid */
3533 dst->seqid = cpu_to_be32(dst_seqid + 1);
3541 struct nfs4_closedata {
3542 struct inode *inode;
3543 struct nfs4_state *state;
3544 struct nfs_closeargs arg;
3545 struct nfs_closeres res;
3547 struct nfs4_layoutreturn_args arg;
3548 struct nfs4_layoutreturn_res res;
3549 struct nfs4_xdr_opaque_data ld_private;
3553 struct nfs_fattr fattr;
3554 unsigned long timestamp;
3557 static void nfs4_free_closedata(void *data)
3559 struct nfs4_closedata *calldata = data;
3560 struct nfs4_state_owner *sp = calldata->state->owner;
3561 struct super_block *sb = calldata->state->inode->i_sb;
3563 if (calldata->lr.roc)
3564 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3565 calldata->res.lr_ret);
3566 nfs4_put_open_state(calldata->state);
3567 nfs_free_seqid(calldata->arg.seqid);
3568 nfs4_put_state_owner(sp);
3569 nfs_sb_deactive(sb);
3573 static void nfs4_close_done(struct rpc_task *task, void *data)
3575 struct nfs4_closedata *calldata = data;
3576 struct nfs4_state *state = calldata->state;
3577 struct nfs_server *server = NFS_SERVER(calldata->inode);
3578 nfs4_stateid *res_stateid = NULL;
3579 struct nfs4_exception exception = {
3581 .inode = calldata->inode,
3582 .stateid = &calldata->arg.stateid,
3585 dprintk("%s: begin!\n", __func__);
3586 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3588 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3590 /* Handle Layoutreturn errors */
3591 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3592 &calldata->res.lr_ret) == -EAGAIN)
3595 /* hmm. we are done with the inode, and in the process of freeing
3596 * the state_owner. we keep this around to process errors
3598 switch (task->tk_status) {
3600 res_stateid = &calldata->res.stateid;
3601 renew_lease(server, calldata->timestamp);
3603 case -NFS4ERR_ACCESS:
3604 if (calldata->arg.bitmask != NULL) {
3605 calldata->arg.bitmask = NULL;
3606 calldata->res.fattr = NULL;
3611 case -NFS4ERR_OLD_STATEID:
3612 /* Did we race with OPEN? */
3613 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3617 case -NFS4ERR_ADMIN_REVOKED:
3618 case -NFS4ERR_STALE_STATEID:
3619 case -NFS4ERR_EXPIRED:
3620 nfs4_free_revoked_stateid(server,
3621 &calldata->arg.stateid,
3622 task->tk_msg.rpc_cred);
3624 case -NFS4ERR_BAD_STATEID:
3625 if (calldata->arg.fmode == 0)
3629 task->tk_status = nfs4_async_handle_exception(task,
3630 server, task->tk_status, &exception);
3631 if (exception.retry)
3634 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3635 res_stateid, calldata->arg.fmode);
3637 task->tk_status = 0;
3638 nfs_release_seqid(calldata->arg.seqid);
3639 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3640 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3643 task->tk_status = 0;
3644 rpc_restart_call_prepare(task);
3648 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3650 struct nfs4_closedata *calldata = data;
3651 struct nfs4_state *state = calldata->state;
3652 struct inode *inode = calldata->inode;
3653 struct nfs_server *server = NFS_SERVER(inode);
3654 struct pnfs_layout_hdr *lo;
3655 bool is_rdonly, is_wronly, is_rdwr;
3658 dprintk("%s: begin!\n", __func__);
3659 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3662 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3663 spin_lock(&state->owner->so_lock);
3664 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3665 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3666 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3667 /* Calculate the change in open mode */
3668 calldata->arg.fmode = 0;
3669 if (state->n_rdwr == 0) {
3670 if (state->n_rdonly == 0)
3671 call_close |= is_rdonly;
3673 calldata->arg.fmode |= FMODE_READ;
3674 if (state->n_wronly == 0)
3675 call_close |= is_wronly;
3677 calldata->arg.fmode |= FMODE_WRITE;
3678 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3679 call_close |= is_rdwr;
3681 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3683 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3684 if (!nfs4_valid_open_stateid(state))
3686 spin_unlock(&state->owner->so_lock);
3689 /* Note: exit _without_ calling nfs4_close_done */
3693 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3694 nfs_release_seqid(calldata->arg.seqid);
3698 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3699 if (lo && !pnfs_layout_is_valid(lo)) {
3700 calldata->arg.lr_args = NULL;
3701 calldata->res.lr_res = NULL;
3704 if (calldata->arg.fmode == 0)
3705 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3707 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3708 /* Close-to-open cache consistency revalidation */
3709 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3710 nfs4_bitmask_set(calldata->arg.bitmask_store,
3711 server->cache_consistency_bitmask,
3712 inode, server, NULL);
3713 calldata->arg.bitmask = calldata->arg.bitmask_store;
3715 calldata->arg.bitmask = NULL;
3718 calldata->arg.share_access =
3719 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3720 calldata->arg.fmode, 0);
3722 if (calldata->res.fattr == NULL)
3723 calldata->arg.bitmask = NULL;
3724 else if (calldata->arg.bitmask == NULL)
3725 calldata->res.fattr = NULL;
3726 calldata->timestamp = jiffies;
3727 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3728 &calldata->arg.seq_args,
3729 &calldata->res.seq_res,
3731 nfs_release_seqid(calldata->arg.seqid);
3732 dprintk("%s: done!\n", __func__);
3735 task->tk_action = NULL;
3737 nfs4_sequence_done(task, &calldata->res.seq_res);
3740 static const struct rpc_call_ops nfs4_close_ops = {
3741 .rpc_call_prepare = nfs4_close_prepare,
3742 .rpc_call_done = nfs4_close_done,
3743 .rpc_release = nfs4_free_closedata,
3747 * It is possible for data to be read/written from a mem-mapped file
3748 * after the sys_close call (which hits the vfs layer as a flush).
3749 * This means that we can't safely call nfsv4 close on a file until
3750 * the inode is cleared. This in turn means that we are not good
3751 * NFSv4 citizens - we do not indicate to the server to update the file's
3752 * share state even when we are done with one of the three share
3753 * stateid's in the inode.
3755 * NOTE: Caller must be holding the sp->so_owner semaphore!
3757 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3759 struct nfs_server *server = NFS_SERVER(state->inode);
3760 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3761 struct nfs4_closedata *calldata;
3762 struct nfs4_state_owner *sp = state->owner;
3763 struct rpc_task *task;
3764 struct rpc_message msg = {
3765 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3766 .rpc_cred = state->owner->so_cred,
3768 struct rpc_task_setup task_setup_data = {
3769 .rpc_client = server->client,
3770 .rpc_message = &msg,
3771 .callback_ops = &nfs4_close_ops,
3772 .workqueue = nfsiod_workqueue,
3773 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3775 int status = -ENOMEM;
3777 if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
3778 task_setup_data.flags |= RPC_TASK_MOVEABLE;
3780 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3781 &task_setup_data.rpc_client, &msg);
3783 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3784 if (calldata == NULL)
3786 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3787 calldata->inode = state->inode;
3788 calldata->state = state;
3789 calldata->arg.fh = NFS_FH(state->inode);
3790 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3791 goto out_free_calldata;
3792 /* Serialization for the sequence id */
3793 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3794 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3795 if (IS_ERR(calldata->arg.seqid))
3796 goto out_free_calldata;
3797 nfs_fattr_init(&calldata->fattr);
3798 calldata->arg.fmode = 0;
3799 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3800 calldata->res.fattr = &calldata->fattr;
3801 calldata->res.seqid = calldata->arg.seqid;
3802 calldata->res.server = server;
3803 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3804 calldata->lr.roc = pnfs_roc(state->inode,
3805 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3806 if (calldata->lr.roc) {
3807 calldata->arg.lr_args = &calldata->lr.arg;
3808 calldata->res.lr_res = &calldata->lr.res;
3810 nfs_sb_active(calldata->inode->i_sb);
3812 msg.rpc_argp = &calldata->arg;
3813 msg.rpc_resp = &calldata->res;
3814 task_setup_data.callback_data = calldata;
3815 task = rpc_run_task(&task_setup_data);
3817 return PTR_ERR(task);
3820 status = rpc_wait_for_completion_task(task);
3826 nfs4_put_open_state(state);
3827 nfs4_put_state_owner(sp);
3831 static struct inode *
3832 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3833 int open_flags, struct iattr *attr, int *opened)
3835 struct nfs4_state *state;
3836 struct nfs4_label l, *label;
3838 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3840 /* Protect against concurrent sillydeletes */
3841 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3843 nfs4_label_release_security(label);
3846 return ERR_CAST(state);
3847 return state->inode;
3850 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3852 if (ctx->state == NULL)
3855 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3857 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3860 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3861 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3862 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3864 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3866 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3867 struct nfs4_server_caps_arg args = {
3871 struct nfs4_server_caps_res res = {};
3872 struct rpc_message msg = {
3873 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3880 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3881 FATTR4_WORD0_FH_EXPIRE_TYPE |
3882 FATTR4_WORD0_LINK_SUPPORT |
3883 FATTR4_WORD0_SYMLINK_SUPPORT |
3884 FATTR4_WORD0_ACLSUPPORT;
3886 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3888 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3890 /* Sanity check the server answers */
3891 switch (minorversion) {
3893 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3894 res.attr_bitmask[2] = 0;
3897 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3900 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3902 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3903 server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3904 NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3905 server->fattr_valid = NFS_ATTR_FATTR_V4;
3906 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3907 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3908 server->caps |= NFS_CAP_ACLS;
3909 if (res.has_links != 0)
3910 server->caps |= NFS_CAP_HARDLINKS;
3911 if (res.has_symlinks != 0)
3912 server->caps |= NFS_CAP_SYMLINKS;
3913 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3914 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3915 server->caps |= NFS_CAP_SECURITY_LABEL;
3917 if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS)
3918 server->caps |= NFS_CAP_FS_LOCATIONS;
3919 if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3920 server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3921 if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3922 server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3923 if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3924 server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3925 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3926 server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3927 NFS_ATTR_FATTR_OWNER_NAME);
3928 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3929 server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3930 NFS_ATTR_FATTR_GROUP_NAME);
3931 if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3932 server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3933 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3934 server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3935 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3936 server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3937 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3938 server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
3939 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3940 sizeof(server->attr_bitmask));
3941 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3943 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3944 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3945 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3946 server->cache_consistency_bitmask[2] = 0;
3948 /* Avoid a regression due to buggy server */
3949 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3950 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3951 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3952 sizeof(server->exclcreat_bitmask));
3954 server->acl_bitmask = res.acl_bitmask;
3955 server->fh_expire_type = res.fh_expire_type;
3961 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3963 struct nfs4_exception exception = {
3964 .interruptible = true,
3968 err = nfs4_handle_exception(server,
3969 _nfs4_server_capabilities(server, fhandle),
3971 } while (exception.retry);
3975 static int _nfs4_discover_trunking(struct nfs_server *server,
3976 struct nfs_fh *fhandle)
3978 struct nfs4_fs_locations *locations = NULL;
3980 const struct cred *cred;
3981 struct nfs_client *clp = server->nfs_client;
3982 const struct nfs4_state_maintenance_ops *ops =
3983 clp->cl_mvops->state_renewal_ops;
3984 int status = -ENOMEM;
3986 cred = ops->get_state_renewal_cred(clp);
3988 cred = nfs4_get_clid_cred(clp);
3993 page = alloc_page(GFP_KERNEL);
3996 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3999 locations->fattr = nfs_alloc_fattr();
4000 if (!locations->fattr)
4003 status = nfs4_proc_get_locations(server, fhandle, locations, page,
4006 kfree(locations->fattr);
4016 static int nfs4_discover_trunking(struct nfs_server *server,
4017 struct nfs_fh *fhandle)
4019 struct nfs4_exception exception = {
4020 .interruptible = true,
4022 struct nfs_client *clp = server->nfs_client;
4025 if (!nfs4_has_session(clp))
4028 err = nfs4_handle_exception(server,
4029 _nfs4_discover_trunking(server, fhandle),
4031 } while (exception.retry);
4036 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4037 struct nfs_fsinfo *info)
4040 struct nfs4_lookup_root_arg args = {
4043 struct nfs4_lookup_res res = {
4045 .fattr = info->fattr,
4048 struct rpc_message msg = {
4049 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
4054 bitmask[0] = nfs4_fattr_bitmap[0];
4055 bitmask[1] = nfs4_fattr_bitmap[1];
4057 * Process the label in the upcoming getfattr
4059 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
4061 nfs_fattr_init(info->fattr);
4062 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4065 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4066 struct nfs_fsinfo *info)
4068 struct nfs4_exception exception = {
4069 .interruptible = true,
4073 err = _nfs4_lookup_root(server, fhandle, info);
4074 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
4077 case -NFS4ERR_WRONGSEC:
4080 err = nfs4_handle_exception(server, err, &exception);
4082 } while (exception.retry);
4087 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4088 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4090 struct rpc_auth_create_args auth_args = {
4091 .pseudoflavor = flavor,
4093 struct rpc_auth *auth;
4095 auth = rpcauth_create(&auth_args, server->client);
4098 return nfs4_lookup_root(server, fhandle, info);
4102 * Retry pseudoroot lookup with various security flavors. We do this when:
4104 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4105 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4107 * Returns zero on success, or a negative NFS4ERR value, or a
4108 * negative errno value.
4110 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4111 struct nfs_fsinfo *info)
4113 /* Per 3530bis 15.33.5 */
4114 static const rpc_authflavor_t flav_array[] = {
4118 RPC_AUTH_UNIX, /* courtesy */
4121 int status = -EPERM;
4124 if (server->auth_info.flavor_len > 0) {
4125 /* try each flavor specified by user */
4126 for (i = 0; i < server->auth_info.flavor_len; i++) {
4127 status = nfs4_lookup_root_sec(server, fhandle, info,
4128 server->auth_info.flavors[i]);
4129 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4134 /* no flavors specified by user, try default list */
4135 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4136 status = nfs4_lookup_root_sec(server, fhandle, info,
4138 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4145 * -EACCES could mean that the user doesn't have correct permissions
4146 * to access the mount. It could also mean that we tried to mount
4147 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4148 * existing mount programs don't handle -EACCES very well so it should
4149 * be mapped to -EPERM instead.
4151 if (status == -EACCES)
4157 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4158 * @server: initialized nfs_server handle
4159 * @fhandle: we fill in the pseudo-fs root file handle
4160 * @info: we fill in an FSINFO struct
4161 * @auth_probe: probe the auth flavours
4163 * Returns zero on success, or a negative errno.
4165 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4166 struct nfs_fsinfo *info,
4172 status = nfs4_lookup_root(server, fhandle, info);
4174 if (auth_probe || status == NFS4ERR_WRONGSEC)
4175 status = server->nfs_client->cl_mvops->find_root_sec(server,
4179 status = nfs4_server_capabilities(server, fhandle);
4181 status = nfs4_do_fsinfo(server, fhandle, info);
4183 return nfs4_map_errors(status);
4186 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4187 struct nfs_fsinfo *info)
4190 struct nfs_fattr *fattr = info->fattr;
4191 struct nfs4_label *label = fattr->label;
4193 error = nfs4_server_capabilities(server, mntfh);
4195 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4199 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
4201 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4205 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4206 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4207 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4214 * Get locations and (maybe) other attributes of a referral.
4215 * Note that we'll actually follow the referral later when
4216 * we detect fsid mismatch in inode revalidation
4218 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4219 const struct qstr *name, struct nfs_fattr *fattr,
4220 struct nfs_fh *fhandle)
4222 int status = -ENOMEM;
4223 struct page *page = NULL;
4224 struct nfs4_fs_locations *locations = NULL;
4226 page = alloc_page(GFP_KERNEL);
4229 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4230 if (locations == NULL)
4233 locations->fattr = fattr;
4235 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4240 * If the fsid didn't change, this is a migration event, not a
4241 * referral. Cause us to drop into the exception handler, which
4242 * will kick off migration recovery.
4244 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &fattr->fsid)) {
4245 dprintk("%s: server did not return a different fsid for"
4246 " a referral at %s\n", __func__, name->name);
4247 status = -NFS4ERR_MOVED;
4250 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4251 nfs_fixup_referral_attributes(fattr);
4252 memset(fhandle, 0, sizeof(struct nfs_fh));
4260 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4261 struct nfs_fattr *fattr, struct nfs4_label *label,
4262 struct inode *inode)
4264 __u32 bitmask[NFS4_BITMASK_SZ];
4265 struct nfs4_getattr_arg args = {
4269 struct nfs4_getattr_res res = {
4274 struct rpc_message msg = {
4275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4279 unsigned short task_flags = 0;
4281 if (nfs4_has_session(server->nfs_client))
4282 task_flags = RPC_TASK_MOVEABLE;
4284 /* Is this is an attribute revalidation, subject to softreval? */
4285 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4286 task_flags |= RPC_TASK_TIMEOUT;
4288 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode, 0);
4289 nfs_fattr_init(fattr);
4290 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4291 return nfs4_do_call_sync(server->client, server, &msg,
4292 &args.seq_args, &res.seq_res, task_flags);
4295 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4296 struct nfs_fattr *fattr, struct nfs4_label *label,
4297 struct inode *inode)
4299 struct nfs4_exception exception = {
4300 .interruptible = true,
4304 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4305 trace_nfs4_getattr(server, fhandle, fattr, err);
4306 err = nfs4_handle_exception(server, err,
4308 } while (exception.retry);
4313 * The file is not closed if it is opened due to the a request to change
4314 * the size of the file. The open call will not be needed once the
4315 * VFS layer lookup-intents are implemented.
4317 * Close is called when the inode is destroyed.
4318 * If we haven't opened the file for O_WRONLY, we
4319 * need to in the size_change case to obtain a stateid.
4322 * Because OPEN is always done by name in nfsv4, it is
4323 * possible that we opened a different file by the same
4324 * name. We can recognize this race condition, but we
4325 * can't do anything about it besides returning an error.
4327 * This will be fixed with VFS changes (lookup-intent).
4330 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4331 struct iattr *sattr)
4333 struct inode *inode = d_inode(dentry);
4334 const struct cred *cred = NULL;
4335 struct nfs_open_context *ctx = NULL;
4336 struct nfs4_label *label = NULL;
4339 if (pnfs_ld_layoutret_on_setattr(inode) &&
4340 sattr->ia_valid & ATTR_SIZE &&
4341 sattr->ia_size < i_size_read(inode))
4342 pnfs_commit_and_return_layout(inode);
4344 nfs_fattr_init(fattr);
4346 /* Deal with open(O_TRUNC) */
4347 if (sattr->ia_valid & ATTR_OPEN)
4348 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4350 /* Optimization: if the end result is no change, don't RPC */
4351 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4354 /* Search for an existing open(O_WRITE) file */
4355 if (sattr->ia_valid & ATTR_FILE) {
4357 ctx = nfs_file_open_context(sattr->ia_file);
4362 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4364 return PTR_ERR(label);
4366 /* Return any delegations if we're going to change ACLs */
4367 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4368 nfs4_inode_make_writeable(inode);
4370 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4372 nfs_setattr_update_inode(inode, sattr, fattr);
4373 nfs_setsecurity(inode, fattr, label);
4375 nfs4_label_free(label);
4379 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4380 struct dentry *dentry, struct nfs_fh *fhandle,
4381 struct nfs_fattr *fattr, struct nfs4_label *label)
4383 struct nfs_server *server = NFS_SERVER(dir);
4385 struct nfs4_lookup_arg args = {
4386 .bitmask = server->attr_bitmask,
4387 .dir_fh = NFS_FH(dir),
4388 .name = &dentry->d_name,
4390 struct nfs4_lookup_res res = {
4396 struct rpc_message msg = {
4397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4401 unsigned short task_flags = 0;
4403 if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
4404 task_flags = RPC_TASK_MOVEABLE;
4406 /* Is this is an attribute revalidation, subject to softreval? */
4407 if (nfs_lookup_is_soft_revalidate(dentry))
4408 task_flags |= RPC_TASK_TIMEOUT;
4410 args.bitmask = nfs4_bitmask(server, label);
4412 nfs_fattr_init(fattr);
4414 dprintk("NFS call lookup %pd2\n", dentry);
4415 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4416 status = nfs4_do_call_sync(clnt, server, &msg,
4417 &args.seq_args, &res.seq_res, task_flags);
4418 dprintk("NFS reply lookup: %d\n", status);
4422 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4424 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4425 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4426 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4430 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4431 struct dentry *dentry, struct nfs_fh *fhandle,
4432 struct nfs_fattr *fattr, struct nfs4_label *label)
4434 struct nfs4_exception exception = {
4435 .interruptible = true,
4437 struct rpc_clnt *client = *clnt;
4438 const struct qstr *name = &dentry->d_name;
4441 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
4442 trace_nfs4_lookup(dir, name, err);
4444 case -NFS4ERR_BADNAME:
4447 case -NFS4ERR_MOVED:
4448 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4449 if (err == -NFS4ERR_MOVED)
4450 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4452 case -NFS4ERR_WRONGSEC:
4454 if (client != *clnt)
4456 client = nfs4_negotiate_security(client, dir, name);
4458 return PTR_ERR(client);
4460 exception.retry = 1;
4463 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4465 } while (exception.retry);
4470 else if (client != *clnt)
4471 rpc_shutdown_client(client);
4476 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4477 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4478 struct nfs4_label *label)
4481 struct rpc_clnt *client = NFS_CLIENT(dir);
4483 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
4484 if (client != NFS_CLIENT(dir)) {
4485 rpc_shutdown_client(client);
4486 nfs_fixup_secinfo_attributes(fattr);
4492 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4493 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4495 struct rpc_clnt *client = NFS_CLIENT(dir);
4498 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
4500 return ERR_PTR(status);
4501 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4504 static int _nfs4_proc_lookupp(struct inode *inode,
4505 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4506 struct nfs4_label *label)
4508 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4509 struct nfs_server *server = NFS_SERVER(inode);
4511 struct nfs4_lookupp_arg args = {
4512 .bitmask = server->attr_bitmask,
4513 .fh = NFS_FH(inode),
4515 struct nfs4_lookupp_res res = {
4521 struct rpc_message msg = {
4522 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4526 unsigned short task_flags = 0;
4528 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4529 task_flags |= RPC_TASK_TIMEOUT;
4531 args.bitmask = nfs4_bitmask(server, label);
4533 nfs_fattr_init(fattr);
4535 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4536 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4537 &res.seq_res, task_flags);
4538 dprintk("NFS reply lookupp: %d\n", status);
4542 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4543 struct nfs_fattr *fattr, struct nfs4_label *label)
4545 struct nfs4_exception exception = {
4546 .interruptible = true,
4550 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4551 trace_nfs4_lookupp(inode, err);
4552 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4554 } while (exception.retry);
4558 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4560 struct nfs_server *server = NFS_SERVER(inode);
4561 struct nfs4_accessargs args = {
4562 .fh = NFS_FH(inode),
4563 .access = entry->mask,
4565 struct nfs4_accessres res = {
4568 struct rpc_message msg = {
4569 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4572 .rpc_cred = entry->cred,
4576 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4577 res.fattr = nfs_alloc_fattr();
4578 if (res.fattr == NULL)
4580 args.bitmask = server->cache_consistency_bitmask;
4582 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4584 nfs_access_set_mask(entry, res.access);
4586 nfs_refresh_inode(inode, res.fattr);
4588 nfs_free_fattr(res.fattr);
4592 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4594 struct nfs4_exception exception = {
4595 .interruptible = true,
4599 err = _nfs4_proc_access(inode, entry);
4600 trace_nfs4_access(inode, err);
4601 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4603 } while (exception.retry);
4608 * TODO: For the time being, we don't try to get any attributes
4609 * along with any of the zero-copy operations READ, READDIR,
4612 * In the case of the first three, we want to put the GETATTR
4613 * after the read-type operation -- this is because it is hard
4614 * to predict the length of a GETATTR response in v4, and thus
4615 * align the READ data correctly. This means that the GETATTR
4616 * may end up partially falling into the page cache, and we should
4617 * shift it into the 'tail' of the xdr_buf before processing.
4618 * To do this efficiently, we need to know the total length
4619 * of data received, which doesn't seem to be available outside
4622 * In the case of WRITE, we also want to put the GETATTR after
4623 * the operation -- in this case because we want to make sure
4624 * we get the post-operation mtime and size.
4626 * Both of these changes to the XDR layer would in fact be quite
4627 * minor, but I decided to leave them for a subsequent patch.
4629 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4630 unsigned int pgbase, unsigned int pglen)
4632 struct nfs4_readlink args = {
4633 .fh = NFS_FH(inode),
4638 struct nfs4_readlink_res res;
4639 struct rpc_message msg = {
4640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4645 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4648 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4649 unsigned int pgbase, unsigned int pglen)
4651 struct nfs4_exception exception = {
4652 .interruptible = true,
4656 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4657 trace_nfs4_readlink(inode, err);
4658 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4660 } while (exception.retry);
4665 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4668 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4671 struct nfs_server *server = NFS_SERVER(dir);
4672 struct nfs4_label l, *ilabel;
4673 struct nfs_open_context *ctx;
4674 struct nfs4_state *state;
4677 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4679 return PTR_ERR(ctx);
4681 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4683 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4684 sattr->ia_mode &= ~current_umask();
4685 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4686 if (IS_ERR(state)) {
4687 status = PTR_ERR(state);
4691 nfs4_label_release_security(ilabel);
4692 put_nfs_open_context(ctx);
4697 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4699 struct nfs_server *server = NFS_SERVER(dir);
4700 struct nfs_removeargs args = {
4704 struct nfs_removeres res = {
4707 struct rpc_message msg = {
4708 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4712 unsigned long timestamp = jiffies;
4715 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4717 spin_lock(&dir->i_lock);
4718 /* Removing a directory decrements nlink in the parent */
4719 if (ftype == NF4DIR && dir->i_nlink > 2)
4720 nfs4_dec_nlink_locked(dir);
4721 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4722 NFS_INO_INVALID_DATA);
4723 spin_unlock(&dir->i_lock);
4728 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4730 struct nfs4_exception exception = {
4731 .interruptible = true,
4733 struct inode *inode = d_inode(dentry);
4737 if (inode->i_nlink == 1)
4738 nfs4_inode_return_delegation(inode);
4740 nfs4_inode_make_writeable(inode);
4743 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4744 trace_nfs4_remove(dir, &dentry->d_name, err);
4745 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4747 } while (exception.retry);
4751 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4753 struct nfs4_exception exception = {
4754 .interruptible = true,
4759 err = _nfs4_proc_remove(dir, name, NF4DIR);
4760 trace_nfs4_remove(dir, name, err);
4761 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4763 } while (exception.retry);
4767 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4768 struct dentry *dentry,
4769 struct inode *inode)
4771 struct nfs_removeargs *args = msg->rpc_argp;
4772 struct nfs_removeres *res = msg->rpc_resp;
4774 res->server = NFS_SB(dentry->d_sb);
4775 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4776 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4778 nfs_fattr_init(res->dir_attr);
4781 nfs4_inode_return_delegation(inode);
4784 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4786 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4787 &data->args.seq_args,
4792 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4794 struct nfs_unlinkdata *data = task->tk_calldata;
4795 struct nfs_removeres *res = &data->res;
4797 if (!nfs4_sequence_done(task, &res->seq_res))
4799 if (nfs4_async_handle_error(task, res->server, NULL,
4800 &data->timeout) == -EAGAIN)
4802 if (task->tk_status == 0)
4803 nfs4_update_changeattr(dir, &res->cinfo,
4804 res->dir_attr->time_start,
4805 NFS_INO_INVALID_DATA);
4809 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4810 struct dentry *old_dentry,
4811 struct dentry *new_dentry)
4813 struct nfs_renameargs *arg = msg->rpc_argp;
4814 struct nfs_renameres *res = msg->rpc_resp;
4815 struct inode *old_inode = d_inode(old_dentry);
4816 struct inode *new_inode = d_inode(new_dentry);
4819 nfs4_inode_make_writeable(old_inode);
4821 nfs4_inode_return_delegation(new_inode);
4822 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4823 res->server = NFS_SB(old_dentry->d_sb);
4824 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4827 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4829 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4830 &data->args.seq_args,
4835 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4836 struct inode *new_dir)
4838 struct nfs_renamedata *data = task->tk_calldata;
4839 struct nfs_renameres *res = &data->res;
4841 if (!nfs4_sequence_done(task, &res->seq_res))
4843 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4846 if (task->tk_status == 0) {
4847 if (new_dir != old_dir) {
4848 /* Note: If we moved a directory, nlink will change */
4849 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4850 res->old_fattr->time_start,
4851 NFS_INO_INVALID_NLINK |
4852 NFS_INO_INVALID_DATA);
4853 nfs4_update_changeattr(new_dir, &res->new_cinfo,
4854 res->new_fattr->time_start,
4855 NFS_INO_INVALID_NLINK |
4856 NFS_INO_INVALID_DATA);
4858 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4859 res->old_fattr->time_start,
4860 NFS_INO_INVALID_DATA);
4865 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4867 struct nfs_server *server = NFS_SERVER(inode);
4868 __u32 bitmask[NFS4_BITMASK_SZ];
4869 struct nfs4_link_arg arg = {
4870 .fh = NFS_FH(inode),
4871 .dir_fh = NFS_FH(dir),
4875 struct nfs4_link_res res = {
4879 struct rpc_message msg = {
4880 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4884 int status = -ENOMEM;
4886 res.fattr = nfs_alloc_fattr();
4887 if (res.fattr == NULL)
4890 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4891 if (IS_ERR(res.label)) {
4892 status = PTR_ERR(res.label);
4896 nfs4_inode_make_writeable(inode);
4897 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.label), inode,
4898 NFS_INO_INVALID_CHANGE);
4899 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4901 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4902 NFS_INO_INVALID_DATA);
4903 nfs4_inc_nlink(inode);
4904 status = nfs_post_op_update_inode(inode, res.fattr);
4906 nfs_setsecurity(inode, res.fattr, res.label);
4910 nfs4_label_free(res.label);
4913 nfs_free_fattr(res.fattr);
4917 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4919 struct nfs4_exception exception = {
4920 .interruptible = true,
4924 err = nfs4_handle_exception(NFS_SERVER(inode),
4925 _nfs4_proc_link(inode, dir, name),
4927 } while (exception.retry);
4931 struct nfs4_createdata {
4932 struct rpc_message msg;
4933 struct nfs4_create_arg arg;
4934 struct nfs4_create_res res;
4936 struct nfs_fattr fattr;
4937 struct nfs4_label *label;
4940 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4941 const struct qstr *name, struct iattr *sattr, u32 ftype)
4943 struct nfs4_createdata *data;
4945 data = kzalloc(sizeof(*data), GFP_KERNEL);
4947 struct nfs_server *server = NFS_SERVER(dir);
4949 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4950 if (IS_ERR(data->label))
4953 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4954 data->msg.rpc_argp = &data->arg;
4955 data->msg.rpc_resp = &data->res;
4956 data->arg.dir_fh = NFS_FH(dir);
4957 data->arg.server = server;
4958 data->arg.name = name;
4959 data->arg.attrs = sattr;
4960 data->arg.ftype = ftype;
4961 data->arg.bitmask = nfs4_bitmask(server, data->label);
4962 data->arg.umask = current_umask();
4963 data->res.server = server;
4964 data->res.fh = &data->fh;
4965 data->res.fattr = &data->fattr;
4966 data->res.label = data->label;
4967 nfs_fattr_init(data->res.fattr);
4975 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4977 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4978 &data->arg.seq_args, &data->res.seq_res, 1);
4980 spin_lock(&dir->i_lock);
4981 /* Creating a directory bumps nlink in the parent */
4982 if (data->arg.ftype == NF4DIR)
4983 nfs4_inc_nlink_locked(dir);
4984 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4985 data->res.fattr->time_start,
4986 NFS_INO_INVALID_DATA);
4987 spin_unlock(&dir->i_lock);
4988 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4993 static void nfs4_free_createdata(struct nfs4_createdata *data)
4995 nfs4_label_free(data->label);
4999 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5000 struct page *page, unsigned int len, struct iattr *sattr,
5001 struct nfs4_label *label)
5003 struct nfs4_createdata *data;
5004 int status = -ENAMETOOLONG;
5006 if (len > NFS4_MAXPATHLEN)
5010 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
5014 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
5015 data->arg.u.symlink.pages = &page;
5016 data->arg.u.symlink.len = len;
5017 data->arg.label = label;
5019 status = nfs4_do_create(dir, dentry, data);
5021 nfs4_free_createdata(data);
5026 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5027 struct page *page, unsigned int len, struct iattr *sattr)
5029 struct nfs4_exception exception = {
5030 .interruptible = true,
5032 struct nfs4_label l, *label;
5035 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5038 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
5039 trace_nfs4_symlink(dir, &dentry->d_name, err);
5040 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5042 } while (exception.retry);
5044 nfs4_label_release_security(label);
5048 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5049 struct iattr *sattr, struct nfs4_label *label)
5051 struct nfs4_createdata *data;
5052 int status = -ENOMEM;
5054 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
5058 data->arg.label = label;
5059 status = nfs4_do_create(dir, dentry, data);
5061 nfs4_free_createdata(data);
5066 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5067 struct iattr *sattr)
5069 struct nfs_server *server = NFS_SERVER(dir);
5070 struct nfs4_exception exception = {
5071 .interruptible = true,
5073 struct nfs4_label l, *label;
5076 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5078 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5079 sattr->ia_mode &= ~current_umask();
5081 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
5082 trace_nfs4_mkdir(dir, &dentry->d_name, err);
5083 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5085 } while (exception.retry);
5086 nfs4_label_release_security(label);
5091 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5092 struct nfs_readdir_res *nr_res)
5094 struct inode *dir = d_inode(nr_arg->dentry);
5095 struct nfs_server *server = NFS_SERVER(dir);
5096 struct nfs4_readdir_arg args = {
5098 .pages = nr_arg->pages,
5100 .count = nr_arg->page_len,
5101 .plus = nr_arg->plus,
5103 struct nfs4_readdir_res res;
5104 struct rpc_message msg = {
5105 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5108 .rpc_cred = nr_arg->cred,
5112 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5113 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5114 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5115 args.bitmask = server->attr_bitmask_nl;
5117 args.bitmask = server->attr_bitmask;
5119 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5120 res.pgbase = args.pgbase;
5121 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5124 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5125 status += args.pgbase;
5128 nfs_invalidate_atime(dir);
5130 dprintk("%s: returns %d\n", __func__, status);
5134 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5135 struct nfs_readdir_res *res)
5137 struct nfs4_exception exception = {
5138 .interruptible = true,
5142 err = _nfs4_proc_readdir(arg, res);
5143 trace_nfs4_readdir(d_inode(arg->dentry), err);
5144 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5146 } while (exception.retry);
5150 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5151 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5153 struct nfs4_createdata *data;
5154 int mode = sattr->ia_mode;
5155 int status = -ENOMEM;
5157 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5162 data->arg.ftype = NF4FIFO;
5163 else if (S_ISBLK(mode)) {
5164 data->arg.ftype = NF4BLK;
5165 data->arg.u.device.specdata1 = MAJOR(rdev);
5166 data->arg.u.device.specdata2 = MINOR(rdev);
5168 else if (S_ISCHR(mode)) {
5169 data->arg.ftype = NF4CHR;
5170 data->arg.u.device.specdata1 = MAJOR(rdev);
5171 data->arg.u.device.specdata2 = MINOR(rdev);
5172 } else if (!S_ISSOCK(mode)) {
5177 data->arg.label = label;
5178 status = nfs4_do_create(dir, dentry, data);
5180 nfs4_free_createdata(data);
5185 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5186 struct iattr *sattr, dev_t rdev)
5188 struct nfs_server *server = NFS_SERVER(dir);
5189 struct nfs4_exception exception = {
5190 .interruptible = true,
5192 struct nfs4_label l, *label;
5195 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5197 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5198 sattr->ia_mode &= ~current_umask();
5200 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5201 trace_nfs4_mknod(dir, &dentry->d_name, err);
5202 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5204 } while (exception.retry);
5206 nfs4_label_release_security(label);
5211 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5212 struct nfs_fsstat *fsstat)
5214 struct nfs4_statfs_arg args = {
5216 .bitmask = server->attr_bitmask,
5218 struct nfs4_statfs_res res = {
5221 struct rpc_message msg = {
5222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5227 nfs_fattr_init(fsstat->fattr);
5228 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5231 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5233 struct nfs4_exception exception = {
5234 .interruptible = true,
5238 err = nfs4_handle_exception(server,
5239 _nfs4_proc_statfs(server, fhandle, fsstat),
5241 } while (exception.retry);
5245 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5246 struct nfs_fsinfo *fsinfo)
5248 struct nfs4_fsinfo_arg args = {
5250 .bitmask = server->attr_bitmask,
5252 struct nfs4_fsinfo_res res = {
5255 struct rpc_message msg = {
5256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5261 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5264 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5266 struct nfs4_exception exception = {
5267 .interruptible = true,
5272 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5273 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5275 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5278 err = nfs4_handle_exception(server, err, &exception);
5279 } while (exception.retry);
5283 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5287 nfs_fattr_init(fsinfo->fattr);
5288 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5290 /* block layout checks this! */
5291 server->pnfs_blksize = fsinfo->blksize;
5292 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5298 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5299 struct nfs_pathconf *pathconf)
5301 struct nfs4_pathconf_arg args = {
5303 .bitmask = server->attr_bitmask,
5305 struct nfs4_pathconf_res res = {
5306 .pathconf = pathconf,
5308 struct rpc_message msg = {
5309 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5314 /* None of the pathconf attributes are mandatory to implement */
5315 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5316 memset(pathconf, 0, sizeof(*pathconf));
5320 nfs_fattr_init(pathconf->fattr);
5321 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5324 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5325 struct nfs_pathconf *pathconf)
5327 struct nfs4_exception exception = {
5328 .interruptible = true,
5333 err = nfs4_handle_exception(server,
5334 _nfs4_proc_pathconf(server, fhandle, pathconf),
5336 } while (exception.retry);
5340 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5341 const struct nfs_open_context *ctx,
5342 const struct nfs_lock_context *l_ctx,
5345 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5347 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5349 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5350 const struct nfs_open_context *ctx,
5351 const struct nfs_lock_context *l_ctx,
5354 nfs4_stateid _current_stateid;
5356 /* If the current stateid represents a lost lock, then exit */
5357 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5359 return nfs4_stateid_match(stateid, &_current_stateid);
5362 static bool nfs4_error_stateid_expired(int err)
5365 case -NFS4ERR_DELEG_REVOKED:
5366 case -NFS4ERR_ADMIN_REVOKED:
5367 case -NFS4ERR_BAD_STATEID:
5368 case -NFS4ERR_STALE_STATEID:
5369 case -NFS4ERR_OLD_STATEID:
5370 case -NFS4ERR_OPENMODE:
5371 case -NFS4ERR_EXPIRED:
5377 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5379 struct nfs_server *server = NFS_SERVER(hdr->inode);
5381 trace_nfs4_read(hdr, task->tk_status);
5382 if (task->tk_status < 0) {
5383 struct nfs4_exception exception = {
5384 .inode = hdr->inode,
5385 .state = hdr->args.context->state,
5386 .stateid = &hdr->args.stateid,
5388 task->tk_status = nfs4_async_handle_exception(task,
5389 server, task->tk_status, &exception);
5390 if (exception.retry) {
5391 rpc_restart_call_prepare(task);
5396 if (task->tk_status > 0)
5397 renew_lease(server, hdr->timestamp);
5401 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5402 struct nfs_pgio_args *args)
5405 if (!nfs4_error_stateid_expired(task->tk_status) ||
5406 nfs4_stateid_is_current(&args->stateid,
5411 rpc_restart_call_prepare(task);
5415 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5416 struct nfs_pgio_header *hdr)
5418 struct nfs_server *server = NFS_SERVER(hdr->inode);
5419 struct rpc_message *msg = &task->tk_msg;
5421 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5422 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5423 server->caps &= ~NFS_CAP_READ_PLUS;
5424 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5425 rpc_restart_call_prepare(task);
5431 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5433 dprintk("--> %s\n", __func__);
5435 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5437 if (nfs4_read_stateid_changed(task, &hdr->args))
5439 if (nfs4_read_plus_not_supported(task, hdr))
5441 if (task->tk_status > 0)
5442 nfs_invalidate_atime(hdr->inode);
5443 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5444 nfs4_read_done_cb(task, hdr);
5447 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5448 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5449 struct rpc_message *msg)
5451 /* Note: We don't use READ_PLUS with pNFS yet */
5452 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
5453 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5456 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5457 struct rpc_message *msg)
5460 #endif /* CONFIG_NFS_V4_2 */
5462 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5463 struct rpc_message *msg)
5465 hdr->timestamp = jiffies;
5466 if (!hdr->pgio_done_cb)
5467 hdr->pgio_done_cb = nfs4_read_done_cb;
5468 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5469 nfs42_read_plus_support(hdr, msg);
5470 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5473 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5474 struct nfs_pgio_header *hdr)
5476 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5477 &hdr->args.seq_args,
5481 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5482 hdr->args.lock_context,
5483 hdr->rw_mode) == -EIO)
5485 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5490 static int nfs4_write_done_cb(struct rpc_task *task,
5491 struct nfs_pgio_header *hdr)
5493 struct inode *inode = hdr->inode;
5495 trace_nfs4_write(hdr, task->tk_status);
5496 if (task->tk_status < 0) {
5497 struct nfs4_exception exception = {
5498 .inode = hdr->inode,
5499 .state = hdr->args.context->state,
5500 .stateid = &hdr->args.stateid,
5502 task->tk_status = nfs4_async_handle_exception(task,
5503 NFS_SERVER(inode), task->tk_status,
5505 if (exception.retry) {
5506 rpc_restart_call_prepare(task);
5510 if (task->tk_status >= 0) {
5511 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5512 nfs_writeback_update_inode(hdr);
5517 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5518 struct nfs_pgio_args *args)
5521 if (!nfs4_error_stateid_expired(task->tk_status) ||
5522 nfs4_stateid_is_current(&args->stateid,
5527 rpc_restart_call_prepare(task);
5531 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5533 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5535 if (nfs4_write_stateid_changed(task, &hdr->args))
5537 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5538 nfs4_write_done_cb(task, hdr);
5542 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5544 /* Don't request attributes for pNFS or O_DIRECT writes */
5545 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5547 /* Otherwise, request attributes if and only if we don't hold
5550 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5553 static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ], const __u32 *src,
5554 struct inode *inode, struct nfs_server *server,
5555 struct nfs4_label *label)
5557 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
5560 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5562 if (cache_validity & NFS_INO_INVALID_CHANGE)
5563 bitmask[0] |= FATTR4_WORD0_CHANGE;
5564 if (cache_validity & NFS_INO_INVALID_ATIME)
5565 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5566 if (cache_validity & NFS_INO_INVALID_MODE)
5567 bitmask[1] |= FATTR4_WORD1_MODE;
5568 if (cache_validity & NFS_INO_INVALID_OTHER)
5569 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5570 if (cache_validity & NFS_INO_INVALID_NLINK)
5571 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5572 if (label && label->len && cache_validity & NFS_INO_INVALID_LABEL)
5573 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
5574 if (cache_validity & NFS_INO_INVALID_CTIME)
5575 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5576 if (cache_validity & NFS_INO_INVALID_MTIME)
5577 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5578 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5579 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5581 if (cache_validity & NFS_INO_INVALID_SIZE)
5582 bitmask[0] |= FATTR4_WORD0_SIZE;
5584 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5585 bitmask[i] &= server->attr_bitmask[i];
5588 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5589 struct rpc_message *msg,
5590 struct rpc_clnt **clnt)
5592 struct nfs_server *server = NFS_SERVER(hdr->inode);
5594 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5595 hdr->args.bitmask = NULL;
5596 hdr->res.fattr = NULL;
5598 nfs4_bitmask_set(hdr->args.bitmask_store,
5599 server->cache_consistency_bitmask,
5600 hdr->inode, server, NULL);
5601 hdr->args.bitmask = hdr->args.bitmask_store;
5604 if (!hdr->pgio_done_cb)
5605 hdr->pgio_done_cb = nfs4_write_done_cb;
5606 hdr->res.server = server;
5607 hdr->timestamp = jiffies;
5609 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5610 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5611 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5614 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5616 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5617 &data->args.seq_args,
5622 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5624 struct inode *inode = data->inode;
5626 trace_nfs4_commit(data, task->tk_status);
5627 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5628 NULL, NULL) == -EAGAIN) {
5629 rpc_restart_call_prepare(task);
5635 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5637 if (!nfs4_sequence_done(task, &data->res.seq_res))
5639 return data->commit_done_cb(task, data);
5642 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5643 struct rpc_clnt **clnt)
5645 struct nfs_server *server = NFS_SERVER(data->inode);
5647 if (data->commit_done_cb == NULL)
5648 data->commit_done_cb = nfs4_commit_done_cb;
5649 data->res.server = server;
5650 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5651 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5652 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5655 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5656 struct nfs_commitres *res)
5658 struct inode *dst_inode = file_inode(dst);
5659 struct nfs_server *server = NFS_SERVER(dst_inode);
5660 struct rpc_message msg = {
5661 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5666 args->fh = NFS_FH(dst_inode);
5667 return nfs4_call_sync(server->client, server, &msg,
5668 &args->seq_args, &res->seq_res, 1);
5671 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5673 struct nfs_commitargs args = {
5677 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5678 struct nfs4_exception exception = { };
5682 status = _nfs4_proc_commit(dst, &args, res);
5683 status = nfs4_handle_exception(dst_server, status, &exception);
5684 } while (exception.retry);
5689 struct nfs4_renewdata {
5690 struct nfs_client *client;
5691 unsigned long timestamp;
5695 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5696 * standalone procedure for queueing an asynchronous RENEW.
5698 static void nfs4_renew_release(void *calldata)
5700 struct nfs4_renewdata *data = calldata;
5701 struct nfs_client *clp = data->client;
5703 if (refcount_read(&clp->cl_count) > 1)
5704 nfs4_schedule_state_renewal(clp);
5705 nfs_put_client(clp);
5709 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5711 struct nfs4_renewdata *data = calldata;
5712 struct nfs_client *clp = data->client;
5713 unsigned long timestamp = data->timestamp;
5715 trace_nfs4_renew_async(clp, task->tk_status);
5716 switch (task->tk_status) {
5719 case -NFS4ERR_LEASE_MOVED:
5720 nfs4_schedule_lease_moved_recovery(clp);
5723 /* Unless we're shutting down, schedule state recovery! */
5724 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5726 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5727 nfs4_schedule_lease_recovery(clp);
5730 nfs4_schedule_path_down_recovery(clp);
5732 do_renew_lease(clp, timestamp);
5735 static const struct rpc_call_ops nfs4_renew_ops = {
5736 .rpc_call_done = nfs4_renew_done,
5737 .rpc_release = nfs4_renew_release,
5740 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5742 struct rpc_message msg = {
5743 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5747 struct nfs4_renewdata *data;
5749 if (renew_flags == 0)
5751 if (!refcount_inc_not_zero(&clp->cl_count))
5753 data = kmalloc(sizeof(*data), GFP_NOFS);
5755 nfs_put_client(clp);
5759 data->timestamp = jiffies;
5760 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5761 &nfs4_renew_ops, data);
5764 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5766 struct rpc_message msg = {
5767 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5771 unsigned long now = jiffies;
5774 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5777 do_renew_lease(clp, now);
5781 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5783 return server->caps & NFS_CAP_ACLS;
5786 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5787 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5790 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5792 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5793 struct page **pages)
5795 struct page *newpage, **spages;
5801 len = min_t(size_t, PAGE_SIZE, buflen);
5802 newpage = alloc_page(GFP_KERNEL);
5804 if (newpage == NULL)
5806 memcpy(page_address(newpage), buf, len);
5811 } while (buflen != 0);
5817 __free_page(spages[rc-1]);
5821 struct nfs4_cached_acl {
5827 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5829 struct nfs_inode *nfsi = NFS_I(inode);
5831 spin_lock(&inode->i_lock);
5832 kfree(nfsi->nfs4_acl);
5833 nfsi->nfs4_acl = acl;
5834 spin_unlock(&inode->i_lock);
5837 static void nfs4_zap_acl_attr(struct inode *inode)
5839 nfs4_set_cached_acl(inode, NULL);
5842 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5844 struct nfs_inode *nfsi = NFS_I(inode);
5845 struct nfs4_cached_acl *acl;
5848 spin_lock(&inode->i_lock);
5849 acl = nfsi->nfs4_acl;
5852 if (buf == NULL) /* user is just asking for length */
5854 if (acl->cached == 0)
5856 ret = -ERANGE; /* see getxattr(2) man page */
5857 if (acl->len > buflen)
5859 memcpy(buf, acl->data, acl->len);
5863 spin_unlock(&inode->i_lock);
5867 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5869 struct nfs4_cached_acl *acl;
5870 size_t buflen = sizeof(*acl) + acl_len;
5872 if (buflen <= PAGE_SIZE) {
5873 acl = kmalloc(buflen, GFP_KERNEL);
5877 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5879 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5886 nfs4_set_cached_acl(inode, acl);
5890 * The getxattr API returns the required buffer length when called with a
5891 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5892 * the required buf. On a NULL buf, we send a page of data to the server
5893 * guessing that the ACL request can be serviced by a page. If so, we cache
5894 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5895 * the cache. If not so, we throw away the page, and cache the required
5896 * length. The next getxattr call will then produce another round trip to
5897 * the server, this time with the input buf of the required size.
5899 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5901 struct page **pages;
5902 struct nfs_getaclargs args = {
5903 .fh = NFS_FH(inode),
5906 struct nfs_getaclres res = {
5909 struct rpc_message msg = {
5910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5914 unsigned int npages;
5915 int ret = -ENOMEM, i;
5916 struct nfs_server *server = NFS_SERVER(inode);
5919 buflen = server->rsize;
5921 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5922 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
5926 args.acl_pages = pages;
5928 for (i = 0; i < npages; i++) {
5929 pages[i] = alloc_page(GFP_KERNEL);
5934 /* for decoding across pages */
5935 res.acl_scratch = alloc_page(GFP_KERNEL);
5936 if (!res.acl_scratch)
5939 args.acl_len = npages * PAGE_SIZE;
5941 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5942 __func__, buf, buflen, npages, args.acl_len);
5943 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5944 &msg, &args.seq_args, &res.seq_res, 0);
5948 /* Handle the case where the passed-in buffer is too short */
5949 if (res.acl_flags & NFS4_ACL_TRUNC) {
5950 /* Did the user only issue a request for the acl length? */
5956 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5958 if (res.acl_len > buflen) {
5962 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5968 __free_page(pages[i]);
5969 if (res.acl_scratch)
5970 __free_page(res.acl_scratch);
5975 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5977 struct nfs4_exception exception = {
5978 .interruptible = true,
5982 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5983 trace_nfs4_get_acl(inode, ret);
5986 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5987 } while (exception.retry);
5991 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5993 struct nfs_server *server = NFS_SERVER(inode);
5996 if (!nfs4_server_supports_acls(server))
5998 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
6001 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
6002 nfs_zap_acl_cache(inode);
6003 ret = nfs4_read_cached_acl(inode, buf, buflen);
6005 /* -ENOENT is returned if there is no ACL or if there is an ACL
6006 * but no cached acl data, just the acl length */
6008 return nfs4_get_acl_uncached(inode, buf, buflen);
6011 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
6013 struct nfs_server *server = NFS_SERVER(inode);
6014 struct page *pages[NFS4ACL_MAXPAGES];
6015 struct nfs_setaclargs arg = {
6016 .fh = NFS_FH(inode),
6020 struct nfs_setaclres res;
6021 struct rpc_message msg = {
6022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
6026 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
6029 /* You can't remove system.nfs4_acl: */
6032 if (!nfs4_server_supports_acls(server))
6034 if (npages > ARRAY_SIZE(pages))
6036 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
6039 nfs4_inode_make_writeable(inode);
6040 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6043 * Free each page after tx, so the only ref left is
6044 * held by the network stack
6047 put_page(pages[i-1]);
6050 * Acl update can result in inode attribute update.
6051 * so mark the attribute cache invalid.
6053 spin_lock(&inode->i_lock);
6054 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
6055 NFS_INO_INVALID_CTIME |
6056 NFS_INO_REVAL_FORCED);
6057 spin_unlock(&inode->i_lock);
6058 nfs_access_zap_cache(inode);
6059 nfs_zap_acl_cache(inode);
6063 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
6065 struct nfs4_exception exception = { };
6068 err = __nfs4_proc_set_acl(inode, buf, buflen);
6069 trace_nfs4_set_acl(inode, err);
6070 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
6072 * no need to retry since the kernel
6073 * isn't involved in encoding the ACEs.
6078 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6080 } while (exception.retry);
6084 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6085 static int _nfs4_get_security_label(struct inode *inode, void *buf,
6088 struct nfs_server *server = NFS_SERVER(inode);
6089 struct nfs_fattr fattr;
6090 struct nfs4_label label = {0, 0, buflen, buf};
6092 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6093 struct nfs4_getattr_arg arg = {
6094 .fh = NFS_FH(inode),
6097 struct nfs4_getattr_res res = {
6102 struct rpc_message msg = {
6103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6109 nfs_fattr_init(&fattr);
6111 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6114 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6119 static int nfs4_get_security_label(struct inode *inode, void *buf,
6122 struct nfs4_exception exception = {
6123 .interruptible = true,
6127 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6131 err = _nfs4_get_security_label(inode, buf, buflen);
6132 trace_nfs4_get_security_label(inode, err);
6133 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6135 } while (exception.retry);
6139 static int _nfs4_do_set_security_label(struct inode *inode,
6140 struct nfs4_label *ilabel,
6141 struct nfs_fattr *fattr,
6142 struct nfs4_label *olabel)
6145 struct iattr sattr = {0};
6146 struct nfs_server *server = NFS_SERVER(inode);
6147 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6148 struct nfs_setattrargs arg = {
6149 .fh = NFS_FH(inode),
6155 struct nfs_setattrres res = {
6160 struct rpc_message msg = {
6161 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6167 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6169 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6171 dprintk("%s failed: %d\n", __func__, status);
6176 static int nfs4_do_set_security_label(struct inode *inode,
6177 struct nfs4_label *ilabel,
6178 struct nfs_fattr *fattr,
6179 struct nfs4_label *olabel)
6181 struct nfs4_exception exception = { };
6185 err = _nfs4_do_set_security_label(inode, ilabel,
6187 trace_nfs4_set_security_label(inode, err);
6188 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6190 } while (exception.retry);
6195 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6197 struct nfs4_label ilabel, *olabel = NULL;
6198 struct nfs_fattr fattr;
6201 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6204 nfs_fattr_init(&fattr);
6208 ilabel.label = (char *)buf;
6209 ilabel.len = buflen;
6211 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6212 if (IS_ERR(olabel)) {
6213 status = -PTR_ERR(olabel);
6217 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6219 nfs_setsecurity(inode, &fattr, olabel);
6221 nfs4_label_free(olabel);
6225 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6228 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6229 nfs4_verifier *bootverf)
6233 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6234 /* An impossible timestamp guarantees this value
6235 * will never match a generated boot time. */
6236 verf[0] = cpu_to_be32(U32_MAX);
6237 verf[1] = cpu_to_be32(U32_MAX);
6239 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6240 u64 ns = ktime_to_ns(nn->boot_time);
6242 verf[0] = cpu_to_be32(ns >> 32);
6243 verf[1] = cpu_to_be32(ns);
6245 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6249 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6251 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6252 struct nfs_netns_client *nn_clp = nn->nfs_client;
6259 id = rcu_dereference(nn_clp->identifier);
6261 strscpy(buf, id, buflen);
6265 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6266 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6272 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6274 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6279 if (clp->cl_owner_id != NULL)
6284 strlen(clp->cl_rpcclient->cl_nodename) +
6286 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6290 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6294 if (len > NFS4_OPAQUE_LIMIT + 1)
6298 * Since this string is allocated at mount time, and held until the
6299 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6300 * about a memory-reclaim deadlock.
6302 str = kmalloc(len, GFP_KERNEL);
6308 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6309 clp->cl_rpcclient->cl_nodename, buf,
6310 rpc_peeraddr2str(clp->cl_rpcclient,
6313 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6314 clp->cl_rpcclient->cl_nodename,
6315 rpc_peeraddr2str(clp->cl_rpcclient,
6319 clp->cl_owner_id = str;
6324 nfs4_init_uniform_client_string(struct nfs_client *clp)
6326 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6331 if (clp->cl_owner_id != NULL)
6334 len = 10 + 10 + 1 + 10 + 1 +
6335 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6337 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6341 if (len > NFS4_OPAQUE_LIMIT + 1)
6345 * Since this string is allocated at mount time, and held until the
6346 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6347 * about a memory-reclaim deadlock.
6349 str = kmalloc(len, GFP_KERNEL);
6354 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6355 clp->rpc_ops->version, clp->cl_minorversion,
6356 buf, clp->cl_rpcclient->cl_nodename);
6358 scnprintf(str, len, "Linux NFSv%u.%u %s",
6359 clp->rpc_ops->version, clp->cl_minorversion,
6360 clp->cl_rpcclient->cl_nodename);
6361 clp->cl_owner_id = str;
6366 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6367 * services. Advertise one based on the address family of the
6371 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6373 if (strchr(clp->cl_ipaddr, ':') != NULL)
6374 return scnprintf(buf, len, "tcp6");
6376 return scnprintf(buf, len, "tcp");
6379 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6381 struct nfs4_setclientid *sc = calldata;
6383 if (task->tk_status == 0)
6384 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6387 static const struct rpc_call_ops nfs4_setclientid_ops = {
6388 .rpc_call_done = nfs4_setclientid_done,
6392 * nfs4_proc_setclientid - Negotiate client ID
6393 * @clp: state data structure
6394 * @program: RPC program for NFSv4 callback service
6395 * @port: IP port number for NFS4 callback service
6396 * @cred: credential to use for this call
6397 * @res: where to place the result
6399 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6401 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6402 unsigned short port, const struct cred *cred,
6403 struct nfs4_setclientid_res *res)
6405 nfs4_verifier sc_verifier;
6406 struct nfs4_setclientid setclientid = {
6407 .sc_verifier = &sc_verifier,
6411 struct rpc_message msg = {
6412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6413 .rpc_argp = &setclientid,
6417 struct rpc_task_setup task_setup_data = {
6418 .rpc_client = clp->cl_rpcclient,
6419 .rpc_message = &msg,
6420 .callback_ops = &nfs4_setclientid_ops,
6421 .callback_data = &setclientid,
6422 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6424 unsigned long now = jiffies;
6427 /* nfs_client_id4 */
6428 nfs4_init_boot_verifier(clp, &sc_verifier);
6430 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6431 status = nfs4_init_uniform_client_string(clp);
6433 status = nfs4_init_nonuniform_client_string(clp);
6439 setclientid.sc_netid_len =
6440 nfs4_init_callback_netid(clp,
6441 setclientid.sc_netid,
6442 sizeof(setclientid.sc_netid));
6443 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6444 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6445 clp->cl_ipaddr, port >> 8, port & 255);
6447 dprintk("NFS call setclientid auth=%s, '%s'\n",
6448 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6451 status = nfs4_call_sync_custom(&task_setup_data);
6452 if (setclientid.sc_cred) {
6453 kfree(clp->cl_acceptor);
6454 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6455 put_rpccred(setclientid.sc_cred);
6459 do_renew_lease(clp, now);
6461 trace_nfs4_setclientid(clp, status);
6462 dprintk("NFS reply setclientid: %d\n", status);
6467 * nfs4_proc_setclientid_confirm - Confirm client ID
6468 * @clp: state data structure
6469 * @arg: result of a previous SETCLIENTID
6470 * @cred: credential to use for this call
6472 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6474 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6475 struct nfs4_setclientid_res *arg,
6476 const struct cred *cred)
6478 struct rpc_message msg = {
6479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6485 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6486 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6488 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6489 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6490 trace_nfs4_setclientid_confirm(clp, status);
6491 dprintk("NFS reply setclientid_confirm: %d\n", status);
6495 struct nfs4_delegreturndata {
6496 struct nfs4_delegreturnargs args;
6497 struct nfs4_delegreturnres res;
6499 nfs4_stateid stateid;
6500 unsigned long timestamp;
6502 struct nfs4_layoutreturn_args arg;
6503 struct nfs4_layoutreturn_res res;
6504 struct nfs4_xdr_opaque_data ld_private;
6508 struct nfs_fattr fattr;
6510 struct inode *inode;
6513 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6515 struct nfs4_delegreturndata *data = calldata;
6516 struct nfs4_exception exception = {
6517 .inode = data->inode,
6518 .stateid = &data->stateid,
6519 .task_is_privileged = data->args.seq_args.sa_privileged,
6522 if (!nfs4_sequence_done(task, &data->res.seq_res))
6525 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6527 /* Handle Layoutreturn errors */
6528 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6529 &data->res.lr_ret) == -EAGAIN)
6532 switch (task->tk_status) {
6534 renew_lease(data->res.server, data->timestamp);
6536 case -NFS4ERR_ADMIN_REVOKED:
6537 case -NFS4ERR_DELEG_REVOKED:
6538 case -NFS4ERR_EXPIRED:
6539 nfs4_free_revoked_stateid(data->res.server,
6541 task->tk_msg.rpc_cred);
6543 case -NFS4ERR_BAD_STATEID:
6544 case -NFS4ERR_STALE_STATEID:
6546 task->tk_status = 0;
6548 case -NFS4ERR_OLD_STATEID:
6549 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6550 nfs4_stateid_seqid_inc(&data->stateid);
6551 if (data->args.bitmask) {
6552 data->args.bitmask = NULL;
6553 data->res.fattr = NULL;
6556 case -NFS4ERR_ACCESS:
6557 if (data->args.bitmask) {
6558 data->args.bitmask = NULL;
6559 data->res.fattr = NULL;
6564 task->tk_status = nfs4_async_handle_exception(task,
6565 data->res.server, task->tk_status,
6567 if (exception.retry)
6570 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6571 data->rpc_status = task->tk_status;
6574 task->tk_status = 0;
6575 rpc_restart_call_prepare(task);
6578 static void nfs4_delegreturn_release(void *calldata)
6580 struct nfs4_delegreturndata *data = calldata;
6581 struct inode *inode = data->inode;
6584 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6587 nfs4_fattr_set_prechange(&data->fattr,
6588 inode_peek_iversion_raw(inode));
6589 nfs_refresh_inode(inode, &data->fattr);
6590 nfs_iput_and_deactive(inode);
6595 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6597 struct nfs4_delegreturndata *d_data;
6598 struct pnfs_layout_hdr *lo;
6600 d_data = (struct nfs4_delegreturndata *)data;
6602 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6603 nfs4_sequence_done(task, &d_data->res.seq_res);
6607 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6608 if (lo && !pnfs_layout_is_valid(lo)) {
6609 d_data->args.lr_args = NULL;
6610 d_data->res.lr_res = NULL;
6613 nfs4_setup_sequence(d_data->res.server->nfs_client,
6614 &d_data->args.seq_args,
6615 &d_data->res.seq_res,
6619 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6620 .rpc_call_prepare = nfs4_delegreturn_prepare,
6621 .rpc_call_done = nfs4_delegreturn_done,
6622 .rpc_release = nfs4_delegreturn_release,
6625 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6627 struct nfs4_delegreturndata *data;
6628 struct nfs_server *server = NFS_SERVER(inode);
6629 struct rpc_task *task;
6630 struct rpc_message msg = {
6631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6634 struct rpc_task_setup task_setup_data = {
6635 .rpc_client = server->client,
6636 .rpc_message = &msg,
6637 .callback_ops = &nfs4_delegreturn_ops,
6638 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6642 if (nfs_server_capable(inode, NFS_CAP_MOVEABLE))
6643 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6645 data = kzalloc(sizeof(*data), GFP_KERNEL);
6649 nfs4_state_protect(server->nfs_client,
6650 NFS_SP4_MACH_CRED_CLEANUP,
6651 &task_setup_data.rpc_client, &msg);
6653 data->args.fhandle = &data->fh;
6654 data->args.stateid = &data->stateid;
6655 nfs4_bitmask_set(data->args.bitmask_store,
6656 server->cache_consistency_bitmask, inode, server,
6658 data->args.bitmask = data->args.bitmask_store;
6659 nfs_copy_fh(&data->fh, NFS_FH(inode));
6660 nfs4_stateid_copy(&data->stateid, stateid);
6661 data->res.fattr = &data->fattr;
6662 data->res.server = server;
6663 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6664 data->lr.arg.ld_private = &data->lr.ld_private;
6665 nfs_fattr_init(data->res.fattr);
6666 data->timestamp = jiffies;
6667 data->rpc_status = 0;
6668 data->inode = nfs_igrab_and_active(inode);
6669 if (data->inode || issync) {
6670 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6673 data->args.lr_args = &data->lr.arg;
6674 data->res.lr_res = &data->lr.res;
6679 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6682 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6684 task_setup_data.callback_data = data;
6685 msg.rpc_argp = &data->args;
6686 msg.rpc_resp = &data->res;
6687 task = rpc_run_task(&task_setup_data);
6689 return PTR_ERR(task);
6692 status = rpc_wait_for_completion_task(task);
6695 status = data->rpc_status;
6701 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6703 struct nfs_server *server = NFS_SERVER(inode);
6704 struct nfs4_exception exception = { };
6707 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6708 trace_nfs4_delegreturn(inode, stateid, err);
6710 case -NFS4ERR_STALE_STATEID:
6711 case -NFS4ERR_EXPIRED:
6715 err = nfs4_handle_exception(server, err, &exception);
6716 } while (exception.retry);
6720 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6722 struct inode *inode = state->inode;
6723 struct nfs_server *server = NFS_SERVER(inode);
6724 struct nfs_client *clp = server->nfs_client;
6725 struct nfs_lockt_args arg = {
6726 .fh = NFS_FH(inode),
6729 struct nfs_lockt_res res = {
6732 struct rpc_message msg = {
6733 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6736 .rpc_cred = state->owner->so_cred,
6738 struct nfs4_lock_state *lsp;
6741 arg.lock_owner.clientid = clp->cl_clientid;
6742 status = nfs4_set_lock_state(state, request);
6745 lsp = request->fl_u.nfs4_fl.owner;
6746 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6747 arg.lock_owner.s_dev = server->s_dev;
6748 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6751 request->fl_type = F_UNLCK;
6753 case -NFS4ERR_DENIED:
6756 request->fl_ops->fl_release_private(request);
6757 request->fl_ops = NULL;
6762 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6764 struct nfs4_exception exception = {
6765 .interruptible = true,
6770 err = _nfs4_proc_getlk(state, cmd, request);
6771 trace_nfs4_get_lock(request, state, cmd, err);
6772 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6774 } while (exception.retry);
6779 * Update the seqid of a lock stateid after receiving
6780 * NFS4ERR_OLD_STATEID
6782 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6783 struct nfs4_lock_state *lsp)
6785 struct nfs4_state *state = lsp->ls_state;
6788 spin_lock(&state->state_lock);
6789 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6791 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6792 nfs4_stateid_seqid_inc(dst);
6794 dst->seqid = lsp->ls_stateid.seqid;
6797 spin_unlock(&state->state_lock);
6801 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6802 struct nfs4_lock_state *lsp)
6804 struct nfs4_state *state = lsp->ls_state;
6807 spin_lock(&state->state_lock);
6808 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6809 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6810 spin_unlock(&state->state_lock);
6814 struct nfs4_unlockdata {
6815 struct nfs_locku_args arg;
6816 struct nfs_locku_res res;
6817 struct nfs4_lock_state *lsp;
6818 struct nfs_open_context *ctx;
6819 struct nfs_lock_context *l_ctx;
6820 struct file_lock fl;
6821 struct nfs_server *server;
6822 unsigned long timestamp;
6825 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6826 struct nfs_open_context *ctx,
6827 struct nfs4_lock_state *lsp,
6828 struct nfs_seqid *seqid)
6830 struct nfs4_unlockdata *p;
6831 struct nfs4_state *state = lsp->ls_state;
6832 struct inode *inode = state->inode;
6834 p = kzalloc(sizeof(*p), GFP_KERNEL);
6837 p->arg.fh = NFS_FH(inode);
6839 p->arg.seqid = seqid;
6840 p->res.seqid = seqid;
6842 /* Ensure we don't close file until we're done freeing locks! */
6843 p->ctx = get_nfs_open_context(ctx);
6844 p->l_ctx = nfs_get_lock_context(ctx);
6845 locks_init_lock(&p->fl);
6846 locks_copy_lock(&p->fl, fl);
6847 p->server = NFS_SERVER(inode);
6848 spin_lock(&state->state_lock);
6849 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6850 spin_unlock(&state->state_lock);
6854 static void nfs4_locku_release_calldata(void *data)
6856 struct nfs4_unlockdata *calldata = data;
6857 nfs_free_seqid(calldata->arg.seqid);
6858 nfs4_put_lock_state(calldata->lsp);
6859 nfs_put_lock_context(calldata->l_ctx);
6860 put_nfs_open_context(calldata->ctx);
6864 static void nfs4_locku_done(struct rpc_task *task, void *data)
6866 struct nfs4_unlockdata *calldata = data;
6867 struct nfs4_exception exception = {
6868 .inode = calldata->lsp->ls_state->inode,
6869 .stateid = &calldata->arg.stateid,
6872 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6874 switch (task->tk_status) {
6876 renew_lease(calldata->server, calldata->timestamp);
6877 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6878 if (nfs4_update_lock_stateid(calldata->lsp,
6879 &calldata->res.stateid))
6882 case -NFS4ERR_ADMIN_REVOKED:
6883 case -NFS4ERR_EXPIRED:
6884 nfs4_free_revoked_stateid(calldata->server,
6885 &calldata->arg.stateid,
6886 task->tk_msg.rpc_cred);
6888 case -NFS4ERR_BAD_STATEID:
6889 case -NFS4ERR_STALE_STATEID:
6890 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6892 rpc_restart_call_prepare(task);
6894 case -NFS4ERR_OLD_STATEID:
6895 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6897 rpc_restart_call_prepare(task);
6900 task->tk_status = nfs4_async_handle_exception(task,
6901 calldata->server, task->tk_status,
6903 if (exception.retry)
6904 rpc_restart_call_prepare(task);
6906 nfs_release_seqid(calldata->arg.seqid);
6909 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6911 struct nfs4_unlockdata *calldata = data;
6913 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6914 nfs_async_iocounter_wait(task, calldata->l_ctx))
6917 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6919 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6920 /* Note: exit _without_ running nfs4_locku_done */
6923 calldata->timestamp = jiffies;
6924 if (nfs4_setup_sequence(calldata->server->nfs_client,
6925 &calldata->arg.seq_args,
6926 &calldata->res.seq_res,
6928 nfs_release_seqid(calldata->arg.seqid);
6931 task->tk_action = NULL;
6933 nfs4_sequence_done(task, &calldata->res.seq_res);
6936 static const struct rpc_call_ops nfs4_locku_ops = {
6937 .rpc_call_prepare = nfs4_locku_prepare,
6938 .rpc_call_done = nfs4_locku_done,
6939 .rpc_release = nfs4_locku_release_calldata,
6942 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6943 struct nfs_open_context *ctx,
6944 struct nfs4_lock_state *lsp,
6945 struct nfs_seqid *seqid)
6947 struct nfs4_unlockdata *data;
6948 struct rpc_message msg = {
6949 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6950 .rpc_cred = ctx->cred,
6952 struct rpc_task_setup task_setup_data = {
6953 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6954 .rpc_message = &msg,
6955 .callback_ops = &nfs4_locku_ops,
6956 .workqueue = nfsiod_workqueue,
6957 .flags = RPC_TASK_ASYNC,
6960 if (nfs_server_capable(lsp->ls_state->inode, NFS_CAP_MOVEABLE))
6961 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6963 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6964 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6966 /* Ensure this is an unlock - when canceling a lock, the
6967 * canceled lock is passed in, and it won't be an unlock.
6969 fl->fl_type = F_UNLCK;
6970 if (fl->fl_flags & FL_CLOSE)
6971 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6973 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6975 nfs_free_seqid(seqid);
6976 return ERR_PTR(-ENOMEM);
6979 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6980 msg.rpc_argp = &data->arg;
6981 msg.rpc_resp = &data->res;
6982 task_setup_data.callback_data = data;
6983 return rpc_run_task(&task_setup_data);
6986 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6988 struct inode *inode = state->inode;
6989 struct nfs4_state_owner *sp = state->owner;
6990 struct nfs_inode *nfsi = NFS_I(inode);
6991 struct nfs_seqid *seqid;
6992 struct nfs4_lock_state *lsp;
6993 struct rpc_task *task;
6994 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6996 unsigned char fl_flags = request->fl_flags;
6998 status = nfs4_set_lock_state(state, request);
6999 /* Unlock _before_ we do the RPC call */
7000 request->fl_flags |= FL_EXISTS;
7001 /* Exclude nfs_delegation_claim_locks() */
7002 mutex_lock(&sp->so_delegreturn_mutex);
7003 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
7004 down_read(&nfsi->rwsem);
7005 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
7006 up_read(&nfsi->rwsem);
7007 mutex_unlock(&sp->so_delegreturn_mutex);
7010 up_read(&nfsi->rwsem);
7011 mutex_unlock(&sp->so_delegreturn_mutex);
7014 /* Is this a delegated lock? */
7015 lsp = request->fl_u.nfs4_fl.owner;
7016 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
7018 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
7019 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
7023 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
7024 status = PTR_ERR(task);
7027 status = rpc_wait_for_completion_task(task);
7030 request->fl_flags = fl_flags;
7031 trace_nfs4_unlock(request, state, F_SETLK, status);
7035 struct nfs4_lockdata {
7036 struct nfs_lock_args arg;
7037 struct nfs_lock_res res;
7038 struct nfs4_lock_state *lsp;
7039 struct nfs_open_context *ctx;
7040 struct file_lock fl;
7041 unsigned long timestamp;
7044 struct nfs_server *server;
7047 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
7048 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
7051 struct nfs4_lockdata *p;
7052 struct inode *inode = lsp->ls_state->inode;
7053 struct nfs_server *server = NFS_SERVER(inode);
7054 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7056 p = kzalloc(sizeof(*p), gfp_mask);
7060 p->arg.fh = NFS_FH(inode);
7062 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
7063 if (IS_ERR(p->arg.open_seqid))
7065 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
7066 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
7067 if (IS_ERR(p->arg.lock_seqid))
7068 goto out_free_seqid;
7069 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
7070 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
7071 p->arg.lock_owner.s_dev = server->s_dev;
7072 p->res.lock_seqid = p->arg.lock_seqid;
7075 p->ctx = get_nfs_open_context(ctx);
7076 locks_init_lock(&p->fl);
7077 locks_copy_lock(&p->fl, fl);
7080 nfs_free_seqid(p->arg.open_seqid);
7086 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7088 struct nfs4_lockdata *data = calldata;
7089 struct nfs4_state *state = data->lsp->ls_state;
7091 dprintk("%s: begin!\n", __func__);
7092 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
7094 /* Do we need to do an open_to_lock_owner? */
7095 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
7096 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
7097 goto out_release_lock_seqid;
7099 nfs4_stateid_copy(&data->arg.open_stateid,
7100 &state->open_stateid);
7101 data->arg.new_lock_owner = 1;
7102 data->res.open_seqid = data->arg.open_seqid;
7104 data->arg.new_lock_owner = 0;
7105 nfs4_stateid_copy(&data->arg.lock_stateid,
7106 &data->lsp->ls_stateid);
7108 if (!nfs4_valid_open_stateid(state)) {
7109 data->rpc_status = -EBADF;
7110 task->tk_action = NULL;
7111 goto out_release_open_seqid;
7113 data->timestamp = jiffies;
7114 if (nfs4_setup_sequence(data->server->nfs_client,
7115 &data->arg.seq_args,
7119 out_release_open_seqid:
7120 nfs_release_seqid(data->arg.open_seqid);
7121 out_release_lock_seqid:
7122 nfs_release_seqid(data->arg.lock_seqid);
7124 nfs4_sequence_done(task, &data->res.seq_res);
7125 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
7128 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7130 struct nfs4_lockdata *data = calldata;
7131 struct nfs4_lock_state *lsp = data->lsp;
7132 struct nfs_server *server = NFS_SERVER(d_inode(data->ctx->dentry));
7134 dprintk("%s: begin!\n", __func__);
7136 if (!nfs4_sequence_done(task, &data->res.seq_res))
7139 data->rpc_status = task->tk_status;
7140 switch (task->tk_status) {
7142 renew_lease(server, data->timestamp);
7143 if (data->arg.new_lock && !data->cancelled) {
7144 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
7145 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7148 if (data->arg.new_lock_owner != 0) {
7149 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7150 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7151 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7152 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7155 case -NFS4ERR_OLD_STATEID:
7156 if (data->arg.new_lock_owner != 0 &&
7157 nfs4_refresh_open_old_stateid(&data->arg.open_stateid,
7160 if (nfs4_refresh_lock_old_stateid(&data->arg.lock_stateid, lsp))
7163 case -NFS4ERR_BAD_STATEID:
7164 case -NFS4ERR_STALE_STATEID:
7165 case -NFS4ERR_EXPIRED:
7166 if (data->arg.new_lock_owner != 0) {
7167 if (!nfs4_stateid_match(&data->arg.open_stateid,
7168 &lsp->ls_state->open_stateid))
7170 else if (nfs4_async_handle_error(task, server, lsp->ls_state, NULL) == -EAGAIN)
7172 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7177 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
7180 if (!data->cancelled)
7181 rpc_restart_call_prepare(task);
7185 static void nfs4_lock_release(void *calldata)
7187 struct nfs4_lockdata *data = calldata;
7189 dprintk("%s: begin!\n", __func__);
7190 nfs_free_seqid(data->arg.open_seqid);
7191 if (data->cancelled && data->rpc_status == 0) {
7192 struct rpc_task *task;
7193 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7194 data->arg.lock_seqid);
7196 rpc_put_task_async(task);
7197 dprintk("%s: cancelling lock!\n", __func__);
7199 nfs_free_seqid(data->arg.lock_seqid);
7200 nfs4_put_lock_state(data->lsp);
7201 put_nfs_open_context(data->ctx);
7203 dprintk("%s: done!\n", __func__);
7206 static const struct rpc_call_ops nfs4_lock_ops = {
7207 .rpc_call_prepare = nfs4_lock_prepare,
7208 .rpc_call_done = nfs4_lock_done,
7209 .rpc_release = nfs4_lock_release,
7212 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7215 case -NFS4ERR_ADMIN_REVOKED:
7216 case -NFS4ERR_EXPIRED:
7217 case -NFS4ERR_BAD_STATEID:
7218 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7219 if (new_lock_owner != 0 ||
7220 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7221 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7223 case -NFS4ERR_STALE_STATEID:
7224 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7225 nfs4_schedule_lease_recovery(server->nfs_client);
7229 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7231 struct nfs4_lockdata *data;
7232 struct rpc_task *task;
7233 struct rpc_message msg = {
7234 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7235 .rpc_cred = state->owner->so_cred,
7237 struct rpc_task_setup task_setup_data = {
7238 .rpc_client = NFS_CLIENT(state->inode),
7239 .rpc_message = &msg,
7240 .callback_ops = &nfs4_lock_ops,
7241 .workqueue = nfsiod_workqueue,
7242 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7246 if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
7247 task_setup_data.flags |= RPC_TASK_MOVEABLE;
7249 dprintk("%s: begin!\n", __func__);
7250 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7251 fl->fl_u.nfs4_fl.owner, GFP_KERNEL);
7255 data->arg.block = 1;
7256 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7257 recovery_type > NFS_LOCK_NEW);
7258 msg.rpc_argp = &data->arg;
7259 msg.rpc_resp = &data->res;
7260 task_setup_data.callback_data = data;
7261 if (recovery_type > NFS_LOCK_NEW) {
7262 if (recovery_type == NFS_LOCK_RECLAIM)
7263 data->arg.reclaim = NFS_LOCK_RECLAIM;
7265 data->arg.new_lock = 1;
7266 task = rpc_run_task(&task_setup_data);
7268 return PTR_ERR(task);
7269 ret = rpc_wait_for_completion_task(task);
7271 ret = data->rpc_status;
7273 nfs4_handle_setlk_error(data->server, data->lsp,
7274 data->arg.new_lock_owner, ret);
7276 data->cancelled = true;
7277 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7279 dprintk("%s: done, ret = %d!\n", __func__, ret);
7283 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7285 struct nfs_server *server = NFS_SERVER(state->inode);
7286 struct nfs4_exception exception = {
7287 .inode = state->inode,
7292 /* Cache the lock if possible... */
7293 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7295 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7296 if (err != -NFS4ERR_DELAY)
7298 nfs4_handle_exception(server, err, &exception);
7299 } while (exception.retry);
7303 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7305 struct nfs_server *server = NFS_SERVER(state->inode);
7306 struct nfs4_exception exception = {
7307 .inode = state->inode,
7311 err = nfs4_set_lock_state(state, request);
7314 if (!recover_lost_locks) {
7315 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7319 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7321 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7325 case -NFS4ERR_GRACE:
7326 case -NFS4ERR_DELAY:
7327 nfs4_handle_exception(server, err, &exception);
7330 } while (exception.retry);
7335 #if defined(CONFIG_NFS_V4_1)
7336 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7338 struct nfs4_lock_state *lsp;
7341 status = nfs4_set_lock_state(state, request);
7344 lsp = request->fl_u.nfs4_fl.owner;
7345 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7346 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7348 return nfs4_lock_expired(state, request);
7352 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7354 struct nfs_inode *nfsi = NFS_I(state->inode);
7355 struct nfs4_state_owner *sp = state->owner;
7356 unsigned char fl_flags = request->fl_flags;
7359 request->fl_flags |= FL_ACCESS;
7360 status = locks_lock_inode_wait(state->inode, request);
7363 mutex_lock(&sp->so_delegreturn_mutex);
7364 down_read(&nfsi->rwsem);
7365 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7366 /* Yes: cache locks! */
7367 /* ...but avoid races with delegation recall... */
7368 request->fl_flags = fl_flags & ~FL_SLEEP;
7369 status = locks_lock_inode_wait(state->inode, request);
7370 up_read(&nfsi->rwsem);
7371 mutex_unlock(&sp->so_delegreturn_mutex);
7374 up_read(&nfsi->rwsem);
7375 mutex_unlock(&sp->so_delegreturn_mutex);
7376 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7378 request->fl_flags = fl_flags;
7382 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7384 struct nfs4_exception exception = {
7386 .inode = state->inode,
7387 .interruptible = true,
7392 err = _nfs4_proc_setlk(state, cmd, request);
7393 if (err == -NFS4ERR_DENIED)
7395 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7397 } while (exception.retry);
7401 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7402 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7405 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7406 struct file_lock *request)
7408 int status = -ERESTARTSYS;
7409 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7411 while(!signalled()) {
7412 status = nfs4_proc_setlk(state, cmd, request);
7413 if ((status != -EAGAIN) || IS_SETLK(cmd))
7415 freezable_schedule_timeout_interruptible(timeout);
7417 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7418 status = -ERESTARTSYS;
7423 #ifdef CONFIG_NFS_V4_1
7424 struct nfs4_lock_waiter {
7425 struct inode *inode;
7426 struct nfs_lowner owner;
7427 wait_queue_entry_t wait;
7431 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7433 struct nfs4_lock_waiter *waiter =
7434 container_of(wait, struct nfs4_lock_waiter, wait);
7436 /* NULL key means to wake up everyone */
7438 struct cb_notify_lock_args *cbnl = key;
7439 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7440 *wowner = &waiter->owner;
7442 /* Only wake if the callback was for the same owner. */
7443 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7446 /* Make sure it's for the right inode */
7447 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7451 return woken_wake_function(wait, mode, flags, key);
7455 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7457 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7458 struct nfs_server *server = NFS_SERVER(state->inode);
7459 struct nfs_client *clp = server->nfs_client;
7460 wait_queue_head_t *q = &clp->cl_lock_waitq;
7461 struct nfs4_lock_waiter waiter = {
7462 .inode = state->inode,
7463 .owner = { .clientid = clp->cl_clientid,
7464 .id = lsp->ls_seqid.owner_id,
7465 .s_dev = server->s_dev },
7469 /* Don't bother with waitqueue if we don't expect a callback */
7470 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7471 return nfs4_retry_setlk_simple(state, cmd, request);
7473 init_wait(&waiter.wait);
7474 waiter.wait.func = nfs4_wake_lock_waiter;
7475 add_wait_queue(q, &waiter.wait);
7478 status = nfs4_proc_setlk(state, cmd, request);
7479 if (status != -EAGAIN || IS_SETLK(cmd))
7482 status = -ERESTARTSYS;
7483 freezer_do_not_count();
7484 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE,
7485 NFS4_LOCK_MAXTIMEOUT);
7487 } while (!signalled());
7489 remove_wait_queue(q, &waiter.wait);
7493 #else /* !CONFIG_NFS_V4_1 */
7495 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7497 return nfs4_retry_setlk_simple(state, cmd, request);
7502 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7504 struct nfs_open_context *ctx;
7505 struct nfs4_state *state;
7508 /* verify open state */
7509 ctx = nfs_file_open_context(filp);
7512 if (IS_GETLK(cmd)) {
7514 return nfs4_proc_getlk(state, F_GETLK, request);
7518 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7521 if (request->fl_type == F_UNLCK) {
7523 return nfs4_proc_unlck(state, cmd, request);
7530 if ((request->fl_flags & FL_POSIX) &&
7531 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7535 * Don't rely on the VFS having checked the file open mode,
7536 * since it won't do this for flock() locks.
7538 switch (request->fl_type) {
7540 if (!(filp->f_mode & FMODE_READ))
7544 if (!(filp->f_mode & FMODE_WRITE))
7548 status = nfs4_set_lock_state(state, request);
7552 return nfs4_retry_setlk(state, cmd, request);
7555 static int nfs4_delete_lease(struct file *file, void **priv)
7557 return generic_setlease(file, F_UNLCK, NULL, priv);
7560 static int nfs4_add_lease(struct file *file, long arg, struct file_lock **lease,
7563 struct inode *inode = file_inode(file);
7564 fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7567 /* No delegation, no lease */
7568 if (!nfs4_have_delegation(inode, type))
7570 ret = generic_setlease(file, arg, lease, priv);
7571 if (ret || nfs4_have_delegation(inode, type))
7573 /* We raced with a delegation return */
7574 nfs4_delete_lease(file, priv);
7578 int nfs4_proc_setlease(struct file *file, long arg, struct file_lock **lease,
7584 return nfs4_add_lease(file, arg, lease, priv);
7586 return nfs4_delete_lease(file, priv);
7592 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7594 struct nfs_server *server = NFS_SERVER(state->inode);
7597 err = nfs4_set_lock_state(state, fl);
7601 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7602 if (err != -NFS4ERR_DELAY)
7605 } while (err == -NFS4ERR_DELAY);
7606 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7609 struct nfs_release_lockowner_data {
7610 struct nfs4_lock_state *lsp;
7611 struct nfs_server *server;
7612 struct nfs_release_lockowner_args args;
7613 struct nfs_release_lockowner_res res;
7614 unsigned long timestamp;
7617 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7619 struct nfs_release_lockowner_data *data = calldata;
7620 struct nfs_server *server = data->server;
7621 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7622 &data->res.seq_res, task);
7623 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7624 data->timestamp = jiffies;
7627 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7629 struct nfs_release_lockowner_data *data = calldata;
7630 struct nfs_server *server = data->server;
7632 nfs40_sequence_done(task, &data->res.seq_res);
7634 switch (task->tk_status) {
7636 renew_lease(server, data->timestamp);
7638 case -NFS4ERR_STALE_CLIENTID:
7639 case -NFS4ERR_EXPIRED:
7640 nfs4_schedule_lease_recovery(server->nfs_client);
7642 case -NFS4ERR_LEASE_MOVED:
7643 case -NFS4ERR_DELAY:
7644 if (nfs4_async_handle_error(task, server,
7645 NULL, NULL) == -EAGAIN)
7646 rpc_restart_call_prepare(task);
7650 static void nfs4_release_lockowner_release(void *calldata)
7652 struct nfs_release_lockowner_data *data = calldata;
7653 nfs4_free_lock_state(data->server, data->lsp);
7657 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7658 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7659 .rpc_call_done = nfs4_release_lockowner_done,
7660 .rpc_release = nfs4_release_lockowner_release,
7664 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7666 struct nfs_release_lockowner_data *data;
7667 struct rpc_message msg = {
7668 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7671 if (server->nfs_client->cl_mvops->minor_version != 0)
7674 data = kmalloc(sizeof(*data), GFP_KERNEL);
7678 data->server = server;
7679 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7680 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7681 data->args.lock_owner.s_dev = server->s_dev;
7683 msg.rpc_argp = &data->args;
7684 msg.rpc_resp = &data->res;
7685 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7686 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7689 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7691 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7692 struct user_namespace *mnt_userns,
7693 struct dentry *unused, struct inode *inode,
7694 const char *key, const void *buf,
7695 size_t buflen, int flags)
7697 return nfs4_proc_set_acl(inode, buf, buflen);
7700 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7701 struct dentry *unused, struct inode *inode,
7702 const char *key, void *buf, size_t buflen)
7704 return nfs4_proc_get_acl(inode, buf, buflen);
7707 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7709 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7712 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7714 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7715 struct user_namespace *mnt_userns,
7716 struct dentry *unused, struct inode *inode,
7717 const char *key, const void *buf,
7718 size_t buflen, int flags)
7720 if (security_ismaclabel(key))
7721 return nfs4_set_security_label(inode, buf, buflen);
7726 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7727 struct dentry *unused, struct inode *inode,
7728 const char *key, void *buf, size_t buflen)
7730 if (security_ismaclabel(key))
7731 return nfs4_get_security_label(inode, buf, buflen);
7736 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7740 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7741 len = security_inode_listsecurity(inode, list, list_len);
7742 if (len >= 0 && list_len && len > list_len)
7748 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7749 .prefix = XATTR_SECURITY_PREFIX,
7750 .get = nfs4_xattr_get_nfs4_label,
7751 .set = nfs4_xattr_set_nfs4_label,
7757 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7764 #ifdef CONFIG_NFS_V4_2
7765 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7766 struct user_namespace *mnt_userns,
7767 struct dentry *unused, struct inode *inode,
7768 const char *key, const void *buf,
7769 size_t buflen, int flags)
7774 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7778 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7779 * flags right now. Handling of xattr operations use the normal
7780 * file read/write permissions.
7782 * Just in case the server has other ideas (which RFC 8276 allows),
7783 * do a cached access check for the XA* flags to possibly avoid
7784 * doing an RPC and getting EACCES back.
7786 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7787 if (!(mask & NFS_ACCESS_XAWRITE))
7792 ret = nfs42_proc_removexattr(inode, key);
7794 nfs4_xattr_cache_remove(inode, key);
7796 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7798 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7804 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7805 struct dentry *unused, struct inode *inode,
7806 const char *key, void *buf, size_t buflen)
7811 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7814 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7815 if (!(mask & NFS_ACCESS_XAREAD))
7819 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7823 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7824 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7827 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7833 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7842 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7845 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7846 if (!(mask & NFS_ACCESS_XALIST))
7850 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7854 ret = nfs4_xattr_cache_list(inode, list, list_len);
7855 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7860 buflen = list_len ? list_len : XATTR_LIST_MAX;
7861 buf = list_len ? list : NULL;
7865 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7878 nfs4_xattr_cache_set_list(inode, list, size);
7886 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7890 #endif /* CONFIG_NFS_V4_2 */
7893 * nfs_fhget will use either the mounted_on_fileid or the fileid
7895 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7897 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7898 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7899 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7900 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7903 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7904 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7905 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7909 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7910 const struct qstr *name,
7911 struct nfs4_fs_locations *fs_locations,
7914 struct nfs_server *server = NFS_SERVER(dir);
7916 struct nfs4_fs_locations_arg args = {
7917 .dir_fh = NFS_FH(dir),
7922 struct nfs4_fs_locations_res res = {
7923 .fs_locations = fs_locations,
7925 struct rpc_message msg = {
7926 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7932 dprintk("%s: start\n", __func__);
7934 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7935 bitmask[1] = nfs4_fattr_bitmap[1];
7937 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7938 * is not supported */
7939 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7940 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7942 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7944 nfs_fattr_init(fs_locations->fattr);
7945 fs_locations->server = server;
7946 fs_locations->nlocations = 0;
7947 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7948 dprintk("%s: returned status = %d\n", __func__, status);
7952 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7953 const struct qstr *name,
7954 struct nfs4_fs_locations *fs_locations,
7957 struct nfs4_exception exception = {
7958 .interruptible = true,
7962 err = _nfs4_proc_fs_locations(client, dir, name,
7963 fs_locations, page);
7964 trace_nfs4_get_fs_locations(dir, name, err);
7965 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7967 } while (exception.retry);
7972 * This operation also signals the server that this client is
7973 * performing migration recovery. The server can stop returning
7974 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7975 * appended to this compound to identify the client ID which is
7976 * performing recovery.
7978 static int _nfs40_proc_get_locations(struct nfs_server *server,
7979 struct nfs_fh *fhandle,
7980 struct nfs4_fs_locations *locations,
7981 struct page *page, const struct cred *cred)
7983 struct rpc_clnt *clnt = server->client;
7985 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7987 struct nfs4_fs_locations_arg args = {
7988 .clientid = server->nfs_client->cl_clientid,
7992 .migration = 1, /* skip LOOKUP */
7993 .renew = 1, /* append RENEW */
7995 struct nfs4_fs_locations_res res = {
7996 .fs_locations = locations,
8000 struct rpc_message msg = {
8001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8006 unsigned long now = jiffies;
8009 nfs_fattr_init(locations->fattr);
8010 locations->server = server;
8011 locations->nlocations = 0;
8013 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8014 status = nfs4_call_sync_sequence(clnt, server, &msg,
8015 &args.seq_args, &res.seq_res);
8019 renew_lease(server, now);
8023 #ifdef CONFIG_NFS_V4_1
8026 * This operation also signals the server that this client is
8027 * performing migration recovery. The server can stop asserting
8028 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
8029 * performing this operation is identified in the SEQUENCE
8030 * operation in this compound.
8032 * When the client supports GETATTR(fs_locations_info), it can
8033 * be plumbed in here.
8035 static int _nfs41_proc_get_locations(struct nfs_server *server,
8036 struct nfs_fh *fhandle,
8037 struct nfs4_fs_locations *locations,
8038 struct page *page, const struct cred *cred)
8040 struct rpc_clnt *clnt = server->client;
8042 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8044 struct nfs4_fs_locations_arg args = {
8048 .migration = 1, /* skip LOOKUP */
8050 struct nfs4_fs_locations_res res = {
8051 .fs_locations = locations,
8054 struct rpc_message msg = {
8055 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8062 nfs_fattr_init(locations->fattr);
8063 locations->server = server;
8064 locations->nlocations = 0;
8066 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8067 status = nfs4_call_sync_sequence(clnt, server, &msg,
8068 &args.seq_args, &res.seq_res);
8069 if (status == NFS4_OK &&
8070 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8071 status = -NFS4ERR_LEASE_MOVED;
8075 #endif /* CONFIG_NFS_V4_1 */
8078 * nfs4_proc_get_locations - discover locations for a migrated FSID
8079 * @inode: inode on FSID that is migrating
8080 * @locations: result of query
8082 * @cred: credential to use for this operation
8084 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
8085 * operation failed, or a negative errno if a local error occurred.
8087 * On success, "locations" is filled in, but if the server has
8088 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
8091 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
8092 * from this client that require migration recovery.
8094 int nfs4_proc_get_locations(struct nfs_server *server,
8095 struct nfs_fh *fhandle,
8096 struct nfs4_fs_locations *locations,
8097 struct page *page, const struct cred *cred)
8099 struct nfs_client *clp = server->nfs_client;
8100 const struct nfs4_mig_recovery_ops *ops =
8101 clp->cl_mvops->mig_recovery_ops;
8102 struct nfs4_exception exception = {
8103 .interruptible = true,
8107 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8108 (unsigned long long)server->fsid.major,
8109 (unsigned long long)server->fsid.minor,
8111 nfs_display_fhandle(fhandle, __func__);
8114 status = ops->get_locations(server, fhandle, locations, page,
8116 if (status != -NFS4ERR_DELAY)
8118 nfs4_handle_exception(server, status, &exception);
8119 } while (exception.retry);
8124 * This operation also signals the server that this client is
8125 * performing "lease moved" recovery. The server can stop
8126 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
8127 * is appended to this compound to identify the client ID which is
8128 * performing recovery.
8130 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
8132 struct nfs_server *server = NFS_SERVER(inode);
8133 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8134 struct rpc_clnt *clnt = server->client;
8135 struct nfs4_fsid_present_arg args = {
8136 .fh = NFS_FH(inode),
8137 .clientid = clp->cl_clientid,
8138 .renew = 1, /* append RENEW */
8140 struct nfs4_fsid_present_res res = {
8143 struct rpc_message msg = {
8144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8149 unsigned long now = jiffies;
8152 res.fh = nfs_alloc_fhandle();
8156 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8157 status = nfs4_call_sync_sequence(clnt, server, &msg,
8158 &args.seq_args, &res.seq_res);
8159 nfs_free_fhandle(res.fh);
8163 do_renew_lease(clp, now);
8167 #ifdef CONFIG_NFS_V4_1
8170 * This operation also signals the server that this client is
8171 * performing "lease moved" recovery. The server can stop asserting
8172 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8173 * this operation is identified in the SEQUENCE operation in this
8176 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8178 struct nfs_server *server = NFS_SERVER(inode);
8179 struct rpc_clnt *clnt = server->client;
8180 struct nfs4_fsid_present_arg args = {
8181 .fh = NFS_FH(inode),
8183 struct nfs4_fsid_present_res res = {
8185 struct rpc_message msg = {
8186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8193 res.fh = nfs_alloc_fhandle();
8197 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8198 status = nfs4_call_sync_sequence(clnt, server, &msg,
8199 &args.seq_args, &res.seq_res);
8200 nfs_free_fhandle(res.fh);
8201 if (status == NFS4_OK &&
8202 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8203 status = -NFS4ERR_LEASE_MOVED;
8207 #endif /* CONFIG_NFS_V4_1 */
8210 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8211 * @inode: inode on FSID to check
8212 * @cred: credential to use for this operation
8214 * Server indicates whether the FSID is present, moved, or not
8215 * recognized. This operation is necessary to clear a LEASE_MOVED
8216 * condition for this client ID.
8218 * Returns NFS4_OK if the FSID is present on this server,
8219 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8220 * NFS4ERR code if some error occurred on the server, or a
8221 * negative errno if a local failure occurred.
8223 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8225 struct nfs_server *server = NFS_SERVER(inode);
8226 struct nfs_client *clp = server->nfs_client;
8227 const struct nfs4_mig_recovery_ops *ops =
8228 clp->cl_mvops->mig_recovery_ops;
8229 struct nfs4_exception exception = {
8230 .interruptible = true,
8234 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8235 (unsigned long long)server->fsid.major,
8236 (unsigned long long)server->fsid.minor,
8238 nfs_display_fhandle(NFS_FH(inode), __func__);
8241 status = ops->fsid_present(inode, cred);
8242 if (status != -NFS4ERR_DELAY)
8244 nfs4_handle_exception(server, status, &exception);
8245 } while (exception.retry);
8250 * If 'use_integrity' is true and the state managment nfs_client
8251 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8252 * and the machine credential as per RFC3530bis and RFC5661 Security
8253 * Considerations sections. Otherwise, just use the user cred with the
8254 * filesystem's rpc_client.
8256 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8259 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8260 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8261 struct nfs4_secinfo_arg args = {
8262 .dir_fh = NFS_FH(dir),
8265 struct nfs4_secinfo_res res = {
8268 struct rpc_message msg = {
8269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8273 struct nfs4_call_sync_data data = {
8274 .seq_server = NFS_SERVER(dir),
8275 .seq_args = &args.seq_args,
8276 .seq_res = &res.seq_res,
8278 struct rpc_task_setup task_setup = {
8280 .rpc_message = &msg,
8281 .callback_ops = clp->cl_mvops->call_sync_ops,
8282 .callback_data = &data,
8283 .flags = RPC_TASK_NO_ROUND_ROBIN,
8285 const struct cred *cred = NULL;
8287 if (use_integrity) {
8288 clnt = clp->cl_rpcclient;
8289 task_setup.rpc_client = clnt;
8291 cred = nfs4_get_clid_cred(clp);
8292 msg.rpc_cred = cred;
8295 dprintk("NFS call secinfo %s\n", name->name);
8297 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8298 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8299 status = nfs4_call_sync_custom(&task_setup);
8301 dprintk("NFS reply secinfo: %d\n", status);
8307 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8308 struct nfs4_secinfo_flavors *flavors)
8310 struct nfs4_exception exception = {
8311 .interruptible = true,
8315 err = -NFS4ERR_WRONGSEC;
8317 /* try to use integrity protection with machine cred */
8318 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8319 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8322 * if unable to use integrity protection, or SECINFO with
8323 * integrity protection returns NFS4ERR_WRONGSEC (which is
8324 * disallowed by spec, but exists in deployed servers) use
8325 * the current filesystem's rpc_client and the user cred.
8327 if (err == -NFS4ERR_WRONGSEC)
8328 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8330 trace_nfs4_secinfo(dir, name, err);
8331 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8333 } while (exception.retry);
8337 #ifdef CONFIG_NFS_V4_1
8339 * Check the exchange flags returned by the server for invalid flags, having
8340 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8343 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8345 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8347 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8349 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8350 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8352 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8356 return -NFS4ERR_INVAL;
8360 nfs41_same_server_scope(struct nfs41_server_scope *a,
8361 struct nfs41_server_scope *b)
8363 if (a->server_scope_sz != b->server_scope_sz)
8365 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8369 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8371 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8372 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8373 struct nfs_client *clp = args->client;
8375 switch (task->tk_status) {
8376 case -NFS4ERR_BADSESSION:
8377 case -NFS4ERR_DEADSESSION:
8378 nfs4_schedule_session_recovery(clp->cl_session,
8382 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8383 res->dir != NFS4_CDFS4_BOTH) {
8384 rpc_task_close_connection(task);
8385 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8386 rpc_restart_call(task);
8390 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8391 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8395 * nfs4_proc_bind_one_conn_to_session()
8397 * The 4.1 client currently uses the same TCP connection for the
8398 * fore and backchannel.
8401 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8402 struct rpc_xprt *xprt,
8403 struct nfs_client *clp,
8404 const struct cred *cred)
8407 struct nfs41_bind_conn_to_session_args args = {
8409 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8412 struct nfs41_bind_conn_to_session_res res;
8413 struct rpc_message msg = {
8415 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8420 struct rpc_task_setup task_setup_data = {
8423 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8424 .rpc_message = &msg,
8425 .flags = RPC_TASK_TIMEOUT,
8427 struct rpc_task *task;
8429 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8430 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8431 args.dir = NFS4_CDFC4_FORE;
8433 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8434 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8435 args.dir = NFS4_CDFC4_FORE;
8437 task = rpc_run_task(&task_setup_data);
8438 if (!IS_ERR(task)) {
8439 status = task->tk_status;
8442 status = PTR_ERR(task);
8443 trace_nfs4_bind_conn_to_session(clp, status);
8445 if (memcmp(res.sessionid.data,
8446 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8447 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8450 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8451 dprintk("NFS: %s: Unexpected direction from server\n",
8455 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8456 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8465 struct rpc_bind_conn_calldata {
8466 struct nfs_client *clp;
8467 const struct cred *cred;
8471 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8472 struct rpc_xprt *xprt,
8475 struct rpc_bind_conn_calldata *p = calldata;
8477 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8480 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8482 struct rpc_bind_conn_calldata data = {
8486 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8487 nfs4_proc_bind_conn_to_session_callback, &data);
8491 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8492 * and operations we'd like to see to enable certain features in the allow map
8494 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8495 .how = SP4_MACH_CRED,
8496 .enforce.u.words = {
8497 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8498 1 << (OP_EXCHANGE_ID - 32) |
8499 1 << (OP_CREATE_SESSION - 32) |
8500 1 << (OP_DESTROY_SESSION - 32) |
8501 1 << (OP_DESTROY_CLIENTID - 32)
8504 [0] = 1 << (OP_CLOSE) |
8505 1 << (OP_OPEN_DOWNGRADE) |
8507 1 << (OP_DELEGRETURN) |
8509 [1] = 1 << (OP_SECINFO - 32) |
8510 1 << (OP_SECINFO_NO_NAME - 32) |
8511 1 << (OP_LAYOUTRETURN - 32) |
8512 1 << (OP_TEST_STATEID - 32) |
8513 1 << (OP_FREE_STATEID - 32) |
8514 1 << (OP_WRITE - 32)
8519 * Select the state protection mode for client `clp' given the server results
8520 * from exchange_id in `sp'.
8522 * Returns 0 on success, negative errno otherwise.
8524 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8525 struct nfs41_state_protection *sp)
8527 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8528 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8529 1 << (OP_EXCHANGE_ID - 32) |
8530 1 << (OP_CREATE_SESSION - 32) |
8531 1 << (OP_DESTROY_SESSION - 32) |
8532 1 << (OP_DESTROY_CLIENTID - 32)
8534 unsigned long flags = 0;
8538 if (sp->how == SP4_MACH_CRED) {
8539 /* Print state protect result */
8540 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8541 for (i = 0; i <= LAST_NFS4_OP; i++) {
8542 if (test_bit(i, sp->enforce.u.longs))
8543 dfprintk(MOUNT, " enforce op %d\n", i);
8544 if (test_bit(i, sp->allow.u.longs))
8545 dfprintk(MOUNT, " allow op %d\n", i);
8548 /* make sure nothing is on enforce list that isn't supported */
8549 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8550 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8551 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8558 * Minimal mode - state operations are allowed to use machine
8559 * credential. Note this already happens by default, so the
8560 * client doesn't have to do anything more than the negotiation.
8562 * NOTE: we don't care if EXCHANGE_ID is in the list -
8563 * we're already using the machine cred for exchange_id
8564 * and will never use a different cred.
8566 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8567 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8568 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8569 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8570 dfprintk(MOUNT, "sp4_mach_cred:\n");
8571 dfprintk(MOUNT, " minimal mode enabled\n");
8572 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8574 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8579 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8580 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8581 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8582 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8583 dfprintk(MOUNT, " cleanup mode enabled\n");
8584 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8587 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8588 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8589 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8592 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8593 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8594 dfprintk(MOUNT, " secinfo mode enabled\n");
8595 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8598 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8599 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8600 dfprintk(MOUNT, " stateid mode enabled\n");
8601 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8604 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8605 dfprintk(MOUNT, " write mode enabled\n");
8606 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8609 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8610 dfprintk(MOUNT, " commit mode enabled\n");
8611 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8615 clp->cl_sp4_flags = flags;
8619 struct nfs41_exchange_id_data {
8620 struct nfs41_exchange_id_res res;
8621 struct nfs41_exchange_id_args args;
8624 static void nfs4_exchange_id_release(void *data)
8626 struct nfs41_exchange_id_data *cdata =
8627 (struct nfs41_exchange_id_data *)data;
8629 nfs_put_client(cdata->args.client);
8630 kfree(cdata->res.impl_id);
8631 kfree(cdata->res.server_scope);
8632 kfree(cdata->res.server_owner);
8636 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8637 .rpc_release = nfs4_exchange_id_release,
8641 * _nfs4_proc_exchange_id()
8643 * Wrapper for EXCHANGE_ID operation.
8645 static struct rpc_task *
8646 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8647 u32 sp4_how, struct rpc_xprt *xprt)
8649 struct rpc_message msg = {
8650 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8653 struct rpc_task_setup task_setup_data = {
8654 .rpc_client = clp->cl_rpcclient,
8655 .callback_ops = &nfs4_exchange_id_call_ops,
8656 .rpc_message = &msg,
8657 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8659 struct nfs41_exchange_id_data *calldata;
8662 if (!refcount_inc_not_zero(&clp->cl_count))
8663 return ERR_PTR(-EIO);
8666 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8670 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8672 status = nfs4_init_uniform_client_string(clp);
8676 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8679 if (unlikely(calldata->res.server_owner == NULL))
8682 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8684 if (unlikely(calldata->res.server_scope == NULL))
8685 goto out_server_owner;
8687 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8688 if (unlikely(calldata->res.impl_id == NULL))
8689 goto out_server_scope;
8693 calldata->args.state_protect.how = SP4_NONE;
8697 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8707 task_setup_data.rpc_xprt = xprt;
8708 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8709 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8710 sizeof(calldata->args.verifier.data));
8712 calldata->args.client = clp;
8713 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8714 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8715 #ifdef CONFIG_NFS_V4_1_MIGRATION
8716 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8718 if (test_bit(NFS_CS_DS, &clp->cl_flags))
8719 calldata->args.flags |= EXCHGID4_FLAG_USE_PNFS_DS;
8720 msg.rpc_argp = &calldata->args;
8721 msg.rpc_resp = &calldata->res;
8722 task_setup_data.callback_data = calldata;
8724 return rpc_run_task(&task_setup_data);
8727 kfree(calldata->res.impl_id);
8729 kfree(calldata->res.server_scope);
8731 kfree(calldata->res.server_owner);
8735 nfs_put_client(clp);
8736 return ERR_PTR(status);
8740 * _nfs4_proc_exchange_id()
8742 * Wrapper for EXCHANGE_ID operation.
8744 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8747 struct rpc_task *task;
8748 struct nfs41_exchange_id_args *argp;
8749 struct nfs41_exchange_id_res *resp;
8750 unsigned long now = jiffies;
8753 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8755 return PTR_ERR(task);
8757 argp = task->tk_msg.rpc_argp;
8758 resp = task->tk_msg.rpc_resp;
8759 status = task->tk_status;
8763 status = nfs4_check_cl_exchange_flags(resp->flags,
8764 clp->cl_mvops->minor_version);
8768 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8772 do_renew_lease(clp, now);
8774 clp->cl_clientid = resp->clientid;
8775 clp->cl_exchange_flags = resp->flags;
8776 clp->cl_seqid = resp->seqid;
8777 /* Client ID is not confirmed */
8778 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8779 clear_bit(NFS4_SESSION_ESTABLISHED,
8780 &clp->cl_session->session_state);
8782 if (clp->cl_serverscope != NULL &&
8783 !nfs41_same_server_scope(clp->cl_serverscope,
8784 resp->server_scope)) {
8785 dprintk("%s: server_scope mismatch detected\n",
8787 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8790 swap(clp->cl_serverowner, resp->server_owner);
8791 swap(clp->cl_serverscope, resp->server_scope);
8792 swap(clp->cl_implid, resp->impl_id);
8794 /* Save the EXCHANGE_ID verifier session trunk tests */
8795 memcpy(clp->cl_confirm.data, argp->verifier.data,
8796 sizeof(clp->cl_confirm.data));
8798 trace_nfs4_exchange_id(clp, status);
8804 * nfs4_proc_exchange_id()
8806 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8808 * Since the clientid has expired, all compounds using sessions
8809 * associated with the stale clientid will be returning
8810 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8811 * be in some phase of session reset.
8813 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8815 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8817 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8820 /* try SP4_MACH_CRED if krb5i/p */
8821 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8822 authflavor == RPC_AUTH_GSS_KRB5P) {
8823 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8829 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8833 * nfs4_test_session_trunk
8835 * This is an add_xprt_test() test function called from
8836 * rpc_clnt_setup_test_and_add_xprt.
8838 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8839 * and is dereferrenced in nfs4_exchange_id_release
8841 * Upon success, add the new transport to the rpc_clnt
8843 * @clnt: struct rpc_clnt to get new transport
8844 * @xprt: the rpc_xprt to test
8845 * @data: call data for _nfs4_proc_exchange_id.
8847 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8850 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8851 struct rpc_task *task;
8856 dprintk("--> %s try %s\n", __func__,
8857 xprt->address_strings[RPC_DISPLAY_ADDR]);
8859 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8861 /* Test connection for session trunking. Async exchange_id call */
8862 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8866 status = task->tk_status;
8868 status = nfs4_detect_session_trunking(adata->clp,
8869 task->tk_msg.rpc_resp, xprt);
8872 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8876 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8878 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8879 const struct cred *cred)
8881 struct rpc_message msg = {
8882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8888 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8889 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8890 trace_nfs4_destroy_clientid(clp, status);
8892 dprintk("NFS: Got error %d from the server %s on "
8893 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8897 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8898 const struct cred *cred)
8903 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8904 ret = _nfs4_proc_destroy_clientid(clp, cred);
8906 case -NFS4ERR_DELAY:
8907 case -NFS4ERR_CLIENTID_BUSY:
8917 int nfs4_destroy_clientid(struct nfs_client *clp)
8919 const struct cred *cred;
8922 if (clp->cl_mvops->minor_version < 1)
8924 if (clp->cl_exchange_flags == 0)
8926 if (clp->cl_preserve_clid)
8928 cred = nfs4_get_clid_cred(clp);
8929 ret = nfs4_proc_destroy_clientid(clp, cred);
8933 case -NFS4ERR_STALE_CLIENTID:
8934 clp->cl_exchange_flags = 0;
8940 #endif /* CONFIG_NFS_V4_1 */
8942 struct nfs4_get_lease_time_data {
8943 struct nfs4_get_lease_time_args *args;
8944 struct nfs4_get_lease_time_res *res;
8945 struct nfs_client *clp;
8948 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8951 struct nfs4_get_lease_time_data *data =
8952 (struct nfs4_get_lease_time_data *)calldata;
8954 dprintk("--> %s\n", __func__);
8955 /* just setup sequence, do not trigger session recovery
8956 since we're invoked within one */
8957 nfs4_setup_sequence(data->clp,
8958 &data->args->la_seq_args,
8959 &data->res->lr_seq_res,
8961 dprintk("<-- %s\n", __func__);
8965 * Called from nfs4_state_manager thread for session setup, so don't recover
8966 * from sequence operation or clientid errors.
8968 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8970 struct nfs4_get_lease_time_data *data =
8971 (struct nfs4_get_lease_time_data *)calldata;
8973 dprintk("--> %s\n", __func__);
8974 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8976 switch (task->tk_status) {
8977 case -NFS4ERR_DELAY:
8978 case -NFS4ERR_GRACE:
8979 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8980 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8981 task->tk_status = 0;
8983 case -NFS4ERR_RETRY_UNCACHED_REP:
8984 rpc_restart_call_prepare(task);
8987 dprintk("<-- %s\n", __func__);
8990 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8991 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8992 .rpc_call_done = nfs4_get_lease_time_done,
8995 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8997 struct nfs4_get_lease_time_args args;
8998 struct nfs4_get_lease_time_res res = {
8999 .lr_fsinfo = fsinfo,
9001 struct nfs4_get_lease_time_data data = {
9006 struct rpc_message msg = {
9007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
9011 struct rpc_task_setup task_setup = {
9012 .rpc_client = clp->cl_rpcclient,
9013 .rpc_message = &msg,
9014 .callback_ops = &nfs4_get_lease_time_ops,
9015 .callback_data = &data,
9016 .flags = RPC_TASK_TIMEOUT,
9019 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
9020 return nfs4_call_sync_custom(&task_setup);
9023 #ifdef CONFIG_NFS_V4_1
9026 * Initialize the values to be used by the client in CREATE_SESSION
9027 * If nfs4_init_session set the fore channel request and response sizes,
9030 * Set the back channel max_resp_sz_cached to zero to force the client to
9031 * always set csa_cachethis to FALSE because the current implementation
9032 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
9034 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
9035 struct rpc_clnt *clnt)
9037 unsigned int max_rqst_sz, max_resp_sz;
9038 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
9039 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
9041 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
9042 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
9044 /* Fore channel attributes */
9045 args->fc_attrs.max_rqst_sz = max_rqst_sz;
9046 args->fc_attrs.max_resp_sz = max_resp_sz;
9047 args->fc_attrs.max_ops = NFS4_MAX_OPS;
9048 args->fc_attrs.max_reqs = max_session_slots;
9050 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
9051 "max_ops=%u max_reqs=%u\n",
9053 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
9054 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
9056 /* Back channel attributes */
9057 args->bc_attrs.max_rqst_sz = max_bc_payload;
9058 args->bc_attrs.max_resp_sz = max_bc_payload;
9059 args->bc_attrs.max_resp_sz_cached = 0;
9060 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
9061 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
9062 if (args->bc_attrs.max_reqs > max_bc_slots)
9063 args->bc_attrs.max_reqs = max_bc_slots;
9065 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
9066 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
9068 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
9069 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
9070 args->bc_attrs.max_reqs);
9073 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
9074 struct nfs41_create_session_res *res)
9076 struct nfs4_channel_attrs *sent = &args->fc_attrs;
9077 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
9079 if (rcvd->max_resp_sz > sent->max_resp_sz)
9082 * Our requested max_ops is the minimum we need; we're not
9083 * prepared to break up compounds into smaller pieces than that.
9084 * So, no point even trying to continue if the server won't
9087 if (rcvd->max_ops < sent->max_ops)
9089 if (rcvd->max_reqs == 0)
9091 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
9092 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
9096 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
9097 struct nfs41_create_session_res *res)
9099 struct nfs4_channel_attrs *sent = &args->bc_attrs;
9100 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
9102 if (!(res->flags & SESSION4_BACK_CHAN))
9104 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
9106 if (rcvd->max_resp_sz < sent->max_resp_sz)
9108 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
9110 if (rcvd->max_ops > sent->max_ops)
9112 if (rcvd->max_reqs > sent->max_reqs)
9118 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
9119 struct nfs41_create_session_res *res)
9123 ret = nfs4_verify_fore_channel_attrs(args, res);
9126 return nfs4_verify_back_channel_attrs(args, res);
9129 static void nfs4_update_session(struct nfs4_session *session,
9130 struct nfs41_create_session_res *res)
9132 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
9133 /* Mark client id and session as being confirmed */
9134 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
9135 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
9136 session->flags = res->flags;
9137 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
9138 if (res->flags & SESSION4_BACK_CHAN)
9139 memcpy(&session->bc_attrs, &res->bc_attrs,
9140 sizeof(session->bc_attrs));
9143 static int _nfs4_proc_create_session(struct nfs_client *clp,
9144 const struct cred *cred)
9146 struct nfs4_session *session = clp->cl_session;
9147 struct nfs41_create_session_args args = {
9149 .clientid = clp->cl_clientid,
9150 .seqid = clp->cl_seqid,
9151 .cb_program = NFS4_CALLBACK,
9153 struct nfs41_create_session_res res;
9155 struct rpc_message msg = {
9156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9163 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
9164 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
9166 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9167 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9168 trace_nfs4_create_session(clp, status);
9171 case -NFS4ERR_STALE_CLIENTID:
9172 case -NFS4ERR_DELAY:
9181 /* Verify the session's negotiated channel_attrs values */
9182 status = nfs4_verify_channel_attrs(&args, &res);
9183 /* Increment the clientid slot sequence id */
9186 nfs4_update_session(session, &res);
9193 * Issues a CREATE_SESSION operation to the server.
9194 * It is the responsibility of the caller to verify the session is
9195 * expired before calling this routine.
9197 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9201 struct nfs4_session *session = clp->cl_session;
9203 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9205 status = _nfs4_proc_create_session(clp, cred);
9209 /* Init or reset the session slot tables */
9210 status = nfs4_setup_session_slot_tables(session);
9211 dprintk("slot table setup returned %d\n", status);
9215 ptr = (unsigned *)&session->sess_id.data[0];
9216 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9217 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9219 dprintk("<-- %s\n", __func__);
9224 * Issue the over-the-wire RPC DESTROY_SESSION.
9225 * The caller must serialize access to this routine.
9227 int nfs4_proc_destroy_session(struct nfs4_session *session,
9228 const struct cred *cred)
9230 struct rpc_message msg = {
9231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9232 .rpc_argp = session,
9237 dprintk("--> nfs4_proc_destroy_session\n");
9239 /* session is still being setup */
9240 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9243 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9244 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9245 trace_nfs4_destroy_session(session->clp, status);
9248 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9249 "Session has been destroyed regardless...\n", status);
9251 dprintk("<-- nfs4_proc_destroy_session\n");
9256 * Renew the cl_session lease.
9258 struct nfs4_sequence_data {
9259 struct nfs_client *clp;
9260 struct nfs4_sequence_args args;
9261 struct nfs4_sequence_res res;
9264 static void nfs41_sequence_release(void *data)
9266 struct nfs4_sequence_data *calldata = data;
9267 struct nfs_client *clp = calldata->clp;
9269 if (refcount_read(&clp->cl_count) > 1)
9270 nfs4_schedule_state_renewal(clp);
9271 nfs_put_client(clp);
9275 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9277 switch(task->tk_status) {
9278 case -NFS4ERR_DELAY:
9279 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9282 nfs4_schedule_lease_recovery(clp);
9287 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9289 struct nfs4_sequence_data *calldata = data;
9290 struct nfs_client *clp = calldata->clp;
9292 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9295 trace_nfs4_sequence(clp, task->tk_status);
9296 if (task->tk_status < 0) {
9297 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9298 if (refcount_read(&clp->cl_count) == 1)
9301 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9302 rpc_restart_call_prepare(task);
9306 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9308 dprintk("<-- %s\n", __func__);
9311 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9313 struct nfs4_sequence_data *calldata = data;
9314 struct nfs_client *clp = calldata->clp;
9315 struct nfs4_sequence_args *args;
9316 struct nfs4_sequence_res *res;
9318 args = task->tk_msg.rpc_argp;
9319 res = task->tk_msg.rpc_resp;
9321 nfs4_setup_sequence(clp, args, res, task);
9324 static const struct rpc_call_ops nfs41_sequence_ops = {
9325 .rpc_call_done = nfs41_sequence_call_done,
9326 .rpc_call_prepare = nfs41_sequence_prepare,
9327 .rpc_release = nfs41_sequence_release,
9330 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9331 const struct cred *cred,
9332 struct nfs4_slot *slot,
9335 struct nfs4_sequence_data *calldata;
9336 struct rpc_message msg = {
9337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9340 struct rpc_task_setup task_setup_data = {
9341 .rpc_client = clp->cl_rpcclient,
9342 .rpc_message = &msg,
9343 .callback_ops = &nfs41_sequence_ops,
9344 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
9346 struct rpc_task *ret;
9348 ret = ERR_PTR(-EIO);
9349 if (!refcount_inc_not_zero(&clp->cl_count))
9352 ret = ERR_PTR(-ENOMEM);
9353 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
9354 if (calldata == NULL)
9356 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9357 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9358 msg.rpc_argp = &calldata->args;
9359 msg.rpc_resp = &calldata->res;
9360 calldata->clp = clp;
9361 task_setup_data.callback_data = calldata;
9363 ret = rpc_run_task(&task_setup_data);
9368 nfs_put_client(clp);
9370 nfs41_release_slot(slot);
9374 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9376 struct rpc_task *task;
9379 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9381 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9383 ret = PTR_ERR(task);
9385 rpc_put_task_async(task);
9386 dprintk("<-- %s status=%d\n", __func__, ret);
9390 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9392 struct rpc_task *task;
9395 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9397 ret = PTR_ERR(task);
9400 ret = rpc_wait_for_completion_task(task);
9402 ret = task->tk_status;
9405 dprintk("<-- %s status=%d\n", __func__, ret);
9409 struct nfs4_reclaim_complete_data {
9410 struct nfs_client *clp;
9411 struct nfs41_reclaim_complete_args arg;
9412 struct nfs41_reclaim_complete_res res;
9415 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9417 struct nfs4_reclaim_complete_data *calldata = data;
9419 nfs4_setup_sequence(calldata->clp,
9420 &calldata->arg.seq_args,
9421 &calldata->res.seq_res,
9425 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9427 switch(task->tk_status) {
9429 wake_up_all(&clp->cl_lock_waitq);
9431 case -NFS4ERR_COMPLETE_ALREADY:
9432 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9434 case -NFS4ERR_DELAY:
9435 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9437 case -NFS4ERR_RETRY_UNCACHED_REP:
9439 dprintk("%s: failed to reclaim complete error %d for server %s, retrying\n",
9440 __func__, task->tk_status, clp->cl_hostname);
9442 case -NFS4ERR_BADSESSION:
9443 case -NFS4ERR_DEADSESSION:
9444 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9447 nfs4_schedule_lease_recovery(clp);
9452 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9454 struct nfs4_reclaim_complete_data *calldata = data;
9455 struct nfs_client *clp = calldata->clp;
9456 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9458 dprintk("--> %s\n", __func__);
9459 if (!nfs41_sequence_done(task, res))
9462 trace_nfs4_reclaim_complete(clp, task->tk_status);
9463 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9464 rpc_restart_call_prepare(task);
9467 dprintk("<-- %s\n", __func__);
9470 static void nfs4_free_reclaim_complete_data(void *data)
9472 struct nfs4_reclaim_complete_data *calldata = data;
9477 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9478 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9479 .rpc_call_done = nfs4_reclaim_complete_done,
9480 .rpc_release = nfs4_free_reclaim_complete_data,
9484 * Issue a global reclaim complete.
9486 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9487 const struct cred *cred)
9489 struct nfs4_reclaim_complete_data *calldata;
9490 struct rpc_message msg = {
9491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9494 struct rpc_task_setup task_setup_data = {
9495 .rpc_client = clp->cl_rpcclient,
9496 .rpc_message = &msg,
9497 .callback_ops = &nfs4_reclaim_complete_call_ops,
9498 .flags = RPC_TASK_NO_ROUND_ROBIN,
9500 int status = -ENOMEM;
9502 dprintk("--> %s\n", __func__);
9503 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9504 if (calldata == NULL)
9506 calldata->clp = clp;
9507 calldata->arg.one_fs = 0;
9509 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9510 msg.rpc_argp = &calldata->arg;
9511 msg.rpc_resp = &calldata->res;
9512 task_setup_data.callback_data = calldata;
9513 status = nfs4_call_sync_custom(&task_setup_data);
9515 dprintk("<-- %s status=%d\n", __func__, status);
9520 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9522 struct nfs4_layoutget *lgp = calldata;
9523 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9525 dprintk("--> %s\n", __func__);
9526 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9527 &lgp->res.seq_res, task);
9528 dprintk("<-- %s\n", __func__);
9531 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9533 struct nfs4_layoutget *lgp = calldata;
9535 dprintk("--> %s\n", __func__);
9536 nfs41_sequence_process(task, &lgp->res.seq_res);
9537 dprintk("<-- %s\n", __func__);
9541 nfs4_layoutget_handle_exception(struct rpc_task *task,
9542 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9544 struct inode *inode = lgp->args.inode;
9545 struct nfs_server *server = NFS_SERVER(inode);
9546 struct pnfs_layout_hdr *lo = lgp->lo;
9547 int nfs4err = task->tk_status;
9548 int err, status = 0;
9551 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9553 nfs4_sequence_free_slot(&lgp->res.seq_res);
9560 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9561 * on the file. set tk_status to -ENODATA to tell upper layer to
9564 case -NFS4ERR_LAYOUTUNAVAILABLE:
9568 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9569 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9571 case -NFS4ERR_BADLAYOUT:
9572 status = -EOVERFLOW;
9575 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9576 * (or clients) writing to the same RAID stripe except when
9577 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9579 * Treat it like we would RECALLCONFLICT -- we retry for a little
9580 * while, and then eventually give up.
9582 case -NFS4ERR_LAYOUTTRYLATER:
9583 if (lgp->args.minlength == 0) {
9584 status = -EOVERFLOW;
9589 case -NFS4ERR_RECALLCONFLICT:
9590 status = -ERECALLCONFLICT;
9592 case -NFS4ERR_DELEG_REVOKED:
9593 case -NFS4ERR_ADMIN_REVOKED:
9594 case -NFS4ERR_EXPIRED:
9595 case -NFS4ERR_BAD_STATEID:
9596 exception->timeout = 0;
9597 spin_lock(&inode->i_lock);
9598 /* If the open stateid was bad, then recover it. */
9599 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9600 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9601 spin_unlock(&inode->i_lock);
9602 exception->state = lgp->args.ctx->state;
9603 exception->stateid = &lgp->args.stateid;
9608 * Mark the bad layout state as invalid, then retry
9610 pnfs_mark_layout_stateid_invalid(lo, &head);
9611 spin_unlock(&inode->i_lock);
9612 nfs_commit_inode(inode, 0);
9613 pnfs_free_lseg_list(&head);
9618 err = nfs4_handle_exception(server, nfs4err, exception);
9620 if (exception->retry)
9626 dprintk("<-- %s\n", __func__);
9630 size_t max_response_pages(struct nfs_server *server)
9632 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9633 return nfs_page_array_len(0, max_resp_sz);
9636 static void nfs4_layoutget_release(void *calldata)
9638 struct nfs4_layoutget *lgp = calldata;
9640 dprintk("--> %s\n", __func__);
9641 nfs4_sequence_free_slot(&lgp->res.seq_res);
9642 pnfs_layoutget_free(lgp);
9643 dprintk("<-- %s\n", __func__);
9646 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9647 .rpc_call_prepare = nfs4_layoutget_prepare,
9648 .rpc_call_done = nfs4_layoutget_done,
9649 .rpc_release = nfs4_layoutget_release,
9652 struct pnfs_layout_segment *
9653 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9655 struct inode *inode = lgp->args.inode;
9656 struct nfs_server *server = NFS_SERVER(inode);
9657 struct rpc_task *task;
9658 struct rpc_message msg = {
9659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9660 .rpc_argp = &lgp->args,
9661 .rpc_resp = &lgp->res,
9662 .rpc_cred = lgp->cred,
9664 struct rpc_task_setup task_setup_data = {
9665 .rpc_client = server->client,
9666 .rpc_message = &msg,
9667 .callback_ops = &nfs4_layoutget_call_ops,
9668 .callback_data = lgp,
9669 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
9672 struct pnfs_layout_segment *lseg = NULL;
9673 struct nfs4_exception exception = {
9675 .timeout = *timeout,
9679 dprintk("--> %s\n", __func__);
9681 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9683 task = rpc_run_task(&task_setup_data);
9685 status = rpc_wait_for_completion_task(task);
9689 if (task->tk_status < 0) {
9690 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9691 *timeout = exception.timeout;
9692 } else if (lgp->res.layoutp->len == 0) {
9694 *timeout = nfs4_update_delay(&exception.timeout);
9696 lseg = pnfs_layout_process(lgp);
9698 trace_nfs4_layoutget(lgp->args.ctx,
9705 dprintk("<-- %s status=%d\n", __func__, status);
9707 return ERR_PTR(status);
9712 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9714 struct nfs4_layoutreturn *lrp = calldata;
9716 dprintk("--> %s\n", __func__);
9717 nfs4_setup_sequence(lrp->clp,
9718 &lrp->args.seq_args,
9721 if (!pnfs_layout_is_valid(lrp->args.layout))
9725 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9727 struct nfs4_layoutreturn *lrp = calldata;
9728 struct nfs_server *server;
9730 dprintk("--> %s\n", __func__);
9732 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9736 * Was there an RPC level error? Assume the call succeeded,
9737 * and that we need to release the layout
9739 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9740 lrp->res.lrs_present = 0;
9744 server = NFS_SERVER(lrp->args.inode);
9745 switch (task->tk_status) {
9746 case -NFS4ERR_OLD_STATEID:
9747 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9753 task->tk_status = 0;
9757 case -NFS4ERR_DELAY:
9758 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9762 dprintk("<-- %s\n", __func__);
9765 task->tk_status = 0;
9766 nfs4_sequence_free_slot(&lrp->res.seq_res);
9767 rpc_restart_call_prepare(task);
9770 static void nfs4_layoutreturn_release(void *calldata)
9772 struct nfs4_layoutreturn *lrp = calldata;
9773 struct pnfs_layout_hdr *lo = lrp->args.layout;
9775 dprintk("--> %s\n", __func__);
9776 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9777 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9778 nfs4_sequence_free_slot(&lrp->res.seq_res);
9779 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9780 lrp->ld_private.ops->free(&lrp->ld_private);
9781 pnfs_put_layout_hdr(lrp->args.layout);
9782 nfs_iput_and_deactive(lrp->inode);
9783 put_cred(lrp->cred);
9785 dprintk("<-- %s\n", __func__);
9788 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9789 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9790 .rpc_call_done = nfs4_layoutreturn_done,
9791 .rpc_release = nfs4_layoutreturn_release,
9794 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9796 struct rpc_task *task;
9797 struct rpc_message msg = {
9798 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9799 .rpc_argp = &lrp->args,
9800 .rpc_resp = &lrp->res,
9801 .rpc_cred = lrp->cred,
9803 struct rpc_task_setup task_setup_data = {
9804 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9805 .rpc_message = &msg,
9806 .callback_ops = &nfs4_layoutreturn_call_ops,
9807 .callback_data = lrp,
9808 .flags = RPC_TASK_MOVEABLE,
9812 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9813 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9814 &task_setup_data.rpc_client, &msg);
9816 dprintk("--> %s\n", __func__);
9817 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9820 nfs4_layoutreturn_release(lrp);
9823 task_setup_data.flags |= RPC_TASK_ASYNC;
9826 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9829 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9831 task = rpc_run_task(&task_setup_data);
9833 return PTR_ERR(task);
9835 status = task->tk_status;
9836 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9837 dprintk("<-- %s status=%d\n", __func__, status);
9843 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9844 struct pnfs_device *pdev,
9845 const struct cred *cred)
9847 struct nfs4_getdeviceinfo_args args = {
9849 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9850 NOTIFY_DEVICEID4_DELETE,
9852 struct nfs4_getdeviceinfo_res res = {
9855 struct rpc_message msg = {
9856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9863 dprintk("--> %s\n", __func__);
9864 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9865 if (res.notification & ~args.notify_types)
9866 dprintk("%s: unsupported notification\n", __func__);
9867 if (res.notification != args.notify_types)
9870 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9872 dprintk("<-- %s status=%d\n", __func__, status);
9877 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9878 struct pnfs_device *pdev,
9879 const struct cred *cred)
9881 struct nfs4_exception exception = { };
9885 err = nfs4_handle_exception(server,
9886 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9888 } while (exception.retry);
9891 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9893 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9895 struct nfs4_layoutcommit_data *data = calldata;
9896 struct nfs_server *server = NFS_SERVER(data->args.inode);
9898 nfs4_setup_sequence(server->nfs_client,
9899 &data->args.seq_args,
9905 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9907 struct nfs4_layoutcommit_data *data = calldata;
9908 struct nfs_server *server = NFS_SERVER(data->args.inode);
9910 if (!nfs41_sequence_done(task, &data->res.seq_res))
9913 switch (task->tk_status) { /* Just ignore these failures */
9914 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9915 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9916 case -NFS4ERR_BADLAYOUT: /* no layout */
9917 case -NFS4ERR_GRACE: /* loca_recalim always false */
9918 task->tk_status = 0;
9923 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9924 rpc_restart_call_prepare(task);
9930 static void nfs4_layoutcommit_release(void *calldata)
9932 struct nfs4_layoutcommit_data *data = calldata;
9934 pnfs_cleanup_layoutcommit(data);
9935 nfs_post_op_update_inode_force_wcc(data->args.inode,
9937 put_cred(data->cred);
9938 nfs_iput_and_deactive(data->inode);
9942 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9943 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9944 .rpc_call_done = nfs4_layoutcommit_done,
9945 .rpc_release = nfs4_layoutcommit_release,
9949 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9951 struct rpc_message msg = {
9952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9953 .rpc_argp = &data->args,
9954 .rpc_resp = &data->res,
9955 .rpc_cred = data->cred,
9957 struct rpc_task_setup task_setup_data = {
9958 .task = &data->task,
9959 .rpc_client = NFS_CLIENT(data->args.inode),
9960 .rpc_message = &msg,
9961 .callback_ops = &nfs4_layoutcommit_ops,
9962 .callback_data = data,
9963 .flags = RPC_TASK_MOVEABLE,
9965 struct rpc_task *task;
9968 dprintk("NFS: initiating layoutcommit call. sync %d "
9969 "lbw: %llu inode %lu\n", sync,
9970 data->args.lastbytewritten,
9971 data->args.inode->i_ino);
9974 data->inode = nfs_igrab_and_active(data->args.inode);
9975 if (data->inode == NULL) {
9976 nfs4_layoutcommit_release(data);
9979 task_setup_data.flags = RPC_TASK_ASYNC;
9981 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9982 task = rpc_run_task(&task_setup_data);
9984 return PTR_ERR(task);
9986 status = task->tk_status;
9987 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9988 dprintk("%s: status %d\n", __func__, status);
9994 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9995 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9998 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9999 struct nfs_fsinfo *info,
10000 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
10002 struct nfs41_secinfo_no_name_args args = {
10003 .style = SECINFO_STYLE_CURRENT_FH,
10005 struct nfs4_secinfo_res res = {
10006 .flavors = flavors,
10008 struct rpc_message msg = {
10009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
10013 struct nfs4_call_sync_data data = {
10014 .seq_server = server,
10015 .seq_args = &args.seq_args,
10016 .seq_res = &res.seq_res,
10018 struct rpc_task_setup task_setup = {
10019 .rpc_client = server->client,
10020 .rpc_message = &msg,
10021 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
10022 .callback_data = &data,
10023 .flags = RPC_TASK_NO_ROUND_ROBIN,
10025 const struct cred *cred = NULL;
10028 if (use_integrity) {
10029 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
10031 cred = nfs4_get_clid_cred(server->nfs_client);
10032 msg.rpc_cred = cred;
10035 dprintk("--> %s\n", __func__);
10036 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
10037 status = nfs4_call_sync_custom(&task_setup);
10038 dprintk("<-- %s status=%d\n", __func__, status);
10046 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10047 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
10049 struct nfs4_exception exception = {
10050 .interruptible = true,
10054 /* first try using integrity protection */
10055 err = -NFS4ERR_WRONGSEC;
10057 /* try to use integrity protection with machine cred */
10058 if (_nfs4_is_integrity_protected(server->nfs_client))
10059 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10063 * if unable to use integrity protection, or SECINFO with
10064 * integrity protection returns NFS4ERR_WRONGSEC (which is
10065 * disallowed by spec, but exists in deployed servers) use
10066 * the current filesystem's rpc_client and the user cred.
10068 if (err == -NFS4ERR_WRONGSEC)
10069 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10074 case -NFS4ERR_WRONGSEC:
10078 err = nfs4_handle_exception(server, err, &exception);
10080 } while (exception.retry);
10086 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
10087 struct nfs_fsinfo *info)
10091 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
10092 struct nfs4_secinfo_flavors *flavors;
10093 struct nfs4_secinfo4 *secinfo;
10096 page = alloc_page(GFP_KERNEL);
10102 flavors = page_address(page);
10103 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
10106 * Fall back on "guess and check" method if
10107 * the server doesn't support SECINFO_NO_NAME
10109 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
10110 err = nfs4_find_root_sec(server, fhandle, info);
10116 for (i = 0; i < flavors->num_flavors; i++) {
10117 secinfo = &flavors->flavors[i];
10119 switch (secinfo->flavor) {
10120 case RPC_AUTH_NULL:
10121 case RPC_AUTH_UNIX:
10123 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
10124 &secinfo->flavor_info);
10127 flavor = RPC_AUTH_MAXFLAVOR;
10131 if (!nfs_auth_info_match(&server->auth_info, flavor))
10132 flavor = RPC_AUTH_MAXFLAVOR;
10134 if (flavor != RPC_AUTH_MAXFLAVOR) {
10135 err = nfs4_lookup_root_sec(server, fhandle,
10142 if (flavor == RPC_AUTH_MAXFLAVOR)
10147 if (err == -EACCES)
10153 static int _nfs41_test_stateid(struct nfs_server *server,
10154 nfs4_stateid *stateid,
10155 const struct cred *cred)
10158 struct nfs41_test_stateid_args args = {
10159 .stateid = stateid,
10161 struct nfs41_test_stateid_res res;
10162 struct rpc_message msg = {
10163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10168 struct rpc_clnt *rpc_client = server->client;
10170 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10171 &rpc_client, &msg);
10173 dprintk("NFS call test_stateid %p\n", stateid);
10174 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
10175 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
10176 &args.seq_args, &res.seq_res);
10177 if (status != NFS_OK) {
10178 dprintk("NFS reply test_stateid: failed, %d\n", status);
10181 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10182 return -res.status;
10185 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10186 int err, struct nfs4_exception *exception)
10188 exception->retry = 0;
10190 case -NFS4ERR_DELAY:
10191 case -NFS4ERR_RETRY_UNCACHED_REP:
10192 nfs4_handle_exception(server, err, exception);
10194 case -NFS4ERR_BADSESSION:
10195 case -NFS4ERR_BADSLOT:
10196 case -NFS4ERR_BAD_HIGH_SLOT:
10197 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10198 case -NFS4ERR_DEADSESSION:
10199 nfs4_do_handle_exception(server, err, exception);
10204 * nfs41_test_stateid - perform a TEST_STATEID operation
10206 * @server: server / transport on which to perform the operation
10207 * @stateid: state ID to test
10208 * @cred: credential
10210 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10211 * Otherwise a negative NFS4ERR value is returned if the operation
10212 * failed or the state ID is not currently valid.
10214 static int nfs41_test_stateid(struct nfs_server *server,
10215 nfs4_stateid *stateid,
10216 const struct cred *cred)
10218 struct nfs4_exception exception = {
10219 .interruptible = true,
10223 err = _nfs41_test_stateid(server, stateid, cred);
10224 nfs4_handle_delay_or_session_error(server, err, &exception);
10225 } while (exception.retry);
10229 struct nfs_free_stateid_data {
10230 struct nfs_server *server;
10231 struct nfs41_free_stateid_args args;
10232 struct nfs41_free_stateid_res res;
10235 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10237 struct nfs_free_stateid_data *data = calldata;
10238 nfs4_setup_sequence(data->server->nfs_client,
10239 &data->args.seq_args,
10240 &data->res.seq_res,
10244 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10246 struct nfs_free_stateid_data *data = calldata;
10248 nfs41_sequence_done(task, &data->res.seq_res);
10250 switch (task->tk_status) {
10251 case -NFS4ERR_DELAY:
10252 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10253 rpc_restart_call_prepare(task);
10257 static void nfs41_free_stateid_release(void *calldata)
10262 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10263 .rpc_call_prepare = nfs41_free_stateid_prepare,
10264 .rpc_call_done = nfs41_free_stateid_done,
10265 .rpc_release = nfs41_free_stateid_release,
10269 * nfs41_free_stateid - perform a FREE_STATEID operation
10271 * @server: server / transport on which to perform the operation
10272 * @stateid: state ID to release
10273 * @cred: credential
10274 * @privileged: set to true if this call needs to be privileged
10276 * Note: this function is always asynchronous.
10278 static int nfs41_free_stateid(struct nfs_server *server,
10279 const nfs4_stateid *stateid,
10280 const struct cred *cred,
10283 struct rpc_message msg = {
10284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10287 struct rpc_task_setup task_setup = {
10288 .rpc_client = server->client,
10289 .rpc_message = &msg,
10290 .callback_ops = &nfs41_free_stateid_ops,
10291 .flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
10293 struct nfs_free_stateid_data *data;
10294 struct rpc_task *task;
10296 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10297 &task_setup.rpc_client, &msg);
10299 dprintk("NFS call free_stateid %p\n", stateid);
10300 data = kmalloc(sizeof(*data), GFP_KERNEL);
10303 data->server = server;
10304 nfs4_stateid_copy(&data->args.stateid, stateid);
10306 task_setup.callback_data = data;
10308 msg.rpc_argp = &data->args;
10309 msg.rpc_resp = &data->res;
10310 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10311 task = rpc_run_task(&task_setup);
10313 return PTR_ERR(task);
10314 rpc_put_task(task);
10319 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10321 const struct cred *cred = lsp->ls_state->owner->so_cred;
10323 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10324 nfs4_free_lock_state(server, lsp);
10327 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10328 const nfs4_stateid *s2)
10330 if (s1->type != s2->type)
10333 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10336 if (s1->seqid == s2->seqid)
10339 return s1->seqid == 0 || s2->seqid == 0;
10342 #endif /* CONFIG_NFS_V4_1 */
10344 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10345 const nfs4_stateid *s2)
10347 return nfs4_stateid_match(s1, s2);
10351 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10352 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10353 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10354 .recover_open = nfs4_open_reclaim,
10355 .recover_lock = nfs4_lock_reclaim,
10356 .establish_clid = nfs4_init_clientid,
10357 .detect_trunking = nfs40_discover_server_trunking,
10360 #if defined(CONFIG_NFS_V4_1)
10361 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10362 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10363 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10364 .recover_open = nfs4_open_reclaim,
10365 .recover_lock = nfs4_lock_reclaim,
10366 .establish_clid = nfs41_init_clientid,
10367 .reclaim_complete = nfs41_proc_reclaim_complete,
10368 .detect_trunking = nfs41_discover_server_trunking,
10370 #endif /* CONFIG_NFS_V4_1 */
10372 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10373 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10374 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10375 .recover_open = nfs40_open_expired,
10376 .recover_lock = nfs4_lock_expired,
10377 .establish_clid = nfs4_init_clientid,
10380 #if defined(CONFIG_NFS_V4_1)
10381 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10382 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10383 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10384 .recover_open = nfs41_open_expired,
10385 .recover_lock = nfs41_lock_expired,
10386 .establish_clid = nfs41_init_clientid,
10388 #endif /* CONFIG_NFS_V4_1 */
10390 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10391 .sched_state_renewal = nfs4_proc_async_renew,
10392 .get_state_renewal_cred = nfs4_get_renew_cred,
10393 .renew_lease = nfs4_proc_renew,
10396 #if defined(CONFIG_NFS_V4_1)
10397 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10398 .sched_state_renewal = nfs41_proc_async_sequence,
10399 .get_state_renewal_cred = nfs4_get_machine_cred,
10400 .renew_lease = nfs4_proc_sequence,
10404 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10405 .get_locations = _nfs40_proc_get_locations,
10406 .fsid_present = _nfs40_proc_fsid_present,
10409 #if defined(CONFIG_NFS_V4_1)
10410 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10411 .get_locations = _nfs41_proc_get_locations,
10412 .fsid_present = _nfs41_proc_fsid_present,
10414 #endif /* CONFIG_NFS_V4_1 */
10416 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10417 .minor_version = 0,
10418 .init_caps = NFS_CAP_READDIRPLUS
10419 | NFS_CAP_ATOMIC_OPEN
10420 | NFS_CAP_POSIX_LOCK,
10421 .init_client = nfs40_init_client,
10422 .shutdown_client = nfs40_shutdown_client,
10423 .match_stateid = nfs4_match_stateid,
10424 .find_root_sec = nfs4_find_root_sec,
10425 .free_lock_state = nfs4_release_lockowner,
10426 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10427 .alloc_seqid = nfs_alloc_seqid,
10428 .call_sync_ops = &nfs40_call_sync_ops,
10429 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10430 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10431 .state_renewal_ops = &nfs40_state_renewal_ops,
10432 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10435 #if defined(CONFIG_NFS_V4_1)
10436 static struct nfs_seqid *
10437 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10442 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10443 .minor_version = 1,
10444 .init_caps = NFS_CAP_READDIRPLUS
10445 | NFS_CAP_ATOMIC_OPEN
10446 | NFS_CAP_POSIX_LOCK
10447 | NFS_CAP_STATEID_NFSV41
10448 | NFS_CAP_ATOMIC_OPEN_V1
10450 | NFS_CAP_MOVEABLE,
10451 .init_client = nfs41_init_client,
10452 .shutdown_client = nfs41_shutdown_client,
10453 .match_stateid = nfs41_match_stateid,
10454 .find_root_sec = nfs41_find_root_sec,
10455 .free_lock_state = nfs41_free_lock_state,
10456 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10457 .alloc_seqid = nfs_alloc_no_seqid,
10458 .session_trunk = nfs4_test_session_trunk,
10459 .call_sync_ops = &nfs41_call_sync_ops,
10460 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10461 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10462 .state_renewal_ops = &nfs41_state_renewal_ops,
10463 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10467 #if defined(CONFIG_NFS_V4_2)
10468 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10469 .minor_version = 2,
10470 .init_caps = NFS_CAP_READDIRPLUS
10471 | NFS_CAP_ATOMIC_OPEN
10472 | NFS_CAP_POSIX_LOCK
10473 | NFS_CAP_STATEID_NFSV41
10474 | NFS_CAP_ATOMIC_OPEN_V1
10478 | NFS_CAP_OFFLOAD_CANCEL
10479 | NFS_CAP_COPY_NOTIFY
10480 | NFS_CAP_DEALLOCATE
10482 | NFS_CAP_LAYOUTSTATS
10484 | NFS_CAP_LAYOUTERROR
10485 | NFS_CAP_READ_PLUS
10486 | NFS_CAP_MOVEABLE,
10487 .init_client = nfs41_init_client,
10488 .shutdown_client = nfs41_shutdown_client,
10489 .match_stateid = nfs41_match_stateid,
10490 .find_root_sec = nfs41_find_root_sec,
10491 .free_lock_state = nfs41_free_lock_state,
10492 .call_sync_ops = &nfs41_call_sync_ops,
10493 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10494 .alloc_seqid = nfs_alloc_no_seqid,
10495 .session_trunk = nfs4_test_session_trunk,
10496 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10497 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10498 .state_renewal_ops = &nfs41_state_renewal_ops,
10499 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10503 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10504 [0] = &nfs_v4_0_minor_ops,
10505 #if defined(CONFIG_NFS_V4_1)
10506 [1] = &nfs_v4_1_minor_ops,
10508 #if defined(CONFIG_NFS_V4_2)
10509 [2] = &nfs_v4_2_minor_ops,
10513 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10515 ssize_t error, error2, error3;
10517 error = generic_listxattr(dentry, list, size);
10525 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10534 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10538 return error + error2 + error3;
10541 static void nfs4_enable_swap(struct inode *inode)
10543 /* The state manager thread must always be running.
10544 * It will notice the client is a swapper, and stay put.
10546 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10548 nfs4_schedule_state_manager(clp);
10551 static void nfs4_disable_swap(struct inode *inode)
10553 /* The state manager thread will now exit once it is
10556 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10558 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
10559 clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
10560 wake_up_var(&clp->cl_state);
10563 static const struct inode_operations nfs4_dir_inode_operations = {
10564 .create = nfs_create,
10565 .lookup = nfs_lookup,
10566 .atomic_open = nfs_atomic_open,
10568 .unlink = nfs_unlink,
10569 .symlink = nfs_symlink,
10570 .mkdir = nfs_mkdir,
10571 .rmdir = nfs_rmdir,
10572 .mknod = nfs_mknod,
10573 .rename = nfs_rename,
10574 .permission = nfs_permission,
10575 .getattr = nfs_getattr,
10576 .setattr = nfs_setattr,
10577 .listxattr = nfs4_listxattr,
10580 static const struct inode_operations nfs4_file_inode_operations = {
10581 .permission = nfs_permission,
10582 .getattr = nfs_getattr,
10583 .setattr = nfs_setattr,
10584 .listxattr = nfs4_listxattr,
10587 const struct nfs_rpc_ops nfs_v4_clientops = {
10588 .version = 4, /* protocol version */
10589 .dentry_ops = &nfs4_dentry_operations,
10590 .dir_inode_ops = &nfs4_dir_inode_operations,
10591 .file_inode_ops = &nfs4_file_inode_operations,
10592 .file_ops = &nfs4_file_operations,
10593 .getroot = nfs4_proc_get_root,
10594 .submount = nfs4_submount,
10595 .try_get_tree = nfs4_try_get_tree,
10596 .getattr = nfs4_proc_getattr,
10597 .setattr = nfs4_proc_setattr,
10598 .lookup = nfs4_proc_lookup,
10599 .lookupp = nfs4_proc_lookupp,
10600 .access = nfs4_proc_access,
10601 .readlink = nfs4_proc_readlink,
10602 .create = nfs4_proc_create,
10603 .remove = nfs4_proc_remove,
10604 .unlink_setup = nfs4_proc_unlink_setup,
10605 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10606 .unlink_done = nfs4_proc_unlink_done,
10607 .rename_setup = nfs4_proc_rename_setup,
10608 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10609 .rename_done = nfs4_proc_rename_done,
10610 .link = nfs4_proc_link,
10611 .symlink = nfs4_proc_symlink,
10612 .mkdir = nfs4_proc_mkdir,
10613 .rmdir = nfs4_proc_rmdir,
10614 .readdir = nfs4_proc_readdir,
10615 .mknod = nfs4_proc_mknod,
10616 .statfs = nfs4_proc_statfs,
10617 .fsinfo = nfs4_proc_fsinfo,
10618 .pathconf = nfs4_proc_pathconf,
10619 .set_capabilities = nfs4_server_capabilities,
10620 .decode_dirent = nfs4_decode_dirent,
10621 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10622 .read_setup = nfs4_proc_read_setup,
10623 .read_done = nfs4_read_done,
10624 .write_setup = nfs4_proc_write_setup,
10625 .write_done = nfs4_write_done,
10626 .commit_setup = nfs4_proc_commit_setup,
10627 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10628 .commit_done = nfs4_commit_done,
10629 .lock = nfs4_proc_lock,
10630 .clear_acl_cache = nfs4_zap_acl_attr,
10631 .close_context = nfs4_close_context,
10632 .open_context = nfs4_atomic_open,
10633 .have_delegation = nfs4_have_delegation,
10634 .alloc_client = nfs4_alloc_client,
10635 .init_client = nfs4_init_client,
10636 .free_client = nfs4_free_client,
10637 .create_server = nfs4_create_server,
10638 .clone_server = nfs_clone_server,
10639 .discover_trunking = nfs4_discover_trunking,
10640 .enable_swap = nfs4_enable_swap,
10641 .disable_swap = nfs4_disable_swap,
10644 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10645 .name = XATTR_NAME_NFSV4_ACL,
10646 .list = nfs4_xattr_list_nfs4_acl,
10647 .get = nfs4_xattr_get_nfs4_acl,
10648 .set = nfs4_xattr_set_nfs4_acl,
10651 #ifdef CONFIG_NFS_V4_2
10652 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10653 .prefix = XATTR_USER_PREFIX,
10654 .get = nfs4_xattr_get_nfs4_user,
10655 .set = nfs4_xattr_set_nfs4_user,
10659 const struct xattr_handler *nfs4_xattr_handlers[] = {
10660 &nfs4_xattr_nfs4_acl_handler,
10661 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10662 &nfs4_xattr_nfs4_label_handler,
10664 #ifdef CONFIG_NFS_V4_2
10665 &nfs4_xattr_nfs4_user_handler,