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_test_and_free_stateid(struct nfs_server *server,
367 nfs4_stateid *stateid,
368 const struct cred *cred)
370 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
372 ops->test_and_free_expired(server, stateid, cred);
375 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
376 nfs4_stateid *stateid,
377 const struct cred *cred)
379 stateid->type = NFS4_REVOKED_STATEID_TYPE;
380 nfs4_test_and_free_stateid(server, stateid, cred);
383 static void nfs4_free_revoked_stateid(struct nfs_server *server,
384 const nfs4_stateid *stateid,
385 const struct cred *cred)
389 nfs4_stateid_copy(&tmp, stateid);
390 __nfs4_free_revoked_stateid(server, &tmp, cred);
393 static long nfs4_update_delay(long *timeout)
397 return NFS4_POLL_RETRY_MAX;
399 *timeout = NFS4_POLL_RETRY_MIN;
400 if (*timeout > NFS4_POLL_RETRY_MAX)
401 *timeout = NFS4_POLL_RETRY_MAX;
407 static int nfs4_delay_killable(long *timeout)
411 freezable_schedule_timeout_killable_unsafe(
412 nfs4_update_delay(timeout));
413 if (!__fatal_signal_pending(current))
418 static int nfs4_delay_interruptible(long *timeout)
422 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
423 if (!signal_pending(current))
425 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
428 static int nfs4_delay(long *timeout, bool interruptible)
431 return nfs4_delay_interruptible(timeout);
432 return nfs4_delay_killable(timeout);
435 static const nfs4_stateid *
436 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
440 switch (stateid->type) {
441 case NFS4_OPEN_STATEID_TYPE:
442 case NFS4_LOCK_STATEID_TYPE:
443 case NFS4_DELEGATION_STATEID_TYPE:
451 /* This is the error handling routine for processes that are allowed
454 static int nfs4_do_handle_exception(struct nfs_server *server,
455 int errorcode, struct nfs4_exception *exception)
457 struct nfs_client *clp = server->nfs_client;
458 struct nfs4_state *state = exception->state;
459 const nfs4_stateid *stateid;
460 struct inode *inode = exception->inode;
463 exception->delay = 0;
464 exception->recovering = 0;
465 exception->retry = 0;
467 stateid = nfs4_recoverable_stateid(exception->stateid);
468 if (stateid == NULL && state != NULL)
469 stateid = nfs4_recoverable_stateid(&state->stateid);
474 case -NFS4ERR_BADHANDLE:
476 if (inode != NULL && S_ISREG(inode->i_mode))
477 pnfs_destroy_layout(NFS_I(inode));
479 case -NFS4ERR_DELEG_REVOKED:
480 case -NFS4ERR_ADMIN_REVOKED:
481 case -NFS4ERR_EXPIRED:
482 case -NFS4ERR_BAD_STATEID:
483 case -NFS4ERR_PARTNER_NO_AUTH:
484 if (inode != NULL && stateid != NULL) {
485 nfs_inode_find_state_and_recover(inode,
487 goto wait_on_recovery;
490 case -NFS4ERR_OPENMODE:
494 err = nfs_async_inode_return_delegation(inode,
497 goto wait_on_recovery;
498 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
499 exception->retry = 1;
505 ret = nfs4_schedule_stateid_recovery(server, state);
508 goto wait_on_recovery;
509 case -NFS4ERR_STALE_STATEID:
510 case -NFS4ERR_STALE_CLIENTID:
511 nfs4_schedule_lease_recovery(clp);
512 goto wait_on_recovery;
514 ret = nfs4_schedule_migration_recovery(server);
517 goto wait_on_recovery;
518 case -NFS4ERR_LEASE_MOVED:
519 nfs4_schedule_lease_moved_recovery(clp);
520 goto wait_on_recovery;
521 #if defined(CONFIG_NFS_V4_1)
522 case -NFS4ERR_BADSESSION:
523 case -NFS4ERR_BADSLOT:
524 case -NFS4ERR_BAD_HIGH_SLOT:
525 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
526 case -NFS4ERR_DEADSESSION:
527 case -NFS4ERR_SEQ_FALSE_RETRY:
528 case -NFS4ERR_SEQ_MISORDERED:
529 /* Handled in nfs41_sequence_process() */
530 goto wait_on_recovery;
531 #endif /* defined(CONFIG_NFS_V4_1) */
532 case -NFS4ERR_FILE_OPEN:
533 if (exception->timeout > HZ) {
534 /* We have retried a decent amount, time to
542 nfs_inc_server_stats(server, NFSIOS_DELAY);
545 case -NFS4ERR_LAYOUTTRYLATER:
546 case -NFS4ERR_RECALLCONFLICT:
547 exception->delay = 1;
550 case -NFS4ERR_RETRY_UNCACHED_REP:
551 case -NFS4ERR_OLD_STATEID:
552 exception->retry = 1;
554 case -NFS4ERR_BADOWNER:
555 /* The following works around a Linux server bug! */
556 case -NFS4ERR_BADNAME:
557 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
558 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
559 exception->retry = 1;
560 printk(KERN_WARNING "NFS: v4 server %s "
561 "does not accept raw "
563 "Reenabling the idmapper.\n",
564 server->nfs_client->cl_hostname);
567 /* We failed to handle the error */
568 return nfs4_map_errors(ret);
570 exception->recovering = 1;
574 /* This is the error handling routine for processes that are allowed
577 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
579 struct nfs_client *clp = server->nfs_client;
582 ret = nfs4_do_handle_exception(server, errorcode, exception);
583 if (exception->delay) {
584 ret = nfs4_delay(&exception->timeout,
585 exception->interruptible);
588 if (exception->recovering) {
589 if (exception->task_is_privileged)
591 ret = nfs4_wait_clnt_recover(clp);
592 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
599 exception->retry = 1;
604 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
605 int errorcode, struct nfs4_exception *exception)
607 struct nfs_client *clp = server->nfs_client;
610 ret = nfs4_do_handle_exception(server, errorcode, exception);
611 if (exception->delay) {
612 rpc_delay(task, nfs4_update_delay(&exception->timeout));
615 if (exception->recovering) {
616 if (exception->task_is_privileged)
618 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
619 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
620 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
623 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
628 exception->retry = 1;
630 * For NFS4ERR_MOVED, the client transport will need to
631 * be recomputed after migration recovery has completed.
633 if (errorcode == -NFS4ERR_MOVED)
634 rpc_task_release_transport(task);
640 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
641 struct nfs4_state *state, long *timeout)
643 struct nfs4_exception exception = {
647 if (task->tk_status >= 0)
650 exception.timeout = *timeout;
651 task->tk_status = nfs4_async_handle_exception(task, server,
654 if (exception.delay && timeout)
655 *timeout = exception.timeout;
662 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
663 * or 'false' otherwise.
665 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
667 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
668 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
671 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
673 spin_lock(&clp->cl_lock);
674 if (time_before(clp->cl_last_renewal,timestamp))
675 clp->cl_last_renewal = timestamp;
676 spin_unlock(&clp->cl_lock);
679 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
681 struct nfs_client *clp = server->nfs_client;
683 if (!nfs4_has_session(clp))
684 do_renew_lease(clp, timestamp);
687 struct nfs4_call_sync_data {
688 const struct nfs_server *seq_server;
689 struct nfs4_sequence_args *seq_args;
690 struct nfs4_sequence_res *seq_res;
693 void nfs4_init_sequence(struct nfs4_sequence_args *args,
694 struct nfs4_sequence_res *res, int cache_reply,
697 args->sa_slot = NULL;
698 args->sa_cache_this = cache_reply;
699 args->sa_privileged = privileged;
704 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
706 struct nfs4_slot *slot = res->sr_slot;
707 struct nfs4_slot_table *tbl;
710 spin_lock(&tbl->slot_tbl_lock);
711 if (!nfs41_wake_and_assign_slot(tbl, slot))
712 nfs4_free_slot(tbl, slot);
713 spin_unlock(&tbl->slot_tbl_lock);
718 static int nfs40_sequence_done(struct rpc_task *task,
719 struct nfs4_sequence_res *res)
721 if (res->sr_slot != NULL)
722 nfs40_sequence_free_slot(res);
726 #if defined(CONFIG_NFS_V4_1)
728 static void nfs41_release_slot(struct nfs4_slot *slot)
730 struct nfs4_session *session;
731 struct nfs4_slot_table *tbl;
732 bool send_new_highest_used_slotid = false;
737 session = tbl->session;
739 /* Bump the slot sequence number */
744 spin_lock(&tbl->slot_tbl_lock);
745 /* Be nice to the server: try to ensure that the last transmitted
746 * value for highest_user_slotid <= target_highest_slotid
748 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
749 send_new_highest_used_slotid = true;
751 if (nfs41_wake_and_assign_slot(tbl, slot)) {
752 send_new_highest_used_slotid = false;
755 nfs4_free_slot(tbl, slot);
757 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
758 send_new_highest_used_slotid = false;
760 spin_unlock(&tbl->slot_tbl_lock);
761 if (send_new_highest_used_slotid)
762 nfs41_notify_server(session->clp);
763 if (waitqueue_active(&tbl->slot_waitq))
764 wake_up_all(&tbl->slot_waitq);
767 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
769 nfs41_release_slot(res->sr_slot);
773 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
776 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
777 slot->seq_nr_highest_sent = seqnr;
779 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
782 slot->seq_nr_highest_sent = seqnr;
783 slot->seq_nr_last_acked = seqnr;
786 static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
787 struct nfs4_slot *slot)
789 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
791 rpc_put_task_async(task);
794 static int nfs41_sequence_process(struct rpc_task *task,
795 struct nfs4_sequence_res *res)
797 struct nfs4_session *session;
798 struct nfs4_slot *slot = res->sr_slot;
799 struct nfs_client *clp;
805 /* don't increment the sequence number if the task wasn't sent */
806 if (!RPC_WAS_SENT(task) || slot->seq_done)
809 session = slot->table->session;
812 trace_nfs4_sequence_done(session, res);
814 status = res->sr_status;
815 if (task->tk_status == -NFS4ERR_DEADSESSION)
816 status = -NFS4ERR_DEADSESSION;
818 /* Check the SEQUENCE operation status */
821 /* Mark this sequence number as having been acked */
822 nfs4_slot_sequence_acked(slot, slot->seq_nr);
823 /* Update the slot's sequence and clientid lease timer */
825 do_renew_lease(clp, res->sr_timestamp);
826 /* Check sequence flags */
827 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
829 nfs41_update_target_slotid(slot->table, slot, res);
833 * sr_status remains 1 if an RPC level error occurred.
834 * The server may or may not have processed the sequence
837 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
841 /* The server detected a resend of the RPC call and
842 * returned NFS4ERR_DELAY as per Section 2.10.6.2
845 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
849 nfs4_slot_sequence_acked(slot, slot->seq_nr);
851 case -NFS4ERR_RETRY_UNCACHED_REP:
852 case -NFS4ERR_SEQ_FALSE_RETRY:
854 * The server thinks we tried to replay a request.
855 * Retry the call after bumping the sequence ID.
857 nfs4_slot_sequence_acked(slot, slot->seq_nr);
859 case -NFS4ERR_BADSLOT:
861 * The slot id we used was probably retired. Try again
862 * using a different slot id.
864 if (slot->slot_nr < slot->table->target_highest_slotid)
865 goto session_recover;
867 case -NFS4ERR_SEQ_MISORDERED:
868 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
870 * Were one or more calls using this slot interrupted?
871 * If the server never received the request, then our
872 * transmitted slot sequence number may be too high. However,
873 * if the server did receive the request then it might
874 * accidentally give us a reply with a mismatched operation.
875 * We can sort this out by sending a lone sequence operation
876 * to the server on the same slot.
878 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
880 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
881 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
888 * A retry might be sent while the original request is
889 * still in progress on the replier. The replier SHOULD
890 * deal with the issue by returning NFS4ERR_DELAY as the
891 * reply to SEQUENCE or CB_SEQUENCE operation, but
892 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
894 * Restart the search after a delay.
896 slot->seq_nr = slot->seq_nr_highest_sent;
898 case -NFS4ERR_BADSESSION:
899 case -NFS4ERR_DEADSESSION:
900 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
901 goto session_recover;
903 /* Just update the slot sequence no. */
907 /* The session may be reset by one of the error handlers. */
908 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
912 nfs4_schedule_session_recovery(session, status);
913 dprintk("%s ERROR: %d Reset session\n", __func__, status);
914 nfs41_sequence_free_slot(res);
919 if (rpc_restart_call_prepare(task)) {
920 nfs41_sequence_free_slot(res);
926 if (!rpc_restart_call(task))
928 rpc_delay(task, NFS4_POLL_RETRY_MAX);
932 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
934 if (!nfs41_sequence_process(task, res))
936 if (res->sr_slot != NULL)
937 nfs41_sequence_free_slot(res);
941 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
943 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
945 if (res->sr_slot == NULL)
947 if (res->sr_slot->table->session != NULL)
948 return nfs41_sequence_process(task, res);
949 return nfs40_sequence_done(task, res);
952 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
954 if (res->sr_slot != NULL) {
955 if (res->sr_slot->table->session != NULL)
956 nfs41_sequence_free_slot(res);
958 nfs40_sequence_free_slot(res);
962 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
964 if (res->sr_slot == NULL)
966 if (!res->sr_slot->table->session)
967 return nfs40_sequence_done(task, res);
968 return nfs41_sequence_done(task, res);
970 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
972 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
974 struct nfs4_call_sync_data *data = calldata;
976 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
978 nfs4_setup_sequence(data->seq_server->nfs_client,
979 data->seq_args, data->seq_res, task);
982 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
984 struct nfs4_call_sync_data *data = calldata;
986 nfs41_sequence_done(task, data->seq_res);
989 static const struct rpc_call_ops nfs41_call_sync_ops = {
990 .rpc_call_prepare = nfs41_call_sync_prepare,
991 .rpc_call_done = nfs41_call_sync_done,
994 #else /* !CONFIG_NFS_V4_1 */
996 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
998 return nfs40_sequence_done(task, res);
1001 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1003 if (res->sr_slot != NULL)
1004 nfs40_sequence_free_slot(res);
1007 int nfs4_sequence_done(struct rpc_task *task,
1008 struct nfs4_sequence_res *res)
1010 return nfs40_sequence_done(task, res);
1012 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1014 #endif /* !CONFIG_NFS_V4_1 */
1016 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1018 res->sr_timestamp = jiffies;
1019 res->sr_status_flags = 0;
1024 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1025 struct nfs4_sequence_res *res,
1026 struct nfs4_slot *slot)
1030 slot->privileged = args->sa_privileged ? 1 : 0;
1031 args->sa_slot = slot;
1033 res->sr_slot = slot;
1036 int nfs4_setup_sequence(struct nfs_client *client,
1037 struct nfs4_sequence_args *args,
1038 struct nfs4_sequence_res *res,
1039 struct rpc_task *task)
1041 struct nfs4_session *session = nfs4_get_session(client);
1042 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1043 struct nfs4_slot *slot;
1045 /* slot already allocated? */
1046 if (res->sr_slot != NULL)
1050 tbl = &session->fc_slot_table;
1052 spin_lock(&tbl->slot_tbl_lock);
1053 /* The state manager will wait until the slot table is empty */
1054 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1057 slot = nfs4_alloc_slot(tbl);
1059 if (slot == ERR_PTR(-ENOMEM))
1060 goto out_sleep_timeout;
1063 spin_unlock(&tbl->slot_tbl_lock);
1065 nfs4_sequence_attach_slot(args, res, slot);
1067 trace_nfs4_setup_sequence(session, args);
1069 nfs41_sequence_res_init(res);
1070 rpc_call_start(task);
1073 /* Try again in 1/4 second */
1074 if (args->sa_privileged)
1075 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1076 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1078 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1079 NULL, jiffies + (HZ >> 2));
1080 spin_unlock(&tbl->slot_tbl_lock);
1083 if (args->sa_privileged)
1084 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1085 RPC_PRIORITY_PRIVILEGED);
1087 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1088 spin_unlock(&tbl->slot_tbl_lock);
1091 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1093 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1095 struct nfs4_call_sync_data *data = calldata;
1096 nfs4_setup_sequence(data->seq_server->nfs_client,
1097 data->seq_args, data->seq_res, task);
1100 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1102 struct nfs4_call_sync_data *data = calldata;
1103 nfs4_sequence_done(task, data->seq_res);
1106 static const struct rpc_call_ops nfs40_call_sync_ops = {
1107 .rpc_call_prepare = nfs40_call_sync_prepare,
1108 .rpc_call_done = nfs40_call_sync_done,
1111 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1114 struct rpc_task *task;
1116 task = rpc_run_task(task_setup);
1118 return PTR_ERR(task);
1120 ret = task->tk_status;
1125 static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1126 struct nfs_server *server,
1127 struct rpc_message *msg,
1128 struct nfs4_sequence_args *args,
1129 struct nfs4_sequence_res *res,
1130 unsigned short task_flags)
1132 struct nfs_client *clp = server->nfs_client;
1133 struct nfs4_call_sync_data data = {
1134 .seq_server = server,
1138 struct rpc_task_setup task_setup = {
1141 .callback_ops = clp->cl_mvops->call_sync_ops,
1142 .callback_data = &data,
1143 .flags = task_flags,
1146 return nfs4_call_sync_custom(&task_setup);
1149 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1150 struct nfs_server *server,
1151 struct rpc_message *msg,
1152 struct nfs4_sequence_args *args,
1153 struct nfs4_sequence_res *res)
1155 unsigned short task_flags = 0;
1157 if (server->nfs_client->cl_minorversion)
1158 task_flags = RPC_TASK_MOVEABLE;
1159 return nfs4_do_call_sync(clnt, server, msg, args, res, task_flags);
1163 int nfs4_call_sync(struct rpc_clnt *clnt,
1164 struct nfs_server *server,
1165 struct rpc_message *msg,
1166 struct nfs4_sequence_args *args,
1167 struct nfs4_sequence_res *res,
1170 nfs4_init_sequence(args, res, cache_reply, 0);
1171 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1175 nfs4_inc_nlink_locked(struct inode *inode)
1177 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1178 NFS_INO_INVALID_CTIME |
1179 NFS_INO_INVALID_NLINK);
1184 nfs4_inc_nlink(struct inode *inode)
1186 spin_lock(&inode->i_lock);
1187 nfs4_inc_nlink_locked(inode);
1188 spin_unlock(&inode->i_lock);
1192 nfs4_dec_nlink_locked(struct inode *inode)
1194 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1195 NFS_INO_INVALID_CTIME |
1196 NFS_INO_INVALID_NLINK);
1201 nfs4_update_changeattr_locked(struct inode *inode,
1202 struct nfs4_change_info *cinfo,
1203 unsigned long timestamp, unsigned long cache_validity)
1205 struct nfs_inode *nfsi = NFS_I(inode);
1206 u64 change_attr = inode_peek_iversion_raw(inode);
1208 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
1209 if (S_ISDIR(inode->i_mode))
1210 cache_validity |= NFS_INO_INVALID_DATA;
1212 switch (NFS_SERVER(inode)->change_attr_type) {
1213 case NFS4_CHANGE_TYPE_IS_UNDEFINED:
1214 if (cinfo->after == change_attr)
1218 if ((s64)(change_attr - cinfo->after) >= 0)
1222 inode_set_iversion_raw(inode, cinfo->after);
1223 if (!cinfo->atomic || cinfo->before != change_attr) {
1224 if (S_ISDIR(inode->i_mode))
1225 nfs_force_lookup_revalidate(inode);
1227 if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1229 NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
1230 NFS_INO_INVALID_SIZE | NFS_INO_INVALID_OTHER |
1231 NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
1232 NFS_INO_INVALID_MODE | NFS_INO_INVALID_XATTR |
1233 NFS_INO_REVAL_PAGECACHE;
1234 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1236 nfsi->attrtimeo_timestamp = jiffies;
1237 nfsi->read_cache_jiffies = timestamp;
1238 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1239 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1241 nfs_set_cache_invalid(inode, cache_validity);
1245 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1246 unsigned long timestamp, unsigned long cache_validity)
1248 spin_lock(&dir->i_lock);
1249 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1250 spin_unlock(&dir->i_lock);
1253 struct nfs4_open_createattrs {
1254 struct nfs4_label *label;
1255 struct iattr *sattr;
1256 const __u32 verf[2];
1259 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1260 int err, struct nfs4_exception *exception)
1264 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1266 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1267 exception->retry = 1;
1271 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1273 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1276 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1278 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1280 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1284 nfs4_map_atomic_open_share(struct nfs_server *server,
1285 fmode_t fmode, int openflags)
1289 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1291 res = NFS4_SHARE_ACCESS_READ;
1294 res = NFS4_SHARE_ACCESS_WRITE;
1296 case FMODE_READ|FMODE_WRITE:
1297 res = NFS4_SHARE_ACCESS_BOTH;
1299 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1301 /* Want no delegation if we're using O_DIRECT */
1302 if (openflags & O_DIRECT)
1303 res |= NFS4_SHARE_WANT_NO_DELEG;
1308 static enum open_claim_type4
1309 nfs4_map_atomic_open_claim(struct nfs_server *server,
1310 enum open_claim_type4 claim)
1312 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1317 case NFS4_OPEN_CLAIM_FH:
1318 return NFS4_OPEN_CLAIM_NULL;
1319 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1320 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1321 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1322 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1326 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1328 p->o_res.f_attr = &p->f_attr;
1329 p->o_res.seqid = p->o_arg.seqid;
1330 p->c_res.seqid = p->c_arg.seqid;
1331 p->o_res.server = p->o_arg.server;
1332 p->o_res.access_request = p->o_arg.access;
1333 nfs_fattr_init(&p->f_attr);
1334 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1337 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1338 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1339 const struct nfs4_open_createattrs *c,
1340 enum open_claim_type4 claim,
1343 struct dentry *parent = dget_parent(dentry);
1344 struct inode *dir = d_inode(parent);
1345 struct nfs_server *server = NFS_SERVER(dir);
1346 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1347 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1348 struct nfs4_opendata *p;
1350 p = kzalloc(sizeof(*p), gfp_mask);
1354 p->f_attr.label = nfs4_label_alloc(server, gfp_mask);
1355 if (IS_ERR(p->f_attr.label))
1358 p->a_label = nfs4_label_alloc(server, gfp_mask);
1359 if (IS_ERR(p->a_label))
1362 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1363 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1364 if (IS_ERR(p->o_arg.seqid))
1365 goto err_free_label;
1366 nfs_sb_active(dentry->d_sb);
1367 p->dentry = dget(dentry);
1370 atomic_inc(&sp->so_count);
1371 p->o_arg.open_flags = flags;
1372 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1373 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1374 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1376 if (flags & O_CREAT) {
1377 p->o_arg.umask = current_umask();
1378 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1379 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1380 p->o_arg.u.attrs = &p->attrs;
1381 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1383 memcpy(p->o_arg.u.verifier.data, c->verf,
1384 sizeof(p->o_arg.u.verifier.data));
1387 /* ask server to check for all possible rights as results
1389 switch (p->o_arg.claim) {
1392 case NFS4_OPEN_CLAIM_NULL:
1393 case NFS4_OPEN_CLAIM_FH:
1394 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1395 NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE |
1396 NFS4_ACCESS_EXECUTE;
1397 #ifdef CONFIG_NFS_V4_2
1398 if (!(server->caps & NFS_CAP_XATTR))
1400 p->o_arg.access |= NFS4_ACCESS_XAREAD | NFS4_ACCESS_XAWRITE |
1404 p->o_arg.clientid = server->nfs_client->cl_clientid;
1405 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1406 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1407 p->o_arg.name = &dentry->d_name;
1408 p->o_arg.server = server;
1409 p->o_arg.bitmask = nfs4_bitmask(server, label);
1410 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1411 switch (p->o_arg.claim) {
1412 case NFS4_OPEN_CLAIM_NULL:
1413 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1414 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1415 p->o_arg.fh = NFS_FH(dir);
1417 case NFS4_OPEN_CLAIM_PREVIOUS:
1418 case NFS4_OPEN_CLAIM_FH:
1419 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1420 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1421 p->o_arg.fh = NFS_FH(d_inode(dentry));
1423 p->c_arg.fh = &p->o_res.fh;
1424 p->c_arg.stateid = &p->o_res.stateid;
1425 p->c_arg.seqid = p->o_arg.seqid;
1426 nfs4_init_opendata_res(p);
1427 kref_init(&p->kref);
1431 nfs4_label_free(p->a_label);
1433 nfs4_label_free(p->f_attr.label);
1441 static void nfs4_opendata_free(struct kref *kref)
1443 struct nfs4_opendata *p = container_of(kref,
1444 struct nfs4_opendata, kref);
1445 struct super_block *sb = p->dentry->d_sb;
1447 nfs4_lgopen_release(p->lgp);
1448 nfs_free_seqid(p->o_arg.seqid);
1449 nfs4_sequence_free_slot(&p->o_res.seq_res);
1450 if (p->state != NULL)
1451 nfs4_put_open_state(p->state);
1452 nfs4_put_state_owner(p->owner);
1454 nfs4_label_free(p->a_label);
1455 nfs4_label_free(p->f_attr.label);
1459 nfs_sb_deactive(sb);
1460 nfs_fattr_free_names(&p->f_attr);
1461 kfree(p->f_attr.mdsthreshold);
1465 static void nfs4_opendata_put(struct nfs4_opendata *p)
1468 kref_put(&p->kref, nfs4_opendata_free);
1471 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1474 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1475 case FMODE_READ|FMODE_WRITE:
1476 return state->n_rdwr != 0;
1478 return state->n_wronly != 0;
1480 return state->n_rdonly != 0;
1486 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1487 int open_mode, enum open_claim_type4 claim)
1491 if (open_mode & (O_EXCL|O_TRUNC))
1494 case NFS4_OPEN_CLAIM_NULL:
1495 case NFS4_OPEN_CLAIM_FH:
1500 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1502 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1503 && state->n_rdonly != 0;
1506 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1507 && state->n_wronly != 0;
1509 case FMODE_READ|FMODE_WRITE:
1510 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1511 && state->n_rdwr != 0;
1517 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1518 enum open_claim_type4 claim)
1520 if (delegation == NULL)
1522 if ((delegation->type & fmode) != fmode)
1525 case NFS4_OPEN_CLAIM_NULL:
1526 case NFS4_OPEN_CLAIM_FH:
1528 case NFS4_OPEN_CLAIM_PREVIOUS:
1529 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1535 nfs_mark_delegation_referenced(delegation);
1539 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1548 case FMODE_READ|FMODE_WRITE:
1551 nfs4_state_set_mode_locked(state, state->state | fmode);
1554 #ifdef CONFIG_NFS_V4_1
1555 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1557 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1559 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1561 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1565 #endif /* CONFIG_NFS_V4_1 */
1567 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1569 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1570 wake_up_all(&state->waitq);
1573 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1575 struct nfs_client *clp = state->owner->so_server->nfs_client;
1576 bool need_recover = false;
1578 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1579 need_recover = true;
1580 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1581 need_recover = true;
1582 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1583 need_recover = true;
1585 nfs4_state_mark_reclaim_nograce(clp, state);
1589 * Check for whether or not the caller may update the open stateid
1590 * to the value passed in by stateid.
1592 * Note: This function relies heavily on the server implementing
1593 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1595 * i.e. The stateid seqids have to be initialised to 1, and
1596 * are then incremented on every state transition.
1598 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1599 const nfs4_stateid *stateid)
1601 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1602 /* The common case - we're updating to a new sequence number */
1603 if (nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1604 if (nfs4_stateid_is_next(&state->open_stateid, stateid))
1608 /* The server returned a new stateid */
1610 /* This is the first OPEN in this generation */
1611 if (stateid->seqid == cpu_to_be32(1))
1616 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1618 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1620 if (state->n_wronly)
1621 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1622 if (state->n_rdonly)
1623 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1625 set_bit(NFS_O_RDWR_STATE, &state->flags);
1626 set_bit(NFS_OPEN_STATE, &state->flags);
1629 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1630 nfs4_stateid *stateid, fmode_t fmode)
1632 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1633 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1635 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1638 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1641 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1642 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1643 clear_bit(NFS_OPEN_STATE, &state->flags);
1645 if (stateid == NULL)
1647 /* Handle OPEN+OPEN_DOWNGRADE races */
1648 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1649 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1650 nfs_resync_open_stateid_locked(state);
1653 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1654 nfs4_stateid_copy(&state->stateid, stateid);
1655 nfs4_stateid_copy(&state->open_stateid, stateid);
1656 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1658 nfs_state_log_update_open_stateid(state);
1661 static void nfs_clear_open_stateid(struct nfs4_state *state,
1662 nfs4_stateid *arg_stateid,
1663 nfs4_stateid *stateid, fmode_t fmode)
1665 write_seqlock(&state->seqlock);
1666 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1667 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1668 nfs_clear_open_stateid_locked(state, stateid, fmode);
1669 write_sequnlock(&state->seqlock);
1670 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1671 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1674 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1675 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1676 __must_hold(&state->owner->so_lock)
1677 __must_hold(&state->seqlock)
1685 if (nfs_stateid_is_sequential(state, stateid))
1690 /* Rely on seqids for serialisation with NFSv4.0 */
1691 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1694 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1695 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1697 * Ensure we process the state changes in the same order
1698 * in which the server processed them by delaying the
1699 * update of the stateid until we are in sequence.
1701 write_sequnlock(&state->seqlock);
1702 spin_unlock(&state->owner->so_lock);
1704 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1706 if (!fatal_signal_pending(current)) {
1707 if (schedule_timeout(5*HZ) == 0)
1713 finish_wait(&state->waitq, &wait);
1715 spin_lock(&state->owner->so_lock);
1716 write_seqlock(&state->seqlock);
1719 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1720 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1721 nfs4_stateid_copy(freeme, &state->open_stateid);
1722 nfs_test_and_clear_all_open_stateid(state);
1725 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1726 nfs4_stateid_copy(&state->stateid, stateid);
1727 nfs4_stateid_copy(&state->open_stateid, stateid);
1728 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1729 nfs_state_log_update_open_stateid(state);
1732 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1733 const nfs4_stateid *open_stateid,
1735 nfs4_stateid *freeme)
1738 * Protect the call to nfs4_state_set_mode_locked and
1739 * serialise the stateid update
1741 write_seqlock(&state->seqlock);
1742 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1745 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1748 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1750 case FMODE_READ|FMODE_WRITE:
1751 set_bit(NFS_O_RDWR_STATE, &state->flags);
1753 set_bit(NFS_OPEN_STATE, &state->flags);
1754 write_sequnlock(&state->seqlock);
1757 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1759 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1760 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1761 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1762 clear_bit(NFS_OPEN_STATE, &state->flags);
1765 static void nfs_state_set_delegation(struct nfs4_state *state,
1766 const nfs4_stateid *deleg_stateid,
1770 * Protect the call to nfs4_state_set_mode_locked and
1771 * serialise the stateid update
1773 write_seqlock(&state->seqlock);
1774 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1775 set_bit(NFS_DELEGATED_STATE, &state->flags);
1776 write_sequnlock(&state->seqlock);
1779 static void nfs_state_clear_delegation(struct nfs4_state *state)
1781 write_seqlock(&state->seqlock);
1782 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1783 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1784 write_sequnlock(&state->seqlock);
1787 int update_open_stateid(struct nfs4_state *state,
1788 const nfs4_stateid *open_stateid,
1789 const nfs4_stateid *delegation,
1792 struct nfs_server *server = NFS_SERVER(state->inode);
1793 struct nfs_client *clp = server->nfs_client;
1794 struct nfs_inode *nfsi = NFS_I(state->inode);
1795 struct nfs_delegation *deleg_cur;
1796 nfs4_stateid freeme = { };
1799 fmode &= (FMODE_READ|FMODE_WRITE);
1802 spin_lock(&state->owner->so_lock);
1803 if (open_stateid != NULL) {
1804 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1808 deleg_cur = nfs4_get_valid_delegation(state->inode);
1809 if (deleg_cur == NULL)
1812 spin_lock(&deleg_cur->lock);
1813 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1814 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1815 (deleg_cur->type & fmode) != fmode)
1816 goto no_delegation_unlock;
1818 if (delegation == NULL)
1819 delegation = &deleg_cur->stateid;
1820 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1821 goto no_delegation_unlock;
1823 nfs_mark_delegation_referenced(deleg_cur);
1824 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1826 no_delegation_unlock:
1827 spin_unlock(&deleg_cur->lock);
1830 update_open_stateflags(state, fmode);
1831 spin_unlock(&state->owner->so_lock);
1834 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1835 nfs4_schedule_state_manager(clp);
1836 if (freeme.type != 0)
1837 nfs4_test_and_free_stateid(server, &freeme,
1838 state->owner->so_cred);
1843 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1844 const nfs4_stateid *stateid)
1846 struct nfs4_state *state = lsp->ls_state;
1849 spin_lock(&state->state_lock);
1850 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1852 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1854 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1857 spin_unlock(&state->state_lock);
1861 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1863 struct nfs_delegation *delegation;
1865 fmode &= FMODE_READ|FMODE_WRITE;
1867 delegation = nfs4_get_valid_delegation(inode);
1868 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1873 nfs4_inode_return_delegation(inode);
1876 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1878 struct nfs4_state *state = opendata->state;
1879 struct nfs_delegation *delegation;
1880 int open_mode = opendata->o_arg.open_flags;
1881 fmode_t fmode = opendata->o_arg.fmode;
1882 enum open_claim_type4 claim = opendata->o_arg.claim;
1883 nfs4_stateid stateid;
1887 spin_lock(&state->owner->so_lock);
1888 if (can_open_cached(state, fmode, open_mode, claim)) {
1889 update_open_stateflags(state, fmode);
1890 spin_unlock(&state->owner->so_lock);
1891 goto out_return_state;
1893 spin_unlock(&state->owner->so_lock);
1895 delegation = nfs4_get_valid_delegation(state->inode);
1896 if (!can_open_delegated(delegation, fmode, claim)) {
1900 /* Save the delegation */
1901 nfs4_stateid_copy(&stateid, &delegation->stateid);
1903 nfs_release_seqid(opendata->o_arg.seqid);
1904 if (!opendata->is_recover) {
1905 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1911 /* Try to update the stateid using the delegation */
1912 if (update_open_stateid(state, NULL, &stateid, fmode))
1913 goto out_return_state;
1916 return ERR_PTR(ret);
1918 refcount_inc(&state->count);
1923 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1925 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1926 struct nfs_delegation *delegation;
1927 int delegation_flags = 0;
1930 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1932 delegation_flags = delegation->flags;
1934 switch (data->o_arg.claim) {
1937 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1938 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1939 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1940 "returning a delegation for "
1941 "OPEN(CLAIM_DELEGATE_CUR)\n",
1945 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1946 nfs_inode_set_delegation(state->inode,
1947 data->owner->so_cred,
1948 data->o_res.delegation_type,
1949 &data->o_res.delegation,
1950 data->o_res.pagemod_limit);
1952 nfs_inode_reclaim_delegation(state->inode,
1953 data->owner->so_cred,
1954 data->o_res.delegation_type,
1955 &data->o_res.delegation,
1956 data->o_res.pagemod_limit);
1958 if (data->o_res.do_recall)
1959 nfs_async_inode_return_delegation(state->inode,
1960 &data->o_res.delegation);
1964 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1965 * and update the nfs4_state.
1967 static struct nfs4_state *
1968 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1970 struct inode *inode = data->state->inode;
1971 struct nfs4_state *state = data->state;
1974 if (!data->rpc_done) {
1975 if (data->rpc_status)
1976 return ERR_PTR(data->rpc_status);
1977 /* cached opens have already been processed */
1981 ret = nfs_refresh_inode(inode, &data->f_attr);
1983 return ERR_PTR(ret);
1985 if (data->o_res.delegation_type != 0)
1986 nfs4_opendata_check_deleg(data, state);
1988 if (!update_open_stateid(state, &data->o_res.stateid,
1989 NULL, data->o_arg.fmode))
1990 return ERR_PTR(-EAGAIN);
1991 refcount_inc(&state->count);
1996 static struct inode *
1997 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1999 struct inode *inode;
2001 switch (data->o_arg.claim) {
2002 case NFS4_OPEN_CLAIM_NULL:
2003 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2004 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2005 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2006 return ERR_PTR(-EAGAIN);
2007 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2011 inode = d_inode(data->dentry);
2013 nfs_refresh_inode(inode, &data->f_attr);
2018 static struct nfs4_state *
2019 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2021 struct nfs4_state *state;
2022 struct inode *inode;
2024 inode = nfs4_opendata_get_inode(data);
2026 return ERR_CAST(inode);
2027 if (data->state != NULL && data->state->inode == inode) {
2028 state = data->state;
2029 refcount_inc(&state->count);
2031 state = nfs4_get_open_state(inode, data->owner);
2034 state = ERR_PTR(-ENOMEM);
2038 static struct nfs4_state *
2039 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2041 struct nfs4_state *state;
2043 if (!data->rpc_done) {
2044 state = nfs4_try_open_cached(data);
2045 trace_nfs4_cached_open(data->state);
2049 state = nfs4_opendata_find_nfs4_state(data);
2053 if (data->o_res.delegation_type != 0)
2054 nfs4_opendata_check_deleg(data, state);
2055 if (!update_open_stateid(state, &data->o_res.stateid,
2056 NULL, data->o_arg.fmode)) {
2057 nfs4_put_open_state(state);
2058 state = ERR_PTR(-EAGAIN);
2061 nfs_release_seqid(data->o_arg.seqid);
2065 static struct nfs4_state *
2066 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2068 struct nfs4_state *ret;
2070 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2071 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2073 ret = _nfs4_opendata_to_nfs4_state(data);
2074 nfs4_sequence_free_slot(&data->o_res.seq_res);
2078 static struct nfs_open_context *
2079 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2081 struct nfs_inode *nfsi = NFS_I(state->inode);
2082 struct nfs_open_context *ctx;
2085 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2086 if (ctx->state != state)
2088 if ((ctx->mode & mode) != mode)
2090 if (!get_nfs_open_context(ctx))
2096 return ERR_PTR(-ENOENT);
2099 static struct nfs_open_context *
2100 nfs4_state_find_open_context(struct nfs4_state *state)
2102 struct nfs_open_context *ctx;
2104 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2107 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2110 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2113 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2114 struct nfs4_state *state, enum open_claim_type4 claim)
2116 struct nfs4_opendata *opendata;
2118 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2119 NULL, claim, GFP_NOFS);
2120 if (opendata == NULL)
2121 return ERR_PTR(-ENOMEM);
2122 opendata->state = state;
2123 refcount_inc(&state->count);
2127 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2130 struct nfs4_state *newstate;
2133 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2135 opendata->o_arg.open_flags = 0;
2136 opendata->o_arg.fmode = fmode;
2137 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2138 NFS_SB(opendata->dentry->d_sb),
2140 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2141 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2142 nfs4_init_opendata_res(opendata);
2143 ret = _nfs4_recover_proc_open(opendata);
2146 newstate = nfs4_opendata_to_nfs4_state(opendata);
2147 if (IS_ERR(newstate))
2148 return PTR_ERR(newstate);
2149 if (newstate != opendata->state)
2151 nfs4_close_state(newstate, fmode);
2155 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2159 /* memory barrier prior to reading state->n_* */
2161 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2164 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2167 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2171 * We may have performed cached opens for all three recoveries.
2172 * Check if we need to update the current stateid.
2174 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2175 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2176 write_seqlock(&state->seqlock);
2177 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2178 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2179 write_sequnlock(&state->seqlock);
2186 * reclaim state on the server after a reboot.
2188 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2190 struct nfs_delegation *delegation;
2191 struct nfs4_opendata *opendata;
2192 fmode_t delegation_type = 0;
2195 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2196 NFS4_OPEN_CLAIM_PREVIOUS);
2197 if (IS_ERR(opendata))
2198 return PTR_ERR(opendata);
2200 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2201 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2202 delegation_type = delegation->type;
2204 opendata->o_arg.u.delegation_type = delegation_type;
2205 status = nfs4_open_recover(opendata, state);
2206 nfs4_opendata_put(opendata);
2210 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2212 struct nfs_server *server = NFS_SERVER(state->inode);
2213 struct nfs4_exception exception = { };
2216 err = _nfs4_do_open_reclaim(ctx, state);
2217 trace_nfs4_open_reclaim(ctx, 0, err);
2218 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2220 if (err != -NFS4ERR_DELAY)
2222 nfs4_handle_exception(server, err, &exception);
2223 } while (exception.retry);
2227 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2229 struct nfs_open_context *ctx;
2232 ctx = nfs4_state_find_open_context(state);
2235 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2236 nfs_state_clear_open_state_flags(state);
2237 ret = nfs4_do_open_reclaim(ctx, state);
2238 put_nfs_open_context(ctx);
2242 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)
2246 printk(KERN_ERR "NFS: %s: unhandled error "
2247 "%d.\n", __func__, err);
2255 case -NFS4ERR_BADSESSION:
2256 case -NFS4ERR_BADSLOT:
2257 case -NFS4ERR_BAD_HIGH_SLOT:
2258 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2259 case -NFS4ERR_DEADSESSION:
2261 case -NFS4ERR_STALE_CLIENTID:
2262 case -NFS4ERR_STALE_STATEID:
2263 /* Don't recall a delegation if it was lost */
2264 nfs4_schedule_lease_recovery(server->nfs_client);
2266 case -NFS4ERR_MOVED:
2267 nfs4_schedule_migration_recovery(server);
2269 case -NFS4ERR_LEASE_MOVED:
2270 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2272 case -NFS4ERR_DELEG_REVOKED:
2273 case -NFS4ERR_ADMIN_REVOKED:
2274 case -NFS4ERR_EXPIRED:
2275 case -NFS4ERR_BAD_STATEID:
2276 case -NFS4ERR_OPENMODE:
2277 nfs_inode_find_state_and_recover(state->inode,
2279 nfs4_schedule_stateid_recovery(server, state);
2281 case -NFS4ERR_DELAY:
2282 case -NFS4ERR_GRACE:
2286 case -NFS4ERR_DENIED:
2288 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2290 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2297 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2298 struct nfs4_state *state, const nfs4_stateid *stateid)
2300 struct nfs_server *server = NFS_SERVER(state->inode);
2301 struct nfs4_opendata *opendata;
2304 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2305 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2306 if (IS_ERR(opendata))
2307 return PTR_ERR(opendata);
2308 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2309 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2310 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2314 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2315 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2319 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2320 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2324 nfs_state_clear_delegation(state);
2326 nfs4_opendata_put(opendata);
2327 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2330 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2332 struct nfs4_opendata *data = calldata;
2334 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2335 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2338 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2340 struct nfs4_opendata *data = calldata;
2342 nfs40_sequence_done(task, &data->c_res.seq_res);
2344 data->rpc_status = task->tk_status;
2345 if (data->rpc_status == 0) {
2346 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2347 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2348 renew_lease(data->o_res.server, data->timestamp);
2349 data->rpc_done = true;
2353 static void nfs4_open_confirm_release(void *calldata)
2355 struct nfs4_opendata *data = calldata;
2356 struct nfs4_state *state = NULL;
2358 /* If this request hasn't been cancelled, do nothing */
2359 if (!data->cancelled)
2361 /* In case of error, no cleanup! */
2362 if (!data->rpc_done)
2364 state = nfs4_opendata_to_nfs4_state(data);
2366 nfs4_close_state(state, data->o_arg.fmode);
2368 nfs4_opendata_put(data);
2371 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2372 .rpc_call_prepare = nfs4_open_confirm_prepare,
2373 .rpc_call_done = nfs4_open_confirm_done,
2374 .rpc_release = nfs4_open_confirm_release,
2378 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2380 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2382 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2383 struct rpc_task *task;
2384 struct rpc_message msg = {
2385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2386 .rpc_argp = &data->c_arg,
2387 .rpc_resp = &data->c_res,
2388 .rpc_cred = data->owner->so_cred,
2390 struct rpc_task_setup task_setup_data = {
2391 .rpc_client = server->client,
2392 .rpc_message = &msg,
2393 .callback_ops = &nfs4_open_confirm_ops,
2394 .callback_data = data,
2395 .workqueue = nfsiod_workqueue,
2396 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2400 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2402 kref_get(&data->kref);
2403 data->rpc_done = false;
2404 data->rpc_status = 0;
2405 data->timestamp = jiffies;
2406 task = rpc_run_task(&task_setup_data);
2408 return PTR_ERR(task);
2409 status = rpc_wait_for_completion_task(task);
2411 data->cancelled = true;
2414 status = data->rpc_status;
2419 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2421 struct nfs4_opendata *data = calldata;
2422 struct nfs4_state_owner *sp = data->owner;
2423 struct nfs_client *clp = sp->so_server->nfs_client;
2424 enum open_claim_type4 claim = data->o_arg.claim;
2426 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2429 * Check if we still need to send an OPEN call, or if we can use
2430 * a delegation instead.
2432 if (data->state != NULL) {
2433 struct nfs_delegation *delegation;
2435 if (can_open_cached(data->state, data->o_arg.fmode,
2436 data->o_arg.open_flags, claim))
2439 delegation = nfs4_get_valid_delegation(data->state->inode);
2440 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2441 goto unlock_no_action;
2444 /* Update client id. */
2445 data->o_arg.clientid = clp->cl_clientid;
2449 case NFS4_OPEN_CLAIM_PREVIOUS:
2450 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2451 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2452 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2454 case NFS4_OPEN_CLAIM_FH:
2455 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2457 data->timestamp = jiffies;
2458 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2459 &data->o_arg.seq_args,
2460 &data->o_res.seq_res,
2462 nfs_release_seqid(data->o_arg.seqid);
2464 /* Set the create mode (note dependency on the session type) */
2465 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2466 if (data->o_arg.open_flags & O_EXCL) {
2467 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2468 if (clp->cl_mvops->minor_version == 0) {
2469 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2470 /* don't put an ACCESS op in OPEN compound if O_EXCL,
2471 * because ACCESS will return permission denied for
2472 * all bits until close */
2473 data->o_res.access_request = data->o_arg.access = 0;
2474 } else if (nfs4_has_persistent_session(clp))
2475 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2479 trace_nfs4_cached_open(data->state);
2482 task->tk_action = NULL;
2484 nfs4_sequence_done(task, &data->o_res.seq_res);
2487 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2489 struct nfs4_opendata *data = calldata;
2491 data->rpc_status = task->tk_status;
2493 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2496 if (task->tk_status == 0) {
2497 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2498 switch (data->o_res.f_attr->mode & S_IFMT) {
2502 data->rpc_status = -ELOOP;
2505 data->rpc_status = -EISDIR;
2508 data->rpc_status = -ENOTDIR;
2511 renew_lease(data->o_res.server, data->timestamp);
2512 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2513 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2515 data->rpc_done = true;
2518 static void nfs4_open_release(void *calldata)
2520 struct nfs4_opendata *data = calldata;
2521 struct nfs4_state *state = NULL;
2523 /* If this request hasn't been cancelled, do nothing */
2524 if (!data->cancelled)
2526 /* In case of error, no cleanup! */
2527 if (data->rpc_status != 0 || !data->rpc_done)
2529 /* In case we need an open_confirm, no cleanup! */
2530 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2532 state = nfs4_opendata_to_nfs4_state(data);
2534 nfs4_close_state(state, data->o_arg.fmode);
2536 nfs4_opendata_put(data);
2539 static const struct rpc_call_ops nfs4_open_ops = {
2540 .rpc_call_prepare = nfs4_open_prepare,
2541 .rpc_call_done = nfs4_open_done,
2542 .rpc_release = nfs4_open_release,
2545 static int nfs4_run_open_task(struct nfs4_opendata *data,
2546 struct nfs_open_context *ctx)
2548 struct inode *dir = d_inode(data->dir);
2549 struct nfs_server *server = NFS_SERVER(dir);
2550 struct nfs_openargs *o_arg = &data->o_arg;
2551 struct nfs_openres *o_res = &data->o_res;
2552 struct rpc_task *task;
2553 struct rpc_message msg = {
2554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2557 .rpc_cred = data->owner->so_cred,
2559 struct rpc_task_setup task_setup_data = {
2560 .rpc_client = server->client,
2561 .rpc_message = &msg,
2562 .callback_ops = &nfs4_open_ops,
2563 .callback_data = data,
2564 .workqueue = nfsiod_workqueue,
2565 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2569 if (server->nfs_client->cl_minorversion)
2570 task_setup_data.flags |= RPC_TASK_MOVEABLE;
2572 kref_get(&data->kref);
2573 data->rpc_done = false;
2574 data->rpc_status = 0;
2575 data->cancelled = false;
2576 data->is_recover = false;
2578 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2579 data->is_recover = true;
2580 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2582 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2583 pnfs_lgopen_prepare(data, ctx);
2585 task = rpc_run_task(&task_setup_data);
2587 return PTR_ERR(task);
2588 status = rpc_wait_for_completion_task(task);
2590 data->cancelled = true;
2593 status = data->rpc_status;
2599 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2601 struct inode *dir = d_inode(data->dir);
2602 struct nfs_openres *o_res = &data->o_res;
2605 status = nfs4_run_open_task(data, NULL);
2606 if (status != 0 || !data->rpc_done)
2609 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2611 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2612 status = _nfs4_proc_open_confirm(data);
2618 * Additional permission checks in order to distinguish between an
2619 * open for read, and an open for execute. This works around the
2620 * fact that NFSv4 OPEN treats read and execute permissions as being
2622 * Note that in the non-execute case, we want to turn off permission
2623 * checking if we just created a new file (POSIX open() semantics).
2625 static int nfs4_opendata_access(const struct cred *cred,
2626 struct nfs4_opendata *opendata,
2627 struct nfs4_state *state, fmode_t fmode,
2630 struct nfs_access_entry cache;
2633 /* access call failed or for some reason the server doesn't
2634 * support any access modes -- defer access call until later */
2635 if (opendata->o_res.access_supported == 0)
2640 * Use openflags to check for exec, because fmode won't
2641 * always have FMODE_EXEC set when file open for exec.
2643 if (openflags & __FMODE_EXEC) {
2644 /* ONLY check for exec rights */
2645 if (S_ISDIR(state->inode->i_mode))
2646 mask = NFS4_ACCESS_LOOKUP;
2648 mask = NFS4_ACCESS_EXECUTE;
2649 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2650 mask = NFS4_ACCESS_READ;
2652 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2653 nfs_access_add_cache(state->inode, &cache, cred);
2655 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2656 if ((mask & ~cache.mask & flags) == 0)
2663 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2665 static int _nfs4_proc_open(struct nfs4_opendata *data,
2666 struct nfs_open_context *ctx)
2668 struct inode *dir = d_inode(data->dir);
2669 struct nfs_server *server = NFS_SERVER(dir);
2670 struct nfs_openargs *o_arg = &data->o_arg;
2671 struct nfs_openres *o_res = &data->o_res;
2674 status = nfs4_run_open_task(data, ctx);
2675 if (!data->rpc_done)
2678 if (status == -NFS4ERR_BADNAME &&
2679 !(o_arg->open_flags & O_CREAT))
2684 nfs_fattr_map_and_free_names(server, &data->f_attr);
2686 if (o_arg->open_flags & O_CREAT) {
2687 if (o_arg->open_flags & O_EXCL)
2688 data->file_created = true;
2689 else if (o_res->cinfo.before != o_res->cinfo.after)
2690 data->file_created = true;
2691 if (data->file_created ||
2692 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2693 nfs4_update_changeattr(dir, &o_res->cinfo,
2694 o_res->f_attr->time_start,
2695 NFS_INO_INVALID_DATA);
2697 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2698 server->caps &= ~NFS_CAP_POSIX_LOCK;
2699 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2700 status = _nfs4_proc_open_confirm(data);
2704 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2705 nfs4_sequence_free_slot(&o_res->seq_res);
2706 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, NULL);
2713 * reclaim state on the server after a network partition.
2714 * Assumes caller holds the appropriate lock
2716 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2718 struct nfs4_opendata *opendata;
2721 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2722 NFS4_OPEN_CLAIM_FH);
2723 if (IS_ERR(opendata))
2724 return PTR_ERR(opendata);
2725 ret = nfs4_open_recover(opendata, state);
2727 d_drop(ctx->dentry);
2728 nfs4_opendata_put(opendata);
2732 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2734 struct nfs_server *server = NFS_SERVER(state->inode);
2735 struct nfs4_exception exception = { };
2739 err = _nfs4_open_expired(ctx, state);
2740 trace_nfs4_open_expired(ctx, 0, err);
2741 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2746 case -NFS4ERR_GRACE:
2747 case -NFS4ERR_DELAY:
2748 nfs4_handle_exception(server, err, &exception);
2751 } while (exception.retry);
2756 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2758 struct nfs_open_context *ctx;
2761 ctx = nfs4_state_find_open_context(state);
2764 ret = nfs4_do_open_expired(ctx, state);
2765 put_nfs_open_context(ctx);
2769 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2770 const nfs4_stateid *stateid)
2772 nfs_remove_bad_delegation(state->inode, stateid);
2773 nfs_state_clear_delegation(state);
2776 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2778 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2779 nfs_finish_clear_delegation_stateid(state, NULL);
2782 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2784 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2785 nfs40_clear_delegation_stateid(state);
2786 nfs_state_clear_open_state_flags(state);
2787 return nfs4_open_expired(sp, state);
2790 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2791 nfs4_stateid *stateid,
2792 const struct cred *cred)
2794 return -NFS4ERR_BAD_STATEID;
2797 #if defined(CONFIG_NFS_V4_1)
2798 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2799 nfs4_stateid *stateid,
2800 const struct cred *cred)
2804 switch (stateid->type) {
2807 case NFS4_INVALID_STATEID_TYPE:
2808 case NFS4_SPECIAL_STATEID_TYPE:
2809 return -NFS4ERR_BAD_STATEID;
2810 case NFS4_REVOKED_STATEID_TYPE:
2814 status = nfs41_test_stateid(server, stateid, cred);
2816 case -NFS4ERR_EXPIRED:
2817 case -NFS4ERR_ADMIN_REVOKED:
2818 case -NFS4ERR_DELEG_REVOKED:
2824 /* Ack the revoked state to the server */
2825 nfs41_free_stateid(server, stateid, cred, true);
2826 return -NFS4ERR_EXPIRED;
2829 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2831 struct nfs_server *server = NFS_SERVER(state->inode);
2832 nfs4_stateid stateid;
2833 struct nfs_delegation *delegation;
2834 const struct cred *cred = NULL;
2835 int status, ret = NFS_OK;
2837 /* Get the delegation credential for use by test/free_stateid */
2839 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2840 if (delegation == NULL) {
2842 nfs_state_clear_delegation(state);
2846 spin_lock(&delegation->lock);
2847 nfs4_stateid_copy(&stateid, &delegation->stateid);
2849 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2850 &delegation->flags)) {
2851 spin_unlock(&delegation->lock);
2856 if (delegation->cred)
2857 cred = get_cred(delegation->cred);
2858 spin_unlock(&delegation->lock);
2860 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2861 trace_nfs4_test_delegation_stateid(state, NULL, status);
2862 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2863 nfs_finish_clear_delegation_stateid(state, &stateid);
2871 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2875 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2876 nfs4_copy_delegation_stateid(state->inode, state->state,
2878 nfs4_stateid_match_other(&state->stateid, &tmp))
2879 nfs_state_set_delegation(state, &tmp, state->state);
2881 nfs_state_clear_delegation(state);
2885 * nfs41_check_expired_locks - possibly free a lock stateid
2887 * @state: NFSv4 state for an inode
2889 * Returns NFS_OK if recovery for this stateid is now finished.
2890 * Otherwise a negative NFS4ERR value is returned.
2892 static int nfs41_check_expired_locks(struct nfs4_state *state)
2894 int status, ret = NFS_OK;
2895 struct nfs4_lock_state *lsp, *prev = NULL;
2896 struct nfs_server *server = NFS_SERVER(state->inode);
2898 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2901 spin_lock(&state->state_lock);
2902 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2903 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2904 const struct cred *cred = lsp->ls_state->owner->so_cred;
2906 refcount_inc(&lsp->ls_count);
2907 spin_unlock(&state->state_lock);
2909 nfs4_put_lock_state(prev);
2912 status = nfs41_test_and_free_expired_stateid(server,
2915 trace_nfs4_test_lock_stateid(state, lsp, status);
2916 if (status == -NFS4ERR_EXPIRED ||
2917 status == -NFS4ERR_BAD_STATEID) {
2918 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2919 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2920 if (!recover_lost_locks)
2921 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2922 } else if (status != NFS_OK) {
2924 nfs4_put_lock_state(prev);
2927 spin_lock(&state->state_lock);
2930 spin_unlock(&state->state_lock);
2931 nfs4_put_lock_state(prev);
2937 * nfs41_check_open_stateid - possibly free an open stateid
2939 * @state: NFSv4 state for an inode
2941 * Returns NFS_OK if recovery for this stateid is now finished.
2942 * Otherwise a negative NFS4ERR value is returned.
2944 static int nfs41_check_open_stateid(struct nfs4_state *state)
2946 struct nfs_server *server = NFS_SERVER(state->inode);
2947 nfs4_stateid *stateid = &state->open_stateid;
2948 const struct cred *cred = state->owner->so_cred;
2951 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2952 return -NFS4ERR_BAD_STATEID;
2953 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2954 trace_nfs4_test_open_stateid(state, NULL, status);
2955 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2956 nfs_state_clear_open_state_flags(state);
2957 stateid->type = NFS4_INVALID_STATEID_TYPE;
2960 if (nfs_open_stateid_recover_openmode(state))
2961 return -NFS4ERR_OPENMODE;
2965 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2969 status = nfs41_check_delegation_stateid(state);
2970 if (status != NFS_OK)
2972 nfs41_delegation_recover_stateid(state);
2974 status = nfs41_check_expired_locks(state);
2975 if (status != NFS_OK)
2977 status = nfs41_check_open_stateid(state);
2978 if (status != NFS_OK)
2979 status = nfs4_open_expired(sp, state);
2985 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2986 * fields corresponding to attributes that were used to store the verifier.
2987 * Make sure we clobber those fields in the later setattr call
2989 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2990 struct iattr *sattr, struct nfs4_label **label)
2992 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2997 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2998 attrset[i] = opendata->o_res.attrset[i];
2999 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3000 attrset[i] &= ~bitmask[i];
3003 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3004 sattr->ia_valid : 0;
3006 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3007 if (sattr->ia_valid & ATTR_ATIME_SET)
3008 ret |= ATTR_ATIME_SET;
3013 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3014 if (sattr->ia_valid & ATTR_MTIME_SET)
3015 ret |= ATTR_MTIME_SET;
3020 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3025 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3026 int flags, struct nfs_open_context *ctx)
3028 struct nfs4_state_owner *sp = opendata->owner;
3029 struct nfs_server *server = sp->so_server;
3030 struct dentry *dentry;
3031 struct nfs4_state *state;
3032 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3033 struct inode *dir = d_inode(opendata->dir);
3034 unsigned long dir_verifier;
3038 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3039 dir_verifier = nfs_save_change_attribute(dir);
3041 ret = _nfs4_proc_open(opendata, ctx);
3045 state = _nfs4_opendata_to_nfs4_state(opendata);
3046 ret = PTR_ERR(state);
3050 if (server->caps & NFS_CAP_POSIX_LOCK)
3051 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3052 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3053 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3055 dentry = opendata->dentry;
3056 if (d_really_is_negative(dentry)) {
3057 struct dentry *alias;
3059 alias = d_exact_alias(dentry, state->inode);
3061 alias = d_splice_alias(igrab(state->inode), dentry);
3062 /* d_splice_alias() can't fail here - it's a non-directory */
3065 ctx->dentry = dentry = alias;
3069 switch(opendata->o_arg.claim) {
3072 case NFS4_OPEN_CLAIM_NULL:
3073 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3074 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3075 if (!opendata->rpc_done)
3077 if (opendata->o_res.delegation_type != 0)
3078 dir_verifier = nfs_save_change_attribute(dir);
3079 nfs_set_verifier(dentry, dir_verifier);
3082 /* Parse layoutget results before we check for access */
3083 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3085 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3090 if (d_inode(dentry) == state->inode) {
3091 nfs_inode_attach_open_context(ctx);
3092 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3093 nfs4_schedule_stateid_recovery(server, state);
3097 if (!opendata->cancelled)
3098 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3103 * Returns a referenced nfs4_state
3105 static int _nfs4_do_open(struct inode *dir,
3106 struct nfs_open_context *ctx,
3108 const struct nfs4_open_createattrs *c,
3111 struct nfs4_state_owner *sp;
3112 struct nfs4_state *state = NULL;
3113 struct nfs_server *server = NFS_SERVER(dir);
3114 struct nfs4_opendata *opendata;
3115 struct dentry *dentry = ctx->dentry;
3116 const struct cred *cred = ctx->cred;
3117 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3118 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3119 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3120 struct iattr *sattr = c->sattr;
3121 struct nfs4_label *label = c->label;
3124 /* Protect against reboot recovery conflicts */
3126 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3128 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3131 status = nfs4_client_recover_expired_lease(server->nfs_client);
3133 goto err_put_state_owner;
3134 if (d_really_is_positive(dentry))
3135 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3137 if (d_really_is_positive(dentry))
3138 claim = NFS4_OPEN_CLAIM_FH;
3139 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3140 c, claim, GFP_KERNEL);
3141 if (opendata == NULL)
3142 goto err_put_state_owner;
3144 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3145 if (!opendata->f_attr.mdsthreshold) {
3146 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3147 if (!opendata->f_attr.mdsthreshold)
3148 goto err_opendata_put;
3150 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3152 if (d_really_is_positive(dentry))
3153 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3155 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3157 goto err_opendata_put;
3160 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3161 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3162 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3164 * send create attributes which was not set by open
3165 * with an extra setattr.
3167 if (attrs || label) {
3168 unsigned ia_old = sattr->ia_valid;
3170 sattr->ia_valid = attrs;
3171 nfs_fattr_init(opendata->o_res.f_attr);
3172 status = nfs4_do_setattr(state->inode, cred,
3173 opendata->o_res.f_attr, sattr,
3176 nfs_setattr_update_inode(state->inode, sattr,
3177 opendata->o_res.f_attr);
3178 nfs_setsecurity(state->inode, opendata->o_res.f_attr);
3180 sattr->ia_valid = ia_old;
3183 if (opened && opendata->file_created)
3186 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3187 *ctx_th = opendata->f_attr.mdsthreshold;
3188 opendata->f_attr.mdsthreshold = NULL;
3191 nfs4_opendata_put(opendata);
3192 nfs4_put_state_owner(sp);
3195 nfs4_opendata_put(opendata);
3196 err_put_state_owner:
3197 nfs4_put_state_owner(sp);
3203 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3204 struct nfs_open_context *ctx,
3206 struct iattr *sattr,
3207 struct nfs4_label *label,
3210 struct nfs_server *server = NFS_SERVER(dir);
3211 struct nfs4_exception exception = {
3212 .interruptible = true,
3214 struct nfs4_state *res;
3215 struct nfs4_open_createattrs c = {
3219 [0] = (__u32)jiffies,
3220 [1] = (__u32)current->pid,
3226 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3228 trace_nfs4_open_file(ctx, flags, status);
3231 /* NOTE: BAD_SEQID means the server and client disagree about the
3232 * book-keeping w.r.t. state-changing operations
3233 * (OPEN/CLOSE/LOCK/LOCKU...)
3234 * It is actually a sign of a bug on the client or on the server.
3236 * If we receive a BAD_SEQID error in the particular case of
3237 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3238 * have unhashed the old state_owner for us, and that we can
3239 * therefore safely retry using a new one. We should still warn
3240 * the user though...
3242 if (status == -NFS4ERR_BAD_SEQID) {
3243 pr_warn_ratelimited("NFS: v4 server %s "
3244 " returned a bad sequence-id error!\n",
3245 NFS_SERVER(dir)->nfs_client->cl_hostname);
3246 exception.retry = 1;
3250 * BAD_STATEID on OPEN means that the server cancelled our
3251 * state before it received the OPEN_CONFIRM.
3252 * Recover by retrying the request as per the discussion
3253 * on Page 181 of RFC3530.
3255 if (status == -NFS4ERR_BAD_STATEID) {
3256 exception.retry = 1;
3259 if (status == -NFS4ERR_EXPIRED) {
3260 nfs4_schedule_lease_recovery(server->nfs_client);
3261 exception.retry = 1;
3264 if (status == -EAGAIN) {
3265 /* We must have found a delegation */
3266 exception.retry = 1;
3269 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3271 res = ERR_PTR(nfs4_handle_exception(server,
3272 status, &exception));
3273 } while (exception.retry);
3277 static int _nfs4_do_setattr(struct inode *inode,
3278 struct nfs_setattrargs *arg,
3279 struct nfs_setattrres *res,
3280 const struct cred *cred,
3281 struct nfs_open_context *ctx)
3283 struct nfs_server *server = NFS_SERVER(inode);
3284 struct rpc_message msg = {
3285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3290 const struct cred *delegation_cred = NULL;
3291 unsigned long timestamp = jiffies;
3295 nfs_fattr_init(res->fattr);
3297 /* Servers should only apply open mode checks for file size changes */
3298 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3300 nfs4_inode_make_writeable(inode);
3304 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3305 /* Use that stateid */
3306 } else if (ctx != NULL && ctx->state) {
3307 struct nfs_lock_context *l_ctx;
3308 if (!nfs4_valid_open_stateid(ctx->state))
3310 l_ctx = nfs_get_lock_context(ctx);
3312 return PTR_ERR(l_ctx);
3313 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3314 &arg->stateid, &delegation_cred);
3315 nfs_put_lock_context(l_ctx);
3318 else if (status == -EAGAIN)
3322 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3324 if (delegation_cred)
3325 msg.rpc_cred = delegation_cred;
3327 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3329 put_cred(delegation_cred);
3330 if (status == 0 && ctx != NULL)
3331 renew_lease(server, timestamp);
3332 trace_nfs4_setattr(inode, &arg->stateid, status);
3336 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3337 struct nfs_fattr *fattr, struct iattr *sattr,
3338 struct nfs_open_context *ctx, struct nfs4_label *ilabel)
3340 struct nfs_server *server = NFS_SERVER(inode);
3341 __u32 bitmask[NFS4_BITMASK_SZ];
3342 struct nfs4_state *state = ctx ? ctx->state : NULL;
3343 struct nfs_setattrargs arg = {
3344 .fh = NFS_FH(inode),
3350 struct nfs_setattrres res = {
3354 struct nfs4_exception exception = {
3357 .stateid = &arg.stateid,
3359 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
3362 if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3363 adjust_flags |= NFS_INO_INVALID_MODE;
3364 if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
3365 adjust_flags |= NFS_INO_INVALID_OTHER;
3368 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label),
3369 inode, adjust_flags);
3371 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3373 case -NFS4ERR_OPENMODE:
3374 if (!(sattr->ia_valid & ATTR_SIZE)) {
3375 pr_warn_once("NFSv4: server %s is incorrectly "
3376 "applying open mode checks to "
3377 "a SETATTR that is not "
3378 "changing file size.\n",
3379 server->nfs_client->cl_hostname);
3381 if (state && !(state->state & FMODE_WRITE)) {
3383 if (sattr->ia_valid & ATTR_OPEN)
3388 err = nfs4_handle_exception(server, err, &exception);
3389 } while (exception.retry);
3395 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3397 if (inode == NULL || !nfs_have_layout(inode))
3400 return pnfs_wait_on_layoutreturn(inode, task);
3404 * Update the seqid of an open stateid
3406 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3407 struct nfs4_state *state)
3414 if (!nfs4_valid_open_stateid(state))
3416 seq = read_seqbegin(&state->seqlock);
3417 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3418 nfs4_stateid_copy(dst, &state->open_stateid);
3419 if (read_seqretry(&state->seqlock, seq))
3423 seqid_open = state->open_stateid.seqid;
3424 if (read_seqretry(&state->seqlock, seq))
3427 dst_seqid = be32_to_cpu(dst->seqid);
3428 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3429 dst->seqid = seqid_open;
3435 * Update the seqid of an open stateid after receiving
3436 * NFS4ERR_OLD_STATEID
3438 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3439 struct nfs4_state *state)
3444 int seq, status = -EAGAIN;
3449 if (!nfs4_valid_open_stateid(state))
3451 seq = read_seqbegin(&state->seqlock);
3452 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3453 if (read_seqretry(&state->seqlock, seq))
3458 write_seqlock(&state->seqlock);
3459 seqid_open = state->open_stateid.seqid;
3461 dst_seqid = be32_to_cpu(dst->seqid);
3463 /* Did another OPEN bump the state's seqid? try again: */
3464 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3465 dst->seqid = seqid_open;
3466 write_sequnlock(&state->seqlock);
3471 /* server says we're behind but we haven't seen the update yet */
3472 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3473 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3474 write_sequnlock(&state->seqlock);
3475 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3477 if (fatal_signal_pending(current))
3480 if (schedule_timeout(5*HZ) != 0)
3483 finish_wait(&state->waitq, &wait);
3487 if (status == -EINTR)
3490 /* we slept the whole 5 seconds, we must have lost a seqid */
3491 dst->seqid = cpu_to_be32(dst_seqid + 1);
3499 struct nfs4_closedata {
3500 struct inode *inode;
3501 struct nfs4_state *state;
3502 struct nfs_closeargs arg;
3503 struct nfs_closeres res;
3505 struct nfs4_layoutreturn_args arg;
3506 struct nfs4_layoutreturn_res res;
3507 struct nfs4_xdr_opaque_data ld_private;
3511 struct nfs_fattr fattr;
3512 unsigned long timestamp;
3515 static void nfs4_free_closedata(void *data)
3517 struct nfs4_closedata *calldata = data;
3518 struct nfs4_state_owner *sp = calldata->state->owner;
3519 struct super_block *sb = calldata->state->inode->i_sb;
3521 if (calldata->lr.roc)
3522 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3523 calldata->res.lr_ret);
3524 nfs4_put_open_state(calldata->state);
3525 nfs_free_seqid(calldata->arg.seqid);
3526 nfs4_put_state_owner(sp);
3527 nfs_sb_deactive(sb);
3531 static void nfs4_close_done(struct rpc_task *task, void *data)
3533 struct nfs4_closedata *calldata = data;
3534 struct nfs4_state *state = calldata->state;
3535 struct nfs_server *server = NFS_SERVER(calldata->inode);
3536 nfs4_stateid *res_stateid = NULL;
3537 struct nfs4_exception exception = {
3539 .inode = calldata->inode,
3540 .stateid = &calldata->arg.stateid,
3543 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3545 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3547 /* Handle Layoutreturn errors */
3548 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3549 &calldata->res.lr_ret) == -EAGAIN)
3552 /* hmm. we are done with the inode, and in the process of freeing
3553 * the state_owner. we keep this around to process errors
3555 switch (task->tk_status) {
3557 res_stateid = &calldata->res.stateid;
3558 renew_lease(server, calldata->timestamp);
3560 case -NFS4ERR_ACCESS:
3561 if (calldata->arg.bitmask != NULL) {
3562 calldata->arg.bitmask = NULL;
3563 calldata->res.fattr = NULL;
3568 case -NFS4ERR_OLD_STATEID:
3569 /* Did we race with OPEN? */
3570 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3574 case -NFS4ERR_ADMIN_REVOKED:
3575 case -NFS4ERR_STALE_STATEID:
3576 case -NFS4ERR_EXPIRED:
3577 nfs4_free_revoked_stateid(server,
3578 &calldata->arg.stateid,
3579 task->tk_msg.rpc_cred);
3581 case -NFS4ERR_BAD_STATEID:
3582 if (calldata->arg.fmode == 0)
3586 task->tk_status = nfs4_async_handle_exception(task,
3587 server, task->tk_status, &exception);
3588 if (exception.retry)
3591 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3592 res_stateid, calldata->arg.fmode);
3594 task->tk_status = 0;
3595 nfs_release_seqid(calldata->arg.seqid);
3596 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3597 dprintk("%s: ret = %d\n", __func__, task->tk_status);
3600 task->tk_status = 0;
3601 rpc_restart_call_prepare(task);
3605 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3607 struct nfs4_closedata *calldata = data;
3608 struct nfs4_state *state = calldata->state;
3609 struct inode *inode = calldata->inode;
3610 struct nfs_server *server = NFS_SERVER(inode);
3611 struct pnfs_layout_hdr *lo;
3612 bool is_rdonly, is_wronly, is_rdwr;
3615 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3618 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3619 spin_lock(&state->owner->so_lock);
3620 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3621 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3622 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3623 /* Calculate the change in open mode */
3624 calldata->arg.fmode = 0;
3625 if (state->n_rdwr == 0) {
3626 if (state->n_rdonly == 0)
3627 call_close |= is_rdonly;
3629 calldata->arg.fmode |= FMODE_READ;
3630 if (state->n_wronly == 0)
3631 call_close |= is_wronly;
3633 calldata->arg.fmode |= FMODE_WRITE;
3634 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3635 call_close |= is_rdwr;
3637 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3639 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3640 if (!nfs4_valid_open_stateid(state))
3642 spin_unlock(&state->owner->so_lock);
3645 /* Note: exit _without_ calling nfs4_close_done */
3649 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3650 nfs_release_seqid(calldata->arg.seqid);
3654 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3655 if (lo && !pnfs_layout_is_valid(lo)) {
3656 calldata->arg.lr_args = NULL;
3657 calldata->res.lr_res = NULL;
3660 if (calldata->arg.fmode == 0)
3661 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3663 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3664 /* Close-to-open cache consistency revalidation */
3665 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3666 nfs4_bitmask_set(calldata->arg.bitmask_store,
3667 server->cache_consistency_bitmask,
3669 calldata->arg.bitmask = calldata->arg.bitmask_store;
3671 calldata->arg.bitmask = NULL;
3674 calldata->arg.share_access =
3675 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3676 calldata->arg.fmode, 0);
3678 if (calldata->res.fattr == NULL)
3679 calldata->arg.bitmask = NULL;
3680 else if (calldata->arg.bitmask == NULL)
3681 calldata->res.fattr = NULL;
3682 calldata->timestamp = jiffies;
3683 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3684 &calldata->arg.seq_args,
3685 &calldata->res.seq_res,
3687 nfs_release_seqid(calldata->arg.seqid);
3690 task->tk_action = NULL;
3692 nfs4_sequence_done(task, &calldata->res.seq_res);
3695 static const struct rpc_call_ops nfs4_close_ops = {
3696 .rpc_call_prepare = nfs4_close_prepare,
3697 .rpc_call_done = nfs4_close_done,
3698 .rpc_release = nfs4_free_closedata,
3702 * It is possible for data to be read/written from a mem-mapped file
3703 * after the sys_close call (which hits the vfs layer as a flush).
3704 * This means that we can't safely call nfsv4 close on a file until
3705 * the inode is cleared. This in turn means that we are not good
3706 * NFSv4 citizens - we do not indicate to the server to update the file's
3707 * share state even when we are done with one of the three share
3708 * stateid's in the inode.
3710 * NOTE: Caller must be holding the sp->so_owner semaphore!
3712 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3714 struct nfs_server *server = NFS_SERVER(state->inode);
3715 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3716 struct nfs4_closedata *calldata;
3717 struct nfs4_state_owner *sp = state->owner;
3718 struct rpc_task *task;
3719 struct rpc_message msg = {
3720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3721 .rpc_cred = state->owner->so_cred,
3723 struct rpc_task_setup task_setup_data = {
3724 .rpc_client = server->client,
3725 .rpc_message = &msg,
3726 .callback_ops = &nfs4_close_ops,
3727 .workqueue = nfsiod_workqueue,
3728 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3730 int status = -ENOMEM;
3732 if (server->nfs_client->cl_minorversion)
3733 task_setup_data.flags |= RPC_TASK_MOVEABLE;
3735 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3736 &task_setup_data.rpc_client, &msg);
3738 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3739 if (calldata == NULL)
3741 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3742 calldata->inode = state->inode;
3743 calldata->state = state;
3744 calldata->arg.fh = NFS_FH(state->inode);
3745 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3746 goto out_free_calldata;
3747 /* Serialization for the sequence id */
3748 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3749 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3750 if (IS_ERR(calldata->arg.seqid))
3751 goto out_free_calldata;
3752 nfs_fattr_init(&calldata->fattr);
3753 calldata->arg.fmode = 0;
3754 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3755 calldata->res.fattr = &calldata->fattr;
3756 calldata->res.seqid = calldata->arg.seqid;
3757 calldata->res.server = server;
3758 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3759 calldata->lr.roc = pnfs_roc(state->inode,
3760 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3761 if (calldata->lr.roc) {
3762 calldata->arg.lr_args = &calldata->lr.arg;
3763 calldata->res.lr_res = &calldata->lr.res;
3765 nfs_sb_active(calldata->inode->i_sb);
3767 msg.rpc_argp = &calldata->arg;
3768 msg.rpc_resp = &calldata->res;
3769 task_setup_data.callback_data = calldata;
3770 task = rpc_run_task(&task_setup_data);
3772 return PTR_ERR(task);
3775 status = rpc_wait_for_completion_task(task);
3781 nfs4_put_open_state(state);
3782 nfs4_put_state_owner(sp);
3786 static struct inode *
3787 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3788 int open_flags, struct iattr *attr, int *opened)
3790 struct nfs4_state *state;
3791 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3793 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3795 /* Protect against concurrent sillydeletes */
3796 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3798 nfs4_label_release_security(label);
3801 return ERR_CAST(state);
3802 return state->inode;
3805 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3807 if (ctx->state == NULL)
3810 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3812 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3815 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3816 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3817 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3819 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3821 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3822 struct nfs4_server_caps_arg args = {
3826 struct nfs4_server_caps_res res = {};
3827 struct rpc_message msg = {
3828 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3835 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3836 FATTR4_WORD0_FH_EXPIRE_TYPE |
3837 FATTR4_WORD0_LINK_SUPPORT |
3838 FATTR4_WORD0_SYMLINK_SUPPORT |
3839 FATTR4_WORD0_ACLSUPPORT |
3840 FATTR4_WORD0_CASE_INSENSITIVE |
3841 FATTR4_WORD0_CASE_PRESERVING;
3843 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3845 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3847 /* Sanity check the server answers */
3848 switch (minorversion) {
3850 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3851 res.attr_bitmask[2] = 0;
3854 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3857 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3859 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3860 server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3861 NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3862 server->fattr_valid = NFS_ATTR_FATTR_V4;
3863 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3864 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3865 server->caps |= NFS_CAP_ACLS;
3866 if (res.has_links != 0)
3867 server->caps |= NFS_CAP_HARDLINKS;
3868 if (res.has_symlinks != 0)
3869 server->caps |= NFS_CAP_SYMLINKS;
3870 if (res.case_insensitive)
3871 server->caps |= NFS_CAP_CASE_INSENSITIVE;
3872 if (res.case_preserving)
3873 server->caps |= NFS_CAP_CASE_PRESERVING;
3874 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3875 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3876 server->caps |= NFS_CAP_SECURITY_LABEL;
3878 if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS)
3879 server->caps |= NFS_CAP_FS_LOCATIONS;
3880 if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3881 server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3882 if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3883 server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3884 if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3885 server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3886 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3887 server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3888 NFS_ATTR_FATTR_OWNER_NAME);
3889 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3890 server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3891 NFS_ATTR_FATTR_GROUP_NAME);
3892 if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3893 server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3894 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3895 server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3896 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3897 server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3898 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3899 server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
3900 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3901 sizeof(server->attr_bitmask));
3902 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3904 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3905 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3906 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3907 server->cache_consistency_bitmask[2] = 0;
3909 /* Avoid a regression due to buggy server */
3910 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3911 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3912 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3913 sizeof(server->exclcreat_bitmask));
3915 server->acl_bitmask = res.acl_bitmask;
3916 server->fh_expire_type = res.fh_expire_type;
3922 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3924 struct nfs4_exception exception = {
3925 .interruptible = true,
3929 nfs4_server_set_init_caps(server);
3931 err = nfs4_handle_exception(server,
3932 _nfs4_server_capabilities(server, fhandle),
3934 } while (exception.retry);
3938 static void test_fs_location_for_trunking(struct nfs4_fs_location *location,
3939 struct nfs_client *clp,
3940 struct nfs_server *server)
3944 for (i = 0; i < location->nservers; i++) {
3945 struct nfs4_string *srv_loc = &location->servers[i];
3946 struct sockaddr addr;
3948 struct xprt_create xprt_args = {
3952 struct nfs4_add_xprt_data xprtdata = {
3955 struct rpc_add_xprt_test rpcdata = {
3956 .add_xprt_test = clp->cl_mvops->session_trunk,
3959 char *servername = NULL;
3964 addrlen = nfs_parse_server_name(srv_loc->data, srv_loc->len,
3965 &addr, sizeof(addr),
3966 clp->cl_net, server->port);
3969 xprt_args.dstaddr = &addr;
3970 xprt_args.addrlen = addrlen;
3971 servername = kmalloc(srv_loc->len + 1, GFP_KERNEL);
3974 memcpy(servername, srv_loc->data, srv_loc->len);
3975 servername[srv_loc->len] = '\0';
3976 xprt_args.servername = servername;
3978 xprtdata.cred = nfs4_get_clid_cred(clp);
3979 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
3980 rpc_clnt_setup_test_and_add_xprt,
3983 put_cred(xprtdata.cred);
3988 static int _nfs4_discover_trunking(struct nfs_server *server,
3989 struct nfs_fh *fhandle)
3991 struct nfs4_fs_locations *locations = NULL;
3993 const struct cred *cred;
3994 struct nfs_client *clp = server->nfs_client;
3995 const struct nfs4_state_maintenance_ops *ops =
3996 clp->cl_mvops->state_renewal_ops;
3997 int status = -ENOMEM, i;
3999 cred = ops->get_state_renewal_cred(clp);
4001 cred = nfs4_get_clid_cred(clp);
4006 page = alloc_page(GFP_KERNEL);
4007 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4008 if (page == NULL || locations == NULL)
4011 status = nfs4_proc_get_locations(server, fhandle, locations, page,
4016 for (i = 0; i < locations->nlocations; i++)
4017 test_fs_location_for_trunking(&locations->locations[i], clp,
4026 static int nfs4_discover_trunking(struct nfs_server *server,
4027 struct nfs_fh *fhandle)
4029 struct nfs4_exception exception = {
4030 .interruptible = true,
4032 struct nfs_client *clp = server->nfs_client;
4035 if (!nfs4_has_session(clp))
4038 err = nfs4_handle_exception(server,
4039 _nfs4_discover_trunking(server, fhandle),
4041 } while (exception.retry);
4046 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4047 struct nfs_fsinfo *info)
4050 struct nfs4_lookup_root_arg args = {
4053 struct nfs4_lookup_res res = {
4055 .fattr = info->fattr,
4058 struct rpc_message msg = {
4059 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
4064 bitmask[0] = nfs4_fattr_bitmap[0];
4065 bitmask[1] = nfs4_fattr_bitmap[1];
4067 * Process the label in the upcoming getfattr
4069 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
4071 nfs_fattr_init(info->fattr);
4072 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4075 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4076 struct nfs_fsinfo *info)
4078 struct nfs4_exception exception = {
4079 .interruptible = true,
4083 err = _nfs4_lookup_root(server, fhandle, info);
4084 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
4087 case -NFS4ERR_WRONGSEC:
4090 err = nfs4_handle_exception(server, err, &exception);
4092 } while (exception.retry);
4097 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4098 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4100 struct rpc_auth_create_args auth_args = {
4101 .pseudoflavor = flavor,
4103 struct rpc_auth *auth;
4105 auth = rpcauth_create(&auth_args, server->client);
4108 return nfs4_lookup_root(server, fhandle, info);
4112 * Retry pseudoroot lookup with various security flavors. We do this when:
4114 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4115 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4117 * Returns zero on success, or a negative NFS4ERR value, or a
4118 * negative errno value.
4120 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4121 struct nfs_fsinfo *info)
4123 /* Per 3530bis 15.33.5 */
4124 static const rpc_authflavor_t flav_array[] = {
4128 RPC_AUTH_UNIX, /* courtesy */
4131 int status = -EPERM;
4134 if (server->auth_info.flavor_len > 0) {
4135 /* try each flavor specified by user */
4136 for (i = 0; i < server->auth_info.flavor_len; i++) {
4137 status = nfs4_lookup_root_sec(server, fhandle, info,
4138 server->auth_info.flavors[i]);
4139 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4144 /* no flavors specified by user, try default list */
4145 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4146 status = nfs4_lookup_root_sec(server, fhandle, info,
4148 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4155 * -EACCES could mean that the user doesn't have correct permissions
4156 * to access the mount. It could also mean that we tried to mount
4157 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4158 * existing mount programs don't handle -EACCES very well so it should
4159 * be mapped to -EPERM instead.
4161 if (status == -EACCES)
4167 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4168 * @server: initialized nfs_server handle
4169 * @fhandle: we fill in the pseudo-fs root file handle
4170 * @info: we fill in an FSINFO struct
4171 * @auth_probe: probe the auth flavours
4173 * Returns zero on success, or a negative errno.
4175 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4176 struct nfs_fsinfo *info,
4182 status = nfs4_lookup_root(server, fhandle, info);
4184 if (auth_probe || status == NFS4ERR_WRONGSEC)
4185 status = server->nfs_client->cl_mvops->find_root_sec(server,
4189 status = nfs4_server_capabilities(server, fhandle);
4191 status = nfs4_do_fsinfo(server, fhandle, info);
4193 return nfs4_map_errors(status);
4196 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4197 struct nfs_fsinfo *info)
4200 struct nfs_fattr *fattr = info->fattr;
4202 error = nfs4_server_capabilities(server, mntfh);
4204 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4208 error = nfs4_proc_getattr(server, mntfh, fattr, NULL);
4210 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4214 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4215 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4216 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4223 * Get locations and (maybe) other attributes of a referral.
4224 * Note that we'll actually follow the referral later when
4225 * we detect fsid mismatch in inode revalidation
4227 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4228 const struct qstr *name, struct nfs_fattr *fattr,
4229 struct nfs_fh *fhandle)
4231 int status = -ENOMEM;
4232 struct page *page = NULL;
4233 struct nfs4_fs_locations *locations = NULL;
4235 page = alloc_page(GFP_KERNEL);
4238 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4239 if (locations == NULL)
4242 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4247 * If the fsid didn't change, this is a migration event, not a
4248 * referral. Cause us to drop into the exception handler, which
4249 * will kick off migration recovery.
4251 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4252 dprintk("%s: server did not return a different fsid for"
4253 " a referral at %s\n", __func__, name->name);
4254 status = -NFS4ERR_MOVED;
4257 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4258 nfs_fixup_referral_attributes(&locations->fattr);
4260 /* replace the lookup nfs_fattr with the locations nfs_fattr */
4261 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4262 memset(fhandle, 0, sizeof(struct nfs_fh));
4270 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4271 struct nfs_fattr *fattr, struct inode *inode)
4273 __u32 bitmask[NFS4_BITMASK_SZ];
4274 struct nfs4_getattr_arg args = {
4278 struct nfs4_getattr_res res = {
4282 struct rpc_message msg = {
4283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4287 unsigned short task_flags = 0;
4289 if (nfs4_has_session(server->nfs_client))
4290 task_flags = RPC_TASK_MOVEABLE;
4292 /* Is this is an attribute revalidation, subject to softreval? */
4293 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4294 task_flags |= RPC_TASK_TIMEOUT;
4296 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0);
4297 nfs_fattr_init(fattr);
4298 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4299 return nfs4_do_call_sync(server->client, server, &msg,
4300 &args.seq_args, &res.seq_res, task_flags);
4303 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4304 struct nfs_fattr *fattr, struct inode *inode)
4306 struct nfs4_exception exception = {
4307 .interruptible = true,
4311 err = _nfs4_proc_getattr(server, fhandle, fattr, inode);
4312 trace_nfs4_getattr(server, fhandle, fattr, err);
4313 err = nfs4_handle_exception(server, err,
4315 } while (exception.retry);
4320 * The file is not closed if it is opened due to the a request to change
4321 * the size of the file. The open call will not be needed once the
4322 * VFS layer lookup-intents are implemented.
4324 * Close is called when the inode is destroyed.
4325 * If we haven't opened the file for O_WRONLY, we
4326 * need to in the size_change case to obtain a stateid.
4329 * Because OPEN is always done by name in nfsv4, it is
4330 * possible that we opened a different file by the same
4331 * name. We can recognize this race condition, but we
4332 * can't do anything about it besides returning an error.
4334 * This will be fixed with VFS changes (lookup-intent).
4337 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4338 struct iattr *sattr)
4340 struct inode *inode = d_inode(dentry);
4341 const struct cred *cred = NULL;
4342 struct nfs_open_context *ctx = NULL;
4345 if (pnfs_ld_layoutret_on_setattr(inode) &&
4346 sattr->ia_valid & ATTR_SIZE &&
4347 sattr->ia_size < i_size_read(inode))
4348 pnfs_commit_and_return_layout(inode);
4350 nfs_fattr_init(fattr);
4352 /* Deal with open(O_TRUNC) */
4353 if (sattr->ia_valid & ATTR_OPEN)
4354 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4356 /* Optimization: if the end result is no change, don't RPC */
4357 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4360 /* Search for an existing open(O_WRITE) file */
4361 if (sattr->ia_valid & ATTR_FILE) {
4363 ctx = nfs_file_open_context(sattr->ia_file);
4368 /* Return any delegations if we're going to change ACLs */
4369 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4370 nfs4_inode_make_writeable(inode);
4372 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL);
4374 nfs_setattr_update_inode(inode, sattr, fattr);
4375 nfs_setsecurity(inode, fattr);
4380 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4381 struct dentry *dentry, struct nfs_fh *fhandle,
4382 struct nfs_fattr *fattr)
4384 struct nfs_server *server = NFS_SERVER(dir);
4386 struct nfs4_lookup_arg args = {
4387 .bitmask = server->attr_bitmask,
4388 .dir_fh = NFS_FH(dir),
4389 .name = &dentry->d_name,
4391 struct nfs4_lookup_res res = {
4396 struct rpc_message msg = {
4397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4401 unsigned short task_flags = 0;
4403 if (server->nfs_client->cl_minorversion)
4404 task_flags = RPC_TASK_MOVEABLE;
4406 /* Is this is an attribute revalidation, subject to softreval? */
4407 if (nfs_lookup_is_soft_revalidate(dentry))
4408 task_flags |= RPC_TASK_TIMEOUT;
4410 args.bitmask = nfs4_bitmask(server, fattr->label);
4412 nfs_fattr_init(fattr);
4414 dprintk("NFS call lookup %pd2\n", dentry);
4415 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4416 status = nfs4_do_call_sync(clnt, server, &msg,
4417 &args.seq_args, &res.seq_res, task_flags);
4418 dprintk("NFS reply lookup: %d\n", status);
4422 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4424 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4425 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4426 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4430 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4431 struct dentry *dentry, struct nfs_fh *fhandle,
4432 struct nfs_fattr *fattr)
4434 struct nfs4_exception exception = {
4435 .interruptible = true,
4437 struct rpc_clnt *client = *clnt;
4438 const struct qstr *name = &dentry->d_name;
4441 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr);
4442 trace_nfs4_lookup(dir, name, err);
4444 case -NFS4ERR_BADNAME:
4447 case -NFS4ERR_MOVED:
4448 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4449 if (err == -NFS4ERR_MOVED)
4450 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4452 case -NFS4ERR_WRONGSEC:
4454 if (client != *clnt)
4456 client = nfs4_negotiate_security(client, dir, name);
4458 return PTR_ERR(client);
4460 exception.retry = 1;
4463 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4465 } while (exception.retry);
4470 else if (client != *clnt)
4471 rpc_shutdown_client(client);
4476 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4477 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4480 struct rpc_clnt *client = NFS_CLIENT(dir);
4482 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4483 if (client != NFS_CLIENT(dir)) {
4484 rpc_shutdown_client(client);
4485 nfs_fixup_secinfo_attributes(fattr);
4491 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4492 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4494 struct rpc_clnt *client = NFS_CLIENT(dir);
4497 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4499 return ERR_PTR(status);
4500 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4503 static int _nfs4_proc_lookupp(struct inode *inode,
4504 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4506 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4507 struct nfs_server *server = NFS_SERVER(inode);
4509 struct nfs4_lookupp_arg args = {
4510 .bitmask = server->attr_bitmask,
4511 .fh = NFS_FH(inode),
4513 struct nfs4_lookupp_res res = {
4518 struct rpc_message msg = {
4519 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4523 unsigned short task_flags = 0;
4525 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4526 task_flags |= RPC_TASK_TIMEOUT;
4528 args.bitmask = nfs4_bitmask(server, fattr->label);
4530 nfs_fattr_init(fattr);
4532 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4533 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4534 &res.seq_res, task_flags);
4535 dprintk("NFS reply lookupp: %d\n", status);
4539 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4540 struct nfs_fattr *fattr)
4542 struct nfs4_exception exception = {
4543 .interruptible = true,
4547 err = _nfs4_proc_lookupp(inode, fhandle, fattr);
4548 trace_nfs4_lookupp(inode, err);
4549 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4551 } while (exception.retry);
4555 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4556 const struct cred *cred)
4558 struct nfs_server *server = NFS_SERVER(inode);
4559 struct nfs4_accessargs args = {
4560 .fh = NFS_FH(inode),
4561 .access = entry->mask,
4563 struct nfs4_accessres res = {
4566 struct rpc_message msg = {
4567 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4574 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4575 res.fattr = nfs_alloc_fattr();
4576 if (res.fattr == NULL)
4578 args.bitmask = server->cache_consistency_bitmask;
4580 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4582 nfs_access_set_mask(entry, res.access);
4584 nfs_refresh_inode(inode, res.fattr);
4586 nfs_free_fattr(res.fattr);
4590 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4591 const struct cred *cred)
4593 struct nfs4_exception exception = {
4594 .interruptible = true,
4598 err = _nfs4_proc_access(inode, entry, cred);
4599 trace_nfs4_access(inode, err);
4600 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4602 } while (exception.retry);
4607 * TODO: For the time being, we don't try to get any attributes
4608 * along with any of the zero-copy operations READ, READDIR,
4611 * In the case of the first three, we want to put the GETATTR
4612 * after the read-type operation -- this is because it is hard
4613 * to predict the length of a GETATTR response in v4, and thus
4614 * align the READ data correctly. This means that the GETATTR
4615 * may end up partially falling into the page cache, and we should
4616 * shift it into the 'tail' of the xdr_buf before processing.
4617 * To do this efficiently, we need to know the total length
4618 * of data received, which doesn't seem to be available outside
4621 * In the case of WRITE, we also want to put the GETATTR after
4622 * the operation -- in this case because we want to make sure
4623 * we get the post-operation mtime and size.
4625 * Both of these changes to the XDR layer would in fact be quite
4626 * minor, but I decided to leave them for a subsequent patch.
4628 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4629 unsigned int pgbase, unsigned int pglen)
4631 struct nfs4_readlink args = {
4632 .fh = NFS_FH(inode),
4637 struct nfs4_readlink_res res;
4638 struct rpc_message msg = {
4639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4644 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4647 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4648 unsigned int pgbase, unsigned int pglen)
4650 struct nfs4_exception exception = {
4651 .interruptible = true,
4655 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4656 trace_nfs4_readlink(inode, err);
4657 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4659 } while (exception.retry);
4664 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4667 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4670 struct nfs_server *server = NFS_SERVER(dir);
4671 struct nfs4_label l, *ilabel = NULL;
4672 struct nfs_open_context *ctx;
4673 struct nfs4_state *state;
4676 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4678 return PTR_ERR(ctx);
4680 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4682 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4683 sattr->ia_mode &= ~current_umask();
4684 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4685 if (IS_ERR(state)) {
4686 status = PTR_ERR(state);
4690 nfs4_label_release_security(ilabel);
4691 put_nfs_open_context(ctx);
4696 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4698 struct nfs_server *server = NFS_SERVER(dir);
4699 struct nfs_removeargs args = {
4703 struct nfs_removeres res = {
4706 struct rpc_message msg = {
4707 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4711 unsigned long timestamp = jiffies;
4714 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4716 spin_lock(&dir->i_lock);
4717 /* Removing a directory decrements nlink in the parent */
4718 if (ftype == NF4DIR && dir->i_nlink > 2)
4719 nfs4_dec_nlink_locked(dir);
4720 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4721 NFS_INO_INVALID_DATA);
4722 spin_unlock(&dir->i_lock);
4727 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4729 struct nfs4_exception exception = {
4730 .interruptible = true,
4732 struct inode *inode = d_inode(dentry);
4736 if (inode->i_nlink == 1)
4737 nfs4_inode_return_delegation(inode);
4739 nfs4_inode_make_writeable(inode);
4742 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4743 trace_nfs4_remove(dir, &dentry->d_name, err);
4744 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4746 } while (exception.retry);
4750 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4752 struct nfs4_exception exception = {
4753 .interruptible = true,
4758 err = _nfs4_proc_remove(dir, name, NF4DIR);
4759 trace_nfs4_remove(dir, name, err);
4760 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4762 } while (exception.retry);
4766 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4767 struct dentry *dentry,
4768 struct inode *inode)
4770 struct nfs_removeargs *args = msg->rpc_argp;
4771 struct nfs_removeres *res = msg->rpc_resp;
4773 res->server = NFS_SB(dentry->d_sb);
4774 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4775 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4777 nfs_fattr_init(res->dir_attr);
4780 nfs4_inode_return_delegation(inode);
4781 nfs_d_prune_case_insensitive_aliases(inode);
4785 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4787 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4788 &data->args.seq_args,
4793 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4795 struct nfs_unlinkdata *data = task->tk_calldata;
4796 struct nfs_removeres *res = &data->res;
4798 if (!nfs4_sequence_done(task, &res->seq_res))
4800 if (nfs4_async_handle_error(task, res->server, NULL,
4801 &data->timeout) == -EAGAIN)
4803 if (task->tk_status == 0)
4804 nfs4_update_changeattr(dir, &res->cinfo,
4805 res->dir_attr->time_start,
4806 NFS_INO_INVALID_DATA);
4810 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4811 struct dentry *old_dentry,
4812 struct dentry *new_dentry)
4814 struct nfs_renameargs *arg = msg->rpc_argp;
4815 struct nfs_renameres *res = msg->rpc_resp;
4816 struct inode *old_inode = d_inode(old_dentry);
4817 struct inode *new_inode = d_inode(new_dentry);
4820 nfs4_inode_make_writeable(old_inode);
4822 nfs4_inode_return_delegation(new_inode);
4823 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4824 res->server = NFS_SB(old_dentry->d_sb);
4825 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4828 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4830 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4831 &data->args.seq_args,
4836 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4837 struct inode *new_dir)
4839 struct nfs_renamedata *data = task->tk_calldata;
4840 struct nfs_renameres *res = &data->res;
4842 if (!nfs4_sequence_done(task, &res->seq_res))
4844 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4847 if (task->tk_status == 0) {
4848 nfs_d_prune_case_insensitive_aliases(d_inode(data->old_dentry));
4849 if (new_dir != old_dir) {
4850 /* Note: If we moved a directory, nlink will change */
4851 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4852 res->old_fattr->time_start,
4853 NFS_INO_INVALID_NLINK |
4854 NFS_INO_INVALID_DATA);
4855 nfs4_update_changeattr(new_dir, &res->new_cinfo,
4856 res->new_fattr->time_start,
4857 NFS_INO_INVALID_NLINK |
4858 NFS_INO_INVALID_DATA);
4860 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4861 res->old_fattr->time_start,
4862 NFS_INO_INVALID_DATA);
4867 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4869 struct nfs_server *server = NFS_SERVER(inode);
4870 __u32 bitmask[NFS4_BITMASK_SZ];
4871 struct nfs4_link_arg arg = {
4872 .fh = NFS_FH(inode),
4873 .dir_fh = NFS_FH(dir),
4877 struct nfs4_link_res res = {
4880 struct rpc_message msg = {
4881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4885 int status = -ENOMEM;
4887 res.fattr = nfs_alloc_fattr_with_label(server);
4888 if (res.fattr == NULL)
4891 nfs4_inode_make_writeable(inode);
4892 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label), inode,
4893 NFS_INO_INVALID_CHANGE);
4894 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4896 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4897 NFS_INO_INVALID_DATA);
4898 nfs4_inc_nlink(inode);
4899 status = nfs_post_op_update_inode(inode, res.fattr);
4901 nfs_setsecurity(inode, res.fattr);
4905 nfs_free_fattr(res.fattr);
4909 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4911 struct nfs4_exception exception = {
4912 .interruptible = true,
4916 err = nfs4_handle_exception(NFS_SERVER(inode),
4917 _nfs4_proc_link(inode, dir, name),
4919 } while (exception.retry);
4923 struct nfs4_createdata {
4924 struct rpc_message msg;
4925 struct nfs4_create_arg arg;
4926 struct nfs4_create_res res;
4928 struct nfs_fattr fattr;
4931 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4932 const struct qstr *name, struct iattr *sattr, u32 ftype)
4934 struct nfs4_createdata *data;
4936 data = kzalloc(sizeof(*data), GFP_KERNEL);
4938 struct nfs_server *server = NFS_SERVER(dir);
4940 data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL);
4941 if (IS_ERR(data->fattr.label))
4944 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4945 data->msg.rpc_argp = &data->arg;
4946 data->msg.rpc_resp = &data->res;
4947 data->arg.dir_fh = NFS_FH(dir);
4948 data->arg.server = server;
4949 data->arg.name = name;
4950 data->arg.attrs = sattr;
4951 data->arg.ftype = ftype;
4952 data->arg.bitmask = nfs4_bitmask(server, data->fattr.label);
4953 data->arg.umask = current_umask();
4954 data->res.server = server;
4955 data->res.fh = &data->fh;
4956 data->res.fattr = &data->fattr;
4957 nfs_fattr_init(data->res.fattr);
4965 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4967 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4968 &data->arg.seq_args, &data->res.seq_res, 1);
4970 spin_lock(&dir->i_lock);
4971 /* Creating a directory bumps nlink in the parent */
4972 if (data->arg.ftype == NF4DIR)
4973 nfs4_inc_nlink_locked(dir);
4974 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4975 data->res.fattr->time_start,
4976 NFS_INO_INVALID_DATA);
4977 spin_unlock(&dir->i_lock);
4978 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
4983 static void nfs4_free_createdata(struct nfs4_createdata *data)
4985 nfs4_label_free(data->fattr.label);
4989 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4990 struct page *page, unsigned int len, struct iattr *sattr,
4991 struct nfs4_label *label)
4993 struct nfs4_createdata *data;
4994 int status = -ENAMETOOLONG;
4996 if (len > NFS4_MAXPATHLEN)
5000 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
5004 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
5005 data->arg.u.symlink.pages = &page;
5006 data->arg.u.symlink.len = len;
5007 data->arg.label = label;
5009 status = nfs4_do_create(dir, dentry, data);
5011 nfs4_free_createdata(data);
5016 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5017 struct page *page, unsigned int len, struct iattr *sattr)
5019 struct nfs4_exception exception = {
5020 .interruptible = true,
5022 struct nfs4_label l, *label = NULL;
5025 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5028 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
5029 trace_nfs4_symlink(dir, &dentry->d_name, err);
5030 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5032 } while (exception.retry);
5034 nfs4_label_release_security(label);
5038 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5039 struct iattr *sattr, struct nfs4_label *label)
5041 struct nfs4_createdata *data;
5042 int status = -ENOMEM;
5044 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
5048 data->arg.label = label;
5049 status = nfs4_do_create(dir, dentry, data);
5051 nfs4_free_createdata(data);
5056 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5057 struct iattr *sattr)
5059 struct nfs_server *server = NFS_SERVER(dir);
5060 struct nfs4_exception exception = {
5061 .interruptible = true,
5063 struct nfs4_label l, *label = NULL;
5066 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5068 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5069 sattr->ia_mode &= ~current_umask();
5071 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
5072 trace_nfs4_mkdir(dir, &dentry->d_name, err);
5073 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5075 } while (exception.retry);
5076 nfs4_label_release_security(label);
5081 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5082 struct nfs_readdir_res *nr_res)
5084 struct inode *dir = d_inode(nr_arg->dentry);
5085 struct nfs_server *server = NFS_SERVER(dir);
5086 struct nfs4_readdir_arg args = {
5088 .pages = nr_arg->pages,
5090 .count = nr_arg->page_len,
5091 .plus = nr_arg->plus,
5093 struct nfs4_readdir_res res;
5094 struct rpc_message msg = {
5095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5098 .rpc_cred = nr_arg->cred,
5102 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5103 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5104 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5105 args.bitmask = server->attr_bitmask_nl;
5107 args.bitmask = server->attr_bitmask;
5109 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5110 res.pgbase = args.pgbase;
5111 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5114 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5115 status += args.pgbase;
5118 nfs_invalidate_atime(dir);
5120 dprintk("%s: returns %d\n", __func__, status);
5124 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5125 struct nfs_readdir_res *res)
5127 struct nfs4_exception exception = {
5128 .interruptible = true,
5132 err = _nfs4_proc_readdir(arg, res);
5133 trace_nfs4_readdir(d_inode(arg->dentry), err);
5134 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5136 } while (exception.retry);
5140 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5141 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5143 struct nfs4_createdata *data;
5144 int mode = sattr->ia_mode;
5145 int status = -ENOMEM;
5147 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5152 data->arg.ftype = NF4FIFO;
5153 else if (S_ISBLK(mode)) {
5154 data->arg.ftype = NF4BLK;
5155 data->arg.u.device.specdata1 = MAJOR(rdev);
5156 data->arg.u.device.specdata2 = MINOR(rdev);
5158 else if (S_ISCHR(mode)) {
5159 data->arg.ftype = NF4CHR;
5160 data->arg.u.device.specdata1 = MAJOR(rdev);
5161 data->arg.u.device.specdata2 = MINOR(rdev);
5162 } else if (!S_ISSOCK(mode)) {
5167 data->arg.label = label;
5168 status = nfs4_do_create(dir, dentry, data);
5170 nfs4_free_createdata(data);
5175 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5176 struct iattr *sattr, dev_t rdev)
5178 struct nfs_server *server = NFS_SERVER(dir);
5179 struct nfs4_exception exception = {
5180 .interruptible = true,
5182 struct nfs4_label l, *label = NULL;
5185 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5187 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5188 sattr->ia_mode &= ~current_umask();
5190 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5191 trace_nfs4_mknod(dir, &dentry->d_name, err);
5192 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5194 } while (exception.retry);
5196 nfs4_label_release_security(label);
5201 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5202 struct nfs_fsstat *fsstat)
5204 struct nfs4_statfs_arg args = {
5206 .bitmask = server->attr_bitmask,
5208 struct nfs4_statfs_res res = {
5211 struct rpc_message msg = {
5212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5217 nfs_fattr_init(fsstat->fattr);
5218 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5221 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5223 struct nfs4_exception exception = {
5224 .interruptible = true,
5228 err = nfs4_handle_exception(server,
5229 _nfs4_proc_statfs(server, fhandle, fsstat),
5231 } while (exception.retry);
5235 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5236 struct nfs_fsinfo *fsinfo)
5238 struct nfs4_fsinfo_arg args = {
5240 .bitmask = server->attr_bitmask,
5242 struct nfs4_fsinfo_res res = {
5245 struct rpc_message msg = {
5246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5251 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5254 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5256 struct nfs4_exception exception = {
5257 .interruptible = true,
5262 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5263 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5265 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5268 err = nfs4_handle_exception(server, err, &exception);
5269 } while (exception.retry);
5273 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5277 nfs_fattr_init(fsinfo->fattr);
5278 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5280 /* block layout checks this! */
5281 server->pnfs_blksize = fsinfo->blksize;
5282 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5288 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5289 struct nfs_pathconf *pathconf)
5291 struct nfs4_pathconf_arg args = {
5293 .bitmask = server->attr_bitmask,
5295 struct nfs4_pathconf_res res = {
5296 .pathconf = pathconf,
5298 struct rpc_message msg = {
5299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5304 /* None of the pathconf attributes are mandatory to implement */
5305 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5306 memset(pathconf, 0, sizeof(*pathconf));
5310 nfs_fattr_init(pathconf->fattr);
5311 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5314 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5315 struct nfs_pathconf *pathconf)
5317 struct nfs4_exception exception = {
5318 .interruptible = true,
5323 err = nfs4_handle_exception(server,
5324 _nfs4_proc_pathconf(server, fhandle, pathconf),
5326 } while (exception.retry);
5330 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5331 const struct nfs_open_context *ctx,
5332 const struct nfs_lock_context *l_ctx,
5335 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5337 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5339 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5340 const struct nfs_open_context *ctx,
5341 const struct nfs_lock_context *l_ctx,
5344 nfs4_stateid _current_stateid;
5346 /* If the current stateid represents a lost lock, then exit */
5347 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5349 return nfs4_stateid_match(stateid, &_current_stateid);
5352 static bool nfs4_error_stateid_expired(int err)
5355 case -NFS4ERR_DELEG_REVOKED:
5356 case -NFS4ERR_ADMIN_REVOKED:
5357 case -NFS4ERR_BAD_STATEID:
5358 case -NFS4ERR_STALE_STATEID:
5359 case -NFS4ERR_OLD_STATEID:
5360 case -NFS4ERR_OPENMODE:
5361 case -NFS4ERR_EXPIRED:
5367 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5369 struct nfs_server *server = NFS_SERVER(hdr->inode);
5371 trace_nfs4_read(hdr, task->tk_status);
5372 if (task->tk_status < 0) {
5373 struct nfs4_exception exception = {
5374 .inode = hdr->inode,
5375 .state = hdr->args.context->state,
5376 .stateid = &hdr->args.stateid,
5378 task->tk_status = nfs4_async_handle_exception(task,
5379 server, task->tk_status, &exception);
5380 if (exception.retry) {
5381 rpc_restart_call_prepare(task);
5386 if (task->tk_status > 0)
5387 renew_lease(server, hdr->timestamp);
5391 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5392 struct nfs_pgio_args *args)
5395 if (!nfs4_error_stateid_expired(task->tk_status) ||
5396 nfs4_stateid_is_current(&args->stateid,
5401 rpc_restart_call_prepare(task);
5405 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5406 struct nfs_pgio_header *hdr)
5408 struct nfs_server *server = NFS_SERVER(hdr->inode);
5409 struct rpc_message *msg = &task->tk_msg;
5411 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5412 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5413 server->caps &= ~NFS_CAP_READ_PLUS;
5414 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5415 rpc_restart_call_prepare(task);
5421 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5423 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5425 if (nfs4_read_stateid_changed(task, &hdr->args))
5427 if (nfs4_read_plus_not_supported(task, hdr))
5429 if (task->tk_status > 0)
5430 nfs_invalidate_atime(hdr->inode);
5431 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5432 nfs4_read_done_cb(task, hdr);
5435 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5436 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5437 struct rpc_message *msg)
5439 /* Note: We don't use READ_PLUS with pNFS yet */
5440 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
5441 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5444 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5445 struct rpc_message *msg)
5448 #endif /* CONFIG_NFS_V4_2 */
5450 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5451 struct rpc_message *msg)
5453 hdr->timestamp = jiffies;
5454 if (!hdr->pgio_done_cb)
5455 hdr->pgio_done_cb = nfs4_read_done_cb;
5456 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5457 nfs42_read_plus_support(hdr, msg);
5458 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5461 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5462 struct nfs_pgio_header *hdr)
5464 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5465 &hdr->args.seq_args,
5469 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5470 hdr->args.lock_context,
5471 hdr->rw_mode) == -EIO)
5473 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5478 static int nfs4_write_done_cb(struct rpc_task *task,
5479 struct nfs_pgio_header *hdr)
5481 struct inode *inode = hdr->inode;
5483 trace_nfs4_write(hdr, task->tk_status);
5484 if (task->tk_status < 0) {
5485 struct nfs4_exception exception = {
5486 .inode = hdr->inode,
5487 .state = hdr->args.context->state,
5488 .stateid = &hdr->args.stateid,
5490 task->tk_status = nfs4_async_handle_exception(task,
5491 NFS_SERVER(inode), task->tk_status,
5493 if (exception.retry) {
5494 rpc_restart_call_prepare(task);
5498 if (task->tk_status >= 0) {
5499 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5500 nfs_writeback_update_inode(hdr);
5505 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5506 struct nfs_pgio_args *args)
5509 if (!nfs4_error_stateid_expired(task->tk_status) ||
5510 nfs4_stateid_is_current(&args->stateid,
5515 rpc_restart_call_prepare(task);
5519 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5521 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5523 if (nfs4_write_stateid_changed(task, &hdr->args))
5525 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5526 nfs4_write_done_cb(task, hdr);
5530 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5532 /* Don't request attributes for pNFS or O_DIRECT writes */
5533 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5535 /* Otherwise, request attributes if and only if we don't hold
5538 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5541 void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[],
5542 struct inode *inode, unsigned long cache_validity)
5544 struct nfs_server *server = NFS_SERVER(inode);
5547 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5548 cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity);
5550 if (cache_validity & NFS_INO_INVALID_CHANGE)
5551 bitmask[0] |= FATTR4_WORD0_CHANGE;
5552 if (cache_validity & NFS_INO_INVALID_ATIME)
5553 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5554 if (cache_validity & NFS_INO_INVALID_MODE)
5555 bitmask[1] |= FATTR4_WORD1_MODE;
5556 if (cache_validity & NFS_INO_INVALID_OTHER)
5557 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5558 if (cache_validity & NFS_INO_INVALID_NLINK)
5559 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5560 if (cache_validity & NFS_INO_INVALID_CTIME)
5561 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5562 if (cache_validity & NFS_INO_INVALID_MTIME)
5563 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5564 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5565 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5567 if (cache_validity & NFS_INO_INVALID_SIZE)
5568 bitmask[0] |= FATTR4_WORD0_SIZE;
5570 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5571 bitmask[i] &= server->attr_bitmask[i];
5574 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5575 struct rpc_message *msg,
5576 struct rpc_clnt **clnt)
5578 struct nfs_server *server = NFS_SERVER(hdr->inode);
5580 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5581 hdr->args.bitmask = NULL;
5582 hdr->res.fattr = NULL;
5584 nfs4_bitmask_set(hdr->args.bitmask_store,
5585 server->cache_consistency_bitmask,
5586 hdr->inode, NFS_INO_INVALID_BLOCKS);
5587 hdr->args.bitmask = hdr->args.bitmask_store;
5590 if (!hdr->pgio_done_cb)
5591 hdr->pgio_done_cb = nfs4_write_done_cb;
5592 hdr->res.server = server;
5593 hdr->timestamp = jiffies;
5595 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5596 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5597 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5600 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5602 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5603 &data->args.seq_args,
5608 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5610 struct inode *inode = data->inode;
5612 trace_nfs4_commit(data, task->tk_status);
5613 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5614 NULL, NULL) == -EAGAIN) {
5615 rpc_restart_call_prepare(task);
5621 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5623 if (!nfs4_sequence_done(task, &data->res.seq_res))
5625 return data->commit_done_cb(task, data);
5628 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5629 struct rpc_clnt **clnt)
5631 struct nfs_server *server = NFS_SERVER(data->inode);
5633 if (data->commit_done_cb == NULL)
5634 data->commit_done_cb = nfs4_commit_done_cb;
5635 data->res.server = server;
5636 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5637 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5638 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5641 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5642 struct nfs_commitres *res)
5644 struct inode *dst_inode = file_inode(dst);
5645 struct nfs_server *server = NFS_SERVER(dst_inode);
5646 struct rpc_message msg = {
5647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5652 args->fh = NFS_FH(dst_inode);
5653 return nfs4_call_sync(server->client, server, &msg,
5654 &args->seq_args, &res->seq_res, 1);
5657 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5659 struct nfs_commitargs args = {
5663 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5664 struct nfs4_exception exception = { };
5668 status = _nfs4_proc_commit(dst, &args, res);
5669 status = nfs4_handle_exception(dst_server, status, &exception);
5670 } while (exception.retry);
5675 struct nfs4_renewdata {
5676 struct nfs_client *client;
5677 unsigned long timestamp;
5681 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5682 * standalone procedure for queueing an asynchronous RENEW.
5684 static void nfs4_renew_release(void *calldata)
5686 struct nfs4_renewdata *data = calldata;
5687 struct nfs_client *clp = data->client;
5689 if (refcount_read(&clp->cl_count) > 1)
5690 nfs4_schedule_state_renewal(clp);
5691 nfs_put_client(clp);
5695 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5697 struct nfs4_renewdata *data = calldata;
5698 struct nfs_client *clp = data->client;
5699 unsigned long timestamp = data->timestamp;
5701 trace_nfs4_renew_async(clp, task->tk_status);
5702 switch (task->tk_status) {
5705 case -NFS4ERR_LEASE_MOVED:
5706 nfs4_schedule_lease_moved_recovery(clp);
5709 /* Unless we're shutting down, schedule state recovery! */
5710 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5712 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5713 nfs4_schedule_lease_recovery(clp);
5716 nfs4_schedule_path_down_recovery(clp);
5718 do_renew_lease(clp, timestamp);
5721 static const struct rpc_call_ops nfs4_renew_ops = {
5722 .rpc_call_done = nfs4_renew_done,
5723 .rpc_release = nfs4_renew_release,
5726 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5728 struct rpc_message msg = {
5729 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5733 struct nfs4_renewdata *data;
5735 if (renew_flags == 0)
5737 if (!refcount_inc_not_zero(&clp->cl_count))
5739 data = kmalloc(sizeof(*data), GFP_NOFS);
5741 nfs_put_client(clp);
5745 data->timestamp = jiffies;
5746 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5747 &nfs4_renew_ops, data);
5750 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5752 struct rpc_message msg = {
5753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5757 unsigned long now = jiffies;
5760 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5763 do_renew_lease(clp, now);
5767 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5769 return server->caps & NFS_CAP_ACLS;
5772 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5773 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5776 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5778 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5779 struct page **pages)
5781 struct page *newpage, **spages;
5787 len = min_t(size_t, PAGE_SIZE, buflen);
5788 newpage = alloc_page(GFP_KERNEL);
5790 if (newpage == NULL)
5792 memcpy(page_address(newpage), buf, len);
5797 } while (buflen != 0);
5803 __free_page(spages[rc-1]);
5807 struct nfs4_cached_acl {
5813 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5815 struct nfs_inode *nfsi = NFS_I(inode);
5817 spin_lock(&inode->i_lock);
5818 kfree(nfsi->nfs4_acl);
5819 nfsi->nfs4_acl = acl;
5820 spin_unlock(&inode->i_lock);
5823 static void nfs4_zap_acl_attr(struct inode *inode)
5825 nfs4_set_cached_acl(inode, NULL);
5828 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5830 struct nfs_inode *nfsi = NFS_I(inode);
5831 struct nfs4_cached_acl *acl;
5834 spin_lock(&inode->i_lock);
5835 acl = nfsi->nfs4_acl;
5838 if (buf == NULL) /* user is just asking for length */
5840 if (acl->cached == 0)
5842 ret = -ERANGE; /* see getxattr(2) man page */
5843 if (acl->len > buflen)
5845 memcpy(buf, acl->data, acl->len);
5849 spin_unlock(&inode->i_lock);
5853 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5855 struct nfs4_cached_acl *acl;
5856 size_t buflen = sizeof(*acl) + acl_len;
5858 if (buflen <= PAGE_SIZE) {
5859 acl = kmalloc(buflen, GFP_KERNEL);
5863 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5865 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5872 nfs4_set_cached_acl(inode, acl);
5876 * The getxattr API returns the required buffer length when called with a
5877 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5878 * the required buf. On a NULL buf, we send a page of data to the server
5879 * guessing that the ACL request can be serviced by a page. If so, we cache
5880 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5881 * the cache. If not so, we throw away the page, and cache the required
5882 * length. The next getxattr call will then produce another round trip to
5883 * the server, this time with the input buf of the required size.
5885 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5887 struct page **pages;
5888 struct nfs_getaclargs args = {
5889 .fh = NFS_FH(inode),
5892 struct nfs_getaclres res = {
5895 struct rpc_message msg = {
5896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5900 unsigned int npages;
5901 int ret = -ENOMEM, i;
5902 struct nfs_server *server = NFS_SERVER(inode);
5905 buflen = server->rsize;
5907 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5908 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5912 args.acl_pages = pages;
5914 for (i = 0; i < npages; i++) {
5915 pages[i] = alloc_page(GFP_KERNEL);
5920 /* for decoding across pages */
5921 res.acl_scratch = alloc_page(GFP_KERNEL);
5922 if (!res.acl_scratch)
5925 args.acl_len = npages * PAGE_SIZE;
5927 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5928 __func__, buf, buflen, npages, args.acl_len);
5929 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5930 &msg, &args.seq_args, &res.seq_res, 0);
5934 /* Handle the case where the passed-in buffer is too short */
5935 if (res.acl_flags & NFS4_ACL_TRUNC) {
5936 /* Did the user only issue a request for the acl length? */
5942 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5944 if (res.acl_len > buflen) {
5948 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5953 for (i = 0; i < npages; i++)
5955 __free_page(pages[i]);
5956 if (res.acl_scratch)
5957 __free_page(res.acl_scratch);
5962 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5964 struct nfs4_exception exception = {
5965 .interruptible = true,
5969 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5970 trace_nfs4_get_acl(inode, ret);
5973 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5974 } while (exception.retry);
5978 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5980 struct nfs_server *server = NFS_SERVER(inode);
5983 if (!nfs4_server_supports_acls(server))
5985 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
5988 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5989 nfs_zap_acl_cache(inode);
5990 ret = nfs4_read_cached_acl(inode, buf, buflen);
5992 /* -ENOENT is returned if there is no ACL or if there is an ACL
5993 * but no cached acl data, just the acl length */
5995 return nfs4_get_acl_uncached(inode, buf, buflen);
5998 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
6000 struct nfs_server *server = NFS_SERVER(inode);
6001 struct page *pages[NFS4ACL_MAXPAGES];
6002 struct nfs_setaclargs arg = {
6003 .fh = NFS_FH(inode),
6007 struct nfs_setaclres res;
6008 struct rpc_message msg = {
6009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
6013 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
6016 /* You can't remove system.nfs4_acl: */
6019 if (!nfs4_server_supports_acls(server))
6021 if (npages > ARRAY_SIZE(pages))
6023 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
6026 nfs4_inode_make_writeable(inode);
6027 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6030 * Free each page after tx, so the only ref left is
6031 * held by the network stack
6034 put_page(pages[i-1]);
6037 * Acl update can result in inode attribute update.
6038 * so mark the attribute cache invalid.
6040 spin_lock(&inode->i_lock);
6041 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
6042 NFS_INO_INVALID_CTIME |
6043 NFS_INO_REVAL_FORCED);
6044 spin_unlock(&inode->i_lock);
6045 nfs_access_zap_cache(inode);
6046 nfs_zap_acl_cache(inode);
6050 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
6052 struct nfs4_exception exception = { };
6055 err = __nfs4_proc_set_acl(inode, buf, buflen);
6056 trace_nfs4_set_acl(inode, err);
6057 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
6059 * no need to retry since the kernel
6060 * isn't involved in encoding the ACEs.
6065 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6067 } while (exception.retry);
6071 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6072 static int _nfs4_get_security_label(struct inode *inode, void *buf,
6075 struct nfs_server *server = NFS_SERVER(inode);
6076 struct nfs4_label label = {0, 0, buflen, buf};
6078 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6079 struct nfs_fattr fattr = {
6082 struct nfs4_getattr_arg arg = {
6083 .fh = NFS_FH(inode),
6086 struct nfs4_getattr_res res = {
6090 struct rpc_message msg = {
6091 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6097 nfs_fattr_init(&fattr);
6099 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6102 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6107 static int nfs4_get_security_label(struct inode *inode, void *buf,
6110 struct nfs4_exception exception = {
6111 .interruptible = true,
6115 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6119 err = _nfs4_get_security_label(inode, buf, buflen);
6120 trace_nfs4_get_security_label(inode, err);
6121 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6123 } while (exception.retry);
6127 static int _nfs4_do_set_security_label(struct inode *inode,
6128 struct nfs4_label *ilabel,
6129 struct nfs_fattr *fattr)
6132 struct iattr sattr = {0};
6133 struct nfs_server *server = NFS_SERVER(inode);
6134 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6135 struct nfs_setattrargs arg = {
6136 .fh = NFS_FH(inode),
6142 struct nfs_setattrres res = {
6146 struct rpc_message msg = {
6147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6153 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6155 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6157 dprintk("%s failed: %d\n", __func__, status);
6162 static int nfs4_do_set_security_label(struct inode *inode,
6163 struct nfs4_label *ilabel,
6164 struct nfs_fattr *fattr)
6166 struct nfs4_exception exception = { };
6170 err = _nfs4_do_set_security_label(inode, ilabel, fattr);
6171 trace_nfs4_set_security_label(inode, err);
6172 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6174 } while (exception.retry);
6179 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6181 struct nfs4_label ilabel = {0, 0, buflen, (char *)buf };
6182 struct nfs_fattr *fattr;
6185 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6188 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
6192 status = nfs4_do_set_security_label(inode, &ilabel, fattr);
6194 nfs_setsecurity(inode, fattr);
6198 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6201 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6202 nfs4_verifier *bootverf)
6206 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6207 /* An impossible timestamp guarantees this value
6208 * will never match a generated boot time. */
6209 verf[0] = cpu_to_be32(U32_MAX);
6210 verf[1] = cpu_to_be32(U32_MAX);
6212 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6213 u64 ns = ktime_to_ns(nn->boot_time);
6215 verf[0] = cpu_to_be32(ns >> 32);
6216 verf[1] = cpu_to_be32(ns);
6218 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6222 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6224 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6225 struct nfs_netns_client *nn_clp = nn->nfs_client;
6232 id = rcu_dereference(nn_clp->identifier);
6234 strscpy(buf, id, buflen);
6238 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6239 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6245 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6247 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6252 if (clp->cl_owner_id != NULL)
6257 strlen(clp->cl_rpcclient->cl_nodename) +
6259 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6263 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6267 if (len > NFS4_OPAQUE_LIMIT + 1)
6271 * Since this string is allocated at mount time, and held until the
6272 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6273 * about a memory-reclaim deadlock.
6275 str = kmalloc(len, GFP_KERNEL);
6281 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6282 clp->cl_rpcclient->cl_nodename, buf,
6283 rpc_peeraddr2str(clp->cl_rpcclient,
6286 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6287 clp->cl_rpcclient->cl_nodename,
6288 rpc_peeraddr2str(clp->cl_rpcclient,
6292 clp->cl_owner_id = str;
6297 nfs4_init_uniform_client_string(struct nfs_client *clp)
6299 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6304 if (clp->cl_owner_id != NULL)
6307 len = 10 + 10 + 1 + 10 + 1 +
6308 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6310 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6314 if (len > NFS4_OPAQUE_LIMIT + 1)
6318 * Since this string is allocated at mount time, and held until the
6319 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6320 * about a memory-reclaim deadlock.
6322 str = kmalloc(len, GFP_KERNEL);
6327 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6328 clp->rpc_ops->version, clp->cl_minorversion,
6329 buf, clp->cl_rpcclient->cl_nodename);
6331 scnprintf(str, len, "Linux NFSv%u.%u %s",
6332 clp->rpc_ops->version, clp->cl_minorversion,
6333 clp->cl_rpcclient->cl_nodename);
6334 clp->cl_owner_id = str;
6339 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6340 * services. Advertise one based on the address family of the
6344 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6346 if (strchr(clp->cl_ipaddr, ':') != NULL)
6347 return scnprintf(buf, len, "tcp6");
6349 return scnprintf(buf, len, "tcp");
6352 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6354 struct nfs4_setclientid *sc = calldata;
6356 if (task->tk_status == 0)
6357 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6360 static const struct rpc_call_ops nfs4_setclientid_ops = {
6361 .rpc_call_done = nfs4_setclientid_done,
6365 * nfs4_proc_setclientid - Negotiate client ID
6366 * @clp: state data structure
6367 * @program: RPC program for NFSv4 callback service
6368 * @port: IP port number for NFS4 callback service
6369 * @cred: credential to use for this call
6370 * @res: where to place the result
6372 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6374 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6375 unsigned short port, const struct cred *cred,
6376 struct nfs4_setclientid_res *res)
6378 nfs4_verifier sc_verifier;
6379 struct nfs4_setclientid setclientid = {
6380 .sc_verifier = &sc_verifier,
6384 struct rpc_message msg = {
6385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6386 .rpc_argp = &setclientid,
6390 struct rpc_task_setup task_setup_data = {
6391 .rpc_client = clp->cl_rpcclient,
6392 .rpc_message = &msg,
6393 .callback_ops = &nfs4_setclientid_ops,
6394 .callback_data = &setclientid,
6395 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6397 unsigned long now = jiffies;
6400 /* nfs_client_id4 */
6401 nfs4_init_boot_verifier(clp, &sc_verifier);
6403 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6404 status = nfs4_init_uniform_client_string(clp);
6406 status = nfs4_init_nonuniform_client_string(clp);
6412 setclientid.sc_netid_len =
6413 nfs4_init_callback_netid(clp,
6414 setclientid.sc_netid,
6415 sizeof(setclientid.sc_netid));
6416 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6417 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6418 clp->cl_ipaddr, port >> 8, port & 255);
6420 dprintk("NFS call setclientid auth=%s, '%s'\n",
6421 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6424 status = nfs4_call_sync_custom(&task_setup_data);
6425 if (setclientid.sc_cred) {
6426 kfree(clp->cl_acceptor);
6427 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6428 put_rpccred(setclientid.sc_cred);
6432 do_renew_lease(clp, now);
6434 trace_nfs4_setclientid(clp, status);
6435 dprintk("NFS reply setclientid: %d\n", status);
6440 * nfs4_proc_setclientid_confirm - Confirm client ID
6441 * @clp: state data structure
6442 * @arg: result of a previous SETCLIENTID
6443 * @cred: credential to use for this call
6445 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6447 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6448 struct nfs4_setclientid_res *arg,
6449 const struct cred *cred)
6451 struct rpc_message msg = {
6452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6458 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6459 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6461 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6462 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6463 trace_nfs4_setclientid_confirm(clp, status);
6464 dprintk("NFS reply setclientid_confirm: %d\n", status);
6468 struct nfs4_delegreturndata {
6469 struct nfs4_delegreturnargs args;
6470 struct nfs4_delegreturnres res;
6472 nfs4_stateid stateid;
6473 unsigned long timestamp;
6475 struct nfs4_layoutreturn_args arg;
6476 struct nfs4_layoutreturn_res res;
6477 struct nfs4_xdr_opaque_data ld_private;
6481 struct nfs_fattr fattr;
6483 struct inode *inode;
6486 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6488 struct nfs4_delegreturndata *data = calldata;
6489 struct nfs4_exception exception = {
6490 .inode = data->inode,
6491 .stateid = &data->stateid,
6492 .task_is_privileged = data->args.seq_args.sa_privileged,
6495 if (!nfs4_sequence_done(task, &data->res.seq_res))
6498 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6500 /* Handle Layoutreturn errors */
6501 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6502 &data->res.lr_ret) == -EAGAIN)
6505 switch (task->tk_status) {
6507 renew_lease(data->res.server, data->timestamp);
6509 case -NFS4ERR_ADMIN_REVOKED:
6510 case -NFS4ERR_DELEG_REVOKED:
6511 case -NFS4ERR_EXPIRED:
6512 nfs4_free_revoked_stateid(data->res.server,
6514 task->tk_msg.rpc_cred);
6516 case -NFS4ERR_BAD_STATEID:
6517 case -NFS4ERR_STALE_STATEID:
6519 task->tk_status = 0;
6521 case -NFS4ERR_OLD_STATEID:
6522 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6523 nfs4_stateid_seqid_inc(&data->stateid);
6524 if (data->args.bitmask) {
6525 data->args.bitmask = NULL;
6526 data->res.fattr = NULL;
6529 case -NFS4ERR_ACCESS:
6530 if (data->args.bitmask) {
6531 data->args.bitmask = NULL;
6532 data->res.fattr = NULL;
6537 task->tk_status = nfs4_async_handle_exception(task,
6538 data->res.server, task->tk_status,
6540 if (exception.retry)
6543 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6544 data->rpc_status = task->tk_status;
6547 task->tk_status = 0;
6548 rpc_restart_call_prepare(task);
6551 static void nfs4_delegreturn_release(void *calldata)
6553 struct nfs4_delegreturndata *data = calldata;
6554 struct inode *inode = data->inode;
6557 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6560 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6561 nfs_iput_and_deactive(inode);
6566 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6568 struct nfs4_delegreturndata *d_data;
6569 struct pnfs_layout_hdr *lo;
6571 d_data = (struct nfs4_delegreturndata *)data;
6573 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6574 nfs4_sequence_done(task, &d_data->res.seq_res);
6578 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6579 if (lo && !pnfs_layout_is_valid(lo)) {
6580 d_data->args.lr_args = NULL;
6581 d_data->res.lr_res = NULL;
6584 nfs4_setup_sequence(d_data->res.server->nfs_client,
6585 &d_data->args.seq_args,
6586 &d_data->res.seq_res,
6590 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6591 .rpc_call_prepare = nfs4_delegreturn_prepare,
6592 .rpc_call_done = nfs4_delegreturn_done,
6593 .rpc_release = nfs4_delegreturn_release,
6596 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6598 struct nfs4_delegreturndata *data;
6599 struct nfs_server *server = NFS_SERVER(inode);
6600 struct rpc_task *task;
6601 struct rpc_message msg = {
6602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6605 struct rpc_task_setup task_setup_data = {
6606 .rpc_client = server->client,
6607 .rpc_message = &msg,
6608 .callback_ops = &nfs4_delegreturn_ops,
6609 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
6613 data = kzalloc(sizeof(*data), GFP_NOFS);
6617 nfs4_state_protect(server->nfs_client,
6618 NFS_SP4_MACH_CRED_CLEANUP,
6619 &task_setup_data.rpc_client, &msg);
6621 data->args.fhandle = &data->fh;
6622 data->args.stateid = &data->stateid;
6623 nfs4_bitmask_set(data->args.bitmask_store,
6624 server->cache_consistency_bitmask, inode, 0);
6625 data->args.bitmask = data->args.bitmask_store;
6626 nfs_copy_fh(&data->fh, NFS_FH(inode));
6627 nfs4_stateid_copy(&data->stateid, stateid);
6628 data->res.fattr = &data->fattr;
6629 data->res.server = server;
6630 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6631 data->lr.arg.ld_private = &data->lr.ld_private;
6632 nfs_fattr_init(data->res.fattr);
6633 data->timestamp = jiffies;
6634 data->rpc_status = 0;
6635 data->inode = nfs_igrab_and_active(inode);
6636 if (data->inode || issync) {
6637 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6640 data->args.lr_args = &data->lr.arg;
6641 data->res.lr_res = &data->lr.res;
6646 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6649 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6651 task_setup_data.callback_data = data;
6652 msg.rpc_argp = &data->args;
6653 msg.rpc_resp = &data->res;
6654 task = rpc_run_task(&task_setup_data);
6656 return PTR_ERR(task);
6659 status = rpc_wait_for_completion_task(task);
6662 status = data->rpc_status;
6668 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6670 struct nfs_server *server = NFS_SERVER(inode);
6671 struct nfs4_exception exception = { };
6674 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6675 trace_nfs4_delegreturn(inode, stateid, err);
6677 case -NFS4ERR_STALE_STATEID:
6678 case -NFS4ERR_EXPIRED:
6682 err = nfs4_handle_exception(server, err, &exception);
6683 } while (exception.retry);
6687 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6689 struct inode *inode = state->inode;
6690 struct nfs_server *server = NFS_SERVER(inode);
6691 struct nfs_client *clp = server->nfs_client;
6692 struct nfs_lockt_args arg = {
6693 .fh = NFS_FH(inode),
6696 struct nfs_lockt_res res = {
6699 struct rpc_message msg = {
6700 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6703 .rpc_cred = state->owner->so_cred,
6705 struct nfs4_lock_state *lsp;
6708 arg.lock_owner.clientid = clp->cl_clientid;
6709 status = nfs4_set_lock_state(state, request);
6712 lsp = request->fl_u.nfs4_fl.owner;
6713 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6714 arg.lock_owner.s_dev = server->s_dev;
6715 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6718 request->fl_type = F_UNLCK;
6720 case -NFS4ERR_DENIED:
6723 request->fl_ops->fl_release_private(request);
6724 request->fl_ops = NULL;
6729 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6731 struct nfs4_exception exception = {
6732 .interruptible = true,
6737 err = _nfs4_proc_getlk(state, cmd, request);
6738 trace_nfs4_get_lock(request, state, cmd, err);
6739 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6741 } while (exception.retry);
6746 * Update the seqid of a lock stateid after receiving
6747 * NFS4ERR_OLD_STATEID
6749 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6750 struct nfs4_lock_state *lsp)
6752 struct nfs4_state *state = lsp->ls_state;
6755 spin_lock(&state->state_lock);
6756 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6758 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6759 nfs4_stateid_seqid_inc(dst);
6761 dst->seqid = lsp->ls_stateid.seqid;
6764 spin_unlock(&state->state_lock);
6768 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6769 struct nfs4_lock_state *lsp)
6771 struct nfs4_state *state = lsp->ls_state;
6774 spin_lock(&state->state_lock);
6775 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6776 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6777 spin_unlock(&state->state_lock);
6781 struct nfs4_unlockdata {
6782 struct nfs_locku_args arg;
6783 struct nfs_locku_res res;
6784 struct nfs4_lock_state *lsp;
6785 struct nfs_open_context *ctx;
6786 struct nfs_lock_context *l_ctx;
6787 struct file_lock fl;
6788 struct nfs_server *server;
6789 unsigned long timestamp;
6792 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6793 struct nfs_open_context *ctx,
6794 struct nfs4_lock_state *lsp,
6795 struct nfs_seqid *seqid)
6797 struct nfs4_unlockdata *p;
6798 struct nfs4_state *state = lsp->ls_state;
6799 struct inode *inode = state->inode;
6801 p = kzalloc(sizeof(*p), GFP_NOFS);
6804 p->arg.fh = NFS_FH(inode);
6806 p->arg.seqid = seqid;
6807 p->res.seqid = seqid;
6809 /* Ensure we don't close file until we're done freeing locks! */
6810 p->ctx = get_nfs_open_context(ctx);
6811 p->l_ctx = nfs_get_lock_context(ctx);
6812 locks_init_lock(&p->fl);
6813 locks_copy_lock(&p->fl, fl);
6814 p->server = NFS_SERVER(inode);
6815 spin_lock(&state->state_lock);
6816 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6817 spin_unlock(&state->state_lock);
6821 static void nfs4_locku_release_calldata(void *data)
6823 struct nfs4_unlockdata *calldata = data;
6824 nfs_free_seqid(calldata->arg.seqid);
6825 nfs4_put_lock_state(calldata->lsp);
6826 nfs_put_lock_context(calldata->l_ctx);
6827 put_nfs_open_context(calldata->ctx);
6831 static void nfs4_locku_done(struct rpc_task *task, void *data)
6833 struct nfs4_unlockdata *calldata = data;
6834 struct nfs4_exception exception = {
6835 .inode = calldata->lsp->ls_state->inode,
6836 .stateid = &calldata->arg.stateid,
6839 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6841 switch (task->tk_status) {
6843 renew_lease(calldata->server, calldata->timestamp);
6844 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6845 if (nfs4_update_lock_stateid(calldata->lsp,
6846 &calldata->res.stateid))
6849 case -NFS4ERR_ADMIN_REVOKED:
6850 case -NFS4ERR_EXPIRED:
6851 nfs4_free_revoked_stateid(calldata->server,
6852 &calldata->arg.stateid,
6853 task->tk_msg.rpc_cred);
6855 case -NFS4ERR_BAD_STATEID:
6856 case -NFS4ERR_STALE_STATEID:
6857 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6859 rpc_restart_call_prepare(task);
6861 case -NFS4ERR_OLD_STATEID:
6862 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6864 rpc_restart_call_prepare(task);
6867 task->tk_status = nfs4_async_handle_exception(task,
6868 calldata->server, task->tk_status,
6870 if (exception.retry)
6871 rpc_restart_call_prepare(task);
6873 nfs_release_seqid(calldata->arg.seqid);
6876 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6878 struct nfs4_unlockdata *calldata = data;
6880 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6881 nfs_async_iocounter_wait(task, calldata->l_ctx))
6884 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6886 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6887 /* Note: exit _without_ running nfs4_locku_done */
6890 calldata->timestamp = jiffies;
6891 if (nfs4_setup_sequence(calldata->server->nfs_client,
6892 &calldata->arg.seq_args,
6893 &calldata->res.seq_res,
6895 nfs_release_seqid(calldata->arg.seqid);
6898 task->tk_action = NULL;
6900 nfs4_sequence_done(task, &calldata->res.seq_res);
6903 static const struct rpc_call_ops nfs4_locku_ops = {
6904 .rpc_call_prepare = nfs4_locku_prepare,
6905 .rpc_call_done = nfs4_locku_done,
6906 .rpc_release = nfs4_locku_release_calldata,
6909 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6910 struct nfs_open_context *ctx,
6911 struct nfs4_lock_state *lsp,
6912 struct nfs_seqid *seqid)
6914 struct nfs4_unlockdata *data;
6915 struct rpc_message msg = {
6916 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6917 .rpc_cred = ctx->cred,
6919 struct rpc_task_setup task_setup_data = {
6920 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6921 .rpc_message = &msg,
6922 .callback_ops = &nfs4_locku_ops,
6923 .workqueue = nfsiod_workqueue,
6924 .flags = RPC_TASK_ASYNC,
6926 struct nfs_client *client =
6927 NFS_SERVER(lsp->ls_state->inode)->nfs_client;
6929 if (client->cl_minorversion)
6930 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6932 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6933 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6935 /* Ensure this is an unlock - when canceling a lock, the
6936 * canceled lock is passed in, and it won't be an unlock.
6938 fl->fl_type = F_UNLCK;
6939 if (fl->fl_flags & FL_CLOSE)
6940 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6942 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6944 nfs_free_seqid(seqid);
6945 return ERR_PTR(-ENOMEM);
6948 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6949 msg.rpc_argp = &data->arg;
6950 msg.rpc_resp = &data->res;
6951 task_setup_data.callback_data = data;
6952 return rpc_run_task(&task_setup_data);
6955 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6957 struct inode *inode = state->inode;
6958 struct nfs4_state_owner *sp = state->owner;
6959 struct nfs_inode *nfsi = NFS_I(inode);
6960 struct nfs_seqid *seqid;
6961 struct nfs4_lock_state *lsp;
6962 struct rpc_task *task;
6963 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6965 unsigned char fl_flags = request->fl_flags;
6967 status = nfs4_set_lock_state(state, request);
6968 /* Unlock _before_ we do the RPC call */
6969 request->fl_flags |= FL_EXISTS;
6970 /* Exclude nfs_delegation_claim_locks() */
6971 mutex_lock(&sp->so_delegreturn_mutex);
6972 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6973 down_read(&nfsi->rwsem);
6974 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6975 up_read(&nfsi->rwsem);
6976 mutex_unlock(&sp->so_delegreturn_mutex);
6979 up_read(&nfsi->rwsem);
6980 mutex_unlock(&sp->so_delegreturn_mutex);
6983 /* Is this a delegated lock? */
6984 lsp = request->fl_u.nfs4_fl.owner;
6985 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6987 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6988 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6992 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6993 status = PTR_ERR(task);
6996 status = rpc_wait_for_completion_task(task);
6999 request->fl_flags = fl_flags;
7000 trace_nfs4_unlock(request, state, F_SETLK, status);
7004 struct nfs4_lockdata {
7005 struct nfs_lock_args arg;
7006 struct nfs_lock_res res;
7007 struct nfs4_lock_state *lsp;
7008 struct nfs_open_context *ctx;
7009 struct file_lock fl;
7010 unsigned long timestamp;
7013 struct nfs_server *server;
7016 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
7017 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
7020 struct nfs4_lockdata *p;
7021 struct inode *inode = lsp->ls_state->inode;
7022 struct nfs_server *server = NFS_SERVER(inode);
7023 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7025 p = kzalloc(sizeof(*p), gfp_mask);
7029 p->arg.fh = NFS_FH(inode);
7031 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
7032 if (IS_ERR(p->arg.open_seqid))
7034 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
7035 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
7036 if (IS_ERR(p->arg.lock_seqid))
7037 goto out_free_seqid;
7038 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
7039 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
7040 p->arg.lock_owner.s_dev = server->s_dev;
7041 p->res.lock_seqid = p->arg.lock_seqid;
7044 p->ctx = get_nfs_open_context(ctx);
7045 locks_init_lock(&p->fl);
7046 locks_copy_lock(&p->fl, fl);
7049 nfs_free_seqid(p->arg.open_seqid);
7055 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7057 struct nfs4_lockdata *data = calldata;
7058 struct nfs4_state *state = data->lsp->ls_state;
7060 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
7062 /* Do we need to do an open_to_lock_owner? */
7063 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
7064 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
7065 goto out_release_lock_seqid;
7067 nfs4_stateid_copy(&data->arg.open_stateid,
7068 &state->open_stateid);
7069 data->arg.new_lock_owner = 1;
7070 data->res.open_seqid = data->arg.open_seqid;
7072 data->arg.new_lock_owner = 0;
7073 nfs4_stateid_copy(&data->arg.lock_stateid,
7074 &data->lsp->ls_stateid);
7076 if (!nfs4_valid_open_stateid(state)) {
7077 data->rpc_status = -EBADF;
7078 task->tk_action = NULL;
7079 goto out_release_open_seqid;
7081 data->timestamp = jiffies;
7082 if (nfs4_setup_sequence(data->server->nfs_client,
7083 &data->arg.seq_args,
7087 out_release_open_seqid:
7088 nfs_release_seqid(data->arg.open_seqid);
7089 out_release_lock_seqid:
7090 nfs_release_seqid(data->arg.lock_seqid);
7092 nfs4_sequence_done(task, &data->res.seq_res);
7093 dprintk("%s: ret = %d\n", __func__, data->rpc_status);
7096 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7098 struct nfs4_lockdata *data = calldata;
7099 struct nfs4_lock_state *lsp = data->lsp;
7101 if (!nfs4_sequence_done(task, &data->res.seq_res))
7104 data->rpc_status = task->tk_status;
7105 switch (task->tk_status) {
7107 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
7109 if (data->arg.new_lock && !data->cancelled) {
7110 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
7111 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7114 if (data->arg.new_lock_owner != 0) {
7115 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7116 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7117 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7118 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7121 case -NFS4ERR_BAD_STATEID:
7122 case -NFS4ERR_OLD_STATEID:
7123 case -NFS4ERR_STALE_STATEID:
7124 case -NFS4ERR_EXPIRED:
7125 if (data->arg.new_lock_owner != 0) {
7126 if (!nfs4_stateid_match(&data->arg.open_stateid,
7127 &lsp->ls_state->open_stateid))
7129 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7134 dprintk("%s: ret = %d!\n", __func__, data->rpc_status);
7137 if (!data->cancelled)
7138 rpc_restart_call_prepare(task);
7142 static void nfs4_lock_release(void *calldata)
7144 struct nfs4_lockdata *data = calldata;
7146 nfs_free_seqid(data->arg.open_seqid);
7147 if (data->cancelled && data->rpc_status == 0) {
7148 struct rpc_task *task;
7149 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7150 data->arg.lock_seqid);
7152 rpc_put_task_async(task);
7153 dprintk("%s: cancelling lock!\n", __func__);
7155 nfs_free_seqid(data->arg.lock_seqid);
7156 nfs4_put_lock_state(data->lsp);
7157 put_nfs_open_context(data->ctx);
7161 static const struct rpc_call_ops nfs4_lock_ops = {
7162 .rpc_call_prepare = nfs4_lock_prepare,
7163 .rpc_call_done = nfs4_lock_done,
7164 .rpc_release = nfs4_lock_release,
7167 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7170 case -NFS4ERR_ADMIN_REVOKED:
7171 case -NFS4ERR_EXPIRED:
7172 case -NFS4ERR_BAD_STATEID:
7173 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7174 if (new_lock_owner != 0 ||
7175 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7176 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7178 case -NFS4ERR_STALE_STATEID:
7179 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7180 nfs4_schedule_lease_recovery(server->nfs_client);
7184 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7186 struct nfs4_lockdata *data;
7187 struct rpc_task *task;
7188 struct rpc_message msg = {
7189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7190 .rpc_cred = state->owner->so_cred,
7192 struct rpc_task_setup task_setup_data = {
7193 .rpc_client = NFS_CLIENT(state->inode),
7194 .rpc_message = &msg,
7195 .callback_ops = &nfs4_lock_ops,
7196 .workqueue = nfsiod_workqueue,
7197 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7200 struct nfs_client *client = NFS_SERVER(state->inode)->nfs_client;
7202 if (client->cl_minorversion)
7203 task_setup_data.flags |= RPC_TASK_MOVEABLE;
7205 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7206 fl->fl_u.nfs4_fl.owner,
7207 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
7211 data->arg.block = 1;
7212 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7213 recovery_type > NFS_LOCK_NEW);
7214 msg.rpc_argp = &data->arg;
7215 msg.rpc_resp = &data->res;
7216 task_setup_data.callback_data = data;
7217 if (recovery_type > NFS_LOCK_NEW) {
7218 if (recovery_type == NFS_LOCK_RECLAIM)
7219 data->arg.reclaim = NFS_LOCK_RECLAIM;
7221 data->arg.new_lock = 1;
7222 task = rpc_run_task(&task_setup_data);
7224 return PTR_ERR(task);
7225 ret = rpc_wait_for_completion_task(task);
7227 ret = data->rpc_status;
7229 nfs4_handle_setlk_error(data->server, data->lsp,
7230 data->arg.new_lock_owner, ret);
7232 data->cancelled = true;
7233 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7235 dprintk("%s: ret = %d\n", __func__, ret);
7239 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7241 struct nfs_server *server = NFS_SERVER(state->inode);
7242 struct nfs4_exception exception = {
7243 .inode = state->inode,
7248 /* Cache the lock if possible... */
7249 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7251 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7252 if (err != -NFS4ERR_DELAY)
7254 nfs4_handle_exception(server, err, &exception);
7255 } while (exception.retry);
7259 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7261 struct nfs_server *server = NFS_SERVER(state->inode);
7262 struct nfs4_exception exception = {
7263 .inode = state->inode,
7267 err = nfs4_set_lock_state(state, request);
7270 if (!recover_lost_locks) {
7271 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7275 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7277 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7281 case -NFS4ERR_GRACE:
7282 case -NFS4ERR_DELAY:
7283 nfs4_handle_exception(server, err, &exception);
7286 } while (exception.retry);
7291 #if defined(CONFIG_NFS_V4_1)
7292 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7294 struct nfs4_lock_state *lsp;
7297 status = nfs4_set_lock_state(state, request);
7300 lsp = request->fl_u.nfs4_fl.owner;
7301 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7302 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7304 return nfs4_lock_expired(state, request);
7308 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7310 struct nfs_inode *nfsi = NFS_I(state->inode);
7311 struct nfs4_state_owner *sp = state->owner;
7312 unsigned char fl_flags = request->fl_flags;
7315 request->fl_flags |= FL_ACCESS;
7316 status = locks_lock_inode_wait(state->inode, request);
7319 mutex_lock(&sp->so_delegreturn_mutex);
7320 down_read(&nfsi->rwsem);
7321 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7322 /* Yes: cache locks! */
7323 /* ...but avoid races with delegation recall... */
7324 request->fl_flags = fl_flags & ~FL_SLEEP;
7325 status = locks_lock_inode_wait(state->inode, request);
7326 up_read(&nfsi->rwsem);
7327 mutex_unlock(&sp->so_delegreturn_mutex);
7330 up_read(&nfsi->rwsem);
7331 mutex_unlock(&sp->so_delegreturn_mutex);
7332 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7334 request->fl_flags = fl_flags;
7338 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7340 struct nfs4_exception exception = {
7342 .inode = state->inode,
7343 .interruptible = true,
7348 err = _nfs4_proc_setlk(state, cmd, request);
7349 if (err == -NFS4ERR_DENIED)
7351 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7353 } while (exception.retry);
7357 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7358 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7361 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7362 struct file_lock *request)
7364 int status = -ERESTARTSYS;
7365 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7367 while(!signalled()) {
7368 status = nfs4_proc_setlk(state, cmd, request);
7369 if ((status != -EAGAIN) || IS_SETLK(cmd))
7371 freezable_schedule_timeout_interruptible(timeout);
7373 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7374 status = -ERESTARTSYS;
7379 #ifdef CONFIG_NFS_V4_1
7380 struct nfs4_lock_waiter {
7381 struct inode *inode;
7382 struct nfs_lowner owner;
7383 wait_queue_entry_t wait;
7387 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7389 struct nfs4_lock_waiter *waiter =
7390 container_of(wait, struct nfs4_lock_waiter, wait);
7392 /* NULL key means to wake up everyone */
7394 struct cb_notify_lock_args *cbnl = key;
7395 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7396 *wowner = &waiter->owner;
7398 /* Only wake if the callback was for the same owner. */
7399 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7402 /* Make sure it's for the right inode */
7403 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7407 return woken_wake_function(wait, mode, flags, key);
7411 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7413 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7414 struct nfs_server *server = NFS_SERVER(state->inode);
7415 struct nfs_client *clp = server->nfs_client;
7416 wait_queue_head_t *q = &clp->cl_lock_waitq;
7417 struct nfs4_lock_waiter waiter = {
7418 .inode = state->inode,
7419 .owner = { .clientid = clp->cl_clientid,
7420 .id = lsp->ls_seqid.owner_id,
7421 .s_dev = server->s_dev },
7425 /* Don't bother with waitqueue if we don't expect a callback */
7426 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7427 return nfs4_retry_setlk_simple(state, cmd, request);
7429 init_wait(&waiter.wait);
7430 waiter.wait.func = nfs4_wake_lock_waiter;
7431 add_wait_queue(q, &waiter.wait);
7434 status = nfs4_proc_setlk(state, cmd, request);
7435 if (status != -EAGAIN || IS_SETLK(cmd))
7438 status = -ERESTARTSYS;
7439 freezer_do_not_count();
7440 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE,
7441 NFS4_LOCK_MAXTIMEOUT);
7443 } while (!signalled());
7445 remove_wait_queue(q, &waiter.wait);
7449 #else /* !CONFIG_NFS_V4_1 */
7451 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7453 return nfs4_retry_setlk_simple(state, cmd, request);
7458 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7460 struct nfs_open_context *ctx;
7461 struct nfs4_state *state;
7464 /* verify open state */
7465 ctx = nfs_file_open_context(filp);
7468 if (IS_GETLK(cmd)) {
7470 return nfs4_proc_getlk(state, F_GETLK, request);
7474 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7477 if (request->fl_type == F_UNLCK) {
7479 return nfs4_proc_unlck(state, cmd, request);
7486 if ((request->fl_flags & FL_POSIX) &&
7487 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7491 * Don't rely on the VFS having checked the file open mode,
7492 * since it won't do this for flock() locks.
7494 switch (request->fl_type) {
7496 if (!(filp->f_mode & FMODE_READ))
7500 if (!(filp->f_mode & FMODE_WRITE))
7504 status = nfs4_set_lock_state(state, request);
7508 return nfs4_retry_setlk(state, cmd, request);
7511 static int nfs4_delete_lease(struct file *file, void **priv)
7513 return generic_setlease(file, F_UNLCK, NULL, priv);
7516 static int nfs4_add_lease(struct file *file, long arg, struct file_lock **lease,
7519 struct inode *inode = file_inode(file);
7520 fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7523 /* No delegation, no lease */
7524 if (!nfs4_have_delegation(inode, type))
7526 ret = generic_setlease(file, arg, lease, priv);
7527 if (ret || nfs4_have_delegation(inode, type))
7529 /* We raced with a delegation return */
7530 nfs4_delete_lease(file, priv);
7534 int nfs4_proc_setlease(struct file *file, long arg, struct file_lock **lease,
7540 return nfs4_add_lease(file, arg, lease, priv);
7542 return nfs4_delete_lease(file, priv);
7548 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7550 struct nfs_server *server = NFS_SERVER(state->inode);
7553 err = nfs4_set_lock_state(state, fl);
7557 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7558 if (err != -NFS4ERR_DELAY)
7561 } while (err == -NFS4ERR_DELAY);
7562 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7565 struct nfs_release_lockowner_data {
7566 struct nfs4_lock_state *lsp;
7567 struct nfs_server *server;
7568 struct nfs_release_lockowner_args args;
7569 struct nfs_release_lockowner_res res;
7570 unsigned long timestamp;
7573 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7575 struct nfs_release_lockowner_data *data = calldata;
7576 struct nfs_server *server = data->server;
7577 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7578 &data->res.seq_res, task);
7579 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7580 data->timestamp = jiffies;
7583 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7585 struct nfs_release_lockowner_data *data = calldata;
7586 struct nfs_server *server = data->server;
7588 nfs40_sequence_done(task, &data->res.seq_res);
7590 switch (task->tk_status) {
7592 renew_lease(server, data->timestamp);
7594 case -NFS4ERR_STALE_CLIENTID:
7595 case -NFS4ERR_EXPIRED:
7596 nfs4_schedule_lease_recovery(server->nfs_client);
7598 case -NFS4ERR_LEASE_MOVED:
7599 case -NFS4ERR_DELAY:
7600 if (nfs4_async_handle_error(task, server,
7601 NULL, NULL) == -EAGAIN)
7602 rpc_restart_call_prepare(task);
7606 static void nfs4_release_lockowner_release(void *calldata)
7608 struct nfs_release_lockowner_data *data = calldata;
7609 nfs4_free_lock_state(data->server, data->lsp);
7613 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7614 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7615 .rpc_call_done = nfs4_release_lockowner_done,
7616 .rpc_release = nfs4_release_lockowner_release,
7620 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7622 struct nfs_release_lockowner_data *data;
7623 struct rpc_message msg = {
7624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7627 if (server->nfs_client->cl_mvops->minor_version != 0)
7630 data = kmalloc(sizeof(*data), GFP_NOFS);
7634 data->server = server;
7635 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7636 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7637 data->args.lock_owner.s_dev = server->s_dev;
7639 msg.rpc_argp = &data->args;
7640 msg.rpc_resp = &data->res;
7641 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7642 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7645 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7647 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7648 struct user_namespace *mnt_userns,
7649 struct dentry *unused, struct inode *inode,
7650 const char *key, const void *buf,
7651 size_t buflen, int flags)
7653 return nfs4_proc_set_acl(inode, buf, buflen);
7656 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7657 struct dentry *unused, struct inode *inode,
7658 const char *key, void *buf, size_t buflen)
7660 return nfs4_proc_get_acl(inode, buf, buflen);
7663 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7665 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7668 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7670 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7671 struct user_namespace *mnt_userns,
7672 struct dentry *unused, struct inode *inode,
7673 const char *key, const void *buf,
7674 size_t buflen, int flags)
7676 if (security_ismaclabel(key))
7677 return nfs4_set_security_label(inode, buf, buflen);
7682 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7683 struct dentry *unused, struct inode *inode,
7684 const char *key, void *buf, size_t buflen)
7686 if (security_ismaclabel(key))
7687 return nfs4_get_security_label(inode, buf, buflen);
7692 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7696 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7697 len = security_inode_listsecurity(inode, list, list_len);
7698 if (len >= 0 && list_len && len > list_len)
7704 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7705 .prefix = XATTR_SECURITY_PREFIX,
7706 .get = nfs4_xattr_get_nfs4_label,
7707 .set = nfs4_xattr_set_nfs4_label,
7713 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7720 #ifdef CONFIG_NFS_V4_2
7721 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7722 struct user_namespace *mnt_userns,
7723 struct dentry *unused, struct inode *inode,
7724 const char *key, const void *buf,
7725 size_t buflen, int flags)
7730 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7734 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7735 * flags right now. Handling of xattr operations use the normal
7736 * file read/write permissions.
7738 * Just in case the server has other ideas (which RFC 8276 allows),
7739 * do a cached access check for the XA* flags to possibly avoid
7740 * doing an RPC and getting EACCES back.
7742 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7743 if (!(mask & NFS_ACCESS_XAWRITE))
7748 ret = nfs42_proc_removexattr(inode, key);
7750 nfs4_xattr_cache_remove(inode, key);
7752 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7754 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7760 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7761 struct dentry *unused, struct inode *inode,
7762 const char *key, void *buf, size_t buflen)
7767 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7770 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7771 if (!(mask & NFS_ACCESS_XAREAD))
7775 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7779 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7780 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7783 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7789 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7798 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7801 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7802 if (!(mask & NFS_ACCESS_XALIST))
7806 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7810 ret = nfs4_xattr_cache_list(inode, list, list_len);
7811 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7816 buflen = list_len ? list_len : XATTR_LIST_MAX;
7817 buf = list_len ? list : NULL;
7821 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7834 nfs4_xattr_cache_set_list(inode, list, size);
7842 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7846 #endif /* CONFIG_NFS_V4_2 */
7849 * nfs_fhget will use either the mounted_on_fileid or the fileid
7851 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7853 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7854 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7855 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7856 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7859 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7860 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7861 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7865 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7866 const struct qstr *name,
7867 struct nfs4_fs_locations *fs_locations,
7870 struct nfs_server *server = NFS_SERVER(dir);
7872 struct nfs4_fs_locations_arg args = {
7873 .dir_fh = NFS_FH(dir),
7878 struct nfs4_fs_locations_res res = {
7879 .fs_locations = fs_locations,
7881 struct rpc_message msg = {
7882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7888 dprintk("%s: start\n", __func__);
7890 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7891 bitmask[1] = nfs4_fattr_bitmap[1];
7893 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7894 * is not supported */
7895 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7896 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7898 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7900 nfs_fattr_init(&fs_locations->fattr);
7901 fs_locations->server = server;
7902 fs_locations->nlocations = 0;
7903 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7904 dprintk("%s: returned status = %d\n", __func__, status);
7908 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7909 const struct qstr *name,
7910 struct nfs4_fs_locations *fs_locations,
7913 struct nfs4_exception exception = {
7914 .interruptible = true,
7918 err = _nfs4_proc_fs_locations(client, dir, name,
7919 fs_locations, page);
7920 trace_nfs4_get_fs_locations(dir, name, err);
7921 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7923 } while (exception.retry);
7928 * This operation also signals the server that this client is
7929 * performing migration recovery. The server can stop returning
7930 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7931 * appended to this compound to identify the client ID which is
7932 * performing recovery.
7934 static int _nfs40_proc_get_locations(struct nfs_server *server,
7935 struct nfs_fh *fhandle,
7936 struct nfs4_fs_locations *locations,
7937 struct page *page, const struct cred *cred)
7939 struct rpc_clnt *clnt = server->client;
7941 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7943 struct nfs4_fs_locations_arg args = {
7944 .clientid = server->nfs_client->cl_clientid,
7948 .migration = 1, /* skip LOOKUP */
7949 .renew = 1, /* append RENEW */
7951 struct nfs4_fs_locations_res res = {
7952 .fs_locations = locations,
7956 struct rpc_message msg = {
7957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7962 unsigned long now = jiffies;
7965 nfs_fattr_init(&locations->fattr);
7966 locations->server = server;
7967 locations->nlocations = 0;
7969 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7970 status = nfs4_call_sync_sequence(clnt, server, &msg,
7971 &args.seq_args, &res.seq_res);
7975 renew_lease(server, now);
7979 #ifdef CONFIG_NFS_V4_1
7982 * This operation also signals the server that this client is
7983 * performing migration recovery. The server can stop asserting
7984 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7985 * performing this operation is identified in the SEQUENCE
7986 * operation in this compound.
7988 * When the client supports GETATTR(fs_locations_info), it can
7989 * be plumbed in here.
7991 static int _nfs41_proc_get_locations(struct nfs_server *server,
7992 struct nfs_fh *fhandle,
7993 struct nfs4_fs_locations *locations,
7994 struct page *page, const struct cred *cred)
7996 struct rpc_clnt *clnt = server->client;
7998 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8000 struct nfs4_fs_locations_arg args = {
8004 .migration = 1, /* skip LOOKUP */
8006 struct nfs4_fs_locations_res res = {
8007 .fs_locations = locations,
8010 struct rpc_message msg = {
8011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8018 nfs_fattr_init(&locations->fattr);
8019 locations->server = server;
8020 locations->nlocations = 0;
8022 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8023 status = nfs4_call_sync_sequence(clnt, server, &msg,
8024 &args.seq_args, &res.seq_res);
8025 if (status == NFS4_OK &&
8026 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8027 status = -NFS4ERR_LEASE_MOVED;
8031 #endif /* CONFIG_NFS_V4_1 */
8034 * nfs4_proc_get_locations - discover locations for a migrated FSID
8035 * @server: pointer to nfs_server to process
8036 * @fhandle: pointer to the kernel NFS client file handle
8037 * @locations: result of query
8039 * @cred: credential to use for this operation
8041 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
8042 * operation failed, or a negative errno if a local error occurred.
8044 * On success, "locations" is filled in, but if the server has
8045 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
8048 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
8049 * from this client that require migration recovery.
8051 int nfs4_proc_get_locations(struct nfs_server *server,
8052 struct nfs_fh *fhandle,
8053 struct nfs4_fs_locations *locations,
8054 struct page *page, const struct cred *cred)
8056 struct nfs_client *clp = server->nfs_client;
8057 const struct nfs4_mig_recovery_ops *ops =
8058 clp->cl_mvops->mig_recovery_ops;
8059 struct nfs4_exception exception = {
8060 .interruptible = true,
8064 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8065 (unsigned long long)server->fsid.major,
8066 (unsigned long long)server->fsid.minor,
8068 nfs_display_fhandle(fhandle, __func__);
8071 status = ops->get_locations(server, fhandle, locations, page,
8073 if (status != -NFS4ERR_DELAY)
8075 nfs4_handle_exception(server, status, &exception);
8076 } while (exception.retry);
8081 * This operation also signals the server that this client is
8082 * performing "lease moved" recovery. The server can stop
8083 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
8084 * is appended to this compound to identify the client ID which is
8085 * performing recovery.
8087 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
8089 struct nfs_server *server = NFS_SERVER(inode);
8090 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8091 struct rpc_clnt *clnt = server->client;
8092 struct nfs4_fsid_present_arg args = {
8093 .fh = NFS_FH(inode),
8094 .clientid = clp->cl_clientid,
8095 .renew = 1, /* append RENEW */
8097 struct nfs4_fsid_present_res res = {
8100 struct rpc_message msg = {
8101 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8106 unsigned long now = jiffies;
8109 res.fh = nfs_alloc_fhandle();
8113 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8114 status = nfs4_call_sync_sequence(clnt, server, &msg,
8115 &args.seq_args, &res.seq_res);
8116 nfs_free_fhandle(res.fh);
8120 do_renew_lease(clp, now);
8124 #ifdef CONFIG_NFS_V4_1
8127 * This operation also signals the server that this client is
8128 * performing "lease moved" recovery. The server can stop asserting
8129 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8130 * this operation is identified in the SEQUENCE operation in this
8133 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8135 struct nfs_server *server = NFS_SERVER(inode);
8136 struct rpc_clnt *clnt = server->client;
8137 struct nfs4_fsid_present_arg args = {
8138 .fh = NFS_FH(inode),
8140 struct nfs4_fsid_present_res res = {
8142 struct rpc_message msg = {
8143 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8150 res.fh = nfs_alloc_fhandle();
8154 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8155 status = nfs4_call_sync_sequence(clnt, server, &msg,
8156 &args.seq_args, &res.seq_res);
8157 nfs_free_fhandle(res.fh);
8158 if (status == NFS4_OK &&
8159 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8160 status = -NFS4ERR_LEASE_MOVED;
8164 #endif /* CONFIG_NFS_V4_1 */
8167 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8168 * @inode: inode on FSID to check
8169 * @cred: credential to use for this operation
8171 * Server indicates whether the FSID is present, moved, or not
8172 * recognized. This operation is necessary to clear a LEASE_MOVED
8173 * condition for this client ID.
8175 * Returns NFS4_OK if the FSID is present on this server,
8176 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8177 * NFS4ERR code if some error occurred on the server, or a
8178 * negative errno if a local failure occurred.
8180 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8182 struct nfs_server *server = NFS_SERVER(inode);
8183 struct nfs_client *clp = server->nfs_client;
8184 const struct nfs4_mig_recovery_ops *ops =
8185 clp->cl_mvops->mig_recovery_ops;
8186 struct nfs4_exception exception = {
8187 .interruptible = true,
8191 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8192 (unsigned long long)server->fsid.major,
8193 (unsigned long long)server->fsid.minor,
8195 nfs_display_fhandle(NFS_FH(inode), __func__);
8198 status = ops->fsid_present(inode, cred);
8199 if (status != -NFS4ERR_DELAY)
8201 nfs4_handle_exception(server, status, &exception);
8202 } while (exception.retry);
8207 * If 'use_integrity' is true and the state managment nfs_client
8208 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8209 * and the machine credential as per RFC3530bis and RFC5661 Security
8210 * Considerations sections. Otherwise, just use the user cred with the
8211 * filesystem's rpc_client.
8213 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8216 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8217 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8218 struct nfs4_secinfo_arg args = {
8219 .dir_fh = NFS_FH(dir),
8222 struct nfs4_secinfo_res res = {
8225 struct rpc_message msg = {
8226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8230 struct nfs4_call_sync_data data = {
8231 .seq_server = NFS_SERVER(dir),
8232 .seq_args = &args.seq_args,
8233 .seq_res = &res.seq_res,
8235 struct rpc_task_setup task_setup = {
8237 .rpc_message = &msg,
8238 .callback_ops = clp->cl_mvops->call_sync_ops,
8239 .callback_data = &data,
8240 .flags = RPC_TASK_NO_ROUND_ROBIN,
8242 const struct cred *cred = NULL;
8244 if (use_integrity) {
8245 clnt = clp->cl_rpcclient;
8246 task_setup.rpc_client = clnt;
8248 cred = nfs4_get_clid_cred(clp);
8249 msg.rpc_cred = cred;
8252 dprintk("NFS call secinfo %s\n", name->name);
8254 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8255 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8256 status = nfs4_call_sync_custom(&task_setup);
8258 dprintk("NFS reply secinfo: %d\n", status);
8264 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8265 struct nfs4_secinfo_flavors *flavors)
8267 struct nfs4_exception exception = {
8268 .interruptible = true,
8272 err = -NFS4ERR_WRONGSEC;
8274 /* try to use integrity protection with machine cred */
8275 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8276 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8279 * if unable to use integrity protection, or SECINFO with
8280 * integrity protection returns NFS4ERR_WRONGSEC (which is
8281 * disallowed by spec, but exists in deployed servers) use
8282 * the current filesystem's rpc_client and the user cred.
8284 if (err == -NFS4ERR_WRONGSEC)
8285 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8287 trace_nfs4_secinfo(dir, name, err);
8288 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8290 } while (exception.retry);
8294 #ifdef CONFIG_NFS_V4_1
8296 * Check the exchange flags returned by the server for invalid flags, having
8297 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8300 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8302 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8304 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8306 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8307 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8309 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8313 return -NFS4ERR_INVAL;
8317 nfs41_same_server_scope(struct nfs41_server_scope *a,
8318 struct nfs41_server_scope *b)
8320 if (a->server_scope_sz != b->server_scope_sz)
8322 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8326 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8328 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8329 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8330 struct nfs_client *clp = args->client;
8332 switch (task->tk_status) {
8333 case -NFS4ERR_BADSESSION:
8334 case -NFS4ERR_DEADSESSION:
8335 nfs4_schedule_session_recovery(clp->cl_session,
8338 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8339 res->dir != NFS4_CDFS4_BOTH) {
8340 rpc_task_close_connection(task);
8341 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8342 rpc_restart_call(task);
8346 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8347 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8351 * nfs4_proc_bind_one_conn_to_session()
8353 * The 4.1 client currently uses the same TCP connection for the
8354 * fore and backchannel.
8357 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8358 struct rpc_xprt *xprt,
8359 struct nfs_client *clp,
8360 const struct cred *cred)
8363 struct nfs41_bind_conn_to_session_args args = {
8365 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8368 struct nfs41_bind_conn_to_session_res res;
8369 struct rpc_message msg = {
8371 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8376 struct rpc_task_setup task_setup_data = {
8379 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8380 .rpc_message = &msg,
8381 .flags = RPC_TASK_TIMEOUT,
8383 struct rpc_task *task;
8385 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8386 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8387 args.dir = NFS4_CDFC4_FORE;
8389 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8390 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8391 args.dir = NFS4_CDFC4_FORE;
8393 task = rpc_run_task(&task_setup_data);
8394 if (!IS_ERR(task)) {
8395 status = task->tk_status;
8398 status = PTR_ERR(task);
8399 trace_nfs4_bind_conn_to_session(clp, status);
8401 if (memcmp(res.sessionid.data,
8402 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8403 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8406 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8407 dprintk("NFS: %s: Unexpected direction from server\n",
8411 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8412 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8421 struct rpc_bind_conn_calldata {
8422 struct nfs_client *clp;
8423 const struct cred *cred;
8427 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8428 struct rpc_xprt *xprt,
8431 struct rpc_bind_conn_calldata *p = calldata;
8433 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8436 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8438 struct rpc_bind_conn_calldata data = {
8442 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8443 nfs4_proc_bind_conn_to_session_callback, &data);
8447 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8448 * and operations we'd like to see to enable certain features in the allow map
8450 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8451 .how = SP4_MACH_CRED,
8452 .enforce.u.words = {
8453 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8454 1 << (OP_EXCHANGE_ID - 32) |
8455 1 << (OP_CREATE_SESSION - 32) |
8456 1 << (OP_DESTROY_SESSION - 32) |
8457 1 << (OP_DESTROY_CLIENTID - 32)
8460 [0] = 1 << (OP_CLOSE) |
8461 1 << (OP_OPEN_DOWNGRADE) |
8463 1 << (OP_DELEGRETURN) |
8465 [1] = 1 << (OP_SECINFO - 32) |
8466 1 << (OP_SECINFO_NO_NAME - 32) |
8467 1 << (OP_LAYOUTRETURN - 32) |
8468 1 << (OP_TEST_STATEID - 32) |
8469 1 << (OP_FREE_STATEID - 32) |
8470 1 << (OP_WRITE - 32)
8475 * Select the state protection mode for client `clp' given the server results
8476 * from exchange_id in `sp'.
8478 * Returns 0 on success, negative errno otherwise.
8480 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8481 struct nfs41_state_protection *sp)
8483 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8484 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8485 1 << (OP_EXCHANGE_ID - 32) |
8486 1 << (OP_CREATE_SESSION - 32) |
8487 1 << (OP_DESTROY_SESSION - 32) |
8488 1 << (OP_DESTROY_CLIENTID - 32)
8490 unsigned long flags = 0;
8494 if (sp->how == SP4_MACH_CRED) {
8495 /* Print state protect result */
8496 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8497 for (i = 0; i <= LAST_NFS4_OP; i++) {
8498 if (test_bit(i, sp->enforce.u.longs))
8499 dfprintk(MOUNT, " enforce op %d\n", i);
8500 if (test_bit(i, sp->allow.u.longs))
8501 dfprintk(MOUNT, " allow op %d\n", i);
8504 /* make sure nothing is on enforce list that isn't supported */
8505 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8506 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8507 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8514 * Minimal mode - state operations are allowed to use machine
8515 * credential. Note this already happens by default, so the
8516 * client doesn't have to do anything more than the negotiation.
8518 * NOTE: we don't care if EXCHANGE_ID is in the list -
8519 * we're already using the machine cred for exchange_id
8520 * and will never use a different cred.
8522 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8523 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8524 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8525 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8526 dfprintk(MOUNT, "sp4_mach_cred:\n");
8527 dfprintk(MOUNT, " minimal mode enabled\n");
8528 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8530 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8535 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8536 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8537 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8538 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8539 dfprintk(MOUNT, " cleanup mode enabled\n");
8540 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8543 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8544 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8545 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8548 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8549 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8550 dfprintk(MOUNT, " secinfo mode enabled\n");
8551 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8554 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8555 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8556 dfprintk(MOUNT, " stateid mode enabled\n");
8557 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8560 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8561 dfprintk(MOUNT, " write mode enabled\n");
8562 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8565 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8566 dfprintk(MOUNT, " commit mode enabled\n");
8567 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8571 clp->cl_sp4_flags = flags;
8575 struct nfs41_exchange_id_data {
8576 struct nfs41_exchange_id_res res;
8577 struct nfs41_exchange_id_args args;
8580 static void nfs4_exchange_id_release(void *data)
8582 struct nfs41_exchange_id_data *cdata =
8583 (struct nfs41_exchange_id_data *)data;
8585 nfs_put_client(cdata->args.client);
8586 kfree(cdata->res.impl_id);
8587 kfree(cdata->res.server_scope);
8588 kfree(cdata->res.server_owner);
8592 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8593 .rpc_release = nfs4_exchange_id_release,
8597 * _nfs4_proc_exchange_id()
8599 * Wrapper for EXCHANGE_ID operation.
8601 static struct rpc_task *
8602 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8603 u32 sp4_how, struct rpc_xprt *xprt)
8605 struct rpc_message msg = {
8606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8609 struct rpc_task_setup task_setup_data = {
8610 .rpc_client = clp->cl_rpcclient,
8611 .callback_ops = &nfs4_exchange_id_call_ops,
8612 .rpc_message = &msg,
8613 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8615 struct nfs41_exchange_id_data *calldata;
8618 if (!refcount_inc_not_zero(&clp->cl_count))
8619 return ERR_PTR(-EIO);
8622 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8626 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8628 status = nfs4_init_uniform_client_string(clp);
8632 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8635 if (unlikely(calldata->res.server_owner == NULL))
8638 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8640 if (unlikely(calldata->res.server_scope == NULL))
8641 goto out_server_owner;
8643 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8644 if (unlikely(calldata->res.impl_id == NULL))
8645 goto out_server_scope;
8649 calldata->args.state_protect.how = SP4_NONE;
8653 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8663 task_setup_data.rpc_xprt = xprt;
8664 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8665 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8666 sizeof(calldata->args.verifier.data));
8668 calldata->args.client = clp;
8669 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8670 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8671 #ifdef CONFIG_NFS_V4_1_MIGRATION
8672 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8674 msg.rpc_argp = &calldata->args;
8675 msg.rpc_resp = &calldata->res;
8676 task_setup_data.callback_data = calldata;
8678 return rpc_run_task(&task_setup_data);
8681 kfree(calldata->res.impl_id);
8683 kfree(calldata->res.server_scope);
8685 kfree(calldata->res.server_owner);
8689 nfs_put_client(clp);
8690 return ERR_PTR(status);
8694 * _nfs4_proc_exchange_id()
8696 * Wrapper for EXCHANGE_ID operation.
8698 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8701 struct rpc_task *task;
8702 struct nfs41_exchange_id_args *argp;
8703 struct nfs41_exchange_id_res *resp;
8704 unsigned long now = jiffies;
8707 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8709 return PTR_ERR(task);
8711 argp = task->tk_msg.rpc_argp;
8712 resp = task->tk_msg.rpc_resp;
8713 status = task->tk_status;
8717 status = nfs4_check_cl_exchange_flags(resp->flags,
8718 clp->cl_mvops->minor_version);
8722 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8726 do_renew_lease(clp, now);
8728 clp->cl_clientid = resp->clientid;
8729 clp->cl_exchange_flags = resp->flags;
8730 clp->cl_seqid = resp->seqid;
8731 /* Client ID is not confirmed */
8732 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8733 clear_bit(NFS4_SESSION_ESTABLISHED,
8734 &clp->cl_session->session_state);
8736 if (clp->cl_serverscope != NULL &&
8737 !nfs41_same_server_scope(clp->cl_serverscope,
8738 resp->server_scope)) {
8739 dprintk("%s: server_scope mismatch detected\n",
8741 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8744 swap(clp->cl_serverowner, resp->server_owner);
8745 swap(clp->cl_serverscope, resp->server_scope);
8746 swap(clp->cl_implid, resp->impl_id);
8748 /* Save the EXCHANGE_ID verifier session trunk tests */
8749 memcpy(clp->cl_confirm.data, argp->verifier.data,
8750 sizeof(clp->cl_confirm.data));
8752 trace_nfs4_exchange_id(clp, status);
8758 * nfs4_proc_exchange_id()
8760 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8762 * Since the clientid has expired, all compounds using sessions
8763 * associated with the stale clientid will be returning
8764 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8765 * be in some phase of session reset.
8767 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8769 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8771 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8774 /* try SP4_MACH_CRED if krb5i/p */
8775 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8776 authflavor == RPC_AUTH_GSS_KRB5P) {
8777 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8783 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8787 * nfs4_test_session_trunk
8789 * This is an add_xprt_test() test function called from
8790 * rpc_clnt_setup_test_and_add_xprt.
8792 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8793 * and is dereferrenced in nfs4_exchange_id_release
8795 * Upon success, add the new transport to the rpc_clnt
8797 * @clnt: struct rpc_clnt to get new transport
8798 * @xprt: the rpc_xprt to test
8799 * @data: call data for _nfs4_proc_exchange_id.
8801 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8804 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8805 struct rpc_task *task;
8810 dprintk("--> %s try %s\n", __func__,
8811 xprt->address_strings[RPC_DISPLAY_ADDR]);
8813 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8815 /* Test connection for session trunking. Async exchange_id call */
8816 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8820 status = task->tk_status;
8822 status = nfs4_detect_session_trunking(adata->clp,
8823 task->tk_msg.rpc_resp, xprt);
8826 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8830 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8832 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8833 const struct cred *cred)
8835 struct rpc_message msg = {
8836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8842 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8843 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8844 trace_nfs4_destroy_clientid(clp, status);
8846 dprintk("NFS: Got error %d from the server %s on "
8847 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8851 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8852 const struct cred *cred)
8857 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8858 ret = _nfs4_proc_destroy_clientid(clp, cred);
8860 case -NFS4ERR_DELAY:
8861 case -NFS4ERR_CLIENTID_BUSY:
8871 int nfs4_destroy_clientid(struct nfs_client *clp)
8873 const struct cred *cred;
8876 if (clp->cl_mvops->minor_version < 1)
8878 if (clp->cl_exchange_flags == 0)
8880 if (clp->cl_preserve_clid)
8882 cred = nfs4_get_clid_cred(clp);
8883 ret = nfs4_proc_destroy_clientid(clp, cred);
8887 case -NFS4ERR_STALE_CLIENTID:
8888 clp->cl_exchange_flags = 0;
8894 #endif /* CONFIG_NFS_V4_1 */
8896 struct nfs4_get_lease_time_data {
8897 struct nfs4_get_lease_time_args *args;
8898 struct nfs4_get_lease_time_res *res;
8899 struct nfs_client *clp;
8902 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8905 struct nfs4_get_lease_time_data *data =
8906 (struct nfs4_get_lease_time_data *)calldata;
8908 /* just setup sequence, do not trigger session recovery
8909 since we're invoked within one */
8910 nfs4_setup_sequence(data->clp,
8911 &data->args->la_seq_args,
8912 &data->res->lr_seq_res,
8917 * Called from nfs4_state_manager thread for session setup, so don't recover
8918 * from sequence operation or clientid errors.
8920 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8922 struct nfs4_get_lease_time_data *data =
8923 (struct nfs4_get_lease_time_data *)calldata;
8925 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8927 switch (task->tk_status) {
8928 case -NFS4ERR_DELAY:
8929 case -NFS4ERR_GRACE:
8930 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8931 task->tk_status = 0;
8933 case -NFS4ERR_RETRY_UNCACHED_REP:
8934 rpc_restart_call_prepare(task);
8939 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8940 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8941 .rpc_call_done = nfs4_get_lease_time_done,
8944 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8946 struct nfs4_get_lease_time_args args;
8947 struct nfs4_get_lease_time_res res = {
8948 .lr_fsinfo = fsinfo,
8950 struct nfs4_get_lease_time_data data = {
8955 struct rpc_message msg = {
8956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8960 struct rpc_task_setup task_setup = {
8961 .rpc_client = clp->cl_rpcclient,
8962 .rpc_message = &msg,
8963 .callback_ops = &nfs4_get_lease_time_ops,
8964 .callback_data = &data,
8965 .flags = RPC_TASK_TIMEOUT,
8968 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8969 return nfs4_call_sync_custom(&task_setup);
8972 #ifdef CONFIG_NFS_V4_1
8975 * Initialize the values to be used by the client in CREATE_SESSION
8976 * If nfs4_init_session set the fore channel request and response sizes,
8979 * Set the back channel max_resp_sz_cached to zero to force the client to
8980 * always set csa_cachethis to FALSE because the current implementation
8981 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8983 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8984 struct rpc_clnt *clnt)
8986 unsigned int max_rqst_sz, max_resp_sz;
8987 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8988 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8990 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8991 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8993 /* Fore channel attributes */
8994 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8995 args->fc_attrs.max_resp_sz = max_resp_sz;
8996 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8997 args->fc_attrs.max_reqs = max_session_slots;
8999 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
9000 "max_ops=%u max_reqs=%u\n",
9002 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
9003 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
9005 /* Back channel attributes */
9006 args->bc_attrs.max_rqst_sz = max_bc_payload;
9007 args->bc_attrs.max_resp_sz = max_bc_payload;
9008 args->bc_attrs.max_resp_sz_cached = 0;
9009 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
9010 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
9011 if (args->bc_attrs.max_reqs > max_bc_slots)
9012 args->bc_attrs.max_reqs = max_bc_slots;
9014 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
9015 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
9017 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
9018 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
9019 args->bc_attrs.max_reqs);
9022 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
9023 struct nfs41_create_session_res *res)
9025 struct nfs4_channel_attrs *sent = &args->fc_attrs;
9026 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
9028 if (rcvd->max_resp_sz > sent->max_resp_sz)
9031 * Our requested max_ops is the minimum we need; we're not
9032 * prepared to break up compounds into smaller pieces than that.
9033 * So, no point even trying to continue if the server won't
9036 if (rcvd->max_ops < sent->max_ops)
9038 if (rcvd->max_reqs == 0)
9040 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
9041 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
9045 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
9046 struct nfs41_create_session_res *res)
9048 struct nfs4_channel_attrs *sent = &args->bc_attrs;
9049 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
9051 if (!(res->flags & SESSION4_BACK_CHAN))
9053 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
9055 if (rcvd->max_resp_sz < sent->max_resp_sz)
9057 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
9059 if (rcvd->max_ops > sent->max_ops)
9061 if (rcvd->max_reqs > sent->max_reqs)
9067 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
9068 struct nfs41_create_session_res *res)
9072 ret = nfs4_verify_fore_channel_attrs(args, res);
9075 return nfs4_verify_back_channel_attrs(args, res);
9078 static void nfs4_update_session(struct nfs4_session *session,
9079 struct nfs41_create_session_res *res)
9081 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
9082 /* Mark client id and session as being confirmed */
9083 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
9084 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
9085 session->flags = res->flags;
9086 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
9087 if (res->flags & SESSION4_BACK_CHAN)
9088 memcpy(&session->bc_attrs, &res->bc_attrs,
9089 sizeof(session->bc_attrs));
9092 static int _nfs4_proc_create_session(struct nfs_client *clp,
9093 const struct cred *cred)
9095 struct nfs4_session *session = clp->cl_session;
9096 struct nfs41_create_session_args args = {
9098 .clientid = clp->cl_clientid,
9099 .seqid = clp->cl_seqid,
9100 .cb_program = NFS4_CALLBACK,
9102 struct nfs41_create_session_res res;
9104 struct rpc_message msg = {
9105 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9112 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
9113 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
9115 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9116 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9117 trace_nfs4_create_session(clp, status);
9120 case -NFS4ERR_STALE_CLIENTID:
9121 case -NFS4ERR_DELAY:
9130 /* Verify the session's negotiated channel_attrs values */
9131 status = nfs4_verify_channel_attrs(&args, &res);
9132 /* Increment the clientid slot sequence id */
9135 nfs4_update_session(session, &res);
9142 * Issues a CREATE_SESSION operation to the server.
9143 * It is the responsibility of the caller to verify the session is
9144 * expired before calling this routine.
9146 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9150 struct nfs4_session *session = clp->cl_session;
9152 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9154 status = _nfs4_proc_create_session(clp, cred);
9158 /* Init or reset the session slot tables */
9159 status = nfs4_setup_session_slot_tables(session);
9160 dprintk("slot table setup returned %d\n", status);
9164 ptr = (unsigned *)&session->sess_id.data[0];
9165 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9166 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9172 * Issue the over-the-wire RPC DESTROY_SESSION.
9173 * The caller must serialize access to this routine.
9175 int nfs4_proc_destroy_session(struct nfs4_session *session,
9176 const struct cred *cred)
9178 struct rpc_message msg = {
9179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9180 .rpc_argp = session,
9185 /* session is still being setup */
9186 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9189 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9190 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9191 trace_nfs4_destroy_session(session->clp, status);
9194 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9195 "Session has been destroyed regardless...\n", status);
9200 * Renew the cl_session lease.
9202 struct nfs4_sequence_data {
9203 struct nfs_client *clp;
9204 struct nfs4_sequence_args args;
9205 struct nfs4_sequence_res res;
9208 static void nfs41_sequence_release(void *data)
9210 struct nfs4_sequence_data *calldata = data;
9211 struct nfs_client *clp = calldata->clp;
9213 if (refcount_read(&clp->cl_count) > 1)
9214 nfs4_schedule_state_renewal(clp);
9215 nfs_put_client(clp);
9219 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9221 switch(task->tk_status) {
9222 case -NFS4ERR_DELAY:
9223 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9226 nfs4_schedule_lease_recovery(clp);
9231 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9233 struct nfs4_sequence_data *calldata = data;
9234 struct nfs_client *clp = calldata->clp;
9236 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9239 trace_nfs4_sequence(clp, task->tk_status);
9240 if (task->tk_status < 0) {
9241 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9242 if (refcount_read(&clp->cl_count) == 1)
9245 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9246 rpc_restart_call_prepare(task);
9250 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9253 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9255 struct nfs4_sequence_data *calldata = data;
9256 struct nfs_client *clp = calldata->clp;
9257 struct nfs4_sequence_args *args;
9258 struct nfs4_sequence_res *res;
9260 args = task->tk_msg.rpc_argp;
9261 res = task->tk_msg.rpc_resp;
9263 nfs4_setup_sequence(clp, args, res, task);
9266 static const struct rpc_call_ops nfs41_sequence_ops = {
9267 .rpc_call_done = nfs41_sequence_call_done,
9268 .rpc_call_prepare = nfs41_sequence_prepare,
9269 .rpc_release = nfs41_sequence_release,
9272 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9273 const struct cred *cred,
9274 struct nfs4_slot *slot,
9277 struct nfs4_sequence_data *calldata;
9278 struct rpc_message msg = {
9279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9282 struct rpc_task_setup task_setup_data = {
9283 .rpc_client = clp->cl_rpcclient,
9284 .rpc_message = &msg,
9285 .callback_ops = &nfs41_sequence_ops,
9286 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
9288 struct rpc_task *ret;
9290 ret = ERR_PTR(-EIO);
9291 if (!refcount_inc_not_zero(&clp->cl_count))
9294 ret = ERR_PTR(-ENOMEM);
9295 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9296 if (calldata == NULL)
9298 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9299 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9300 msg.rpc_argp = &calldata->args;
9301 msg.rpc_resp = &calldata->res;
9302 calldata->clp = clp;
9303 task_setup_data.callback_data = calldata;
9305 ret = rpc_run_task(&task_setup_data);
9310 nfs_put_client(clp);
9312 nfs41_release_slot(slot);
9316 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9318 struct rpc_task *task;
9321 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9323 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9325 ret = PTR_ERR(task);
9327 rpc_put_task_async(task);
9328 dprintk("<-- %s status=%d\n", __func__, ret);
9332 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9334 struct rpc_task *task;
9337 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9339 ret = PTR_ERR(task);
9342 ret = rpc_wait_for_completion_task(task);
9344 ret = task->tk_status;
9347 dprintk("<-- %s status=%d\n", __func__, ret);
9351 struct nfs4_reclaim_complete_data {
9352 struct nfs_client *clp;
9353 struct nfs41_reclaim_complete_args arg;
9354 struct nfs41_reclaim_complete_res res;
9357 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9359 struct nfs4_reclaim_complete_data *calldata = data;
9361 nfs4_setup_sequence(calldata->clp,
9362 &calldata->arg.seq_args,
9363 &calldata->res.seq_res,
9367 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9369 switch(task->tk_status) {
9371 wake_up_all(&clp->cl_lock_waitq);
9373 case -NFS4ERR_COMPLETE_ALREADY:
9374 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9376 case -NFS4ERR_DELAY:
9377 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9379 case -NFS4ERR_RETRY_UNCACHED_REP:
9381 case -NFS4ERR_BADSESSION:
9382 case -NFS4ERR_DEADSESSION:
9383 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9386 nfs4_schedule_lease_recovery(clp);
9391 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9393 struct nfs4_reclaim_complete_data *calldata = data;
9394 struct nfs_client *clp = calldata->clp;
9395 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9397 if (!nfs41_sequence_done(task, res))
9400 trace_nfs4_reclaim_complete(clp, task->tk_status);
9401 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9402 rpc_restart_call_prepare(task);
9407 static void nfs4_free_reclaim_complete_data(void *data)
9409 struct nfs4_reclaim_complete_data *calldata = data;
9414 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9415 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9416 .rpc_call_done = nfs4_reclaim_complete_done,
9417 .rpc_release = nfs4_free_reclaim_complete_data,
9421 * Issue a global reclaim complete.
9423 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9424 const struct cred *cred)
9426 struct nfs4_reclaim_complete_data *calldata;
9427 struct rpc_message msg = {
9428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9431 struct rpc_task_setup task_setup_data = {
9432 .rpc_client = clp->cl_rpcclient,
9433 .rpc_message = &msg,
9434 .callback_ops = &nfs4_reclaim_complete_call_ops,
9435 .flags = RPC_TASK_NO_ROUND_ROBIN,
9437 int status = -ENOMEM;
9439 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9440 if (calldata == NULL)
9442 calldata->clp = clp;
9443 calldata->arg.one_fs = 0;
9445 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9446 msg.rpc_argp = &calldata->arg;
9447 msg.rpc_resp = &calldata->res;
9448 task_setup_data.callback_data = calldata;
9449 status = nfs4_call_sync_custom(&task_setup_data);
9451 dprintk("<-- %s status=%d\n", __func__, status);
9456 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9458 struct nfs4_layoutget *lgp = calldata;
9459 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9461 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9462 &lgp->res.seq_res, task);
9465 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9467 struct nfs4_layoutget *lgp = calldata;
9469 nfs41_sequence_process(task, &lgp->res.seq_res);
9473 nfs4_layoutget_handle_exception(struct rpc_task *task,
9474 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9476 struct inode *inode = lgp->args.inode;
9477 struct nfs_server *server = NFS_SERVER(inode);
9478 struct pnfs_layout_hdr *lo = lgp->lo;
9479 int nfs4err = task->tk_status;
9480 int err, status = 0;
9483 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9485 nfs4_sequence_free_slot(&lgp->res.seq_res);
9492 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9493 * on the file. set tk_status to -ENODATA to tell upper layer to
9496 case -NFS4ERR_LAYOUTUNAVAILABLE:
9500 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9501 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9503 case -NFS4ERR_BADLAYOUT:
9504 status = -EOVERFLOW;
9507 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9508 * (or clients) writing to the same RAID stripe except when
9509 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9511 * Treat it like we would RECALLCONFLICT -- we retry for a little
9512 * while, and then eventually give up.
9514 case -NFS4ERR_LAYOUTTRYLATER:
9515 if (lgp->args.minlength == 0) {
9516 status = -EOVERFLOW;
9521 case -NFS4ERR_RECALLCONFLICT:
9522 status = -ERECALLCONFLICT;
9524 case -NFS4ERR_DELEG_REVOKED:
9525 case -NFS4ERR_ADMIN_REVOKED:
9526 case -NFS4ERR_EXPIRED:
9527 case -NFS4ERR_BAD_STATEID:
9528 exception->timeout = 0;
9529 spin_lock(&inode->i_lock);
9530 /* If the open stateid was bad, then recover it. */
9531 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9532 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9533 spin_unlock(&inode->i_lock);
9534 exception->state = lgp->args.ctx->state;
9535 exception->stateid = &lgp->args.stateid;
9540 * Mark the bad layout state as invalid, then retry
9542 pnfs_mark_layout_stateid_invalid(lo, &head);
9543 spin_unlock(&inode->i_lock);
9544 nfs_commit_inode(inode, 0);
9545 pnfs_free_lseg_list(&head);
9550 err = nfs4_handle_exception(server, nfs4err, exception);
9552 if (exception->retry)
9561 size_t max_response_pages(struct nfs_server *server)
9563 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9564 return nfs_page_array_len(0, max_resp_sz);
9567 static void nfs4_layoutget_release(void *calldata)
9569 struct nfs4_layoutget *lgp = calldata;
9571 nfs4_sequence_free_slot(&lgp->res.seq_res);
9572 pnfs_layoutget_free(lgp);
9575 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9576 .rpc_call_prepare = nfs4_layoutget_prepare,
9577 .rpc_call_done = nfs4_layoutget_done,
9578 .rpc_release = nfs4_layoutget_release,
9581 struct pnfs_layout_segment *
9582 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9584 struct inode *inode = lgp->args.inode;
9585 struct nfs_server *server = NFS_SERVER(inode);
9586 struct rpc_task *task;
9587 struct rpc_message msg = {
9588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9589 .rpc_argp = &lgp->args,
9590 .rpc_resp = &lgp->res,
9591 .rpc_cred = lgp->cred,
9593 struct rpc_task_setup task_setup_data = {
9594 .rpc_client = server->client,
9595 .rpc_message = &msg,
9596 .callback_ops = &nfs4_layoutget_call_ops,
9597 .callback_data = lgp,
9598 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
9601 struct pnfs_layout_segment *lseg = NULL;
9602 struct nfs4_exception exception = {
9604 .timeout = *timeout,
9608 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9610 task = rpc_run_task(&task_setup_data);
9612 status = rpc_wait_for_completion_task(task);
9616 if (task->tk_status < 0) {
9617 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9618 *timeout = exception.timeout;
9619 } else if (lgp->res.layoutp->len == 0) {
9621 *timeout = nfs4_update_delay(&exception.timeout);
9623 lseg = pnfs_layout_process(lgp);
9625 trace_nfs4_layoutget(lgp->args.ctx,
9632 dprintk("<-- %s status=%d\n", __func__, status);
9634 return ERR_PTR(status);
9639 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9641 struct nfs4_layoutreturn *lrp = calldata;
9643 nfs4_setup_sequence(lrp->clp,
9644 &lrp->args.seq_args,
9647 if (!pnfs_layout_is_valid(lrp->args.layout))
9651 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9653 struct nfs4_layoutreturn *lrp = calldata;
9654 struct nfs_server *server;
9656 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9660 * Was there an RPC level error? Assume the call succeeded,
9661 * and that we need to release the layout
9663 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9664 lrp->res.lrs_present = 0;
9668 server = NFS_SERVER(lrp->args.inode);
9669 switch (task->tk_status) {
9670 case -NFS4ERR_OLD_STATEID:
9671 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9677 task->tk_status = 0;
9681 case -NFS4ERR_DELAY:
9682 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9688 task->tk_status = 0;
9689 nfs4_sequence_free_slot(&lrp->res.seq_res);
9690 rpc_restart_call_prepare(task);
9693 static void nfs4_layoutreturn_release(void *calldata)
9695 struct nfs4_layoutreturn *lrp = calldata;
9696 struct pnfs_layout_hdr *lo = lrp->args.layout;
9698 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9699 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9700 nfs4_sequence_free_slot(&lrp->res.seq_res);
9701 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9702 lrp->ld_private.ops->free(&lrp->ld_private);
9703 pnfs_put_layout_hdr(lrp->args.layout);
9704 nfs_iput_and_deactive(lrp->inode);
9705 put_cred(lrp->cred);
9709 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9710 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9711 .rpc_call_done = nfs4_layoutreturn_done,
9712 .rpc_release = nfs4_layoutreturn_release,
9715 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9717 struct rpc_task *task;
9718 struct rpc_message msg = {
9719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9720 .rpc_argp = &lrp->args,
9721 .rpc_resp = &lrp->res,
9722 .rpc_cred = lrp->cred,
9724 struct rpc_task_setup task_setup_data = {
9725 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9726 .rpc_message = &msg,
9727 .callback_ops = &nfs4_layoutreturn_call_ops,
9728 .callback_data = lrp,
9729 .flags = RPC_TASK_MOVEABLE,
9733 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9734 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9735 &task_setup_data.rpc_client, &msg);
9737 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9740 nfs4_layoutreturn_release(lrp);
9743 task_setup_data.flags |= RPC_TASK_ASYNC;
9746 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9749 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9751 task = rpc_run_task(&task_setup_data);
9753 return PTR_ERR(task);
9755 status = task->tk_status;
9756 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9757 dprintk("<-- %s status=%d\n", __func__, status);
9763 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9764 struct pnfs_device *pdev,
9765 const struct cred *cred)
9767 struct nfs4_getdeviceinfo_args args = {
9769 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9770 NOTIFY_DEVICEID4_DELETE,
9772 struct nfs4_getdeviceinfo_res res = {
9775 struct rpc_message msg = {
9776 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9783 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9784 if (res.notification & ~args.notify_types)
9785 dprintk("%s: unsupported notification\n", __func__);
9786 if (res.notification != args.notify_types)
9789 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9791 dprintk("<-- %s status=%d\n", __func__, status);
9796 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9797 struct pnfs_device *pdev,
9798 const struct cred *cred)
9800 struct nfs4_exception exception = { };
9804 err = nfs4_handle_exception(server,
9805 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9807 } while (exception.retry);
9810 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9812 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9814 struct nfs4_layoutcommit_data *data = calldata;
9815 struct nfs_server *server = NFS_SERVER(data->args.inode);
9817 nfs4_setup_sequence(server->nfs_client,
9818 &data->args.seq_args,
9824 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9826 struct nfs4_layoutcommit_data *data = calldata;
9827 struct nfs_server *server = NFS_SERVER(data->args.inode);
9829 if (!nfs41_sequence_done(task, &data->res.seq_res))
9832 switch (task->tk_status) { /* Just ignore these failures */
9833 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9834 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9835 case -NFS4ERR_BADLAYOUT: /* no layout */
9836 case -NFS4ERR_GRACE: /* loca_recalim always false */
9837 task->tk_status = 0;
9842 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9843 rpc_restart_call_prepare(task);
9849 static void nfs4_layoutcommit_release(void *calldata)
9851 struct nfs4_layoutcommit_data *data = calldata;
9853 pnfs_cleanup_layoutcommit(data);
9854 nfs_post_op_update_inode_force_wcc(data->args.inode,
9856 put_cred(data->cred);
9857 nfs_iput_and_deactive(data->inode);
9861 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9862 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9863 .rpc_call_done = nfs4_layoutcommit_done,
9864 .rpc_release = nfs4_layoutcommit_release,
9868 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9870 struct rpc_message msg = {
9871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9872 .rpc_argp = &data->args,
9873 .rpc_resp = &data->res,
9874 .rpc_cred = data->cred,
9876 struct rpc_task_setup task_setup_data = {
9877 .task = &data->task,
9878 .rpc_client = NFS_CLIENT(data->args.inode),
9879 .rpc_message = &msg,
9880 .callback_ops = &nfs4_layoutcommit_ops,
9881 .callback_data = data,
9882 .flags = RPC_TASK_MOVEABLE,
9884 struct rpc_task *task;
9887 dprintk("NFS: initiating layoutcommit call. sync %d "
9888 "lbw: %llu inode %lu\n", sync,
9889 data->args.lastbytewritten,
9890 data->args.inode->i_ino);
9893 data->inode = nfs_igrab_and_active(data->args.inode);
9894 if (data->inode == NULL) {
9895 nfs4_layoutcommit_release(data);
9898 task_setup_data.flags = RPC_TASK_ASYNC;
9900 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9901 task = rpc_run_task(&task_setup_data);
9903 return PTR_ERR(task);
9905 status = task->tk_status;
9906 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9907 dprintk("%s: status %d\n", __func__, status);
9913 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9914 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9917 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9918 struct nfs_fsinfo *info,
9919 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9921 struct nfs41_secinfo_no_name_args args = {
9922 .style = SECINFO_STYLE_CURRENT_FH,
9924 struct nfs4_secinfo_res res = {
9927 struct rpc_message msg = {
9928 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9932 struct nfs4_call_sync_data data = {
9933 .seq_server = server,
9934 .seq_args = &args.seq_args,
9935 .seq_res = &res.seq_res,
9937 struct rpc_task_setup task_setup = {
9938 .rpc_client = server->client,
9939 .rpc_message = &msg,
9940 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9941 .callback_data = &data,
9942 .flags = RPC_TASK_NO_ROUND_ROBIN,
9944 const struct cred *cred = NULL;
9947 if (use_integrity) {
9948 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
9950 cred = nfs4_get_clid_cred(server->nfs_client);
9951 msg.rpc_cred = cred;
9954 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9955 status = nfs4_call_sync_custom(&task_setup);
9956 dprintk("<-- %s status=%d\n", __func__, status);
9964 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9965 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9967 struct nfs4_exception exception = {
9968 .interruptible = true,
9972 /* first try using integrity protection */
9973 err = -NFS4ERR_WRONGSEC;
9975 /* try to use integrity protection with machine cred */
9976 if (_nfs4_is_integrity_protected(server->nfs_client))
9977 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9981 * if unable to use integrity protection, or SECINFO with
9982 * integrity protection returns NFS4ERR_WRONGSEC (which is
9983 * disallowed by spec, but exists in deployed servers) use
9984 * the current filesystem's rpc_client and the user cred.
9986 if (err == -NFS4ERR_WRONGSEC)
9987 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9992 case -NFS4ERR_WRONGSEC:
9996 err = nfs4_handle_exception(server, err, &exception);
9998 } while (exception.retry);
10004 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
10005 struct nfs_fsinfo *info)
10009 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
10010 struct nfs4_secinfo_flavors *flavors;
10011 struct nfs4_secinfo4 *secinfo;
10014 page = alloc_page(GFP_KERNEL);
10020 flavors = page_address(page);
10021 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
10024 * Fall back on "guess and check" method if
10025 * the server doesn't support SECINFO_NO_NAME
10027 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
10028 err = nfs4_find_root_sec(server, fhandle, info);
10034 for (i = 0; i < flavors->num_flavors; i++) {
10035 secinfo = &flavors->flavors[i];
10037 switch (secinfo->flavor) {
10038 case RPC_AUTH_NULL:
10039 case RPC_AUTH_UNIX:
10041 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
10042 &secinfo->flavor_info);
10045 flavor = RPC_AUTH_MAXFLAVOR;
10049 if (!nfs_auth_info_match(&server->auth_info, flavor))
10050 flavor = RPC_AUTH_MAXFLAVOR;
10052 if (flavor != RPC_AUTH_MAXFLAVOR) {
10053 err = nfs4_lookup_root_sec(server, fhandle,
10060 if (flavor == RPC_AUTH_MAXFLAVOR)
10065 if (err == -EACCES)
10071 static int _nfs41_test_stateid(struct nfs_server *server,
10072 nfs4_stateid *stateid,
10073 const struct cred *cred)
10076 struct nfs41_test_stateid_args args = {
10077 .stateid = stateid,
10079 struct nfs41_test_stateid_res res;
10080 struct rpc_message msg = {
10081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10086 struct rpc_clnt *rpc_client = server->client;
10088 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10089 &rpc_client, &msg);
10091 dprintk("NFS call test_stateid %p\n", stateid);
10092 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
10093 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
10094 &args.seq_args, &res.seq_res);
10095 if (status != NFS_OK) {
10096 dprintk("NFS reply test_stateid: failed, %d\n", status);
10099 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10100 return -res.status;
10103 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10104 int err, struct nfs4_exception *exception)
10106 exception->retry = 0;
10108 case -NFS4ERR_DELAY:
10109 case -NFS4ERR_RETRY_UNCACHED_REP:
10110 nfs4_handle_exception(server, err, exception);
10112 case -NFS4ERR_BADSESSION:
10113 case -NFS4ERR_BADSLOT:
10114 case -NFS4ERR_BAD_HIGH_SLOT:
10115 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10116 case -NFS4ERR_DEADSESSION:
10117 nfs4_do_handle_exception(server, err, exception);
10122 * nfs41_test_stateid - perform a TEST_STATEID operation
10124 * @server: server / transport on which to perform the operation
10125 * @stateid: state ID to test
10126 * @cred: credential
10128 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10129 * Otherwise a negative NFS4ERR value is returned if the operation
10130 * failed or the state ID is not currently valid.
10132 static int nfs41_test_stateid(struct nfs_server *server,
10133 nfs4_stateid *stateid,
10134 const struct cred *cred)
10136 struct nfs4_exception exception = {
10137 .interruptible = true,
10141 err = _nfs41_test_stateid(server, stateid, cred);
10142 nfs4_handle_delay_or_session_error(server, err, &exception);
10143 } while (exception.retry);
10147 struct nfs_free_stateid_data {
10148 struct nfs_server *server;
10149 struct nfs41_free_stateid_args args;
10150 struct nfs41_free_stateid_res res;
10153 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10155 struct nfs_free_stateid_data *data = calldata;
10156 nfs4_setup_sequence(data->server->nfs_client,
10157 &data->args.seq_args,
10158 &data->res.seq_res,
10162 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10164 struct nfs_free_stateid_data *data = calldata;
10166 nfs41_sequence_done(task, &data->res.seq_res);
10168 switch (task->tk_status) {
10169 case -NFS4ERR_DELAY:
10170 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10171 rpc_restart_call_prepare(task);
10175 static void nfs41_free_stateid_release(void *calldata)
10177 struct nfs_free_stateid_data *data = calldata;
10178 struct nfs_client *clp = data->server->nfs_client;
10180 nfs_put_client(clp);
10184 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10185 .rpc_call_prepare = nfs41_free_stateid_prepare,
10186 .rpc_call_done = nfs41_free_stateid_done,
10187 .rpc_release = nfs41_free_stateid_release,
10191 * nfs41_free_stateid - perform a FREE_STATEID operation
10193 * @server: server / transport on which to perform the operation
10194 * @stateid: state ID to release
10195 * @cred: credential
10196 * @privileged: set to true if this call needs to be privileged
10198 * Note: this function is always asynchronous.
10200 static int nfs41_free_stateid(struct nfs_server *server,
10201 const nfs4_stateid *stateid,
10202 const struct cred *cred,
10205 struct rpc_message msg = {
10206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10209 struct rpc_task_setup task_setup = {
10210 .rpc_client = server->client,
10211 .rpc_message = &msg,
10212 .callback_ops = &nfs41_free_stateid_ops,
10213 .flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
10215 struct nfs_free_stateid_data *data;
10216 struct rpc_task *task;
10217 struct nfs_client *clp = server->nfs_client;
10219 if (!refcount_inc_not_zero(&clp->cl_count))
10222 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10223 &task_setup.rpc_client, &msg);
10225 dprintk("NFS call free_stateid %p\n", stateid);
10226 data = kmalloc(sizeof(*data), GFP_NOFS);
10229 data->server = server;
10230 nfs4_stateid_copy(&data->args.stateid, stateid);
10232 task_setup.callback_data = data;
10234 msg.rpc_argp = &data->args;
10235 msg.rpc_resp = &data->res;
10236 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10237 task = rpc_run_task(&task_setup);
10239 return PTR_ERR(task);
10240 rpc_put_task(task);
10245 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10247 const struct cred *cred = lsp->ls_state->owner->so_cred;
10249 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10250 nfs4_free_lock_state(server, lsp);
10253 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10254 const nfs4_stateid *s2)
10256 if (s1->type != s2->type)
10259 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10262 if (s1->seqid == s2->seqid)
10265 return s1->seqid == 0 || s2->seqid == 0;
10268 #endif /* CONFIG_NFS_V4_1 */
10270 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10271 const nfs4_stateid *s2)
10273 return nfs4_stateid_match(s1, s2);
10277 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10278 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10279 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10280 .recover_open = nfs4_open_reclaim,
10281 .recover_lock = nfs4_lock_reclaim,
10282 .establish_clid = nfs4_init_clientid,
10283 .detect_trunking = nfs40_discover_server_trunking,
10286 #if defined(CONFIG_NFS_V4_1)
10287 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10288 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10289 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10290 .recover_open = nfs4_open_reclaim,
10291 .recover_lock = nfs4_lock_reclaim,
10292 .establish_clid = nfs41_init_clientid,
10293 .reclaim_complete = nfs41_proc_reclaim_complete,
10294 .detect_trunking = nfs41_discover_server_trunking,
10296 #endif /* CONFIG_NFS_V4_1 */
10298 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10299 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10300 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10301 .recover_open = nfs40_open_expired,
10302 .recover_lock = nfs4_lock_expired,
10303 .establish_clid = nfs4_init_clientid,
10306 #if defined(CONFIG_NFS_V4_1)
10307 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10308 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10309 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10310 .recover_open = nfs41_open_expired,
10311 .recover_lock = nfs41_lock_expired,
10312 .establish_clid = nfs41_init_clientid,
10314 #endif /* CONFIG_NFS_V4_1 */
10316 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10317 .sched_state_renewal = nfs4_proc_async_renew,
10318 .get_state_renewal_cred = nfs4_get_renew_cred,
10319 .renew_lease = nfs4_proc_renew,
10322 #if defined(CONFIG_NFS_V4_1)
10323 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10324 .sched_state_renewal = nfs41_proc_async_sequence,
10325 .get_state_renewal_cred = nfs4_get_machine_cred,
10326 .renew_lease = nfs4_proc_sequence,
10330 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10331 .get_locations = _nfs40_proc_get_locations,
10332 .fsid_present = _nfs40_proc_fsid_present,
10335 #if defined(CONFIG_NFS_V4_1)
10336 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10337 .get_locations = _nfs41_proc_get_locations,
10338 .fsid_present = _nfs41_proc_fsid_present,
10340 #endif /* CONFIG_NFS_V4_1 */
10342 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10343 .minor_version = 0,
10344 .init_caps = NFS_CAP_READDIRPLUS
10345 | NFS_CAP_ATOMIC_OPEN
10346 | NFS_CAP_POSIX_LOCK,
10347 .init_client = nfs40_init_client,
10348 .shutdown_client = nfs40_shutdown_client,
10349 .match_stateid = nfs4_match_stateid,
10350 .find_root_sec = nfs4_find_root_sec,
10351 .free_lock_state = nfs4_release_lockowner,
10352 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10353 .alloc_seqid = nfs_alloc_seqid,
10354 .call_sync_ops = &nfs40_call_sync_ops,
10355 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10356 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10357 .state_renewal_ops = &nfs40_state_renewal_ops,
10358 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10361 #if defined(CONFIG_NFS_V4_1)
10362 static struct nfs_seqid *
10363 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10368 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10369 .minor_version = 1,
10370 .init_caps = NFS_CAP_READDIRPLUS
10371 | NFS_CAP_ATOMIC_OPEN
10372 | NFS_CAP_POSIX_LOCK
10373 | NFS_CAP_STATEID_NFSV41
10374 | NFS_CAP_ATOMIC_OPEN_V1
10376 .init_client = nfs41_init_client,
10377 .shutdown_client = nfs41_shutdown_client,
10378 .match_stateid = nfs41_match_stateid,
10379 .find_root_sec = nfs41_find_root_sec,
10380 .free_lock_state = nfs41_free_lock_state,
10381 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10382 .alloc_seqid = nfs_alloc_no_seqid,
10383 .session_trunk = nfs4_test_session_trunk,
10384 .call_sync_ops = &nfs41_call_sync_ops,
10385 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10386 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10387 .state_renewal_ops = &nfs41_state_renewal_ops,
10388 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10392 #if defined(CONFIG_NFS_V4_2)
10393 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10394 .minor_version = 2,
10395 .init_caps = NFS_CAP_READDIRPLUS
10396 | NFS_CAP_ATOMIC_OPEN
10397 | NFS_CAP_POSIX_LOCK
10398 | NFS_CAP_STATEID_NFSV41
10399 | NFS_CAP_ATOMIC_OPEN_V1
10403 | NFS_CAP_OFFLOAD_CANCEL
10404 | NFS_CAP_COPY_NOTIFY
10405 | NFS_CAP_DEALLOCATE
10407 | NFS_CAP_LAYOUTSTATS
10409 | NFS_CAP_LAYOUTERROR
10410 | NFS_CAP_READ_PLUS,
10411 .init_client = nfs41_init_client,
10412 .shutdown_client = nfs41_shutdown_client,
10413 .match_stateid = nfs41_match_stateid,
10414 .find_root_sec = nfs41_find_root_sec,
10415 .free_lock_state = nfs41_free_lock_state,
10416 .call_sync_ops = &nfs41_call_sync_ops,
10417 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10418 .alloc_seqid = nfs_alloc_no_seqid,
10419 .session_trunk = nfs4_test_session_trunk,
10420 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10421 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10422 .state_renewal_ops = &nfs41_state_renewal_ops,
10423 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10427 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10428 [0] = &nfs_v4_0_minor_ops,
10429 #if defined(CONFIG_NFS_V4_1)
10430 [1] = &nfs_v4_1_minor_ops,
10432 #if defined(CONFIG_NFS_V4_2)
10433 [2] = &nfs_v4_2_minor_ops,
10437 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10439 ssize_t error, error2, error3;
10441 error = generic_listxattr(dentry, list, size);
10449 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10458 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10462 return error + error2 + error3;
10465 static const struct inode_operations nfs4_dir_inode_operations = {
10466 .create = nfs_create,
10467 .lookup = nfs_lookup,
10468 .atomic_open = nfs_atomic_open,
10470 .unlink = nfs_unlink,
10471 .symlink = nfs_symlink,
10472 .mkdir = nfs_mkdir,
10473 .rmdir = nfs_rmdir,
10474 .mknod = nfs_mknod,
10475 .rename = nfs_rename,
10476 .permission = nfs_permission,
10477 .getattr = nfs_getattr,
10478 .setattr = nfs_setattr,
10479 .listxattr = nfs4_listxattr,
10482 static const struct inode_operations nfs4_file_inode_operations = {
10483 .permission = nfs_permission,
10484 .getattr = nfs_getattr,
10485 .setattr = nfs_setattr,
10486 .listxattr = nfs4_listxattr,
10489 const struct nfs_rpc_ops nfs_v4_clientops = {
10490 .version = 4, /* protocol version */
10491 .dentry_ops = &nfs4_dentry_operations,
10492 .dir_inode_ops = &nfs4_dir_inode_operations,
10493 .file_inode_ops = &nfs4_file_inode_operations,
10494 .file_ops = &nfs4_file_operations,
10495 .getroot = nfs4_proc_get_root,
10496 .submount = nfs4_submount,
10497 .try_get_tree = nfs4_try_get_tree,
10498 .getattr = nfs4_proc_getattr,
10499 .setattr = nfs4_proc_setattr,
10500 .lookup = nfs4_proc_lookup,
10501 .lookupp = nfs4_proc_lookupp,
10502 .access = nfs4_proc_access,
10503 .readlink = nfs4_proc_readlink,
10504 .create = nfs4_proc_create,
10505 .remove = nfs4_proc_remove,
10506 .unlink_setup = nfs4_proc_unlink_setup,
10507 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10508 .unlink_done = nfs4_proc_unlink_done,
10509 .rename_setup = nfs4_proc_rename_setup,
10510 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10511 .rename_done = nfs4_proc_rename_done,
10512 .link = nfs4_proc_link,
10513 .symlink = nfs4_proc_symlink,
10514 .mkdir = nfs4_proc_mkdir,
10515 .rmdir = nfs4_proc_rmdir,
10516 .readdir = nfs4_proc_readdir,
10517 .mknod = nfs4_proc_mknod,
10518 .statfs = nfs4_proc_statfs,
10519 .fsinfo = nfs4_proc_fsinfo,
10520 .pathconf = nfs4_proc_pathconf,
10521 .set_capabilities = nfs4_server_capabilities,
10522 .decode_dirent = nfs4_decode_dirent,
10523 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10524 .read_setup = nfs4_proc_read_setup,
10525 .read_done = nfs4_read_done,
10526 .write_setup = nfs4_proc_write_setup,
10527 .write_done = nfs4_write_done,
10528 .commit_setup = nfs4_proc_commit_setup,
10529 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10530 .commit_done = nfs4_commit_done,
10531 .lock = nfs4_proc_lock,
10532 .clear_acl_cache = nfs4_zap_acl_attr,
10533 .close_context = nfs4_close_context,
10534 .open_context = nfs4_atomic_open,
10535 .have_delegation = nfs4_have_delegation,
10536 .alloc_client = nfs4_alloc_client,
10537 .init_client = nfs4_init_client,
10538 .free_client = nfs4_free_client,
10539 .create_server = nfs4_create_server,
10540 .clone_server = nfs_clone_server,
10541 .discover_trunking = nfs4_discover_trunking,
10544 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10545 .name = XATTR_NAME_NFSV4_ACL,
10546 .list = nfs4_xattr_list_nfs4_acl,
10547 .get = nfs4_xattr_get_nfs4_acl,
10548 .set = nfs4_xattr_set_nfs4_acl,
10551 #ifdef CONFIG_NFS_V4_2
10552 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10553 .prefix = XATTR_USER_PREFIX,
10554 .get = nfs4_xattr_get_nfs4_user,
10555 .set = nfs4_xattr_set_nfs4_user,
10559 const struct xattr_handler *nfs4_xattr_handlers[] = {
10560 &nfs4_xattr_nfs4_acl_handler,
10561 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10562 &nfs4_xattr_nfs4_label_handler,
10564 #ifdef CONFIG_NFS_V4_2
10565 &nfs4_xattr_nfs4_user_handler,