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,
97 struct nfs_fattr *fattr, struct inode *inode);
98 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
99 struct nfs_fattr *fattr, struct iattr *sattr,
100 struct nfs_open_context *ctx, struct nfs4_label *ilabel);
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);
112 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
113 static inline struct nfs4_label *
114 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
115 struct iattr *sattr, struct nfs4_label *label)
122 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
130 err = security_dentry_init_security(dentry, sattr->ia_mode,
131 &dentry->d_name, NULL,
132 (void **)&label->label, &label->len);
139 nfs4_label_release_security(struct nfs4_label *label)
142 security_release_secctx(label->label, label->len);
144 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
147 return server->attr_bitmask;
149 return server->attr_bitmask_nl;
152 static inline struct nfs4_label *
153 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
154 struct iattr *sattr, struct nfs4_label *l)
157 nfs4_label_release_security(struct nfs4_label *label)
160 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
161 { return server->attr_bitmask; }
164 /* Prevent leaks of NFSv4 errors into userland */
165 static int nfs4_map_errors(int err)
170 case -NFS4ERR_RESOURCE:
171 case -NFS4ERR_LAYOUTTRYLATER:
172 case -NFS4ERR_RECALLCONFLICT:
173 case -NFS4ERR_RETURNCONFLICT:
175 case -NFS4ERR_WRONGSEC:
176 case -NFS4ERR_WRONG_CRED:
178 case -NFS4ERR_BADOWNER:
179 case -NFS4ERR_BADNAME:
181 case -NFS4ERR_SHARE_DENIED:
183 case -NFS4ERR_MINOR_VERS_MISMATCH:
184 return -EPROTONOSUPPORT;
185 case -NFS4ERR_FILE_OPEN:
187 case -NFS4ERR_NOT_SAME:
190 dprintk("%s could not handle NFSv4 error %d\n",
198 * This is our standard bitmap for GETATTR requests.
200 const u32 nfs4_fattr_bitmap[3] = {
202 | FATTR4_WORD0_CHANGE
205 | FATTR4_WORD0_FILEID,
207 | FATTR4_WORD1_NUMLINKS
209 | FATTR4_WORD1_OWNER_GROUP
210 | FATTR4_WORD1_RAWDEV
211 | FATTR4_WORD1_SPACE_USED
212 | FATTR4_WORD1_TIME_ACCESS
213 | FATTR4_WORD1_TIME_METADATA
214 | FATTR4_WORD1_TIME_MODIFY
215 | FATTR4_WORD1_MOUNTED_ON_FILEID,
216 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
217 FATTR4_WORD2_SECURITY_LABEL
221 static const u32 nfs4_pnfs_open_bitmap[3] = {
223 | FATTR4_WORD0_CHANGE
226 | FATTR4_WORD0_FILEID,
228 | FATTR4_WORD1_NUMLINKS
230 | FATTR4_WORD1_OWNER_GROUP
231 | FATTR4_WORD1_RAWDEV
232 | FATTR4_WORD1_SPACE_USED
233 | FATTR4_WORD1_TIME_ACCESS
234 | FATTR4_WORD1_TIME_METADATA
235 | FATTR4_WORD1_TIME_MODIFY,
236 FATTR4_WORD2_MDSTHRESHOLD
237 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
238 | FATTR4_WORD2_SECURITY_LABEL
242 static const u32 nfs4_open_noattr_bitmap[3] = {
244 | FATTR4_WORD0_FILEID,
247 const u32 nfs4_statfs_bitmap[3] = {
248 FATTR4_WORD0_FILES_AVAIL
249 | FATTR4_WORD0_FILES_FREE
250 | FATTR4_WORD0_FILES_TOTAL,
251 FATTR4_WORD1_SPACE_AVAIL
252 | FATTR4_WORD1_SPACE_FREE
253 | FATTR4_WORD1_SPACE_TOTAL
256 const u32 nfs4_pathconf_bitmap[3] = {
258 | FATTR4_WORD0_MAXNAME,
262 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
263 | FATTR4_WORD0_MAXREAD
264 | FATTR4_WORD0_MAXWRITE
265 | FATTR4_WORD0_LEASE_TIME,
266 FATTR4_WORD1_TIME_DELTA
267 | FATTR4_WORD1_FS_LAYOUT_TYPES,
268 FATTR4_WORD2_LAYOUT_BLKSIZE
269 | FATTR4_WORD2_CLONE_BLKSIZE
270 | FATTR4_WORD2_CHANGE_ATTR_TYPE
271 | FATTR4_WORD2_XATTR_SUPPORT
274 const u32 nfs4_fs_locations_bitmap[3] = {
278 | FATTR4_WORD0_FILEID
279 | FATTR4_WORD0_FS_LOCATIONS,
281 | FATTR4_WORD1_OWNER_GROUP
282 | FATTR4_WORD1_RAWDEV
283 | FATTR4_WORD1_SPACE_USED
284 | FATTR4_WORD1_TIME_ACCESS
285 | FATTR4_WORD1_TIME_METADATA
286 | FATTR4_WORD1_TIME_MODIFY
287 | FATTR4_WORD1_MOUNTED_ON_FILEID,
290 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
291 struct inode *inode, unsigned long flags)
293 unsigned long cache_validity;
295 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
296 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
299 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags;
301 /* Remove the attributes over which we have full control */
302 dst[1] &= ~FATTR4_WORD1_RAWDEV;
303 if (!(cache_validity & NFS_INO_INVALID_SIZE))
304 dst[0] &= ~FATTR4_WORD0_SIZE;
306 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
307 dst[0] &= ~FATTR4_WORD0_CHANGE;
309 if (!(cache_validity & NFS_INO_INVALID_MODE))
310 dst[1] &= ~FATTR4_WORD1_MODE;
311 if (!(cache_validity & NFS_INO_INVALID_OTHER))
312 dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP);
315 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
316 struct nfs4_readdir_arg *readdir)
318 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
322 readdir->cookie = cookie;
323 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
328 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
333 * NFSv4 servers do not return entries for '.' and '..'
334 * Therefore, we fake these entries here. We let '.'
335 * have cookie 0 and '..' have cookie 1. Note that
336 * when talking to the server, we always send cookie 0
339 start = p = kmap_atomic(*readdir->pages);
342 *p++ = xdr_one; /* next */
343 *p++ = xdr_zero; /* cookie, first word */
344 *p++ = xdr_one; /* cookie, second word */
345 *p++ = xdr_one; /* entry len */
346 memcpy(p, ".\0\0\0", 4); /* entry */
348 *p++ = xdr_one; /* bitmap length */
349 *p++ = htonl(attrs); /* bitmap */
350 *p++ = htonl(12); /* attribute buffer length */
351 *p++ = htonl(NF4DIR);
352 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
355 *p++ = xdr_one; /* next */
356 *p++ = xdr_zero; /* cookie, first word */
357 *p++ = xdr_two; /* cookie, second word */
358 *p++ = xdr_two; /* entry len */
359 memcpy(p, "..\0\0", 4); /* entry */
361 *p++ = xdr_one; /* bitmap length */
362 *p++ = htonl(attrs); /* bitmap */
363 *p++ = htonl(12); /* attribute buffer length */
364 *p++ = htonl(NF4DIR);
365 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
367 readdir->pgbase = (char *)p - (char *)start;
368 readdir->count -= readdir->pgbase;
369 kunmap_atomic(start);
372 static void nfs4_fattr_set_prechange(struct nfs_fattr *fattr, u64 version)
374 if (!(fattr->valid & NFS_ATTR_FATTR_PRECHANGE)) {
375 fattr->pre_change_attr = version;
376 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
380 static void nfs4_test_and_free_stateid(struct nfs_server *server,
381 nfs4_stateid *stateid,
382 const struct cred *cred)
384 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
386 ops->test_and_free_expired(server, stateid, cred);
389 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
390 nfs4_stateid *stateid,
391 const struct cred *cred)
393 stateid->type = NFS4_REVOKED_STATEID_TYPE;
394 nfs4_test_and_free_stateid(server, stateid, cred);
397 static void nfs4_free_revoked_stateid(struct nfs_server *server,
398 const nfs4_stateid *stateid,
399 const struct cred *cred)
403 nfs4_stateid_copy(&tmp, stateid);
404 __nfs4_free_revoked_stateid(server, &tmp, cred);
407 static long nfs4_update_delay(long *timeout)
411 return NFS4_POLL_RETRY_MAX;
413 *timeout = NFS4_POLL_RETRY_MIN;
414 if (*timeout > NFS4_POLL_RETRY_MAX)
415 *timeout = NFS4_POLL_RETRY_MAX;
421 static int nfs4_delay_killable(long *timeout)
425 __set_current_state(TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
426 schedule_timeout(nfs4_update_delay(timeout));
427 if (!__fatal_signal_pending(current))
432 static int nfs4_delay_interruptible(long *timeout)
436 __set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE_UNSAFE);
437 schedule_timeout(nfs4_update_delay(timeout));
438 if (!signal_pending(current))
440 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
443 static int nfs4_delay(long *timeout, bool interruptible)
446 return nfs4_delay_interruptible(timeout);
447 return nfs4_delay_killable(timeout);
450 static const nfs4_stateid *
451 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
455 switch (stateid->type) {
456 case NFS4_OPEN_STATEID_TYPE:
457 case NFS4_LOCK_STATEID_TYPE:
458 case NFS4_DELEGATION_STATEID_TYPE:
466 /* This is the error handling routine for processes that are allowed
469 static int nfs4_do_handle_exception(struct nfs_server *server,
470 int errorcode, struct nfs4_exception *exception)
472 struct nfs_client *clp = server->nfs_client;
473 struct nfs4_state *state = exception->state;
474 const nfs4_stateid *stateid;
475 struct inode *inode = exception->inode;
478 exception->delay = 0;
479 exception->recovering = 0;
480 exception->retry = 0;
482 stateid = nfs4_recoverable_stateid(exception->stateid);
483 if (stateid == NULL && state != NULL)
484 stateid = nfs4_recoverable_stateid(&state->stateid);
489 case -NFS4ERR_BADHANDLE:
491 if (inode != NULL && S_ISREG(inode->i_mode))
492 pnfs_destroy_layout(NFS_I(inode));
494 case -NFS4ERR_DELEG_REVOKED:
495 case -NFS4ERR_ADMIN_REVOKED:
496 case -NFS4ERR_EXPIRED:
497 case -NFS4ERR_BAD_STATEID:
498 case -NFS4ERR_PARTNER_NO_AUTH:
499 if (inode != NULL && stateid != NULL) {
500 nfs_inode_find_state_and_recover(inode,
502 goto wait_on_recovery;
505 case -NFS4ERR_OPENMODE:
509 err = nfs_async_inode_return_delegation(inode,
512 goto wait_on_recovery;
513 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
514 exception->retry = 1;
520 ret = nfs4_schedule_stateid_recovery(server, state);
523 goto wait_on_recovery;
524 case -NFS4ERR_STALE_STATEID:
525 case -NFS4ERR_STALE_CLIENTID:
526 nfs4_schedule_lease_recovery(clp);
527 goto wait_on_recovery;
529 ret = nfs4_schedule_migration_recovery(server);
532 goto wait_on_recovery;
533 case -NFS4ERR_LEASE_MOVED:
534 nfs4_schedule_lease_moved_recovery(clp);
535 goto wait_on_recovery;
536 #if defined(CONFIG_NFS_V4_1)
537 case -NFS4ERR_BADSESSION:
538 case -NFS4ERR_BADSLOT:
539 case -NFS4ERR_BAD_HIGH_SLOT:
540 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
541 case -NFS4ERR_DEADSESSION:
542 case -NFS4ERR_SEQ_FALSE_RETRY:
543 case -NFS4ERR_SEQ_MISORDERED:
544 /* Handled in nfs41_sequence_process() */
545 goto wait_on_recovery;
546 #endif /* defined(CONFIG_NFS_V4_1) */
547 case -NFS4ERR_FILE_OPEN:
548 if (exception->timeout > HZ) {
549 /* We have retried a decent amount, time to
557 nfs_inc_server_stats(server, NFSIOS_DELAY);
560 case -NFS4ERR_LAYOUTTRYLATER:
561 case -NFS4ERR_RECALLCONFLICT:
562 case -NFS4ERR_RETURNCONFLICT:
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.seqid = p->o_arg.seqid;
1344 p->c_res.seqid = p->c_arg.seqid;
1345 p->o_res.server = p->o_arg.server;
1346 p->o_res.access_request = p->o_arg.access;
1347 nfs_fattr_init(&p->f_attr);
1348 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1351 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1352 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1353 const struct nfs4_open_createattrs *c,
1354 enum open_claim_type4 claim,
1357 struct dentry *parent = dget_parent(dentry);
1358 struct inode *dir = d_inode(parent);
1359 struct nfs_server *server = NFS_SERVER(dir);
1360 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1361 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1362 struct nfs4_opendata *p;
1364 p = kzalloc(sizeof(*p), gfp_mask);
1368 p->f_attr.label = nfs4_label_alloc(server, gfp_mask);
1369 if (IS_ERR(p->f_attr.label))
1372 p->a_label = nfs4_label_alloc(server, gfp_mask);
1373 if (IS_ERR(p->a_label))
1376 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1377 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1378 if (IS_ERR(p->o_arg.seqid))
1379 goto err_free_label;
1380 nfs_sb_active(dentry->d_sb);
1381 p->dentry = dget(dentry);
1384 atomic_inc(&sp->so_count);
1385 p->o_arg.open_flags = flags;
1386 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1387 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1388 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1390 if (flags & O_CREAT) {
1391 p->o_arg.umask = current_umask();
1392 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1393 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1394 p->o_arg.u.attrs = &p->attrs;
1395 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1397 memcpy(p->o_arg.u.verifier.data, c->verf,
1398 sizeof(p->o_arg.u.verifier.data));
1401 /* ask server to check for all possible rights as results
1403 switch (p->o_arg.claim) {
1406 case NFS4_OPEN_CLAIM_NULL:
1407 case NFS4_OPEN_CLAIM_FH:
1408 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1409 NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE |
1410 NFS4_ACCESS_EXECUTE |
1411 nfs_access_xattr_mask(server);
1413 p->o_arg.clientid = server->nfs_client->cl_clientid;
1414 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1415 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1416 p->o_arg.name = &dentry->d_name;
1417 p->o_arg.server = server;
1418 p->o_arg.bitmask = nfs4_bitmask(server, label);
1419 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1420 switch (p->o_arg.claim) {
1421 case NFS4_OPEN_CLAIM_NULL:
1422 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1423 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1424 p->o_arg.fh = NFS_FH(dir);
1426 case NFS4_OPEN_CLAIM_PREVIOUS:
1427 case NFS4_OPEN_CLAIM_FH:
1428 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1429 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1430 p->o_arg.fh = NFS_FH(d_inode(dentry));
1432 p->c_arg.fh = &p->o_res.fh;
1433 p->c_arg.stateid = &p->o_res.stateid;
1434 p->c_arg.seqid = p->o_arg.seqid;
1435 nfs4_init_opendata_res(p);
1436 kref_init(&p->kref);
1440 nfs4_label_free(p->a_label);
1442 nfs4_label_free(p->f_attr.label);
1450 static void nfs4_opendata_free(struct kref *kref)
1452 struct nfs4_opendata *p = container_of(kref,
1453 struct nfs4_opendata, kref);
1454 struct super_block *sb = p->dentry->d_sb;
1456 nfs4_lgopen_release(p->lgp);
1457 nfs_free_seqid(p->o_arg.seqid);
1458 nfs4_sequence_free_slot(&p->o_res.seq_res);
1459 if (p->state != NULL)
1460 nfs4_put_open_state(p->state);
1461 nfs4_put_state_owner(p->owner);
1463 nfs4_label_free(p->a_label);
1464 nfs4_label_free(p->f_attr.label);
1468 nfs_sb_deactive(sb);
1469 nfs_fattr_free_names(&p->f_attr);
1470 kfree(p->f_attr.mdsthreshold);
1474 static void nfs4_opendata_put(struct nfs4_opendata *p)
1477 kref_put(&p->kref, nfs4_opendata_free);
1480 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1483 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1484 case FMODE_READ|FMODE_WRITE:
1485 return state->n_rdwr != 0;
1487 return state->n_wronly != 0;
1489 return state->n_rdonly != 0;
1495 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1496 int open_mode, enum open_claim_type4 claim)
1500 if (open_mode & (O_EXCL|O_TRUNC))
1503 case NFS4_OPEN_CLAIM_NULL:
1504 case NFS4_OPEN_CLAIM_FH:
1509 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1511 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1512 && state->n_rdonly != 0;
1515 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1516 && state->n_wronly != 0;
1518 case FMODE_READ|FMODE_WRITE:
1519 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1520 && state->n_rdwr != 0;
1526 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1527 enum open_claim_type4 claim)
1529 if (delegation == NULL)
1531 if ((delegation->type & fmode) != fmode)
1534 case NFS4_OPEN_CLAIM_NULL:
1535 case NFS4_OPEN_CLAIM_FH:
1537 case NFS4_OPEN_CLAIM_PREVIOUS:
1538 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1544 nfs_mark_delegation_referenced(delegation);
1548 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1557 case FMODE_READ|FMODE_WRITE:
1560 nfs4_state_set_mode_locked(state, state->state | fmode);
1563 #ifdef CONFIG_NFS_V4_1
1564 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1566 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1568 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1570 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1574 #endif /* CONFIG_NFS_V4_1 */
1576 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1578 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1579 wake_up_all(&state->waitq);
1582 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1584 struct nfs_client *clp = state->owner->so_server->nfs_client;
1585 bool need_recover = false;
1587 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1588 need_recover = true;
1589 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1590 need_recover = true;
1591 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1592 need_recover = true;
1594 nfs4_state_mark_reclaim_nograce(clp, state);
1598 * Check for whether or not the caller may update the open stateid
1599 * to the value passed in by stateid.
1601 * Note: This function relies heavily on the server implementing
1602 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1604 * i.e. The stateid seqids have to be initialised to 1, and
1605 * are then incremented on every state transition.
1607 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1608 const nfs4_stateid *stateid)
1610 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1611 /* The common case - we're updating to a new sequence number */
1612 if (nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1613 if (nfs4_stateid_is_next(&state->open_stateid, stateid))
1617 /* The server returned a new stateid */
1619 /* This is the first OPEN in this generation */
1620 if (stateid->seqid == cpu_to_be32(1))
1625 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1627 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1629 if (state->n_wronly)
1630 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1631 if (state->n_rdonly)
1632 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1634 set_bit(NFS_O_RDWR_STATE, &state->flags);
1635 set_bit(NFS_OPEN_STATE, &state->flags);
1638 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1639 nfs4_stateid *stateid, fmode_t fmode)
1641 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1642 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1644 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1647 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1650 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1651 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1652 clear_bit(NFS_OPEN_STATE, &state->flags);
1654 if (stateid == NULL)
1656 /* Handle OPEN+OPEN_DOWNGRADE races */
1657 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1658 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1659 nfs_resync_open_stateid_locked(state);
1662 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1663 nfs4_stateid_copy(&state->stateid, stateid);
1664 nfs4_stateid_copy(&state->open_stateid, stateid);
1665 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1667 nfs_state_log_update_open_stateid(state);
1670 static void nfs_clear_open_stateid(struct nfs4_state *state,
1671 nfs4_stateid *arg_stateid,
1672 nfs4_stateid *stateid, fmode_t fmode)
1674 write_seqlock(&state->seqlock);
1675 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1676 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1677 nfs_clear_open_stateid_locked(state, stateid, fmode);
1678 write_sequnlock(&state->seqlock);
1679 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1680 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1683 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1684 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1685 __must_hold(&state->owner->so_lock)
1686 __must_hold(&state->seqlock)
1694 if (nfs_stateid_is_sequential(state, stateid))
1699 /* Rely on seqids for serialisation with NFSv4.0 */
1700 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1703 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1704 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1706 * Ensure we process the state changes in the same order
1707 * in which the server processed them by delaying the
1708 * update of the stateid until we are in sequence.
1710 write_sequnlock(&state->seqlock);
1711 spin_unlock(&state->owner->so_lock);
1713 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1715 if (!fatal_signal_pending(current)) {
1716 if (schedule_timeout(5*HZ) == 0)
1722 finish_wait(&state->waitq, &wait);
1724 spin_lock(&state->owner->so_lock);
1725 write_seqlock(&state->seqlock);
1728 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1729 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1730 nfs4_stateid_copy(freeme, &state->open_stateid);
1731 nfs_test_and_clear_all_open_stateid(state);
1734 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1735 nfs4_stateid_copy(&state->stateid, stateid);
1736 nfs4_stateid_copy(&state->open_stateid, stateid);
1737 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1738 nfs_state_log_update_open_stateid(state);
1741 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1742 const nfs4_stateid *open_stateid,
1744 nfs4_stateid *freeme)
1747 * Protect the call to nfs4_state_set_mode_locked and
1748 * serialise the stateid update
1750 write_seqlock(&state->seqlock);
1751 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1754 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1757 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1759 case FMODE_READ|FMODE_WRITE:
1760 set_bit(NFS_O_RDWR_STATE, &state->flags);
1762 set_bit(NFS_OPEN_STATE, &state->flags);
1763 write_sequnlock(&state->seqlock);
1766 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1768 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1769 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1770 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1771 clear_bit(NFS_OPEN_STATE, &state->flags);
1774 static void nfs_state_set_delegation(struct nfs4_state *state,
1775 const nfs4_stateid *deleg_stateid,
1779 * Protect the call to nfs4_state_set_mode_locked and
1780 * serialise the stateid update
1782 write_seqlock(&state->seqlock);
1783 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1784 set_bit(NFS_DELEGATED_STATE, &state->flags);
1785 write_sequnlock(&state->seqlock);
1788 static void nfs_state_clear_delegation(struct nfs4_state *state)
1790 write_seqlock(&state->seqlock);
1791 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1792 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1793 write_sequnlock(&state->seqlock);
1796 int update_open_stateid(struct nfs4_state *state,
1797 const nfs4_stateid *open_stateid,
1798 const nfs4_stateid *delegation,
1801 struct nfs_server *server = NFS_SERVER(state->inode);
1802 struct nfs_client *clp = server->nfs_client;
1803 struct nfs_inode *nfsi = NFS_I(state->inode);
1804 struct nfs_delegation *deleg_cur;
1805 nfs4_stateid freeme = { };
1808 fmode &= (FMODE_READ|FMODE_WRITE);
1811 spin_lock(&state->owner->so_lock);
1812 if (open_stateid != NULL) {
1813 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1817 deleg_cur = nfs4_get_valid_delegation(state->inode);
1818 if (deleg_cur == NULL)
1821 spin_lock(&deleg_cur->lock);
1822 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1823 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1824 (deleg_cur->type & fmode) != fmode)
1825 goto no_delegation_unlock;
1827 if (delegation == NULL)
1828 delegation = &deleg_cur->stateid;
1829 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1830 goto no_delegation_unlock;
1832 nfs_mark_delegation_referenced(deleg_cur);
1833 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1835 no_delegation_unlock:
1836 spin_unlock(&deleg_cur->lock);
1839 update_open_stateflags(state, fmode);
1840 spin_unlock(&state->owner->so_lock);
1843 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1844 nfs4_schedule_state_manager(clp);
1845 if (freeme.type != 0)
1846 nfs4_test_and_free_stateid(server, &freeme,
1847 state->owner->so_cred);
1852 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1853 const nfs4_stateid *stateid)
1855 struct nfs4_state *state = lsp->ls_state;
1858 spin_lock(&state->state_lock);
1859 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1861 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1863 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1866 spin_unlock(&state->state_lock);
1870 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1872 struct nfs_delegation *delegation;
1874 fmode &= FMODE_READ|FMODE_WRITE;
1876 delegation = nfs4_get_valid_delegation(inode);
1877 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1882 nfs4_inode_return_delegation(inode);
1885 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1887 struct nfs4_state *state = opendata->state;
1888 struct nfs_delegation *delegation;
1889 int open_mode = opendata->o_arg.open_flags;
1890 fmode_t fmode = opendata->o_arg.fmode;
1891 enum open_claim_type4 claim = opendata->o_arg.claim;
1892 nfs4_stateid stateid;
1896 spin_lock(&state->owner->so_lock);
1897 if (can_open_cached(state, fmode, open_mode, claim)) {
1898 update_open_stateflags(state, fmode);
1899 spin_unlock(&state->owner->so_lock);
1900 goto out_return_state;
1902 spin_unlock(&state->owner->so_lock);
1904 delegation = nfs4_get_valid_delegation(state->inode);
1905 if (!can_open_delegated(delegation, fmode, claim)) {
1909 /* Save the delegation */
1910 nfs4_stateid_copy(&stateid, &delegation->stateid);
1912 nfs_release_seqid(opendata->o_arg.seqid);
1913 if (!opendata->is_recover) {
1914 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1920 /* Try to update the stateid using the delegation */
1921 if (update_open_stateid(state, NULL, &stateid, fmode))
1922 goto out_return_state;
1925 return ERR_PTR(ret);
1927 refcount_inc(&state->count);
1932 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1934 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1935 struct nfs_delegation *delegation;
1936 int delegation_flags = 0;
1939 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1941 delegation_flags = delegation->flags;
1943 switch (data->o_arg.claim) {
1946 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1947 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1948 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1949 "returning a delegation for "
1950 "OPEN(CLAIM_DELEGATE_CUR)\n",
1954 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1955 nfs_inode_set_delegation(state->inode,
1956 data->owner->so_cred,
1957 data->o_res.delegation_type,
1958 &data->o_res.delegation,
1959 data->o_res.pagemod_limit);
1961 nfs_inode_reclaim_delegation(state->inode,
1962 data->owner->so_cred,
1963 data->o_res.delegation_type,
1964 &data->o_res.delegation,
1965 data->o_res.pagemod_limit);
1967 if (data->o_res.do_recall)
1968 nfs_async_inode_return_delegation(state->inode,
1969 &data->o_res.delegation);
1973 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1974 * and update the nfs4_state.
1976 static struct nfs4_state *
1977 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1979 struct inode *inode = data->state->inode;
1980 struct nfs4_state *state = data->state;
1983 if (!data->rpc_done) {
1984 if (data->rpc_status)
1985 return ERR_PTR(data->rpc_status);
1986 return nfs4_try_open_cached(data);
1989 ret = nfs_refresh_inode(inode, &data->f_attr);
1991 return ERR_PTR(ret);
1993 if (data->o_res.delegation_type != 0)
1994 nfs4_opendata_check_deleg(data, state);
1996 if (!update_open_stateid(state, &data->o_res.stateid,
1997 NULL, data->o_arg.fmode))
1998 return ERR_PTR(-EAGAIN);
1999 refcount_inc(&state->count);
2004 static struct inode *
2005 nfs4_opendata_get_inode(struct nfs4_opendata *data)
2007 struct inode *inode;
2009 switch (data->o_arg.claim) {
2010 case NFS4_OPEN_CLAIM_NULL:
2011 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2012 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2013 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2014 return ERR_PTR(-EAGAIN);
2015 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2019 inode = d_inode(data->dentry);
2021 nfs_refresh_inode(inode, &data->f_attr);
2026 static struct nfs4_state *
2027 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2029 struct nfs4_state *state;
2030 struct inode *inode;
2032 inode = nfs4_opendata_get_inode(data);
2034 return ERR_CAST(inode);
2035 if (data->state != NULL && data->state->inode == inode) {
2036 state = data->state;
2037 refcount_inc(&state->count);
2039 state = nfs4_get_open_state(inode, data->owner);
2042 state = ERR_PTR(-ENOMEM);
2046 static struct nfs4_state *
2047 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2049 struct nfs4_state *state;
2051 if (!data->rpc_done) {
2052 state = nfs4_try_open_cached(data);
2053 trace_nfs4_cached_open(data->state);
2057 state = nfs4_opendata_find_nfs4_state(data);
2061 if (data->o_res.delegation_type != 0)
2062 nfs4_opendata_check_deleg(data, state);
2063 if (!update_open_stateid(state, &data->o_res.stateid,
2064 NULL, data->o_arg.fmode)) {
2065 nfs4_put_open_state(state);
2066 state = ERR_PTR(-EAGAIN);
2069 nfs_release_seqid(data->o_arg.seqid);
2073 static struct nfs4_state *
2074 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2076 struct nfs4_state *ret;
2078 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2079 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2081 ret = _nfs4_opendata_to_nfs4_state(data);
2082 nfs4_sequence_free_slot(&data->o_res.seq_res);
2086 static struct nfs_open_context *
2087 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2089 struct nfs_inode *nfsi = NFS_I(state->inode);
2090 struct nfs_open_context *ctx;
2093 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2094 if (ctx->state != state)
2096 if ((ctx->mode & mode) != mode)
2098 if (!get_nfs_open_context(ctx))
2104 return ERR_PTR(-ENOENT);
2107 static struct nfs_open_context *
2108 nfs4_state_find_open_context(struct nfs4_state *state)
2110 struct nfs_open_context *ctx;
2112 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2115 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2118 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2121 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2122 struct nfs4_state *state, enum open_claim_type4 claim)
2124 struct nfs4_opendata *opendata;
2126 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2127 NULL, claim, GFP_NOFS);
2128 if (opendata == NULL)
2129 return ERR_PTR(-ENOMEM);
2130 opendata->state = state;
2131 refcount_inc(&state->count);
2135 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2138 struct nfs4_state *newstate;
2139 struct nfs_server *server = NFS_SB(opendata->dentry->d_sb);
2140 int openflags = opendata->o_arg.open_flags;
2143 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2145 opendata->o_arg.fmode = fmode;
2146 opendata->o_arg.share_access =
2147 nfs4_map_atomic_open_share(server, fmode, openflags);
2148 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2149 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2150 nfs4_init_opendata_res(opendata);
2151 ret = _nfs4_recover_proc_open(opendata);
2154 newstate = nfs4_opendata_to_nfs4_state(opendata);
2155 if (IS_ERR(newstate))
2156 return PTR_ERR(newstate);
2157 if (newstate != opendata->state)
2159 nfs4_close_state(newstate, fmode);
2163 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2167 /* memory barrier prior to reading state->n_* */
2169 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2172 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2175 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2179 * We may have performed cached opens for all three recoveries.
2180 * Check if we need to update the current stateid.
2182 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2183 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2184 write_seqlock(&state->seqlock);
2185 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2186 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2187 write_sequnlock(&state->seqlock);
2194 * reclaim state on the server after a reboot.
2196 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2198 struct nfs_delegation *delegation;
2199 struct nfs4_opendata *opendata;
2200 fmode_t delegation_type = 0;
2203 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2204 NFS4_OPEN_CLAIM_PREVIOUS);
2205 if (IS_ERR(opendata))
2206 return PTR_ERR(opendata);
2208 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2209 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2210 delegation_type = delegation->type;
2212 opendata->o_arg.u.delegation_type = delegation_type;
2213 status = nfs4_open_recover(opendata, state);
2214 nfs4_opendata_put(opendata);
2218 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2220 struct nfs_server *server = NFS_SERVER(state->inode);
2221 struct nfs4_exception exception = { };
2224 err = _nfs4_do_open_reclaim(ctx, state);
2225 trace_nfs4_open_reclaim(ctx, 0, err);
2226 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2228 if (err != -NFS4ERR_DELAY)
2230 nfs4_handle_exception(server, err, &exception);
2231 } while (exception.retry);
2235 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2237 struct nfs_open_context *ctx;
2240 ctx = nfs4_state_find_open_context(state);
2243 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2244 nfs_state_clear_open_state_flags(state);
2245 ret = nfs4_do_open_reclaim(ctx, state);
2246 put_nfs_open_context(ctx);
2250 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)
2254 printk(KERN_ERR "NFS: %s: unhandled error "
2255 "%d.\n", __func__, err);
2263 case -NFS4ERR_BADSESSION:
2264 case -NFS4ERR_BADSLOT:
2265 case -NFS4ERR_BAD_HIGH_SLOT:
2266 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2267 case -NFS4ERR_DEADSESSION:
2269 case -NFS4ERR_STALE_CLIENTID:
2270 case -NFS4ERR_STALE_STATEID:
2271 /* Don't recall a delegation if it was lost */
2272 nfs4_schedule_lease_recovery(server->nfs_client);
2274 case -NFS4ERR_MOVED:
2275 nfs4_schedule_migration_recovery(server);
2277 case -NFS4ERR_LEASE_MOVED:
2278 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2280 case -NFS4ERR_DELEG_REVOKED:
2281 case -NFS4ERR_ADMIN_REVOKED:
2282 case -NFS4ERR_EXPIRED:
2283 case -NFS4ERR_BAD_STATEID:
2284 case -NFS4ERR_OPENMODE:
2285 nfs_inode_find_state_and_recover(state->inode,
2287 nfs4_schedule_stateid_recovery(server, state);
2289 case -NFS4ERR_DELAY:
2290 case -NFS4ERR_GRACE:
2294 case -NFS4ERR_DENIED:
2296 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2298 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2305 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2306 struct nfs4_state *state, const nfs4_stateid *stateid)
2308 struct nfs_server *server = NFS_SERVER(state->inode);
2309 struct nfs4_opendata *opendata;
2312 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2313 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2314 if (IS_ERR(opendata))
2315 return PTR_ERR(opendata);
2316 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2317 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2318 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2322 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2323 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2327 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2328 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2332 nfs_state_clear_delegation(state);
2334 nfs4_opendata_put(opendata);
2335 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2338 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2340 struct nfs4_opendata *data = calldata;
2342 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2343 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2346 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2348 struct nfs4_opendata *data = calldata;
2350 nfs40_sequence_done(task, &data->c_res.seq_res);
2352 data->rpc_status = task->tk_status;
2353 if (data->rpc_status == 0) {
2354 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2355 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2356 renew_lease(data->o_res.server, data->timestamp);
2357 data->rpc_done = true;
2361 static void nfs4_open_confirm_release(void *calldata)
2363 struct nfs4_opendata *data = calldata;
2364 struct nfs4_state *state = NULL;
2366 /* If this request hasn't been cancelled, do nothing */
2367 if (!data->cancelled)
2369 /* In case of error, no cleanup! */
2370 if (!data->rpc_done)
2372 state = nfs4_opendata_to_nfs4_state(data);
2374 nfs4_close_state(state, data->o_arg.fmode);
2376 nfs4_opendata_put(data);
2379 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2380 .rpc_call_prepare = nfs4_open_confirm_prepare,
2381 .rpc_call_done = nfs4_open_confirm_done,
2382 .rpc_release = nfs4_open_confirm_release,
2386 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2388 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2390 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2391 struct rpc_task *task;
2392 struct rpc_message msg = {
2393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2394 .rpc_argp = &data->c_arg,
2395 .rpc_resp = &data->c_res,
2396 .rpc_cred = data->owner->so_cred,
2398 struct rpc_task_setup task_setup_data = {
2399 .rpc_client = server->client,
2400 .rpc_message = &msg,
2401 .callback_ops = &nfs4_open_confirm_ops,
2402 .callback_data = data,
2403 .workqueue = nfsiod_workqueue,
2404 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2408 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2410 kref_get(&data->kref);
2411 data->rpc_done = false;
2412 data->rpc_status = 0;
2413 data->timestamp = jiffies;
2414 task = rpc_run_task(&task_setup_data);
2416 return PTR_ERR(task);
2417 status = rpc_wait_for_completion_task(task);
2419 data->cancelled = true;
2422 status = data->rpc_status;
2427 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2429 struct nfs4_opendata *data = calldata;
2430 struct nfs4_state_owner *sp = data->owner;
2431 struct nfs_client *clp = sp->so_server->nfs_client;
2432 enum open_claim_type4 claim = data->o_arg.claim;
2434 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2437 * Check if we still need to send an OPEN call, or if we can use
2438 * a delegation instead.
2440 if (data->state != NULL) {
2441 struct nfs_delegation *delegation;
2443 if (can_open_cached(data->state, data->o_arg.fmode,
2444 data->o_arg.open_flags, claim))
2447 delegation = nfs4_get_valid_delegation(data->state->inode);
2448 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2449 goto unlock_no_action;
2452 /* Update client id. */
2453 data->o_arg.clientid = clp->cl_clientid;
2457 case NFS4_OPEN_CLAIM_PREVIOUS:
2458 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2459 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2460 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2462 case NFS4_OPEN_CLAIM_FH:
2463 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2465 data->timestamp = jiffies;
2466 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2467 &data->o_arg.seq_args,
2468 &data->o_res.seq_res,
2470 nfs_release_seqid(data->o_arg.seqid);
2472 /* Set the create mode (note dependency on the session type) */
2473 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2474 if (data->o_arg.open_flags & O_EXCL) {
2475 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2476 if (clp->cl_mvops->minor_version == 0) {
2477 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2478 /* don't put an ACCESS op in OPEN compound if O_EXCL,
2479 * because ACCESS will return permission denied for
2480 * all bits until close */
2481 data->o_res.access_request = data->o_arg.access = 0;
2482 } else if (nfs4_has_persistent_session(clp))
2483 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2487 trace_nfs4_cached_open(data->state);
2490 task->tk_action = NULL;
2492 nfs4_sequence_done(task, &data->o_res.seq_res);
2495 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2497 struct nfs4_opendata *data = calldata;
2499 data->rpc_status = task->tk_status;
2501 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2504 if (task->tk_status == 0) {
2505 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2506 switch (data->o_res.f_attr->mode & S_IFMT) {
2510 data->rpc_status = -ELOOP;
2513 data->rpc_status = -EISDIR;
2516 data->rpc_status = -ENOTDIR;
2519 renew_lease(data->o_res.server, data->timestamp);
2520 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2521 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2523 data->rpc_done = true;
2526 static void nfs4_open_release(void *calldata)
2528 struct nfs4_opendata *data = calldata;
2529 struct nfs4_state *state = NULL;
2531 /* If this request hasn't been cancelled, do nothing */
2532 if (!data->cancelled)
2534 /* In case of error, no cleanup! */
2535 if (data->rpc_status != 0 || !data->rpc_done)
2537 /* In case we need an open_confirm, no cleanup! */
2538 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2540 state = nfs4_opendata_to_nfs4_state(data);
2542 nfs4_close_state(state, data->o_arg.fmode);
2544 nfs4_opendata_put(data);
2547 static const struct rpc_call_ops nfs4_open_ops = {
2548 .rpc_call_prepare = nfs4_open_prepare,
2549 .rpc_call_done = nfs4_open_done,
2550 .rpc_release = nfs4_open_release,
2553 static int nfs4_run_open_task(struct nfs4_opendata *data,
2554 struct nfs_open_context *ctx)
2556 struct inode *dir = d_inode(data->dir);
2557 struct nfs_server *server = NFS_SERVER(dir);
2558 struct nfs_openargs *o_arg = &data->o_arg;
2559 struct nfs_openres *o_res = &data->o_res;
2560 struct rpc_task *task;
2561 struct rpc_message msg = {
2562 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2565 .rpc_cred = data->owner->so_cred,
2567 struct rpc_task_setup task_setup_data = {
2568 .rpc_client = server->client,
2569 .rpc_message = &msg,
2570 .callback_ops = &nfs4_open_ops,
2571 .callback_data = data,
2572 .workqueue = nfsiod_workqueue,
2573 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2577 if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
2578 task_setup_data.flags |= RPC_TASK_MOVEABLE;
2580 kref_get(&data->kref);
2581 data->rpc_done = false;
2582 data->rpc_status = 0;
2583 data->cancelled = false;
2584 data->is_recover = false;
2586 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2587 data->is_recover = true;
2588 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2590 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2591 pnfs_lgopen_prepare(data, ctx);
2593 task = rpc_run_task(&task_setup_data);
2595 return PTR_ERR(task);
2596 status = rpc_wait_for_completion_task(task);
2598 data->cancelled = true;
2601 status = data->rpc_status;
2607 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2609 struct inode *dir = d_inode(data->dir);
2610 struct nfs_openres *o_res = &data->o_res;
2613 status = nfs4_run_open_task(data, NULL);
2614 if (status != 0 || !data->rpc_done)
2617 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2619 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2620 status = _nfs4_proc_open_confirm(data);
2626 * Additional permission checks in order to distinguish between an
2627 * open for read, and an open for execute. This works around the
2628 * fact that NFSv4 OPEN treats read and execute permissions as being
2630 * Note that in the non-execute case, we want to turn off permission
2631 * checking if we just created a new file (POSIX open() semantics).
2633 static int nfs4_opendata_access(const struct cred *cred,
2634 struct nfs4_opendata *opendata,
2635 struct nfs4_state *state, fmode_t fmode)
2637 struct nfs_access_entry cache;
2640 /* access call failed or for some reason the server doesn't
2641 * support any access modes -- defer access call until later */
2642 if (opendata->o_res.access_supported == 0)
2646 if (fmode & FMODE_EXEC) {
2647 /* ONLY check for exec rights */
2648 if (S_ISDIR(state->inode->i_mode))
2649 mask = NFS4_ACCESS_LOOKUP;
2651 mask = NFS4_ACCESS_EXECUTE;
2652 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2653 mask = NFS4_ACCESS_READ;
2655 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2656 nfs_access_add_cache(state->inode, &cache, cred);
2658 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2659 if ((mask & ~cache.mask & flags) == 0)
2666 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2668 static int _nfs4_proc_open(struct nfs4_opendata *data,
2669 struct nfs_open_context *ctx)
2671 struct inode *dir = d_inode(data->dir);
2672 struct nfs_server *server = NFS_SERVER(dir);
2673 struct nfs_openargs *o_arg = &data->o_arg;
2674 struct nfs_openres *o_res = &data->o_res;
2677 status = nfs4_run_open_task(data, ctx);
2678 if (!data->rpc_done)
2681 if (status == -NFS4ERR_BADNAME &&
2682 !(o_arg->open_flags & O_CREAT))
2687 nfs_fattr_map_and_free_names(server, &data->f_attr);
2689 if (o_arg->open_flags & O_CREAT) {
2690 if (o_arg->open_flags & O_EXCL)
2691 data->file_created = true;
2692 else if (o_res->cinfo.before != o_res->cinfo.after)
2693 data->file_created = true;
2694 if (data->file_created ||
2695 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2696 nfs4_update_changeattr(dir, &o_res->cinfo,
2697 o_res->f_attr->time_start,
2698 NFS_INO_INVALID_DATA);
2700 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2701 server->caps &= ~NFS_CAP_POSIX_LOCK;
2702 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2703 status = _nfs4_proc_open_confirm(data);
2707 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2708 struct nfs_fh *fh = &o_res->fh;
2710 nfs4_sequence_free_slot(&o_res->seq_res);
2711 if (o_arg->claim == NFS4_OPEN_CLAIM_FH)
2712 fh = NFS_FH(d_inode(data->dentry));
2713 nfs4_proc_getattr(server, fh, o_res->f_attr, NULL);
2720 * reclaim state on the server after a network partition.
2721 * Assumes caller holds the appropriate lock
2723 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2725 struct nfs4_opendata *opendata;
2728 opendata = nfs4_open_recoverdata_alloc(ctx, state, NFS4_OPEN_CLAIM_FH);
2729 if (IS_ERR(opendata))
2730 return PTR_ERR(opendata);
2732 * We're not recovering a delegation, so ask for no delegation.
2733 * Otherwise the recovery thread could deadlock with an outstanding
2734 * delegation return.
2736 opendata->o_arg.open_flags = O_DIRECT;
2737 ret = nfs4_open_recover(opendata, state);
2739 d_drop(ctx->dentry);
2740 nfs4_opendata_put(opendata);
2744 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2746 struct nfs_server *server = NFS_SERVER(state->inode);
2747 struct nfs4_exception exception = { };
2751 err = _nfs4_open_expired(ctx, state);
2752 trace_nfs4_open_expired(ctx, 0, err);
2753 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2758 case -NFS4ERR_GRACE:
2759 case -NFS4ERR_DELAY:
2760 nfs4_handle_exception(server, err, &exception);
2763 } while (exception.retry);
2768 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2770 struct nfs_open_context *ctx;
2773 ctx = nfs4_state_find_open_context(state);
2776 ret = nfs4_do_open_expired(ctx, state);
2777 put_nfs_open_context(ctx);
2781 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2782 const nfs4_stateid *stateid)
2784 nfs_remove_bad_delegation(state->inode, stateid);
2785 nfs_state_clear_delegation(state);
2788 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2790 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2791 nfs_finish_clear_delegation_stateid(state, NULL);
2794 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2796 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2797 nfs40_clear_delegation_stateid(state);
2798 nfs_state_clear_open_state_flags(state);
2799 return nfs4_open_expired(sp, state);
2802 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2803 nfs4_stateid *stateid,
2804 const struct cred *cred)
2806 return -NFS4ERR_BAD_STATEID;
2809 #if defined(CONFIG_NFS_V4_1)
2810 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2811 nfs4_stateid *stateid,
2812 const struct cred *cred)
2816 switch (stateid->type) {
2819 case NFS4_INVALID_STATEID_TYPE:
2820 case NFS4_SPECIAL_STATEID_TYPE:
2821 return -NFS4ERR_BAD_STATEID;
2822 case NFS4_REVOKED_STATEID_TYPE:
2826 status = nfs41_test_stateid(server, stateid, cred);
2828 case -NFS4ERR_EXPIRED:
2829 case -NFS4ERR_ADMIN_REVOKED:
2830 case -NFS4ERR_DELEG_REVOKED:
2836 /* Ack the revoked state to the server */
2837 nfs41_free_stateid(server, stateid, cred, true);
2838 return -NFS4ERR_EXPIRED;
2841 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2843 struct nfs_server *server = NFS_SERVER(state->inode);
2844 nfs4_stateid stateid;
2845 struct nfs_delegation *delegation;
2846 const struct cred *cred = NULL;
2847 int status, ret = NFS_OK;
2849 /* Get the delegation credential for use by test/free_stateid */
2851 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2852 if (delegation == NULL) {
2854 nfs_state_clear_delegation(state);
2858 spin_lock(&delegation->lock);
2859 nfs4_stateid_copy(&stateid, &delegation->stateid);
2861 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2862 &delegation->flags)) {
2863 spin_unlock(&delegation->lock);
2868 if (delegation->cred)
2869 cred = get_cred(delegation->cred);
2870 spin_unlock(&delegation->lock);
2872 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2873 trace_nfs4_test_delegation_stateid(state, NULL, status);
2874 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2875 nfs_finish_clear_delegation_stateid(state, &stateid);
2883 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2887 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2888 nfs4_copy_delegation_stateid(state->inode, state->state,
2890 nfs4_stateid_match_other(&state->stateid, &tmp))
2891 nfs_state_set_delegation(state, &tmp, state->state);
2893 nfs_state_clear_delegation(state);
2897 * nfs41_check_expired_locks - possibly free a lock stateid
2899 * @state: NFSv4 state for an inode
2901 * Returns NFS_OK if recovery for this stateid is now finished.
2902 * Otherwise a negative NFS4ERR value is returned.
2904 static int nfs41_check_expired_locks(struct nfs4_state *state)
2906 int status, ret = NFS_OK;
2907 struct nfs4_lock_state *lsp, *prev = NULL;
2908 struct nfs_server *server = NFS_SERVER(state->inode);
2910 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2913 spin_lock(&state->state_lock);
2914 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2915 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2916 const struct cred *cred = lsp->ls_state->owner->so_cred;
2918 refcount_inc(&lsp->ls_count);
2919 spin_unlock(&state->state_lock);
2921 nfs4_put_lock_state(prev);
2924 status = nfs41_test_and_free_expired_stateid(server,
2927 trace_nfs4_test_lock_stateid(state, lsp, status);
2928 if (status == -NFS4ERR_EXPIRED ||
2929 status == -NFS4ERR_BAD_STATEID) {
2930 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2931 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2932 if (!recover_lost_locks)
2933 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2934 } else if (status != NFS_OK) {
2936 nfs4_put_lock_state(prev);
2939 spin_lock(&state->state_lock);
2942 spin_unlock(&state->state_lock);
2943 nfs4_put_lock_state(prev);
2949 * nfs41_check_open_stateid - possibly free an open stateid
2951 * @state: NFSv4 state for an inode
2953 * Returns NFS_OK if recovery for this stateid is now finished.
2954 * Otherwise a negative NFS4ERR value is returned.
2956 static int nfs41_check_open_stateid(struct nfs4_state *state)
2958 struct nfs_server *server = NFS_SERVER(state->inode);
2959 nfs4_stateid *stateid = &state->open_stateid;
2960 const struct cred *cred = state->owner->so_cred;
2963 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2964 return -NFS4ERR_BAD_STATEID;
2965 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2966 trace_nfs4_test_open_stateid(state, NULL, status);
2967 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2968 nfs_state_clear_open_state_flags(state);
2969 stateid->type = NFS4_INVALID_STATEID_TYPE;
2972 if (nfs_open_stateid_recover_openmode(state))
2973 return -NFS4ERR_OPENMODE;
2977 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2981 status = nfs41_check_delegation_stateid(state);
2982 if (status != NFS_OK)
2984 nfs41_delegation_recover_stateid(state);
2986 status = nfs41_check_expired_locks(state);
2987 if (status != NFS_OK)
2989 status = nfs41_check_open_stateid(state);
2990 if (status != NFS_OK)
2991 status = nfs4_open_expired(sp, state);
2997 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2998 * fields corresponding to attributes that were used to store the verifier.
2999 * Make sure we clobber those fields in the later setattr call
3001 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
3002 struct iattr *sattr, struct nfs4_label **label)
3004 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
3009 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
3010 attrset[i] = opendata->o_res.attrset[i];
3011 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3012 attrset[i] &= ~bitmask[i];
3015 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3016 sattr->ia_valid : 0;
3018 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3019 if (sattr->ia_valid & ATTR_ATIME_SET)
3020 ret |= ATTR_ATIME_SET;
3025 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3026 if (sattr->ia_valid & ATTR_MTIME_SET)
3027 ret |= ATTR_MTIME_SET;
3032 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3037 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3038 struct nfs_open_context *ctx)
3040 struct nfs4_state_owner *sp = opendata->owner;
3041 struct nfs_server *server = sp->so_server;
3042 struct dentry *dentry;
3043 struct nfs4_state *state;
3044 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3045 struct inode *dir = d_inode(opendata->dir);
3046 unsigned long dir_verifier;
3050 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3051 dir_verifier = nfs_save_change_attribute(dir);
3053 ret = _nfs4_proc_open(opendata, ctx);
3057 state = _nfs4_opendata_to_nfs4_state(opendata);
3058 ret = PTR_ERR(state);
3062 if (server->caps & NFS_CAP_POSIX_LOCK)
3063 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3064 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3065 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3066 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_PRESERVE_UNLINKED)
3067 set_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(state->inode)->flags);
3069 dentry = opendata->dentry;
3070 if (d_really_is_negative(dentry)) {
3071 struct dentry *alias;
3073 alias = d_exact_alias(dentry, state->inode);
3075 alias = d_splice_alias(igrab(state->inode), dentry);
3076 /* d_splice_alias() can't fail here - it's a non-directory */
3079 ctx->dentry = dentry = alias;
3083 switch(opendata->o_arg.claim) {
3086 case NFS4_OPEN_CLAIM_NULL:
3087 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3088 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3089 if (!opendata->rpc_done)
3091 if (opendata->o_res.delegation_type != 0)
3092 dir_verifier = nfs_save_change_attribute(dir);
3093 nfs_set_verifier(dentry, dir_verifier);
3096 /* Parse layoutget results before we check for access */
3097 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3099 ret = nfs4_opendata_access(sp->so_cred, opendata, state, acc_mode);
3103 if (d_inode(dentry) == state->inode) {
3104 nfs_inode_attach_open_context(ctx);
3105 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3106 nfs4_schedule_stateid_recovery(server, state);
3110 if (!opendata->cancelled) {
3111 if (opendata->lgp) {
3112 nfs4_lgopen_release(opendata->lgp);
3113 opendata->lgp = NULL;
3115 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3121 * Returns a referenced nfs4_state
3123 static int _nfs4_do_open(struct inode *dir,
3124 struct nfs_open_context *ctx,
3126 const struct nfs4_open_createattrs *c,
3129 struct nfs4_state_owner *sp;
3130 struct nfs4_state *state = NULL;
3131 struct nfs_server *server = NFS_SERVER(dir);
3132 struct nfs4_opendata *opendata;
3133 struct dentry *dentry = ctx->dentry;
3134 const struct cred *cred = ctx->cred;
3135 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3136 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3137 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3138 struct iattr *sattr = c->sattr;
3139 struct nfs4_label *label = c->label;
3142 /* Protect against reboot recovery conflicts */
3144 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3146 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3149 status = nfs4_client_recover_expired_lease(server->nfs_client);
3151 goto err_put_state_owner;
3152 if (d_really_is_positive(dentry))
3153 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3155 if (d_really_is_positive(dentry))
3156 claim = NFS4_OPEN_CLAIM_FH;
3157 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3158 c, claim, GFP_KERNEL);
3159 if (opendata == NULL)
3160 goto err_put_state_owner;
3162 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3163 if (!opendata->f_attr.mdsthreshold) {
3164 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3165 if (!opendata->f_attr.mdsthreshold)
3166 goto err_opendata_put;
3168 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3170 if (d_really_is_positive(dentry))
3171 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3173 status = _nfs4_open_and_get_state(opendata, ctx);
3175 goto err_opendata_put;
3178 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3179 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3180 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3182 * send create attributes which was not set by open
3183 * with an extra setattr.
3185 if (attrs || label) {
3186 unsigned ia_old = sattr->ia_valid;
3188 sattr->ia_valid = attrs;
3189 nfs_fattr_init(opendata->o_res.f_attr);
3190 status = nfs4_do_setattr(state->inode, cred,
3191 opendata->o_res.f_attr, sattr,
3194 nfs_setattr_update_inode(state->inode, sattr,
3195 opendata->o_res.f_attr);
3196 nfs_setsecurity(state->inode, opendata->o_res.f_attr);
3198 sattr->ia_valid = ia_old;
3201 if (opened && opendata->file_created)
3204 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3205 *ctx_th = opendata->f_attr.mdsthreshold;
3206 opendata->f_attr.mdsthreshold = NULL;
3209 nfs4_opendata_put(opendata);
3210 nfs4_put_state_owner(sp);
3213 nfs4_opendata_put(opendata);
3214 err_put_state_owner:
3215 nfs4_put_state_owner(sp);
3221 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3222 struct nfs_open_context *ctx,
3224 struct iattr *sattr,
3225 struct nfs4_label *label,
3228 struct nfs_server *server = NFS_SERVER(dir);
3229 struct nfs4_exception exception = {
3230 .interruptible = true,
3232 struct nfs4_state *res;
3233 struct nfs4_open_createattrs c = {
3237 [0] = (__u32)jiffies,
3238 [1] = (__u32)current->pid,
3244 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3246 trace_nfs4_open_file(ctx, flags, status);
3249 /* NOTE: BAD_SEQID means the server and client disagree about the
3250 * book-keeping w.r.t. state-changing operations
3251 * (OPEN/CLOSE/LOCK/LOCKU...)
3252 * It is actually a sign of a bug on the client or on the server.
3254 * If we receive a BAD_SEQID error in the particular case of
3255 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3256 * have unhashed the old state_owner for us, and that we can
3257 * therefore safely retry using a new one. We should still warn
3258 * the user though...
3260 if (status == -NFS4ERR_BAD_SEQID) {
3261 pr_warn_ratelimited("NFS: v4 server %s "
3262 " returned a bad sequence-id error!\n",
3263 NFS_SERVER(dir)->nfs_client->cl_hostname);
3264 exception.retry = 1;
3268 * BAD_STATEID on OPEN means that the server cancelled our
3269 * state before it received the OPEN_CONFIRM.
3270 * Recover by retrying the request as per the discussion
3271 * on Page 181 of RFC3530.
3273 if (status == -NFS4ERR_BAD_STATEID) {
3274 exception.retry = 1;
3277 if (status == -NFS4ERR_EXPIRED) {
3278 nfs4_schedule_lease_recovery(server->nfs_client);
3279 exception.retry = 1;
3282 if (status == -EAGAIN) {
3283 /* We must have found a delegation */
3284 exception.retry = 1;
3287 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3289 res = ERR_PTR(nfs4_handle_exception(server,
3290 status, &exception));
3291 } while (exception.retry);
3295 static int _nfs4_do_setattr(struct inode *inode,
3296 struct nfs_setattrargs *arg,
3297 struct nfs_setattrres *res,
3298 const struct cred *cred,
3299 struct nfs_open_context *ctx)
3301 struct nfs_server *server = NFS_SERVER(inode);
3302 struct rpc_message msg = {
3303 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3308 const struct cred *delegation_cred = NULL;
3309 unsigned long timestamp = jiffies;
3313 nfs_fattr_init(res->fattr);
3315 /* Servers should only apply open mode checks for file size changes */
3316 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3318 nfs4_inode_make_writeable(inode);
3322 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3323 /* Use that stateid */
3324 } else if (ctx != NULL && ctx->state) {
3325 struct nfs_lock_context *l_ctx;
3326 if (!nfs4_valid_open_stateid(ctx->state))
3328 l_ctx = nfs_get_lock_context(ctx);
3330 return PTR_ERR(l_ctx);
3331 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3332 &arg->stateid, &delegation_cred);
3333 nfs_put_lock_context(l_ctx);
3336 else if (status == -EAGAIN)
3340 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3342 if (delegation_cred)
3343 msg.rpc_cred = delegation_cred;
3345 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3347 put_cred(delegation_cred);
3348 if (status == 0 && ctx != NULL)
3349 renew_lease(server, timestamp);
3350 trace_nfs4_setattr(inode, &arg->stateid, status);
3354 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3355 struct nfs_fattr *fattr, struct iattr *sattr,
3356 struct nfs_open_context *ctx, struct nfs4_label *ilabel)
3358 struct nfs_server *server = NFS_SERVER(inode);
3359 __u32 bitmask[NFS4_BITMASK_SZ];
3360 struct nfs4_state *state = ctx ? ctx->state : NULL;
3361 struct nfs_setattrargs arg = {
3362 .fh = NFS_FH(inode),
3368 struct nfs_setattrres res = {
3372 struct nfs4_exception exception = {
3375 .stateid = &arg.stateid,
3377 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
3380 if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3381 adjust_flags |= NFS_INO_INVALID_MODE;
3382 if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
3383 adjust_flags |= NFS_INO_INVALID_OTHER;
3386 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label),
3387 inode, adjust_flags);
3389 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3391 case -NFS4ERR_OPENMODE:
3392 if (!(sattr->ia_valid & ATTR_SIZE)) {
3393 pr_warn_once("NFSv4: server %s is incorrectly "
3394 "applying open mode checks to "
3395 "a SETATTR that is not "
3396 "changing file size.\n",
3397 server->nfs_client->cl_hostname);
3399 if (state && !(state->state & FMODE_WRITE)) {
3401 if (sattr->ia_valid & ATTR_OPEN)
3406 err = nfs4_handle_exception(server, err, &exception);
3407 } while (exception.retry);
3413 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3415 if (inode == NULL || !nfs_have_layout(inode))
3418 return pnfs_wait_on_layoutreturn(inode, task);
3422 * Update the seqid of an open stateid
3424 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3425 struct nfs4_state *state)
3432 if (!nfs4_valid_open_stateid(state))
3434 seq = read_seqbegin(&state->seqlock);
3435 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3436 nfs4_stateid_copy(dst, &state->open_stateid);
3437 if (read_seqretry(&state->seqlock, seq))
3441 seqid_open = state->open_stateid.seqid;
3442 if (read_seqretry(&state->seqlock, seq))
3445 dst_seqid = be32_to_cpu(dst->seqid);
3446 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3447 dst->seqid = seqid_open;
3453 * Update the seqid of an open stateid after receiving
3454 * NFS4ERR_OLD_STATEID
3456 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3457 struct nfs4_state *state)
3462 int seq, status = -EAGAIN;
3467 if (!nfs4_valid_open_stateid(state))
3469 seq = read_seqbegin(&state->seqlock);
3470 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3471 if (read_seqretry(&state->seqlock, seq))
3476 write_seqlock(&state->seqlock);
3477 seqid_open = state->open_stateid.seqid;
3479 dst_seqid = be32_to_cpu(dst->seqid);
3481 /* Did another OPEN bump the state's seqid? try again: */
3482 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3483 dst->seqid = seqid_open;
3484 write_sequnlock(&state->seqlock);
3489 /* server says we're behind but we haven't seen the update yet */
3490 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3491 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3492 write_sequnlock(&state->seqlock);
3493 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3495 if (fatal_signal_pending(current))
3498 if (schedule_timeout(5*HZ) != 0)
3501 finish_wait(&state->waitq, &wait);
3505 if (status == -EINTR)
3508 /* we slept the whole 5 seconds, we must have lost a seqid */
3509 dst->seqid = cpu_to_be32(dst_seqid + 1);
3517 struct nfs4_closedata {
3518 struct inode *inode;
3519 struct nfs4_state *state;
3520 struct nfs_closeargs arg;
3521 struct nfs_closeres res;
3523 struct nfs4_layoutreturn_args arg;
3524 struct nfs4_layoutreturn_res res;
3525 struct nfs4_xdr_opaque_data ld_private;
3529 struct nfs_fattr fattr;
3530 unsigned long timestamp;
3533 static void nfs4_free_closedata(void *data)
3535 struct nfs4_closedata *calldata = data;
3536 struct nfs4_state_owner *sp = calldata->state->owner;
3537 struct super_block *sb = calldata->state->inode->i_sb;
3539 if (calldata->lr.roc)
3540 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3541 calldata->res.lr_ret);
3542 nfs4_put_open_state(calldata->state);
3543 nfs_free_seqid(calldata->arg.seqid);
3544 nfs4_put_state_owner(sp);
3545 nfs_sb_deactive(sb);
3549 static void nfs4_close_done(struct rpc_task *task, void *data)
3551 struct nfs4_closedata *calldata = data;
3552 struct nfs4_state *state = calldata->state;
3553 struct nfs_server *server = NFS_SERVER(calldata->inode);
3554 nfs4_stateid *res_stateid = NULL;
3555 struct nfs4_exception exception = {
3557 .inode = calldata->inode,
3558 .stateid = &calldata->arg.stateid,
3561 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3563 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3565 /* Handle Layoutreturn errors */
3566 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3567 &calldata->res.lr_ret) == -EAGAIN)
3570 /* hmm. we are done with the inode, and in the process of freeing
3571 * the state_owner. we keep this around to process errors
3573 switch (task->tk_status) {
3575 res_stateid = &calldata->res.stateid;
3576 renew_lease(server, calldata->timestamp);
3578 case -NFS4ERR_ACCESS:
3579 if (calldata->arg.bitmask != NULL) {
3580 calldata->arg.bitmask = NULL;
3581 calldata->res.fattr = NULL;
3586 case -NFS4ERR_OLD_STATEID:
3587 /* Did we race with OPEN? */
3588 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3592 case -NFS4ERR_ADMIN_REVOKED:
3593 case -NFS4ERR_STALE_STATEID:
3594 case -NFS4ERR_EXPIRED:
3595 nfs4_free_revoked_stateid(server,
3596 &calldata->arg.stateid,
3597 task->tk_msg.rpc_cred);
3599 case -NFS4ERR_BAD_STATEID:
3600 if (calldata->arg.fmode == 0)
3604 task->tk_status = nfs4_async_handle_exception(task,
3605 server, task->tk_status, &exception);
3606 if (exception.retry)
3609 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3610 res_stateid, calldata->arg.fmode);
3612 task->tk_status = 0;
3613 nfs_release_seqid(calldata->arg.seqid);
3614 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3615 dprintk("%s: ret = %d\n", __func__, task->tk_status);
3618 task->tk_status = 0;
3619 rpc_restart_call_prepare(task);
3623 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3625 struct nfs4_closedata *calldata = data;
3626 struct nfs4_state *state = calldata->state;
3627 struct inode *inode = calldata->inode;
3628 struct nfs_server *server = NFS_SERVER(inode);
3629 struct pnfs_layout_hdr *lo;
3630 bool is_rdonly, is_wronly, is_rdwr;
3633 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3636 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3637 spin_lock(&state->owner->so_lock);
3638 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3639 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3640 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3641 /* Calculate the change in open mode */
3642 calldata->arg.fmode = 0;
3643 if (state->n_rdwr == 0) {
3644 if (state->n_rdonly == 0)
3645 call_close |= is_rdonly;
3647 calldata->arg.fmode |= FMODE_READ;
3648 if (state->n_wronly == 0)
3649 call_close |= is_wronly;
3651 calldata->arg.fmode |= FMODE_WRITE;
3652 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3653 call_close |= is_rdwr;
3655 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3657 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3658 if (!nfs4_valid_open_stateid(state))
3660 spin_unlock(&state->owner->so_lock);
3663 /* Note: exit _without_ calling nfs4_close_done */
3667 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3668 nfs_release_seqid(calldata->arg.seqid);
3672 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3673 if (lo && !pnfs_layout_is_valid(lo)) {
3674 calldata->arg.lr_args = NULL;
3675 calldata->res.lr_res = NULL;
3678 if (calldata->arg.fmode == 0)
3679 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3681 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3682 /* Close-to-open cache consistency revalidation */
3683 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3684 nfs4_bitmask_set(calldata->arg.bitmask_store,
3685 server->cache_consistency_bitmask,
3687 calldata->arg.bitmask = calldata->arg.bitmask_store;
3689 calldata->arg.bitmask = NULL;
3692 calldata->arg.share_access =
3693 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3694 calldata->arg.fmode, 0);
3696 if (calldata->res.fattr == NULL)
3697 calldata->arg.bitmask = NULL;
3698 else if (calldata->arg.bitmask == NULL)
3699 calldata->res.fattr = NULL;
3700 calldata->timestamp = jiffies;
3701 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3702 &calldata->arg.seq_args,
3703 &calldata->res.seq_res,
3705 nfs_release_seqid(calldata->arg.seqid);
3708 task->tk_action = NULL;
3710 nfs4_sequence_done(task, &calldata->res.seq_res);
3713 static const struct rpc_call_ops nfs4_close_ops = {
3714 .rpc_call_prepare = nfs4_close_prepare,
3715 .rpc_call_done = nfs4_close_done,
3716 .rpc_release = nfs4_free_closedata,
3720 * It is possible for data to be read/written from a mem-mapped file
3721 * after the sys_close call (which hits the vfs layer as a flush).
3722 * This means that we can't safely call nfsv4 close on a file until
3723 * the inode is cleared. This in turn means that we are not good
3724 * NFSv4 citizens - we do not indicate to the server to update the file's
3725 * share state even when we are done with one of the three share
3726 * stateid's in the inode.
3728 * NOTE: Caller must be holding the sp->so_owner semaphore!
3730 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3732 struct nfs_server *server = NFS_SERVER(state->inode);
3733 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3734 struct nfs4_closedata *calldata;
3735 struct nfs4_state_owner *sp = state->owner;
3736 struct rpc_task *task;
3737 struct rpc_message msg = {
3738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3739 .rpc_cred = state->owner->so_cred,
3741 struct rpc_task_setup task_setup_data = {
3742 .rpc_client = server->client,
3743 .rpc_message = &msg,
3744 .callback_ops = &nfs4_close_ops,
3745 .workqueue = nfsiod_workqueue,
3746 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3748 int status = -ENOMEM;
3750 if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
3751 task_setup_data.flags |= RPC_TASK_MOVEABLE;
3753 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3754 &task_setup_data.rpc_client, &msg);
3756 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3757 if (calldata == NULL)
3759 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3760 calldata->inode = state->inode;
3761 calldata->state = state;
3762 calldata->arg.fh = NFS_FH(state->inode);
3763 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3764 goto out_free_calldata;
3765 /* Serialization for the sequence id */
3766 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3767 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3768 if (IS_ERR(calldata->arg.seqid))
3769 goto out_free_calldata;
3770 nfs_fattr_init(&calldata->fattr);
3771 calldata->arg.fmode = 0;
3772 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3773 calldata->res.fattr = &calldata->fattr;
3774 calldata->res.seqid = calldata->arg.seqid;
3775 calldata->res.server = server;
3776 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3777 calldata->lr.roc = pnfs_roc(state->inode,
3778 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3779 if (calldata->lr.roc) {
3780 calldata->arg.lr_args = &calldata->lr.arg;
3781 calldata->res.lr_res = &calldata->lr.res;
3783 nfs_sb_active(calldata->inode->i_sb);
3785 msg.rpc_argp = &calldata->arg;
3786 msg.rpc_resp = &calldata->res;
3787 task_setup_data.callback_data = calldata;
3788 task = rpc_run_task(&task_setup_data);
3790 return PTR_ERR(task);
3793 status = rpc_wait_for_completion_task(task);
3799 nfs4_put_open_state(state);
3800 nfs4_put_state_owner(sp);
3804 static struct inode *
3805 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3806 int open_flags, struct iattr *attr, int *opened)
3808 struct nfs4_state *state;
3809 struct nfs4_label l, *label;
3811 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3813 /* Protect against concurrent sillydeletes */
3814 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3816 nfs4_label_release_security(label);
3819 return ERR_CAST(state);
3820 return state->inode;
3823 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3825 if (ctx->state == NULL)
3828 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3830 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3833 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3834 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3835 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3837 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3839 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3840 struct nfs4_server_caps_arg args = {
3844 struct nfs4_server_caps_res res = {};
3845 struct rpc_message msg = {
3846 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3853 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3854 FATTR4_WORD0_FH_EXPIRE_TYPE |
3855 FATTR4_WORD0_LINK_SUPPORT |
3856 FATTR4_WORD0_SYMLINK_SUPPORT |
3857 FATTR4_WORD0_ACLSUPPORT |
3858 FATTR4_WORD0_CASE_INSENSITIVE |
3859 FATTR4_WORD0_CASE_PRESERVING;
3861 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3863 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3865 /* Sanity check the server answers */
3866 switch (minorversion) {
3868 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3869 res.attr_bitmask[2] = 0;
3872 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3875 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3877 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3878 server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3879 NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3880 server->fattr_valid = NFS_ATTR_FATTR_V4;
3881 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3882 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3883 server->caps |= NFS_CAP_ACLS;
3884 if (res.has_links != 0)
3885 server->caps |= NFS_CAP_HARDLINKS;
3886 if (res.has_symlinks != 0)
3887 server->caps |= NFS_CAP_SYMLINKS;
3888 if (res.case_insensitive)
3889 server->caps |= NFS_CAP_CASE_INSENSITIVE;
3890 if (res.case_preserving)
3891 server->caps |= NFS_CAP_CASE_PRESERVING;
3892 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3893 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3894 server->caps |= NFS_CAP_SECURITY_LABEL;
3896 if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS)
3897 server->caps |= NFS_CAP_FS_LOCATIONS;
3898 if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3899 server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3900 if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3901 server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3902 if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3903 server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3904 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3905 server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3906 NFS_ATTR_FATTR_OWNER_NAME);
3907 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3908 server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3909 NFS_ATTR_FATTR_GROUP_NAME);
3910 if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3911 server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3912 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3913 server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3914 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3915 server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3916 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3917 server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
3918 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3919 sizeof(server->attr_bitmask));
3920 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3922 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3923 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3924 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3925 server->cache_consistency_bitmask[2] = 0;
3927 /* Avoid a regression due to buggy server */
3928 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3929 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3930 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3931 sizeof(server->exclcreat_bitmask));
3933 server->acl_bitmask = res.acl_bitmask;
3934 server->fh_expire_type = res.fh_expire_type;
3940 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3942 struct nfs4_exception exception = {
3943 .interruptible = true,
3947 nfs4_server_set_init_caps(server);
3949 err = nfs4_handle_exception(server,
3950 _nfs4_server_capabilities(server, fhandle),
3952 } while (exception.retry);
3956 static void test_fs_location_for_trunking(struct nfs4_fs_location *location,
3957 struct nfs_client *clp,
3958 struct nfs_server *server)
3962 for (i = 0; i < location->nservers; i++) {
3963 struct nfs4_string *srv_loc = &location->servers[i];
3964 struct sockaddr_storage addr;
3966 struct xprt_create xprt_args = {
3970 struct nfs4_add_xprt_data xprtdata = {
3973 struct rpc_add_xprt_test rpcdata = {
3974 .add_xprt_test = clp->cl_mvops->session_trunk,
3977 char *servername = NULL;
3982 addrlen = nfs_parse_server_name(srv_loc->data, srv_loc->len,
3983 &addr, sizeof(addr),
3984 clp->cl_net, server->port);
3987 xprt_args.dstaddr = (struct sockaddr *)&addr;
3988 xprt_args.addrlen = addrlen;
3989 servername = kmalloc(srv_loc->len + 1, GFP_KERNEL);
3992 memcpy(servername, srv_loc->data, srv_loc->len);
3993 servername[srv_loc->len] = '\0';
3994 xprt_args.servername = servername;
3996 xprtdata.cred = nfs4_get_clid_cred(clp);
3997 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
3998 rpc_clnt_setup_test_and_add_xprt,
4001 put_cred(xprtdata.cred);
4006 static int _nfs4_discover_trunking(struct nfs_server *server,
4007 struct nfs_fh *fhandle)
4009 struct nfs4_fs_locations *locations = NULL;
4011 const struct cred *cred;
4012 struct nfs_client *clp = server->nfs_client;
4013 const struct nfs4_state_maintenance_ops *ops =
4014 clp->cl_mvops->state_renewal_ops;
4015 int status = -ENOMEM, i;
4017 cred = ops->get_state_renewal_cred(clp);
4019 cred = nfs4_get_clid_cred(clp);
4024 page = alloc_page(GFP_KERNEL);
4027 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4030 locations->fattr = nfs_alloc_fattr();
4031 if (!locations->fattr)
4034 status = nfs4_proc_get_locations(server, fhandle, locations, page,
4039 for (i = 0; i < locations->nlocations; i++)
4040 test_fs_location_for_trunking(&locations->locations[i], clp,
4043 kfree(locations->fattr);
4053 static int nfs4_discover_trunking(struct nfs_server *server,
4054 struct nfs_fh *fhandle)
4056 struct nfs4_exception exception = {
4057 .interruptible = true,
4059 struct nfs_client *clp = server->nfs_client;
4062 if (!nfs4_has_session(clp))
4065 err = nfs4_handle_exception(server,
4066 _nfs4_discover_trunking(server, fhandle),
4068 } while (exception.retry);
4073 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4074 struct nfs_fsinfo *info)
4077 struct nfs4_lookup_root_arg args = {
4080 struct nfs4_lookup_res res = {
4082 .fattr = info->fattr,
4085 struct rpc_message msg = {
4086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
4091 bitmask[0] = nfs4_fattr_bitmap[0];
4092 bitmask[1] = nfs4_fattr_bitmap[1];
4094 * Process the label in the upcoming getfattr
4096 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
4098 nfs_fattr_init(info->fattr);
4099 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4102 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4103 struct nfs_fsinfo *info)
4105 struct nfs4_exception exception = {
4106 .interruptible = true,
4110 err = _nfs4_lookup_root(server, fhandle, info);
4111 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
4114 case -NFS4ERR_WRONGSEC:
4117 err = nfs4_handle_exception(server, err, &exception);
4119 } while (exception.retry);
4124 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4125 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4127 struct rpc_auth_create_args auth_args = {
4128 .pseudoflavor = flavor,
4130 struct rpc_auth *auth;
4132 auth = rpcauth_create(&auth_args, server->client);
4135 return nfs4_lookup_root(server, fhandle, info);
4139 * Retry pseudoroot lookup with various security flavors. We do this when:
4141 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4142 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4144 * Returns zero on success, or a negative NFS4ERR value, or a
4145 * negative errno value.
4147 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4148 struct nfs_fsinfo *info)
4150 /* Per 3530bis 15.33.5 */
4151 static const rpc_authflavor_t flav_array[] = {
4155 RPC_AUTH_UNIX, /* courtesy */
4158 int status = -EPERM;
4161 if (server->auth_info.flavor_len > 0) {
4162 /* try each flavor specified by user */
4163 for (i = 0; i < server->auth_info.flavor_len; i++) {
4164 status = nfs4_lookup_root_sec(server, fhandle, info,
4165 server->auth_info.flavors[i]);
4166 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4171 /* no flavors specified by user, try default list */
4172 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4173 status = nfs4_lookup_root_sec(server, fhandle, info,
4175 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4182 * -EACCES could mean that the user doesn't have correct permissions
4183 * to access the mount. It could also mean that we tried to mount
4184 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4185 * existing mount programs don't handle -EACCES very well so it should
4186 * be mapped to -EPERM instead.
4188 if (status == -EACCES)
4194 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4195 * @server: initialized nfs_server handle
4196 * @fhandle: we fill in the pseudo-fs root file handle
4197 * @info: we fill in an FSINFO struct
4198 * @auth_probe: probe the auth flavours
4200 * Returns zero on success, or a negative errno.
4202 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4203 struct nfs_fsinfo *info,
4209 status = nfs4_lookup_root(server, fhandle, info);
4211 if (auth_probe || status == NFS4ERR_WRONGSEC)
4212 status = server->nfs_client->cl_mvops->find_root_sec(server,
4216 status = nfs4_server_capabilities(server, fhandle);
4218 status = nfs4_do_fsinfo(server, fhandle, info);
4220 return nfs4_map_errors(status);
4223 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4224 struct nfs_fsinfo *info)
4227 struct nfs_fattr *fattr = info->fattr;
4229 error = nfs4_server_capabilities(server, mntfh);
4231 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4235 error = nfs4_proc_getattr(server, mntfh, fattr, NULL);
4237 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4241 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4242 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4243 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4250 * Get locations and (maybe) other attributes of a referral.
4251 * Note that we'll actually follow the referral later when
4252 * we detect fsid mismatch in inode revalidation
4254 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4255 const struct qstr *name, struct nfs_fattr *fattr,
4256 struct nfs_fh *fhandle)
4258 int status = -ENOMEM;
4259 struct page *page = NULL;
4260 struct nfs4_fs_locations *locations = NULL;
4262 page = alloc_page(GFP_KERNEL);
4265 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4266 if (locations == NULL)
4269 locations->fattr = fattr;
4271 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4276 * If the fsid didn't change, this is a migration event, not a
4277 * referral. Cause us to drop into the exception handler, which
4278 * will kick off migration recovery.
4280 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &fattr->fsid)) {
4281 dprintk("%s: server did not return a different fsid for"
4282 " a referral at %s\n", __func__, name->name);
4283 status = -NFS4ERR_MOVED;
4286 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4287 nfs_fixup_referral_attributes(fattr);
4288 memset(fhandle, 0, sizeof(struct nfs_fh));
4296 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4297 struct nfs_fattr *fattr, struct inode *inode)
4299 __u32 bitmask[NFS4_BITMASK_SZ];
4300 struct nfs4_getattr_arg args = {
4304 struct nfs4_getattr_res res = {
4308 struct rpc_message msg = {
4309 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4313 unsigned short task_flags = 0;
4315 if (nfs4_has_session(server->nfs_client))
4316 task_flags = RPC_TASK_MOVEABLE;
4318 /* Is this is an attribute revalidation, subject to softreval? */
4319 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4320 task_flags |= RPC_TASK_TIMEOUT;
4322 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0);
4323 nfs_fattr_init(fattr);
4324 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4325 return nfs4_do_call_sync(server->client, server, &msg,
4326 &args.seq_args, &res.seq_res, task_flags);
4329 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4330 struct nfs_fattr *fattr, struct inode *inode)
4332 struct nfs4_exception exception = {
4333 .interruptible = true,
4337 err = _nfs4_proc_getattr(server, fhandle, fattr, inode);
4338 trace_nfs4_getattr(server, fhandle, fattr, err);
4339 err = nfs4_handle_exception(server, err,
4341 } while (exception.retry);
4346 * The file is not closed if it is opened due to the a request to change
4347 * the size of the file. The open call will not be needed once the
4348 * VFS layer lookup-intents are implemented.
4350 * Close is called when the inode is destroyed.
4351 * If we haven't opened the file for O_WRONLY, we
4352 * need to in the size_change case to obtain a stateid.
4355 * Because OPEN is always done by name in nfsv4, it is
4356 * possible that we opened a different file by the same
4357 * name. We can recognize this race condition, but we
4358 * can't do anything about it besides returning an error.
4360 * This will be fixed with VFS changes (lookup-intent).
4363 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4364 struct iattr *sattr)
4366 struct inode *inode = d_inode(dentry);
4367 const struct cred *cred = NULL;
4368 struct nfs_open_context *ctx = NULL;
4371 if (pnfs_ld_layoutret_on_setattr(inode) &&
4372 sattr->ia_valid & ATTR_SIZE &&
4373 sattr->ia_size < i_size_read(inode))
4374 pnfs_commit_and_return_layout(inode);
4376 nfs_fattr_init(fattr);
4378 /* Deal with open(O_TRUNC) */
4379 if (sattr->ia_valid & ATTR_OPEN)
4380 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4382 /* Optimization: if the end result is no change, don't RPC */
4383 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4386 /* Search for an existing open(O_WRITE) file */
4387 if (sattr->ia_valid & ATTR_FILE) {
4389 ctx = nfs_file_open_context(sattr->ia_file);
4394 /* Return any delegations if we're going to change ACLs */
4395 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4396 nfs4_inode_make_writeable(inode);
4398 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL);
4400 nfs_setattr_update_inode(inode, sattr, fattr);
4401 nfs_setsecurity(inode, fattr);
4406 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4407 struct dentry *dentry, struct nfs_fh *fhandle,
4408 struct nfs_fattr *fattr)
4410 struct nfs_server *server = NFS_SERVER(dir);
4412 struct nfs4_lookup_arg args = {
4413 .bitmask = server->attr_bitmask,
4414 .dir_fh = NFS_FH(dir),
4415 .name = &dentry->d_name,
4417 struct nfs4_lookup_res res = {
4422 struct rpc_message msg = {
4423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4427 unsigned short task_flags = 0;
4429 if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
4430 task_flags = RPC_TASK_MOVEABLE;
4432 /* Is this is an attribute revalidation, subject to softreval? */
4433 if (nfs_lookup_is_soft_revalidate(dentry))
4434 task_flags |= RPC_TASK_TIMEOUT;
4436 args.bitmask = nfs4_bitmask(server, fattr->label);
4438 nfs_fattr_init(fattr);
4440 dprintk("NFS call lookup %pd2\n", dentry);
4441 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4442 status = nfs4_do_call_sync(clnt, server, &msg,
4443 &args.seq_args, &res.seq_res, task_flags);
4444 dprintk("NFS reply lookup: %d\n", status);
4448 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4450 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4451 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4452 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4456 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4457 struct dentry *dentry, struct nfs_fh *fhandle,
4458 struct nfs_fattr *fattr)
4460 struct nfs4_exception exception = {
4461 .interruptible = true,
4463 struct rpc_clnt *client = *clnt;
4464 const struct qstr *name = &dentry->d_name;
4467 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr);
4468 trace_nfs4_lookup(dir, name, err);
4470 case -NFS4ERR_BADNAME:
4473 case -NFS4ERR_MOVED:
4474 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4475 if (err == -NFS4ERR_MOVED)
4476 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4478 case -NFS4ERR_WRONGSEC:
4480 if (client != *clnt)
4482 client = nfs4_negotiate_security(client, dir, name);
4484 return PTR_ERR(client);
4486 exception.retry = 1;
4489 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4491 } while (exception.retry);
4496 else if (client != *clnt)
4497 rpc_shutdown_client(client);
4502 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4503 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4506 struct rpc_clnt *client = NFS_CLIENT(dir);
4508 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4509 if (client != NFS_CLIENT(dir)) {
4510 rpc_shutdown_client(client);
4511 nfs_fixup_secinfo_attributes(fattr);
4517 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4518 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4520 struct rpc_clnt *client = NFS_CLIENT(dir);
4523 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4525 return ERR_PTR(status);
4526 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4529 static int _nfs4_proc_lookupp(struct inode *inode,
4530 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4532 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4533 struct nfs_server *server = NFS_SERVER(inode);
4535 struct nfs4_lookupp_arg args = {
4536 .bitmask = server->attr_bitmask,
4537 .fh = NFS_FH(inode),
4539 struct nfs4_lookupp_res res = {
4544 struct rpc_message msg = {
4545 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4549 unsigned short task_flags = 0;
4551 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4552 task_flags |= RPC_TASK_TIMEOUT;
4554 args.bitmask = nfs4_bitmask(server, fattr->label);
4556 nfs_fattr_init(fattr);
4558 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4559 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4560 &res.seq_res, task_flags);
4561 dprintk("NFS reply lookupp: %d\n", status);
4565 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4566 struct nfs_fattr *fattr)
4568 struct nfs4_exception exception = {
4569 .interruptible = true,
4573 err = _nfs4_proc_lookupp(inode, fhandle, fattr);
4574 trace_nfs4_lookupp(inode, err);
4575 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4577 } while (exception.retry);
4581 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4582 const struct cred *cred)
4584 struct nfs_server *server = NFS_SERVER(inode);
4585 struct nfs4_accessargs args = {
4586 .fh = NFS_FH(inode),
4587 .access = entry->mask,
4589 struct nfs4_accessres res = {
4592 struct rpc_message msg = {
4593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4600 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4601 res.fattr = nfs_alloc_fattr();
4602 if (res.fattr == NULL)
4604 args.bitmask = server->cache_consistency_bitmask;
4606 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4608 nfs_access_set_mask(entry, res.access);
4610 nfs_refresh_inode(inode, res.fattr);
4612 nfs_free_fattr(res.fattr);
4616 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4617 const struct cred *cred)
4619 struct nfs4_exception exception = {
4620 .interruptible = true,
4624 err = _nfs4_proc_access(inode, entry, cred);
4625 trace_nfs4_access(inode, err);
4626 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4628 } while (exception.retry);
4633 * TODO: For the time being, we don't try to get any attributes
4634 * along with any of the zero-copy operations READ, READDIR,
4637 * In the case of the first three, we want to put the GETATTR
4638 * after the read-type operation -- this is because it is hard
4639 * to predict the length of a GETATTR response in v4, and thus
4640 * align the READ data correctly. This means that the GETATTR
4641 * may end up partially falling into the page cache, and we should
4642 * shift it into the 'tail' of the xdr_buf before processing.
4643 * To do this efficiently, we need to know the total length
4644 * of data received, which doesn't seem to be available outside
4647 * In the case of WRITE, we also want to put the GETATTR after
4648 * the operation -- in this case because we want to make sure
4649 * we get the post-operation mtime and size.
4651 * Both of these changes to the XDR layer would in fact be quite
4652 * minor, but I decided to leave them for a subsequent patch.
4654 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4655 unsigned int pgbase, unsigned int pglen)
4657 struct nfs4_readlink args = {
4658 .fh = NFS_FH(inode),
4663 struct nfs4_readlink_res res;
4664 struct rpc_message msg = {
4665 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4670 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4673 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4674 unsigned int pgbase, unsigned int pglen)
4676 struct nfs4_exception exception = {
4677 .interruptible = true,
4681 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4682 trace_nfs4_readlink(inode, err);
4683 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4685 } while (exception.retry);
4690 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4693 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4696 struct nfs_server *server = NFS_SERVER(dir);
4697 struct nfs4_label l, *ilabel;
4698 struct nfs_open_context *ctx;
4699 struct nfs4_state *state;
4702 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4704 return PTR_ERR(ctx);
4706 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4708 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4709 sattr->ia_mode &= ~current_umask();
4710 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4711 if (IS_ERR(state)) {
4712 status = PTR_ERR(state);
4716 nfs4_label_release_security(ilabel);
4717 put_nfs_open_context(ctx);
4722 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4724 struct nfs_server *server = NFS_SERVER(dir);
4725 struct nfs_removeargs args = {
4729 struct nfs_removeres res = {
4732 struct rpc_message msg = {
4733 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4737 unsigned long timestamp = jiffies;
4740 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4742 spin_lock(&dir->i_lock);
4743 /* Removing a directory decrements nlink in the parent */
4744 if (ftype == NF4DIR && dir->i_nlink > 2)
4745 nfs4_dec_nlink_locked(dir);
4746 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4747 NFS_INO_INVALID_DATA);
4748 spin_unlock(&dir->i_lock);
4753 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4755 struct nfs4_exception exception = {
4756 .interruptible = true,
4758 struct inode *inode = d_inode(dentry);
4762 if (inode->i_nlink == 1)
4763 nfs4_inode_return_delegation(inode);
4765 nfs4_inode_make_writeable(inode);
4768 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4769 trace_nfs4_remove(dir, &dentry->d_name, err);
4770 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4772 } while (exception.retry);
4776 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4778 struct nfs4_exception exception = {
4779 .interruptible = true,
4784 err = _nfs4_proc_remove(dir, name, NF4DIR);
4785 trace_nfs4_remove(dir, name, err);
4786 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4788 } while (exception.retry);
4792 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4793 struct dentry *dentry,
4794 struct inode *inode)
4796 struct nfs_removeargs *args = msg->rpc_argp;
4797 struct nfs_removeres *res = msg->rpc_resp;
4799 res->server = NFS_SB(dentry->d_sb);
4800 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4801 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4803 nfs_fattr_init(res->dir_attr);
4806 nfs4_inode_return_delegation(inode);
4807 nfs_d_prune_case_insensitive_aliases(inode);
4811 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4813 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4814 &data->args.seq_args,
4819 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4821 struct nfs_unlinkdata *data = task->tk_calldata;
4822 struct nfs_removeres *res = &data->res;
4824 if (!nfs4_sequence_done(task, &res->seq_res))
4826 if (nfs4_async_handle_error(task, res->server, NULL,
4827 &data->timeout) == -EAGAIN)
4829 if (task->tk_status == 0)
4830 nfs4_update_changeattr(dir, &res->cinfo,
4831 res->dir_attr->time_start,
4832 NFS_INO_INVALID_DATA);
4836 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4837 struct dentry *old_dentry,
4838 struct dentry *new_dentry)
4840 struct nfs_renameargs *arg = msg->rpc_argp;
4841 struct nfs_renameres *res = msg->rpc_resp;
4842 struct inode *old_inode = d_inode(old_dentry);
4843 struct inode *new_inode = d_inode(new_dentry);
4846 nfs4_inode_make_writeable(old_inode);
4848 nfs4_inode_return_delegation(new_inode);
4849 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4850 res->server = NFS_SB(old_dentry->d_sb);
4851 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4854 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4856 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4857 &data->args.seq_args,
4862 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4863 struct inode *new_dir)
4865 struct nfs_renamedata *data = task->tk_calldata;
4866 struct nfs_renameres *res = &data->res;
4868 if (!nfs4_sequence_done(task, &res->seq_res))
4870 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4873 if (task->tk_status == 0) {
4874 nfs_d_prune_case_insensitive_aliases(d_inode(data->old_dentry));
4875 if (new_dir != old_dir) {
4876 /* Note: If we moved a directory, nlink will change */
4877 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4878 res->old_fattr->time_start,
4879 NFS_INO_INVALID_NLINK |
4880 NFS_INO_INVALID_DATA);
4881 nfs4_update_changeattr(new_dir, &res->new_cinfo,
4882 res->new_fattr->time_start,
4883 NFS_INO_INVALID_NLINK |
4884 NFS_INO_INVALID_DATA);
4886 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4887 res->old_fattr->time_start,
4888 NFS_INO_INVALID_DATA);
4893 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4895 struct nfs_server *server = NFS_SERVER(inode);
4896 __u32 bitmask[NFS4_BITMASK_SZ];
4897 struct nfs4_link_arg arg = {
4898 .fh = NFS_FH(inode),
4899 .dir_fh = NFS_FH(dir),
4903 struct nfs4_link_res res = {
4906 struct rpc_message msg = {
4907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4911 int status = -ENOMEM;
4913 res.fattr = nfs_alloc_fattr_with_label(server);
4914 if (res.fattr == NULL)
4917 nfs4_inode_make_writeable(inode);
4918 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label), inode,
4919 NFS_INO_INVALID_CHANGE);
4920 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4922 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4923 NFS_INO_INVALID_DATA);
4924 nfs4_inc_nlink(inode);
4925 status = nfs_post_op_update_inode(inode, res.fattr);
4927 nfs_setsecurity(inode, res.fattr);
4931 nfs_free_fattr(res.fattr);
4935 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4937 struct nfs4_exception exception = {
4938 .interruptible = true,
4942 err = nfs4_handle_exception(NFS_SERVER(inode),
4943 _nfs4_proc_link(inode, dir, name),
4945 } while (exception.retry);
4949 struct nfs4_createdata {
4950 struct rpc_message msg;
4951 struct nfs4_create_arg arg;
4952 struct nfs4_create_res res;
4954 struct nfs_fattr fattr;
4957 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4958 const struct qstr *name, struct iattr *sattr, u32 ftype)
4960 struct nfs4_createdata *data;
4962 data = kzalloc(sizeof(*data), GFP_KERNEL);
4964 struct nfs_server *server = NFS_SERVER(dir);
4966 data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL);
4967 if (IS_ERR(data->fattr.label))
4970 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4971 data->msg.rpc_argp = &data->arg;
4972 data->msg.rpc_resp = &data->res;
4973 data->arg.dir_fh = NFS_FH(dir);
4974 data->arg.server = server;
4975 data->arg.name = name;
4976 data->arg.attrs = sattr;
4977 data->arg.ftype = ftype;
4978 data->arg.bitmask = nfs4_bitmask(server, data->fattr.label);
4979 data->arg.umask = current_umask();
4980 data->res.server = server;
4981 data->res.fh = &data->fh;
4982 data->res.fattr = &data->fattr;
4983 nfs_fattr_init(data->res.fattr);
4991 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4993 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4994 &data->arg.seq_args, &data->res.seq_res, 1);
4996 spin_lock(&dir->i_lock);
4997 /* Creating a directory bumps nlink in the parent */
4998 if (data->arg.ftype == NF4DIR)
4999 nfs4_inc_nlink_locked(dir);
5000 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
5001 data->res.fattr->time_start,
5002 NFS_INO_INVALID_DATA);
5003 spin_unlock(&dir->i_lock);
5004 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
5009 static void nfs4_free_createdata(struct nfs4_createdata *data)
5011 nfs4_label_free(data->fattr.label);
5015 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5016 struct page *page, unsigned int len, struct iattr *sattr,
5017 struct nfs4_label *label)
5019 struct nfs4_createdata *data;
5020 int status = -ENAMETOOLONG;
5022 if (len > NFS4_MAXPATHLEN)
5026 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
5030 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
5031 data->arg.u.symlink.pages = &page;
5032 data->arg.u.symlink.len = len;
5033 data->arg.label = label;
5035 status = nfs4_do_create(dir, dentry, data);
5037 nfs4_free_createdata(data);
5042 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5043 struct page *page, unsigned int len, struct iattr *sattr)
5045 struct nfs4_exception exception = {
5046 .interruptible = true,
5048 struct nfs4_label l, *label;
5051 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5054 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
5055 trace_nfs4_symlink(dir, &dentry->d_name, err);
5056 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5058 } while (exception.retry);
5060 nfs4_label_release_security(label);
5064 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5065 struct iattr *sattr, struct nfs4_label *label)
5067 struct nfs4_createdata *data;
5068 int status = -ENOMEM;
5070 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
5074 data->arg.label = label;
5075 status = nfs4_do_create(dir, dentry, data);
5077 nfs4_free_createdata(data);
5082 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5083 struct iattr *sattr)
5085 struct nfs_server *server = NFS_SERVER(dir);
5086 struct nfs4_exception exception = {
5087 .interruptible = true,
5089 struct nfs4_label l, *label;
5092 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5094 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5095 sattr->ia_mode &= ~current_umask();
5097 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
5098 trace_nfs4_mkdir(dir, &dentry->d_name, err);
5099 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5101 } while (exception.retry);
5102 nfs4_label_release_security(label);
5107 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5108 struct nfs_readdir_res *nr_res)
5110 struct inode *dir = d_inode(nr_arg->dentry);
5111 struct nfs_server *server = NFS_SERVER(dir);
5112 struct nfs4_readdir_arg args = {
5114 .pages = nr_arg->pages,
5116 .count = nr_arg->page_len,
5117 .plus = nr_arg->plus,
5119 struct nfs4_readdir_res res;
5120 struct rpc_message msg = {
5121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5124 .rpc_cred = nr_arg->cred,
5128 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5129 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5130 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5131 args.bitmask = server->attr_bitmask_nl;
5133 args.bitmask = server->attr_bitmask;
5135 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5136 res.pgbase = args.pgbase;
5137 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5140 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5141 status += args.pgbase;
5144 nfs_invalidate_atime(dir);
5146 dprintk("%s: returns %d\n", __func__, status);
5150 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5151 struct nfs_readdir_res *res)
5153 struct nfs4_exception exception = {
5154 .interruptible = true,
5158 err = _nfs4_proc_readdir(arg, res);
5159 trace_nfs4_readdir(d_inode(arg->dentry), err);
5160 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5162 } while (exception.retry);
5166 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5167 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5169 struct nfs4_createdata *data;
5170 int mode = sattr->ia_mode;
5171 int status = -ENOMEM;
5173 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5178 data->arg.ftype = NF4FIFO;
5179 else if (S_ISBLK(mode)) {
5180 data->arg.ftype = NF4BLK;
5181 data->arg.u.device.specdata1 = MAJOR(rdev);
5182 data->arg.u.device.specdata2 = MINOR(rdev);
5184 else if (S_ISCHR(mode)) {
5185 data->arg.ftype = NF4CHR;
5186 data->arg.u.device.specdata1 = MAJOR(rdev);
5187 data->arg.u.device.specdata2 = MINOR(rdev);
5188 } else if (!S_ISSOCK(mode)) {
5193 data->arg.label = label;
5194 status = nfs4_do_create(dir, dentry, data);
5196 nfs4_free_createdata(data);
5201 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5202 struct iattr *sattr, dev_t rdev)
5204 struct nfs_server *server = NFS_SERVER(dir);
5205 struct nfs4_exception exception = {
5206 .interruptible = true,
5208 struct nfs4_label l, *label;
5211 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5213 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5214 sattr->ia_mode &= ~current_umask();
5216 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5217 trace_nfs4_mknod(dir, &dentry->d_name, err);
5218 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5220 } while (exception.retry);
5222 nfs4_label_release_security(label);
5227 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5228 struct nfs_fsstat *fsstat)
5230 struct nfs4_statfs_arg args = {
5232 .bitmask = server->attr_bitmask,
5234 struct nfs4_statfs_res res = {
5237 struct rpc_message msg = {
5238 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5243 nfs_fattr_init(fsstat->fattr);
5244 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5247 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5249 struct nfs4_exception exception = {
5250 .interruptible = true,
5254 err = nfs4_handle_exception(server,
5255 _nfs4_proc_statfs(server, fhandle, fsstat),
5257 } while (exception.retry);
5261 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5262 struct nfs_fsinfo *fsinfo)
5264 struct nfs4_fsinfo_arg args = {
5266 .bitmask = server->attr_bitmask,
5268 struct nfs4_fsinfo_res res = {
5271 struct rpc_message msg = {
5272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5277 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5280 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5282 struct nfs4_exception exception = {
5283 .interruptible = true,
5288 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5289 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5291 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5294 err = nfs4_handle_exception(server, err, &exception);
5295 } while (exception.retry);
5299 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5303 nfs_fattr_init(fsinfo->fattr);
5304 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5306 /* block layout checks this! */
5307 server->pnfs_blksize = fsinfo->blksize;
5308 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5314 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5315 struct nfs_pathconf *pathconf)
5317 struct nfs4_pathconf_arg args = {
5319 .bitmask = server->attr_bitmask,
5321 struct nfs4_pathconf_res res = {
5322 .pathconf = pathconf,
5324 struct rpc_message msg = {
5325 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5330 /* None of the pathconf attributes are mandatory to implement */
5331 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5332 memset(pathconf, 0, sizeof(*pathconf));
5336 nfs_fattr_init(pathconf->fattr);
5337 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5340 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5341 struct nfs_pathconf *pathconf)
5343 struct nfs4_exception exception = {
5344 .interruptible = true,
5349 err = nfs4_handle_exception(server,
5350 _nfs4_proc_pathconf(server, fhandle, pathconf),
5352 } while (exception.retry);
5356 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5357 const struct nfs_open_context *ctx,
5358 const struct nfs_lock_context *l_ctx,
5361 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5363 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5365 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5366 const struct nfs_open_context *ctx,
5367 const struct nfs_lock_context *l_ctx,
5370 nfs4_stateid _current_stateid;
5372 /* If the current stateid represents a lost lock, then exit */
5373 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5375 return nfs4_stateid_match(stateid, &_current_stateid);
5378 static bool nfs4_error_stateid_expired(int err)
5381 case -NFS4ERR_DELEG_REVOKED:
5382 case -NFS4ERR_ADMIN_REVOKED:
5383 case -NFS4ERR_BAD_STATEID:
5384 case -NFS4ERR_STALE_STATEID:
5385 case -NFS4ERR_OLD_STATEID:
5386 case -NFS4ERR_OPENMODE:
5387 case -NFS4ERR_EXPIRED:
5393 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5395 struct nfs_server *server = NFS_SERVER(hdr->inode);
5397 trace_nfs4_read(hdr, task->tk_status);
5398 if (task->tk_status < 0) {
5399 struct nfs4_exception exception = {
5400 .inode = hdr->inode,
5401 .state = hdr->args.context->state,
5402 .stateid = &hdr->args.stateid,
5404 task->tk_status = nfs4_async_handle_exception(task,
5405 server, task->tk_status, &exception);
5406 if (exception.retry) {
5407 rpc_restart_call_prepare(task);
5412 if (task->tk_status > 0)
5413 renew_lease(server, hdr->timestamp);
5417 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5418 struct nfs_pgio_args *args)
5421 if (!nfs4_error_stateid_expired(task->tk_status) ||
5422 nfs4_stateid_is_current(&args->stateid,
5427 rpc_restart_call_prepare(task);
5431 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5432 struct nfs_pgio_header *hdr)
5434 struct nfs_server *server = NFS_SERVER(hdr->inode);
5435 struct rpc_message *msg = &task->tk_msg;
5437 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5438 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5439 server->caps &= ~NFS_CAP_READ_PLUS;
5440 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5441 rpc_restart_call_prepare(task);
5447 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5449 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5451 if (nfs4_read_stateid_changed(task, &hdr->args))
5453 if (nfs4_read_plus_not_supported(task, hdr))
5455 if (task->tk_status > 0)
5456 nfs_invalidate_atime(hdr->inode);
5457 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5458 nfs4_read_done_cb(task, hdr);
5461 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5462 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5463 struct rpc_message *msg)
5465 /* Note: We don't use READ_PLUS with pNFS yet */
5466 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp) {
5467 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5468 return nfs_read_alloc_scratch(hdr, READ_PLUS_SCRATCH_SIZE);
5473 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5474 struct rpc_message *msg)
5478 #endif /* CONFIG_NFS_V4_2 */
5480 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5481 struct rpc_message *msg)
5483 hdr->timestamp = jiffies;
5484 if (!hdr->pgio_done_cb)
5485 hdr->pgio_done_cb = nfs4_read_done_cb;
5486 if (!nfs42_read_plus_support(hdr, msg))
5487 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5488 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5491 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5492 struct nfs_pgio_header *hdr)
5494 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5495 &hdr->args.seq_args,
5499 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5500 hdr->args.lock_context,
5501 hdr->rw_mode) == -EIO)
5503 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5508 static int nfs4_write_done_cb(struct rpc_task *task,
5509 struct nfs_pgio_header *hdr)
5511 struct inode *inode = hdr->inode;
5513 trace_nfs4_write(hdr, task->tk_status);
5514 if (task->tk_status < 0) {
5515 struct nfs4_exception exception = {
5516 .inode = hdr->inode,
5517 .state = hdr->args.context->state,
5518 .stateid = &hdr->args.stateid,
5520 task->tk_status = nfs4_async_handle_exception(task,
5521 NFS_SERVER(inode), task->tk_status,
5523 if (exception.retry) {
5524 rpc_restart_call_prepare(task);
5528 if (task->tk_status >= 0) {
5529 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5530 nfs_writeback_update_inode(hdr);
5535 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5536 struct nfs_pgio_args *args)
5539 if (!nfs4_error_stateid_expired(task->tk_status) ||
5540 nfs4_stateid_is_current(&args->stateid,
5545 rpc_restart_call_prepare(task);
5549 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5551 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5553 if (nfs4_write_stateid_changed(task, &hdr->args))
5555 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5556 nfs4_write_done_cb(task, hdr);
5560 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5562 /* Don't request attributes for pNFS or O_DIRECT writes */
5563 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5565 /* Otherwise, request attributes if and only if we don't hold
5568 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5571 void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[],
5572 struct inode *inode, unsigned long cache_validity)
5574 struct nfs_server *server = NFS_SERVER(inode);
5577 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5578 cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity);
5580 if (cache_validity & NFS_INO_INVALID_CHANGE)
5581 bitmask[0] |= FATTR4_WORD0_CHANGE;
5582 if (cache_validity & NFS_INO_INVALID_ATIME)
5583 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5584 if (cache_validity & NFS_INO_INVALID_MODE)
5585 bitmask[1] |= FATTR4_WORD1_MODE;
5586 if (cache_validity & NFS_INO_INVALID_OTHER)
5587 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5588 if (cache_validity & NFS_INO_INVALID_NLINK)
5589 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5590 if (cache_validity & NFS_INO_INVALID_CTIME)
5591 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5592 if (cache_validity & NFS_INO_INVALID_MTIME)
5593 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5594 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5595 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5597 if (cache_validity & NFS_INO_INVALID_SIZE)
5598 bitmask[0] |= FATTR4_WORD0_SIZE;
5600 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5601 bitmask[i] &= server->attr_bitmask[i];
5604 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5605 struct rpc_message *msg,
5606 struct rpc_clnt **clnt)
5608 struct nfs_server *server = NFS_SERVER(hdr->inode);
5610 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5611 hdr->args.bitmask = NULL;
5612 hdr->res.fattr = NULL;
5614 nfs4_bitmask_set(hdr->args.bitmask_store,
5615 server->cache_consistency_bitmask,
5616 hdr->inode, NFS_INO_INVALID_BLOCKS);
5617 hdr->args.bitmask = hdr->args.bitmask_store;
5620 if (!hdr->pgio_done_cb)
5621 hdr->pgio_done_cb = nfs4_write_done_cb;
5622 hdr->res.server = server;
5623 hdr->timestamp = jiffies;
5625 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5626 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5627 nfs4_state_protect_write(hdr->ds_clp ? hdr->ds_clp : server->nfs_client, clnt, msg, hdr);
5630 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5632 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5633 &data->args.seq_args,
5638 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5640 struct inode *inode = data->inode;
5642 trace_nfs4_commit(data, task->tk_status);
5643 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5644 NULL, NULL) == -EAGAIN) {
5645 rpc_restart_call_prepare(task);
5651 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5653 if (!nfs4_sequence_done(task, &data->res.seq_res))
5655 return data->commit_done_cb(task, data);
5658 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5659 struct rpc_clnt **clnt)
5661 struct nfs_server *server = NFS_SERVER(data->inode);
5663 if (data->commit_done_cb == NULL)
5664 data->commit_done_cb = nfs4_commit_done_cb;
5665 data->res.server = server;
5666 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5667 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5668 nfs4_state_protect(data->ds_clp ? data->ds_clp : server->nfs_client,
5669 NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5672 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5673 struct nfs_commitres *res)
5675 struct inode *dst_inode = file_inode(dst);
5676 struct nfs_server *server = NFS_SERVER(dst_inode);
5677 struct rpc_message msg = {
5678 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5683 args->fh = NFS_FH(dst_inode);
5684 return nfs4_call_sync(server->client, server, &msg,
5685 &args->seq_args, &res->seq_res, 1);
5688 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5690 struct nfs_commitargs args = {
5694 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5695 struct nfs4_exception exception = { };
5699 status = _nfs4_proc_commit(dst, &args, res);
5700 status = nfs4_handle_exception(dst_server, status, &exception);
5701 } while (exception.retry);
5706 struct nfs4_renewdata {
5707 struct nfs_client *client;
5708 unsigned long timestamp;
5712 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5713 * standalone procedure for queueing an asynchronous RENEW.
5715 static void nfs4_renew_release(void *calldata)
5717 struct nfs4_renewdata *data = calldata;
5718 struct nfs_client *clp = data->client;
5720 if (refcount_read(&clp->cl_count) > 1)
5721 nfs4_schedule_state_renewal(clp);
5722 nfs_put_client(clp);
5726 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5728 struct nfs4_renewdata *data = calldata;
5729 struct nfs_client *clp = data->client;
5730 unsigned long timestamp = data->timestamp;
5732 trace_nfs4_renew_async(clp, task->tk_status);
5733 switch (task->tk_status) {
5736 case -NFS4ERR_LEASE_MOVED:
5737 nfs4_schedule_lease_moved_recovery(clp);
5740 /* Unless we're shutting down, schedule state recovery! */
5741 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5743 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5744 nfs4_schedule_lease_recovery(clp);
5747 nfs4_schedule_path_down_recovery(clp);
5749 do_renew_lease(clp, timestamp);
5752 static const struct rpc_call_ops nfs4_renew_ops = {
5753 .rpc_call_done = nfs4_renew_done,
5754 .rpc_release = nfs4_renew_release,
5757 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5759 struct rpc_message msg = {
5760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5764 struct nfs4_renewdata *data;
5766 if (renew_flags == 0)
5768 if (!refcount_inc_not_zero(&clp->cl_count))
5770 data = kmalloc(sizeof(*data), GFP_NOFS);
5772 nfs_put_client(clp);
5776 data->timestamp = jiffies;
5777 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5778 &nfs4_renew_ops, data);
5781 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5783 struct rpc_message msg = {
5784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5788 unsigned long now = jiffies;
5791 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5794 do_renew_lease(clp, now);
5798 static bool nfs4_server_supports_acls(const struct nfs_server *server,
5799 enum nfs4_acl_type type)
5803 return server->attr_bitmask[0] & FATTR4_WORD0_ACL;
5805 return server->attr_bitmask[1] & FATTR4_WORD1_DACL;
5807 return server->attr_bitmask[1] & FATTR4_WORD1_SACL;
5811 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5812 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5815 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5817 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5818 struct page **pages)
5820 struct page *newpage, **spages;
5826 len = min_t(size_t, PAGE_SIZE, buflen);
5827 newpage = alloc_page(GFP_KERNEL);
5829 if (newpage == NULL)
5831 memcpy(page_address(newpage), buf, len);
5836 } while (buflen != 0);
5842 __free_page(spages[rc-1]);
5846 struct nfs4_cached_acl {
5847 enum nfs4_acl_type type;
5853 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5855 struct nfs_inode *nfsi = NFS_I(inode);
5857 spin_lock(&inode->i_lock);
5858 kfree(nfsi->nfs4_acl);
5859 nfsi->nfs4_acl = acl;
5860 spin_unlock(&inode->i_lock);
5863 static void nfs4_zap_acl_attr(struct inode *inode)
5865 nfs4_set_cached_acl(inode, NULL);
5868 static ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf,
5869 size_t buflen, enum nfs4_acl_type type)
5871 struct nfs_inode *nfsi = NFS_I(inode);
5872 struct nfs4_cached_acl *acl;
5875 spin_lock(&inode->i_lock);
5876 acl = nfsi->nfs4_acl;
5879 if (acl->type != type)
5881 if (buf == NULL) /* user is just asking for length */
5883 if (acl->cached == 0)
5885 ret = -ERANGE; /* see getxattr(2) man page */
5886 if (acl->len > buflen)
5888 memcpy(buf, acl->data, acl->len);
5892 spin_unlock(&inode->i_lock);
5896 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages,
5897 size_t pgbase, size_t acl_len,
5898 enum nfs4_acl_type type)
5900 struct nfs4_cached_acl *acl;
5901 size_t buflen = sizeof(*acl) + acl_len;
5903 if (buflen <= PAGE_SIZE) {
5904 acl = kmalloc(buflen, GFP_KERNEL);
5908 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5910 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5918 nfs4_set_cached_acl(inode, acl);
5922 * The getxattr API returns the required buffer length when called with a
5923 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5924 * the required buf. On a NULL buf, we send a page of data to the server
5925 * guessing that the ACL request can be serviced by a page. If so, we cache
5926 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5927 * the cache. If not so, we throw away the page, and cache the required
5928 * length. The next getxattr call will then produce another round trip to
5929 * the server, this time with the input buf of the required size.
5931 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf,
5932 size_t buflen, enum nfs4_acl_type type)
5934 struct page **pages;
5935 struct nfs_getaclargs args = {
5936 .fh = NFS_FH(inode),
5940 struct nfs_getaclres res = {
5944 struct rpc_message msg = {
5945 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5949 unsigned int npages;
5950 int ret = -ENOMEM, i;
5951 struct nfs_server *server = NFS_SERVER(inode);
5954 buflen = server->rsize;
5956 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5957 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
5961 args.acl_pages = pages;
5963 for (i = 0; i < npages; i++) {
5964 pages[i] = alloc_page(GFP_KERNEL);
5969 /* for decoding across pages */
5970 res.acl_scratch = alloc_page(GFP_KERNEL);
5971 if (!res.acl_scratch)
5974 args.acl_len = npages * PAGE_SIZE;
5976 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5977 __func__, buf, buflen, npages, args.acl_len);
5978 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5979 &msg, &args.seq_args, &res.seq_res, 0);
5983 /* Handle the case where the passed-in buffer is too short */
5984 if (res.acl_flags & NFS4_ACL_TRUNC) {
5985 /* Did the user only issue a request for the acl length? */
5991 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len,
5994 if (res.acl_len > buflen) {
5998 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
6004 __free_page(pages[i]);
6005 if (res.acl_scratch)
6006 __free_page(res.acl_scratch);
6011 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf,
6012 size_t buflen, enum nfs4_acl_type type)
6014 struct nfs4_exception exception = {
6015 .interruptible = true,
6019 ret = __nfs4_get_acl_uncached(inode, buf, buflen, type);
6020 trace_nfs4_get_acl(inode, ret);
6023 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
6024 } while (exception.retry);
6028 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen,
6029 enum nfs4_acl_type type)
6031 struct nfs_server *server = NFS_SERVER(inode);
6034 if (!nfs4_server_supports_acls(server, type))
6036 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
6039 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
6040 nfs_zap_acl_cache(inode);
6041 ret = nfs4_read_cached_acl(inode, buf, buflen, type);
6043 /* -ENOENT is returned if there is no ACL or if there is an ACL
6044 * but no cached acl data, just the acl length */
6046 return nfs4_get_acl_uncached(inode, buf, buflen, type);
6049 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf,
6050 size_t buflen, enum nfs4_acl_type type)
6052 struct nfs_server *server = NFS_SERVER(inode);
6053 struct page *pages[NFS4ACL_MAXPAGES];
6054 struct nfs_setaclargs arg = {
6055 .fh = NFS_FH(inode),
6060 struct nfs_setaclres res;
6061 struct rpc_message msg = {
6062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
6066 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
6069 /* You can't remove system.nfs4_acl: */
6072 if (!nfs4_server_supports_acls(server, type))
6074 if (npages > ARRAY_SIZE(pages))
6076 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
6079 nfs4_inode_make_writeable(inode);
6080 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6083 * Free each page after tx, so the only ref left is
6084 * held by the network stack
6087 put_page(pages[i-1]);
6090 * Acl update can result in inode attribute update.
6091 * so mark the attribute cache invalid.
6093 spin_lock(&inode->i_lock);
6094 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
6095 NFS_INO_INVALID_CTIME |
6096 NFS_INO_REVAL_FORCED);
6097 spin_unlock(&inode->i_lock);
6098 nfs_access_zap_cache(inode);
6099 nfs_zap_acl_cache(inode);
6103 static int nfs4_proc_set_acl(struct inode *inode, const void *buf,
6104 size_t buflen, enum nfs4_acl_type type)
6106 struct nfs4_exception exception = { };
6109 err = __nfs4_proc_set_acl(inode, buf, buflen, type);
6110 trace_nfs4_set_acl(inode, err);
6111 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
6113 * no need to retry since the kernel
6114 * isn't involved in encoding the ACEs.
6119 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6121 } while (exception.retry);
6125 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6126 static int _nfs4_get_security_label(struct inode *inode, void *buf,
6129 struct nfs_server *server = NFS_SERVER(inode);
6130 struct nfs4_label label = {0, 0, buflen, buf};
6132 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6133 struct nfs_fattr fattr = {
6136 struct nfs4_getattr_arg arg = {
6137 .fh = NFS_FH(inode),
6140 struct nfs4_getattr_res res = {
6144 struct rpc_message msg = {
6145 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6151 nfs_fattr_init(&fattr);
6153 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6156 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6161 static int nfs4_get_security_label(struct inode *inode, void *buf,
6164 struct nfs4_exception exception = {
6165 .interruptible = true,
6169 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6173 err = _nfs4_get_security_label(inode, buf, buflen);
6174 trace_nfs4_get_security_label(inode, err);
6175 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6177 } while (exception.retry);
6181 static int _nfs4_do_set_security_label(struct inode *inode,
6182 struct nfs4_label *ilabel,
6183 struct nfs_fattr *fattr)
6186 struct iattr sattr = {0};
6187 struct nfs_server *server = NFS_SERVER(inode);
6188 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6189 struct nfs_setattrargs arg = {
6190 .fh = NFS_FH(inode),
6196 struct nfs_setattrres res = {
6200 struct rpc_message msg = {
6201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6207 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6209 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6211 dprintk("%s failed: %d\n", __func__, status);
6216 static int nfs4_do_set_security_label(struct inode *inode,
6217 struct nfs4_label *ilabel,
6218 struct nfs_fattr *fattr)
6220 struct nfs4_exception exception = { };
6224 err = _nfs4_do_set_security_label(inode, ilabel, fattr);
6225 trace_nfs4_set_security_label(inode, err);
6226 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6228 } while (exception.retry);
6233 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6235 struct nfs4_label ilabel = {0, 0, buflen, (char *)buf };
6236 struct nfs_fattr *fattr;
6239 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6242 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
6246 status = nfs4_do_set_security_label(inode, &ilabel, fattr);
6248 nfs_setsecurity(inode, fattr);
6252 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6255 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6256 nfs4_verifier *bootverf)
6260 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6261 /* An impossible timestamp guarantees this value
6262 * will never match a generated boot time. */
6263 verf[0] = cpu_to_be32(U32_MAX);
6264 verf[1] = cpu_to_be32(U32_MAX);
6266 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6267 u64 ns = ktime_to_ns(nn->boot_time);
6269 verf[0] = cpu_to_be32(ns >> 32);
6270 verf[1] = cpu_to_be32(ns);
6272 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6276 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6278 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6279 struct nfs_netns_client *nn_clp = nn->nfs_client;
6286 id = rcu_dereference(nn_clp->identifier);
6288 strscpy(buf, id, buflen);
6292 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6293 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6299 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6301 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6306 if (clp->cl_owner_id != NULL)
6311 strlen(clp->cl_rpcclient->cl_nodename) +
6313 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6317 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6321 if (len > NFS4_OPAQUE_LIMIT + 1)
6325 * Since this string is allocated at mount time, and held until the
6326 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6327 * about a memory-reclaim deadlock.
6329 str = kmalloc(len, GFP_KERNEL);
6335 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6336 clp->cl_rpcclient->cl_nodename, buf,
6337 rpc_peeraddr2str(clp->cl_rpcclient,
6340 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6341 clp->cl_rpcclient->cl_nodename,
6342 rpc_peeraddr2str(clp->cl_rpcclient,
6346 clp->cl_owner_id = str;
6351 nfs4_init_uniform_client_string(struct nfs_client *clp)
6353 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6358 if (clp->cl_owner_id != NULL)
6361 len = 10 + 10 + 1 + 10 + 1 +
6362 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6364 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6368 if (len > NFS4_OPAQUE_LIMIT + 1)
6372 * Since this string is allocated at mount time, and held until the
6373 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6374 * about a memory-reclaim deadlock.
6376 str = kmalloc(len, GFP_KERNEL);
6381 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6382 clp->rpc_ops->version, clp->cl_minorversion,
6383 buf, clp->cl_rpcclient->cl_nodename);
6385 scnprintf(str, len, "Linux NFSv%u.%u %s",
6386 clp->rpc_ops->version, clp->cl_minorversion,
6387 clp->cl_rpcclient->cl_nodename);
6388 clp->cl_owner_id = str;
6393 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6394 * services. Advertise one based on the address family of the
6398 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6400 if (strchr(clp->cl_ipaddr, ':') != NULL)
6401 return scnprintf(buf, len, "tcp6");
6403 return scnprintf(buf, len, "tcp");
6406 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6408 struct nfs4_setclientid *sc = calldata;
6410 if (task->tk_status == 0)
6411 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6414 static const struct rpc_call_ops nfs4_setclientid_ops = {
6415 .rpc_call_done = nfs4_setclientid_done,
6419 * nfs4_proc_setclientid - Negotiate client ID
6420 * @clp: state data structure
6421 * @program: RPC program for NFSv4 callback service
6422 * @port: IP port number for NFS4 callback service
6423 * @cred: credential to use for this call
6424 * @res: where to place the result
6426 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6428 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6429 unsigned short port, const struct cred *cred,
6430 struct nfs4_setclientid_res *res)
6432 nfs4_verifier sc_verifier;
6433 struct nfs4_setclientid setclientid = {
6434 .sc_verifier = &sc_verifier,
6438 struct rpc_message msg = {
6439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6440 .rpc_argp = &setclientid,
6444 struct rpc_task_setup task_setup_data = {
6445 .rpc_client = clp->cl_rpcclient,
6446 .rpc_message = &msg,
6447 .callback_ops = &nfs4_setclientid_ops,
6448 .callback_data = &setclientid,
6449 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6451 unsigned long now = jiffies;
6454 /* nfs_client_id4 */
6455 nfs4_init_boot_verifier(clp, &sc_verifier);
6457 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6458 status = nfs4_init_uniform_client_string(clp);
6460 status = nfs4_init_nonuniform_client_string(clp);
6466 setclientid.sc_netid_len =
6467 nfs4_init_callback_netid(clp,
6468 setclientid.sc_netid,
6469 sizeof(setclientid.sc_netid));
6470 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6471 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6472 clp->cl_ipaddr, port >> 8, port & 255);
6474 dprintk("NFS call setclientid auth=%s, '%s'\n",
6475 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6478 status = nfs4_call_sync_custom(&task_setup_data);
6479 if (setclientid.sc_cred) {
6480 kfree(clp->cl_acceptor);
6481 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6482 put_rpccred(setclientid.sc_cred);
6486 do_renew_lease(clp, now);
6488 trace_nfs4_setclientid(clp, status);
6489 dprintk("NFS reply setclientid: %d\n", status);
6494 * nfs4_proc_setclientid_confirm - Confirm client ID
6495 * @clp: state data structure
6496 * @arg: result of a previous SETCLIENTID
6497 * @cred: credential to use for this call
6499 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6501 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6502 struct nfs4_setclientid_res *arg,
6503 const struct cred *cred)
6505 struct rpc_message msg = {
6506 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6512 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6513 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6515 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6516 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6517 trace_nfs4_setclientid_confirm(clp, status);
6518 dprintk("NFS reply setclientid_confirm: %d\n", status);
6522 struct nfs4_delegreturndata {
6523 struct nfs4_delegreturnargs args;
6524 struct nfs4_delegreturnres res;
6526 nfs4_stateid stateid;
6527 unsigned long timestamp;
6529 struct nfs4_layoutreturn_args arg;
6530 struct nfs4_layoutreturn_res res;
6531 struct nfs4_xdr_opaque_data ld_private;
6535 struct nfs_fattr fattr;
6537 struct inode *inode;
6540 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6542 struct nfs4_delegreturndata *data = calldata;
6543 struct nfs4_exception exception = {
6544 .inode = data->inode,
6545 .stateid = &data->stateid,
6546 .task_is_privileged = data->args.seq_args.sa_privileged,
6549 if (!nfs4_sequence_done(task, &data->res.seq_res))
6552 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6554 /* Handle Layoutreturn errors */
6555 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6556 &data->res.lr_ret) == -EAGAIN)
6559 switch (task->tk_status) {
6561 renew_lease(data->res.server, data->timestamp);
6563 case -NFS4ERR_ADMIN_REVOKED:
6564 case -NFS4ERR_DELEG_REVOKED:
6565 case -NFS4ERR_EXPIRED:
6566 nfs4_free_revoked_stateid(data->res.server,
6568 task->tk_msg.rpc_cred);
6570 case -NFS4ERR_BAD_STATEID:
6571 case -NFS4ERR_STALE_STATEID:
6573 task->tk_status = 0;
6575 case -NFS4ERR_OLD_STATEID:
6576 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6577 nfs4_stateid_seqid_inc(&data->stateid);
6578 if (data->args.bitmask) {
6579 data->args.bitmask = NULL;
6580 data->res.fattr = NULL;
6583 case -NFS4ERR_ACCESS:
6584 if (data->args.bitmask) {
6585 data->args.bitmask = NULL;
6586 data->res.fattr = NULL;
6591 task->tk_status = nfs4_async_handle_exception(task,
6592 data->res.server, task->tk_status,
6594 if (exception.retry)
6597 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6598 data->rpc_status = task->tk_status;
6601 task->tk_status = 0;
6602 rpc_restart_call_prepare(task);
6605 static void nfs4_delegreturn_release(void *calldata)
6607 struct nfs4_delegreturndata *data = calldata;
6608 struct inode *inode = data->inode;
6611 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6614 nfs4_fattr_set_prechange(&data->fattr,
6615 inode_peek_iversion_raw(inode));
6616 nfs_refresh_inode(inode, &data->fattr);
6617 nfs_iput_and_deactive(inode);
6622 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6624 struct nfs4_delegreturndata *d_data;
6625 struct pnfs_layout_hdr *lo;
6629 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6630 nfs4_sequence_done(task, &d_data->res.seq_res);
6634 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6635 if (lo && !pnfs_layout_is_valid(lo)) {
6636 d_data->args.lr_args = NULL;
6637 d_data->res.lr_res = NULL;
6640 nfs4_setup_sequence(d_data->res.server->nfs_client,
6641 &d_data->args.seq_args,
6642 &d_data->res.seq_res,
6646 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6647 .rpc_call_prepare = nfs4_delegreturn_prepare,
6648 .rpc_call_done = nfs4_delegreturn_done,
6649 .rpc_release = nfs4_delegreturn_release,
6652 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6654 struct nfs4_delegreturndata *data;
6655 struct nfs_server *server = NFS_SERVER(inode);
6656 struct rpc_task *task;
6657 struct rpc_message msg = {
6658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6661 struct rpc_task_setup task_setup_data = {
6662 .rpc_client = server->client,
6663 .rpc_message = &msg,
6664 .callback_ops = &nfs4_delegreturn_ops,
6665 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6669 if (nfs_server_capable(inode, NFS_CAP_MOVEABLE))
6670 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6672 data = kzalloc(sizeof(*data), GFP_KERNEL);
6676 nfs4_state_protect(server->nfs_client,
6677 NFS_SP4_MACH_CRED_CLEANUP,
6678 &task_setup_data.rpc_client, &msg);
6680 data->args.fhandle = &data->fh;
6681 data->args.stateid = &data->stateid;
6682 nfs4_bitmask_set(data->args.bitmask_store,
6683 server->cache_consistency_bitmask, inode, 0);
6684 data->args.bitmask = data->args.bitmask_store;
6685 nfs_copy_fh(&data->fh, NFS_FH(inode));
6686 nfs4_stateid_copy(&data->stateid, stateid);
6687 data->res.fattr = &data->fattr;
6688 data->res.server = server;
6689 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6690 data->lr.arg.ld_private = &data->lr.ld_private;
6691 nfs_fattr_init(data->res.fattr);
6692 data->timestamp = jiffies;
6693 data->rpc_status = 0;
6694 data->inode = nfs_igrab_and_active(inode);
6695 if (data->inode || issync) {
6696 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6699 data->args.lr_args = &data->lr.arg;
6700 data->res.lr_res = &data->lr.res;
6705 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6708 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6710 task_setup_data.callback_data = data;
6711 msg.rpc_argp = &data->args;
6712 msg.rpc_resp = &data->res;
6713 task = rpc_run_task(&task_setup_data);
6715 return PTR_ERR(task);
6718 status = rpc_wait_for_completion_task(task);
6721 status = data->rpc_status;
6727 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6729 struct nfs_server *server = NFS_SERVER(inode);
6730 struct nfs4_exception exception = { };
6733 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6734 trace_nfs4_delegreturn(inode, stateid, err);
6736 case -NFS4ERR_STALE_STATEID:
6737 case -NFS4ERR_EXPIRED:
6741 err = nfs4_handle_exception(server, err, &exception);
6742 } while (exception.retry);
6746 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6748 struct inode *inode = state->inode;
6749 struct nfs_server *server = NFS_SERVER(inode);
6750 struct nfs_client *clp = server->nfs_client;
6751 struct nfs_lockt_args arg = {
6752 .fh = NFS_FH(inode),
6755 struct nfs_lockt_res res = {
6758 struct rpc_message msg = {
6759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6762 .rpc_cred = state->owner->so_cred,
6764 struct nfs4_lock_state *lsp;
6767 arg.lock_owner.clientid = clp->cl_clientid;
6768 status = nfs4_set_lock_state(state, request);
6771 lsp = request->fl_u.nfs4_fl.owner;
6772 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6773 arg.lock_owner.s_dev = server->s_dev;
6774 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6777 request->fl_type = F_UNLCK;
6779 case -NFS4ERR_DENIED:
6782 request->fl_ops->fl_release_private(request);
6783 request->fl_ops = NULL;
6788 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6790 struct nfs4_exception exception = {
6791 .interruptible = true,
6796 err = _nfs4_proc_getlk(state, cmd, request);
6797 trace_nfs4_get_lock(request, state, cmd, err);
6798 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6800 } while (exception.retry);
6805 * Update the seqid of a lock stateid after receiving
6806 * NFS4ERR_OLD_STATEID
6808 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6809 struct nfs4_lock_state *lsp)
6811 struct nfs4_state *state = lsp->ls_state;
6814 spin_lock(&state->state_lock);
6815 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6817 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6818 nfs4_stateid_seqid_inc(dst);
6820 dst->seqid = lsp->ls_stateid.seqid;
6823 spin_unlock(&state->state_lock);
6827 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6828 struct nfs4_lock_state *lsp)
6830 struct nfs4_state *state = lsp->ls_state;
6833 spin_lock(&state->state_lock);
6834 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6835 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6836 spin_unlock(&state->state_lock);
6840 struct nfs4_unlockdata {
6841 struct nfs_locku_args arg;
6842 struct nfs_locku_res res;
6843 struct nfs4_lock_state *lsp;
6844 struct nfs_open_context *ctx;
6845 struct nfs_lock_context *l_ctx;
6846 struct file_lock fl;
6847 struct nfs_server *server;
6848 unsigned long timestamp;
6851 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6852 struct nfs_open_context *ctx,
6853 struct nfs4_lock_state *lsp,
6854 struct nfs_seqid *seqid)
6856 struct nfs4_unlockdata *p;
6857 struct nfs4_state *state = lsp->ls_state;
6858 struct inode *inode = state->inode;
6860 p = kzalloc(sizeof(*p), GFP_KERNEL);
6863 p->arg.fh = NFS_FH(inode);
6865 p->arg.seqid = seqid;
6866 p->res.seqid = seqid;
6868 /* Ensure we don't close file until we're done freeing locks! */
6869 p->ctx = get_nfs_open_context(ctx);
6870 p->l_ctx = nfs_get_lock_context(ctx);
6871 locks_init_lock(&p->fl);
6872 locks_copy_lock(&p->fl, fl);
6873 p->server = NFS_SERVER(inode);
6874 spin_lock(&state->state_lock);
6875 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6876 spin_unlock(&state->state_lock);
6880 static void nfs4_locku_release_calldata(void *data)
6882 struct nfs4_unlockdata *calldata = data;
6883 nfs_free_seqid(calldata->arg.seqid);
6884 nfs4_put_lock_state(calldata->lsp);
6885 nfs_put_lock_context(calldata->l_ctx);
6886 put_nfs_open_context(calldata->ctx);
6890 static void nfs4_locku_done(struct rpc_task *task, void *data)
6892 struct nfs4_unlockdata *calldata = data;
6893 struct nfs4_exception exception = {
6894 .inode = calldata->lsp->ls_state->inode,
6895 .stateid = &calldata->arg.stateid,
6898 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6900 switch (task->tk_status) {
6902 renew_lease(calldata->server, calldata->timestamp);
6903 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6904 if (nfs4_update_lock_stateid(calldata->lsp,
6905 &calldata->res.stateid))
6908 case -NFS4ERR_ADMIN_REVOKED:
6909 case -NFS4ERR_EXPIRED:
6910 nfs4_free_revoked_stateid(calldata->server,
6911 &calldata->arg.stateid,
6912 task->tk_msg.rpc_cred);
6914 case -NFS4ERR_BAD_STATEID:
6915 case -NFS4ERR_STALE_STATEID:
6916 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6918 rpc_restart_call_prepare(task);
6920 case -NFS4ERR_OLD_STATEID:
6921 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6923 rpc_restart_call_prepare(task);
6926 task->tk_status = nfs4_async_handle_exception(task,
6927 calldata->server, task->tk_status,
6929 if (exception.retry)
6930 rpc_restart_call_prepare(task);
6932 nfs_release_seqid(calldata->arg.seqid);
6935 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6937 struct nfs4_unlockdata *calldata = data;
6939 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6940 nfs_async_iocounter_wait(task, calldata->l_ctx))
6943 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6945 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6946 /* Note: exit _without_ running nfs4_locku_done */
6949 calldata->timestamp = jiffies;
6950 if (nfs4_setup_sequence(calldata->server->nfs_client,
6951 &calldata->arg.seq_args,
6952 &calldata->res.seq_res,
6954 nfs_release_seqid(calldata->arg.seqid);
6957 task->tk_action = NULL;
6959 nfs4_sequence_done(task, &calldata->res.seq_res);
6962 static const struct rpc_call_ops nfs4_locku_ops = {
6963 .rpc_call_prepare = nfs4_locku_prepare,
6964 .rpc_call_done = nfs4_locku_done,
6965 .rpc_release = nfs4_locku_release_calldata,
6968 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6969 struct nfs_open_context *ctx,
6970 struct nfs4_lock_state *lsp,
6971 struct nfs_seqid *seqid)
6973 struct nfs4_unlockdata *data;
6974 struct rpc_message msg = {
6975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6976 .rpc_cred = ctx->cred,
6978 struct rpc_task_setup task_setup_data = {
6979 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6980 .rpc_message = &msg,
6981 .callback_ops = &nfs4_locku_ops,
6982 .workqueue = nfsiod_workqueue,
6983 .flags = RPC_TASK_ASYNC,
6986 if (nfs_server_capable(lsp->ls_state->inode, NFS_CAP_MOVEABLE))
6987 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6989 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6990 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6992 /* Ensure this is an unlock - when canceling a lock, the
6993 * canceled lock is passed in, and it won't be an unlock.
6995 fl->fl_type = F_UNLCK;
6996 if (fl->fl_flags & FL_CLOSE)
6997 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6999 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
7001 nfs_free_seqid(seqid);
7002 return ERR_PTR(-ENOMEM);
7005 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
7006 msg.rpc_argp = &data->arg;
7007 msg.rpc_resp = &data->res;
7008 task_setup_data.callback_data = data;
7009 return rpc_run_task(&task_setup_data);
7012 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
7014 struct inode *inode = state->inode;
7015 struct nfs4_state_owner *sp = state->owner;
7016 struct nfs_inode *nfsi = NFS_I(inode);
7017 struct nfs_seqid *seqid;
7018 struct nfs4_lock_state *lsp;
7019 struct rpc_task *task;
7020 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7022 unsigned char fl_flags = request->fl_flags;
7024 status = nfs4_set_lock_state(state, request);
7025 /* Unlock _before_ we do the RPC call */
7026 request->fl_flags |= FL_EXISTS;
7027 /* Exclude nfs_delegation_claim_locks() */
7028 mutex_lock(&sp->so_delegreturn_mutex);
7029 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
7030 down_read(&nfsi->rwsem);
7031 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
7032 up_read(&nfsi->rwsem);
7033 mutex_unlock(&sp->so_delegreturn_mutex);
7036 lsp = request->fl_u.nfs4_fl.owner;
7037 set_bit(NFS_LOCK_UNLOCKING, &lsp->ls_flags);
7038 up_read(&nfsi->rwsem);
7039 mutex_unlock(&sp->so_delegreturn_mutex);
7042 /* Is this a delegated lock? */
7043 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
7045 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
7046 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
7050 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
7051 status = PTR_ERR(task);
7054 status = rpc_wait_for_completion_task(task);
7057 request->fl_flags = fl_flags;
7058 trace_nfs4_unlock(request, state, F_SETLK, status);
7062 struct nfs4_lockdata {
7063 struct nfs_lock_args arg;
7064 struct nfs_lock_res res;
7065 struct nfs4_lock_state *lsp;
7066 struct nfs_open_context *ctx;
7067 struct file_lock fl;
7068 unsigned long timestamp;
7071 struct nfs_server *server;
7074 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
7075 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
7078 struct nfs4_lockdata *p;
7079 struct inode *inode = lsp->ls_state->inode;
7080 struct nfs_server *server = NFS_SERVER(inode);
7081 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7083 p = kzalloc(sizeof(*p), gfp_mask);
7087 p->arg.fh = NFS_FH(inode);
7089 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
7090 if (IS_ERR(p->arg.open_seqid))
7092 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
7093 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
7094 if (IS_ERR(p->arg.lock_seqid))
7095 goto out_free_seqid;
7096 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
7097 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
7098 p->arg.lock_owner.s_dev = server->s_dev;
7099 p->res.lock_seqid = p->arg.lock_seqid;
7102 p->ctx = get_nfs_open_context(ctx);
7103 locks_init_lock(&p->fl);
7104 locks_copy_lock(&p->fl, fl);
7107 nfs_free_seqid(p->arg.open_seqid);
7113 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7115 struct nfs4_lockdata *data = calldata;
7116 struct nfs4_state *state = data->lsp->ls_state;
7118 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
7120 /* Do we need to do an open_to_lock_owner? */
7121 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
7122 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
7123 goto out_release_lock_seqid;
7125 nfs4_stateid_copy(&data->arg.open_stateid,
7126 &state->open_stateid);
7127 data->arg.new_lock_owner = 1;
7128 data->res.open_seqid = data->arg.open_seqid;
7130 data->arg.new_lock_owner = 0;
7131 nfs4_stateid_copy(&data->arg.lock_stateid,
7132 &data->lsp->ls_stateid);
7134 if (!nfs4_valid_open_stateid(state)) {
7135 data->rpc_status = -EBADF;
7136 task->tk_action = NULL;
7137 goto out_release_open_seqid;
7139 data->timestamp = jiffies;
7140 if (nfs4_setup_sequence(data->server->nfs_client,
7141 &data->arg.seq_args,
7145 out_release_open_seqid:
7146 nfs_release_seqid(data->arg.open_seqid);
7147 out_release_lock_seqid:
7148 nfs_release_seqid(data->arg.lock_seqid);
7150 nfs4_sequence_done(task, &data->res.seq_res);
7151 dprintk("%s: ret = %d\n", __func__, data->rpc_status);
7154 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7156 struct nfs4_lockdata *data = calldata;
7157 struct nfs4_lock_state *lsp = data->lsp;
7159 if (!nfs4_sequence_done(task, &data->res.seq_res))
7162 data->rpc_status = task->tk_status;
7163 switch (task->tk_status) {
7165 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
7167 if (data->arg.new_lock && !data->cancelled) {
7168 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
7169 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7172 if (data->arg.new_lock_owner != 0) {
7173 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7174 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7175 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7176 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7179 case -NFS4ERR_OLD_STATEID:
7180 if (data->arg.new_lock_owner != 0 &&
7181 nfs4_refresh_open_old_stateid(&data->arg.open_stateid,
7184 if (nfs4_refresh_lock_old_stateid(&data->arg.lock_stateid, lsp))
7187 case -NFS4ERR_BAD_STATEID:
7188 case -NFS4ERR_STALE_STATEID:
7189 case -NFS4ERR_EXPIRED:
7190 if (data->arg.new_lock_owner != 0) {
7191 if (!nfs4_stateid_match(&data->arg.open_stateid,
7192 &lsp->ls_state->open_stateid))
7194 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7199 dprintk("%s: ret = %d!\n", __func__, data->rpc_status);
7202 if (!data->cancelled)
7203 rpc_restart_call_prepare(task);
7207 static void nfs4_lock_release(void *calldata)
7209 struct nfs4_lockdata *data = calldata;
7211 nfs_free_seqid(data->arg.open_seqid);
7212 if (data->cancelled && data->rpc_status == 0) {
7213 struct rpc_task *task;
7214 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7215 data->arg.lock_seqid);
7217 rpc_put_task_async(task);
7218 dprintk("%s: cancelling lock!\n", __func__);
7220 nfs_free_seqid(data->arg.lock_seqid);
7221 nfs4_put_lock_state(data->lsp);
7222 put_nfs_open_context(data->ctx);
7226 static const struct rpc_call_ops nfs4_lock_ops = {
7227 .rpc_call_prepare = nfs4_lock_prepare,
7228 .rpc_call_done = nfs4_lock_done,
7229 .rpc_release = nfs4_lock_release,
7232 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7235 case -NFS4ERR_ADMIN_REVOKED:
7236 case -NFS4ERR_EXPIRED:
7237 case -NFS4ERR_BAD_STATEID:
7238 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7239 if (new_lock_owner != 0 ||
7240 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7241 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7243 case -NFS4ERR_STALE_STATEID:
7244 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7245 nfs4_schedule_lease_recovery(server->nfs_client);
7249 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7251 struct nfs4_lockdata *data;
7252 struct rpc_task *task;
7253 struct rpc_message msg = {
7254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7255 .rpc_cred = state->owner->so_cred,
7257 struct rpc_task_setup task_setup_data = {
7258 .rpc_client = NFS_CLIENT(state->inode),
7259 .rpc_message = &msg,
7260 .callback_ops = &nfs4_lock_ops,
7261 .workqueue = nfsiod_workqueue,
7262 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7266 if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
7267 task_setup_data.flags |= RPC_TASK_MOVEABLE;
7269 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7270 fl->fl_u.nfs4_fl.owner, GFP_KERNEL);
7274 data->arg.block = 1;
7275 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7276 recovery_type > NFS_LOCK_NEW);
7277 msg.rpc_argp = &data->arg;
7278 msg.rpc_resp = &data->res;
7279 task_setup_data.callback_data = data;
7280 if (recovery_type > NFS_LOCK_NEW) {
7281 if (recovery_type == NFS_LOCK_RECLAIM)
7282 data->arg.reclaim = NFS_LOCK_RECLAIM;
7284 data->arg.new_lock = 1;
7285 task = rpc_run_task(&task_setup_data);
7287 return PTR_ERR(task);
7288 ret = rpc_wait_for_completion_task(task);
7290 ret = data->rpc_status;
7292 nfs4_handle_setlk_error(data->server, data->lsp,
7293 data->arg.new_lock_owner, ret);
7295 data->cancelled = true;
7296 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7298 dprintk("%s: ret = %d\n", __func__, ret);
7302 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7304 struct nfs_server *server = NFS_SERVER(state->inode);
7305 struct nfs4_exception exception = {
7306 .inode = state->inode,
7311 /* Cache the lock if possible... */
7312 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7314 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7315 if (err != -NFS4ERR_DELAY)
7317 nfs4_handle_exception(server, err, &exception);
7318 } while (exception.retry);
7322 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7324 struct nfs_server *server = NFS_SERVER(state->inode);
7325 struct nfs4_exception exception = {
7326 .inode = state->inode,
7330 err = nfs4_set_lock_state(state, request);
7333 if (!recover_lost_locks) {
7334 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7338 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7340 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7344 case -NFS4ERR_GRACE:
7345 case -NFS4ERR_DELAY:
7346 nfs4_handle_exception(server, err, &exception);
7349 } while (exception.retry);
7354 #if defined(CONFIG_NFS_V4_1)
7355 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7357 struct nfs4_lock_state *lsp;
7360 status = nfs4_set_lock_state(state, request);
7363 lsp = request->fl_u.nfs4_fl.owner;
7364 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7365 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7367 return nfs4_lock_expired(state, request);
7371 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7373 struct nfs_inode *nfsi = NFS_I(state->inode);
7374 struct nfs4_state_owner *sp = state->owner;
7375 unsigned char fl_flags = request->fl_flags;
7378 request->fl_flags |= FL_ACCESS;
7379 status = locks_lock_inode_wait(state->inode, request);
7382 mutex_lock(&sp->so_delegreturn_mutex);
7383 down_read(&nfsi->rwsem);
7384 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7385 /* Yes: cache locks! */
7386 /* ...but avoid races with delegation recall... */
7387 request->fl_flags = fl_flags & ~FL_SLEEP;
7388 status = locks_lock_inode_wait(state->inode, request);
7389 up_read(&nfsi->rwsem);
7390 mutex_unlock(&sp->so_delegreturn_mutex);
7393 up_read(&nfsi->rwsem);
7394 mutex_unlock(&sp->so_delegreturn_mutex);
7395 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7397 request->fl_flags = fl_flags;
7401 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7403 struct nfs4_exception exception = {
7405 .inode = state->inode,
7406 .interruptible = true,
7411 err = _nfs4_proc_setlk(state, cmd, request);
7412 if (err == -NFS4ERR_DENIED)
7414 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7416 } while (exception.retry);
7420 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7421 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7424 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7425 struct file_lock *request)
7427 int status = -ERESTARTSYS;
7428 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7430 while(!signalled()) {
7431 status = nfs4_proc_setlk(state, cmd, request);
7432 if ((status != -EAGAIN) || IS_SETLK(cmd))
7434 __set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
7435 schedule_timeout(timeout);
7437 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7438 status = -ERESTARTSYS;
7443 #ifdef CONFIG_NFS_V4_1
7444 struct nfs4_lock_waiter {
7445 struct inode *inode;
7446 struct nfs_lowner owner;
7447 wait_queue_entry_t wait;
7451 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7453 struct nfs4_lock_waiter *waiter =
7454 container_of(wait, struct nfs4_lock_waiter, wait);
7456 /* NULL key means to wake up everyone */
7458 struct cb_notify_lock_args *cbnl = key;
7459 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7460 *wowner = &waiter->owner;
7462 /* Only wake if the callback was for the same owner. */
7463 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7466 /* Make sure it's for the right inode */
7467 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7471 return woken_wake_function(wait, mode, flags, key);
7475 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7477 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7478 struct nfs_server *server = NFS_SERVER(state->inode);
7479 struct nfs_client *clp = server->nfs_client;
7480 wait_queue_head_t *q = &clp->cl_lock_waitq;
7481 struct nfs4_lock_waiter waiter = {
7482 .inode = state->inode,
7483 .owner = { .clientid = clp->cl_clientid,
7484 .id = lsp->ls_seqid.owner_id,
7485 .s_dev = server->s_dev },
7489 /* Don't bother with waitqueue if we don't expect a callback */
7490 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7491 return nfs4_retry_setlk_simple(state, cmd, request);
7493 init_wait(&waiter.wait);
7494 waiter.wait.func = nfs4_wake_lock_waiter;
7495 add_wait_queue(q, &waiter.wait);
7498 status = nfs4_proc_setlk(state, cmd, request);
7499 if (status != -EAGAIN || IS_SETLK(cmd))
7502 status = -ERESTARTSYS;
7503 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE|TASK_FREEZABLE,
7504 NFS4_LOCK_MAXTIMEOUT);
7505 } while (!signalled());
7507 remove_wait_queue(q, &waiter.wait);
7511 #else /* !CONFIG_NFS_V4_1 */
7513 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7515 return nfs4_retry_setlk_simple(state, cmd, request);
7520 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7522 struct nfs_open_context *ctx;
7523 struct nfs4_state *state;
7526 /* verify open state */
7527 ctx = nfs_file_open_context(filp);
7530 if (IS_GETLK(cmd)) {
7532 return nfs4_proc_getlk(state, F_GETLK, request);
7536 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7539 if (request->fl_type == F_UNLCK) {
7541 return nfs4_proc_unlck(state, cmd, request);
7548 if ((request->fl_flags & FL_POSIX) &&
7549 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7553 * Don't rely on the VFS having checked the file open mode,
7554 * since it won't do this for flock() locks.
7556 switch (request->fl_type) {
7558 if (!(filp->f_mode & FMODE_READ))
7562 if (!(filp->f_mode & FMODE_WRITE))
7566 status = nfs4_set_lock_state(state, request);
7570 return nfs4_retry_setlk(state, cmd, request);
7573 static int nfs4_delete_lease(struct file *file, void **priv)
7575 return generic_setlease(file, F_UNLCK, NULL, priv);
7578 static int nfs4_add_lease(struct file *file, int arg, struct file_lock **lease,
7581 struct inode *inode = file_inode(file);
7582 fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7585 /* No delegation, no lease */
7586 if (!nfs4_have_delegation(inode, type))
7588 ret = generic_setlease(file, arg, lease, priv);
7589 if (ret || nfs4_have_delegation(inode, type))
7591 /* We raced with a delegation return */
7592 nfs4_delete_lease(file, priv);
7596 int nfs4_proc_setlease(struct file *file, int arg, struct file_lock **lease,
7602 return nfs4_add_lease(file, arg, lease, priv);
7604 return nfs4_delete_lease(file, priv);
7610 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7612 struct nfs_server *server = NFS_SERVER(state->inode);
7615 err = nfs4_set_lock_state(state, fl);
7619 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7620 if (err != -NFS4ERR_DELAY)
7623 } while (err == -NFS4ERR_DELAY);
7624 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7627 struct nfs_release_lockowner_data {
7628 struct nfs4_lock_state *lsp;
7629 struct nfs_server *server;
7630 struct nfs_release_lockowner_args args;
7631 struct nfs_release_lockowner_res res;
7632 unsigned long timestamp;
7635 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7637 struct nfs_release_lockowner_data *data = calldata;
7638 struct nfs_server *server = data->server;
7639 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7640 &data->res.seq_res, task);
7641 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7642 data->timestamp = jiffies;
7645 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7647 struct nfs_release_lockowner_data *data = calldata;
7648 struct nfs_server *server = data->server;
7650 nfs40_sequence_done(task, &data->res.seq_res);
7652 switch (task->tk_status) {
7654 renew_lease(server, data->timestamp);
7656 case -NFS4ERR_STALE_CLIENTID:
7657 case -NFS4ERR_EXPIRED:
7658 nfs4_schedule_lease_recovery(server->nfs_client);
7660 case -NFS4ERR_LEASE_MOVED:
7661 case -NFS4ERR_DELAY:
7662 if (nfs4_async_handle_error(task, server,
7663 NULL, NULL) == -EAGAIN)
7664 rpc_restart_call_prepare(task);
7668 static void nfs4_release_lockowner_release(void *calldata)
7670 struct nfs_release_lockowner_data *data = calldata;
7671 nfs4_free_lock_state(data->server, data->lsp);
7675 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7676 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7677 .rpc_call_done = nfs4_release_lockowner_done,
7678 .rpc_release = nfs4_release_lockowner_release,
7682 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7684 struct nfs_release_lockowner_data *data;
7685 struct rpc_message msg = {
7686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7689 if (server->nfs_client->cl_mvops->minor_version != 0)
7692 data = kmalloc(sizeof(*data), GFP_KERNEL);
7696 data->server = server;
7697 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7698 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7699 data->args.lock_owner.s_dev = server->s_dev;
7701 msg.rpc_argp = &data->args;
7702 msg.rpc_resp = &data->res;
7703 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7704 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7707 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7709 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7710 struct mnt_idmap *idmap,
7711 struct dentry *unused, struct inode *inode,
7712 const char *key, const void *buf,
7713 size_t buflen, int flags)
7715 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_ACL);
7718 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7719 struct dentry *unused, struct inode *inode,
7720 const char *key, void *buf, size_t buflen)
7722 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_ACL);
7725 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7727 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_ACL);
7730 #if defined(CONFIG_NFS_V4_1)
7731 #define XATTR_NAME_NFSV4_DACL "system.nfs4_dacl"
7733 static int nfs4_xattr_set_nfs4_dacl(const struct xattr_handler *handler,
7734 struct mnt_idmap *idmap,
7735 struct dentry *unused, struct inode *inode,
7736 const char *key, const void *buf,
7737 size_t buflen, int flags)
7739 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_DACL);
7742 static int nfs4_xattr_get_nfs4_dacl(const struct xattr_handler *handler,
7743 struct dentry *unused, struct inode *inode,
7744 const char *key, void *buf, size_t buflen)
7746 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_DACL);
7749 static bool nfs4_xattr_list_nfs4_dacl(struct dentry *dentry)
7751 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_DACL);
7754 #define XATTR_NAME_NFSV4_SACL "system.nfs4_sacl"
7756 static int nfs4_xattr_set_nfs4_sacl(const struct xattr_handler *handler,
7757 struct mnt_idmap *idmap,
7758 struct dentry *unused, struct inode *inode,
7759 const char *key, const void *buf,
7760 size_t buflen, int flags)
7762 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_SACL);
7765 static int nfs4_xattr_get_nfs4_sacl(const struct xattr_handler *handler,
7766 struct dentry *unused, struct inode *inode,
7767 const char *key, void *buf, size_t buflen)
7769 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_SACL);
7772 static bool nfs4_xattr_list_nfs4_sacl(struct dentry *dentry)
7774 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_SACL);
7779 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7781 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7782 struct mnt_idmap *idmap,
7783 struct dentry *unused, struct inode *inode,
7784 const char *key, const void *buf,
7785 size_t buflen, int flags)
7787 if (security_ismaclabel(key))
7788 return nfs4_set_security_label(inode, buf, buflen);
7793 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7794 struct dentry *unused, struct inode *inode,
7795 const char *key, void *buf, size_t buflen)
7797 if (security_ismaclabel(key))
7798 return nfs4_get_security_label(inode, buf, buflen);
7803 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7807 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7808 len = security_inode_listsecurity(inode, list, list_len);
7809 if (len >= 0 && list_len && len > list_len)
7815 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7816 .prefix = XATTR_SECURITY_PREFIX,
7817 .get = nfs4_xattr_get_nfs4_label,
7818 .set = nfs4_xattr_set_nfs4_label,
7824 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7831 #ifdef CONFIG_NFS_V4_2
7832 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7833 struct mnt_idmap *idmap,
7834 struct dentry *unused, struct inode *inode,
7835 const char *key, const void *buf,
7836 size_t buflen, int flags)
7841 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7845 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7846 * flags right now. Handling of xattr operations use the normal
7847 * file read/write permissions.
7849 * Just in case the server has other ideas (which RFC 8276 allows),
7850 * do a cached access check for the XA* flags to possibly avoid
7851 * doing an RPC and getting EACCES back.
7853 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7854 if (!(mask & NFS_ACCESS_XAWRITE))
7859 ret = nfs42_proc_removexattr(inode, key);
7861 nfs4_xattr_cache_remove(inode, key);
7863 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7865 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7871 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7872 struct dentry *unused, struct inode *inode,
7873 const char *key, void *buf, size_t buflen)
7878 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7881 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7882 if (!(mask & NFS_ACCESS_XAREAD))
7886 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7890 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7891 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7894 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7900 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7909 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7912 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7913 if (!(mask & NFS_ACCESS_XALIST))
7917 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7921 ret = nfs4_xattr_cache_list(inode, list, list_len);
7922 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7927 buflen = list_len ? list_len : XATTR_LIST_MAX;
7928 buf = list_len ? list : NULL;
7932 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7945 nfs4_xattr_cache_set_list(inode, list, size);
7953 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7957 #endif /* CONFIG_NFS_V4_2 */
7960 * nfs_fhget will use either the mounted_on_fileid or the fileid
7962 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7964 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7965 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7966 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7967 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7970 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7971 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7972 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7976 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7977 const struct qstr *name,
7978 struct nfs4_fs_locations *fs_locations,
7981 struct nfs_server *server = NFS_SERVER(dir);
7983 struct nfs4_fs_locations_arg args = {
7984 .dir_fh = NFS_FH(dir),
7989 struct nfs4_fs_locations_res res = {
7990 .fs_locations = fs_locations,
7992 struct rpc_message msg = {
7993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7999 dprintk("%s: start\n", __func__);
8001 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
8002 bitmask[1] = nfs4_fattr_bitmap[1];
8004 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
8005 * is not supported */
8006 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
8007 bitmask[0] &= ~FATTR4_WORD0_FILEID;
8009 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
8011 nfs_fattr_init(fs_locations->fattr);
8012 fs_locations->server = server;
8013 fs_locations->nlocations = 0;
8014 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
8015 dprintk("%s: returned status = %d\n", __func__, status);
8019 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
8020 const struct qstr *name,
8021 struct nfs4_fs_locations *fs_locations,
8024 struct nfs4_exception exception = {
8025 .interruptible = true,
8029 err = _nfs4_proc_fs_locations(client, dir, name,
8030 fs_locations, page);
8031 trace_nfs4_get_fs_locations(dir, name, err);
8032 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8034 } while (exception.retry);
8039 * This operation also signals the server that this client is
8040 * performing migration recovery. The server can stop returning
8041 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
8042 * appended to this compound to identify the client ID which is
8043 * performing recovery.
8045 static int _nfs40_proc_get_locations(struct nfs_server *server,
8046 struct nfs_fh *fhandle,
8047 struct nfs4_fs_locations *locations,
8048 struct page *page, const struct cred *cred)
8050 struct rpc_clnt *clnt = server->client;
8052 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8054 struct nfs4_fs_locations_arg args = {
8055 .clientid = server->nfs_client->cl_clientid,
8059 .migration = 1, /* skip LOOKUP */
8060 .renew = 1, /* append RENEW */
8062 struct nfs4_fs_locations_res res = {
8063 .fs_locations = locations,
8067 struct rpc_message msg = {
8068 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8073 unsigned long now = jiffies;
8076 nfs_fattr_init(locations->fattr);
8077 locations->server = server;
8078 locations->nlocations = 0;
8080 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8081 status = nfs4_call_sync_sequence(clnt, server, &msg,
8082 &args.seq_args, &res.seq_res);
8086 renew_lease(server, now);
8090 #ifdef CONFIG_NFS_V4_1
8093 * This operation also signals the server that this client is
8094 * performing migration recovery. The server can stop asserting
8095 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
8096 * performing this operation is identified in the SEQUENCE
8097 * operation in this compound.
8099 * When the client supports GETATTR(fs_locations_info), it can
8100 * be plumbed in here.
8102 static int _nfs41_proc_get_locations(struct nfs_server *server,
8103 struct nfs_fh *fhandle,
8104 struct nfs4_fs_locations *locations,
8105 struct page *page, const struct cred *cred)
8107 struct rpc_clnt *clnt = server->client;
8109 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8111 struct nfs4_fs_locations_arg args = {
8115 .migration = 1, /* skip LOOKUP */
8117 struct nfs4_fs_locations_res res = {
8118 .fs_locations = locations,
8121 struct rpc_message msg = {
8122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8127 struct nfs4_call_sync_data data = {
8128 .seq_server = server,
8129 .seq_args = &args.seq_args,
8130 .seq_res = &res.seq_res,
8132 struct rpc_task_setup task_setup_data = {
8134 .rpc_message = &msg,
8135 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
8136 .callback_data = &data,
8137 .flags = RPC_TASK_NO_ROUND_ROBIN,
8141 nfs_fattr_init(locations->fattr);
8142 locations->server = server;
8143 locations->nlocations = 0;
8145 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8146 status = nfs4_call_sync_custom(&task_setup_data);
8147 if (status == NFS4_OK &&
8148 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8149 status = -NFS4ERR_LEASE_MOVED;
8153 #endif /* CONFIG_NFS_V4_1 */
8156 * nfs4_proc_get_locations - discover locations for a migrated FSID
8157 * @server: pointer to nfs_server to process
8158 * @fhandle: pointer to the kernel NFS client file handle
8159 * @locations: result of query
8161 * @cred: credential to use for this operation
8163 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
8164 * operation failed, or a negative errno if a local error occurred.
8166 * On success, "locations" is filled in, but if the server has
8167 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
8170 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
8171 * from this client that require migration recovery.
8173 int nfs4_proc_get_locations(struct nfs_server *server,
8174 struct nfs_fh *fhandle,
8175 struct nfs4_fs_locations *locations,
8176 struct page *page, const struct cred *cred)
8178 struct nfs_client *clp = server->nfs_client;
8179 const struct nfs4_mig_recovery_ops *ops =
8180 clp->cl_mvops->mig_recovery_ops;
8181 struct nfs4_exception exception = {
8182 .interruptible = true,
8186 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8187 (unsigned long long)server->fsid.major,
8188 (unsigned long long)server->fsid.minor,
8190 nfs_display_fhandle(fhandle, __func__);
8193 status = ops->get_locations(server, fhandle, locations, page,
8195 if (status != -NFS4ERR_DELAY)
8197 nfs4_handle_exception(server, status, &exception);
8198 } while (exception.retry);
8203 * This operation also signals the server that this client is
8204 * performing "lease moved" recovery. The server can stop
8205 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
8206 * is appended to this compound to identify the client ID which is
8207 * performing recovery.
8209 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
8211 struct nfs_server *server = NFS_SERVER(inode);
8212 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8213 struct rpc_clnt *clnt = server->client;
8214 struct nfs4_fsid_present_arg args = {
8215 .fh = NFS_FH(inode),
8216 .clientid = clp->cl_clientid,
8217 .renew = 1, /* append RENEW */
8219 struct nfs4_fsid_present_res res = {
8222 struct rpc_message msg = {
8223 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8228 unsigned long now = jiffies;
8231 res.fh = nfs_alloc_fhandle();
8235 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8236 status = nfs4_call_sync_sequence(clnt, server, &msg,
8237 &args.seq_args, &res.seq_res);
8238 nfs_free_fhandle(res.fh);
8242 do_renew_lease(clp, now);
8246 #ifdef CONFIG_NFS_V4_1
8249 * This operation also signals the server that this client is
8250 * performing "lease moved" recovery. The server can stop asserting
8251 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8252 * this operation is identified in the SEQUENCE operation in this
8255 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8257 struct nfs_server *server = NFS_SERVER(inode);
8258 struct rpc_clnt *clnt = server->client;
8259 struct nfs4_fsid_present_arg args = {
8260 .fh = NFS_FH(inode),
8262 struct nfs4_fsid_present_res res = {
8264 struct rpc_message msg = {
8265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8272 res.fh = nfs_alloc_fhandle();
8276 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8277 status = nfs4_call_sync_sequence(clnt, server, &msg,
8278 &args.seq_args, &res.seq_res);
8279 nfs_free_fhandle(res.fh);
8280 if (status == NFS4_OK &&
8281 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8282 status = -NFS4ERR_LEASE_MOVED;
8286 #endif /* CONFIG_NFS_V4_1 */
8289 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8290 * @inode: inode on FSID to check
8291 * @cred: credential to use for this operation
8293 * Server indicates whether the FSID is present, moved, or not
8294 * recognized. This operation is necessary to clear a LEASE_MOVED
8295 * condition for this client ID.
8297 * Returns NFS4_OK if the FSID is present on this server,
8298 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8299 * NFS4ERR code if some error occurred on the server, or a
8300 * negative errno if a local failure occurred.
8302 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8304 struct nfs_server *server = NFS_SERVER(inode);
8305 struct nfs_client *clp = server->nfs_client;
8306 const struct nfs4_mig_recovery_ops *ops =
8307 clp->cl_mvops->mig_recovery_ops;
8308 struct nfs4_exception exception = {
8309 .interruptible = true,
8313 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8314 (unsigned long long)server->fsid.major,
8315 (unsigned long long)server->fsid.minor,
8317 nfs_display_fhandle(NFS_FH(inode), __func__);
8320 status = ops->fsid_present(inode, cred);
8321 if (status != -NFS4ERR_DELAY)
8323 nfs4_handle_exception(server, status, &exception);
8324 } while (exception.retry);
8329 * If 'use_integrity' is true and the state managment nfs_client
8330 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8331 * and the machine credential as per RFC3530bis and RFC5661 Security
8332 * Considerations sections. Otherwise, just use the user cred with the
8333 * filesystem's rpc_client.
8335 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8338 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8339 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8340 struct nfs4_secinfo_arg args = {
8341 .dir_fh = NFS_FH(dir),
8344 struct nfs4_secinfo_res res = {
8347 struct rpc_message msg = {
8348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8352 struct nfs4_call_sync_data data = {
8353 .seq_server = NFS_SERVER(dir),
8354 .seq_args = &args.seq_args,
8355 .seq_res = &res.seq_res,
8357 struct rpc_task_setup task_setup = {
8359 .rpc_message = &msg,
8360 .callback_ops = clp->cl_mvops->call_sync_ops,
8361 .callback_data = &data,
8362 .flags = RPC_TASK_NO_ROUND_ROBIN,
8364 const struct cred *cred = NULL;
8366 if (use_integrity) {
8367 clnt = clp->cl_rpcclient;
8368 task_setup.rpc_client = clnt;
8370 cred = nfs4_get_clid_cred(clp);
8371 msg.rpc_cred = cred;
8374 dprintk("NFS call secinfo %s\n", name->name);
8376 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8377 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8378 status = nfs4_call_sync_custom(&task_setup);
8380 dprintk("NFS reply secinfo: %d\n", status);
8386 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8387 struct nfs4_secinfo_flavors *flavors)
8389 struct nfs4_exception exception = {
8390 .interruptible = true,
8394 err = -NFS4ERR_WRONGSEC;
8396 /* try to use integrity protection with machine cred */
8397 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8398 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8401 * if unable to use integrity protection, or SECINFO with
8402 * integrity protection returns NFS4ERR_WRONGSEC (which is
8403 * disallowed by spec, but exists in deployed servers) use
8404 * the current filesystem's rpc_client and the user cred.
8406 if (err == -NFS4ERR_WRONGSEC)
8407 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8409 trace_nfs4_secinfo(dir, name, err);
8410 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8412 } while (exception.retry);
8416 #ifdef CONFIG_NFS_V4_1
8418 * Check the exchange flags returned by the server for invalid flags, having
8419 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8422 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8424 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8426 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8428 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8429 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8431 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8435 return -NFS4ERR_INVAL;
8439 nfs41_same_server_scope(struct nfs41_server_scope *a,
8440 struct nfs41_server_scope *b)
8442 if (a->server_scope_sz != b->server_scope_sz)
8444 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8448 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8450 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8451 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8452 struct nfs_client *clp = args->client;
8454 switch (task->tk_status) {
8455 case -NFS4ERR_BADSESSION:
8456 case -NFS4ERR_DEADSESSION:
8457 nfs4_schedule_session_recovery(clp->cl_session,
8461 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8462 res->dir != NFS4_CDFS4_BOTH) {
8463 rpc_task_close_connection(task);
8464 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8465 rpc_restart_call(task);
8469 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8470 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8474 * nfs4_proc_bind_one_conn_to_session()
8476 * The 4.1 client currently uses the same TCP connection for the
8477 * fore and backchannel.
8480 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8481 struct rpc_xprt *xprt,
8482 struct nfs_client *clp,
8483 const struct cred *cred)
8486 struct nfs41_bind_conn_to_session_args args = {
8488 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8491 struct nfs41_bind_conn_to_session_res res;
8492 struct rpc_message msg = {
8494 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8499 struct rpc_task_setup task_setup_data = {
8502 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8503 .rpc_message = &msg,
8504 .flags = RPC_TASK_TIMEOUT,
8506 struct rpc_task *task;
8508 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8509 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8510 args.dir = NFS4_CDFC4_FORE;
8512 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8513 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8514 args.dir = NFS4_CDFC4_FORE;
8516 task = rpc_run_task(&task_setup_data);
8517 if (!IS_ERR(task)) {
8518 status = task->tk_status;
8521 status = PTR_ERR(task);
8522 trace_nfs4_bind_conn_to_session(clp, status);
8524 if (memcmp(res.sessionid.data,
8525 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8526 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8529 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8530 dprintk("NFS: %s: Unexpected direction from server\n",
8534 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8535 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8544 struct rpc_bind_conn_calldata {
8545 struct nfs_client *clp;
8546 const struct cred *cred;
8550 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8551 struct rpc_xprt *xprt,
8554 struct rpc_bind_conn_calldata *p = calldata;
8556 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8559 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8561 struct rpc_bind_conn_calldata data = {
8565 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8566 nfs4_proc_bind_conn_to_session_callback, &data);
8570 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8571 * and operations we'd like to see to enable certain features in the allow map
8573 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8574 .how = SP4_MACH_CRED,
8575 .enforce.u.words = {
8576 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8577 1 << (OP_EXCHANGE_ID - 32) |
8578 1 << (OP_CREATE_SESSION - 32) |
8579 1 << (OP_DESTROY_SESSION - 32) |
8580 1 << (OP_DESTROY_CLIENTID - 32)
8583 [0] = 1 << (OP_CLOSE) |
8584 1 << (OP_OPEN_DOWNGRADE) |
8586 1 << (OP_DELEGRETURN) |
8588 [1] = 1 << (OP_SECINFO - 32) |
8589 1 << (OP_SECINFO_NO_NAME - 32) |
8590 1 << (OP_LAYOUTRETURN - 32) |
8591 1 << (OP_TEST_STATEID - 32) |
8592 1 << (OP_FREE_STATEID - 32) |
8593 1 << (OP_WRITE - 32)
8598 * Select the state protection mode for client `clp' given the server results
8599 * from exchange_id in `sp'.
8601 * Returns 0 on success, negative errno otherwise.
8603 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8604 struct nfs41_state_protection *sp)
8606 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8607 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8608 1 << (OP_EXCHANGE_ID - 32) |
8609 1 << (OP_CREATE_SESSION - 32) |
8610 1 << (OP_DESTROY_SESSION - 32) |
8611 1 << (OP_DESTROY_CLIENTID - 32)
8613 unsigned long flags = 0;
8617 if (sp->how == SP4_MACH_CRED) {
8618 /* Print state protect result */
8619 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8620 for (i = 0; i <= LAST_NFS4_OP; i++) {
8621 if (test_bit(i, sp->enforce.u.longs))
8622 dfprintk(MOUNT, " enforce op %d\n", i);
8623 if (test_bit(i, sp->allow.u.longs))
8624 dfprintk(MOUNT, " allow op %d\n", i);
8627 /* make sure nothing is on enforce list that isn't supported */
8628 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8629 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8630 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8637 * Minimal mode - state operations are allowed to use machine
8638 * credential. Note this already happens by default, so the
8639 * client doesn't have to do anything more than the negotiation.
8641 * NOTE: we don't care if EXCHANGE_ID is in the list -
8642 * we're already using the machine cred for exchange_id
8643 * and will never use a different cred.
8645 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8646 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8647 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8648 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8649 dfprintk(MOUNT, "sp4_mach_cred:\n");
8650 dfprintk(MOUNT, " minimal mode enabled\n");
8651 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8653 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8658 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8659 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8660 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8661 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8662 dfprintk(MOUNT, " cleanup mode enabled\n");
8663 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8666 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8667 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8668 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8671 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8672 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8673 dfprintk(MOUNT, " secinfo mode enabled\n");
8674 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8677 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8678 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8679 dfprintk(MOUNT, " stateid mode enabled\n");
8680 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8683 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8684 dfprintk(MOUNT, " write mode enabled\n");
8685 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8688 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8689 dfprintk(MOUNT, " commit mode enabled\n");
8690 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8694 clp->cl_sp4_flags = flags;
8698 struct nfs41_exchange_id_data {
8699 struct nfs41_exchange_id_res res;
8700 struct nfs41_exchange_id_args args;
8703 static void nfs4_exchange_id_release(void *data)
8705 struct nfs41_exchange_id_data *cdata =
8706 (struct nfs41_exchange_id_data *)data;
8708 nfs_put_client(cdata->args.client);
8709 kfree(cdata->res.impl_id);
8710 kfree(cdata->res.server_scope);
8711 kfree(cdata->res.server_owner);
8715 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8716 .rpc_release = nfs4_exchange_id_release,
8720 * _nfs4_proc_exchange_id()
8722 * Wrapper for EXCHANGE_ID operation.
8724 static struct rpc_task *
8725 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8726 u32 sp4_how, struct rpc_xprt *xprt)
8728 struct rpc_message msg = {
8729 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8732 struct rpc_task_setup task_setup_data = {
8733 .rpc_client = clp->cl_rpcclient,
8734 .callback_ops = &nfs4_exchange_id_call_ops,
8735 .rpc_message = &msg,
8736 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8738 struct nfs41_exchange_id_data *calldata;
8741 if (!refcount_inc_not_zero(&clp->cl_count))
8742 return ERR_PTR(-EIO);
8745 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8749 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8751 status = nfs4_init_uniform_client_string(clp);
8755 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8758 if (unlikely(calldata->res.server_owner == NULL))
8761 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8763 if (unlikely(calldata->res.server_scope == NULL))
8764 goto out_server_owner;
8766 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8767 if (unlikely(calldata->res.impl_id == NULL))
8768 goto out_server_scope;
8772 calldata->args.state_protect.how = SP4_NONE;
8776 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8786 task_setup_data.rpc_xprt = xprt;
8787 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8788 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8789 sizeof(calldata->args.verifier.data));
8791 calldata->args.client = clp;
8792 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8793 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8794 #ifdef CONFIG_NFS_V4_1_MIGRATION
8795 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8797 if (test_bit(NFS_CS_DS, &clp->cl_flags))
8798 calldata->args.flags |= EXCHGID4_FLAG_USE_PNFS_DS;
8799 msg.rpc_argp = &calldata->args;
8800 msg.rpc_resp = &calldata->res;
8801 task_setup_data.callback_data = calldata;
8803 return rpc_run_task(&task_setup_data);
8806 kfree(calldata->res.impl_id);
8808 kfree(calldata->res.server_scope);
8810 kfree(calldata->res.server_owner);
8814 nfs_put_client(clp);
8815 return ERR_PTR(status);
8819 * _nfs4_proc_exchange_id()
8821 * Wrapper for EXCHANGE_ID operation.
8823 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8826 struct rpc_task *task;
8827 struct nfs41_exchange_id_args *argp;
8828 struct nfs41_exchange_id_res *resp;
8829 unsigned long now = jiffies;
8832 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8834 return PTR_ERR(task);
8836 argp = task->tk_msg.rpc_argp;
8837 resp = task->tk_msg.rpc_resp;
8838 status = task->tk_status;
8842 status = nfs4_check_cl_exchange_flags(resp->flags,
8843 clp->cl_mvops->minor_version);
8847 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8851 do_renew_lease(clp, now);
8853 clp->cl_clientid = resp->clientid;
8854 clp->cl_exchange_flags = resp->flags;
8855 clp->cl_seqid = resp->seqid;
8856 /* Client ID is not confirmed */
8857 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8858 clear_bit(NFS4_SESSION_ESTABLISHED,
8859 &clp->cl_session->session_state);
8861 if (clp->cl_serverscope != NULL &&
8862 !nfs41_same_server_scope(clp->cl_serverscope,
8863 resp->server_scope)) {
8864 dprintk("%s: server_scope mismatch detected\n",
8866 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8869 swap(clp->cl_serverowner, resp->server_owner);
8870 swap(clp->cl_serverscope, resp->server_scope);
8871 swap(clp->cl_implid, resp->impl_id);
8873 /* Save the EXCHANGE_ID verifier session trunk tests */
8874 memcpy(clp->cl_confirm.data, argp->verifier.data,
8875 sizeof(clp->cl_confirm.data));
8877 trace_nfs4_exchange_id(clp, status);
8883 * nfs4_proc_exchange_id()
8885 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8887 * Since the clientid has expired, all compounds using sessions
8888 * associated with the stale clientid will be returning
8889 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8890 * be in some phase of session reset.
8892 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8894 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8896 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8899 /* try SP4_MACH_CRED if krb5i/p */
8900 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8901 authflavor == RPC_AUTH_GSS_KRB5P) {
8902 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8908 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8912 * nfs4_test_session_trunk
8914 * This is an add_xprt_test() test function called from
8915 * rpc_clnt_setup_test_and_add_xprt.
8917 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8918 * and is dereferrenced in nfs4_exchange_id_release
8920 * Upon success, add the new transport to the rpc_clnt
8922 * @clnt: struct rpc_clnt to get new transport
8923 * @xprt: the rpc_xprt to test
8924 * @data: call data for _nfs4_proc_exchange_id.
8926 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8929 struct nfs4_add_xprt_data *adata = data;
8930 struct rpc_task *task;
8935 dprintk("--> %s try %s\n", __func__,
8936 xprt->address_strings[RPC_DISPLAY_ADDR]);
8938 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8941 /* Test connection for session trunking. Async exchange_id call */
8942 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8946 status = task->tk_status;
8948 status = nfs4_detect_session_trunking(adata->clp,
8949 task->tk_msg.rpc_resp, xprt);
8952 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8953 else if (status != -NFS4ERR_DELAY && rpc_clnt_xprt_switch_has_addr(clnt,
8954 (struct sockaddr *)&xprt->addr))
8955 rpc_clnt_xprt_switch_remove_xprt(clnt, xprt);
8958 if (status == -NFS4ERR_DELAY) {
8963 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8965 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8966 const struct cred *cred)
8968 struct rpc_message msg = {
8969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8975 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8976 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8977 trace_nfs4_destroy_clientid(clp, status);
8979 dprintk("NFS: Got error %d from the server %s on "
8980 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8984 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8985 const struct cred *cred)
8990 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8991 ret = _nfs4_proc_destroy_clientid(clp, cred);
8993 case -NFS4ERR_DELAY:
8994 case -NFS4ERR_CLIENTID_BUSY:
9004 int nfs4_destroy_clientid(struct nfs_client *clp)
9006 const struct cred *cred;
9009 if (clp->cl_mvops->minor_version < 1)
9011 if (clp->cl_exchange_flags == 0)
9013 if (clp->cl_preserve_clid)
9015 cred = nfs4_get_clid_cred(clp);
9016 ret = nfs4_proc_destroy_clientid(clp, cred);
9020 case -NFS4ERR_STALE_CLIENTID:
9021 clp->cl_exchange_flags = 0;
9027 #endif /* CONFIG_NFS_V4_1 */
9029 struct nfs4_get_lease_time_data {
9030 struct nfs4_get_lease_time_args *args;
9031 struct nfs4_get_lease_time_res *res;
9032 struct nfs_client *clp;
9035 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
9038 struct nfs4_get_lease_time_data *data =
9039 (struct nfs4_get_lease_time_data *)calldata;
9041 /* just setup sequence, do not trigger session recovery
9042 since we're invoked within one */
9043 nfs4_setup_sequence(data->clp,
9044 &data->args->la_seq_args,
9045 &data->res->lr_seq_res,
9050 * Called from nfs4_state_manager thread for session setup, so don't recover
9051 * from sequence operation or clientid errors.
9053 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
9055 struct nfs4_get_lease_time_data *data =
9056 (struct nfs4_get_lease_time_data *)calldata;
9058 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
9060 switch (task->tk_status) {
9061 case -NFS4ERR_DELAY:
9062 case -NFS4ERR_GRACE:
9063 rpc_delay(task, NFS4_POLL_RETRY_MIN);
9064 task->tk_status = 0;
9066 case -NFS4ERR_RETRY_UNCACHED_REP:
9067 rpc_restart_call_prepare(task);
9072 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
9073 .rpc_call_prepare = nfs4_get_lease_time_prepare,
9074 .rpc_call_done = nfs4_get_lease_time_done,
9077 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
9079 struct nfs4_get_lease_time_args args;
9080 struct nfs4_get_lease_time_res res = {
9081 .lr_fsinfo = fsinfo,
9083 struct nfs4_get_lease_time_data data = {
9088 struct rpc_message msg = {
9089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
9093 struct rpc_task_setup task_setup = {
9094 .rpc_client = clp->cl_rpcclient,
9095 .rpc_message = &msg,
9096 .callback_ops = &nfs4_get_lease_time_ops,
9097 .callback_data = &data,
9098 .flags = RPC_TASK_TIMEOUT,
9101 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
9102 return nfs4_call_sync_custom(&task_setup);
9105 #ifdef CONFIG_NFS_V4_1
9108 * Initialize the values to be used by the client in CREATE_SESSION
9109 * If nfs4_init_session set the fore channel request and response sizes,
9112 * Set the back channel max_resp_sz_cached to zero to force the client to
9113 * always set csa_cachethis to FALSE because the current implementation
9114 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
9116 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
9117 struct rpc_clnt *clnt)
9119 unsigned int max_rqst_sz, max_resp_sz;
9120 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
9121 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
9123 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
9124 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
9126 /* Fore channel attributes */
9127 args->fc_attrs.max_rqst_sz = max_rqst_sz;
9128 args->fc_attrs.max_resp_sz = max_resp_sz;
9129 args->fc_attrs.max_ops = NFS4_MAX_OPS;
9130 args->fc_attrs.max_reqs = max_session_slots;
9132 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
9133 "max_ops=%u max_reqs=%u\n",
9135 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
9136 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
9138 /* Back channel attributes */
9139 args->bc_attrs.max_rqst_sz = max_bc_payload;
9140 args->bc_attrs.max_resp_sz = max_bc_payload;
9141 args->bc_attrs.max_resp_sz_cached = 0;
9142 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
9143 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
9144 if (args->bc_attrs.max_reqs > max_bc_slots)
9145 args->bc_attrs.max_reqs = max_bc_slots;
9147 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
9148 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
9150 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
9151 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
9152 args->bc_attrs.max_reqs);
9155 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
9156 struct nfs41_create_session_res *res)
9158 struct nfs4_channel_attrs *sent = &args->fc_attrs;
9159 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
9161 if (rcvd->max_resp_sz > sent->max_resp_sz)
9164 * Our requested max_ops is the minimum we need; we're not
9165 * prepared to break up compounds into smaller pieces than that.
9166 * So, no point even trying to continue if the server won't
9169 if (rcvd->max_ops < sent->max_ops)
9171 if (rcvd->max_reqs == 0)
9173 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
9174 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
9178 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
9179 struct nfs41_create_session_res *res)
9181 struct nfs4_channel_attrs *sent = &args->bc_attrs;
9182 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
9184 if (!(res->flags & SESSION4_BACK_CHAN))
9186 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
9188 if (rcvd->max_resp_sz < sent->max_resp_sz)
9190 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
9192 if (rcvd->max_ops > sent->max_ops)
9194 if (rcvd->max_reqs > sent->max_reqs)
9200 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
9201 struct nfs41_create_session_res *res)
9205 ret = nfs4_verify_fore_channel_attrs(args, res);
9208 return nfs4_verify_back_channel_attrs(args, res);
9211 static void nfs4_update_session(struct nfs4_session *session,
9212 struct nfs41_create_session_res *res)
9214 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
9215 /* Mark client id and session as being confirmed */
9216 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
9217 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
9218 session->flags = res->flags;
9219 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
9220 if (res->flags & SESSION4_BACK_CHAN)
9221 memcpy(&session->bc_attrs, &res->bc_attrs,
9222 sizeof(session->bc_attrs));
9225 static int _nfs4_proc_create_session(struct nfs_client *clp,
9226 const struct cred *cred)
9228 struct nfs4_session *session = clp->cl_session;
9229 struct nfs41_create_session_args args = {
9231 .clientid = clp->cl_clientid,
9232 .seqid = clp->cl_seqid,
9233 .cb_program = NFS4_CALLBACK,
9235 struct nfs41_create_session_res res;
9237 struct rpc_message msg = {
9238 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9245 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
9246 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
9248 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9249 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9250 trace_nfs4_create_session(clp, status);
9253 case -NFS4ERR_STALE_CLIENTID:
9254 case -NFS4ERR_DELAY:
9263 /* Verify the session's negotiated channel_attrs values */
9264 status = nfs4_verify_channel_attrs(&args, &res);
9265 /* Increment the clientid slot sequence id */
9268 nfs4_update_session(session, &res);
9275 * Issues a CREATE_SESSION operation to the server.
9276 * It is the responsibility of the caller to verify the session is
9277 * expired before calling this routine.
9279 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9283 struct nfs4_session *session = clp->cl_session;
9284 struct nfs4_add_xprt_data xprtdata = {
9287 struct rpc_add_xprt_test rpcdata = {
9288 .add_xprt_test = clp->cl_mvops->session_trunk,
9292 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9294 status = _nfs4_proc_create_session(clp, cred);
9298 /* Init or reset the session slot tables */
9299 status = nfs4_setup_session_slot_tables(session);
9300 dprintk("slot table setup returned %d\n", status);
9304 ptr = (unsigned *)&session->sess_id.data[0];
9305 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9306 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9307 rpc_clnt_probe_trunked_xprts(clp->cl_rpcclient, &rpcdata);
9313 * Issue the over-the-wire RPC DESTROY_SESSION.
9314 * The caller must serialize access to this routine.
9316 int nfs4_proc_destroy_session(struct nfs4_session *session,
9317 const struct cred *cred)
9319 struct rpc_message msg = {
9320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9321 .rpc_argp = session,
9326 /* session is still being setup */
9327 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9330 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9331 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9332 trace_nfs4_destroy_session(session->clp, status);
9335 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9336 "Session has been destroyed regardless...\n", status);
9337 rpc_clnt_manage_trunked_xprts(session->clp->cl_rpcclient);
9342 * Renew the cl_session lease.
9344 struct nfs4_sequence_data {
9345 struct nfs_client *clp;
9346 struct nfs4_sequence_args args;
9347 struct nfs4_sequence_res res;
9350 static void nfs41_sequence_release(void *data)
9352 struct nfs4_sequence_data *calldata = data;
9353 struct nfs_client *clp = calldata->clp;
9355 if (refcount_read(&clp->cl_count) > 1)
9356 nfs4_schedule_state_renewal(clp);
9357 nfs_put_client(clp);
9361 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9363 switch(task->tk_status) {
9364 case -NFS4ERR_DELAY:
9365 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9368 nfs4_schedule_lease_recovery(clp);
9373 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9375 struct nfs4_sequence_data *calldata = data;
9376 struct nfs_client *clp = calldata->clp;
9378 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9381 trace_nfs4_sequence(clp, task->tk_status);
9382 if (task->tk_status < 0 && !task->tk_client->cl_shutdown) {
9383 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9384 if (refcount_read(&clp->cl_count) == 1)
9387 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9388 rpc_restart_call_prepare(task);
9392 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9395 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9397 struct nfs4_sequence_data *calldata = data;
9398 struct nfs_client *clp = calldata->clp;
9399 struct nfs4_sequence_args *args;
9400 struct nfs4_sequence_res *res;
9402 args = task->tk_msg.rpc_argp;
9403 res = task->tk_msg.rpc_resp;
9405 nfs4_setup_sequence(clp, args, res, task);
9408 static const struct rpc_call_ops nfs41_sequence_ops = {
9409 .rpc_call_done = nfs41_sequence_call_done,
9410 .rpc_call_prepare = nfs41_sequence_prepare,
9411 .rpc_release = nfs41_sequence_release,
9414 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9415 const struct cred *cred,
9416 struct nfs4_slot *slot,
9419 struct nfs4_sequence_data *calldata;
9420 struct rpc_message msg = {
9421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9424 struct rpc_task_setup task_setup_data = {
9425 .rpc_client = clp->cl_rpcclient,
9426 .rpc_message = &msg,
9427 .callback_ops = &nfs41_sequence_ops,
9428 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
9430 struct rpc_task *ret;
9432 ret = ERR_PTR(-EIO);
9433 if (!refcount_inc_not_zero(&clp->cl_count))
9436 ret = ERR_PTR(-ENOMEM);
9437 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
9438 if (calldata == NULL)
9440 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9441 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9442 msg.rpc_argp = &calldata->args;
9443 msg.rpc_resp = &calldata->res;
9444 calldata->clp = clp;
9445 task_setup_data.callback_data = calldata;
9447 ret = rpc_run_task(&task_setup_data);
9452 nfs_put_client(clp);
9454 nfs41_release_slot(slot);
9458 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9460 struct rpc_task *task;
9463 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9465 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9467 ret = PTR_ERR(task);
9469 rpc_put_task_async(task);
9470 dprintk("<-- %s status=%d\n", __func__, ret);
9474 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9476 struct rpc_task *task;
9479 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9481 ret = PTR_ERR(task);
9484 ret = rpc_wait_for_completion_task(task);
9486 ret = task->tk_status;
9489 dprintk("<-- %s status=%d\n", __func__, ret);
9493 struct nfs4_reclaim_complete_data {
9494 struct nfs_client *clp;
9495 struct nfs41_reclaim_complete_args arg;
9496 struct nfs41_reclaim_complete_res res;
9499 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9501 struct nfs4_reclaim_complete_data *calldata = data;
9503 nfs4_setup_sequence(calldata->clp,
9504 &calldata->arg.seq_args,
9505 &calldata->res.seq_res,
9509 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9511 switch(task->tk_status) {
9513 wake_up_all(&clp->cl_lock_waitq);
9515 case -NFS4ERR_COMPLETE_ALREADY:
9516 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9518 case -NFS4ERR_DELAY:
9519 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9521 case -NFS4ERR_RETRY_UNCACHED_REP:
9523 dprintk("%s: failed to reclaim complete error %d for server %s, retrying\n",
9524 __func__, task->tk_status, clp->cl_hostname);
9526 case -NFS4ERR_BADSESSION:
9527 case -NFS4ERR_DEADSESSION:
9528 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9531 nfs4_schedule_lease_recovery(clp);
9536 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9538 struct nfs4_reclaim_complete_data *calldata = data;
9539 struct nfs_client *clp = calldata->clp;
9540 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9542 if (!nfs41_sequence_done(task, res))
9545 trace_nfs4_reclaim_complete(clp, task->tk_status);
9546 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9547 rpc_restart_call_prepare(task);
9552 static void nfs4_free_reclaim_complete_data(void *data)
9554 struct nfs4_reclaim_complete_data *calldata = data;
9559 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9560 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9561 .rpc_call_done = nfs4_reclaim_complete_done,
9562 .rpc_release = nfs4_free_reclaim_complete_data,
9566 * Issue a global reclaim complete.
9568 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9569 const struct cred *cred)
9571 struct nfs4_reclaim_complete_data *calldata;
9572 struct rpc_message msg = {
9573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9576 struct rpc_task_setup task_setup_data = {
9577 .rpc_client = clp->cl_rpcclient,
9578 .rpc_message = &msg,
9579 .callback_ops = &nfs4_reclaim_complete_call_ops,
9580 .flags = RPC_TASK_NO_ROUND_ROBIN,
9582 int status = -ENOMEM;
9584 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9585 if (calldata == NULL)
9587 calldata->clp = clp;
9588 calldata->arg.one_fs = 0;
9590 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9591 msg.rpc_argp = &calldata->arg;
9592 msg.rpc_resp = &calldata->res;
9593 task_setup_data.callback_data = calldata;
9594 status = nfs4_call_sync_custom(&task_setup_data);
9596 dprintk("<-- %s status=%d\n", __func__, status);
9601 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9603 struct nfs4_layoutget *lgp = calldata;
9604 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9606 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9607 &lgp->res.seq_res, task);
9610 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9612 struct nfs4_layoutget *lgp = calldata;
9614 nfs41_sequence_process(task, &lgp->res.seq_res);
9618 nfs4_layoutget_handle_exception(struct rpc_task *task,
9619 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9621 struct inode *inode = lgp->args.inode;
9622 struct nfs_server *server = NFS_SERVER(inode);
9623 struct pnfs_layout_hdr *lo = lgp->lo;
9624 int nfs4err = task->tk_status;
9625 int err, status = 0;
9628 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9630 nfs4_sequence_free_slot(&lgp->res.seq_res);
9637 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9638 * on the file. set tk_status to -ENODATA to tell upper layer to
9641 case -NFS4ERR_LAYOUTUNAVAILABLE:
9645 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9646 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9648 case -NFS4ERR_BADLAYOUT:
9649 status = -EOVERFLOW;
9652 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9653 * (or clients) writing to the same RAID stripe except when
9654 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9656 * Treat it like we would RECALLCONFLICT -- we retry for a little
9657 * while, and then eventually give up.
9659 case -NFS4ERR_LAYOUTTRYLATER:
9660 if (lgp->args.minlength == 0) {
9661 status = -EOVERFLOW;
9666 case -NFS4ERR_RECALLCONFLICT:
9667 case -NFS4ERR_RETURNCONFLICT:
9668 status = -ERECALLCONFLICT;
9670 case -NFS4ERR_DELEG_REVOKED:
9671 case -NFS4ERR_ADMIN_REVOKED:
9672 case -NFS4ERR_EXPIRED:
9673 case -NFS4ERR_BAD_STATEID:
9674 exception->timeout = 0;
9675 spin_lock(&inode->i_lock);
9676 /* If the open stateid was bad, then recover it. */
9677 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9678 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9679 spin_unlock(&inode->i_lock);
9680 exception->state = lgp->args.ctx->state;
9681 exception->stateid = &lgp->args.stateid;
9686 * Mark the bad layout state as invalid, then retry
9688 pnfs_mark_layout_stateid_invalid(lo, &head);
9689 spin_unlock(&inode->i_lock);
9690 nfs_commit_inode(inode, 0);
9691 pnfs_free_lseg_list(&head);
9696 err = nfs4_handle_exception(server, nfs4err, exception);
9698 if (exception->retry)
9707 size_t max_response_pages(struct nfs_server *server)
9709 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9710 return nfs_page_array_len(0, max_resp_sz);
9713 static void nfs4_layoutget_release(void *calldata)
9715 struct nfs4_layoutget *lgp = calldata;
9717 nfs4_sequence_free_slot(&lgp->res.seq_res);
9718 pnfs_layoutget_free(lgp);
9721 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9722 .rpc_call_prepare = nfs4_layoutget_prepare,
9723 .rpc_call_done = nfs4_layoutget_done,
9724 .rpc_release = nfs4_layoutget_release,
9727 struct pnfs_layout_segment *
9728 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9730 struct inode *inode = lgp->args.inode;
9731 struct nfs_server *server = NFS_SERVER(inode);
9732 struct rpc_task *task;
9733 struct rpc_message msg = {
9734 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9735 .rpc_argp = &lgp->args,
9736 .rpc_resp = &lgp->res,
9737 .rpc_cred = lgp->cred,
9739 struct rpc_task_setup task_setup_data = {
9740 .rpc_client = server->client,
9741 .rpc_message = &msg,
9742 .callback_ops = &nfs4_layoutget_call_ops,
9743 .callback_data = lgp,
9744 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
9747 struct pnfs_layout_segment *lseg = NULL;
9748 struct nfs4_exception exception = {
9750 .timeout = *timeout,
9754 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9756 task = rpc_run_task(&task_setup_data);
9758 return ERR_CAST(task);
9760 status = rpc_wait_for_completion_task(task);
9764 if (task->tk_status < 0) {
9765 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9766 *timeout = exception.timeout;
9767 } else if (lgp->res.layoutp->len == 0) {
9769 *timeout = nfs4_update_delay(&exception.timeout);
9771 lseg = pnfs_layout_process(lgp);
9773 trace_nfs4_layoutget(lgp->args.ctx,
9780 dprintk("<-- %s status=%d\n", __func__, status);
9782 return ERR_PTR(status);
9787 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9789 struct nfs4_layoutreturn *lrp = calldata;
9791 nfs4_setup_sequence(lrp->clp,
9792 &lrp->args.seq_args,
9795 if (!pnfs_layout_is_valid(lrp->args.layout))
9799 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9801 struct nfs4_layoutreturn *lrp = calldata;
9802 struct nfs_server *server;
9804 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9808 * Was there an RPC level error? Assume the call succeeded,
9809 * and that we need to release the layout
9811 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9812 lrp->res.lrs_present = 0;
9816 server = NFS_SERVER(lrp->args.inode);
9817 switch (task->tk_status) {
9818 case -NFS4ERR_OLD_STATEID:
9819 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9825 task->tk_status = 0;
9829 case -NFS4ERR_DELAY:
9830 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9836 task->tk_status = 0;
9837 nfs4_sequence_free_slot(&lrp->res.seq_res);
9838 rpc_restart_call_prepare(task);
9841 static void nfs4_layoutreturn_release(void *calldata)
9843 struct nfs4_layoutreturn *lrp = calldata;
9844 struct pnfs_layout_hdr *lo = lrp->args.layout;
9846 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9847 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9848 nfs4_sequence_free_slot(&lrp->res.seq_res);
9849 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9850 lrp->ld_private.ops->free(&lrp->ld_private);
9851 pnfs_put_layout_hdr(lrp->args.layout);
9852 nfs_iput_and_deactive(lrp->inode);
9853 put_cred(lrp->cred);
9857 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9858 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9859 .rpc_call_done = nfs4_layoutreturn_done,
9860 .rpc_release = nfs4_layoutreturn_release,
9863 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9865 struct rpc_task *task;
9866 struct rpc_message msg = {
9867 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9868 .rpc_argp = &lrp->args,
9869 .rpc_resp = &lrp->res,
9870 .rpc_cred = lrp->cred,
9872 struct rpc_task_setup task_setup_data = {
9873 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9874 .rpc_message = &msg,
9875 .callback_ops = &nfs4_layoutreturn_call_ops,
9876 .callback_data = lrp,
9877 .flags = RPC_TASK_MOVEABLE,
9881 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9882 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9883 &task_setup_data.rpc_client, &msg);
9885 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9888 nfs4_layoutreturn_release(lrp);
9891 task_setup_data.flags |= RPC_TASK_ASYNC;
9894 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9897 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9899 task = rpc_run_task(&task_setup_data);
9901 return PTR_ERR(task);
9903 status = task->tk_status;
9904 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9905 dprintk("<-- %s status=%d\n", __func__, status);
9911 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9912 struct pnfs_device *pdev,
9913 const struct cred *cred)
9915 struct nfs4_getdeviceinfo_args args = {
9917 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9918 NOTIFY_DEVICEID4_DELETE,
9920 struct nfs4_getdeviceinfo_res res = {
9923 struct rpc_message msg = {
9924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9931 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9932 if (res.notification & ~args.notify_types)
9933 dprintk("%s: unsupported notification\n", __func__);
9934 if (res.notification != args.notify_types)
9937 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9939 dprintk("<-- %s status=%d\n", __func__, status);
9944 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9945 struct pnfs_device *pdev,
9946 const struct cred *cred)
9948 struct nfs4_exception exception = { };
9952 err = nfs4_handle_exception(server,
9953 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9955 } while (exception.retry);
9958 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9960 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9962 struct nfs4_layoutcommit_data *data = calldata;
9963 struct nfs_server *server = NFS_SERVER(data->args.inode);
9965 nfs4_setup_sequence(server->nfs_client,
9966 &data->args.seq_args,
9972 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9974 struct nfs4_layoutcommit_data *data = calldata;
9975 struct nfs_server *server = NFS_SERVER(data->args.inode);
9977 if (!nfs41_sequence_done(task, &data->res.seq_res))
9980 switch (task->tk_status) { /* Just ignore these failures */
9981 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9982 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9983 case -NFS4ERR_BADLAYOUT: /* no layout */
9984 case -NFS4ERR_GRACE: /* loca_recalim always false */
9985 task->tk_status = 0;
9990 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9991 rpc_restart_call_prepare(task);
9997 static void nfs4_layoutcommit_release(void *calldata)
9999 struct nfs4_layoutcommit_data *data = calldata;
10001 pnfs_cleanup_layoutcommit(data);
10002 nfs_post_op_update_inode_force_wcc(data->args.inode,
10004 put_cred(data->cred);
10005 nfs_iput_and_deactive(data->inode);
10009 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
10010 .rpc_call_prepare = nfs4_layoutcommit_prepare,
10011 .rpc_call_done = nfs4_layoutcommit_done,
10012 .rpc_release = nfs4_layoutcommit_release,
10016 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
10018 struct rpc_message msg = {
10019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
10020 .rpc_argp = &data->args,
10021 .rpc_resp = &data->res,
10022 .rpc_cred = data->cred,
10024 struct rpc_task_setup task_setup_data = {
10025 .task = &data->task,
10026 .rpc_client = NFS_CLIENT(data->args.inode),
10027 .rpc_message = &msg,
10028 .callback_ops = &nfs4_layoutcommit_ops,
10029 .callback_data = data,
10030 .flags = RPC_TASK_MOVEABLE,
10032 struct rpc_task *task;
10035 dprintk("NFS: initiating layoutcommit call. sync %d "
10036 "lbw: %llu inode %lu\n", sync,
10037 data->args.lastbytewritten,
10038 data->args.inode->i_ino);
10041 data->inode = nfs_igrab_and_active(data->args.inode);
10042 if (data->inode == NULL) {
10043 nfs4_layoutcommit_release(data);
10046 task_setup_data.flags = RPC_TASK_ASYNC;
10048 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
10049 task = rpc_run_task(&task_setup_data);
10051 return PTR_ERR(task);
10053 status = task->tk_status;
10054 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
10055 dprintk("%s: status %d\n", __func__, status);
10056 rpc_put_task(task);
10061 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
10062 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
10065 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10066 struct nfs_fsinfo *info,
10067 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
10069 struct nfs41_secinfo_no_name_args args = {
10070 .style = SECINFO_STYLE_CURRENT_FH,
10072 struct nfs4_secinfo_res res = {
10073 .flavors = flavors,
10075 struct rpc_message msg = {
10076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
10080 struct nfs4_call_sync_data data = {
10081 .seq_server = server,
10082 .seq_args = &args.seq_args,
10083 .seq_res = &res.seq_res,
10085 struct rpc_task_setup task_setup = {
10086 .rpc_client = server->client,
10087 .rpc_message = &msg,
10088 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
10089 .callback_data = &data,
10090 .flags = RPC_TASK_NO_ROUND_ROBIN,
10092 const struct cred *cred = NULL;
10095 if (use_integrity) {
10096 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
10098 cred = nfs4_get_clid_cred(server->nfs_client);
10099 msg.rpc_cred = cred;
10102 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
10103 status = nfs4_call_sync_custom(&task_setup);
10104 dprintk("<-- %s status=%d\n", __func__, status);
10112 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10113 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
10115 struct nfs4_exception exception = {
10116 .interruptible = true,
10120 /* first try using integrity protection */
10121 err = -NFS4ERR_WRONGSEC;
10123 /* try to use integrity protection with machine cred */
10124 if (_nfs4_is_integrity_protected(server->nfs_client))
10125 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10129 * if unable to use integrity protection, or SECINFO with
10130 * integrity protection returns NFS4ERR_WRONGSEC (which is
10131 * disallowed by spec, but exists in deployed servers) use
10132 * the current filesystem's rpc_client and the user cred.
10134 if (err == -NFS4ERR_WRONGSEC)
10135 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10140 case -NFS4ERR_WRONGSEC:
10144 err = nfs4_handle_exception(server, err, &exception);
10146 } while (exception.retry);
10152 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
10153 struct nfs_fsinfo *info)
10157 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
10158 struct nfs4_secinfo_flavors *flavors;
10159 struct nfs4_secinfo4 *secinfo;
10162 page = alloc_page(GFP_KERNEL);
10168 flavors = page_address(page);
10169 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
10172 * Fall back on "guess and check" method if
10173 * the server doesn't support SECINFO_NO_NAME
10175 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
10176 err = nfs4_find_root_sec(server, fhandle, info);
10182 for (i = 0; i < flavors->num_flavors; i++) {
10183 secinfo = &flavors->flavors[i];
10185 switch (secinfo->flavor) {
10186 case RPC_AUTH_NULL:
10187 case RPC_AUTH_UNIX:
10189 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
10190 &secinfo->flavor_info);
10193 flavor = RPC_AUTH_MAXFLAVOR;
10197 if (!nfs_auth_info_match(&server->auth_info, flavor))
10198 flavor = RPC_AUTH_MAXFLAVOR;
10200 if (flavor != RPC_AUTH_MAXFLAVOR) {
10201 err = nfs4_lookup_root_sec(server, fhandle,
10208 if (flavor == RPC_AUTH_MAXFLAVOR)
10213 if (err == -EACCES)
10219 static int _nfs41_test_stateid(struct nfs_server *server,
10220 nfs4_stateid *stateid,
10221 const struct cred *cred)
10224 struct nfs41_test_stateid_args args = {
10225 .stateid = stateid,
10227 struct nfs41_test_stateid_res res;
10228 struct rpc_message msg = {
10229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10234 struct rpc_clnt *rpc_client = server->client;
10236 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10237 &rpc_client, &msg);
10239 dprintk("NFS call test_stateid %p\n", stateid);
10240 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
10241 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
10242 &args.seq_args, &res.seq_res);
10243 if (status != NFS_OK) {
10244 dprintk("NFS reply test_stateid: failed, %d\n", status);
10247 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10248 return -res.status;
10251 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10252 int err, struct nfs4_exception *exception)
10254 exception->retry = 0;
10256 case -NFS4ERR_DELAY:
10257 case -NFS4ERR_RETRY_UNCACHED_REP:
10258 nfs4_handle_exception(server, err, exception);
10260 case -NFS4ERR_BADSESSION:
10261 case -NFS4ERR_BADSLOT:
10262 case -NFS4ERR_BAD_HIGH_SLOT:
10263 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10264 case -NFS4ERR_DEADSESSION:
10265 nfs4_do_handle_exception(server, err, exception);
10270 * nfs41_test_stateid - perform a TEST_STATEID operation
10272 * @server: server / transport on which to perform the operation
10273 * @stateid: state ID to test
10274 * @cred: credential
10276 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10277 * Otherwise a negative NFS4ERR value is returned if the operation
10278 * failed or the state ID is not currently valid.
10280 static int nfs41_test_stateid(struct nfs_server *server,
10281 nfs4_stateid *stateid,
10282 const struct cred *cred)
10284 struct nfs4_exception exception = {
10285 .interruptible = true,
10289 err = _nfs41_test_stateid(server, stateid, cred);
10290 nfs4_handle_delay_or_session_error(server, err, &exception);
10291 } while (exception.retry);
10295 struct nfs_free_stateid_data {
10296 struct nfs_server *server;
10297 struct nfs41_free_stateid_args args;
10298 struct nfs41_free_stateid_res res;
10301 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10303 struct nfs_free_stateid_data *data = calldata;
10304 nfs4_setup_sequence(data->server->nfs_client,
10305 &data->args.seq_args,
10306 &data->res.seq_res,
10310 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10312 struct nfs_free_stateid_data *data = calldata;
10314 nfs41_sequence_done(task, &data->res.seq_res);
10316 switch (task->tk_status) {
10317 case -NFS4ERR_DELAY:
10318 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10319 rpc_restart_call_prepare(task);
10323 static void nfs41_free_stateid_release(void *calldata)
10325 struct nfs_free_stateid_data *data = calldata;
10326 struct nfs_client *clp = data->server->nfs_client;
10328 nfs_put_client(clp);
10332 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10333 .rpc_call_prepare = nfs41_free_stateid_prepare,
10334 .rpc_call_done = nfs41_free_stateid_done,
10335 .rpc_release = nfs41_free_stateid_release,
10339 * nfs41_free_stateid - perform a FREE_STATEID operation
10341 * @server: server / transport on which to perform the operation
10342 * @stateid: state ID to release
10343 * @cred: credential
10344 * @privileged: set to true if this call needs to be privileged
10346 * Note: this function is always asynchronous.
10348 static int nfs41_free_stateid(struct nfs_server *server,
10349 const nfs4_stateid *stateid,
10350 const struct cred *cred,
10353 struct rpc_message msg = {
10354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10357 struct rpc_task_setup task_setup = {
10358 .rpc_client = server->client,
10359 .rpc_message = &msg,
10360 .callback_ops = &nfs41_free_stateid_ops,
10361 .flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
10363 struct nfs_free_stateid_data *data;
10364 struct rpc_task *task;
10365 struct nfs_client *clp = server->nfs_client;
10367 if (!refcount_inc_not_zero(&clp->cl_count))
10370 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10371 &task_setup.rpc_client, &msg);
10373 dprintk("NFS call free_stateid %p\n", stateid);
10374 data = kmalloc(sizeof(*data), GFP_KERNEL);
10377 data->server = server;
10378 nfs4_stateid_copy(&data->args.stateid, stateid);
10380 task_setup.callback_data = data;
10382 msg.rpc_argp = &data->args;
10383 msg.rpc_resp = &data->res;
10384 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10385 task = rpc_run_task(&task_setup);
10387 return PTR_ERR(task);
10388 rpc_put_task(task);
10393 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10395 const struct cred *cred = lsp->ls_state->owner->so_cred;
10397 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10398 nfs4_free_lock_state(server, lsp);
10401 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10402 const nfs4_stateid *s2)
10404 if (s1->type != s2->type)
10407 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10410 if (s1->seqid == s2->seqid)
10413 return s1->seqid == 0 || s2->seqid == 0;
10416 #endif /* CONFIG_NFS_V4_1 */
10418 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10419 const nfs4_stateid *s2)
10421 return nfs4_stateid_match(s1, s2);
10425 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10426 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10427 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10428 .recover_open = nfs4_open_reclaim,
10429 .recover_lock = nfs4_lock_reclaim,
10430 .establish_clid = nfs4_init_clientid,
10431 .detect_trunking = nfs40_discover_server_trunking,
10434 #if defined(CONFIG_NFS_V4_1)
10435 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10436 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10437 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10438 .recover_open = nfs4_open_reclaim,
10439 .recover_lock = nfs4_lock_reclaim,
10440 .establish_clid = nfs41_init_clientid,
10441 .reclaim_complete = nfs41_proc_reclaim_complete,
10442 .detect_trunking = nfs41_discover_server_trunking,
10444 #endif /* CONFIG_NFS_V4_1 */
10446 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10447 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10448 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10449 .recover_open = nfs40_open_expired,
10450 .recover_lock = nfs4_lock_expired,
10451 .establish_clid = nfs4_init_clientid,
10454 #if defined(CONFIG_NFS_V4_1)
10455 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10456 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10457 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10458 .recover_open = nfs41_open_expired,
10459 .recover_lock = nfs41_lock_expired,
10460 .establish_clid = nfs41_init_clientid,
10462 #endif /* CONFIG_NFS_V4_1 */
10464 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10465 .sched_state_renewal = nfs4_proc_async_renew,
10466 .get_state_renewal_cred = nfs4_get_renew_cred,
10467 .renew_lease = nfs4_proc_renew,
10470 #if defined(CONFIG_NFS_V4_1)
10471 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10472 .sched_state_renewal = nfs41_proc_async_sequence,
10473 .get_state_renewal_cred = nfs4_get_machine_cred,
10474 .renew_lease = nfs4_proc_sequence,
10478 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10479 .get_locations = _nfs40_proc_get_locations,
10480 .fsid_present = _nfs40_proc_fsid_present,
10483 #if defined(CONFIG_NFS_V4_1)
10484 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10485 .get_locations = _nfs41_proc_get_locations,
10486 .fsid_present = _nfs41_proc_fsid_present,
10488 #endif /* CONFIG_NFS_V4_1 */
10490 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10491 .minor_version = 0,
10492 .init_caps = NFS_CAP_READDIRPLUS
10493 | NFS_CAP_ATOMIC_OPEN
10494 | NFS_CAP_POSIX_LOCK,
10495 .init_client = nfs40_init_client,
10496 .shutdown_client = nfs40_shutdown_client,
10497 .match_stateid = nfs4_match_stateid,
10498 .find_root_sec = nfs4_find_root_sec,
10499 .free_lock_state = nfs4_release_lockowner,
10500 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10501 .alloc_seqid = nfs_alloc_seqid,
10502 .call_sync_ops = &nfs40_call_sync_ops,
10503 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10504 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10505 .state_renewal_ops = &nfs40_state_renewal_ops,
10506 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10509 #if defined(CONFIG_NFS_V4_1)
10510 static struct nfs_seqid *
10511 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10516 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10517 .minor_version = 1,
10518 .init_caps = NFS_CAP_READDIRPLUS
10519 | NFS_CAP_ATOMIC_OPEN
10520 | NFS_CAP_POSIX_LOCK
10521 | NFS_CAP_STATEID_NFSV41
10522 | NFS_CAP_ATOMIC_OPEN_V1
10524 | NFS_CAP_MOVEABLE,
10525 .init_client = nfs41_init_client,
10526 .shutdown_client = nfs41_shutdown_client,
10527 .match_stateid = nfs41_match_stateid,
10528 .find_root_sec = nfs41_find_root_sec,
10529 .free_lock_state = nfs41_free_lock_state,
10530 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10531 .alloc_seqid = nfs_alloc_no_seqid,
10532 .session_trunk = nfs4_test_session_trunk,
10533 .call_sync_ops = &nfs41_call_sync_ops,
10534 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10535 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10536 .state_renewal_ops = &nfs41_state_renewal_ops,
10537 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10541 #if defined(CONFIG_NFS_V4_2)
10542 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10543 .minor_version = 2,
10544 .init_caps = NFS_CAP_READDIRPLUS
10545 | NFS_CAP_ATOMIC_OPEN
10546 | NFS_CAP_POSIX_LOCK
10547 | NFS_CAP_STATEID_NFSV41
10548 | NFS_CAP_ATOMIC_OPEN_V1
10552 | NFS_CAP_OFFLOAD_CANCEL
10553 | NFS_CAP_COPY_NOTIFY
10554 | NFS_CAP_DEALLOCATE
10556 | NFS_CAP_LAYOUTSTATS
10558 | NFS_CAP_LAYOUTERROR
10559 | NFS_CAP_READ_PLUS
10560 | NFS_CAP_MOVEABLE,
10561 .init_client = nfs41_init_client,
10562 .shutdown_client = nfs41_shutdown_client,
10563 .match_stateid = nfs41_match_stateid,
10564 .find_root_sec = nfs41_find_root_sec,
10565 .free_lock_state = nfs41_free_lock_state,
10566 .call_sync_ops = &nfs41_call_sync_ops,
10567 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10568 .alloc_seqid = nfs_alloc_no_seqid,
10569 .session_trunk = nfs4_test_session_trunk,
10570 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10571 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10572 .state_renewal_ops = &nfs41_state_renewal_ops,
10573 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10577 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10578 [0] = &nfs_v4_0_minor_ops,
10579 #if defined(CONFIG_NFS_V4_1)
10580 [1] = &nfs_v4_1_minor_ops,
10582 #if defined(CONFIG_NFS_V4_2)
10583 [2] = &nfs_v4_2_minor_ops,
10587 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10589 ssize_t error, error2, error3;
10591 error = generic_listxattr(dentry, list, size);
10599 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10608 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10612 return error + error2 + error3;
10615 static void nfs4_enable_swap(struct inode *inode)
10617 /* The state manager thread must always be running.
10618 * It will notice the client is a swapper, and stay put.
10620 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10622 nfs4_schedule_state_manager(clp);
10625 static void nfs4_disable_swap(struct inode *inode)
10627 /* The state manager thread will now exit once it is
10630 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10632 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
10633 clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
10634 wake_up_var(&clp->cl_state);
10637 static const struct inode_operations nfs4_dir_inode_operations = {
10638 .create = nfs_create,
10639 .lookup = nfs_lookup,
10640 .atomic_open = nfs_atomic_open,
10642 .unlink = nfs_unlink,
10643 .symlink = nfs_symlink,
10644 .mkdir = nfs_mkdir,
10645 .rmdir = nfs_rmdir,
10646 .mknod = nfs_mknod,
10647 .rename = nfs_rename,
10648 .permission = nfs_permission,
10649 .getattr = nfs_getattr,
10650 .setattr = nfs_setattr,
10651 .listxattr = nfs4_listxattr,
10654 static const struct inode_operations nfs4_file_inode_operations = {
10655 .permission = nfs_permission,
10656 .getattr = nfs_getattr,
10657 .setattr = nfs_setattr,
10658 .listxattr = nfs4_listxattr,
10661 const struct nfs_rpc_ops nfs_v4_clientops = {
10662 .version = 4, /* protocol version */
10663 .dentry_ops = &nfs4_dentry_operations,
10664 .dir_inode_ops = &nfs4_dir_inode_operations,
10665 .file_inode_ops = &nfs4_file_inode_operations,
10666 .file_ops = &nfs4_file_operations,
10667 .getroot = nfs4_proc_get_root,
10668 .submount = nfs4_submount,
10669 .try_get_tree = nfs4_try_get_tree,
10670 .getattr = nfs4_proc_getattr,
10671 .setattr = nfs4_proc_setattr,
10672 .lookup = nfs4_proc_lookup,
10673 .lookupp = nfs4_proc_lookupp,
10674 .access = nfs4_proc_access,
10675 .readlink = nfs4_proc_readlink,
10676 .create = nfs4_proc_create,
10677 .remove = nfs4_proc_remove,
10678 .unlink_setup = nfs4_proc_unlink_setup,
10679 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10680 .unlink_done = nfs4_proc_unlink_done,
10681 .rename_setup = nfs4_proc_rename_setup,
10682 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10683 .rename_done = nfs4_proc_rename_done,
10684 .link = nfs4_proc_link,
10685 .symlink = nfs4_proc_symlink,
10686 .mkdir = nfs4_proc_mkdir,
10687 .rmdir = nfs4_proc_rmdir,
10688 .readdir = nfs4_proc_readdir,
10689 .mknod = nfs4_proc_mknod,
10690 .statfs = nfs4_proc_statfs,
10691 .fsinfo = nfs4_proc_fsinfo,
10692 .pathconf = nfs4_proc_pathconf,
10693 .set_capabilities = nfs4_server_capabilities,
10694 .decode_dirent = nfs4_decode_dirent,
10695 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10696 .read_setup = nfs4_proc_read_setup,
10697 .read_done = nfs4_read_done,
10698 .write_setup = nfs4_proc_write_setup,
10699 .write_done = nfs4_write_done,
10700 .commit_setup = nfs4_proc_commit_setup,
10701 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10702 .commit_done = nfs4_commit_done,
10703 .lock = nfs4_proc_lock,
10704 .clear_acl_cache = nfs4_zap_acl_attr,
10705 .close_context = nfs4_close_context,
10706 .open_context = nfs4_atomic_open,
10707 .have_delegation = nfs4_have_delegation,
10708 .alloc_client = nfs4_alloc_client,
10709 .init_client = nfs4_init_client,
10710 .free_client = nfs4_free_client,
10711 .create_server = nfs4_create_server,
10712 .clone_server = nfs_clone_server,
10713 .discover_trunking = nfs4_discover_trunking,
10714 .enable_swap = nfs4_enable_swap,
10715 .disable_swap = nfs4_disable_swap,
10718 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10719 .name = XATTR_NAME_NFSV4_ACL,
10720 .list = nfs4_xattr_list_nfs4_acl,
10721 .get = nfs4_xattr_get_nfs4_acl,
10722 .set = nfs4_xattr_set_nfs4_acl,
10725 #if defined(CONFIG_NFS_V4_1)
10726 static const struct xattr_handler nfs4_xattr_nfs4_dacl_handler = {
10727 .name = XATTR_NAME_NFSV4_DACL,
10728 .list = nfs4_xattr_list_nfs4_dacl,
10729 .get = nfs4_xattr_get_nfs4_dacl,
10730 .set = nfs4_xattr_set_nfs4_dacl,
10733 static const struct xattr_handler nfs4_xattr_nfs4_sacl_handler = {
10734 .name = XATTR_NAME_NFSV4_SACL,
10735 .list = nfs4_xattr_list_nfs4_sacl,
10736 .get = nfs4_xattr_get_nfs4_sacl,
10737 .set = nfs4_xattr_set_nfs4_sacl,
10741 #ifdef CONFIG_NFS_V4_2
10742 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10743 .prefix = XATTR_USER_PREFIX,
10744 .get = nfs4_xattr_get_nfs4_user,
10745 .set = nfs4_xattr_set_nfs4_user,
10749 const struct xattr_handler *nfs4_xattr_handlers[] = {
10750 &nfs4_xattr_nfs4_acl_handler,
10751 #if defined(CONFIG_NFS_V4_1)
10752 &nfs4_xattr_nfs4_dacl_handler,
10753 &nfs4_xattr_nfs4_sacl_handler,
10755 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10756 &nfs4_xattr_nfs4_label_handler,
10758 #ifdef CONFIG_NFS_V4_2
10759 &nfs4_xattr_nfs4_user_handler,