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)
125 err = security_dentry_init_security(dentry, sattr->ia_mode,
126 &dentry->d_name, NULL,
127 (void **)&label->label, &label->len);
134 nfs4_label_release_security(struct nfs4_label *label)
137 security_release_secctx(label->label, label->len);
139 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
142 return server->attr_bitmask;
144 return server->attr_bitmask_nl;
147 static inline struct nfs4_label *
148 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
149 struct iattr *sattr, struct nfs4_label *l)
152 nfs4_label_release_security(struct nfs4_label *label)
155 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
156 { return server->attr_bitmask; }
159 /* Prevent leaks of NFSv4 errors into userland */
160 static int nfs4_map_errors(int err)
165 case -NFS4ERR_RESOURCE:
166 case -NFS4ERR_LAYOUTTRYLATER:
167 case -NFS4ERR_RECALLCONFLICT:
169 case -NFS4ERR_WRONGSEC:
170 case -NFS4ERR_WRONG_CRED:
172 case -NFS4ERR_BADOWNER:
173 case -NFS4ERR_BADNAME:
175 case -NFS4ERR_SHARE_DENIED:
177 case -NFS4ERR_MINOR_VERS_MISMATCH:
178 return -EPROTONOSUPPORT;
179 case -NFS4ERR_FILE_OPEN:
181 case -NFS4ERR_NOT_SAME:
184 dprintk("%s could not handle NFSv4 error %d\n",
192 * This is our standard bitmap for GETATTR requests.
194 const u32 nfs4_fattr_bitmap[3] = {
196 | FATTR4_WORD0_CHANGE
199 | FATTR4_WORD0_FILEID,
201 | FATTR4_WORD1_NUMLINKS
203 | FATTR4_WORD1_OWNER_GROUP
204 | FATTR4_WORD1_RAWDEV
205 | FATTR4_WORD1_SPACE_USED
206 | FATTR4_WORD1_TIME_ACCESS
207 | FATTR4_WORD1_TIME_METADATA
208 | FATTR4_WORD1_TIME_MODIFY
209 | FATTR4_WORD1_MOUNTED_ON_FILEID,
210 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
211 FATTR4_WORD2_SECURITY_LABEL
215 static const u32 nfs4_pnfs_open_bitmap[3] = {
217 | FATTR4_WORD0_CHANGE
220 | FATTR4_WORD0_FILEID,
222 | FATTR4_WORD1_NUMLINKS
224 | FATTR4_WORD1_OWNER_GROUP
225 | FATTR4_WORD1_RAWDEV
226 | FATTR4_WORD1_SPACE_USED
227 | FATTR4_WORD1_TIME_ACCESS
228 | FATTR4_WORD1_TIME_METADATA
229 | FATTR4_WORD1_TIME_MODIFY,
230 FATTR4_WORD2_MDSTHRESHOLD
231 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
232 | FATTR4_WORD2_SECURITY_LABEL
236 static const u32 nfs4_open_noattr_bitmap[3] = {
238 | FATTR4_WORD0_FILEID,
241 const u32 nfs4_statfs_bitmap[3] = {
242 FATTR4_WORD0_FILES_AVAIL
243 | FATTR4_WORD0_FILES_FREE
244 | FATTR4_WORD0_FILES_TOTAL,
245 FATTR4_WORD1_SPACE_AVAIL
246 | FATTR4_WORD1_SPACE_FREE
247 | FATTR4_WORD1_SPACE_TOTAL
250 const u32 nfs4_pathconf_bitmap[3] = {
252 | FATTR4_WORD0_MAXNAME,
256 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
257 | FATTR4_WORD0_MAXREAD
258 | FATTR4_WORD0_MAXWRITE
259 | FATTR4_WORD0_LEASE_TIME,
260 FATTR4_WORD1_TIME_DELTA
261 | FATTR4_WORD1_FS_LAYOUT_TYPES,
262 FATTR4_WORD2_LAYOUT_BLKSIZE
263 | FATTR4_WORD2_CLONE_BLKSIZE
264 | FATTR4_WORD2_CHANGE_ATTR_TYPE
265 | FATTR4_WORD2_XATTR_SUPPORT
268 const u32 nfs4_fs_locations_bitmap[3] = {
272 | FATTR4_WORD0_FILEID
273 | FATTR4_WORD0_FS_LOCATIONS,
275 | FATTR4_WORD1_OWNER_GROUP
276 | FATTR4_WORD1_RAWDEV
277 | FATTR4_WORD1_SPACE_USED
278 | FATTR4_WORD1_TIME_ACCESS
279 | FATTR4_WORD1_TIME_METADATA
280 | FATTR4_WORD1_TIME_MODIFY
281 | FATTR4_WORD1_MOUNTED_ON_FILEID,
284 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
285 struct inode *inode, unsigned long flags)
287 unsigned long cache_validity;
289 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
290 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
293 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags;
295 /* Remove the attributes over which we have full control */
296 dst[1] &= ~FATTR4_WORD1_RAWDEV;
297 if (!(cache_validity & NFS_INO_INVALID_SIZE))
298 dst[0] &= ~FATTR4_WORD0_SIZE;
300 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
301 dst[0] &= ~FATTR4_WORD0_CHANGE;
303 if (!(cache_validity & NFS_INO_INVALID_MODE))
304 dst[1] &= ~FATTR4_WORD1_MODE;
305 if (!(cache_validity & NFS_INO_INVALID_OTHER))
306 dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP);
309 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
310 struct nfs4_readdir_arg *readdir)
312 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
316 readdir->cookie = cookie;
317 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
322 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
327 * NFSv4 servers do not return entries for '.' and '..'
328 * Therefore, we fake these entries here. We let '.'
329 * have cookie 0 and '..' have cookie 1. Note that
330 * when talking to the server, we always send cookie 0
333 start = p = kmap_atomic(*readdir->pages);
336 *p++ = xdr_one; /* next */
337 *p++ = xdr_zero; /* cookie, first word */
338 *p++ = xdr_one; /* cookie, second word */
339 *p++ = xdr_one; /* entry len */
340 memcpy(p, ".\0\0\0", 4); /* entry */
342 *p++ = xdr_one; /* bitmap length */
343 *p++ = htonl(attrs); /* bitmap */
344 *p++ = htonl(12); /* attribute buffer length */
345 *p++ = htonl(NF4DIR);
346 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
349 *p++ = xdr_one; /* next */
350 *p++ = xdr_zero; /* cookie, first word */
351 *p++ = xdr_two; /* cookie, second word */
352 *p++ = xdr_two; /* entry len */
353 memcpy(p, "..\0\0", 4); /* entry */
355 *p++ = xdr_one; /* bitmap length */
356 *p++ = htonl(attrs); /* bitmap */
357 *p++ = htonl(12); /* attribute buffer length */
358 *p++ = htonl(NF4DIR);
359 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
361 readdir->pgbase = (char *)p - (char *)start;
362 readdir->count -= readdir->pgbase;
363 kunmap_atomic(start);
366 static void nfs4_fattr_set_prechange(struct nfs_fattr *fattr, u64 version)
368 if (!(fattr->valid & NFS_ATTR_FATTR_PRECHANGE)) {
369 fattr->pre_change_attr = version;
370 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
374 static void nfs4_test_and_free_stateid(struct nfs_server *server,
375 nfs4_stateid *stateid,
376 const struct cred *cred)
378 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
380 ops->test_and_free_expired(server, stateid, cred);
383 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
384 nfs4_stateid *stateid,
385 const struct cred *cred)
387 stateid->type = NFS4_REVOKED_STATEID_TYPE;
388 nfs4_test_and_free_stateid(server, stateid, cred);
391 static void nfs4_free_revoked_stateid(struct nfs_server *server,
392 const nfs4_stateid *stateid,
393 const struct cred *cred)
397 nfs4_stateid_copy(&tmp, stateid);
398 __nfs4_free_revoked_stateid(server, &tmp, cred);
401 static long nfs4_update_delay(long *timeout)
405 return NFS4_POLL_RETRY_MAX;
407 *timeout = NFS4_POLL_RETRY_MIN;
408 if (*timeout > NFS4_POLL_RETRY_MAX)
409 *timeout = NFS4_POLL_RETRY_MAX;
415 static int nfs4_delay_killable(long *timeout)
419 freezable_schedule_timeout_killable_unsafe(
420 nfs4_update_delay(timeout));
421 if (!__fatal_signal_pending(current))
426 static int nfs4_delay_interruptible(long *timeout)
430 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
431 if (!signal_pending(current))
433 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
436 static int nfs4_delay(long *timeout, bool interruptible)
439 return nfs4_delay_interruptible(timeout);
440 return nfs4_delay_killable(timeout);
443 static const nfs4_stateid *
444 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
448 switch (stateid->type) {
449 case NFS4_OPEN_STATEID_TYPE:
450 case NFS4_LOCK_STATEID_TYPE:
451 case NFS4_DELEGATION_STATEID_TYPE:
459 /* This is the error handling routine for processes that are allowed
462 static int nfs4_do_handle_exception(struct nfs_server *server,
463 int errorcode, struct nfs4_exception *exception)
465 struct nfs_client *clp = server->nfs_client;
466 struct nfs4_state *state = exception->state;
467 const nfs4_stateid *stateid;
468 struct inode *inode = exception->inode;
471 exception->delay = 0;
472 exception->recovering = 0;
473 exception->retry = 0;
475 stateid = nfs4_recoverable_stateid(exception->stateid);
476 if (stateid == NULL && state != NULL)
477 stateid = nfs4_recoverable_stateid(&state->stateid);
482 case -NFS4ERR_BADHANDLE:
484 if (inode != NULL && S_ISREG(inode->i_mode))
485 pnfs_destroy_layout(NFS_I(inode));
487 case -NFS4ERR_DELEG_REVOKED:
488 case -NFS4ERR_ADMIN_REVOKED:
489 case -NFS4ERR_EXPIRED:
490 case -NFS4ERR_BAD_STATEID:
491 case -NFS4ERR_PARTNER_NO_AUTH:
492 if (inode != NULL && stateid != NULL) {
493 nfs_inode_find_state_and_recover(inode,
495 goto wait_on_recovery;
498 case -NFS4ERR_OPENMODE:
502 err = nfs_async_inode_return_delegation(inode,
505 goto wait_on_recovery;
506 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
507 exception->retry = 1;
513 ret = nfs4_schedule_stateid_recovery(server, state);
516 goto wait_on_recovery;
517 case -NFS4ERR_STALE_STATEID:
518 case -NFS4ERR_STALE_CLIENTID:
519 nfs4_schedule_lease_recovery(clp);
520 goto wait_on_recovery;
522 ret = nfs4_schedule_migration_recovery(server);
525 goto wait_on_recovery;
526 case -NFS4ERR_LEASE_MOVED:
527 nfs4_schedule_lease_moved_recovery(clp);
528 goto wait_on_recovery;
529 #if defined(CONFIG_NFS_V4_1)
530 case -NFS4ERR_BADSESSION:
531 case -NFS4ERR_BADSLOT:
532 case -NFS4ERR_BAD_HIGH_SLOT:
533 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
534 case -NFS4ERR_DEADSESSION:
535 case -NFS4ERR_SEQ_FALSE_RETRY:
536 case -NFS4ERR_SEQ_MISORDERED:
537 /* Handled in nfs41_sequence_process() */
538 goto wait_on_recovery;
539 #endif /* defined(CONFIG_NFS_V4_1) */
540 case -NFS4ERR_FILE_OPEN:
541 if (exception->timeout > HZ) {
542 /* We have retried a decent amount, time to
550 nfs_inc_server_stats(server, NFSIOS_DELAY);
553 case -NFS4ERR_LAYOUTTRYLATER:
554 case -NFS4ERR_RECALLCONFLICT:
555 exception->delay = 1;
558 case -NFS4ERR_RETRY_UNCACHED_REP:
559 case -NFS4ERR_OLD_STATEID:
560 exception->retry = 1;
562 case -NFS4ERR_BADOWNER:
563 /* The following works around a Linux server bug! */
564 case -NFS4ERR_BADNAME:
565 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
566 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
567 exception->retry = 1;
568 printk(KERN_WARNING "NFS: v4 server %s "
569 "does not accept raw "
571 "Reenabling the idmapper.\n",
572 server->nfs_client->cl_hostname);
575 /* We failed to handle the error */
576 return nfs4_map_errors(ret);
578 exception->recovering = 1;
582 /* This is the error handling routine for processes that are allowed
585 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
587 struct nfs_client *clp = server->nfs_client;
590 ret = nfs4_do_handle_exception(server, errorcode, exception);
591 if (exception->delay) {
592 ret = nfs4_delay(&exception->timeout,
593 exception->interruptible);
596 if (exception->recovering) {
597 if (exception->task_is_privileged)
599 ret = nfs4_wait_clnt_recover(clp);
600 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
607 exception->retry = 1;
612 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
613 int errorcode, struct nfs4_exception *exception)
615 struct nfs_client *clp = server->nfs_client;
618 ret = nfs4_do_handle_exception(server, errorcode, exception);
619 if (exception->delay) {
620 rpc_delay(task, nfs4_update_delay(&exception->timeout));
623 if (exception->recovering) {
624 if (exception->task_is_privileged)
626 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
627 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
628 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
631 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
636 exception->retry = 1;
638 * For NFS4ERR_MOVED, the client transport will need to
639 * be recomputed after migration recovery has completed.
641 if (errorcode == -NFS4ERR_MOVED)
642 rpc_task_release_transport(task);
648 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
649 struct nfs4_state *state, long *timeout)
651 struct nfs4_exception exception = {
655 if (task->tk_status >= 0)
658 exception.timeout = *timeout;
659 task->tk_status = nfs4_async_handle_exception(task, server,
662 if (exception.delay && timeout)
663 *timeout = exception.timeout;
670 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
671 * or 'false' otherwise.
673 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
675 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
676 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
679 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
681 spin_lock(&clp->cl_lock);
682 if (time_before(clp->cl_last_renewal,timestamp))
683 clp->cl_last_renewal = timestamp;
684 spin_unlock(&clp->cl_lock);
687 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
689 struct nfs_client *clp = server->nfs_client;
691 if (!nfs4_has_session(clp))
692 do_renew_lease(clp, timestamp);
695 struct nfs4_call_sync_data {
696 const struct nfs_server *seq_server;
697 struct nfs4_sequence_args *seq_args;
698 struct nfs4_sequence_res *seq_res;
701 void nfs4_init_sequence(struct nfs4_sequence_args *args,
702 struct nfs4_sequence_res *res, int cache_reply,
705 args->sa_slot = NULL;
706 args->sa_cache_this = cache_reply;
707 args->sa_privileged = privileged;
712 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
714 struct nfs4_slot *slot = res->sr_slot;
715 struct nfs4_slot_table *tbl;
718 spin_lock(&tbl->slot_tbl_lock);
719 if (!nfs41_wake_and_assign_slot(tbl, slot))
720 nfs4_free_slot(tbl, slot);
721 spin_unlock(&tbl->slot_tbl_lock);
726 static int nfs40_sequence_done(struct rpc_task *task,
727 struct nfs4_sequence_res *res)
729 if (res->sr_slot != NULL)
730 nfs40_sequence_free_slot(res);
734 #if defined(CONFIG_NFS_V4_1)
736 static void nfs41_release_slot(struct nfs4_slot *slot)
738 struct nfs4_session *session;
739 struct nfs4_slot_table *tbl;
740 bool send_new_highest_used_slotid = false;
745 session = tbl->session;
747 /* Bump the slot sequence number */
752 spin_lock(&tbl->slot_tbl_lock);
753 /* Be nice to the server: try to ensure that the last transmitted
754 * value for highest_user_slotid <= target_highest_slotid
756 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
757 send_new_highest_used_slotid = true;
759 if (nfs41_wake_and_assign_slot(tbl, slot)) {
760 send_new_highest_used_slotid = false;
763 nfs4_free_slot(tbl, slot);
765 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
766 send_new_highest_used_slotid = false;
768 spin_unlock(&tbl->slot_tbl_lock);
769 if (send_new_highest_used_slotid)
770 nfs41_notify_server(session->clp);
771 if (waitqueue_active(&tbl->slot_waitq))
772 wake_up_all(&tbl->slot_waitq);
775 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
777 nfs41_release_slot(res->sr_slot);
781 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
784 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
785 slot->seq_nr_highest_sent = seqnr;
787 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
790 slot->seq_nr_highest_sent = seqnr;
791 slot->seq_nr_last_acked = seqnr;
794 static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
795 struct nfs4_slot *slot)
797 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
799 rpc_put_task_async(task);
802 static int nfs41_sequence_process(struct rpc_task *task,
803 struct nfs4_sequence_res *res)
805 struct nfs4_session *session;
806 struct nfs4_slot *slot = res->sr_slot;
807 struct nfs_client *clp;
813 /* don't increment the sequence number if the task wasn't sent */
814 if (!RPC_WAS_SENT(task) || slot->seq_done)
817 session = slot->table->session;
820 trace_nfs4_sequence_done(session, res);
822 status = res->sr_status;
823 if (task->tk_status == -NFS4ERR_DEADSESSION)
824 status = -NFS4ERR_DEADSESSION;
826 /* Check the SEQUENCE operation status */
829 /* Mark this sequence number as having been acked */
830 nfs4_slot_sequence_acked(slot, slot->seq_nr);
831 /* Update the slot's sequence and clientid lease timer */
833 do_renew_lease(clp, res->sr_timestamp);
834 /* Check sequence flags */
835 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
837 nfs41_update_target_slotid(slot->table, slot, res);
841 * sr_status remains 1 if an RPC level error occurred.
842 * The server may or may not have processed the sequence
845 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
849 /* The server detected a resend of the RPC call and
850 * returned NFS4ERR_DELAY as per Section 2.10.6.2
853 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
857 nfs4_slot_sequence_acked(slot, slot->seq_nr);
859 case -NFS4ERR_RETRY_UNCACHED_REP:
860 case -NFS4ERR_SEQ_FALSE_RETRY:
862 * The server thinks we tried to replay a request.
863 * Retry the call after bumping the sequence ID.
865 nfs4_slot_sequence_acked(slot, slot->seq_nr);
867 case -NFS4ERR_BADSLOT:
869 * The slot id we used was probably retired. Try again
870 * using a different slot id.
872 if (slot->slot_nr < slot->table->target_highest_slotid)
873 goto session_recover;
875 case -NFS4ERR_SEQ_MISORDERED:
876 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
878 * Were one or more calls using this slot interrupted?
879 * If the server never received the request, then our
880 * transmitted slot sequence number may be too high. However,
881 * if the server did receive the request then it might
882 * accidentally give us a reply with a mismatched operation.
883 * We can sort this out by sending a lone sequence operation
884 * to the server on the same slot.
886 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
888 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
889 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
896 * A retry might be sent while the original request is
897 * still in progress on the replier. The replier SHOULD
898 * deal with the issue by returning NFS4ERR_DELAY as the
899 * reply to SEQUENCE or CB_SEQUENCE operation, but
900 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
902 * Restart the search after a delay.
904 slot->seq_nr = slot->seq_nr_highest_sent;
906 case -NFS4ERR_BADSESSION:
907 case -NFS4ERR_DEADSESSION:
908 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
909 goto session_recover;
911 /* Just update the slot sequence no. */
915 /* The session may be reset by one of the error handlers. */
916 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
920 nfs4_schedule_session_recovery(session, status);
921 dprintk("%s ERROR: %d Reset session\n", __func__, status);
922 nfs41_sequence_free_slot(res);
927 if (rpc_restart_call_prepare(task)) {
928 nfs41_sequence_free_slot(res);
934 if (!rpc_restart_call(task))
936 rpc_delay(task, NFS4_POLL_RETRY_MAX);
940 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
942 if (!nfs41_sequence_process(task, res))
944 if (res->sr_slot != NULL)
945 nfs41_sequence_free_slot(res);
949 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
951 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
953 if (res->sr_slot == NULL)
955 if (res->sr_slot->table->session != NULL)
956 return nfs41_sequence_process(task, res);
957 return nfs40_sequence_done(task, res);
960 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
962 if (res->sr_slot != NULL) {
963 if (res->sr_slot->table->session != NULL)
964 nfs41_sequence_free_slot(res);
966 nfs40_sequence_free_slot(res);
970 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
972 if (res->sr_slot == NULL)
974 if (!res->sr_slot->table->session)
975 return nfs40_sequence_done(task, res);
976 return nfs41_sequence_done(task, res);
978 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
980 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
982 struct nfs4_call_sync_data *data = calldata;
984 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
986 nfs4_setup_sequence(data->seq_server->nfs_client,
987 data->seq_args, data->seq_res, task);
990 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
992 struct nfs4_call_sync_data *data = calldata;
994 nfs41_sequence_done(task, data->seq_res);
997 static const struct rpc_call_ops nfs41_call_sync_ops = {
998 .rpc_call_prepare = nfs41_call_sync_prepare,
999 .rpc_call_done = nfs41_call_sync_done,
1002 #else /* !CONFIG_NFS_V4_1 */
1004 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1006 return nfs40_sequence_done(task, res);
1009 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1011 if (res->sr_slot != NULL)
1012 nfs40_sequence_free_slot(res);
1015 int nfs4_sequence_done(struct rpc_task *task,
1016 struct nfs4_sequence_res *res)
1018 return nfs40_sequence_done(task, res);
1020 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1022 #endif /* !CONFIG_NFS_V4_1 */
1024 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1026 res->sr_timestamp = jiffies;
1027 res->sr_status_flags = 0;
1032 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1033 struct nfs4_sequence_res *res,
1034 struct nfs4_slot *slot)
1038 slot->privileged = args->sa_privileged ? 1 : 0;
1039 args->sa_slot = slot;
1041 res->sr_slot = slot;
1044 int nfs4_setup_sequence(struct nfs_client *client,
1045 struct nfs4_sequence_args *args,
1046 struct nfs4_sequence_res *res,
1047 struct rpc_task *task)
1049 struct nfs4_session *session = nfs4_get_session(client);
1050 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1051 struct nfs4_slot *slot;
1053 /* slot already allocated? */
1054 if (res->sr_slot != NULL)
1058 tbl = &session->fc_slot_table;
1060 spin_lock(&tbl->slot_tbl_lock);
1061 /* The state manager will wait until the slot table is empty */
1062 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1065 slot = nfs4_alloc_slot(tbl);
1067 if (slot == ERR_PTR(-ENOMEM))
1068 goto out_sleep_timeout;
1071 spin_unlock(&tbl->slot_tbl_lock);
1073 nfs4_sequence_attach_slot(args, res, slot);
1075 trace_nfs4_setup_sequence(session, args);
1077 nfs41_sequence_res_init(res);
1078 rpc_call_start(task);
1081 /* Try again in 1/4 second */
1082 if (args->sa_privileged)
1083 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1084 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1086 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1087 NULL, jiffies + (HZ >> 2));
1088 spin_unlock(&tbl->slot_tbl_lock);
1091 if (args->sa_privileged)
1092 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1093 RPC_PRIORITY_PRIVILEGED);
1095 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1096 spin_unlock(&tbl->slot_tbl_lock);
1099 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1101 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1103 struct nfs4_call_sync_data *data = calldata;
1104 nfs4_setup_sequence(data->seq_server->nfs_client,
1105 data->seq_args, data->seq_res, task);
1108 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1110 struct nfs4_call_sync_data *data = calldata;
1111 nfs4_sequence_done(task, data->seq_res);
1114 static const struct rpc_call_ops nfs40_call_sync_ops = {
1115 .rpc_call_prepare = nfs40_call_sync_prepare,
1116 .rpc_call_done = nfs40_call_sync_done,
1119 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1122 struct rpc_task *task;
1124 task = rpc_run_task(task_setup);
1126 return PTR_ERR(task);
1128 ret = task->tk_status;
1133 static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1134 struct nfs_server *server,
1135 struct rpc_message *msg,
1136 struct nfs4_sequence_args *args,
1137 struct nfs4_sequence_res *res,
1138 unsigned short task_flags)
1140 struct nfs_client *clp = server->nfs_client;
1141 struct nfs4_call_sync_data data = {
1142 .seq_server = server,
1146 struct rpc_task_setup task_setup = {
1149 .callback_ops = clp->cl_mvops->call_sync_ops,
1150 .callback_data = &data,
1151 .flags = task_flags,
1154 return nfs4_call_sync_custom(&task_setup);
1157 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1158 struct nfs_server *server,
1159 struct rpc_message *msg,
1160 struct nfs4_sequence_args *args,
1161 struct nfs4_sequence_res *res)
1163 unsigned short task_flags = 0;
1165 if (server->caps & NFS_CAP_MOVEABLE)
1166 task_flags = RPC_TASK_MOVEABLE;
1167 return nfs4_do_call_sync(clnt, server, msg, args, res, task_flags);
1171 int nfs4_call_sync(struct rpc_clnt *clnt,
1172 struct nfs_server *server,
1173 struct rpc_message *msg,
1174 struct nfs4_sequence_args *args,
1175 struct nfs4_sequence_res *res,
1178 nfs4_init_sequence(args, res, cache_reply, 0);
1179 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1183 nfs4_inc_nlink_locked(struct inode *inode)
1185 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1186 NFS_INO_INVALID_CTIME |
1187 NFS_INO_INVALID_NLINK);
1192 nfs4_inc_nlink(struct inode *inode)
1194 spin_lock(&inode->i_lock);
1195 nfs4_inc_nlink_locked(inode);
1196 spin_unlock(&inode->i_lock);
1200 nfs4_dec_nlink_locked(struct inode *inode)
1202 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1203 NFS_INO_INVALID_CTIME |
1204 NFS_INO_INVALID_NLINK);
1209 nfs4_update_changeattr_locked(struct inode *inode,
1210 struct nfs4_change_info *cinfo,
1211 unsigned long timestamp, unsigned long cache_validity)
1213 struct nfs_inode *nfsi = NFS_I(inode);
1214 u64 change_attr = inode_peek_iversion_raw(inode);
1216 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
1217 if (S_ISDIR(inode->i_mode))
1218 cache_validity |= NFS_INO_INVALID_DATA;
1220 switch (NFS_SERVER(inode)->change_attr_type) {
1221 case NFS4_CHANGE_TYPE_IS_UNDEFINED:
1222 if (cinfo->after == change_attr)
1226 if ((s64)(change_attr - cinfo->after) >= 0)
1230 inode_set_iversion_raw(inode, cinfo->after);
1231 if (!cinfo->atomic || cinfo->before != change_attr) {
1232 if (S_ISDIR(inode->i_mode))
1233 nfs_force_lookup_revalidate(inode);
1235 if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1237 NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
1238 NFS_INO_INVALID_SIZE | NFS_INO_INVALID_OTHER |
1239 NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
1240 NFS_INO_INVALID_MODE | NFS_INO_INVALID_XATTR;
1241 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1243 nfsi->attrtimeo_timestamp = jiffies;
1244 nfsi->read_cache_jiffies = timestamp;
1245 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1246 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1248 nfs_set_cache_invalid(inode, cache_validity);
1252 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1253 unsigned long timestamp, unsigned long cache_validity)
1255 spin_lock(&dir->i_lock);
1256 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1257 spin_unlock(&dir->i_lock);
1260 struct nfs4_open_createattrs {
1261 struct nfs4_label *label;
1262 struct iattr *sattr;
1263 const __u32 verf[2];
1266 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1267 int err, struct nfs4_exception *exception)
1271 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1273 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1274 exception->retry = 1;
1278 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1280 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1283 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1285 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1287 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1291 nfs4_map_atomic_open_share(struct nfs_server *server,
1292 fmode_t fmode, int openflags)
1296 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1298 res = NFS4_SHARE_ACCESS_READ;
1301 res = NFS4_SHARE_ACCESS_WRITE;
1303 case FMODE_READ|FMODE_WRITE:
1304 res = NFS4_SHARE_ACCESS_BOTH;
1306 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1308 /* Want no delegation if we're using O_DIRECT */
1309 if (openflags & O_DIRECT)
1310 res |= NFS4_SHARE_WANT_NO_DELEG;
1315 static enum open_claim_type4
1316 nfs4_map_atomic_open_claim(struct nfs_server *server,
1317 enum open_claim_type4 claim)
1319 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1324 case NFS4_OPEN_CLAIM_FH:
1325 return NFS4_OPEN_CLAIM_NULL;
1326 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1327 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1328 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1329 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1333 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1335 p->o_res.f_attr = &p->f_attr;
1336 p->o_res.seqid = p->o_arg.seqid;
1337 p->c_res.seqid = p->c_arg.seqid;
1338 p->o_res.server = p->o_arg.server;
1339 p->o_res.access_request = p->o_arg.access;
1340 nfs_fattr_init(&p->f_attr);
1341 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1344 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1345 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1346 const struct nfs4_open_createattrs *c,
1347 enum open_claim_type4 claim,
1350 struct dentry *parent = dget_parent(dentry);
1351 struct inode *dir = d_inode(parent);
1352 struct nfs_server *server = NFS_SERVER(dir);
1353 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1354 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1355 struct nfs4_opendata *p;
1357 p = kzalloc(sizeof(*p), gfp_mask);
1361 p->f_attr.label = nfs4_label_alloc(server, gfp_mask);
1362 if (IS_ERR(p->f_attr.label))
1365 p->a_label = nfs4_label_alloc(server, gfp_mask);
1366 if (IS_ERR(p->a_label))
1369 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1370 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1371 if (IS_ERR(p->o_arg.seqid))
1372 goto err_free_label;
1373 nfs_sb_active(dentry->d_sb);
1374 p->dentry = dget(dentry);
1377 atomic_inc(&sp->so_count);
1378 p->o_arg.open_flags = flags;
1379 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1380 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1381 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1383 if (flags & O_CREAT) {
1384 p->o_arg.umask = current_umask();
1385 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1386 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1387 p->o_arg.u.attrs = &p->attrs;
1388 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1390 memcpy(p->o_arg.u.verifier.data, c->verf,
1391 sizeof(p->o_arg.u.verifier.data));
1394 /* ask server to check for all possible rights as results
1396 switch (p->o_arg.claim) {
1399 case NFS4_OPEN_CLAIM_NULL:
1400 case NFS4_OPEN_CLAIM_FH:
1401 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1402 NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE |
1403 NFS4_ACCESS_EXECUTE |
1404 nfs_access_xattr_mask(server);
1406 p->o_arg.clientid = server->nfs_client->cl_clientid;
1407 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1408 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1409 p->o_arg.name = &dentry->d_name;
1410 p->o_arg.server = server;
1411 p->o_arg.bitmask = nfs4_bitmask(server, label);
1412 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1413 switch (p->o_arg.claim) {
1414 case NFS4_OPEN_CLAIM_NULL:
1415 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1416 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1417 p->o_arg.fh = NFS_FH(dir);
1419 case NFS4_OPEN_CLAIM_PREVIOUS:
1420 case NFS4_OPEN_CLAIM_FH:
1421 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1422 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1423 p->o_arg.fh = NFS_FH(d_inode(dentry));
1425 p->c_arg.fh = &p->o_res.fh;
1426 p->c_arg.stateid = &p->o_res.stateid;
1427 p->c_arg.seqid = p->o_arg.seqid;
1428 nfs4_init_opendata_res(p);
1429 kref_init(&p->kref);
1433 nfs4_label_free(p->a_label);
1435 nfs4_label_free(p->f_attr.label);
1443 static void nfs4_opendata_free(struct kref *kref)
1445 struct nfs4_opendata *p = container_of(kref,
1446 struct nfs4_opendata, kref);
1447 struct super_block *sb = p->dentry->d_sb;
1449 nfs4_lgopen_release(p->lgp);
1450 nfs_free_seqid(p->o_arg.seqid);
1451 nfs4_sequence_free_slot(&p->o_res.seq_res);
1452 if (p->state != NULL)
1453 nfs4_put_open_state(p->state);
1454 nfs4_put_state_owner(p->owner);
1456 nfs4_label_free(p->a_label);
1457 nfs4_label_free(p->f_attr.label);
1461 nfs_sb_deactive(sb);
1462 nfs_fattr_free_names(&p->f_attr);
1463 kfree(p->f_attr.mdsthreshold);
1467 static void nfs4_opendata_put(struct nfs4_opendata *p)
1470 kref_put(&p->kref, nfs4_opendata_free);
1473 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1476 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1477 case FMODE_READ|FMODE_WRITE:
1478 return state->n_rdwr != 0;
1480 return state->n_wronly != 0;
1482 return state->n_rdonly != 0;
1488 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1489 int open_mode, enum open_claim_type4 claim)
1493 if (open_mode & (O_EXCL|O_TRUNC))
1496 case NFS4_OPEN_CLAIM_NULL:
1497 case NFS4_OPEN_CLAIM_FH:
1502 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1504 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1505 && state->n_rdonly != 0;
1508 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1509 && state->n_wronly != 0;
1511 case FMODE_READ|FMODE_WRITE:
1512 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1513 && state->n_rdwr != 0;
1519 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1520 enum open_claim_type4 claim)
1522 if (delegation == NULL)
1524 if ((delegation->type & fmode) != fmode)
1527 case NFS4_OPEN_CLAIM_NULL:
1528 case NFS4_OPEN_CLAIM_FH:
1530 case NFS4_OPEN_CLAIM_PREVIOUS:
1531 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1537 nfs_mark_delegation_referenced(delegation);
1541 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1550 case FMODE_READ|FMODE_WRITE:
1553 nfs4_state_set_mode_locked(state, state->state | fmode);
1556 #ifdef CONFIG_NFS_V4_1
1557 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1559 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1561 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1563 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1567 #endif /* CONFIG_NFS_V4_1 */
1569 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1571 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1572 wake_up_all(&state->waitq);
1575 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1577 struct nfs_client *clp = state->owner->so_server->nfs_client;
1578 bool need_recover = false;
1580 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1581 need_recover = true;
1582 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1583 need_recover = true;
1584 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1585 need_recover = true;
1587 nfs4_state_mark_reclaim_nograce(clp, state);
1591 * Check for whether or not the caller may update the open stateid
1592 * to the value passed in by stateid.
1594 * Note: This function relies heavily on the server implementing
1595 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1597 * i.e. The stateid seqids have to be initialised to 1, and
1598 * are then incremented on every state transition.
1600 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1601 const nfs4_stateid *stateid)
1603 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1604 /* The common case - we're updating to a new sequence number */
1605 if (nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1606 if (nfs4_stateid_is_next(&state->open_stateid, stateid))
1610 /* The server returned a new stateid */
1612 /* This is the first OPEN in this generation */
1613 if (stateid->seqid == cpu_to_be32(1))
1618 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1620 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1622 if (state->n_wronly)
1623 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1624 if (state->n_rdonly)
1625 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1627 set_bit(NFS_O_RDWR_STATE, &state->flags);
1628 set_bit(NFS_OPEN_STATE, &state->flags);
1631 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1632 nfs4_stateid *stateid, fmode_t fmode)
1634 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1635 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1637 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1640 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1643 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1644 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1645 clear_bit(NFS_OPEN_STATE, &state->flags);
1647 if (stateid == NULL)
1649 /* Handle OPEN+OPEN_DOWNGRADE races */
1650 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1651 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1652 nfs_resync_open_stateid_locked(state);
1655 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1656 nfs4_stateid_copy(&state->stateid, stateid);
1657 nfs4_stateid_copy(&state->open_stateid, stateid);
1658 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1660 nfs_state_log_update_open_stateid(state);
1663 static void nfs_clear_open_stateid(struct nfs4_state *state,
1664 nfs4_stateid *arg_stateid,
1665 nfs4_stateid *stateid, fmode_t fmode)
1667 write_seqlock(&state->seqlock);
1668 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1669 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1670 nfs_clear_open_stateid_locked(state, stateid, fmode);
1671 write_sequnlock(&state->seqlock);
1672 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1673 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1676 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1677 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1678 __must_hold(&state->owner->so_lock)
1679 __must_hold(&state->seqlock)
1687 if (nfs_stateid_is_sequential(state, stateid))
1692 /* Rely on seqids for serialisation with NFSv4.0 */
1693 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1696 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1697 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1699 * Ensure we process the state changes in the same order
1700 * in which the server processed them by delaying the
1701 * update of the stateid until we are in sequence.
1703 write_sequnlock(&state->seqlock);
1704 spin_unlock(&state->owner->so_lock);
1706 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1708 if (!fatal_signal_pending(current)) {
1709 if (schedule_timeout(5*HZ) == 0)
1715 finish_wait(&state->waitq, &wait);
1717 spin_lock(&state->owner->so_lock);
1718 write_seqlock(&state->seqlock);
1721 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1722 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1723 nfs4_stateid_copy(freeme, &state->open_stateid);
1724 nfs_test_and_clear_all_open_stateid(state);
1727 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1728 nfs4_stateid_copy(&state->stateid, stateid);
1729 nfs4_stateid_copy(&state->open_stateid, stateid);
1730 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1731 nfs_state_log_update_open_stateid(state);
1734 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1735 const nfs4_stateid *open_stateid,
1737 nfs4_stateid *freeme)
1740 * Protect the call to nfs4_state_set_mode_locked and
1741 * serialise the stateid update
1743 write_seqlock(&state->seqlock);
1744 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1747 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1750 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1752 case FMODE_READ|FMODE_WRITE:
1753 set_bit(NFS_O_RDWR_STATE, &state->flags);
1755 set_bit(NFS_OPEN_STATE, &state->flags);
1756 write_sequnlock(&state->seqlock);
1759 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1761 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1762 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1763 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1764 clear_bit(NFS_OPEN_STATE, &state->flags);
1767 static void nfs_state_set_delegation(struct nfs4_state *state,
1768 const nfs4_stateid *deleg_stateid,
1772 * Protect the call to nfs4_state_set_mode_locked and
1773 * serialise the stateid update
1775 write_seqlock(&state->seqlock);
1776 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1777 set_bit(NFS_DELEGATED_STATE, &state->flags);
1778 write_sequnlock(&state->seqlock);
1781 static void nfs_state_clear_delegation(struct nfs4_state *state)
1783 write_seqlock(&state->seqlock);
1784 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1785 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1786 write_sequnlock(&state->seqlock);
1789 int update_open_stateid(struct nfs4_state *state,
1790 const nfs4_stateid *open_stateid,
1791 const nfs4_stateid *delegation,
1794 struct nfs_server *server = NFS_SERVER(state->inode);
1795 struct nfs_client *clp = server->nfs_client;
1796 struct nfs_inode *nfsi = NFS_I(state->inode);
1797 struct nfs_delegation *deleg_cur;
1798 nfs4_stateid freeme = { };
1801 fmode &= (FMODE_READ|FMODE_WRITE);
1804 spin_lock(&state->owner->so_lock);
1805 if (open_stateid != NULL) {
1806 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1810 deleg_cur = nfs4_get_valid_delegation(state->inode);
1811 if (deleg_cur == NULL)
1814 spin_lock(&deleg_cur->lock);
1815 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1816 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1817 (deleg_cur->type & fmode) != fmode)
1818 goto no_delegation_unlock;
1820 if (delegation == NULL)
1821 delegation = &deleg_cur->stateid;
1822 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1823 goto no_delegation_unlock;
1825 nfs_mark_delegation_referenced(deleg_cur);
1826 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1828 no_delegation_unlock:
1829 spin_unlock(&deleg_cur->lock);
1832 update_open_stateflags(state, fmode);
1833 spin_unlock(&state->owner->so_lock);
1836 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1837 nfs4_schedule_state_manager(clp);
1838 if (freeme.type != 0)
1839 nfs4_test_and_free_stateid(server, &freeme,
1840 state->owner->so_cred);
1845 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1846 const nfs4_stateid *stateid)
1848 struct nfs4_state *state = lsp->ls_state;
1851 spin_lock(&state->state_lock);
1852 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1854 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1856 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1859 spin_unlock(&state->state_lock);
1863 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1865 struct nfs_delegation *delegation;
1867 fmode &= FMODE_READ|FMODE_WRITE;
1869 delegation = nfs4_get_valid_delegation(inode);
1870 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1875 nfs4_inode_return_delegation(inode);
1878 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1880 struct nfs4_state *state = opendata->state;
1881 struct nfs_delegation *delegation;
1882 int open_mode = opendata->o_arg.open_flags;
1883 fmode_t fmode = opendata->o_arg.fmode;
1884 enum open_claim_type4 claim = opendata->o_arg.claim;
1885 nfs4_stateid stateid;
1889 spin_lock(&state->owner->so_lock);
1890 if (can_open_cached(state, fmode, open_mode, claim)) {
1891 update_open_stateflags(state, fmode);
1892 spin_unlock(&state->owner->so_lock);
1893 goto out_return_state;
1895 spin_unlock(&state->owner->so_lock);
1897 delegation = nfs4_get_valid_delegation(state->inode);
1898 if (!can_open_delegated(delegation, fmode, claim)) {
1902 /* Save the delegation */
1903 nfs4_stateid_copy(&stateid, &delegation->stateid);
1905 nfs_release_seqid(opendata->o_arg.seqid);
1906 if (!opendata->is_recover) {
1907 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1913 /* Try to update the stateid using the delegation */
1914 if (update_open_stateid(state, NULL, &stateid, fmode))
1915 goto out_return_state;
1918 return ERR_PTR(ret);
1920 refcount_inc(&state->count);
1925 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1927 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1928 struct nfs_delegation *delegation;
1929 int delegation_flags = 0;
1932 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1934 delegation_flags = delegation->flags;
1936 switch (data->o_arg.claim) {
1939 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1940 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1941 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1942 "returning a delegation for "
1943 "OPEN(CLAIM_DELEGATE_CUR)\n",
1947 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1948 nfs_inode_set_delegation(state->inode,
1949 data->owner->so_cred,
1950 data->o_res.delegation_type,
1951 &data->o_res.delegation,
1952 data->o_res.pagemod_limit);
1954 nfs_inode_reclaim_delegation(state->inode,
1955 data->owner->so_cred,
1956 data->o_res.delegation_type,
1957 &data->o_res.delegation,
1958 data->o_res.pagemod_limit);
1960 if (data->o_res.do_recall)
1961 nfs_async_inode_return_delegation(state->inode,
1962 &data->o_res.delegation);
1966 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1967 * and update the nfs4_state.
1969 static struct nfs4_state *
1970 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1972 struct inode *inode = data->state->inode;
1973 struct nfs4_state *state = data->state;
1976 if (!data->rpc_done) {
1977 if (data->rpc_status)
1978 return ERR_PTR(data->rpc_status);
1979 /* cached opens have already been processed */
1983 ret = nfs_refresh_inode(inode, &data->f_attr);
1985 return ERR_PTR(ret);
1987 if (data->o_res.delegation_type != 0)
1988 nfs4_opendata_check_deleg(data, state);
1990 if (!update_open_stateid(state, &data->o_res.stateid,
1991 NULL, data->o_arg.fmode))
1992 return ERR_PTR(-EAGAIN);
1993 refcount_inc(&state->count);
1998 static struct inode *
1999 nfs4_opendata_get_inode(struct nfs4_opendata *data)
2001 struct inode *inode;
2003 switch (data->o_arg.claim) {
2004 case NFS4_OPEN_CLAIM_NULL:
2005 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2006 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2007 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2008 return ERR_PTR(-EAGAIN);
2009 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2013 inode = d_inode(data->dentry);
2015 nfs_refresh_inode(inode, &data->f_attr);
2020 static struct nfs4_state *
2021 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2023 struct nfs4_state *state;
2024 struct inode *inode;
2026 inode = nfs4_opendata_get_inode(data);
2028 return ERR_CAST(inode);
2029 if (data->state != NULL && data->state->inode == inode) {
2030 state = data->state;
2031 refcount_inc(&state->count);
2033 state = nfs4_get_open_state(inode, data->owner);
2036 state = ERR_PTR(-ENOMEM);
2040 static struct nfs4_state *
2041 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2043 struct nfs4_state *state;
2045 if (!data->rpc_done) {
2046 state = nfs4_try_open_cached(data);
2047 trace_nfs4_cached_open(data->state);
2051 state = nfs4_opendata_find_nfs4_state(data);
2055 if (data->o_res.delegation_type != 0)
2056 nfs4_opendata_check_deleg(data, state);
2057 if (!update_open_stateid(state, &data->o_res.stateid,
2058 NULL, data->o_arg.fmode)) {
2059 nfs4_put_open_state(state);
2060 state = ERR_PTR(-EAGAIN);
2063 nfs_release_seqid(data->o_arg.seqid);
2067 static struct nfs4_state *
2068 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2070 struct nfs4_state *ret;
2072 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2073 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2075 ret = _nfs4_opendata_to_nfs4_state(data);
2076 nfs4_sequence_free_slot(&data->o_res.seq_res);
2080 static struct nfs_open_context *
2081 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2083 struct nfs_inode *nfsi = NFS_I(state->inode);
2084 struct nfs_open_context *ctx;
2087 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2088 if (ctx->state != state)
2090 if ((ctx->mode & mode) != mode)
2092 if (!get_nfs_open_context(ctx))
2098 return ERR_PTR(-ENOENT);
2101 static struct nfs_open_context *
2102 nfs4_state_find_open_context(struct nfs4_state *state)
2104 struct nfs_open_context *ctx;
2106 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2109 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2112 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2115 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2116 struct nfs4_state *state, enum open_claim_type4 claim)
2118 struct nfs4_opendata *opendata;
2120 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2121 NULL, claim, GFP_NOFS);
2122 if (opendata == NULL)
2123 return ERR_PTR(-ENOMEM);
2124 opendata->state = state;
2125 refcount_inc(&state->count);
2129 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2132 struct nfs4_state *newstate;
2135 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2137 opendata->o_arg.open_flags = 0;
2138 opendata->o_arg.fmode = fmode;
2139 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2140 NFS_SB(opendata->dentry->d_sb),
2142 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2143 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2144 nfs4_init_opendata_res(opendata);
2145 ret = _nfs4_recover_proc_open(opendata);
2148 newstate = nfs4_opendata_to_nfs4_state(opendata);
2149 if (IS_ERR(newstate))
2150 return PTR_ERR(newstate);
2151 if (newstate != opendata->state)
2153 nfs4_close_state(newstate, fmode);
2157 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2161 /* memory barrier prior to reading state->n_* */
2163 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2166 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2169 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2173 * We may have performed cached opens for all three recoveries.
2174 * Check if we need to update the current stateid.
2176 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2177 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2178 write_seqlock(&state->seqlock);
2179 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2180 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2181 write_sequnlock(&state->seqlock);
2188 * reclaim state on the server after a reboot.
2190 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2192 struct nfs_delegation *delegation;
2193 struct nfs4_opendata *opendata;
2194 fmode_t delegation_type = 0;
2197 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2198 NFS4_OPEN_CLAIM_PREVIOUS);
2199 if (IS_ERR(opendata))
2200 return PTR_ERR(opendata);
2202 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2203 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2204 delegation_type = delegation->type;
2206 opendata->o_arg.u.delegation_type = delegation_type;
2207 status = nfs4_open_recover(opendata, state);
2208 nfs4_opendata_put(opendata);
2212 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2214 struct nfs_server *server = NFS_SERVER(state->inode);
2215 struct nfs4_exception exception = { };
2218 err = _nfs4_do_open_reclaim(ctx, state);
2219 trace_nfs4_open_reclaim(ctx, 0, err);
2220 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2222 if (err != -NFS4ERR_DELAY)
2224 nfs4_handle_exception(server, err, &exception);
2225 } while (exception.retry);
2229 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2231 struct nfs_open_context *ctx;
2234 ctx = nfs4_state_find_open_context(state);
2237 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2238 nfs_state_clear_open_state_flags(state);
2239 ret = nfs4_do_open_reclaim(ctx, state);
2240 put_nfs_open_context(ctx);
2244 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)
2248 printk(KERN_ERR "NFS: %s: unhandled error "
2249 "%d.\n", __func__, err);
2257 case -NFS4ERR_BADSESSION:
2258 case -NFS4ERR_BADSLOT:
2259 case -NFS4ERR_BAD_HIGH_SLOT:
2260 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2261 case -NFS4ERR_DEADSESSION:
2263 case -NFS4ERR_STALE_CLIENTID:
2264 case -NFS4ERR_STALE_STATEID:
2265 /* Don't recall a delegation if it was lost */
2266 nfs4_schedule_lease_recovery(server->nfs_client);
2268 case -NFS4ERR_MOVED:
2269 nfs4_schedule_migration_recovery(server);
2271 case -NFS4ERR_LEASE_MOVED:
2272 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2274 case -NFS4ERR_DELEG_REVOKED:
2275 case -NFS4ERR_ADMIN_REVOKED:
2276 case -NFS4ERR_EXPIRED:
2277 case -NFS4ERR_BAD_STATEID:
2278 case -NFS4ERR_OPENMODE:
2279 nfs_inode_find_state_and_recover(state->inode,
2281 nfs4_schedule_stateid_recovery(server, state);
2283 case -NFS4ERR_DELAY:
2284 case -NFS4ERR_GRACE:
2288 case -NFS4ERR_DENIED:
2290 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2292 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2299 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2300 struct nfs4_state *state, const nfs4_stateid *stateid)
2302 struct nfs_server *server = NFS_SERVER(state->inode);
2303 struct nfs4_opendata *opendata;
2306 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2307 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2308 if (IS_ERR(opendata))
2309 return PTR_ERR(opendata);
2310 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2311 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2312 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2316 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2317 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2321 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2322 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2326 nfs_state_clear_delegation(state);
2328 nfs4_opendata_put(opendata);
2329 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2332 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2334 struct nfs4_opendata *data = calldata;
2336 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2337 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2340 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2342 struct nfs4_opendata *data = calldata;
2344 nfs40_sequence_done(task, &data->c_res.seq_res);
2346 data->rpc_status = task->tk_status;
2347 if (data->rpc_status == 0) {
2348 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2349 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2350 renew_lease(data->o_res.server, data->timestamp);
2351 data->rpc_done = true;
2355 static void nfs4_open_confirm_release(void *calldata)
2357 struct nfs4_opendata *data = calldata;
2358 struct nfs4_state *state = NULL;
2360 /* If this request hasn't been cancelled, do nothing */
2361 if (!data->cancelled)
2363 /* In case of error, no cleanup! */
2364 if (!data->rpc_done)
2366 state = nfs4_opendata_to_nfs4_state(data);
2368 nfs4_close_state(state, data->o_arg.fmode);
2370 nfs4_opendata_put(data);
2373 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2374 .rpc_call_prepare = nfs4_open_confirm_prepare,
2375 .rpc_call_done = nfs4_open_confirm_done,
2376 .rpc_release = nfs4_open_confirm_release,
2380 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2382 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2384 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2385 struct rpc_task *task;
2386 struct rpc_message msg = {
2387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2388 .rpc_argp = &data->c_arg,
2389 .rpc_resp = &data->c_res,
2390 .rpc_cred = data->owner->so_cred,
2392 struct rpc_task_setup task_setup_data = {
2393 .rpc_client = server->client,
2394 .rpc_message = &msg,
2395 .callback_ops = &nfs4_open_confirm_ops,
2396 .callback_data = data,
2397 .workqueue = nfsiod_workqueue,
2398 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2402 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2404 kref_get(&data->kref);
2405 data->rpc_done = false;
2406 data->rpc_status = 0;
2407 data->timestamp = jiffies;
2408 task = rpc_run_task(&task_setup_data);
2410 return PTR_ERR(task);
2411 status = rpc_wait_for_completion_task(task);
2413 data->cancelled = true;
2416 status = data->rpc_status;
2421 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2423 struct nfs4_opendata *data = calldata;
2424 struct nfs4_state_owner *sp = data->owner;
2425 struct nfs_client *clp = sp->so_server->nfs_client;
2426 enum open_claim_type4 claim = data->o_arg.claim;
2428 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2431 * Check if we still need to send an OPEN call, or if we can use
2432 * a delegation instead.
2434 if (data->state != NULL) {
2435 struct nfs_delegation *delegation;
2437 if (can_open_cached(data->state, data->o_arg.fmode,
2438 data->o_arg.open_flags, claim))
2441 delegation = nfs4_get_valid_delegation(data->state->inode);
2442 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2443 goto unlock_no_action;
2446 /* Update client id. */
2447 data->o_arg.clientid = clp->cl_clientid;
2451 case NFS4_OPEN_CLAIM_PREVIOUS:
2452 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2453 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2454 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2456 case NFS4_OPEN_CLAIM_FH:
2457 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2459 data->timestamp = jiffies;
2460 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2461 &data->o_arg.seq_args,
2462 &data->o_res.seq_res,
2464 nfs_release_seqid(data->o_arg.seqid);
2466 /* Set the create mode (note dependency on the session type) */
2467 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2468 if (data->o_arg.open_flags & O_EXCL) {
2469 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2470 if (clp->cl_mvops->minor_version == 0) {
2471 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2472 /* don't put an ACCESS op in OPEN compound if O_EXCL,
2473 * because ACCESS will return permission denied for
2474 * all bits until close */
2475 data->o_res.access_request = data->o_arg.access = 0;
2476 } else if (nfs4_has_persistent_session(clp))
2477 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2481 trace_nfs4_cached_open(data->state);
2484 task->tk_action = NULL;
2486 nfs4_sequence_done(task, &data->o_res.seq_res);
2489 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2491 struct nfs4_opendata *data = calldata;
2493 data->rpc_status = task->tk_status;
2495 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2498 if (task->tk_status == 0) {
2499 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2500 switch (data->o_res.f_attr->mode & S_IFMT) {
2504 data->rpc_status = -ELOOP;
2507 data->rpc_status = -EISDIR;
2510 data->rpc_status = -ENOTDIR;
2513 renew_lease(data->o_res.server, data->timestamp);
2514 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2515 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2517 data->rpc_done = true;
2520 static void nfs4_open_release(void *calldata)
2522 struct nfs4_opendata *data = calldata;
2523 struct nfs4_state *state = NULL;
2525 /* If this request hasn't been cancelled, do nothing */
2526 if (!data->cancelled)
2528 /* In case of error, no cleanup! */
2529 if (data->rpc_status != 0 || !data->rpc_done)
2531 /* In case we need an open_confirm, no cleanup! */
2532 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2534 state = nfs4_opendata_to_nfs4_state(data);
2536 nfs4_close_state(state, data->o_arg.fmode);
2538 nfs4_opendata_put(data);
2541 static const struct rpc_call_ops nfs4_open_ops = {
2542 .rpc_call_prepare = nfs4_open_prepare,
2543 .rpc_call_done = nfs4_open_done,
2544 .rpc_release = nfs4_open_release,
2547 static int nfs4_run_open_task(struct nfs4_opendata *data,
2548 struct nfs_open_context *ctx)
2550 struct inode *dir = d_inode(data->dir);
2551 struct nfs_server *server = NFS_SERVER(dir);
2552 struct nfs_openargs *o_arg = &data->o_arg;
2553 struct nfs_openres *o_res = &data->o_res;
2554 struct rpc_task *task;
2555 struct rpc_message msg = {
2556 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2559 .rpc_cred = data->owner->so_cred,
2561 struct rpc_task_setup task_setup_data = {
2562 .rpc_client = server->client,
2563 .rpc_message = &msg,
2564 .callback_ops = &nfs4_open_ops,
2565 .callback_data = data,
2566 .workqueue = nfsiod_workqueue,
2567 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2571 if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
2572 task_setup_data.flags |= RPC_TASK_MOVEABLE;
2574 kref_get(&data->kref);
2575 data->rpc_done = false;
2576 data->rpc_status = 0;
2577 data->cancelled = false;
2578 data->is_recover = false;
2580 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2581 data->is_recover = true;
2582 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2584 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2585 pnfs_lgopen_prepare(data, ctx);
2587 task = rpc_run_task(&task_setup_data);
2589 return PTR_ERR(task);
2590 status = rpc_wait_for_completion_task(task);
2592 data->cancelled = true;
2595 status = data->rpc_status;
2601 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2603 struct inode *dir = d_inode(data->dir);
2604 struct nfs_openres *o_res = &data->o_res;
2607 status = nfs4_run_open_task(data, NULL);
2608 if (status != 0 || !data->rpc_done)
2611 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2613 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2614 status = _nfs4_proc_open_confirm(data);
2620 * Additional permission checks in order to distinguish between an
2621 * open for read, and an open for execute. This works around the
2622 * fact that NFSv4 OPEN treats read and execute permissions as being
2624 * Note that in the non-execute case, we want to turn off permission
2625 * checking if we just created a new file (POSIX open() semantics).
2627 static int nfs4_opendata_access(const struct cred *cred,
2628 struct nfs4_opendata *opendata,
2629 struct nfs4_state *state, fmode_t fmode,
2632 struct nfs_access_entry cache;
2635 /* access call failed or for some reason the server doesn't
2636 * support any access modes -- defer access call until later */
2637 if (opendata->o_res.access_supported == 0)
2642 * Use openflags to check for exec, because fmode won't
2643 * always have FMODE_EXEC set when file open for exec.
2645 if (openflags & __FMODE_EXEC) {
2646 /* ONLY check for exec rights */
2647 if (S_ISDIR(state->inode->i_mode))
2648 mask = NFS4_ACCESS_LOOKUP;
2650 mask = NFS4_ACCESS_EXECUTE;
2651 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2652 mask = NFS4_ACCESS_READ;
2654 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2655 nfs_access_add_cache(state->inode, &cache, cred);
2657 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2658 if ((mask & ~cache.mask & flags) == 0)
2665 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2667 static int _nfs4_proc_open(struct nfs4_opendata *data,
2668 struct nfs_open_context *ctx)
2670 struct inode *dir = d_inode(data->dir);
2671 struct nfs_server *server = NFS_SERVER(dir);
2672 struct nfs_openargs *o_arg = &data->o_arg;
2673 struct nfs_openres *o_res = &data->o_res;
2676 status = nfs4_run_open_task(data, ctx);
2677 if (!data->rpc_done)
2680 if (status == -NFS4ERR_BADNAME &&
2681 !(o_arg->open_flags & O_CREAT))
2686 nfs_fattr_map_and_free_names(server, &data->f_attr);
2688 if (o_arg->open_flags & O_CREAT) {
2689 if (o_arg->open_flags & O_EXCL)
2690 data->file_created = true;
2691 else if (o_res->cinfo.before != o_res->cinfo.after)
2692 data->file_created = true;
2693 if (data->file_created ||
2694 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2695 nfs4_update_changeattr(dir, &o_res->cinfo,
2696 o_res->f_attr->time_start,
2697 NFS_INO_INVALID_DATA);
2699 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2700 server->caps &= ~NFS_CAP_POSIX_LOCK;
2701 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2702 status = _nfs4_proc_open_confirm(data);
2706 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2707 nfs4_sequence_free_slot(&o_res->seq_res);
2708 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, NULL);
2715 * reclaim state on the server after a network partition.
2716 * Assumes caller holds the appropriate lock
2718 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2720 struct nfs4_opendata *opendata;
2723 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2724 NFS4_OPEN_CLAIM_FH);
2725 if (IS_ERR(opendata))
2726 return PTR_ERR(opendata);
2727 ret = nfs4_open_recover(opendata, state);
2729 d_drop(ctx->dentry);
2730 nfs4_opendata_put(opendata);
2734 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2736 struct nfs_server *server = NFS_SERVER(state->inode);
2737 struct nfs4_exception exception = { };
2741 err = _nfs4_open_expired(ctx, state);
2742 trace_nfs4_open_expired(ctx, 0, err);
2743 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2748 case -NFS4ERR_GRACE:
2749 case -NFS4ERR_DELAY:
2750 nfs4_handle_exception(server, err, &exception);
2753 } while (exception.retry);
2758 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2760 struct nfs_open_context *ctx;
2763 ctx = nfs4_state_find_open_context(state);
2766 ret = nfs4_do_open_expired(ctx, state);
2767 put_nfs_open_context(ctx);
2771 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2772 const nfs4_stateid *stateid)
2774 nfs_remove_bad_delegation(state->inode, stateid);
2775 nfs_state_clear_delegation(state);
2778 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2780 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2781 nfs_finish_clear_delegation_stateid(state, NULL);
2784 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2786 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2787 nfs40_clear_delegation_stateid(state);
2788 nfs_state_clear_open_state_flags(state);
2789 return nfs4_open_expired(sp, state);
2792 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2793 nfs4_stateid *stateid,
2794 const struct cred *cred)
2796 return -NFS4ERR_BAD_STATEID;
2799 #if defined(CONFIG_NFS_V4_1)
2800 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2801 nfs4_stateid *stateid,
2802 const struct cred *cred)
2806 switch (stateid->type) {
2809 case NFS4_INVALID_STATEID_TYPE:
2810 case NFS4_SPECIAL_STATEID_TYPE:
2811 return -NFS4ERR_BAD_STATEID;
2812 case NFS4_REVOKED_STATEID_TYPE:
2816 status = nfs41_test_stateid(server, stateid, cred);
2818 case -NFS4ERR_EXPIRED:
2819 case -NFS4ERR_ADMIN_REVOKED:
2820 case -NFS4ERR_DELEG_REVOKED:
2826 /* Ack the revoked state to the server */
2827 nfs41_free_stateid(server, stateid, cred, true);
2828 return -NFS4ERR_EXPIRED;
2831 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2833 struct nfs_server *server = NFS_SERVER(state->inode);
2834 nfs4_stateid stateid;
2835 struct nfs_delegation *delegation;
2836 const struct cred *cred = NULL;
2837 int status, ret = NFS_OK;
2839 /* Get the delegation credential for use by test/free_stateid */
2841 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2842 if (delegation == NULL) {
2844 nfs_state_clear_delegation(state);
2848 spin_lock(&delegation->lock);
2849 nfs4_stateid_copy(&stateid, &delegation->stateid);
2851 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2852 &delegation->flags)) {
2853 spin_unlock(&delegation->lock);
2858 if (delegation->cred)
2859 cred = get_cred(delegation->cred);
2860 spin_unlock(&delegation->lock);
2862 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2863 trace_nfs4_test_delegation_stateid(state, NULL, status);
2864 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2865 nfs_finish_clear_delegation_stateid(state, &stateid);
2873 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2877 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2878 nfs4_copy_delegation_stateid(state->inode, state->state,
2880 nfs4_stateid_match_other(&state->stateid, &tmp))
2881 nfs_state_set_delegation(state, &tmp, state->state);
2883 nfs_state_clear_delegation(state);
2887 * nfs41_check_expired_locks - possibly free a lock stateid
2889 * @state: NFSv4 state for an inode
2891 * Returns NFS_OK if recovery for this stateid is now finished.
2892 * Otherwise a negative NFS4ERR value is returned.
2894 static int nfs41_check_expired_locks(struct nfs4_state *state)
2896 int status, ret = NFS_OK;
2897 struct nfs4_lock_state *lsp, *prev = NULL;
2898 struct nfs_server *server = NFS_SERVER(state->inode);
2900 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2903 spin_lock(&state->state_lock);
2904 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2905 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2906 const struct cred *cred = lsp->ls_state->owner->so_cred;
2908 refcount_inc(&lsp->ls_count);
2909 spin_unlock(&state->state_lock);
2911 nfs4_put_lock_state(prev);
2914 status = nfs41_test_and_free_expired_stateid(server,
2917 trace_nfs4_test_lock_stateid(state, lsp, status);
2918 if (status == -NFS4ERR_EXPIRED ||
2919 status == -NFS4ERR_BAD_STATEID) {
2920 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2921 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2922 if (!recover_lost_locks)
2923 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2924 } else if (status != NFS_OK) {
2926 nfs4_put_lock_state(prev);
2929 spin_lock(&state->state_lock);
2932 spin_unlock(&state->state_lock);
2933 nfs4_put_lock_state(prev);
2939 * nfs41_check_open_stateid - possibly free an open stateid
2941 * @state: NFSv4 state for an inode
2943 * Returns NFS_OK if recovery for this stateid is now finished.
2944 * Otherwise a negative NFS4ERR value is returned.
2946 static int nfs41_check_open_stateid(struct nfs4_state *state)
2948 struct nfs_server *server = NFS_SERVER(state->inode);
2949 nfs4_stateid *stateid = &state->open_stateid;
2950 const struct cred *cred = state->owner->so_cred;
2953 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2954 return -NFS4ERR_BAD_STATEID;
2955 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2956 trace_nfs4_test_open_stateid(state, NULL, status);
2957 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2958 nfs_state_clear_open_state_flags(state);
2959 stateid->type = NFS4_INVALID_STATEID_TYPE;
2962 if (nfs_open_stateid_recover_openmode(state))
2963 return -NFS4ERR_OPENMODE;
2967 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2971 status = nfs41_check_delegation_stateid(state);
2972 if (status != NFS_OK)
2974 nfs41_delegation_recover_stateid(state);
2976 status = nfs41_check_expired_locks(state);
2977 if (status != NFS_OK)
2979 status = nfs41_check_open_stateid(state);
2980 if (status != NFS_OK)
2981 status = nfs4_open_expired(sp, state);
2987 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2988 * fields corresponding to attributes that were used to store the verifier.
2989 * Make sure we clobber those fields in the later setattr call
2991 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2992 struct iattr *sattr, struct nfs4_label **label)
2994 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2999 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
3000 attrset[i] = opendata->o_res.attrset[i];
3001 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3002 attrset[i] &= ~bitmask[i];
3005 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3006 sattr->ia_valid : 0;
3008 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3009 if (sattr->ia_valid & ATTR_ATIME_SET)
3010 ret |= ATTR_ATIME_SET;
3015 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3016 if (sattr->ia_valid & ATTR_MTIME_SET)
3017 ret |= ATTR_MTIME_SET;
3022 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3027 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3028 int flags, struct nfs_open_context *ctx)
3030 struct nfs4_state_owner *sp = opendata->owner;
3031 struct nfs_server *server = sp->so_server;
3032 struct dentry *dentry;
3033 struct nfs4_state *state;
3034 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3035 struct inode *dir = d_inode(opendata->dir);
3036 unsigned long dir_verifier;
3040 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3041 dir_verifier = nfs_save_change_attribute(dir);
3043 ret = _nfs4_proc_open(opendata, ctx);
3047 state = _nfs4_opendata_to_nfs4_state(opendata);
3048 ret = PTR_ERR(state);
3052 if (server->caps & NFS_CAP_POSIX_LOCK)
3053 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3054 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3055 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3056 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_PRESERVE_UNLINKED)
3057 set_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(state->inode)->flags);
3059 dentry = opendata->dentry;
3060 if (d_really_is_negative(dentry)) {
3061 struct dentry *alias;
3063 alias = d_exact_alias(dentry, state->inode);
3065 alias = d_splice_alias(igrab(state->inode), dentry);
3066 /* d_splice_alias() can't fail here - it's a non-directory */
3069 ctx->dentry = dentry = alias;
3073 switch(opendata->o_arg.claim) {
3076 case NFS4_OPEN_CLAIM_NULL:
3077 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3078 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3079 if (!opendata->rpc_done)
3081 if (opendata->o_res.delegation_type != 0)
3082 dir_verifier = nfs_save_change_attribute(dir);
3083 nfs_set_verifier(dentry, dir_verifier);
3086 /* Parse layoutget results before we check for access */
3087 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3089 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3094 if (d_inode(dentry) == state->inode) {
3095 nfs_inode_attach_open_context(ctx);
3096 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3097 nfs4_schedule_stateid_recovery(server, state);
3101 if (opendata->lgp) {
3102 nfs4_lgopen_release(opendata->lgp);
3103 opendata->lgp = NULL;
3105 if (!opendata->cancelled)
3106 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3111 * Returns a referenced nfs4_state
3113 static int _nfs4_do_open(struct inode *dir,
3114 struct nfs_open_context *ctx,
3116 const struct nfs4_open_createattrs *c,
3119 struct nfs4_state_owner *sp;
3120 struct nfs4_state *state = NULL;
3121 struct nfs_server *server = NFS_SERVER(dir);
3122 struct nfs4_opendata *opendata;
3123 struct dentry *dentry = ctx->dentry;
3124 const struct cred *cred = ctx->cred;
3125 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3126 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3127 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3128 struct iattr *sattr = c->sattr;
3129 struct nfs4_label *label = c->label;
3132 /* Protect against reboot recovery conflicts */
3134 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3136 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3139 status = nfs4_client_recover_expired_lease(server->nfs_client);
3141 goto err_put_state_owner;
3142 if (d_really_is_positive(dentry))
3143 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3145 if (d_really_is_positive(dentry))
3146 claim = NFS4_OPEN_CLAIM_FH;
3147 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3148 c, claim, GFP_KERNEL);
3149 if (opendata == NULL)
3150 goto err_put_state_owner;
3152 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3153 if (!opendata->f_attr.mdsthreshold) {
3154 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3155 if (!opendata->f_attr.mdsthreshold)
3156 goto err_opendata_put;
3158 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3160 if (d_really_is_positive(dentry))
3161 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3163 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3165 goto err_opendata_put;
3168 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3169 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3170 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3172 * send create attributes which was not set by open
3173 * with an extra setattr.
3175 if (attrs || label) {
3176 unsigned ia_old = sattr->ia_valid;
3178 sattr->ia_valid = attrs;
3179 nfs_fattr_init(opendata->o_res.f_attr);
3180 status = nfs4_do_setattr(state->inode, cred,
3181 opendata->o_res.f_attr, sattr,
3184 nfs_setattr_update_inode(state->inode, sattr,
3185 opendata->o_res.f_attr);
3186 nfs_setsecurity(state->inode, opendata->o_res.f_attr);
3188 sattr->ia_valid = ia_old;
3191 if (opened && opendata->file_created)
3194 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3195 *ctx_th = opendata->f_attr.mdsthreshold;
3196 opendata->f_attr.mdsthreshold = NULL;
3199 nfs4_opendata_put(opendata);
3200 nfs4_put_state_owner(sp);
3203 nfs4_opendata_put(opendata);
3204 err_put_state_owner:
3205 nfs4_put_state_owner(sp);
3211 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3212 struct nfs_open_context *ctx,
3214 struct iattr *sattr,
3215 struct nfs4_label *label,
3218 struct nfs_server *server = NFS_SERVER(dir);
3219 struct nfs4_exception exception = {
3220 .interruptible = true,
3222 struct nfs4_state *res;
3223 struct nfs4_open_createattrs c = {
3227 [0] = (__u32)jiffies,
3228 [1] = (__u32)current->pid,
3234 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3236 trace_nfs4_open_file(ctx, flags, status);
3239 /* NOTE: BAD_SEQID means the server and client disagree about the
3240 * book-keeping w.r.t. state-changing operations
3241 * (OPEN/CLOSE/LOCK/LOCKU...)
3242 * It is actually a sign of a bug on the client or on the server.
3244 * If we receive a BAD_SEQID error in the particular case of
3245 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3246 * have unhashed the old state_owner for us, and that we can
3247 * therefore safely retry using a new one. We should still warn
3248 * the user though...
3250 if (status == -NFS4ERR_BAD_SEQID) {
3251 pr_warn_ratelimited("NFS: v4 server %s "
3252 " returned a bad sequence-id error!\n",
3253 NFS_SERVER(dir)->nfs_client->cl_hostname);
3254 exception.retry = 1;
3258 * BAD_STATEID on OPEN means that the server cancelled our
3259 * state before it received the OPEN_CONFIRM.
3260 * Recover by retrying the request as per the discussion
3261 * on Page 181 of RFC3530.
3263 if (status == -NFS4ERR_BAD_STATEID) {
3264 exception.retry = 1;
3267 if (status == -NFS4ERR_EXPIRED) {
3268 nfs4_schedule_lease_recovery(server->nfs_client);
3269 exception.retry = 1;
3272 if (status == -EAGAIN) {
3273 /* We must have found a delegation */
3274 exception.retry = 1;
3277 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3279 res = ERR_PTR(nfs4_handle_exception(server,
3280 status, &exception));
3281 } while (exception.retry);
3285 static int _nfs4_do_setattr(struct inode *inode,
3286 struct nfs_setattrargs *arg,
3287 struct nfs_setattrres *res,
3288 const struct cred *cred,
3289 struct nfs_open_context *ctx)
3291 struct nfs_server *server = NFS_SERVER(inode);
3292 struct rpc_message msg = {
3293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3298 const struct cred *delegation_cred = NULL;
3299 unsigned long timestamp = jiffies;
3303 nfs_fattr_init(res->fattr);
3305 /* Servers should only apply open mode checks for file size changes */
3306 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3308 nfs4_inode_make_writeable(inode);
3312 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3313 /* Use that stateid */
3314 } else if (ctx != NULL && ctx->state) {
3315 struct nfs_lock_context *l_ctx;
3316 if (!nfs4_valid_open_stateid(ctx->state))
3318 l_ctx = nfs_get_lock_context(ctx);
3320 return PTR_ERR(l_ctx);
3321 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3322 &arg->stateid, &delegation_cred);
3323 nfs_put_lock_context(l_ctx);
3326 else if (status == -EAGAIN)
3330 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3332 if (delegation_cred)
3333 msg.rpc_cred = delegation_cred;
3335 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3337 put_cred(delegation_cred);
3338 if (status == 0 && ctx != NULL)
3339 renew_lease(server, timestamp);
3340 trace_nfs4_setattr(inode, &arg->stateid, status);
3344 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3345 struct nfs_fattr *fattr, struct iattr *sattr,
3346 struct nfs_open_context *ctx, struct nfs4_label *ilabel)
3348 struct nfs_server *server = NFS_SERVER(inode);
3349 __u32 bitmask[NFS4_BITMASK_SZ];
3350 struct nfs4_state *state = ctx ? ctx->state : NULL;
3351 struct nfs_setattrargs arg = {
3352 .fh = NFS_FH(inode),
3358 struct nfs_setattrres res = {
3362 struct nfs4_exception exception = {
3365 .stateid = &arg.stateid,
3367 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
3370 if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3371 adjust_flags |= NFS_INO_INVALID_MODE;
3372 if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
3373 adjust_flags |= NFS_INO_INVALID_OTHER;
3376 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label),
3377 inode, adjust_flags);
3379 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3381 case -NFS4ERR_OPENMODE:
3382 if (!(sattr->ia_valid & ATTR_SIZE)) {
3383 pr_warn_once("NFSv4: server %s is incorrectly "
3384 "applying open mode checks to "
3385 "a SETATTR that is not "
3386 "changing file size.\n",
3387 server->nfs_client->cl_hostname);
3389 if (state && !(state->state & FMODE_WRITE)) {
3391 if (sattr->ia_valid & ATTR_OPEN)
3396 err = nfs4_handle_exception(server, err, &exception);
3397 } while (exception.retry);
3403 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3405 if (inode == NULL || !nfs_have_layout(inode))
3408 return pnfs_wait_on_layoutreturn(inode, task);
3412 * Update the seqid of an open stateid
3414 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3415 struct nfs4_state *state)
3422 if (!nfs4_valid_open_stateid(state))
3424 seq = read_seqbegin(&state->seqlock);
3425 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3426 nfs4_stateid_copy(dst, &state->open_stateid);
3427 if (read_seqretry(&state->seqlock, seq))
3431 seqid_open = state->open_stateid.seqid;
3432 if (read_seqretry(&state->seqlock, seq))
3435 dst_seqid = be32_to_cpu(dst->seqid);
3436 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3437 dst->seqid = seqid_open;
3443 * Update the seqid of an open stateid after receiving
3444 * NFS4ERR_OLD_STATEID
3446 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3447 struct nfs4_state *state)
3452 int seq, status = -EAGAIN;
3457 if (!nfs4_valid_open_stateid(state))
3459 seq = read_seqbegin(&state->seqlock);
3460 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3461 if (read_seqretry(&state->seqlock, seq))
3466 write_seqlock(&state->seqlock);
3467 seqid_open = state->open_stateid.seqid;
3469 dst_seqid = be32_to_cpu(dst->seqid);
3471 /* Did another OPEN bump the state's seqid? try again: */
3472 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3473 dst->seqid = seqid_open;
3474 write_sequnlock(&state->seqlock);
3479 /* server says we're behind but we haven't seen the update yet */
3480 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3481 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3482 write_sequnlock(&state->seqlock);
3483 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3485 if (fatal_signal_pending(current))
3488 if (schedule_timeout(5*HZ) != 0)
3491 finish_wait(&state->waitq, &wait);
3495 if (status == -EINTR)
3498 /* we slept the whole 5 seconds, we must have lost a seqid */
3499 dst->seqid = cpu_to_be32(dst_seqid + 1);
3507 struct nfs4_closedata {
3508 struct inode *inode;
3509 struct nfs4_state *state;
3510 struct nfs_closeargs arg;
3511 struct nfs_closeres res;
3513 struct nfs4_layoutreturn_args arg;
3514 struct nfs4_layoutreturn_res res;
3515 struct nfs4_xdr_opaque_data ld_private;
3519 struct nfs_fattr fattr;
3520 unsigned long timestamp;
3523 static void nfs4_free_closedata(void *data)
3525 struct nfs4_closedata *calldata = data;
3526 struct nfs4_state_owner *sp = calldata->state->owner;
3527 struct super_block *sb = calldata->state->inode->i_sb;
3529 if (calldata->lr.roc)
3530 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3531 calldata->res.lr_ret);
3532 nfs4_put_open_state(calldata->state);
3533 nfs_free_seqid(calldata->arg.seqid);
3534 nfs4_put_state_owner(sp);
3535 nfs_sb_deactive(sb);
3539 static void nfs4_close_done(struct rpc_task *task, void *data)
3541 struct nfs4_closedata *calldata = data;
3542 struct nfs4_state *state = calldata->state;
3543 struct nfs_server *server = NFS_SERVER(calldata->inode);
3544 nfs4_stateid *res_stateid = NULL;
3545 struct nfs4_exception exception = {
3547 .inode = calldata->inode,
3548 .stateid = &calldata->arg.stateid,
3551 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3553 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3555 /* Handle Layoutreturn errors */
3556 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3557 &calldata->res.lr_ret) == -EAGAIN)
3560 /* hmm. we are done with the inode, and in the process of freeing
3561 * the state_owner. we keep this around to process errors
3563 switch (task->tk_status) {
3565 res_stateid = &calldata->res.stateid;
3566 renew_lease(server, calldata->timestamp);
3568 case -NFS4ERR_ACCESS:
3569 if (calldata->arg.bitmask != NULL) {
3570 calldata->arg.bitmask = NULL;
3571 calldata->res.fattr = NULL;
3576 case -NFS4ERR_OLD_STATEID:
3577 /* Did we race with OPEN? */
3578 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3582 case -NFS4ERR_ADMIN_REVOKED:
3583 case -NFS4ERR_STALE_STATEID:
3584 case -NFS4ERR_EXPIRED:
3585 nfs4_free_revoked_stateid(server,
3586 &calldata->arg.stateid,
3587 task->tk_msg.rpc_cred);
3589 case -NFS4ERR_BAD_STATEID:
3590 if (calldata->arg.fmode == 0)
3594 task->tk_status = nfs4_async_handle_exception(task,
3595 server, task->tk_status, &exception);
3596 if (exception.retry)
3599 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3600 res_stateid, calldata->arg.fmode);
3602 task->tk_status = 0;
3603 nfs_release_seqid(calldata->arg.seqid);
3604 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3605 dprintk("%s: ret = %d\n", __func__, task->tk_status);
3608 task->tk_status = 0;
3609 rpc_restart_call_prepare(task);
3613 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3615 struct nfs4_closedata *calldata = data;
3616 struct nfs4_state *state = calldata->state;
3617 struct inode *inode = calldata->inode;
3618 struct nfs_server *server = NFS_SERVER(inode);
3619 struct pnfs_layout_hdr *lo;
3620 bool is_rdonly, is_wronly, is_rdwr;
3623 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3626 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3627 spin_lock(&state->owner->so_lock);
3628 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3629 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3630 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3631 /* Calculate the change in open mode */
3632 calldata->arg.fmode = 0;
3633 if (state->n_rdwr == 0) {
3634 if (state->n_rdonly == 0)
3635 call_close |= is_rdonly;
3637 calldata->arg.fmode |= FMODE_READ;
3638 if (state->n_wronly == 0)
3639 call_close |= is_wronly;
3641 calldata->arg.fmode |= FMODE_WRITE;
3642 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3643 call_close |= is_rdwr;
3645 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3647 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3648 if (!nfs4_valid_open_stateid(state))
3650 spin_unlock(&state->owner->so_lock);
3653 /* Note: exit _without_ calling nfs4_close_done */
3657 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3658 nfs_release_seqid(calldata->arg.seqid);
3662 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3663 if (lo && !pnfs_layout_is_valid(lo)) {
3664 calldata->arg.lr_args = NULL;
3665 calldata->res.lr_res = NULL;
3668 if (calldata->arg.fmode == 0)
3669 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3671 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3672 /* Close-to-open cache consistency revalidation */
3673 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3674 nfs4_bitmask_set(calldata->arg.bitmask_store,
3675 server->cache_consistency_bitmask,
3677 calldata->arg.bitmask = calldata->arg.bitmask_store;
3679 calldata->arg.bitmask = NULL;
3682 calldata->arg.share_access =
3683 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3684 calldata->arg.fmode, 0);
3686 if (calldata->res.fattr == NULL)
3687 calldata->arg.bitmask = NULL;
3688 else if (calldata->arg.bitmask == NULL)
3689 calldata->res.fattr = NULL;
3690 calldata->timestamp = jiffies;
3691 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3692 &calldata->arg.seq_args,
3693 &calldata->res.seq_res,
3695 nfs_release_seqid(calldata->arg.seqid);
3698 task->tk_action = NULL;
3700 nfs4_sequence_done(task, &calldata->res.seq_res);
3703 static const struct rpc_call_ops nfs4_close_ops = {
3704 .rpc_call_prepare = nfs4_close_prepare,
3705 .rpc_call_done = nfs4_close_done,
3706 .rpc_release = nfs4_free_closedata,
3710 * It is possible for data to be read/written from a mem-mapped file
3711 * after the sys_close call (which hits the vfs layer as a flush).
3712 * This means that we can't safely call nfsv4 close on a file until
3713 * the inode is cleared. This in turn means that we are not good
3714 * NFSv4 citizens - we do not indicate to the server to update the file's
3715 * share state even when we are done with one of the three share
3716 * stateid's in the inode.
3718 * NOTE: Caller must be holding the sp->so_owner semaphore!
3720 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3722 struct nfs_server *server = NFS_SERVER(state->inode);
3723 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3724 struct nfs4_closedata *calldata;
3725 struct nfs4_state_owner *sp = state->owner;
3726 struct rpc_task *task;
3727 struct rpc_message msg = {
3728 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3729 .rpc_cred = state->owner->so_cred,
3731 struct rpc_task_setup task_setup_data = {
3732 .rpc_client = server->client,
3733 .rpc_message = &msg,
3734 .callback_ops = &nfs4_close_ops,
3735 .workqueue = nfsiod_workqueue,
3736 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3738 int status = -ENOMEM;
3740 if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
3741 task_setup_data.flags |= RPC_TASK_MOVEABLE;
3743 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3744 &task_setup_data.rpc_client, &msg);
3746 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3747 if (calldata == NULL)
3749 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3750 calldata->inode = state->inode;
3751 calldata->state = state;
3752 calldata->arg.fh = NFS_FH(state->inode);
3753 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3754 goto out_free_calldata;
3755 /* Serialization for the sequence id */
3756 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3757 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3758 if (IS_ERR(calldata->arg.seqid))
3759 goto out_free_calldata;
3760 nfs_fattr_init(&calldata->fattr);
3761 calldata->arg.fmode = 0;
3762 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3763 calldata->res.fattr = &calldata->fattr;
3764 calldata->res.seqid = calldata->arg.seqid;
3765 calldata->res.server = server;
3766 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3767 calldata->lr.roc = pnfs_roc(state->inode,
3768 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3769 if (calldata->lr.roc) {
3770 calldata->arg.lr_args = &calldata->lr.arg;
3771 calldata->res.lr_res = &calldata->lr.res;
3773 nfs_sb_active(calldata->inode->i_sb);
3775 msg.rpc_argp = &calldata->arg;
3776 msg.rpc_resp = &calldata->res;
3777 task_setup_data.callback_data = calldata;
3778 task = rpc_run_task(&task_setup_data);
3780 return PTR_ERR(task);
3783 status = rpc_wait_for_completion_task(task);
3789 nfs4_put_open_state(state);
3790 nfs4_put_state_owner(sp);
3794 static struct inode *
3795 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3796 int open_flags, struct iattr *attr, int *opened)
3798 struct nfs4_state *state;
3799 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3801 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3803 /* Protect against concurrent sillydeletes */
3804 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3806 nfs4_label_release_security(label);
3809 return ERR_CAST(state);
3810 return state->inode;
3813 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3815 if (ctx->state == NULL)
3818 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3820 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3823 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3824 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3825 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3827 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3829 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3830 struct nfs4_server_caps_arg args = {
3834 struct nfs4_server_caps_res res = {};
3835 struct rpc_message msg = {
3836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3843 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3844 FATTR4_WORD0_FH_EXPIRE_TYPE |
3845 FATTR4_WORD0_LINK_SUPPORT |
3846 FATTR4_WORD0_SYMLINK_SUPPORT |
3847 FATTR4_WORD0_ACLSUPPORT |
3848 FATTR4_WORD0_CASE_INSENSITIVE |
3849 FATTR4_WORD0_CASE_PRESERVING;
3851 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3853 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3855 /* Sanity check the server answers */
3856 switch (minorversion) {
3858 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3859 res.attr_bitmask[2] = 0;
3862 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3865 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3867 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3868 server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3869 NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3870 server->fattr_valid = NFS_ATTR_FATTR_V4;
3871 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3872 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3873 server->caps |= NFS_CAP_ACLS;
3874 if (res.has_links != 0)
3875 server->caps |= NFS_CAP_HARDLINKS;
3876 if (res.has_symlinks != 0)
3877 server->caps |= NFS_CAP_SYMLINKS;
3878 if (res.case_insensitive)
3879 server->caps |= NFS_CAP_CASE_INSENSITIVE;
3880 if (res.case_preserving)
3881 server->caps |= NFS_CAP_CASE_PRESERVING;
3882 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3883 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3884 server->caps |= NFS_CAP_SECURITY_LABEL;
3886 if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS)
3887 server->caps |= NFS_CAP_FS_LOCATIONS;
3888 if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3889 server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3890 if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3891 server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3892 if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3893 server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3894 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3895 server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3896 NFS_ATTR_FATTR_OWNER_NAME);
3897 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3898 server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3899 NFS_ATTR_FATTR_GROUP_NAME);
3900 if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3901 server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3902 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3903 server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3904 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3905 server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3906 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3907 server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
3908 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3909 sizeof(server->attr_bitmask));
3910 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3912 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3913 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3914 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3915 server->cache_consistency_bitmask[2] = 0;
3917 /* Avoid a regression due to buggy server */
3918 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3919 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3920 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3921 sizeof(server->exclcreat_bitmask));
3923 server->acl_bitmask = res.acl_bitmask;
3924 server->fh_expire_type = res.fh_expire_type;
3930 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3932 struct nfs4_exception exception = {
3933 .interruptible = true,
3937 nfs4_server_set_init_caps(server);
3939 err = nfs4_handle_exception(server,
3940 _nfs4_server_capabilities(server, fhandle),
3942 } while (exception.retry);
3946 static void test_fs_location_for_trunking(struct nfs4_fs_location *location,
3947 struct nfs_client *clp,
3948 struct nfs_server *server)
3952 for (i = 0; i < location->nservers; i++) {
3953 struct nfs4_string *srv_loc = &location->servers[i];
3954 struct sockaddr addr;
3956 struct xprt_create xprt_args = {
3960 struct nfs4_add_xprt_data xprtdata = {
3963 struct rpc_add_xprt_test rpcdata = {
3964 .add_xprt_test = clp->cl_mvops->session_trunk,
3967 char *servername = NULL;
3972 addrlen = nfs_parse_server_name(srv_loc->data, srv_loc->len,
3973 &addr, sizeof(addr),
3974 clp->cl_net, server->port);
3977 xprt_args.dstaddr = &addr;
3978 xprt_args.addrlen = addrlen;
3979 servername = kmalloc(srv_loc->len + 1, GFP_KERNEL);
3982 memcpy(servername, srv_loc->data, srv_loc->len);
3983 servername[srv_loc->len] = '\0';
3984 xprt_args.servername = servername;
3986 xprtdata.cred = nfs4_get_clid_cred(clp);
3987 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
3988 rpc_clnt_setup_test_and_add_xprt,
3991 put_cred(xprtdata.cred);
3996 static int _nfs4_discover_trunking(struct nfs_server *server,
3997 struct nfs_fh *fhandle)
3999 struct nfs4_fs_locations *locations = NULL;
4001 const struct cred *cred;
4002 struct nfs_client *clp = server->nfs_client;
4003 const struct nfs4_state_maintenance_ops *ops =
4004 clp->cl_mvops->state_renewal_ops;
4005 int status = -ENOMEM, i;
4007 cred = ops->get_state_renewal_cred(clp);
4009 cred = nfs4_get_clid_cred(clp);
4014 page = alloc_page(GFP_KERNEL);
4015 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4016 if (page == NULL || locations == NULL)
4019 status = nfs4_proc_get_locations(server, fhandle, locations, page,
4024 for (i = 0; i < locations->nlocations; i++)
4025 test_fs_location_for_trunking(&locations->locations[i], clp,
4034 static int nfs4_discover_trunking(struct nfs_server *server,
4035 struct nfs_fh *fhandle)
4037 struct nfs4_exception exception = {
4038 .interruptible = true,
4040 struct nfs_client *clp = server->nfs_client;
4043 if (!nfs4_has_session(clp))
4046 err = nfs4_handle_exception(server,
4047 _nfs4_discover_trunking(server, fhandle),
4049 } while (exception.retry);
4054 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4055 struct nfs_fsinfo *info)
4058 struct nfs4_lookup_root_arg args = {
4061 struct nfs4_lookup_res res = {
4063 .fattr = info->fattr,
4066 struct rpc_message msg = {
4067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
4072 bitmask[0] = nfs4_fattr_bitmap[0];
4073 bitmask[1] = nfs4_fattr_bitmap[1];
4075 * Process the label in the upcoming getfattr
4077 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
4079 nfs_fattr_init(info->fattr);
4080 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4083 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4084 struct nfs_fsinfo *info)
4086 struct nfs4_exception exception = {
4087 .interruptible = true,
4091 err = _nfs4_lookup_root(server, fhandle, info);
4092 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
4095 case -NFS4ERR_WRONGSEC:
4098 err = nfs4_handle_exception(server, err, &exception);
4100 } while (exception.retry);
4105 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4106 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4108 struct rpc_auth_create_args auth_args = {
4109 .pseudoflavor = flavor,
4111 struct rpc_auth *auth;
4113 auth = rpcauth_create(&auth_args, server->client);
4116 return nfs4_lookup_root(server, fhandle, info);
4120 * Retry pseudoroot lookup with various security flavors. We do this when:
4122 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4123 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4125 * Returns zero on success, or a negative NFS4ERR value, or a
4126 * negative errno value.
4128 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4129 struct nfs_fsinfo *info)
4131 /* Per 3530bis 15.33.5 */
4132 static const rpc_authflavor_t flav_array[] = {
4136 RPC_AUTH_UNIX, /* courtesy */
4139 int status = -EPERM;
4142 if (server->auth_info.flavor_len > 0) {
4143 /* try each flavor specified by user */
4144 for (i = 0; i < server->auth_info.flavor_len; i++) {
4145 status = nfs4_lookup_root_sec(server, fhandle, info,
4146 server->auth_info.flavors[i]);
4147 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4152 /* no flavors specified by user, try default list */
4153 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4154 status = nfs4_lookup_root_sec(server, fhandle, info,
4156 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4163 * -EACCES could mean that the user doesn't have correct permissions
4164 * to access the mount. It could also mean that we tried to mount
4165 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4166 * existing mount programs don't handle -EACCES very well so it should
4167 * be mapped to -EPERM instead.
4169 if (status == -EACCES)
4175 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4176 * @server: initialized nfs_server handle
4177 * @fhandle: we fill in the pseudo-fs root file handle
4178 * @info: we fill in an FSINFO struct
4179 * @auth_probe: probe the auth flavours
4181 * Returns zero on success, or a negative errno.
4183 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4184 struct nfs_fsinfo *info,
4190 status = nfs4_lookup_root(server, fhandle, info);
4192 if (auth_probe || status == NFS4ERR_WRONGSEC)
4193 status = server->nfs_client->cl_mvops->find_root_sec(server,
4197 status = nfs4_server_capabilities(server, fhandle);
4199 status = nfs4_do_fsinfo(server, fhandle, info);
4201 return nfs4_map_errors(status);
4204 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4205 struct nfs_fsinfo *info)
4208 struct nfs_fattr *fattr = info->fattr;
4210 error = nfs4_server_capabilities(server, mntfh);
4212 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4216 error = nfs4_proc_getattr(server, mntfh, fattr, NULL);
4218 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4222 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4223 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4224 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4231 * Get locations and (maybe) other attributes of a referral.
4232 * Note that we'll actually follow the referral later when
4233 * we detect fsid mismatch in inode revalidation
4235 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4236 const struct qstr *name, struct nfs_fattr *fattr,
4237 struct nfs_fh *fhandle)
4239 int status = -ENOMEM;
4240 struct page *page = NULL;
4241 struct nfs4_fs_locations *locations = NULL;
4243 page = alloc_page(GFP_KERNEL);
4246 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4247 if (locations == NULL)
4250 locations->fattr = fattr;
4252 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4257 * If the fsid didn't change, this is a migration event, not a
4258 * referral. Cause us to drop into the exception handler, which
4259 * will kick off migration recovery.
4261 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &fattr->fsid)) {
4262 dprintk("%s: server did not return a different fsid for"
4263 " a referral at %s\n", __func__, name->name);
4264 status = -NFS4ERR_MOVED;
4267 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4268 nfs_fixup_referral_attributes(fattr);
4269 memset(fhandle, 0, sizeof(struct nfs_fh));
4277 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4278 struct nfs_fattr *fattr, struct inode *inode)
4280 __u32 bitmask[NFS4_BITMASK_SZ];
4281 struct nfs4_getattr_arg args = {
4285 struct nfs4_getattr_res res = {
4289 struct rpc_message msg = {
4290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4294 unsigned short task_flags = 0;
4296 if (nfs4_has_session(server->nfs_client))
4297 task_flags = RPC_TASK_MOVEABLE;
4299 /* Is this is an attribute revalidation, subject to softreval? */
4300 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4301 task_flags |= RPC_TASK_TIMEOUT;
4303 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0);
4304 nfs_fattr_init(fattr);
4305 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4306 return nfs4_do_call_sync(server->client, server, &msg,
4307 &args.seq_args, &res.seq_res, task_flags);
4310 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4311 struct nfs_fattr *fattr, struct inode *inode)
4313 struct nfs4_exception exception = {
4314 .interruptible = true,
4318 err = _nfs4_proc_getattr(server, fhandle, fattr, inode);
4319 trace_nfs4_getattr(server, fhandle, fattr, err);
4320 err = nfs4_handle_exception(server, err,
4322 } while (exception.retry);
4327 * The file is not closed if it is opened due to the a request to change
4328 * the size of the file. The open call will not be needed once the
4329 * VFS layer lookup-intents are implemented.
4331 * Close is called when the inode is destroyed.
4332 * If we haven't opened the file for O_WRONLY, we
4333 * need to in the size_change case to obtain a stateid.
4336 * Because OPEN is always done by name in nfsv4, it is
4337 * possible that we opened a different file by the same
4338 * name. We can recognize this race condition, but we
4339 * can't do anything about it besides returning an error.
4341 * This will be fixed with VFS changes (lookup-intent).
4344 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4345 struct iattr *sattr)
4347 struct inode *inode = d_inode(dentry);
4348 const struct cred *cred = NULL;
4349 struct nfs_open_context *ctx = NULL;
4352 if (pnfs_ld_layoutret_on_setattr(inode) &&
4353 sattr->ia_valid & ATTR_SIZE &&
4354 sattr->ia_size < i_size_read(inode))
4355 pnfs_commit_and_return_layout(inode);
4357 nfs_fattr_init(fattr);
4359 /* Deal with open(O_TRUNC) */
4360 if (sattr->ia_valid & ATTR_OPEN)
4361 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4363 /* Optimization: if the end result is no change, don't RPC */
4364 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4367 /* Search for an existing open(O_WRITE) file */
4368 if (sattr->ia_valid & ATTR_FILE) {
4370 ctx = nfs_file_open_context(sattr->ia_file);
4375 /* Return any delegations if we're going to change ACLs */
4376 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4377 nfs4_inode_make_writeable(inode);
4379 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL);
4381 nfs_setattr_update_inode(inode, sattr, fattr);
4382 nfs_setsecurity(inode, fattr);
4387 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4388 struct dentry *dentry, struct nfs_fh *fhandle,
4389 struct nfs_fattr *fattr)
4391 struct nfs_server *server = NFS_SERVER(dir);
4393 struct nfs4_lookup_arg args = {
4394 .bitmask = server->attr_bitmask,
4395 .dir_fh = NFS_FH(dir),
4396 .name = &dentry->d_name,
4398 struct nfs4_lookup_res res = {
4403 struct rpc_message msg = {
4404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4408 unsigned short task_flags = 0;
4410 if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
4411 task_flags = RPC_TASK_MOVEABLE;
4413 /* Is this is an attribute revalidation, subject to softreval? */
4414 if (nfs_lookup_is_soft_revalidate(dentry))
4415 task_flags |= RPC_TASK_TIMEOUT;
4417 args.bitmask = nfs4_bitmask(server, fattr->label);
4419 nfs_fattr_init(fattr);
4421 dprintk("NFS call lookup %pd2\n", dentry);
4422 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4423 status = nfs4_do_call_sync(clnt, server, &msg,
4424 &args.seq_args, &res.seq_res, task_flags);
4425 dprintk("NFS reply lookup: %d\n", status);
4429 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4431 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4432 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4433 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4437 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4438 struct dentry *dentry, struct nfs_fh *fhandle,
4439 struct nfs_fattr *fattr)
4441 struct nfs4_exception exception = {
4442 .interruptible = true,
4444 struct rpc_clnt *client = *clnt;
4445 const struct qstr *name = &dentry->d_name;
4448 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr);
4449 trace_nfs4_lookup(dir, name, err);
4451 case -NFS4ERR_BADNAME:
4454 case -NFS4ERR_MOVED:
4455 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4456 if (err == -NFS4ERR_MOVED)
4457 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4459 case -NFS4ERR_WRONGSEC:
4461 if (client != *clnt)
4463 client = nfs4_negotiate_security(client, dir, name);
4465 return PTR_ERR(client);
4467 exception.retry = 1;
4470 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4472 } while (exception.retry);
4477 else if (client != *clnt)
4478 rpc_shutdown_client(client);
4483 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4484 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4487 struct rpc_clnt *client = NFS_CLIENT(dir);
4489 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4490 if (client != NFS_CLIENT(dir)) {
4491 rpc_shutdown_client(client);
4492 nfs_fixup_secinfo_attributes(fattr);
4498 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4499 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4501 struct rpc_clnt *client = NFS_CLIENT(dir);
4504 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4506 return ERR_PTR(status);
4507 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4510 static int _nfs4_proc_lookupp(struct inode *inode,
4511 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4513 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4514 struct nfs_server *server = NFS_SERVER(inode);
4516 struct nfs4_lookupp_arg args = {
4517 .bitmask = server->attr_bitmask,
4518 .fh = NFS_FH(inode),
4520 struct nfs4_lookupp_res res = {
4525 struct rpc_message msg = {
4526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4530 unsigned short task_flags = 0;
4532 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4533 task_flags |= RPC_TASK_TIMEOUT;
4535 args.bitmask = nfs4_bitmask(server, fattr->label);
4537 nfs_fattr_init(fattr);
4539 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4540 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4541 &res.seq_res, task_flags);
4542 dprintk("NFS reply lookupp: %d\n", status);
4546 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4547 struct nfs_fattr *fattr)
4549 struct nfs4_exception exception = {
4550 .interruptible = true,
4554 err = _nfs4_proc_lookupp(inode, fhandle, fattr);
4555 trace_nfs4_lookupp(inode, err);
4556 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4558 } while (exception.retry);
4562 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4563 const struct cred *cred)
4565 struct nfs_server *server = NFS_SERVER(inode);
4566 struct nfs4_accessargs args = {
4567 .fh = NFS_FH(inode),
4568 .access = entry->mask,
4570 struct nfs4_accessres res = {
4573 struct rpc_message msg = {
4574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4581 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4582 res.fattr = nfs_alloc_fattr();
4583 if (res.fattr == NULL)
4585 args.bitmask = server->cache_consistency_bitmask;
4587 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4589 nfs_access_set_mask(entry, res.access);
4591 nfs_refresh_inode(inode, res.fattr);
4593 nfs_free_fattr(res.fattr);
4597 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4598 const struct cred *cred)
4600 struct nfs4_exception exception = {
4601 .interruptible = true,
4605 err = _nfs4_proc_access(inode, entry, cred);
4606 trace_nfs4_access(inode, err);
4607 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4609 } while (exception.retry);
4614 * TODO: For the time being, we don't try to get any attributes
4615 * along with any of the zero-copy operations READ, READDIR,
4618 * In the case of the first three, we want to put the GETATTR
4619 * after the read-type operation -- this is because it is hard
4620 * to predict the length of a GETATTR response in v4, and thus
4621 * align the READ data correctly. This means that the GETATTR
4622 * may end up partially falling into the page cache, and we should
4623 * shift it into the 'tail' of the xdr_buf before processing.
4624 * To do this efficiently, we need to know the total length
4625 * of data received, which doesn't seem to be available outside
4628 * In the case of WRITE, we also want to put the GETATTR after
4629 * the operation -- in this case because we want to make sure
4630 * we get the post-operation mtime and size.
4632 * Both of these changes to the XDR layer would in fact be quite
4633 * minor, but I decided to leave them for a subsequent patch.
4635 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4636 unsigned int pgbase, unsigned int pglen)
4638 struct nfs4_readlink args = {
4639 .fh = NFS_FH(inode),
4644 struct nfs4_readlink_res res;
4645 struct rpc_message msg = {
4646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4651 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4654 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4655 unsigned int pgbase, unsigned int pglen)
4657 struct nfs4_exception exception = {
4658 .interruptible = true,
4662 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4663 trace_nfs4_readlink(inode, err);
4664 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4666 } while (exception.retry);
4671 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4674 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4677 struct nfs_server *server = NFS_SERVER(dir);
4678 struct nfs4_label l, *ilabel = NULL;
4679 struct nfs_open_context *ctx;
4680 struct nfs4_state *state;
4683 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4685 return PTR_ERR(ctx);
4687 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4689 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4690 sattr->ia_mode &= ~current_umask();
4691 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4692 if (IS_ERR(state)) {
4693 status = PTR_ERR(state);
4697 nfs4_label_release_security(ilabel);
4698 put_nfs_open_context(ctx);
4703 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4705 struct nfs_server *server = NFS_SERVER(dir);
4706 struct nfs_removeargs args = {
4710 struct nfs_removeres res = {
4713 struct rpc_message msg = {
4714 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4718 unsigned long timestamp = jiffies;
4721 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4723 spin_lock(&dir->i_lock);
4724 /* Removing a directory decrements nlink in the parent */
4725 if (ftype == NF4DIR && dir->i_nlink > 2)
4726 nfs4_dec_nlink_locked(dir);
4727 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4728 NFS_INO_INVALID_DATA);
4729 spin_unlock(&dir->i_lock);
4734 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4736 struct nfs4_exception exception = {
4737 .interruptible = true,
4739 struct inode *inode = d_inode(dentry);
4743 if (inode->i_nlink == 1)
4744 nfs4_inode_return_delegation(inode);
4746 nfs4_inode_make_writeable(inode);
4749 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4750 trace_nfs4_remove(dir, &dentry->d_name, err);
4751 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4753 } while (exception.retry);
4757 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4759 struct nfs4_exception exception = {
4760 .interruptible = true,
4765 err = _nfs4_proc_remove(dir, name, NF4DIR);
4766 trace_nfs4_remove(dir, name, err);
4767 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4769 } while (exception.retry);
4773 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4774 struct dentry *dentry,
4775 struct inode *inode)
4777 struct nfs_removeargs *args = msg->rpc_argp;
4778 struct nfs_removeres *res = msg->rpc_resp;
4780 res->server = NFS_SB(dentry->d_sb);
4781 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4782 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4784 nfs_fattr_init(res->dir_attr);
4787 nfs4_inode_return_delegation(inode);
4788 nfs_d_prune_case_insensitive_aliases(inode);
4792 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4794 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4795 &data->args.seq_args,
4800 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4802 struct nfs_unlinkdata *data = task->tk_calldata;
4803 struct nfs_removeres *res = &data->res;
4805 if (!nfs4_sequence_done(task, &res->seq_res))
4807 if (nfs4_async_handle_error(task, res->server, NULL,
4808 &data->timeout) == -EAGAIN)
4810 if (task->tk_status == 0)
4811 nfs4_update_changeattr(dir, &res->cinfo,
4812 res->dir_attr->time_start,
4813 NFS_INO_INVALID_DATA);
4817 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4818 struct dentry *old_dentry,
4819 struct dentry *new_dentry)
4821 struct nfs_renameargs *arg = msg->rpc_argp;
4822 struct nfs_renameres *res = msg->rpc_resp;
4823 struct inode *old_inode = d_inode(old_dentry);
4824 struct inode *new_inode = d_inode(new_dentry);
4827 nfs4_inode_make_writeable(old_inode);
4829 nfs4_inode_return_delegation(new_inode);
4830 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4831 res->server = NFS_SB(old_dentry->d_sb);
4832 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4835 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4837 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4838 &data->args.seq_args,
4843 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4844 struct inode *new_dir)
4846 struct nfs_renamedata *data = task->tk_calldata;
4847 struct nfs_renameres *res = &data->res;
4849 if (!nfs4_sequence_done(task, &res->seq_res))
4851 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4854 if (task->tk_status == 0) {
4855 nfs_d_prune_case_insensitive_aliases(d_inode(data->old_dentry));
4856 if (new_dir != old_dir) {
4857 /* Note: If we moved a directory, nlink will change */
4858 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4859 res->old_fattr->time_start,
4860 NFS_INO_INVALID_NLINK |
4861 NFS_INO_INVALID_DATA);
4862 nfs4_update_changeattr(new_dir, &res->new_cinfo,
4863 res->new_fattr->time_start,
4864 NFS_INO_INVALID_NLINK |
4865 NFS_INO_INVALID_DATA);
4867 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4868 res->old_fattr->time_start,
4869 NFS_INO_INVALID_DATA);
4874 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4876 struct nfs_server *server = NFS_SERVER(inode);
4877 __u32 bitmask[NFS4_BITMASK_SZ];
4878 struct nfs4_link_arg arg = {
4879 .fh = NFS_FH(inode),
4880 .dir_fh = NFS_FH(dir),
4884 struct nfs4_link_res res = {
4887 struct rpc_message msg = {
4888 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4892 int status = -ENOMEM;
4894 res.fattr = nfs_alloc_fattr_with_label(server);
4895 if (res.fattr == NULL)
4898 nfs4_inode_make_writeable(inode);
4899 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label), inode,
4900 NFS_INO_INVALID_CHANGE);
4901 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4903 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4904 NFS_INO_INVALID_DATA);
4905 nfs4_inc_nlink(inode);
4906 status = nfs_post_op_update_inode(inode, res.fattr);
4908 nfs_setsecurity(inode, res.fattr);
4912 nfs_free_fattr(res.fattr);
4916 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4918 struct nfs4_exception exception = {
4919 .interruptible = true,
4923 err = nfs4_handle_exception(NFS_SERVER(inode),
4924 _nfs4_proc_link(inode, dir, name),
4926 } while (exception.retry);
4930 struct nfs4_createdata {
4931 struct rpc_message msg;
4932 struct nfs4_create_arg arg;
4933 struct nfs4_create_res res;
4935 struct nfs_fattr fattr;
4938 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4939 const struct qstr *name, struct iattr *sattr, u32 ftype)
4941 struct nfs4_createdata *data;
4943 data = kzalloc(sizeof(*data), GFP_KERNEL);
4945 struct nfs_server *server = NFS_SERVER(dir);
4947 data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL);
4948 if (IS_ERR(data->fattr.label))
4951 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4952 data->msg.rpc_argp = &data->arg;
4953 data->msg.rpc_resp = &data->res;
4954 data->arg.dir_fh = NFS_FH(dir);
4955 data->arg.server = server;
4956 data->arg.name = name;
4957 data->arg.attrs = sattr;
4958 data->arg.ftype = ftype;
4959 data->arg.bitmask = nfs4_bitmask(server, data->fattr.label);
4960 data->arg.umask = current_umask();
4961 data->res.server = server;
4962 data->res.fh = &data->fh;
4963 data->res.fattr = &data->fattr;
4964 nfs_fattr_init(data->res.fattr);
4972 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4974 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4975 &data->arg.seq_args, &data->res.seq_res, 1);
4977 spin_lock(&dir->i_lock);
4978 /* Creating a directory bumps nlink in the parent */
4979 if (data->arg.ftype == NF4DIR)
4980 nfs4_inc_nlink_locked(dir);
4981 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4982 data->res.fattr->time_start,
4983 NFS_INO_INVALID_DATA);
4984 spin_unlock(&dir->i_lock);
4985 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
4990 static void nfs4_free_createdata(struct nfs4_createdata *data)
4992 nfs4_label_free(data->fattr.label);
4996 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4997 struct page *page, unsigned int len, struct iattr *sattr,
4998 struct nfs4_label *label)
5000 struct nfs4_createdata *data;
5001 int status = -ENAMETOOLONG;
5003 if (len > NFS4_MAXPATHLEN)
5007 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
5011 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
5012 data->arg.u.symlink.pages = &page;
5013 data->arg.u.symlink.len = len;
5014 data->arg.label = label;
5016 status = nfs4_do_create(dir, dentry, data);
5018 nfs4_free_createdata(data);
5023 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5024 struct page *page, unsigned int len, struct iattr *sattr)
5026 struct nfs4_exception exception = {
5027 .interruptible = true,
5029 struct nfs4_label l, *label = NULL;
5032 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5035 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
5036 trace_nfs4_symlink(dir, &dentry->d_name, err);
5037 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5039 } while (exception.retry);
5041 nfs4_label_release_security(label);
5045 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5046 struct iattr *sattr, struct nfs4_label *label)
5048 struct nfs4_createdata *data;
5049 int status = -ENOMEM;
5051 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
5055 data->arg.label = label;
5056 status = nfs4_do_create(dir, dentry, data);
5058 nfs4_free_createdata(data);
5063 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5064 struct iattr *sattr)
5066 struct nfs_server *server = NFS_SERVER(dir);
5067 struct nfs4_exception exception = {
5068 .interruptible = true,
5070 struct nfs4_label l, *label = NULL;
5073 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5075 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5076 sattr->ia_mode &= ~current_umask();
5078 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
5079 trace_nfs4_mkdir(dir, &dentry->d_name, err);
5080 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5082 } while (exception.retry);
5083 nfs4_label_release_security(label);
5088 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5089 struct nfs_readdir_res *nr_res)
5091 struct inode *dir = d_inode(nr_arg->dentry);
5092 struct nfs_server *server = NFS_SERVER(dir);
5093 struct nfs4_readdir_arg args = {
5095 .pages = nr_arg->pages,
5097 .count = nr_arg->page_len,
5098 .plus = nr_arg->plus,
5100 struct nfs4_readdir_res res;
5101 struct rpc_message msg = {
5102 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5105 .rpc_cred = nr_arg->cred,
5109 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5110 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5111 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5112 args.bitmask = server->attr_bitmask_nl;
5114 args.bitmask = server->attr_bitmask;
5116 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5117 res.pgbase = args.pgbase;
5118 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5121 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5122 status += args.pgbase;
5125 nfs_invalidate_atime(dir);
5127 dprintk("%s: returns %d\n", __func__, status);
5131 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5132 struct nfs_readdir_res *res)
5134 struct nfs4_exception exception = {
5135 .interruptible = true,
5139 err = _nfs4_proc_readdir(arg, res);
5140 trace_nfs4_readdir(d_inode(arg->dentry), err);
5141 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5143 } while (exception.retry);
5147 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5148 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5150 struct nfs4_createdata *data;
5151 int mode = sattr->ia_mode;
5152 int status = -ENOMEM;
5154 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5159 data->arg.ftype = NF4FIFO;
5160 else if (S_ISBLK(mode)) {
5161 data->arg.ftype = NF4BLK;
5162 data->arg.u.device.specdata1 = MAJOR(rdev);
5163 data->arg.u.device.specdata2 = MINOR(rdev);
5165 else if (S_ISCHR(mode)) {
5166 data->arg.ftype = NF4CHR;
5167 data->arg.u.device.specdata1 = MAJOR(rdev);
5168 data->arg.u.device.specdata2 = MINOR(rdev);
5169 } else if (!S_ISSOCK(mode)) {
5174 data->arg.label = label;
5175 status = nfs4_do_create(dir, dentry, data);
5177 nfs4_free_createdata(data);
5182 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5183 struct iattr *sattr, dev_t rdev)
5185 struct nfs_server *server = NFS_SERVER(dir);
5186 struct nfs4_exception exception = {
5187 .interruptible = true,
5189 struct nfs4_label l, *label = NULL;
5192 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5194 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5195 sattr->ia_mode &= ~current_umask();
5197 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5198 trace_nfs4_mknod(dir, &dentry->d_name, err);
5199 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5201 } while (exception.retry);
5203 nfs4_label_release_security(label);
5208 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5209 struct nfs_fsstat *fsstat)
5211 struct nfs4_statfs_arg args = {
5213 .bitmask = server->attr_bitmask,
5215 struct nfs4_statfs_res res = {
5218 struct rpc_message msg = {
5219 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5224 nfs_fattr_init(fsstat->fattr);
5225 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5228 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5230 struct nfs4_exception exception = {
5231 .interruptible = true,
5235 err = nfs4_handle_exception(server,
5236 _nfs4_proc_statfs(server, fhandle, fsstat),
5238 } while (exception.retry);
5242 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5243 struct nfs_fsinfo *fsinfo)
5245 struct nfs4_fsinfo_arg args = {
5247 .bitmask = server->attr_bitmask,
5249 struct nfs4_fsinfo_res res = {
5252 struct rpc_message msg = {
5253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5258 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5261 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5263 struct nfs4_exception exception = {
5264 .interruptible = true,
5269 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5270 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5272 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5275 err = nfs4_handle_exception(server, err, &exception);
5276 } while (exception.retry);
5280 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5284 nfs_fattr_init(fsinfo->fattr);
5285 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5287 /* block layout checks this! */
5288 server->pnfs_blksize = fsinfo->blksize;
5289 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5295 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5296 struct nfs_pathconf *pathconf)
5298 struct nfs4_pathconf_arg args = {
5300 .bitmask = server->attr_bitmask,
5302 struct nfs4_pathconf_res res = {
5303 .pathconf = pathconf,
5305 struct rpc_message msg = {
5306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5311 /* None of the pathconf attributes are mandatory to implement */
5312 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5313 memset(pathconf, 0, sizeof(*pathconf));
5317 nfs_fattr_init(pathconf->fattr);
5318 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5321 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5322 struct nfs_pathconf *pathconf)
5324 struct nfs4_exception exception = {
5325 .interruptible = true,
5330 err = nfs4_handle_exception(server,
5331 _nfs4_proc_pathconf(server, fhandle, pathconf),
5333 } while (exception.retry);
5337 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5338 const struct nfs_open_context *ctx,
5339 const struct nfs_lock_context *l_ctx,
5342 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5344 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5346 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5347 const struct nfs_open_context *ctx,
5348 const struct nfs_lock_context *l_ctx,
5351 nfs4_stateid _current_stateid;
5353 /* If the current stateid represents a lost lock, then exit */
5354 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5356 return nfs4_stateid_match(stateid, &_current_stateid);
5359 static bool nfs4_error_stateid_expired(int err)
5362 case -NFS4ERR_DELEG_REVOKED:
5363 case -NFS4ERR_ADMIN_REVOKED:
5364 case -NFS4ERR_BAD_STATEID:
5365 case -NFS4ERR_STALE_STATEID:
5366 case -NFS4ERR_OLD_STATEID:
5367 case -NFS4ERR_OPENMODE:
5368 case -NFS4ERR_EXPIRED:
5374 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5376 struct nfs_server *server = NFS_SERVER(hdr->inode);
5378 trace_nfs4_read(hdr, task->tk_status);
5379 if (task->tk_status < 0) {
5380 struct nfs4_exception exception = {
5381 .inode = hdr->inode,
5382 .state = hdr->args.context->state,
5383 .stateid = &hdr->args.stateid,
5385 task->tk_status = nfs4_async_handle_exception(task,
5386 server, task->tk_status, &exception);
5387 if (exception.retry) {
5388 rpc_restart_call_prepare(task);
5393 if (task->tk_status > 0)
5394 renew_lease(server, hdr->timestamp);
5398 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5399 struct nfs_pgio_args *args)
5402 if (!nfs4_error_stateid_expired(task->tk_status) ||
5403 nfs4_stateid_is_current(&args->stateid,
5408 rpc_restart_call_prepare(task);
5412 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5413 struct nfs_pgio_header *hdr)
5415 struct nfs_server *server = NFS_SERVER(hdr->inode);
5416 struct rpc_message *msg = &task->tk_msg;
5418 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5419 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5420 server->caps &= ~NFS_CAP_READ_PLUS;
5421 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5422 rpc_restart_call_prepare(task);
5428 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5430 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5432 if (nfs4_read_stateid_changed(task, &hdr->args))
5434 if (nfs4_read_plus_not_supported(task, hdr))
5436 if (task->tk_status > 0)
5437 nfs_invalidate_atime(hdr->inode);
5438 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5439 nfs4_read_done_cb(task, hdr);
5442 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5443 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5444 struct rpc_message *msg)
5446 /* Note: We don't use READ_PLUS with pNFS yet */
5447 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
5448 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5451 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5452 struct rpc_message *msg)
5455 #endif /* CONFIG_NFS_V4_2 */
5457 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5458 struct rpc_message *msg)
5460 hdr->timestamp = jiffies;
5461 if (!hdr->pgio_done_cb)
5462 hdr->pgio_done_cb = nfs4_read_done_cb;
5463 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5464 nfs42_read_plus_support(hdr, msg);
5465 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5468 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5469 struct nfs_pgio_header *hdr)
5471 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5472 &hdr->args.seq_args,
5476 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5477 hdr->args.lock_context,
5478 hdr->rw_mode) == -EIO)
5480 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5485 static int nfs4_write_done_cb(struct rpc_task *task,
5486 struct nfs_pgio_header *hdr)
5488 struct inode *inode = hdr->inode;
5490 trace_nfs4_write(hdr, task->tk_status);
5491 if (task->tk_status < 0) {
5492 struct nfs4_exception exception = {
5493 .inode = hdr->inode,
5494 .state = hdr->args.context->state,
5495 .stateid = &hdr->args.stateid,
5497 task->tk_status = nfs4_async_handle_exception(task,
5498 NFS_SERVER(inode), task->tk_status,
5500 if (exception.retry) {
5501 rpc_restart_call_prepare(task);
5505 if (task->tk_status >= 0) {
5506 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5507 nfs_writeback_update_inode(hdr);
5512 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5513 struct nfs_pgio_args *args)
5516 if (!nfs4_error_stateid_expired(task->tk_status) ||
5517 nfs4_stateid_is_current(&args->stateid,
5522 rpc_restart_call_prepare(task);
5526 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5528 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5530 if (nfs4_write_stateid_changed(task, &hdr->args))
5532 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5533 nfs4_write_done_cb(task, hdr);
5537 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5539 /* Don't request attributes for pNFS or O_DIRECT writes */
5540 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5542 /* Otherwise, request attributes if and only if we don't hold
5545 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5548 void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[],
5549 struct inode *inode, unsigned long cache_validity)
5551 struct nfs_server *server = NFS_SERVER(inode);
5554 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5555 cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity);
5557 if (cache_validity & NFS_INO_INVALID_CHANGE)
5558 bitmask[0] |= FATTR4_WORD0_CHANGE;
5559 if (cache_validity & NFS_INO_INVALID_ATIME)
5560 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5561 if (cache_validity & NFS_INO_INVALID_MODE)
5562 bitmask[1] |= FATTR4_WORD1_MODE;
5563 if (cache_validity & NFS_INO_INVALID_OTHER)
5564 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5565 if (cache_validity & NFS_INO_INVALID_NLINK)
5566 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5567 if (cache_validity & NFS_INO_INVALID_CTIME)
5568 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5569 if (cache_validity & NFS_INO_INVALID_MTIME)
5570 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5571 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5572 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5574 if (cache_validity & NFS_INO_INVALID_SIZE)
5575 bitmask[0] |= FATTR4_WORD0_SIZE;
5577 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5578 bitmask[i] &= server->attr_bitmask[i];
5581 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5582 struct rpc_message *msg,
5583 struct rpc_clnt **clnt)
5585 struct nfs_server *server = NFS_SERVER(hdr->inode);
5587 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5588 hdr->args.bitmask = NULL;
5589 hdr->res.fattr = NULL;
5591 nfs4_bitmask_set(hdr->args.bitmask_store,
5592 server->cache_consistency_bitmask,
5593 hdr->inode, NFS_INO_INVALID_BLOCKS);
5594 hdr->args.bitmask = hdr->args.bitmask_store;
5597 if (!hdr->pgio_done_cb)
5598 hdr->pgio_done_cb = nfs4_write_done_cb;
5599 hdr->res.server = server;
5600 hdr->timestamp = jiffies;
5602 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5603 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5604 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5607 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5609 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5610 &data->args.seq_args,
5615 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5617 struct inode *inode = data->inode;
5619 trace_nfs4_commit(data, task->tk_status);
5620 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5621 NULL, NULL) == -EAGAIN) {
5622 rpc_restart_call_prepare(task);
5628 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5630 if (!nfs4_sequence_done(task, &data->res.seq_res))
5632 return data->commit_done_cb(task, data);
5635 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5636 struct rpc_clnt **clnt)
5638 struct nfs_server *server = NFS_SERVER(data->inode);
5640 if (data->commit_done_cb == NULL)
5641 data->commit_done_cb = nfs4_commit_done_cb;
5642 data->res.server = server;
5643 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5644 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5645 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5648 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5649 struct nfs_commitres *res)
5651 struct inode *dst_inode = file_inode(dst);
5652 struct nfs_server *server = NFS_SERVER(dst_inode);
5653 struct rpc_message msg = {
5654 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5659 args->fh = NFS_FH(dst_inode);
5660 return nfs4_call_sync(server->client, server, &msg,
5661 &args->seq_args, &res->seq_res, 1);
5664 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5666 struct nfs_commitargs args = {
5670 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5671 struct nfs4_exception exception = { };
5675 status = _nfs4_proc_commit(dst, &args, res);
5676 status = nfs4_handle_exception(dst_server, status, &exception);
5677 } while (exception.retry);
5682 struct nfs4_renewdata {
5683 struct nfs_client *client;
5684 unsigned long timestamp;
5688 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5689 * standalone procedure for queueing an asynchronous RENEW.
5691 static void nfs4_renew_release(void *calldata)
5693 struct nfs4_renewdata *data = calldata;
5694 struct nfs_client *clp = data->client;
5696 if (refcount_read(&clp->cl_count) > 1)
5697 nfs4_schedule_state_renewal(clp);
5698 nfs_put_client(clp);
5702 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5704 struct nfs4_renewdata *data = calldata;
5705 struct nfs_client *clp = data->client;
5706 unsigned long timestamp = data->timestamp;
5708 trace_nfs4_renew_async(clp, task->tk_status);
5709 switch (task->tk_status) {
5712 case -NFS4ERR_LEASE_MOVED:
5713 nfs4_schedule_lease_moved_recovery(clp);
5716 /* Unless we're shutting down, schedule state recovery! */
5717 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5719 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5720 nfs4_schedule_lease_recovery(clp);
5723 nfs4_schedule_path_down_recovery(clp);
5725 do_renew_lease(clp, timestamp);
5728 static const struct rpc_call_ops nfs4_renew_ops = {
5729 .rpc_call_done = nfs4_renew_done,
5730 .rpc_release = nfs4_renew_release,
5733 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5735 struct rpc_message msg = {
5736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5740 struct nfs4_renewdata *data;
5742 if (renew_flags == 0)
5744 if (!refcount_inc_not_zero(&clp->cl_count))
5746 data = kmalloc(sizeof(*data), GFP_NOFS);
5748 nfs_put_client(clp);
5752 data->timestamp = jiffies;
5753 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5754 &nfs4_renew_ops, data);
5757 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5759 struct rpc_message msg = {
5760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5764 unsigned long now = jiffies;
5767 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5770 do_renew_lease(clp, now);
5774 static bool nfs4_server_supports_acls(const struct nfs_server *server,
5775 enum nfs4_acl_type type)
5779 return server->attr_bitmask[0] & FATTR4_WORD0_ACL;
5781 return server->attr_bitmask[1] & FATTR4_WORD1_DACL;
5783 return server->attr_bitmask[1] & FATTR4_WORD1_SACL;
5787 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5788 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5791 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5793 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5794 struct page **pages)
5796 struct page *newpage, **spages;
5802 len = min_t(size_t, PAGE_SIZE, buflen);
5803 newpage = alloc_page(GFP_KERNEL);
5805 if (newpage == NULL)
5807 memcpy(page_address(newpage), buf, len);
5812 } while (buflen != 0);
5818 __free_page(spages[rc-1]);
5822 struct nfs4_cached_acl {
5823 enum nfs4_acl_type type;
5829 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5831 struct nfs_inode *nfsi = NFS_I(inode);
5833 spin_lock(&inode->i_lock);
5834 kfree(nfsi->nfs4_acl);
5835 nfsi->nfs4_acl = acl;
5836 spin_unlock(&inode->i_lock);
5839 static void nfs4_zap_acl_attr(struct inode *inode)
5841 nfs4_set_cached_acl(inode, NULL);
5844 static ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf,
5845 size_t buflen, enum nfs4_acl_type type)
5847 struct nfs_inode *nfsi = NFS_I(inode);
5848 struct nfs4_cached_acl *acl;
5851 spin_lock(&inode->i_lock);
5852 acl = nfsi->nfs4_acl;
5855 if (acl->type != type)
5857 if (buf == NULL) /* user is just asking for length */
5859 if (acl->cached == 0)
5861 ret = -ERANGE; /* see getxattr(2) man page */
5862 if (acl->len > buflen)
5864 memcpy(buf, acl->data, acl->len);
5868 spin_unlock(&inode->i_lock);
5872 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages,
5873 size_t pgbase, size_t acl_len,
5874 enum nfs4_acl_type type)
5876 struct nfs4_cached_acl *acl;
5877 size_t buflen = sizeof(*acl) + acl_len;
5879 if (buflen <= PAGE_SIZE) {
5880 acl = kmalloc(buflen, GFP_KERNEL);
5884 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5886 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5894 nfs4_set_cached_acl(inode, acl);
5898 * The getxattr API returns the required buffer length when called with a
5899 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5900 * the required buf. On a NULL buf, we send a page of data to the server
5901 * guessing that the ACL request can be serviced by a page. If so, we cache
5902 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5903 * the cache. If not so, we throw away the page, and cache the required
5904 * length. The next getxattr call will then produce another round trip to
5905 * the server, this time with the input buf of the required size.
5907 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf,
5908 size_t buflen, enum nfs4_acl_type type)
5910 struct page **pages;
5911 struct nfs_getaclargs args = {
5912 .fh = NFS_FH(inode),
5916 struct nfs_getaclres res = {
5920 struct rpc_message msg = {
5921 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5925 unsigned int npages;
5926 int ret = -ENOMEM, i;
5927 struct nfs_server *server = NFS_SERVER(inode);
5930 buflen = server->rsize;
5932 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5933 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
5937 args.acl_pages = pages;
5939 for (i = 0; i < npages; i++) {
5940 pages[i] = alloc_page(GFP_KERNEL);
5945 /* for decoding across pages */
5946 res.acl_scratch = alloc_page(GFP_KERNEL);
5947 if (!res.acl_scratch)
5950 args.acl_len = npages * PAGE_SIZE;
5952 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5953 __func__, buf, buflen, npages, args.acl_len);
5954 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5955 &msg, &args.seq_args, &res.seq_res, 0);
5959 /* Handle the case where the passed-in buffer is too short */
5960 if (res.acl_flags & NFS4_ACL_TRUNC) {
5961 /* Did the user only issue a request for the acl length? */
5967 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len,
5970 if (res.acl_len > buflen) {
5974 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5979 for (i = 0; i < npages; i++)
5981 __free_page(pages[i]);
5982 if (res.acl_scratch)
5983 __free_page(res.acl_scratch);
5988 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf,
5989 size_t buflen, enum nfs4_acl_type type)
5991 struct nfs4_exception exception = {
5992 .interruptible = true,
5996 ret = __nfs4_get_acl_uncached(inode, buf, buflen, type);
5997 trace_nfs4_get_acl(inode, ret);
6000 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
6001 } while (exception.retry);
6005 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen,
6006 enum nfs4_acl_type type)
6008 struct nfs_server *server = NFS_SERVER(inode);
6011 if (!nfs4_server_supports_acls(server, type))
6013 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
6016 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
6017 nfs_zap_acl_cache(inode);
6018 ret = nfs4_read_cached_acl(inode, buf, buflen, type);
6020 /* -ENOENT is returned if there is no ACL or if there is an ACL
6021 * but no cached acl data, just the acl length */
6023 return nfs4_get_acl_uncached(inode, buf, buflen, type);
6026 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf,
6027 size_t buflen, enum nfs4_acl_type type)
6029 struct nfs_server *server = NFS_SERVER(inode);
6030 struct page *pages[NFS4ACL_MAXPAGES];
6031 struct nfs_setaclargs arg = {
6032 .fh = NFS_FH(inode),
6037 struct nfs_setaclres res;
6038 struct rpc_message msg = {
6039 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
6043 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
6046 /* You can't remove system.nfs4_acl: */
6049 if (!nfs4_server_supports_acls(server, type))
6051 if (npages > ARRAY_SIZE(pages))
6053 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
6056 nfs4_inode_make_writeable(inode);
6057 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6060 * Free each page after tx, so the only ref left is
6061 * held by the network stack
6064 put_page(pages[i-1]);
6067 * Acl update can result in inode attribute update.
6068 * so mark the attribute cache invalid.
6070 spin_lock(&inode->i_lock);
6071 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
6072 NFS_INO_INVALID_CTIME |
6073 NFS_INO_REVAL_FORCED);
6074 spin_unlock(&inode->i_lock);
6075 nfs_access_zap_cache(inode);
6076 nfs_zap_acl_cache(inode);
6080 static int nfs4_proc_set_acl(struct inode *inode, const void *buf,
6081 size_t buflen, enum nfs4_acl_type type)
6083 struct nfs4_exception exception = { };
6086 err = __nfs4_proc_set_acl(inode, buf, buflen, type);
6087 trace_nfs4_set_acl(inode, err);
6088 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
6090 * no need to retry since the kernel
6091 * isn't involved in encoding the ACEs.
6096 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6098 } while (exception.retry);
6102 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6103 static int _nfs4_get_security_label(struct inode *inode, void *buf,
6106 struct nfs_server *server = NFS_SERVER(inode);
6107 struct nfs4_label label = {0, 0, buflen, buf};
6109 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6110 struct nfs_fattr fattr = {
6113 struct nfs4_getattr_arg arg = {
6114 .fh = NFS_FH(inode),
6117 struct nfs4_getattr_res res = {
6121 struct rpc_message msg = {
6122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6128 nfs_fattr_init(&fattr);
6130 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6133 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6138 static int nfs4_get_security_label(struct inode *inode, void *buf,
6141 struct nfs4_exception exception = {
6142 .interruptible = true,
6146 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6150 err = _nfs4_get_security_label(inode, buf, buflen);
6151 trace_nfs4_get_security_label(inode, err);
6152 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6154 } while (exception.retry);
6158 static int _nfs4_do_set_security_label(struct inode *inode,
6159 struct nfs4_label *ilabel,
6160 struct nfs_fattr *fattr)
6163 struct iattr sattr = {0};
6164 struct nfs_server *server = NFS_SERVER(inode);
6165 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6166 struct nfs_setattrargs arg = {
6167 .fh = NFS_FH(inode),
6173 struct nfs_setattrres res = {
6177 struct rpc_message msg = {
6178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6184 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6186 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6188 dprintk("%s failed: %d\n", __func__, status);
6193 static int nfs4_do_set_security_label(struct inode *inode,
6194 struct nfs4_label *ilabel,
6195 struct nfs_fattr *fattr)
6197 struct nfs4_exception exception = { };
6201 err = _nfs4_do_set_security_label(inode, ilabel, fattr);
6202 trace_nfs4_set_security_label(inode, err);
6203 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6205 } while (exception.retry);
6210 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6212 struct nfs4_label ilabel = {0, 0, buflen, (char *)buf };
6213 struct nfs_fattr *fattr;
6216 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6219 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
6223 status = nfs4_do_set_security_label(inode, &ilabel, fattr);
6225 nfs_setsecurity(inode, fattr);
6229 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6232 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6233 nfs4_verifier *bootverf)
6237 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6238 /* An impossible timestamp guarantees this value
6239 * will never match a generated boot time. */
6240 verf[0] = cpu_to_be32(U32_MAX);
6241 verf[1] = cpu_to_be32(U32_MAX);
6243 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6244 u64 ns = ktime_to_ns(nn->boot_time);
6246 verf[0] = cpu_to_be32(ns >> 32);
6247 verf[1] = cpu_to_be32(ns);
6249 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6253 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6255 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6256 struct nfs_netns_client *nn_clp = nn->nfs_client;
6263 id = rcu_dereference(nn_clp->identifier);
6265 strscpy(buf, id, buflen);
6269 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6270 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6276 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6278 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6283 if (clp->cl_owner_id != NULL)
6288 strlen(clp->cl_rpcclient->cl_nodename) +
6290 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6294 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6298 if (len > NFS4_OPAQUE_LIMIT + 1)
6302 * Since this string is allocated at mount time, and held until the
6303 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6304 * about a memory-reclaim deadlock.
6306 str = kmalloc(len, GFP_KERNEL);
6312 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6313 clp->cl_rpcclient->cl_nodename, buf,
6314 rpc_peeraddr2str(clp->cl_rpcclient,
6317 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6318 clp->cl_rpcclient->cl_nodename,
6319 rpc_peeraddr2str(clp->cl_rpcclient,
6323 clp->cl_owner_id = str;
6328 nfs4_init_uniform_client_string(struct nfs_client *clp)
6330 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6335 if (clp->cl_owner_id != NULL)
6338 len = 10 + 10 + 1 + 10 + 1 +
6339 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6341 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6345 if (len > NFS4_OPAQUE_LIMIT + 1)
6349 * Since this string is allocated at mount time, and held until the
6350 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6351 * about a memory-reclaim deadlock.
6353 str = kmalloc(len, GFP_KERNEL);
6358 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6359 clp->rpc_ops->version, clp->cl_minorversion,
6360 buf, clp->cl_rpcclient->cl_nodename);
6362 scnprintf(str, len, "Linux NFSv%u.%u %s",
6363 clp->rpc_ops->version, clp->cl_minorversion,
6364 clp->cl_rpcclient->cl_nodename);
6365 clp->cl_owner_id = str;
6370 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6371 * services. Advertise one based on the address family of the
6375 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6377 if (strchr(clp->cl_ipaddr, ':') != NULL)
6378 return scnprintf(buf, len, "tcp6");
6380 return scnprintf(buf, len, "tcp");
6383 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6385 struct nfs4_setclientid *sc = calldata;
6387 if (task->tk_status == 0)
6388 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6391 static const struct rpc_call_ops nfs4_setclientid_ops = {
6392 .rpc_call_done = nfs4_setclientid_done,
6396 * nfs4_proc_setclientid - Negotiate client ID
6397 * @clp: state data structure
6398 * @program: RPC program for NFSv4 callback service
6399 * @port: IP port number for NFS4 callback service
6400 * @cred: credential to use for this call
6401 * @res: where to place the result
6403 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6405 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6406 unsigned short port, const struct cred *cred,
6407 struct nfs4_setclientid_res *res)
6409 nfs4_verifier sc_verifier;
6410 struct nfs4_setclientid setclientid = {
6411 .sc_verifier = &sc_verifier,
6415 struct rpc_message msg = {
6416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6417 .rpc_argp = &setclientid,
6421 struct rpc_task_setup task_setup_data = {
6422 .rpc_client = clp->cl_rpcclient,
6423 .rpc_message = &msg,
6424 .callback_ops = &nfs4_setclientid_ops,
6425 .callback_data = &setclientid,
6426 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6428 unsigned long now = jiffies;
6431 /* nfs_client_id4 */
6432 nfs4_init_boot_verifier(clp, &sc_verifier);
6434 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6435 status = nfs4_init_uniform_client_string(clp);
6437 status = nfs4_init_nonuniform_client_string(clp);
6443 setclientid.sc_netid_len =
6444 nfs4_init_callback_netid(clp,
6445 setclientid.sc_netid,
6446 sizeof(setclientid.sc_netid));
6447 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6448 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6449 clp->cl_ipaddr, port >> 8, port & 255);
6451 dprintk("NFS call setclientid auth=%s, '%s'\n",
6452 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6455 status = nfs4_call_sync_custom(&task_setup_data);
6456 if (setclientid.sc_cred) {
6457 kfree(clp->cl_acceptor);
6458 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6459 put_rpccred(setclientid.sc_cred);
6463 do_renew_lease(clp, now);
6465 trace_nfs4_setclientid(clp, status);
6466 dprintk("NFS reply setclientid: %d\n", status);
6471 * nfs4_proc_setclientid_confirm - Confirm client ID
6472 * @clp: state data structure
6473 * @arg: result of a previous SETCLIENTID
6474 * @cred: credential to use for this call
6476 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6478 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6479 struct nfs4_setclientid_res *arg,
6480 const struct cred *cred)
6482 struct rpc_message msg = {
6483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6489 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6490 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6492 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6493 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6494 trace_nfs4_setclientid_confirm(clp, status);
6495 dprintk("NFS reply setclientid_confirm: %d\n", status);
6499 struct nfs4_delegreturndata {
6500 struct nfs4_delegreturnargs args;
6501 struct nfs4_delegreturnres res;
6503 nfs4_stateid stateid;
6504 unsigned long timestamp;
6506 struct nfs4_layoutreturn_args arg;
6507 struct nfs4_layoutreturn_res res;
6508 struct nfs4_xdr_opaque_data ld_private;
6512 struct nfs_fattr fattr;
6514 struct inode *inode;
6517 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6519 struct nfs4_delegreturndata *data = calldata;
6520 struct nfs4_exception exception = {
6521 .inode = data->inode,
6522 .stateid = &data->stateid,
6523 .task_is_privileged = data->args.seq_args.sa_privileged,
6526 if (!nfs4_sequence_done(task, &data->res.seq_res))
6529 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6531 /* Handle Layoutreturn errors */
6532 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6533 &data->res.lr_ret) == -EAGAIN)
6536 switch (task->tk_status) {
6538 renew_lease(data->res.server, data->timestamp);
6540 case -NFS4ERR_ADMIN_REVOKED:
6541 case -NFS4ERR_DELEG_REVOKED:
6542 case -NFS4ERR_EXPIRED:
6543 nfs4_free_revoked_stateid(data->res.server,
6545 task->tk_msg.rpc_cred);
6547 case -NFS4ERR_BAD_STATEID:
6548 case -NFS4ERR_STALE_STATEID:
6550 task->tk_status = 0;
6552 case -NFS4ERR_OLD_STATEID:
6553 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6554 nfs4_stateid_seqid_inc(&data->stateid);
6555 if (data->args.bitmask) {
6556 data->args.bitmask = NULL;
6557 data->res.fattr = NULL;
6560 case -NFS4ERR_ACCESS:
6561 if (data->args.bitmask) {
6562 data->args.bitmask = NULL;
6563 data->res.fattr = NULL;
6568 task->tk_status = nfs4_async_handle_exception(task,
6569 data->res.server, task->tk_status,
6571 if (exception.retry)
6574 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6575 data->rpc_status = task->tk_status;
6578 task->tk_status = 0;
6579 rpc_restart_call_prepare(task);
6582 static void nfs4_delegreturn_release(void *calldata)
6584 struct nfs4_delegreturndata *data = calldata;
6585 struct inode *inode = data->inode;
6588 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6591 nfs4_fattr_set_prechange(&data->fattr,
6592 inode_peek_iversion_raw(inode));
6593 nfs_refresh_inode(inode, &data->fattr);
6594 nfs_iput_and_deactive(inode);
6599 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6601 struct nfs4_delegreturndata *d_data;
6602 struct pnfs_layout_hdr *lo;
6604 d_data = (struct nfs4_delegreturndata *)data;
6606 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6607 nfs4_sequence_done(task, &d_data->res.seq_res);
6611 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6612 if (lo && !pnfs_layout_is_valid(lo)) {
6613 d_data->args.lr_args = NULL;
6614 d_data->res.lr_res = NULL;
6617 nfs4_setup_sequence(d_data->res.server->nfs_client,
6618 &d_data->args.seq_args,
6619 &d_data->res.seq_res,
6623 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6624 .rpc_call_prepare = nfs4_delegreturn_prepare,
6625 .rpc_call_done = nfs4_delegreturn_done,
6626 .rpc_release = nfs4_delegreturn_release,
6629 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6631 struct nfs4_delegreturndata *data;
6632 struct nfs_server *server = NFS_SERVER(inode);
6633 struct rpc_task *task;
6634 struct rpc_message msg = {
6635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6638 struct rpc_task_setup task_setup_data = {
6639 .rpc_client = server->client,
6640 .rpc_message = &msg,
6641 .callback_ops = &nfs4_delegreturn_ops,
6642 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6646 if (nfs_server_capable(inode, NFS_CAP_MOVEABLE))
6647 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6649 data = kzalloc(sizeof(*data), GFP_KERNEL);
6653 nfs4_state_protect(server->nfs_client,
6654 NFS_SP4_MACH_CRED_CLEANUP,
6655 &task_setup_data.rpc_client, &msg);
6657 data->args.fhandle = &data->fh;
6658 data->args.stateid = &data->stateid;
6659 nfs4_bitmask_set(data->args.bitmask_store,
6660 server->cache_consistency_bitmask, inode, 0);
6661 data->args.bitmask = data->args.bitmask_store;
6662 nfs_copy_fh(&data->fh, NFS_FH(inode));
6663 nfs4_stateid_copy(&data->stateid, stateid);
6664 data->res.fattr = &data->fattr;
6665 data->res.server = server;
6666 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6667 data->lr.arg.ld_private = &data->lr.ld_private;
6668 nfs_fattr_init(data->res.fattr);
6669 data->timestamp = jiffies;
6670 data->rpc_status = 0;
6671 data->inode = nfs_igrab_and_active(inode);
6672 if (data->inode || issync) {
6673 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6676 data->args.lr_args = &data->lr.arg;
6677 data->res.lr_res = &data->lr.res;
6682 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6685 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6687 task_setup_data.callback_data = data;
6688 msg.rpc_argp = &data->args;
6689 msg.rpc_resp = &data->res;
6690 task = rpc_run_task(&task_setup_data);
6692 return PTR_ERR(task);
6695 status = rpc_wait_for_completion_task(task);
6698 status = data->rpc_status;
6704 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6706 struct nfs_server *server = NFS_SERVER(inode);
6707 struct nfs4_exception exception = { };
6710 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6711 trace_nfs4_delegreturn(inode, stateid, err);
6713 case -NFS4ERR_STALE_STATEID:
6714 case -NFS4ERR_EXPIRED:
6718 err = nfs4_handle_exception(server, err, &exception);
6719 } while (exception.retry);
6723 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6725 struct inode *inode = state->inode;
6726 struct nfs_server *server = NFS_SERVER(inode);
6727 struct nfs_client *clp = server->nfs_client;
6728 struct nfs_lockt_args arg = {
6729 .fh = NFS_FH(inode),
6732 struct nfs_lockt_res res = {
6735 struct rpc_message msg = {
6736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6739 .rpc_cred = state->owner->so_cred,
6741 struct nfs4_lock_state *lsp;
6744 arg.lock_owner.clientid = clp->cl_clientid;
6745 status = nfs4_set_lock_state(state, request);
6748 lsp = request->fl_u.nfs4_fl.owner;
6749 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6750 arg.lock_owner.s_dev = server->s_dev;
6751 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6754 request->fl_type = F_UNLCK;
6756 case -NFS4ERR_DENIED:
6759 request->fl_ops->fl_release_private(request);
6760 request->fl_ops = NULL;
6765 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6767 struct nfs4_exception exception = {
6768 .interruptible = true,
6773 err = _nfs4_proc_getlk(state, cmd, request);
6774 trace_nfs4_get_lock(request, state, cmd, err);
6775 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6777 } while (exception.retry);
6782 * Update the seqid of a lock stateid after receiving
6783 * NFS4ERR_OLD_STATEID
6785 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6786 struct nfs4_lock_state *lsp)
6788 struct nfs4_state *state = lsp->ls_state;
6791 spin_lock(&state->state_lock);
6792 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6794 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6795 nfs4_stateid_seqid_inc(dst);
6797 dst->seqid = lsp->ls_stateid.seqid;
6800 spin_unlock(&state->state_lock);
6804 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6805 struct nfs4_lock_state *lsp)
6807 struct nfs4_state *state = lsp->ls_state;
6810 spin_lock(&state->state_lock);
6811 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6812 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6813 spin_unlock(&state->state_lock);
6817 struct nfs4_unlockdata {
6818 struct nfs_locku_args arg;
6819 struct nfs_locku_res res;
6820 struct nfs4_lock_state *lsp;
6821 struct nfs_open_context *ctx;
6822 struct nfs_lock_context *l_ctx;
6823 struct file_lock fl;
6824 struct nfs_server *server;
6825 unsigned long timestamp;
6828 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6829 struct nfs_open_context *ctx,
6830 struct nfs4_lock_state *lsp,
6831 struct nfs_seqid *seqid)
6833 struct nfs4_unlockdata *p;
6834 struct nfs4_state *state = lsp->ls_state;
6835 struct inode *inode = state->inode;
6837 p = kzalloc(sizeof(*p), GFP_KERNEL);
6840 p->arg.fh = NFS_FH(inode);
6842 p->arg.seqid = seqid;
6843 p->res.seqid = seqid;
6845 /* Ensure we don't close file until we're done freeing locks! */
6846 p->ctx = get_nfs_open_context(ctx);
6847 p->l_ctx = nfs_get_lock_context(ctx);
6848 locks_init_lock(&p->fl);
6849 locks_copy_lock(&p->fl, fl);
6850 p->server = NFS_SERVER(inode);
6851 spin_lock(&state->state_lock);
6852 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6853 spin_unlock(&state->state_lock);
6857 static void nfs4_locku_release_calldata(void *data)
6859 struct nfs4_unlockdata *calldata = data;
6860 nfs_free_seqid(calldata->arg.seqid);
6861 nfs4_put_lock_state(calldata->lsp);
6862 nfs_put_lock_context(calldata->l_ctx);
6863 put_nfs_open_context(calldata->ctx);
6867 static void nfs4_locku_done(struct rpc_task *task, void *data)
6869 struct nfs4_unlockdata *calldata = data;
6870 struct nfs4_exception exception = {
6871 .inode = calldata->lsp->ls_state->inode,
6872 .stateid = &calldata->arg.stateid,
6875 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6877 switch (task->tk_status) {
6879 renew_lease(calldata->server, calldata->timestamp);
6880 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6881 if (nfs4_update_lock_stateid(calldata->lsp,
6882 &calldata->res.stateid))
6885 case -NFS4ERR_ADMIN_REVOKED:
6886 case -NFS4ERR_EXPIRED:
6887 nfs4_free_revoked_stateid(calldata->server,
6888 &calldata->arg.stateid,
6889 task->tk_msg.rpc_cred);
6891 case -NFS4ERR_BAD_STATEID:
6892 case -NFS4ERR_STALE_STATEID:
6893 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6895 rpc_restart_call_prepare(task);
6897 case -NFS4ERR_OLD_STATEID:
6898 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6900 rpc_restart_call_prepare(task);
6903 task->tk_status = nfs4_async_handle_exception(task,
6904 calldata->server, task->tk_status,
6906 if (exception.retry)
6907 rpc_restart_call_prepare(task);
6909 nfs_release_seqid(calldata->arg.seqid);
6912 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6914 struct nfs4_unlockdata *calldata = data;
6916 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6917 nfs_async_iocounter_wait(task, calldata->l_ctx))
6920 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6922 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6923 /* Note: exit _without_ running nfs4_locku_done */
6926 calldata->timestamp = jiffies;
6927 if (nfs4_setup_sequence(calldata->server->nfs_client,
6928 &calldata->arg.seq_args,
6929 &calldata->res.seq_res,
6931 nfs_release_seqid(calldata->arg.seqid);
6934 task->tk_action = NULL;
6936 nfs4_sequence_done(task, &calldata->res.seq_res);
6939 static const struct rpc_call_ops nfs4_locku_ops = {
6940 .rpc_call_prepare = nfs4_locku_prepare,
6941 .rpc_call_done = nfs4_locku_done,
6942 .rpc_release = nfs4_locku_release_calldata,
6945 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6946 struct nfs_open_context *ctx,
6947 struct nfs4_lock_state *lsp,
6948 struct nfs_seqid *seqid)
6950 struct nfs4_unlockdata *data;
6951 struct rpc_message msg = {
6952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6953 .rpc_cred = ctx->cred,
6955 struct rpc_task_setup task_setup_data = {
6956 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6957 .rpc_message = &msg,
6958 .callback_ops = &nfs4_locku_ops,
6959 .workqueue = nfsiod_workqueue,
6960 .flags = RPC_TASK_ASYNC,
6963 if (nfs_server_capable(lsp->ls_state->inode, NFS_CAP_MOVEABLE))
6964 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6966 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6967 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6969 /* Ensure this is an unlock - when canceling a lock, the
6970 * canceled lock is passed in, and it won't be an unlock.
6972 fl->fl_type = F_UNLCK;
6973 if (fl->fl_flags & FL_CLOSE)
6974 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6976 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6978 nfs_free_seqid(seqid);
6979 return ERR_PTR(-ENOMEM);
6982 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6983 msg.rpc_argp = &data->arg;
6984 msg.rpc_resp = &data->res;
6985 task_setup_data.callback_data = data;
6986 return rpc_run_task(&task_setup_data);
6989 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6991 struct inode *inode = state->inode;
6992 struct nfs4_state_owner *sp = state->owner;
6993 struct nfs_inode *nfsi = NFS_I(inode);
6994 struct nfs_seqid *seqid;
6995 struct nfs4_lock_state *lsp;
6996 struct rpc_task *task;
6997 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6999 unsigned char fl_flags = request->fl_flags;
7001 status = nfs4_set_lock_state(state, request);
7002 /* Unlock _before_ we do the RPC call */
7003 request->fl_flags |= FL_EXISTS;
7004 /* Exclude nfs_delegation_claim_locks() */
7005 mutex_lock(&sp->so_delegreturn_mutex);
7006 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
7007 down_read(&nfsi->rwsem);
7008 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
7009 up_read(&nfsi->rwsem);
7010 mutex_unlock(&sp->so_delegreturn_mutex);
7013 up_read(&nfsi->rwsem);
7014 mutex_unlock(&sp->so_delegreturn_mutex);
7017 /* Is this a delegated lock? */
7018 lsp = request->fl_u.nfs4_fl.owner;
7019 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
7021 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
7022 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
7026 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
7027 status = PTR_ERR(task);
7030 status = rpc_wait_for_completion_task(task);
7033 request->fl_flags = fl_flags;
7034 trace_nfs4_unlock(request, state, F_SETLK, status);
7038 struct nfs4_lockdata {
7039 struct nfs_lock_args arg;
7040 struct nfs_lock_res res;
7041 struct nfs4_lock_state *lsp;
7042 struct nfs_open_context *ctx;
7043 struct file_lock fl;
7044 unsigned long timestamp;
7047 struct nfs_server *server;
7050 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
7051 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
7054 struct nfs4_lockdata *p;
7055 struct inode *inode = lsp->ls_state->inode;
7056 struct nfs_server *server = NFS_SERVER(inode);
7057 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7059 p = kzalloc(sizeof(*p), gfp_mask);
7063 p->arg.fh = NFS_FH(inode);
7065 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
7066 if (IS_ERR(p->arg.open_seqid))
7068 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
7069 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
7070 if (IS_ERR(p->arg.lock_seqid))
7071 goto out_free_seqid;
7072 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
7073 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
7074 p->arg.lock_owner.s_dev = server->s_dev;
7075 p->res.lock_seqid = p->arg.lock_seqid;
7078 p->ctx = get_nfs_open_context(ctx);
7079 locks_init_lock(&p->fl);
7080 locks_copy_lock(&p->fl, fl);
7083 nfs_free_seqid(p->arg.open_seqid);
7089 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7091 struct nfs4_lockdata *data = calldata;
7092 struct nfs4_state *state = data->lsp->ls_state;
7094 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
7096 /* Do we need to do an open_to_lock_owner? */
7097 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
7098 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
7099 goto out_release_lock_seqid;
7101 nfs4_stateid_copy(&data->arg.open_stateid,
7102 &state->open_stateid);
7103 data->arg.new_lock_owner = 1;
7104 data->res.open_seqid = data->arg.open_seqid;
7106 data->arg.new_lock_owner = 0;
7107 nfs4_stateid_copy(&data->arg.lock_stateid,
7108 &data->lsp->ls_stateid);
7110 if (!nfs4_valid_open_stateid(state)) {
7111 data->rpc_status = -EBADF;
7112 task->tk_action = NULL;
7113 goto out_release_open_seqid;
7115 data->timestamp = jiffies;
7116 if (nfs4_setup_sequence(data->server->nfs_client,
7117 &data->arg.seq_args,
7121 out_release_open_seqid:
7122 nfs_release_seqid(data->arg.open_seqid);
7123 out_release_lock_seqid:
7124 nfs_release_seqid(data->arg.lock_seqid);
7126 nfs4_sequence_done(task, &data->res.seq_res);
7127 dprintk("%s: ret = %d\n", __func__, data->rpc_status);
7130 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7132 struct nfs4_lockdata *data = calldata;
7133 struct nfs4_lock_state *lsp = data->lsp;
7135 if (!nfs4_sequence_done(task, &data->res.seq_res))
7138 data->rpc_status = task->tk_status;
7139 switch (task->tk_status) {
7141 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
7143 if (data->arg.new_lock && !data->cancelled) {
7144 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
7145 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7148 if (data->arg.new_lock_owner != 0) {
7149 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7150 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7151 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7152 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7155 case -NFS4ERR_BAD_STATEID:
7156 case -NFS4ERR_OLD_STATEID:
7157 case -NFS4ERR_STALE_STATEID:
7158 case -NFS4ERR_EXPIRED:
7159 if (data->arg.new_lock_owner != 0) {
7160 if (!nfs4_stateid_match(&data->arg.open_stateid,
7161 &lsp->ls_state->open_stateid))
7163 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7168 dprintk("%s: ret = %d!\n", __func__, data->rpc_status);
7171 if (!data->cancelled)
7172 rpc_restart_call_prepare(task);
7176 static void nfs4_lock_release(void *calldata)
7178 struct nfs4_lockdata *data = calldata;
7180 nfs_free_seqid(data->arg.open_seqid);
7181 if (data->cancelled && data->rpc_status == 0) {
7182 struct rpc_task *task;
7183 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7184 data->arg.lock_seqid);
7186 rpc_put_task_async(task);
7187 dprintk("%s: cancelling lock!\n", __func__);
7189 nfs_free_seqid(data->arg.lock_seqid);
7190 nfs4_put_lock_state(data->lsp);
7191 put_nfs_open_context(data->ctx);
7195 static const struct rpc_call_ops nfs4_lock_ops = {
7196 .rpc_call_prepare = nfs4_lock_prepare,
7197 .rpc_call_done = nfs4_lock_done,
7198 .rpc_release = nfs4_lock_release,
7201 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7204 case -NFS4ERR_ADMIN_REVOKED:
7205 case -NFS4ERR_EXPIRED:
7206 case -NFS4ERR_BAD_STATEID:
7207 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7208 if (new_lock_owner != 0 ||
7209 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7210 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7212 case -NFS4ERR_STALE_STATEID:
7213 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7214 nfs4_schedule_lease_recovery(server->nfs_client);
7218 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7220 struct nfs4_lockdata *data;
7221 struct rpc_task *task;
7222 struct rpc_message msg = {
7223 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7224 .rpc_cred = state->owner->so_cred,
7226 struct rpc_task_setup task_setup_data = {
7227 .rpc_client = NFS_CLIENT(state->inode),
7228 .rpc_message = &msg,
7229 .callback_ops = &nfs4_lock_ops,
7230 .workqueue = nfsiod_workqueue,
7231 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7235 if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
7236 task_setup_data.flags |= RPC_TASK_MOVEABLE;
7238 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7239 fl->fl_u.nfs4_fl.owner, GFP_KERNEL);
7243 data->arg.block = 1;
7244 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7245 recovery_type > NFS_LOCK_NEW);
7246 msg.rpc_argp = &data->arg;
7247 msg.rpc_resp = &data->res;
7248 task_setup_data.callback_data = data;
7249 if (recovery_type > NFS_LOCK_NEW) {
7250 if (recovery_type == NFS_LOCK_RECLAIM)
7251 data->arg.reclaim = NFS_LOCK_RECLAIM;
7253 data->arg.new_lock = 1;
7254 task = rpc_run_task(&task_setup_data);
7256 return PTR_ERR(task);
7257 ret = rpc_wait_for_completion_task(task);
7259 ret = data->rpc_status;
7261 nfs4_handle_setlk_error(data->server, data->lsp,
7262 data->arg.new_lock_owner, ret);
7264 data->cancelled = true;
7265 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7267 dprintk("%s: ret = %d\n", __func__, ret);
7271 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7273 struct nfs_server *server = NFS_SERVER(state->inode);
7274 struct nfs4_exception exception = {
7275 .inode = state->inode,
7280 /* Cache the lock if possible... */
7281 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7283 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7284 if (err != -NFS4ERR_DELAY)
7286 nfs4_handle_exception(server, err, &exception);
7287 } while (exception.retry);
7291 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7293 struct nfs_server *server = NFS_SERVER(state->inode);
7294 struct nfs4_exception exception = {
7295 .inode = state->inode,
7299 err = nfs4_set_lock_state(state, request);
7302 if (!recover_lost_locks) {
7303 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7307 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7309 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7313 case -NFS4ERR_GRACE:
7314 case -NFS4ERR_DELAY:
7315 nfs4_handle_exception(server, err, &exception);
7318 } while (exception.retry);
7323 #if defined(CONFIG_NFS_V4_1)
7324 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7326 struct nfs4_lock_state *lsp;
7329 status = nfs4_set_lock_state(state, request);
7332 lsp = request->fl_u.nfs4_fl.owner;
7333 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7334 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7336 return nfs4_lock_expired(state, request);
7340 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7342 struct nfs_inode *nfsi = NFS_I(state->inode);
7343 struct nfs4_state_owner *sp = state->owner;
7344 unsigned char fl_flags = request->fl_flags;
7347 request->fl_flags |= FL_ACCESS;
7348 status = locks_lock_inode_wait(state->inode, request);
7351 mutex_lock(&sp->so_delegreturn_mutex);
7352 down_read(&nfsi->rwsem);
7353 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7354 /* Yes: cache locks! */
7355 /* ...but avoid races with delegation recall... */
7356 request->fl_flags = fl_flags & ~FL_SLEEP;
7357 status = locks_lock_inode_wait(state->inode, request);
7358 up_read(&nfsi->rwsem);
7359 mutex_unlock(&sp->so_delegreturn_mutex);
7362 up_read(&nfsi->rwsem);
7363 mutex_unlock(&sp->so_delegreturn_mutex);
7364 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7366 request->fl_flags = fl_flags;
7370 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7372 struct nfs4_exception exception = {
7374 .inode = state->inode,
7375 .interruptible = true,
7380 err = _nfs4_proc_setlk(state, cmd, request);
7381 if (err == -NFS4ERR_DENIED)
7383 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7385 } while (exception.retry);
7389 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7390 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7393 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7394 struct file_lock *request)
7396 int status = -ERESTARTSYS;
7397 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7399 while(!signalled()) {
7400 status = nfs4_proc_setlk(state, cmd, request);
7401 if ((status != -EAGAIN) || IS_SETLK(cmd))
7403 freezable_schedule_timeout_interruptible(timeout);
7405 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7406 status = -ERESTARTSYS;
7411 #ifdef CONFIG_NFS_V4_1
7412 struct nfs4_lock_waiter {
7413 struct inode *inode;
7414 struct nfs_lowner owner;
7415 wait_queue_entry_t wait;
7419 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7421 struct nfs4_lock_waiter *waiter =
7422 container_of(wait, struct nfs4_lock_waiter, wait);
7424 /* NULL key means to wake up everyone */
7426 struct cb_notify_lock_args *cbnl = key;
7427 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7428 *wowner = &waiter->owner;
7430 /* Only wake if the callback was for the same owner. */
7431 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7434 /* Make sure it's for the right inode */
7435 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7439 return woken_wake_function(wait, mode, flags, key);
7443 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7445 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7446 struct nfs_server *server = NFS_SERVER(state->inode);
7447 struct nfs_client *clp = server->nfs_client;
7448 wait_queue_head_t *q = &clp->cl_lock_waitq;
7449 struct nfs4_lock_waiter waiter = {
7450 .inode = state->inode,
7451 .owner = { .clientid = clp->cl_clientid,
7452 .id = lsp->ls_seqid.owner_id,
7453 .s_dev = server->s_dev },
7457 /* Don't bother with waitqueue if we don't expect a callback */
7458 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7459 return nfs4_retry_setlk_simple(state, cmd, request);
7461 init_wait(&waiter.wait);
7462 waiter.wait.func = nfs4_wake_lock_waiter;
7463 add_wait_queue(q, &waiter.wait);
7466 status = nfs4_proc_setlk(state, cmd, request);
7467 if (status != -EAGAIN || IS_SETLK(cmd))
7470 status = -ERESTARTSYS;
7471 freezer_do_not_count();
7472 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE,
7473 NFS4_LOCK_MAXTIMEOUT);
7475 } while (!signalled());
7477 remove_wait_queue(q, &waiter.wait);
7481 #else /* !CONFIG_NFS_V4_1 */
7483 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7485 return nfs4_retry_setlk_simple(state, cmd, request);
7490 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7492 struct nfs_open_context *ctx;
7493 struct nfs4_state *state;
7496 /* verify open state */
7497 ctx = nfs_file_open_context(filp);
7500 if (IS_GETLK(cmd)) {
7502 return nfs4_proc_getlk(state, F_GETLK, request);
7506 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7509 if (request->fl_type == F_UNLCK) {
7511 return nfs4_proc_unlck(state, cmd, request);
7518 if ((request->fl_flags & FL_POSIX) &&
7519 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7523 * Don't rely on the VFS having checked the file open mode,
7524 * since it won't do this for flock() locks.
7526 switch (request->fl_type) {
7528 if (!(filp->f_mode & FMODE_READ))
7532 if (!(filp->f_mode & FMODE_WRITE))
7536 status = nfs4_set_lock_state(state, request);
7540 return nfs4_retry_setlk(state, cmd, request);
7543 static int nfs4_delete_lease(struct file *file, void **priv)
7545 return generic_setlease(file, F_UNLCK, NULL, priv);
7548 static int nfs4_add_lease(struct file *file, long arg, struct file_lock **lease,
7551 struct inode *inode = file_inode(file);
7552 fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7555 /* No delegation, no lease */
7556 if (!nfs4_have_delegation(inode, type))
7558 ret = generic_setlease(file, arg, lease, priv);
7559 if (ret || nfs4_have_delegation(inode, type))
7561 /* We raced with a delegation return */
7562 nfs4_delete_lease(file, priv);
7566 int nfs4_proc_setlease(struct file *file, long arg, struct file_lock **lease,
7572 return nfs4_add_lease(file, arg, lease, priv);
7574 return nfs4_delete_lease(file, priv);
7580 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7582 struct nfs_server *server = NFS_SERVER(state->inode);
7585 err = nfs4_set_lock_state(state, fl);
7589 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7590 if (err != -NFS4ERR_DELAY)
7593 } while (err == -NFS4ERR_DELAY);
7594 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7597 struct nfs_release_lockowner_data {
7598 struct nfs4_lock_state *lsp;
7599 struct nfs_server *server;
7600 struct nfs_release_lockowner_args args;
7601 struct nfs_release_lockowner_res res;
7602 unsigned long timestamp;
7605 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7607 struct nfs_release_lockowner_data *data = calldata;
7608 struct nfs_server *server = data->server;
7609 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7610 &data->res.seq_res, task);
7611 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7612 data->timestamp = jiffies;
7615 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7617 struct nfs_release_lockowner_data *data = calldata;
7618 struct nfs_server *server = data->server;
7620 nfs40_sequence_done(task, &data->res.seq_res);
7622 switch (task->tk_status) {
7624 renew_lease(server, data->timestamp);
7626 case -NFS4ERR_STALE_CLIENTID:
7627 case -NFS4ERR_EXPIRED:
7628 nfs4_schedule_lease_recovery(server->nfs_client);
7630 case -NFS4ERR_LEASE_MOVED:
7631 case -NFS4ERR_DELAY:
7632 if (nfs4_async_handle_error(task, server,
7633 NULL, NULL) == -EAGAIN)
7634 rpc_restart_call_prepare(task);
7638 static void nfs4_release_lockowner_release(void *calldata)
7640 struct nfs_release_lockowner_data *data = calldata;
7641 nfs4_free_lock_state(data->server, data->lsp);
7645 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7646 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7647 .rpc_call_done = nfs4_release_lockowner_done,
7648 .rpc_release = nfs4_release_lockowner_release,
7652 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7654 struct nfs_release_lockowner_data *data;
7655 struct rpc_message msg = {
7656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7659 if (server->nfs_client->cl_mvops->minor_version != 0)
7662 data = kmalloc(sizeof(*data), GFP_KERNEL);
7666 data->server = server;
7667 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7668 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7669 data->args.lock_owner.s_dev = server->s_dev;
7671 msg.rpc_argp = &data->args;
7672 msg.rpc_resp = &data->res;
7673 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7674 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7677 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7679 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7680 struct user_namespace *mnt_userns,
7681 struct dentry *unused, struct inode *inode,
7682 const char *key, const void *buf,
7683 size_t buflen, int flags)
7685 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_ACL);
7688 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7689 struct dentry *unused, struct inode *inode,
7690 const char *key, void *buf, size_t buflen)
7692 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_ACL);
7695 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7697 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_ACL);
7700 #if defined(CONFIG_NFS_V4_1)
7701 #define XATTR_NAME_NFSV4_DACL "system.nfs4_dacl"
7703 static int nfs4_xattr_set_nfs4_dacl(const struct xattr_handler *handler,
7704 struct user_namespace *mnt_userns,
7705 struct dentry *unused, struct inode *inode,
7706 const char *key, const void *buf,
7707 size_t buflen, int flags)
7709 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_DACL);
7712 static int nfs4_xattr_get_nfs4_dacl(const struct xattr_handler *handler,
7713 struct dentry *unused, struct inode *inode,
7714 const char *key, void *buf, size_t buflen)
7716 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_DACL);
7719 static bool nfs4_xattr_list_nfs4_dacl(struct dentry *dentry)
7721 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_DACL);
7724 #define XATTR_NAME_NFSV4_SACL "system.nfs4_sacl"
7726 static int nfs4_xattr_set_nfs4_sacl(const struct xattr_handler *handler,
7727 struct user_namespace *mnt_userns,
7728 struct dentry *unused, struct inode *inode,
7729 const char *key, const void *buf,
7730 size_t buflen, int flags)
7732 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_SACL);
7735 static int nfs4_xattr_get_nfs4_sacl(const struct xattr_handler *handler,
7736 struct dentry *unused, struct inode *inode,
7737 const char *key, void *buf, size_t buflen)
7739 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_SACL);
7742 static bool nfs4_xattr_list_nfs4_sacl(struct dentry *dentry)
7744 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_SACL);
7749 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7751 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7752 struct user_namespace *mnt_userns,
7753 struct dentry *unused, struct inode *inode,
7754 const char *key, const void *buf,
7755 size_t buflen, int flags)
7757 if (security_ismaclabel(key))
7758 return nfs4_set_security_label(inode, buf, buflen);
7763 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7764 struct dentry *unused, struct inode *inode,
7765 const char *key, void *buf, size_t buflen)
7767 if (security_ismaclabel(key))
7768 return nfs4_get_security_label(inode, buf, buflen);
7773 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7777 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7778 len = security_inode_listsecurity(inode, list, list_len);
7779 if (len >= 0 && list_len && len > list_len)
7785 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7786 .prefix = XATTR_SECURITY_PREFIX,
7787 .get = nfs4_xattr_get_nfs4_label,
7788 .set = nfs4_xattr_set_nfs4_label,
7794 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7801 #ifdef CONFIG_NFS_V4_2
7802 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7803 struct user_namespace *mnt_userns,
7804 struct dentry *unused, struct inode *inode,
7805 const char *key, const void *buf,
7806 size_t buflen, int flags)
7811 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7815 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7816 * flags right now. Handling of xattr operations use the normal
7817 * file read/write permissions.
7819 * Just in case the server has other ideas (which RFC 8276 allows),
7820 * do a cached access check for the XA* flags to possibly avoid
7821 * doing an RPC and getting EACCES back.
7823 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7824 if (!(mask & NFS_ACCESS_XAWRITE))
7829 ret = nfs42_proc_removexattr(inode, key);
7831 nfs4_xattr_cache_remove(inode, key);
7833 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7835 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7841 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7842 struct dentry *unused, struct inode *inode,
7843 const char *key, void *buf, size_t buflen)
7848 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7851 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7852 if (!(mask & NFS_ACCESS_XAREAD))
7856 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7860 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7861 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7864 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7870 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7879 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7882 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7883 if (!(mask & NFS_ACCESS_XALIST))
7887 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7891 ret = nfs4_xattr_cache_list(inode, list, list_len);
7892 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7897 buflen = list_len ? list_len : XATTR_LIST_MAX;
7898 buf = list_len ? list : NULL;
7902 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7915 nfs4_xattr_cache_set_list(inode, list, size);
7923 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7927 #endif /* CONFIG_NFS_V4_2 */
7930 * nfs_fhget will use either the mounted_on_fileid or the fileid
7932 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7934 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7935 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7936 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7937 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7940 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7941 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7942 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7946 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7947 const struct qstr *name,
7948 struct nfs4_fs_locations *fs_locations,
7951 struct nfs_server *server = NFS_SERVER(dir);
7953 struct nfs4_fs_locations_arg args = {
7954 .dir_fh = NFS_FH(dir),
7959 struct nfs4_fs_locations_res res = {
7960 .fs_locations = fs_locations,
7962 struct rpc_message msg = {
7963 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7969 dprintk("%s: start\n", __func__);
7971 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7972 bitmask[1] = nfs4_fattr_bitmap[1];
7974 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7975 * is not supported */
7976 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7977 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7979 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7981 nfs_fattr_init(fs_locations->fattr);
7982 fs_locations->server = server;
7983 fs_locations->nlocations = 0;
7984 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7985 dprintk("%s: returned status = %d\n", __func__, status);
7989 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7990 const struct qstr *name,
7991 struct nfs4_fs_locations *fs_locations,
7994 struct nfs4_exception exception = {
7995 .interruptible = true,
7999 err = _nfs4_proc_fs_locations(client, dir, name,
8000 fs_locations, page);
8001 trace_nfs4_get_fs_locations(dir, name, err);
8002 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8004 } while (exception.retry);
8009 * This operation also signals the server that this client is
8010 * performing migration recovery. The server can stop returning
8011 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
8012 * appended to this compound to identify the client ID which is
8013 * performing recovery.
8015 static int _nfs40_proc_get_locations(struct nfs_server *server,
8016 struct nfs_fh *fhandle,
8017 struct nfs4_fs_locations *locations,
8018 struct page *page, const struct cred *cred)
8020 struct rpc_clnt *clnt = server->client;
8022 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8024 struct nfs4_fs_locations_arg args = {
8025 .clientid = server->nfs_client->cl_clientid,
8029 .migration = 1, /* skip LOOKUP */
8030 .renew = 1, /* append RENEW */
8032 struct nfs4_fs_locations_res res = {
8033 .fs_locations = locations,
8037 struct rpc_message msg = {
8038 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8043 unsigned long now = jiffies;
8046 nfs_fattr_init(locations->fattr);
8047 locations->server = server;
8048 locations->nlocations = 0;
8050 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8051 status = nfs4_call_sync_sequence(clnt, server, &msg,
8052 &args.seq_args, &res.seq_res);
8056 renew_lease(server, now);
8060 #ifdef CONFIG_NFS_V4_1
8063 * This operation also signals the server that this client is
8064 * performing migration recovery. The server can stop asserting
8065 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
8066 * performing this operation is identified in the SEQUENCE
8067 * operation in this compound.
8069 * When the client supports GETATTR(fs_locations_info), it can
8070 * be plumbed in here.
8072 static int _nfs41_proc_get_locations(struct nfs_server *server,
8073 struct nfs_fh *fhandle,
8074 struct nfs4_fs_locations *locations,
8075 struct page *page, const struct cred *cred)
8077 struct rpc_clnt *clnt = server->client;
8079 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8081 struct nfs4_fs_locations_arg args = {
8085 .migration = 1, /* skip LOOKUP */
8087 struct nfs4_fs_locations_res res = {
8088 .fs_locations = locations,
8091 struct rpc_message msg = {
8092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8097 struct nfs4_call_sync_data data = {
8098 .seq_server = server,
8099 .seq_args = &args.seq_args,
8100 .seq_res = &res.seq_res,
8102 struct rpc_task_setup task_setup_data = {
8104 .rpc_message = &msg,
8105 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
8106 .callback_data = &data,
8107 .flags = RPC_TASK_NO_ROUND_ROBIN,
8111 nfs_fattr_init(locations->fattr);
8112 locations->server = server;
8113 locations->nlocations = 0;
8115 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8116 status = nfs4_call_sync_custom(&task_setup_data);
8117 if (status == NFS4_OK &&
8118 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8119 status = -NFS4ERR_LEASE_MOVED;
8123 #endif /* CONFIG_NFS_V4_1 */
8126 * nfs4_proc_get_locations - discover locations for a migrated FSID
8127 * @server: pointer to nfs_server to process
8128 * @fhandle: pointer to the kernel NFS client file handle
8129 * @locations: result of query
8131 * @cred: credential to use for this operation
8133 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
8134 * operation failed, or a negative errno if a local error occurred.
8136 * On success, "locations" is filled in, but if the server has
8137 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
8140 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
8141 * from this client that require migration recovery.
8143 int nfs4_proc_get_locations(struct nfs_server *server,
8144 struct nfs_fh *fhandle,
8145 struct nfs4_fs_locations *locations,
8146 struct page *page, const struct cred *cred)
8148 struct nfs_client *clp = server->nfs_client;
8149 const struct nfs4_mig_recovery_ops *ops =
8150 clp->cl_mvops->mig_recovery_ops;
8151 struct nfs4_exception exception = {
8152 .interruptible = true,
8156 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8157 (unsigned long long)server->fsid.major,
8158 (unsigned long long)server->fsid.minor,
8160 nfs_display_fhandle(fhandle, __func__);
8163 status = ops->get_locations(server, fhandle, locations, page,
8165 if (status != -NFS4ERR_DELAY)
8167 nfs4_handle_exception(server, status, &exception);
8168 } while (exception.retry);
8173 * This operation also signals the server that this client is
8174 * performing "lease moved" recovery. The server can stop
8175 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
8176 * is appended to this compound to identify the client ID which is
8177 * performing recovery.
8179 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
8181 struct nfs_server *server = NFS_SERVER(inode);
8182 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8183 struct rpc_clnt *clnt = server->client;
8184 struct nfs4_fsid_present_arg args = {
8185 .fh = NFS_FH(inode),
8186 .clientid = clp->cl_clientid,
8187 .renew = 1, /* append RENEW */
8189 struct nfs4_fsid_present_res res = {
8192 struct rpc_message msg = {
8193 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8198 unsigned long now = jiffies;
8201 res.fh = nfs_alloc_fhandle();
8205 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8206 status = nfs4_call_sync_sequence(clnt, server, &msg,
8207 &args.seq_args, &res.seq_res);
8208 nfs_free_fhandle(res.fh);
8212 do_renew_lease(clp, now);
8216 #ifdef CONFIG_NFS_V4_1
8219 * This operation also signals the server that this client is
8220 * performing "lease moved" recovery. The server can stop asserting
8221 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8222 * this operation is identified in the SEQUENCE operation in this
8225 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8227 struct nfs_server *server = NFS_SERVER(inode);
8228 struct rpc_clnt *clnt = server->client;
8229 struct nfs4_fsid_present_arg args = {
8230 .fh = NFS_FH(inode),
8232 struct nfs4_fsid_present_res res = {
8234 struct rpc_message msg = {
8235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8242 res.fh = nfs_alloc_fhandle();
8246 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8247 status = nfs4_call_sync_sequence(clnt, server, &msg,
8248 &args.seq_args, &res.seq_res);
8249 nfs_free_fhandle(res.fh);
8250 if (status == NFS4_OK &&
8251 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8252 status = -NFS4ERR_LEASE_MOVED;
8256 #endif /* CONFIG_NFS_V4_1 */
8259 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8260 * @inode: inode on FSID to check
8261 * @cred: credential to use for this operation
8263 * Server indicates whether the FSID is present, moved, or not
8264 * recognized. This operation is necessary to clear a LEASE_MOVED
8265 * condition for this client ID.
8267 * Returns NFS4_OK if the FSID is present on this server,
8268 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8269 * NFS4ERR code if some error occurred on the server, or a
8270 * negative errno if a local failure occurred.
8272 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8274 struct nfs_server *server = NFS_SERVER(inode);
8275 struct nfs_client *clp = server->nfs_client;
8276 const struct nfs4_mig_recovery_ops *ops =
8277 clp->cl_mvops->mig_recovery_ops;
8278 struct nfs4_exception exception = {
8279 .interruptible = true,
8283 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8284 (unsigned long long)server->fsid.major,
8285 (unsigned long long)server->fsid.minor,
8287 nfs_display_fhandle(NFS_FH(inode), __func__);
8290 status = ops->fsid_present(inode, cred);
8291 if (status != -NFS4ERR_DELAY)
8293 nfs4_handle_exception(server, status, &exception);
8294 } while (exception.retry);
8299 * If 'use_integrity' is true and the state managment nfs_client
8300 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8301 * and the machine credential as per RFC3530bis and RFC5661 Security
8302 * Considerations sections. Otherwise, just use the user cred with the
8303 * filesystem's rpc_client.
8305 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8308 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8309 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8310 struct nfs4_secinfo_arg args = {
8311 .dir_fh = NFS_FH(dir),
8314 struct nfs4_secinfo_res res = {
8317 struct rpc_message msg = {
8318 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8322 struct nfs4_call_sync_data data = {
8323 .seq_server = NFS_SERVER(dir),
8324 .seq_args = &args.seq_args,
8325 .seq_res = &res.seq_res,
8327 struct rpc_task_setup task_setup = {
8329 .rpc_message = &msg,
8330 .callback_ops = clp->cl_mvops->call_sync_ops,
8331 .callback_data = &data,
8332 .flags = RPC_TASK_NO_ROUND_ROBIN,
8334 const struct cred *cred = NULL;
8336 if (use_integrity) {
8337 clnt = clp->cl_rpcclient;
8338 task_setup.rpc_client = clnt;
8340 cred = nfs4_get_clid_cred(clp);
8341 msg.rpc_cred = cred;
8344 dprintk("NFS call secinfo %s\n", name->name);
8346 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8347 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8348 status = nfs4_call_sync_custom(&task_setup);
8350 dprintk("NFS reply secinfo: %d\n", status);
8356 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8357 struct nfs4_secinfo_flavors *flavors)
8359 struct nfs4_exception exception = {
8360 .interruptible = true,
8364 err = -NFS4ERR_WRONGSEC;
8366 /* try to use integrity protection with machine cred */
8367 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8368 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8371 * if unable to use integrity protection, or SECINFO with
8372 * integrity protection returns NFS4ERR_WRONGSEC (which is
8373 * disallowed by spec, but exists in deployed servers) use
8374 * the current filesystem's rpc_client and the user cred.
8376 if (err == -NFS4ERR_WRONGSEC)
8377 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8379 trace_nfs4_secinfo(dir, name, err);
8380 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8382 } while (exception.retry);
8386 #ifdef CONFIG_NFS_V4_1
8388 * Check the exchange flags returned by the server for invalid flags, having
8389 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8392 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8394 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8396 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8398 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8399 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8401 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8405 return -NFS4ERR_INVAL;
8409 nfs41_same_server_scope(struct nfs41_server_scope *a,
8410 struct nfs41_server_scope *b)
8412 if (a->server_scope_sz != b->server_scope_sz)
8414 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8418 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8420 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8421 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8422 struct nfs_client *clp = args->client;
8424 switch (task->tk_status) {
8425 case -NFS4ERR_BADSESSION:
8426 case -NFS4ERR_DEADSESSION:
8427 nfs4_schedule_session_recovery(clp->cl_session,
8431 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8432 res->dir != NFS4_CDFS4_BOTH) {
8433 rpc_task_close_connection(task);
8434 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8435 rpc_restart_call(task);
8439 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8440 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8444 * nfs4_proc_bind_one_conn_to_session()
8446 * The 4.1 client currently uses the same TCP connection for the
8447 * fore and backchannel.
8450 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8451 struct rpc_xprt *xprt,
8452 struct nfs_client *clp,
8453 const struct cred *cred)
8456 struct nfs41_bind_conn_to_session_args args = {
8458 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8461 struct nfs41_bind_conn_to_session_res res;
8462 struct rpc_message msg = {
8464 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8469 struct rpc_task_setup task_setup_data = {
8472 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8473 .rpc_message = &msg,
8474 .flags = RPC_TASK_TIMEOUT,
8476 struct rpc_task *task;
8478 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8479 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8480 args.dir = NFS4_CDFC4_FORE;
8482 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8483 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8484 args.dir = NFS4_CDFC4_FORE;
8486 task = rpc_run_task(&task_setup_data);
8487 if (!IS_ERR(task)) {
8488 status = task->tk_status;
8491 status = PTR_ERR(task);
8492 trace_nfs4_bind_conn_to_session(clp, status);
8494 if (memcmp(res.sessionid.data,
8495 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8496 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8499 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8500 dprintk("NFS: %s: Unexpected direction from server\n",
8504 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8505 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8514 struct rpc_bind_conn_calldata {
8515 struct nfs_client *clp;
8516 const struct cred *cred;
8520 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8521 struct rpc_xprt *xprt,
8524 struct rpc_bind_conn_calldata *p = calldata;
8526 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8529 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8531 struct rpc_bind_conn_calldata data = {
8535 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8536 nfs4_proc_bind_conn_to_session_callback, &data);
8540 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8541 * and operations we'd like to see to enable certain features in the allow map
8543 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8544 .how = SP4_MACH_CRED,
8545 .enforce.u.words = {
8546 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8547 1 << (OP_EXCHANGE_ID - 32) |
8548 1 << (OP_CREATE_SESSION - 32) |
8549 1 << (OP_DESTROY_SESSION - 32) |
8550 1 << (OP_DESTROY_CLIENTID - 32)
8553 [0] = 1 << (OP_CLOSE) |
8554 1 << (OP_OPEN_DOWNGRADE) |
8556 1 << (OP_DELEGRETURN) |
8558 [1] = 1 << (OP_SECINFO - 32) |
8559 1 << (OP_SECINFO_NO_NAME - 32) |
8560 1 << (OP_LAYOUTRETURN - 32) |
8561 1 << (OP_TEST_STATEID - 32) |
8562 1 << (OP_FREE_STATEID - 32) |
8563 1 << (OP_WRITE - 32)
8568 * Select the state protection mode for client `clp' given the server results
8569 * from exchange_id in `sp'.
8571 * Returns 0 on success, negative errno otherwise.
8573 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8574 struct nfs41_state_protection *sp)
8576 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8577 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8578 1 << (OP_EXCHANGE_ID - 32) |
8579 1 << (OP_CREATE_SESSION - 32) |
8580 1 << (OP_DESTROY_SESSION - 32) |
8581 1 << (OP_DESTROY_CLIENTID - 32)
8583 unsigned long flags = 0;
8587 if (sp->how == SP4_MACH_CRED) {
8588 /* Print state protect result */
8589 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8590 for (i = 0; i <= LAST_NFS4_OP; i++) {
8591 if (test_bit(i, sp->enforce.u.longs))
8592 dfprintk(MOUNT, " enforce op %d\n", i);
8593 if (test_bit(i, sp->allow.u.longs))
8594 dfprintk(MOUNT, " allow op %d\n", i);
8597 /* make sure nothing is on enforce list that isn't supported */
8598 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8599 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8600 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8607 * Minimal mode - state operations are allowed to use machine
8608 * credential. Note this already happens by default, so the
8609 * client doesn't have to do anything more than the negotiation.
8611 * NOTE: we don't care if EXCHANGE_ID is in the list -
8612 * we're already using the machine cred for exchange_id
8613 * and will never use a different cred.
8615 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8616 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8617 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8618 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8619 dfprintk(MOUNT, "sp4_mach_cred:\n");
8620 dfprintk(MOUNT, " minimal mode enabled\n");
8621 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8623 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8628 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8629 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8630 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8631 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8632 dfprintk(MOUNT, " cleanup mode enabled\n");
8633 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8636 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8637 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8638 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8641 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8642 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8643 dfprintk(MOUNT, " secinfo mode enabled\n");
8644 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8647 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8648 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8649 dfprintk(MOUNT, " stateid mode enabled\n");
8650 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8653 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8654 dfprintk(MOUNT, " write mode enabled\n");
8655 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8658 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8659 dfprintk(MOUNT, " commit mode enabled\n");
8660 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8664 clp->cl_sp4_flags = flags;
8668 struct nfs41_exchange_id_data {
8669 struct nfs41_exchange_id_res res;
8670 struct nfs41_exchange_id_args args;
8673 static void nfs4_exchange_id_release(void *data)
8675 struct nfs41_exchange_id_data *cdata =
8676 (struct nfs41_exchange_id_data *)data;
8678 nfs_put_client(cdata->args.client);
8679 kfree(cdata->res.impl_id);
8680 kfree(cdata->res.server_scope);
8681 kfree(cdata->res.server_owner);
8685 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8686 .rpc_release = nfs4_exchange_id_release,
8690 * _nfs4_proc_exchange_id()
8692 * Wrapper for EXCHANGE_ID operation.
8694 static struct rpc_task *
8695 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8696 u32 sp4_how, struct rpc_xprt *xprt)
8698 struct rpc_message msg = {
8699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8702 struct rpc_task_setup task_setup_data = {
8703 .rpc_client = clp->cl_rpcclient,
8704 .callback_ops = &nfs4_exchange_id_call_ops,
8705 .rpc_message = &msg,
8706 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8708 struct nfs41_exchange_id_data *calldata;
8711 if (!refcount_inc_not_zero(&clp->cl_count))
8712 return ERR_PTR(-EIO);
8715 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8719 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8721 status = nfs4_init_uniform_client_string(clp);
8725 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8728 if (unlikely(calldata->res.server_owner == NULL))
8731 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8733 if (unlikely(calldata->res.server_scope == NULL))
8734 goto out_server_owner;
8736 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8737 if (unlikely(calldata->res.impl_id == NULL))
8738 goto out_server_scope;
8742 calldata->args.state_protect.how = SP4_NONE;
8746 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8756 task_setup_data.rpc_xprt = xprt;
8757 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8758 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8759 sizeof(calldata->args.verifier.data));
8761 calldata->args.client = clp;
8762 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8763 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8764 #ifdef CONFIG_NFS_V4_1_MIGRATION
8765 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8767 msg.rpc_argp = &calldata->args;
8768 msg.rpc_resp = &calldata->res;
8769 task_setup_data.callback_data = calldata;
8771 return rpc_run_task(&task_setup_data);
8774 kfree(calldata->res.impl_id);
8776 kfree(calldata->res.server_scope);
8778 kfree(calldata->res.server_owner);
8782 nfs_put_client(clp);
8783 return ERR_PTR(status);
8787 * _nfs4_proc_exchange_id()
8789 * Wrapper for EXCHANGE_ID operation.
8791 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8794 struct rpc_task *task;
8795 struct nfs41_exchange_id_args *argp;
8796 struct nfs41_exchange_id_res *resp;
8797 unsigned long now = jiffies;
8800 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8802 return PTR_ERR(task);
8804 argp = task->tk_msg.rpc_argp;
8805 resp = task->tk_msg.rpc_resp;
8806 status = task->tk_status;
8810 status = nfs4_check_cl_exchange_flags(resp->flags,
8811 clp->cl_mvops->minor_version);
8815 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8819 do_renew_lease(clp, now);
8821 clp->cl_clientid = resp->clientid;
8822 clp->cl_exchange_flags = resp->flags;
8823 clp->cl_seqid = resp->seqid;
8824 /* Client ID is not confirmed */
8825 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8826 clear_bit(NFS4_SESSION_ESTABLISHED,
8827 &clp->cl_session->session_state);
8829 if (clp->cl_serverscope != NULL &&
8830 !nfs41_same_server_scope(clp->cl_serverscope,
8831 resp->server_scope)) {
8832 dprintk("%s: server_scope mismatch detected\n",
8834 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8837 swap(clp->cl_serverowner, resp->server_owner);
8838 swap(clp->cl_serverscope, resp->server_scope);
8839 swap(clp->cl_implid, resp->impl_id);
8841 /* Save the EXCHANGE_ID verifier session trunk tests */
8842 memcpy(clp->cl_confirm.data, argp->verifier.data,
8843 sizeof(clp->cl_confirm.data));
8845 trace_nfs4_exchange_id(clp, status);
8851 * nfs4_proc_exchange_id()
8853 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8855 * Since the clientid has expired, all compounds using sessions
8856 * associated with the stale clientid will be returning
8857 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8858 * be in some phase of session reset.
8860 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8862 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8864 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8867 /* try SP4_MACH_CRED if krb5i/p */
8868 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8869 authflavor == RPC_AUTH_GSS_KRB5P) {
8870 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8876 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8880 * nfs4_test_session_trunk
8882 * This is an add_xprt_test() test function called from
8883 * rpc_clnt_setup_test_and_add_xprt.
8885 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8886 * and is dereferrenced in nfs4_exchange_id_release
8888 * Upon success, add the new transport to the rpc_clnt
8890 * @clnt: struct rpc_clnt to get new transport
8891 * @xprt: the rpc_xprt to test
8892 * @data: call data for _nfs4_proc_exchange_id.
8894 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8897 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8898 struct rpc_task *task;
8903 dprintk("--> %s try %s\n", __func__,
8904 xprt->address_strings[RPC_DISPLAY_ADDR]);
8906 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8908 /* Test connection for session trunking. Async exchange_id call */
8909 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8913 status = task->tk_status;
8915 status = nfs4_detect_session_trunking(adata->clp,
8916 task->tk_msg.rpc_resp, xprt);
8919 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8923 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8925 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8926 const struct cred *cred)
8928 struct rpc_message msg = {
8929 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8935 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8936 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8937 trace_nfs4_destroy_clientid(clp, status);
8939 dprintk("NFS: Got error %d from the server %s on "
8940 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8944 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8945 const struct cred *cred)
8950 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8951 ret = _nfs4_proc_destroy_clientid(clp, cred);
8953 case -NFS4ERR_DELAY:
8954 case -NFS4ERR_CLIENTID_BUSY:
8964 int nfs4_destroy_clientid(struct nfs_client *clp)
8966 const struct cred *cred;
8969 if (clp->cl_mvops->minor_version < 1)
8971 if (clp->cl_exchange_flags == 0)
8973 if (clp->cl_preserve_clid)
8975 cred = nfs4_get_clid_cred(clp);
8976 ret = nfs4_proc_destroy_clientid(clp, cred);
8980 case -NFS4ERR_STALE_CLIENTID:
8981 clp->cl_exchange_flags = 0;
8987 #endif /* CONFIG_NFS_V4_1 */
8989 struct nfs4_get_lease_time_data {
8990 struct nfs4_get_lease_time_args *args;
8991 struct nfs4_get_lease_time_res *res;
8992 struct nfs_client *clp;
8995 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8998 struct nfs4_get_lease_time_data *data =
8999 (struct nfs4_get_lease_time_data *)calldata;
9001 /* just setup sequence, do not trigger session recovery
9002 since we're invoked within one */
9003 nfs4_setup_sequence(data->clp,
9004 &data->args->la_seq_args,
9005 &data->res->lr_seq_res,
9010 * Called from nfs4_state_manager thread for session setup, so don't recover
9011 * from sequence operation or clientid errors.
9013 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
9015 struct nfs4_get_lease_time_data *data =
9016 (struct nfs4_get_lease_time_data *)calldata;
9018 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
9020 switch (task->tk_status) {
9021 case -NFS4ERR_DELAY:
9022 case -NFS4ERR_GRACE:
9023 rpc_delay(task, NFS4_POLL_RETRY_MIN);
9024 task->tk_status = 0;
9026 case -NFS4ERR_RETRY_UNCACHED_REP:
9027 rpc_restart_call_prepare(task);
9032 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
9033 .rpc_call_prepare = nfs4_get_lease_time_prepare,
9034 .rpc_call_done = nfs4_get_lease_time_done,
9037 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
9039 struct nfs4_get_lease_time_args args;
9040 struct nfs4_get_lease_time_res res = {
9041 .lr_fsinfo = fsinfo,
9043 struct nfs4_get_lease_time_data data = {
9048 struct rpc_message msg = {
9049 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
9053 struct rpc_task_setup task_setup = {
9054 .rpc_client = clp->cl_rpcclient,
9055 .rpc_message = &msg,
9056 .callback_ops = &nfs4_get_lease_time_ops,
9057 .callback_data = &data,
9058 .flags = RPC_TASK_TIMEOUT,
9061 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
9062 return nfs4_call_sync_custom(&task_setup);
9065 #ifdef CONFIG_NFS_V4_1
9068 * Initialize the values to be used by the client in CREATE_SESSION
9069 * If nfs4_init_session set the fore channel request and response sizes,
9072 * Set the back channel max_resp_sz_cached to zero to force the client to
9073 * always set csa_cachethis to FALSE because the current implementation
9074 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
9076 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
9077 struct rpc_clnt *clnt)
9079 unsigned int max_rqst_sz, max_resp_sz;
9080 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
9081 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
9083 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
9084 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
9086 /* Fore channel attributes */
9087 args->fc_attrs.max_rqst_sz = max_rqst_sz;
9088 args->fc_attrs.max_resp_sz = max_resp_sz;
9089 args->fc_attrs.max_ops = NFS4_MAX_OPS;
9090 args->fc_attrs.max_reqs = max_session_slots;
9092 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
9093 "max_ops=%u max_reqs=%u\n",
9095 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
9096 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
9098 /* Back channel attributes */
9099 args->bc_attrs.max_rqst_sz = max_bc_payload;
9100 args->bc_attrs.max_resp_sz = max_bc_payload;
9101 args->bc_attrs.max_resp_sz_cached = 0;
9102 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
9103 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
9104 if (args->bc_attrs.max_reqs > max_bc_slots)
9105 args->bc_attrs.max_reqs = max_bc_slots;
9107 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
9108 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
9110 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
9111 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
9112 args->bc_attrs.max_reqs);
9115 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
9116 struct nfs41_create_session_res *res)
9118 struct nfs4_channel_attrs *sent = &args->fc_attrs;
9119 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
9121 if (rcvd->max_resp_sz > sent->max_resp_sz)
9124 * Our requested max_ops is the minimum we need; we're not
9125 * prepared to break up compounds into smaller pieces than that.
9126 * So, no point even trying to continue if the server won't
9129 if (rcvd->max_ops < sent->max_ops)
9131 if (rcvd->max_reqs == 0)
9133 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
9134 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
9138 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
9139 struct nfs41_create_session_res *res)
9141 struct nfs4_channel_attrs *sent = &args->bc_attrs;
9142 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
9144 if (!(res->flags & SESSION4_BACK_CHAN))
9146 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
9148 if (rcvd->max_resp_sz < sent->max_resp_sz)
9150 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
9152 if (rcvd->max_ops > sent->max_ops)
9154 if (rcvd->max_reqs > sent->max_reqs)
9160 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
9161 struct nfs41_create_session_res *res)
9165 ret = nfs4_verify_fore_channel_attrs(args, res);
9168 return nfs4_verify_back_channel_attrs(args, res);
9171 static void nfs4_update_session(struct nfs4_session *session,
9172 struct nfs41_create_session_res *res)
9174 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
9175 /* Mark client id and session as being confirmed */
9176 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
9177 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
9178 session->flags = res->flags;
9179 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
9180 if (res->flags & SESSION4_BACK_CHAN)
9181 memcpy(&session->bc_attrs, &res->bc_attrs,
9182 sizeof(session->bc_attrs));
9185 static int _nfs4_proc_create_session(struct nfs_client *clp,
9186 const struct cred *cred)
9188 struct nfs4_session *session = clp->cl_session;
9189 struct nfs41_create_session_args args = {
9191 .clientid = clp->cl_clientid,
9192 .seqid = clp->cl_seqid,
9193 .cb_program = NFS4_CALLBACK,
9195 struct nfs41_create_session_res res;
9197 struct rpc_message msg = {
9198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9205 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
9206 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
9208 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9209 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9210 trace_nfs4_create_session(clp, status);
9213 case -NFS4ERR_STALE_CLIENTID:
9214 case -NFS4ERR_DELAY:
9223 /* Verify the session's negotiated channel_attrs values */
9224 status = nfs4_verify_channel_attrs(&args, &res);
9225 /* Increment the clientid slot sequence id */
9228 nfs4_update_session(session, &res);
9235 * Issues a CREATE_SESSION operation to the server.
9236 * It is the responsibility of the caller to verify the session is
9237 * expired before calling this routine.
9239 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9243 struct nfs4_session *session = clp->cl_session;
9245 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9247 status = _nfs4_proc_create_session(clp, cred);
9251 /* Init or reset the session slot tables */
9252 status = nfs4_setup_session_slot_tables(session);
9253 dprintk("slot table setup returned %d\n", status);
9257 ptr = (unsigned *)&session->sess_id.data[0];
9258 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9259 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9265 * Issue the over-the-wire RPC DESTROY_SESSION.
9266 * The caller must serialize access to this routine.
9268 int nfs4_proc_destroy_session(struct nfs4_session *session,
9269 const struct cred *cred)
9271 struct rpc_message msg = {
9272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9273 .rpc_argp = session,
9278 /* session is still being setup */
9279 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9282 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9283 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9284 trace_nfs4_destroy_session(session->clp, status);
9287 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9288 "Session has been destroyed regardless...\n", status);
9293 * Renew the cl_session lease.
9295 struct nfs4_sequence_data {
9296 struct nfs_client *clp;
9297 struct nfs4_sequence_args args;
9298 struct nfs4_sequence_res res;
9301 static void nfs41_sequence_release(void *data)
9303 struct nfs4_sequence_data *calldata = data;
9304 struct nfs_client *clp = calldata->clp;
9306 if (refcount_read(&clp->cl_count) > 1)
9307 nfs4_schedule_state_renewal(clp);
9308 nfs_put_client(clp);
9312 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9314 switch(task->tk_status) {
9315 case -NFS4ERR_DELAY:
9316 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9319 nfs4_schedule_lease_recovery(clp);
9324 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9326 struct nfs4_sequence_data *calldata = data;
9327 struct nfs_client *clp = calldata->clp;
9329 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9332 trace_nfs4_sequence(clp, task->tk_status);
9333 if (task->tk_status < 0) {
9334 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9335 if (refcount_read(&clp->cl_count) == 1)
9338 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9339 rpc_restart_call_prepare(task);
9343 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9346 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9348 struct nfs4_sequence_data *calldata = data;
9349 struct nfs_client *clp = calldata->clp;
9350 struct nfs4_sequence_args *args;
9351 struct nfs4_sequence_res *res;
9353 args = task->tk_msg.rpc_argp;
9354 res = task->tk_msg.rpc_resp;
9356 nfs4_setup_sequence(clp, args, res, task);
9359 static const struct rpc_call_ops nfs41_sequence_ops = {
9360 .rpc_call_done = nfs41_sequence_call_done,
9361 .rpc_call_prepare = nfs41_sequence_prepare,
9362 .rpc_release = nfs41_sequence_release,
9365 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9366 const struct cred *cred,
9367 struct nfs4_slot *slot,
9370 struct nfs4_sequence_data *calldata;
9371 struct rpc_message msg = {
9372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9375 struct rpc_task_setup task_setup_data = {
9376 .rpc_client = clp->cl_rpcclient,
9377 .rpc_message = &msg,
9378 .callback_ops = &nfs41_sequence_ops,
9379 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
9381 struct rpc_task *ret;
9383 ret = ERR_PTR(-EIO);
9384 if (!refcount_inc_not_zero(&clp->cl_count))
9387 ret = ERR_PTR(-ENOMEM);
9388 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
9389 if (calldata == NULL)
9391 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9392 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9393 msg.rpc_argp = &calldata->args;
9394 msg.rpc_resp = &calldata->res;
9395 calldata->clp = clp;
9396 task_setup_data.callback_data = calldata;
9398 ret = rpc_run_task(&task_setup_data);
9403 nfs_put_client(clp);
9405 nfs41_release_slot(slot);
9409 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9411 struct rpc_task *task;
9414 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9416 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9418 ret = PTR_ERR(task);
9420 rpc_put_task_async(task);
9421 dprintk("<-- %s status=%d\n", __func__, ret);
9425 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9427 struct rpc_task *task;
9430 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9432 ret = PTR_ERR(task);
9435 ret = rpc_wait_for_completion_task(task);
9437 ret = task->tk_status;
9440 dprintk("<-- %s status=%d\n", __func__, ret);
9444 struct nfs4_reclaim_complete_data {
9445 struct nfs_client *clp;
9446 struct nfs41_reclaim_complete_args arg;
9447 struct nfs41_reclaim_complete_res res;
9450 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9452 struct nfs4_reclaim_complete_data *calldata = data;
9454 nfs4_setup_sequence(calldata->clp,
9455 &calldata->arg.seq_args,
9456 &calldata->res.seq_res,
9460 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9462 switch(task->tk_status) {
9464 wake_up_all(&clp->cl_lock_waitq);
9466 case -NFS4ERR_COMPLETE_ALREADY:
9467 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9469 case -NFS4ERR_DELAY:
9470 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9472 case -NFS4ERR_RETRY_UNCACHED_REP:
9474 case -NFS4ERR_BADSESSION:
9475 case -NFS4ERR_DEADSESSION:
9476 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9479 nfs4_schedule_lease_recovery(clp);
9484 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9486 struct nfs4_reclaim_complete_data *calldata = data;
9487 struct nfs_client *clp = calldata->clp;
9488 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9490 if (!nfs41_sequence_done(task, res))
9493 trace_nfs4_reclaim_complete(clp, task->tk_status);
9494 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9495 rpc_restart_call_prepare(task);
9500 static void nfs4_free_reclaim_complete_data(void *data)
9502 struct nfs4_reclaim_complete_data *calldata = data;
9507 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9508 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9509 .rpc_call_done = nfs4_reclaim_complete_done,
9510 .rpc_release = nfs4_free_reclaim_complete_data,
9514 * Issue a global reclaim complete.
9516 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9517 const struct cred *cred)
9519 struct nfs4_reclaim_complete_data *calldata;
9520 struct rpc_message msg = {
9521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9524 struct rpc_task_setup task_setup_data = {
9525 .rpc_client = clp->cl_rpcclient,
9526 .rpc_message = &msg,
9527 .callback_ops = &nfs4_reclaim_complete_call_ops,
9528 .flags = RPC_TASK_NO_ROUND_ROBIN,
9530 int status = -ENOMEM;
9532 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9533 if (calldata == NULL)
9535 calldata->clp = clp;
9536 calldata->arg.one_fs = 0;
9538 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9539 msg.rpc_argp = &calldata->arg;
9540 msg.rpc_resp = &calldata->res;
9541 task_setup_data.callback_data = calldata;
9542 status = nfs4_call_sync_custom(&task_setup_data);
9544 dprintk("<-- %s status=%d\n", __func__, status);
9549 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9551 struct nfs4_layoutget *lgp = calldata;
9552 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9554 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9555 &lgp->res.seq_res, task);
9558 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9560 struct nfs4_layoutget *lgp = calldata;
9562 nfs41_sequence_process(task, &lgp->res.seq_res);
9566 nfs4_layoutget_handle_exception(struct rpc_task *task,
9567 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9569 struct inode *inode = lgp->args.inode;
9570 struct nfs_server *server = NFS_SERVER(inode);
9571 struct pnfs_layout_hdr *lo = lgp->lo;
9572 int nfs4err = task->tk_status;
9573 int err, status = 0;
9576 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9578 nfs4_sequence_free_slot(&lgp->res.seq_res);
9585 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9586 * on the file. set tk_status to -ENODATA to tell upper layer to
9589 case -NFS4ERR_LAYOUTUNAVAILABLE:
9593 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9594 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9596 case -NFS4ERR_BADLAYOUT:
9597 status = -EOVERFLOW;
9600 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9601 * (or clients) writing to the same RAID stripe except when
9602 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9604 * Treat it like we would RECALLCONFLICT -- we retry for a little
9605 * while, and then eventually give up.
9607 case -NFS4ERR_LAYOUTTRYLATER:
9608 if (lgp->args.minlength == 0) {
9609 status = -EOVERFLOW;
9614 case -NFS4ERR_RECALLCONFLICT:
9615 status = -ERECALLCONFLICT;
9617 case -NFS4ERR_DELEG_REVOKED:
9618 case -NFS4ERR_ADMIN_REVOKED:
9619 case -NFS4ERR_EXPIRED:
9620 case -NFS4ERR_BAD_STATEID:
9621 exception->timeout = 0;
9622 spin_lock(&inode->i_lock);
9623 /* If the open stateid was bad, then recover it. */
9624 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9625 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9626 spin_unlock(&inode->i_lock);
9627 exception->state = lgp->args.ctx->state;
9628 exception->stateid = &lgp->args.stateid;
9633 * Mark the bad layout state as invalid, then retry
9635 pnfs_mark_layout_stateid_invalid(lo, &head);
9636 spin_unlock(&inode->i_lock);
9637 nfs_commit_inode(inode, 0);
9638 pnfs_free_lseg_list(&head);
9643 err = nfs4_handle_exception(server, nfs4err, exception);
9645 if (exception->retry)
9654 size_t max_response_pages(struct nfs_server *server)
9656 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9657 return nfs_page_array_len(0, max_resp_sz);
9660 static void nfs4_layoutget_release(void *calldata)
9662 struct nfs4_layoutget *lgp = calldata;
9664 nfs4_sequence_free_slot(&lgp->res.seq_res);
9665 pnfs_layoutget_free(lgp);
9668 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9669 .rpc_call_prepare = nfs4_layoutget_prepare,
9670 .rpc_call_done = nfs4_layoutget_done,
9671 .rpc_release = nfs4_layoutget_release,
9674 struct pnfs_layout_segment *
9675 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9677 struct inode *inode = lgp->args.inode;
9678 struct nfs_server *server = NFS_SERVER(inode);
9679 struct rpc_task *task;
9680 struct rpc_message msg = {
9681 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9682 .rpc_argp = &lgp->args,
9683 .rpc_resp = &lgp->res,
9684 .rpc_cred = lgp->cred,
9686 struct rpc_task_setup task_setup_data = {
9687 .rpc_client = server->client,
9688 .rpc_message = &msg,
9689 .callback_ops = &nfs4_layoutget_call_ops,
9690 .callback_data = lgp,
9691 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
9694 struct pnfs_layout_segment *lseg = NULL;
9695 struct nfs4_exception exception = {
9697 .timeout = *timeout,
9701 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9703 task = rpc_run_task(&task_setup_data);
9705 return ERR_CAST(task);
9707 status = rpc_wait_for_completion_task(task);
9711 if (task->tk_status < 0) {
9712 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9713 *timeout = exception.timeout;
9714 } else if (lgp->res.layoutp->len == 0) {
9716 *timeout = nfs4_update_delay(&exception.timeout);
9718 lseg = pnfs_layout_process(lgp);
9720 trace_nfs4_layoutget(lgp->args.ctx,
9727 dprintk("<-- %s status=%d\n", __func__, status);
9729 return ERR_PTR(status);
9734 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9736 struct nfs4_layoutreturn *lrp = calldata;
9738 nfs4_setup_sequence(lrp->clp,
9739 &lrp->args.seq_args,
9742 if (!pnfs_layout_is_valid(lrp->args.layout))
9746 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9748 struct nfs4_layoutreturn *lrp = calldata;
9749 struct nfs_server *server;
9751 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9755 * Was there an RPC level error? Assume the call succeeded,
9756 * and that we need to release the layout
9758 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9759 lrp->res.lrs_present = 0;
9763 server = NFS_SERVER(lrp->args.inode);
9764 switch (task->tk_status) {
9765 case -NFS4ERR_OLD_STATEID:
9766 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9772 task->tk_status = 0;
9776 case -NFS4ERR_DELAY:
9777 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9783 task->tk_status = 0;
9784 nfs4_sequence_free_slot(&lrp->res.seq_res);
9785 rpc_restart_call_prepare(task);
9788 static void nfs4_layoutreturn_release(void *calldata)
9790 struct nfs4_layoutreturn *lrp = calldata;
9791 struct pnfs_layout_hdr *lo = lrp->args.layout;
9793 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9794 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9795 nfs4_sequence_free_slot(&lrp->res.seq_res);
9796 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9797 lrp->ld_private.ops->free(&lrp->ld_private);
9798 pnfs_put_layout_hdr(lrp->args.layout);
9799 nfs_iput_and_deactive(lrp->inode);
9800 put_cred(lrp->cred);
9804 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9805 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9806 .rpc_call_done = nfs4_layoutreturn_done,
9807 .rpc_release = nfs4_layoutreturn_release,
9810 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9812 struct rpc_task *task;
9813 struct rpc_message msg = {
9814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9815 .rpc_argp = &lrp->args,
9816 .rpc_resp = &lrp->res,
9817 .rpc_cred = lrp->cred,
9819 struct rpc_task_setup task_setup_data = {
9820 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9821 .rpc_message = &msg,
9822 .callback_ops = &nfs4_layoutreturn_call_ops,
9823 .callback_data = lrp,
9824 .flags = RPC_TASK_MOVEABLE,
9828 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9829 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9830 &task_setup_data.rpc_client, &msg);
9832 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9835 nfs4_layoutreturn_release(lrp);
9838 task_setup_data.flags |= RPC_TASK_ASYNC;
9841 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9844 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9846 task = rpc_run_task(&task_setup_data);
9848 return PTR_ERR(task);
9850 status = task->tk_status;
9851 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9852 dprintk("<-- %s status=%d\n", __func__, status);
9858 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9859 struct pnfs_device *pdev,
9860 const struct cred *cred)
9862 struct nfs4_getdeviceinfo_args args = {
9864 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9865 NOTIFY_DEVICEID4_DELETE,
9867 struct nfs4_getdeviceinfo_res res = {
9870 struct rpc_message msg = {
9871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9878 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9879 if (res.notification & ~args.notify_types)
9880 dprintk("%s: unsupported notification\n", __func__);
9881 if (res.notification != args.notify_types)
9884 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9886 dprintk("<-- %s status=%d\n", __func__, status);
9891 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9892 struct pnfs_device *pdev,
9893 const struct cred *cred)
9895 struct nfs4_exception exception = { };
9899 err = nfs4_handle_exception(server,
9900 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9902 } while (exception.retry);
9905 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9907 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9909 struct nfs4_layoutcommit_data *data = calldata;
9910 struct nfs_server *server = NFS_SERVER(data->args.inode);
9912 nfs4_setup_sequence(server->nfs_client,
9913 &data->args.seq_args,
9919 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9921 struct nfs4_layoutcommit_data *data = calldata;
9922 struct nfs_server *server = NFS_SERVER(data->args.inode);
9924 if (!nfs41_sequence_done(task, &data->res.seq_res))
9927 switch (task->tk_status) { /* Just ignore these failures */
9928 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9929 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9930 case -NFS4ERR_BADLAYOUT: /* no layout */
9931 case -NFS4ERR_GRACE: /* loca_recalim always false */
9932 task->tk_status = 0;
9937 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9938 rpc_restart_call_prepare(task);
9944 static void nfs4_layoutcommit_release(void *calldata)
9946 struct nfs4_layoutcommit_data *data = calldata;
9948 pnfs_cleanup_layoutcommit(data);
9949 nfs_post_op_update_inode_force_wcc(data->args.inode,
9951 put_cred(data->cred);
9952 nfs_iput_and_deactive(data->inode);
9956 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9957 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9958 .rpc_call_done = nfs4_layoutcommit_done,
9959 .rpc_release = nfs4_layoutcommit_release,
9963 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9965 struct rpc_message msg = {
9966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9967 .rpc_argp = &data->args,
9968 .rpc_resp = &data->res,
9969 .rpc_cred = data->cred,
9971 struct rpc_task_setup task_setup_data = {
9972 .task = &data->task,
9973 .rpc_client = NFS_CLIENT(data->args.inode),
9974 .rpc_message = &msg,
9975 .callback_ops = &nfs4_layoutcommit_ops,
9976 .callback_data = data,
9977 .flags = RPC_TASK_MOVEABLE,
9979 struct rpc_task *task;
9982 dprintk("NFS: initiating layoutcommit call. sync %d "
9983 "lbw: %llu inode %lu\n", sync,
9984 data->args.lastbytewritten,
9985 data->args.inode->i_ino);
9988 data->inode = nfs_igrab_and_active(data->args.inode);
9989 if (data->inode == NULL) {
9990 nfs4_layoutcommit_release(data);
9993 task_setup_data.flags = RPC_TASK_ASYNC;
9995 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9996 task = rpc_run_task(&task_setup_data);
9998 return PTR_ERR(task);
10000 status = task->tk_status;
10001 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
10002 dprintk("%s: status %d\n", __func__, status);
10003 rpc_put_task(task);
10008 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
10009 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
10012 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10013 struct nfs_fsinfo *info,
10014 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
10016 struct nfs41_secinfo_no_name_args args = {
10017 .style = SECINFO_STYLE_CURRENT_FH,
10019 struct nfs4_secinfo_res res = {
10020 .flavors = flavors,
10022 struct rpc_message msg = {
10023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
10027 struct nfs4_call_sync_data data = {
10028 .seq_server = server,
10029 .seq_args = &args.seq_args,
10030 .seq_res = &res.seq_res,
10032 struct rpc_task_setup task_setup = {
10033 .rpc_client = server->client,
10034 .rpc_message = &msg,
10035 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
10036 .callback_data = &data,
10037 .flags = RPC_TASK_NO_ROUND_ROBIN,
10039 const struct cred *cred = NULL;
10042 if (use_integrity) {
10043 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
10045 cred = nfs4_get_clid_cred(server->nfs_client);
10046 msg.rpc_cred = cred;
10049 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
10050 status = nfs4_call_sync_custom(&task_setup);
10051 dprintk("<-- %s status=%d\n", __func__, status);
10059 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10060 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
10062 struct nfs4_exception exception = {
10063 .interruptible = true,
10067 /* first try using integrity protection */
10068 err = -NFS4ERR_WRONGSEC;
10070 /* try to use integrity protection with machine cred */
10071 if (_nfs4_is_integrity_protected(server->nfs_client))
10072 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10076 * if unable to use integrity protection, or SECINFO with
10077 * integrity protection returns NFS4ERR_WRONGSEC (which is
10078 * disallowed by spec, but exists in deployed servers) use
10079 * the current filesystem's rpc_client and the user cred.
10081 if (err == -NFS4ERR_WRONGSEC)
10082 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10087 case -NFS4ERR_WRONGSEC:
10091 err = nfs4_handle_exception(server, err, &exception);
10093 } while (exception.retry);
10099 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
10100 struct nfs_fsinfo *info)
10104 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
10105 struct nfs4_secinfo_flavors *flavors;
10106 struct nfs4_secinfo4 *secinfo;
10109 page = alloc_page(GFP_KERNEL);
10115 flavors = page_address(page);
10116 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
10119 * Fall back on "guess and check" method if
10120 * the server doesn't support SECINFO_NO_NAME
10122 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
10123 err = nfs4_find_root_sec(server, fhandle, info);
10129 for (i = 0; i < flavors->num_flavors; i++) {
10130 secinfo = &flavors->flavors[i];
10132 switch (secinfo->flavor) {
10133 case RPC_AUTH_NULL:
10134 case RPC_AUTH_UNIX:
10136 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
10137 &secinfo->flavor_info);
10140 flavor = RPC_AUTH_MAXFLAVOR;
10144 if (!nfs_auth_info_match(&server->auth_info, flavor))
10145 flavor = RPC_AUTH_MAXFLAVOR;
10147 if (flavor != RPC_AUTH_MAXFLAVOR) {
10148 err = nfs4_lookup_root_sec(server, fhandle,
10155 if (flavor == RPC_AUTH_MAXFLAVOR)
10160 if (err == -EACCES)
10166 static int _nfs41_test_stateid(struct nfs_server *server,
10167 nfs4_stateid *stateid,
10168 const struct cred *cred)
10171 struct nfs41_test_stateid_args args = {
10172 .stateid = stateid,
10174 struct nfs41_test_stateid_res res;
10175 struct rpc_message msg = {
10176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10181 struct rpc_clnt *rpc_client = server->client;
10183 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10184 &rpc_client, &msg);
10186 dprintk("NFS call test_stateid %p\n", stateid);
10187 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
10188 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
10189 &args.seq_args, &res.seq_res);
10190 if (status != NFS_OK) {
10191 dprintk("NFS reply test_stateid: failed, %d\n", status);
10194 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10195 return -res.status;
10198 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10199 int err, struct nfs4_exception *exception)
10201 exception->retry = 0;
10203 case -NFS4ERR_DELAY:
10204 case -NFS4ERR_RETRY_UNCACHED_REP:
10205 nfs4_handle_exception(server, err, exception);
10207 case -NFS4ERR_BADSESSION:
10208 case -NFS4ERR_BADSLOT:
10209 case -NFS4ERR_BAD_HIGH_SLOT:
10210 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10211 case -NFS4ERR_DEADSESSION:
10212 nfs4_do_handle_exception(server, err, exception);
10217 * nfs41_test_stateid - perform a TEST_STATEID operation
10219 * @server: server / transport on which to perform the operation
10220 * @stateid: state ID to test
10221 * @cred: credential
10223 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10224 * Otherwise a negative NFS4ERR value is returned if the operation
10225 * failed or the state ID is not currently valid.
10227 static int nfs41_test_stateid(struct nfs_server *server,
10228 nfs4_stateid *stateid,
10229 const struct cred *cred)
10231 struct nfs4_exception exception = {
10232 .interruptible = true,
10236 err = _nfs41_test_stateid(server, stateid, cred);
10237 nfs4_handle_delay_or_session_error(server, err, &exception);
10238 } while (exception.retry);
10242 struct nfs_free_stateid_data {
10243 struct nfs_server *server;
10244 struct nfs41_free_stateid_args args;
10245 struct nfs41_free_stateid_res res;
10248 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10250 struct nfs_free_stateid_data *data = calldata;
10251 nfs4_setup_sequence(data->server->nfs_client,
10252 &data->args.seq_args,
10253 &data->res.seq_res,
10257 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10259 struct nfs_free_stateid_data *data = calldata;
10261 nfs41_sequence_done(task, &data->res.seq_res);
10263 switch (task->tk_status) {
10264 case -NFS4ERR_DELAY:
10265 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10266 rpc_restart_call_prepare(task);
10270 static void nfs41_free_stateid_release(void *calldata)
10272 struct nfs_free_stateid_data *data = calldata;
10273 struct nfs_client *clp = data->server->nfs_client;
10275 nfs_put_client(clp);
10279 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10280 .rpc_call_prepare = nfs41_free_stateid_prepare,
10281 .rpc_call_done = nfs41_free_stateid_done,
10282 .rpc_release = nfs41_free_stateid_release,
10286 * nfs41_free_stateid - perform a FREE_STATEID operation
10288 * @server: server / transport on which to perform the operation
10289 * @stateid: state ID to release
10290 * @cred: credential
10291 * @privileged: set to true if this call needs to be privileged
10293 * Note: this function is always asynchronous.
10295 static int nfs41_free_stateid(struct nfs_server *server,
10296 const nfs4_stateid *stateid,
10297 const struct cred *cred,
10300 struct rpc_message msg = {
10301 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10304 struct rpc_task_setup task_setup = {
10305 .rpc_client = server->client,
10306 .rpc_message = &msg,
10307 .callback_ops = &nfs41_free_stateid_ops,
10308 .flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
10310 struct nfs_free_stateid_data *data;
10311 struct rpc_task *task;
10312 struct nfs_client *clp = server->nfs_client;
10314 if (!refcount_inc_not_zero(&clp->cl_count))
10317 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10318 &task_setup.rpc_client, &msg);
10320 dprintk("NFS call free_stateid %p\n", stateid);
10321 data = kmalloc(sizeof(*data), GFP_KERNEL);
10324 data->server = server;
10325 nfs4_stateid_copy(&data->args.stateid, stateid);
10327 task_setup.callback_data = data;
10329 msg.rpc_argp = &data->args;
10330 msg.rpc_resp = &data->res;
10331 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10332 task = rpc_run_task(&task_setup);
10334 return PTR_ERR(task);
10335 rpc_put_task(task);
10340 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10342 const struct cred *cred = lsp->ls_state->owner->so_cred;
10344 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10345 nfs4_free_lock_state(server, lsp);
10348 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10349 const nfs4_stateid *s2)
10351 if (s1->type != s2->type)
10354 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10357 if (s1->seqid == s2->seqid)
10360 return s1->seqid == 0 || s2->seqid == 0;
10363 #endif /* CONFIG_NFS_V4_1 */
10365 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10366 const nfs4_stateid *s2)
10368 return nfs4_stateid_match(s1, s2);
10372 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10373 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10374 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10375 .recover_open = nfs4_open_reclaim,
10376 .recover_lock = nfs4_lock_reclaim,
10377 .establish_clid = nfs4_init_clientid,
10378 .detect_trunking = nfs40_discover_server_trunking,
10381 #if defined(CONFIG_NFS_V4_1)
10382 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10383 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10384 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10385 .recover_open = nfs4_open_reclaim,
10386 .recover_lock = nfs4_lock_reclaim,
10387 .establish_clid = nfs41_init_clientid,
10388 .reclaim_complete = nfs41_proc_reclaim_complete,
10389 .detect_trunking = nfs41_discover_server_trunking,
10391 #endif /* CONFIG_NFS_V4_1 */
10393 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10394 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10395 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10396 .recover_open = nfs40_open_expired,
10397 .recover_lock = nfs4_lock_expired,
10398 .establish_clid = nfs4_init_clientid,
10401 #if defined(CONFIG_NFS_V4_1)
10402 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10403 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10404 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10405 .recover_open = nfs41_open_expired,
10406 .recover_lock = nfs41_lock_expired,
10407 .establish_clid = nfs41_init_clientid,
10409 #endif /* CONFIG_NFS_V4_1 */
10411 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10412 .sched_state_renewal = nfs4_proc_async_renew,
10413 .get_state_renewal_cred = nfs4_get_renew_cred,
10414 .renew_lease = nfs4_proc_renew,
10417 #if defined(CONFIG_NFS_V4_1)
10418 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10419 .sched_state_renewal = nfs41_proc_async_sequence,
10420 .get_state_renewal_cred = nfs4_get_machine_cred,
10421 .renew_lease = nfs4_proc_sequence,
10425 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10426 .get_locations = _nfs40_proc_get_locations,
10427 .fsid_present = _nfs40_proc_fsid_present,
10430 #if defined(CONFIG_NFS_V4_1)
10431 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10432 .get_locations = _nfs41_proc_get_locations,
10433 .fsid_present = _nfs41_proc_fsid_present,
10435 #endif /* CONFIG_NFS_V4_1 */
10437 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10438 .minor_version = 0,
10439 .init_caps = NFS_CAP_READDIRPLUS
10440 | NFS_CAP_ATOMIC_OPEN
10441 | NFS_CAP_POSIX_LOCK,
10442 .init_client = nfs40_init_client,
10443 .shutdown_client = nfs40_shutdown_client,
10444 .match_stateid = nfs4_match_stateid,
10445 .find_root_sec = nfs4_find_root_sec,
10446 .free_lock_state = nfs4_release_lockowner,
10447 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10448 .alloc_seqid = nfs_alloc_seqid,
10449 .call_sync_ops = &nfs40_call_sync_ops,
10450 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10451 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10452 .state_renewal_ops = &nfs40_state_renewal_ops,
10453 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10456 #if defined(CONFIG_NFS_V4_1)
10457 static struct nfs_seqid *
10458 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10463 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10464 .minor_version = 1,
10465 .init_caps = NFS_CAP_READDIRPLUS
10466 | NFS_CAP_ATOMIC_OPEN
10467 | NFS_CAP_POSIX_LOCK
10468 | NFS_CAP_STATEID_NFSV41
10469 | NFS_CAP_ATOMIC_OPEN_V1
10471 | NFS_CAP_MOVEABLE,
10472 .init_client = nfs41_init_client,
10473 .shutdown_client = nfs41_shutdown_client,
10474 .match_stateid = nfs41_match_stateid,
10475 .find_root_sec = nfs41_find_root_sec,
10476 .free_lock_state = nfs41_free_lock_state,
10477 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10478 .alloc_seqid = nfs_alloc_no_seqid,
10479 .session_trunk = nfs4_test_session_trunk,
10480 .call_sync_ops = &nfs41_call_sync_ops,
10481 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10482 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10483 .state_renewal_ops = &nfs41_state_renewal_ops,
10484 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10488 #if defined(CONFIG_NFS_V4_2)
10489 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10490 .minor_version = 2,
10491 .init_caps = NFS_CAP_READDIRPLUS
10492 | NFS_CAP_ATOMIC_OPEN
10493 | NFS_CAP_POSIX_LOCK
10494 | NFS_CAP_STATEID_NFSV41
10495 | NFS_CAP_ATOMIC_OPEN_V1
10499 | NFS_CAP_OFFLOAD_CANCEL
10500 | NFS_CAP_COPY_NOTIFY
10501 | NFS_CAP_DEALLOCATE
10503 | NFS_CAP_LAYOUTSTATS
10505 | NFS_CAP_LAYOUTERROR
10506 | NFS_CAP_READ_PLUS
10507 | NFS_CAP_MOVEABLE,
10508 .init_client = nfs41_init_client,
10509 .shutdown_client = nfs41_shutdown_client,
10510 .match_stateid = nfs41_match_stateid,
10511 .find_root_sec = nfs41_find_root_sec,
10512 .free_lock_state = nfs41_free_lock_state,
10513 .call_sync_ops = &nfs41_call_sync_ops,
10514 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10515 .alloc_seqid = nfs_alloc_no_seqid,
10516 .session_trunk = nfs4_test_session_trunk,
10517 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10518 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10519 .state_renewal_ops = &nfs41_state_renewal_ops,
10520 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10524 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10525 [0] = &nfs_v4_0_minor_ops,
10526 #if defined(CONFIG_NFS_V4_1)
10527 [1] = &nfs_v4_1_minor_ops,
10529 #if defined(CONFIG_NFS_V4_2)
10530 [2] = &nfs_v4_2_minor_ops,
10534 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10536 ssize_t error, error2, error3;
10538 error = generic_listxattr(dentry, list, size);
10546 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10555 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10559 return error + error2 + error3;
10562 static void nfs4_enable_swap(struct inode *inode)
10564 /* The state manager thread must always be running.
10565 * It will notice the client is a swapper, and stay put.
10567 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10569 nfs4_schedule_state_manager(clp);
10572 static void nfs4_disable_swap(struct inode *inode)
10574 /* The state manager thread will now exit once it is
10577 wake_up_var(&NFS_SERVER(inode)->nfs_client->cl_state);
10580 static const struct inode_operations nfs4_dir_inode_operations = {
10581 .create = nfs_create,
10582 .lookup = nfs_lookup,
10583 .atomic_open = nfs_atomic_open,
10585 .unlink = nfs_unlink,
10586 .symlink = nfs_symlink,
10587 .mkdir = nfs_mkdir,
10588 .rmdir = nfs_rmdir,
10589 .mknod = nfs_mknod,
10590 .rename = nfs_rename,
10591 .permission = nfs_permission,
10592 .getattr = nfs_getattr,
10593 .setattr = nfs_setattr,
10594 .listxattr = nfs4_listxattr,
10597 static const struct inode_operations nfs4_file_inode_operations = {
10598 .permission = nfs_permission,
10599 .getattr = nfs_getattr,
10600 .setattr = nfs_setattr,
10601 .listxattr = nfs4_listxattr,
10604 const struct nfs_rpc_ops nfs_v4_clientops = {
10605 .version = 4, /* protocol version */
10606 .dentry_ops = &nfs4_dentry_operations,
10607 .dir_inode_ops = &nfs4_dir_inode_operations,
10608 .file_inode_ops = &nfs4_file_inode_operations,
10609 .file_ops = &nfs4_file_operations,
10610 .getroot = nfs4_proc_get_root,
10611 .submount = nfs4_submount,
10612 .try_get_tree = nfs4_try_get_tree,
10613 .getattr = nfs4_proc_getattr,
10614 .setattr = nfs4_proc_setattr,
10615 .lookup = nfs4_proc_lookup,
10616 .lookupp = nfs4_proc_lookupp,
10617 .access = nfs4_proc_access,
10618 .readlink = nfs4_proc_readlink,
10619 .create = nfs4_proc_create,
10620 .remove = nfs4_proc_remove,
10621 .unlink_setup = nfs4_proc_unlink_setup,
10622 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10623 .unlink_done = nfs4_proc_unlink_done,
10624 .rename_setup = nfs4_proc_rename_setup,
10625 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10626 .rename_done = nfs4_proc_rename_done,
10627 .link = nfs4_proc_link,
10628 .symlink = nfs4_proc_symlink,
10629 .mkdir = nfs4_proc_mkdir,
10630 .rmdir = nfs4_proc_rmdir,
10631 .readdir = nfs4_proc_readdir,
10632 .mknod = nfs4_proc_mknod,
10633 .statfs = nfs4_proc_statfs,
10634 .fsinfo = nfs4_proc_fsinfo,
10635 .pathconf = nfs4_proc_pathconf,
10636 .set_capabilities = nfs4_server_capabilities,
10637 .decode_dirent = nfs4_decode_dirent,
10638 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10639 .read_setup = nfs4_proc_read_setup,
10640 .read_done = nfs4_read_done,
10641 .write_setup = nfs4_proc_write_setup,
10642 .write_done = nfs4_write_done,
10643 .commit_setup = nfs4_proc_commit_setup,
10644 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10645 .commit_done = nfs4_commit_done,
10646 .lock = nfs4_proc_lock,
10647 .clear_acl_cache = nfs4_zap_acl_attr,
10648 .close_context = nfs4_close_context,
10649 .open_context = nfs4_atomic_open,
10650 .have_delegation = nfs4_have_delegation,
10651 .alloc_client = nfs4_alloc_client,
10652 .init_client = nfs4_init_client,
10653 .free_client = nfs4_free_client,
10654 .create_server = nfs4_create_server,
10655 .clone_server = nfs_clone_server,
10656 .discover_trunking = nfs4_discover_trunking,
10657 .enable_swap = nfs4_enable_swap,
10658 .disable_swap = nfs4_disable_swap,
10661 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10662 .name = XATTR_NAME_NFSV4_ACL,
10663 .list = nfs4_xattr_list_nfs4_acl,
10664 .get = nfs4_xattr_get_nfs4_acl,
10665 .set = nfs4_xattr_set_nfs4_acl,
10668 #if defined(CONFIG_NFS_V4_1)
10669 static const struct xattr_handler nfs4_xattr_nfs4_dacl_handler = {
10670 .name = XATTR_NAME_NFSV4_DACL,
10671 .list = nfs4_xattr_list_nfs4_dacl,
10672 .get = nfs4_xattr_get_nfs4_dacl,
10673 .set = nfs4_xattr_set_nfs4_dacl,
10676 static const struct xattr_handler nfs4_xattr_nfs4_sacl_handler = {
10677 .name = XATTR_NAME_NFSV4_SACL,
10678 .list = nfs4_xattr_list_nfs4_sacl,
10679 .get = nfs4_xattr_get_nfs4_sacl,
10680 .set = nfs4_xattr_set_nfs4_sacl,
10684 #ifdef CONFIG_NFS_V4_2
10685 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10686 .prefix = XATTR_USER_PREFIX,
10687 .get = nfs4_xattr_get_nfs4_user,
10688 .set = nfs4_xattr_set_nfs4_user,
10692 const struct xattr_handler *nfs4_xattr_handlers[] = {
10693 &nfs4_xattr_nfs4_acl_handler,
10694 #if defined(CONFIG_NFS_V4_1)
10695 &nfs4_xattr_nfs4_dacl_handler,
10696 &nfs4_xattr_nfs4_sacl_handler,
10698 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10699 &nfs4_xattr_nfs4_label_handler,
10701 #ifdef CONFIG_NFS_V4_2
10702 &nfs4_xattr_nfs4_user_handler,