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 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1235 nfsi->attrtimeo_timestamp = jiffies;
1236 nfsi->read_cache_jiffies = timestamp;
1237 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1238 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1240 nfs_set_cache_invalid(inode, cache_validity);
1244 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1245 unsigned long timestamp, unsigned long cache_validity)
1247 spin_lock(&dir->i_lock);
1248 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1249 spin_unlock(&dir->i_lock);
1252 struct nfs4_open_createattrs {
1253 struct nfs4_label *label;
1254 struct iattr *sattr;
1255 const __u32 verf[2];
1258 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1259 int err, struct nfs4_exception *exception)
1263 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1265 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1266 exception->retry = 1;
1270 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1272 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1275 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1277 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1279 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1283 nfs4_map_atomic_open_share(struct nfs_server *server,
1284 fmode_t fmode, int openflags)
1288 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1290 res = NFS4_SHARE_ACCESS_READ;
1293 res = NFS4_SHARE_ACCESS_WRITE;
1295 case FMODE_READ|FMODE_WRITE:
1296 res = NFS4_SHARE_ACCESS_BOTH;
1298 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1300 /* Want no delegation if we're using O_DIRECT */
1301 if (openflags & O_DIRECT)
1302 res |= NFS4_SHARE_WANT_NO_DELEG;
1307 static enum open_claim_type4
1308 nfs4_map_atomic_open_claim(struct nfs_server *server,
1309 enum open_claim_type4 claim)
1311 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1316 case NFS4_OPEN_CLAIM_FH:
1317 return NFS4_OPEN_CLAIM_NULL;
1318 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1319 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1320 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1321 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1325 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1327 p->o_res.f_attr = &p->f_attr;
1328 p->o_res.seqid = p->o_arg.seqid;
1329 p->c_res.seqid = p->c_arg.seqid;
1330 p->o_res.server = p->o_arg.server;
1331 p->o_res.access_request = p->o_arg.access;
1332 nfs_fattr_init(&p->f_attr);
1333 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1336 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1337 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1338 const struct nfs4_open_createattrs *c,
1339 enum open_claim_type4 claim,
1342 struct dentry *parent = dget_parent(dentry);
1343 struct inode *dir = d_inode(parent);
1344 struct nfs_server *server = NFS_SERVER(dir);
1345 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1346 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1347 struct nfs4_opendata *p;
1349 p = kzalloc(sizeof(*p), gfp_mask);
1353 p->f_attr.label = nfs4_label_alloc(server, gfp_mask);
1354 if (IS_ERR(p->f_attr.label))
1357 p->a_label = nfs4_label_alloc(server, gfp_mask);
1358 if (IS_ERR(p->a_label))
1361 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1362 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1363 if (IS_ERR(p->o_arg.seqid))
1364 goto err_free_label;
1365 nfs_sb_active(dentry->d_sb);
1366 p->dentry = dget(dentry);
1369 atomic_inc(&sp->so_count);
1370 p->o_arg.open_flags = flags;
1371 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1372 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1373 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1375 if (flags & O_CREAT) {
1376 p->o_arg.umask = current_umask();
1377 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1378 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1379 p->o_arg.u.attrs = &p->attrs;
1380 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1382 memcpy(p->o_arg.u.verifier.data, c->verf,
1383 sizeof(p->o_arg.u.verifier.data));
1386 /* ask server to check for all possible rights as results
1388 switch (p->o_arg.claim) {
1391 case NFS4_OPEN_CLAIM_NULL:
1392 case NFS4_OPEN_CLAIM_FH:
1393 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1394 NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE |
1395 NFS4_ACCESS_EXECUTE;
1396 #ifdef CONFIG_NFS_V4_2
1397 if (!(server->caps & NFS_CAP_XATTR))
1399 p->o_arg.access |= NFS4_ACCESS_XAREAD | NFS4_ACCESS_XAWRITE |
1403 p->o_arg.clientid = server->nfs_client->cl_clientid;
1404 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1405 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1406 p->o_arg.name = &dentry->d_name;
1407 p->o_arg.server = server;
1408 p->o_arg.bitmask = nfs4_bitmask(server, label);
1409 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1410 switch (p->o_arg.claim) {
1411 case NFS4_OPEN_CLAIM_NULL:
1412 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1413 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1414 p->o_arg.fh = NFS_FH(dir);
1416 case NFS4_OPEN_CLAIM_PREVIOUS:
1417 case NFS4_OPEN_CLAIM_FH:
1418 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1419 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1420 p->o_arg.fh = NFS_FH(d_inode(dentry));
1422 p->c_arg.fh = &p->o_res.fh;
1423 p->c_arg.stateid = &p->o_res.stateid;
1424 p->c_arg.seqid = p->o_arg.seqid;
1425 nfs4_init_opendata_res(p);
1426 kref_init(&p->kref);
1430 nfs4_label_free(p->a_label);
1432 nfs4_label_free(p->f_attr.label);
1440 static void nfs4_opendata_free(struct kref *kref)
1442 struct nfs4_opendata *p = container_of(kref,
1443 struct nfs4_opendata, kref);
1444 struct super_block *sb = p->dentry->d_sb;
1446 nfs4_lgopen_release(p->lgp);
1447 nfs_free_seqid(p->o_arg.seqid);
1448 nfs4_sequence_free_slot(&p->o_res.seq_res);
1449 if (p->state != NULL)
1450 nfs4_put_open_state(p->state);
1451 nfs4_put_state_owner(p->owner);
1453 nfs4_label_free(p->a_label);
1454 nfs4_label_free(p->f_attr.label);
1458 nfs_sb_deactive(sb);
1459 nfs_fattr_free_names(&p->f_attr);
1460 kfree(p->f_attr.mdsthreshold);
1464 static void nfs4_opendata_put(struct nfs4_opendata *p)
1467 kref_put(&p->kref, nfs4_opendata_free);
1470 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1473 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1474 case FMODE_READ|FMODE_WRITE:
1475 return state->n_rdwr != 0;
1477 return state->n_wronly != 0;
1479 return state->n_rdonly != 0;
1485 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1486 int open_mode, enum open_claim_type4 claim)
1490 if (open_mode & (O_EXCL|O_TRUNC))
1493 case NFS4_OPEN_CLAIM_NULL:
1494 case NFS4_OPEN_CLAIM_FH:
1499 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1501 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1502 && state->n_rdonly != 0;
1505 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1506 && state->n_wronly != 0;
1508 case FMODE_READ|FMODE_WRITE:
1509 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1510 && state->n_rdwr != 0;
1516 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1517 enum open_claim_type4 claim)
1519 if (delegation == NULL)
1521 if ((delegation->type & fmode) != fmode)
1524 case NFS4_OPEN_CLAIM_NULL:
1525 case NFS4_OPEN_CLAIM_FH:
1527 case NFS4_OPEN_CLAIM_PREVIOUS:
1528 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1534 nfs_mark_delegation_referenced(delegation);
1538 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1547 case FMODE_READ|FMODE_WRITE:
1550 nfs4_state_set_mode_locked(state, state->state | fmode);
1553 #ifdef CONFIG_NFS_V4_1
1554 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1556 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1558 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1560 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1564 #endif /* CONFIG_NFS_V4_1 */
1566 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1568 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1569 wake_up_all(&state->waitq);
1572 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1574 struct nfs_client *clp = state->owner->so_server->nfs_client;
1575 bool need_recover = false;
1577 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1578 need_recover = true;
1579 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1580 need_recover = true;
1581 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1582 need_recover = true;
1584 nfs4_state_mark_reclaim_nograce(clp, state);
1588 * Check for whether or not the caller may update the open stateid
1589 * to the value passed in by stateid.
1591 * Note: This function relies heavily on the server implementing
1592 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1594 * i.e. The stateid seqids have to be initialised to 1, and
1595 * are then incremented on every state transition.
1597 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1598 const nfs4_stateid *stateid)
1600 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1601 /* The common case - we're updating to a new sequence number */
1602 if (nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1603 if (nfs4_stateid_is_next(&state->open_stateid, stateid))
1607 /* The server returned a new stateid */
1609 /* This is the first OPEN in this generation */
1610 if (stateid->seqid == cpu_to_be32(1))
1615 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1617 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1619 if (state->n_wronly)
1620 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1621 if (state->n_rdonly)
1622 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1624 set_bit(NFS_O_RDWR_STATE, &state->flags);
1625 set_bit(NFS_OPEN_STATE, &state->flags);
1628 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1629 nfs4_stateid *stateid, fmode_t fmode)
1631 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1632 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1634 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1637 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1640 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1641 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1642 clear_bit(NFS_OPEN_STATE, &state->flags);
1644 if (stateid == NULL)
1646 /* Handle OPEN+OPEN_DOWNGRADE races */
1647 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1648 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1649 nfs_resync_open_stateid_locked(state);
1652 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1653 nfs4_stateid_copy(&state->stateid, stateid);
1654 nfs4_stateid_copy(&state->open_stateid, stateid);
1655 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1657 nfs_state_log_update_open_stateid(state);
1660 static void nfs_clear_open_stateid(struct nfs4_state *state,
1661 nfs4_stateid *arg_stateid,
1662 nfs4_stateid *stateid, fmode_t fmode)
1664 write_seqlock(&state->seqlock);
1665 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1666 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1667 nfs_clear_open_stateid_locked(state, stateid, fmode);
1668 write_sequnlock(&state->seqlock);
1669 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1670 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1673 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1674 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1675 __must_hold(&state->owner->so_lock)
1676 __must_hold(&state->seqlock)
1684 if (nfs_stateid_is_sequential(state, stateid))
1689 /* Rely on seqids for serialisation with NFSv4.0 */
1690 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1693 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1694 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1696 * Ensure we process the state changes in the same order
1697 * in which the server processed them by delaying the
1698 * update of the stateid until we are in sequence.
1700 write_sequnlock(&state->seqlock);
1701 spin_unlock(&state->owner->so_lock);
1703 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1705 if (!fatal_signal_pending(current)) {
1706 if (schedule_timeout(5*HZ) == 0)
1712 finish_wait(&state->waitq, &wait);
1714 spin_lock(&state->owner->so_lock);
1715 write_seqlock(&state->seqlock);
1718 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1719 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1720 nfs4_stateid_copy(freeme, &state->open_stateid);
1721 nfs_test_and_clear_all_open_stateid(state);
1724 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1725 nfs4_stateid_copy(&state->stateid, stateid);
1726 nfs4_stateid_copy(&state->open_stateid, stateid);
1727 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1728 nfs_state_log_update_open_stateid(state);
1731 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1732 const nfs4_stateid *open_stateid,
1734 nfs4_stateid *freeme)
1737 * Protect the call to nfs4_state_set_mode_locked and
1738 * serialise the stateid update
1740 write_seqlock(&state->seqlock);
1741 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1744 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1747 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1749 case FMODE_READ|FMODE_WRITE:
1750 set_bit(NFS_O_RDWR_STATE, &state->flags);
1752 set_bit(NFS_OPEN_STATE, &state->flags);
1753 write_sequnlock(&state->seqlock);
1756 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1758 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1759 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1760 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1761 clear_bit(NFS_OPEN_STATE, &state->flags);
1764 static void nfs_state_set_delegation(struct nfs4_state *state,
1765 const nfs4_stateid *deleg_stateid,
1769 * Protect the call to nfs4_state_set_mode_locked and
1770 * serialise the stateid update
1772 write_seqlock(&state->seqlock);
1773 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1774 set_bit(NFS_DELEGATED_STATE, &state->flags);
1775 write_sequnlock(&state->seqlock);
1778 static void nfs_state_clear_delegation(struct nfs4_state *state)
1780 write_seqlock(&state->seqlock);
1781 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1782 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1783 write_sequnlock(&state->seqlock);
1786 int update_open_stateid(struct nfs4_state *state,
1787 const nfs4_stateid *open_stateid,
1788 const nfs4_stateid *delegation,
1791 struct nfs_server *server = NFS_SERVER(state->inode);
1792 struct nfs_client *clp = server->nfs_client;
1793 struct nfs_inode *nfsi = NFS_I(state->inode);
1794 struct nfs_delegation *deleg_cur;
1795 nfs4_stateid freeme = { };
1798 fmode &= (FMODE_READ|FMODE_WRITE);
1801 spin_lock(&state->owner->so_lock);
1802 if (open_stateid != NULL) {
1803 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1807 deleg_cur = nfs4_get_valid_delegation(state->inode);
1808 if (deleg_cur == NULL)
1811 spin_lock(&deleg_cur->lock);
1812 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1813 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1814 (deleg_cur->type & fmode) != fmode)
1815 goto no_delegation_unlock;
1817 if (delegation == NULL)
1818 delegation = &deleg_cur->stateid;
1819 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1820 goto no_delegation_unlock;
1822 nfs_mark_delegation_referenced(deleg_cur);
1823 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1825 no_delegation_unlock:
1826 spin_unlock(&deleg_cur->lock);
1829 update_open_stateflags(state, fmode);
1830 spin_unlock(&state->owner->so_lock);
1833 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1834 nfs4_schedule_state_manager(clp);
1835 if (freeme.type != 0)
1836 nfs4_test_and_free_stateid(server, &freeme,
1837 state->owner->so_cred);
1842 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1843 const nfs4_stateid *stateid)
1845 struct nfs4_state *state = lsp->ls_state;
1848 spin_lock(&state->state_lock);
1849 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1851 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1853 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1856 spin_unlock(&state->state_lock);
1860 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1862 struct nfs_delegation *delegation;
1864 fmode &= FMODE_READ|FMODE_WRITE;
1866 delegation = nfs4_get_valid_delegation(inode);
1867 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1872 nfs4_inode_return_delegation(inode);
1875 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1877 struct nfs4_state *state = opendata->state;
1878 struct nfs_delegation *delegation;
1879 int open_mode = opendata->o_arg.open_flags;
1880 fmode_t fmode = opendata->o_arg.fmode;
1881 enum open_claim_type4 claim = opendata->o_arg.claim;
1882 nfs4_stateid stateid;
1886 spin_lock(&state->owner->so_lock);
1887 if (can_open_cached(state, fmode, open_mode, claim)) {
1888 update_open_stateflags(state, fmode);
1889 spin_unlock(&state->owner->so_lock);
1890 goto out_return_state;
1892 spin_unlock(&state->owner->so_lock);
1894 delegation = nfs4_get_valid_delegation(state->inode);
1895 if (!can_open_delegated(delegation, fmode, claim)) {
1899 /* Save the delegation */
1900 nfs4_stateid_copy(&stateid, &delegation->stateid);
1902 nfs_release_seqid(opendata->o_arg.seqid);
1903 if (!opendata->is_recover) {
1904 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1910 /* Try to update the stateid using the delegation */
1911 if (update_open_stateid(state, NULL, &stateid, fmode))
1912 goto out_return_state;
1915 return ERR_PTR(ret);
1917 refcount_inc(&state->count);
1922 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1924 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1925 struct nfs_delegation *delegation;
1926 int delegation_flags = 0;
1929 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1931 delegation_flags = delegation->flags;
1933 switch (data->o_arg.claim) {
1936 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1937 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1938 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1939 "returning a delegation for "
1940 "OPEN(CLAIM_DELEGATE_CUR)\n",
1944 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1945 nfs_inode_set_delegation(state->inode,
1946 data->owner->so_cred,
1947 data->o_res.delegation_type,
1948 &data->o_res.delegation,
1949 data->o_res.pagemod_limit);
1951 nfs_inode_reclaim_delegation(state->inode,
1952 data->owner->so_cred,
1953 data->o_res.delegation_type,
1954 &data->o_res.delegation,
1955 data->o_res.pagemod_limit);
1957 if (data->o_res.do_recall)
1958 nfs_async_inode_return_delegation(state->inode,
1959 &data->o_res.delegation);
1963 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1964 * and update the nfs4_state.
1966 static struct nfs4_state *
1967 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1969 struct inode *inode = data->state->inode;
1970 struct nfs4_state *state = data->state;
1973 if (!data->rpc_done) {
1974 if (data->rpc_status)
1975 return ERR_PTR(data->rpc_status);
1976 /* cached opens have already been processed */
1980 ret = nfs_refresh_inode(inode, &data->f_attr);
1982 return ERR_PTR(ret);
1984 if (data->o_res.delegation_type != 0)
1985 nfs4_opendata_check_deleg(data, state);
1987 if (!update_open_stateid(state, &data->o_res.stateid,
1988 NULL, data->o_arg.fmode))
1989 return ERR_PTR(-EAGAIN);
1990 refcount_inc(&state->count);
1995 static struct inode *
1996 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1998 struct inode *inode;
2000 switch (data->o_arg.claim) {
2001 case NFS4_OPEN_CLAIM_NULL:
2002 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2003 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2004 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2005 return ERR_PTR(-EAGAIN);
2006 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2010 inode = d_inode(data->dentry);
2012 nfs_refresh_inode(inode, &data->f_attr);
2017 static struct nfs4_state *
2018 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2020 struct nfs4_state *state;
2021 struct inode *inode;
2023 inode = nfs4_opendata_get_inode(data);
2025 return ERR_CAST(inode);
2026 if (data->state != NULL && data->state->inode == inode) {
2027 state = data->state;
2028 refcount_inc(&state->count);
2030 state = nfs4_get_open_state(inode, data->owner);
2033 state = ERR_PTR(-ENOMEM);
2037 static struct nfs4_state *
2038 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2040 struct nfs4_state *state;
2042 if (!data->rpc_done) {
2043 state = nfs4_try_open_cached(data);
2044 trace_nfs4_cached_open(data->state);
2048 state = nfs4_opendata_find_nfs4_state(data);
2052 if (data->o_res.delegation_type != 0)
2053 nfs4_opendata_check_deleg(data, state);
2054 if (!update_open_stateid(state, &data->o_res.stateid,
2055 NULL, data->o_arg.fmode)) {
2056 nfs4_put_open_state(state);
2057 state = ERR_PTR(-EAGAIN);
2060 nfs_release_seqid(data->o_arg.seqid);
2064 static struct nfs4_state *
2065 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2067 struct nfs4_state *ret;
2069 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2070 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2072 ret = _nfs4_opendata_to_nfs4_state(data);
2073 nfs4_sequence_free_slot(&data->o_res.seq_res);
2077 static struct nfs_open_context *
2078 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2080 struct nfs_inode *nfsi = NFS_I(state->inode);
2081 struct nfs_open_context *ctx;
2084 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2085 if (ctx->state != state)
2087 if ((ctx->mode & mode) != mode)
2089 if (!get_nfs_open_context(ctx))
2095 return ERR_PTR(-ENOENT);
2098 static struct nfs_open_context *
2099 nfs4_state_find_open_context(struct nfs4_state *state)
2101 struct nfs_open_context *ctx;
2103 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2106 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2109 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2112 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2113 struct nfs4_state *state, enum open_claim_type4 claim)
2115 struct nfs4_opendata *opendata;
2117 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2118 NULL, claim, GFP_NOFS);
2119 if (opendata == NULL)
2120 return ERR_PTR(-ENOMEM);
2121 opendata->state = state;
2122 refcount_inc(&state->count);
2126 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2129 struct nfs4_state *newstate;
2132 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2134 opendata->o_arg.open_flags = 0;
2135 opendata->o_arg.fmode = fmode;
2136 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2137 NFS_SB(opendata->dentry->d_sb),
2139 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2140 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2141 nfs4_init_opendata_res(opendata);
2142 ret = _nfs4_recover_proc_open(opendata);
2145 newstate = nfs4_opendata_to_nfs4_state(opendata);
2146 if (IS_ERR(newstate))
2147 return PTR_ERR(newstate);
2148 if (newstate != opendata->state)
2150 nfs4_close_state(newstate, fmode);
2154 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2158 /* memory barrier prior to reading state->n_* */
2160 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2163 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2166 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2170 * We may have performed cached opens for all three recoveries.
2171 * Check if we need to update the current stateid.
2173 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2174 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2175 write_seqlock(&state->seqlock);
2176 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2177 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2178 write_sequnlock(&state->seqlock);
2185 * reclaim state on the server after a reboot.
2187 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2189 struct nfs_delegation *delegation;
2190 struct nfs4_opendata *opendata;
2191 fmode_t delegation_type = 0;
2194 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2195 NFS4_OPEN_CLAIM_PREVIOUS);
2196 if (IS_ERR(opendata))
2197 return PTR_ERR(opendata);
2199 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2200 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2201 delegation_type = delegation->type;
2203 opendata->o_arg.u.delegation_type = delegation_type;
2204 status = nfs4_open_recover(opendata, state);
2205 nfs4_opendata_put(opendata);
2209 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2211 struct nfs_server *server = NFS_SERVER(state->inode);
2212 struct nfs4_exception exception = { };
2215 err = _nfs4_do_open_reclaim(ctx, state);
2216 trace_nfs4_open_reclaim(ctx, 0, err);
2217 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2219 if (err != -NFS4ERR_DELAY)
2221 nfs4_handle_exception(server, err, &exception);
2222 } while (exception.retry);
2226 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2228 struct nfs_open_context *ctx;
2231 ctx = nfs4_state_find_open_context(state);
2234 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2235 nfs_state_clear_open_state_flags(state);
2236 ret = nfs4_do_open_reclaim(ctx, state);
2237 put_nfs_open_context(ctx);
2241 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)
2245 printk(KERN_ERR "NFS: %s: unhandled error "
2246 "%d.\n", __func__, err);
2254 case -NFS4ERR_BADSESSION:
2255 case -NFS4ERR_BADSLOT:
2256 case -NFS4ERR_BAD_HIGH_SLOT:
2257 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2258 case -NFS4ERR_DEADSESSION:
2260 case -NFS4ERR_STALE_CLIENTID:
2261 case -NFS4ERR_STALE_STATEID:
2262 /* Don't recall a delegation if it was lost */
2263 nfs4_schedule_lease_recovery(server->nfs_client);
2265 case -NFS4ERR_MOVED:
2266 nfs4_schedule_migration_recovery(server);
2268 case -NFS4ERR_LEASE_MOVED:
2269 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2271 case -NFS4ERR_DELEG_REVOKED:
2272 case -NFS4ERR_ADMIN_REVOKED:
2273 case -NFS4ERR_EXPIRED:
2274 case -NFS4ERR_BAD_STATEID:
2275 case -NFS4ERR_OPENMODE:
2276 nfs_inode_find_state_and_recover(state->inode,
2278 nfs4_schedule_stateid_recovery(server, state);
2280 case -NFS4ERR_DELAY:
2281 case -NFS4ERR_GRACE:
2285 case -NFS4ERR_DENIED:
2287 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2289 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2296 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2297 struct nfs4_state *state, const nfs4_stateid *stateid)
2299 struct nfs_server *server = NFS_SERVER(state->inode);
2300 struct nfs4_opendata *opendata;
2303 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2304 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2305 if (IS_ERR(opendata))
2306 return PTR_ERR(opendata);
2307 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2308 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2309 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2313 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2314 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2318 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2319 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2323 nfs_state_clear_delegation(state);
2325 nfs4_opendata_put(opendata);
2326 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2329 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2331 struct nfs4_opendata *data = calldata;
2333 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2334 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2337 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2339 struct nfs4_opendata *data = calldata;
2341 nfs40_sequence_done(task, &data->c_res.seq_res);
2343 data->rpc_status = task->tk_status;
2344 if (data->rpc_status == 0) {
2345 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2346 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2347 renew_lease(data->o_res.server, data->timestamp);
2348 data->rpc_done = true;
2352 static void nfs4_open_confirm_release(void *calldata)
2354 struct nfs4_opendata *data = calldata;
2355 struct nfs4_state *state = NULL;
2357 /* If this request hasn't been cancelled, do nothing */
2358 if (!data->cancelled)
2360 /* In case of error, no cleanup! */
2361 if (!data->rpc_done)
2363 state = nfs4_opendata_to_nfs4_state(data);
2365 nfs4_close_state(state, data->o_arg.fmode);
2367 nfs4_opendata_put(data);
2370 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2371 .rpc_call_prepare = nfs4_open_confirm_prepare,
2372 .rpc_call_done = nfs4_open_confirm_done,
2373 .rpc_release = nfs4_open_confirm_release,
2377 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2379 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2381 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2382 struct rpc_task *task;
2383 struct rpc_message msg = {
2384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2385 .rpc_argp = &data->c_arg,
2386 .rpc_resp = &data->c_res,
2387 .rpc_cred = data->owner->so_cred,
2389 struct rpc_task_setup task_setup_data = {
2390 .rpc_client = server->client,
2391 .rpc_message = &msg,
2392 .callback_ops = &nfs4_open_confirm_ops,
2393 .callback_data = data,
2394 .workqueue = nfsiod_workqueue,
2395 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2399 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2401 kref_get(&data->kref);
2402 data->rpc_done = false;
2403 data->rpc_status = 0;
2404 data->timestamp = jiffies;
2405 task = rpc_run_task(&task_setup_data);
2407 return PTR_ERR(task);
2408 status = rpc_wait_for_completion_task(task);
2410 data->cancelled = true;
2413 status = data->rpc_status;
2418 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2420 struct nfs4_opendata *data = calldata;
2421 struct nfs4_state_owner *sp = data->owner;
2422 struct nfs_client *clp = sp->so_server->nfs_client;
2423 enum open_claim_type4 claim = data->o_arg.claim;
2425 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2428 * Check if we still need to send an OPEN call, or if we can use
2429 * a delegation instead.
2431 if (data->state != NULL) {
2432 struct nfs_delegation *delegation;
2434 if (can_open_cached(data->state, data->o_arg.fmode,
2435 data->o_arg.open_flags, claim))
2438 delegation = nfs4_get_valid_delegation(data->state->inode);
2439 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2440 goto unlock_no_action;
2443 /* Update client id. */
2444 data->o_arg.clientid = clp->cl_clientid;
2448 case NFS4_OPEN_CLAIM_PREVIOUS:
2449 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2450 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2451 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2453 case NFS4_OPEN_CLAIM_FH:
2454 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2456 data->timestamp = jiffies;
2457 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2458 &data->o_arg.seq_args,
2459 &data->o_res.seq_res,
2461 nfs_release_seqid(data->o_arg.seqid);
2463 /* Set the create mode (note dependency on the session type) */
2464 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2465 if (data->o_arg.open_flags & O_EXCL) {
2466 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2467 if (clp->cl_mvops->minor_version == 0) {
2468 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2469 /* don't put an ACCESS op in OPEN compound if O_EXCL,
2470 * because ACCESS will return permission denied for
2471 * all bits until close */
2472 data->o_res.access_request = data->o_arg.access = 0;
2473 } else if (nfs4_has_persistent_session(clp))
2474 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2478 trace_nfs4_cached_open(data->state);
2481 task->tk_action = NULL;
2483 nfs4_sequence_done(task, &data->o_res.seq_res);
2486 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2488 struct nfs4_opendata *data = calldata;
2490 data->rpc_status = task->tk_status;
2492 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2495 if (task->tk_status == 0) {
2496 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2497 switch (data->o_res.f_attr->mode & S_IFMT) {
2501 data->rpc_status = -ELOOP;
2504 data->rpc_status = -EISDIR;
2507 data->rpc_status = -ENOTDIR;
2510 renew_lease(data->o_res.server, data->timestamp);
2511 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2512 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2514 data->rpc_done = true;
2517 static void nfs4_open_release(void *calldata)
2519 struct nfs4_opendata *data = calldata;
2520 struct nfs4_state *state = NULL;
2522 /* If this request hasn't been cancelled, do nothing */
2523 if (!data->cancelled)
2525 /* In case of error, no cleanup! */
2526 if (data->rpc_status != 0 || !data->rpc_done)
2528 /* In case we need an open_confirm, no cleanup! */
2529 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2531 state = nfs4_opendata_to_nfs4_state(data);
2533 nfs4_close_state(state, data->o_arg.fmode);
2535 nfs4_opendata_put(data);
2538 static const struct rpc_call_ops nfs4_open_ops = {
2539 .rpc_call_prepare = nfs4_open_prepare,
2540 .rpc_call_done = nfs4_open_done,
2541 .rpc_release = nfs4_open_release,
2544 static int nfs4_run_open_task(struct nfs4_opendata *data,
2545 struct nfs_open_context *ctx)
2547 struct inode *dir = d_inode(data->dir);
2548 struct nfs_server *server = NFS_SERVER(dir);
2549 struct nfs_openargs *o_arg = &data->o_arg;
2550 struct nfs_openres *o_res = &data->o_res;
2551 struct rpc_task *task;
2552 struct rpc_message msg = {
2553 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2556 .rpc_cred = data->owner->so_cred,
2558 struct rpc_task_setup task_setup_data = {
2559 .rpc_client = server->client,
2560 .rpc_message = &msg,
2561 .callback_ops = &nfs4_open_ops,
2562 .callback_data = data,
2563 .workqueue = nfsiod_workqueue,
2564 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2568 if (server->nfs_client->cl_minorversion)
2569 task_setup_data.flags |= RPC_TASK_MOVEABLE;
2571 kref_get(&data->kref);
2572 data->rpc_done = false;
2573 data->rpc_status = 0;
2574 data->cancelled = false;
2575 data->is_recover = false;
2577 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2578 data->is_recover = true;
2579 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2581 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2582 pnfs_lgopen_prepare(data, ctx);
2584 task = rpc_run_task(&task_setup_data);
2586 return PTR_ERR(task);
2587 status = rpc_wait_for_completion_task(task);
2589 data->cancelled = true;
2592 status = data->rpc_status;
2598 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2600 struct inode *dir = d_inode(data->dir);
2601 struct nfs_openres *o_res = &data->o_res;
2604 status = nfs4_run_open_task(data, NULL);
2605 if (status != 0 || !data->rpc_done)
2608 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2610 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2611 status = _nfs4_proc_open_confirm(data);
2617 * Additional permission checks in order to distinguish between an
2618 * open for read, and an open for execute. This works around the
2619 * fact that NFSv4 OPEN treats read and execute permissions as being
2621 * Note that in the non-execute case, we want to turn off permission
2622 * checking if we just created a new file (POSIX open() semantics).
2624 static int nfs4_opendata_access(const struct cred *cred,
2625 struct nfs4_opendata *opendata,
2626 struct nfs4_state *state, fmode_t fmode,
2629 struct nfs_access_entry cache;
2632 /* access call failed or for some reason the server doesn't
2633 * support any access modes -- defer access call until later */
2634 if (opendata->o_res.access_supported == 0)
2639 * Use openflags to check for exec, because fmode won't
2640 * always have FMODE_EXEC set when file open for exec.
2642 if (openflags & __FMODE_EXEC) {
2643 /* ONLY check for exec rights */
2644 if (S_ISDIR(state->inode->i_mode))
2645 mask = NFS4_ACCESS_LOOKUP;
2647 mask = NFS4_ACCESS_EXECUTE;
2648 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2649 mask = NFS4_ACCESS_READ;
2651 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2652 nfs_access_add_cache(state->inode, &cache, cred);
2654 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2655 if ((mask & ~cache.mask & flags) == 0)
2662 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2664 static int _nfs4_proc_open(struct nfs4_opendata *data,
2665 struct nfs_open_context *ctx)
2667 struct inode *dir = d_inode(data->dir);
2668 struct nfs_server *server = NFS_SERVER(dir);
2669 struct nfs_openargs *o_arg = &data->o_arg;
2670 struct nfs_openres *o_res = &data->o_res;
2673 status = nfs4_run_open_task(data, ctx);
2674 if (!data->rpc_done)
2677 if (status == -NFS4ERR_BADNAME &&
2678 !(o_arg->open_flags & O_CREAT))
2683 nfs_fattr_map_and_free_names(server, &data->f_attr);
2685 if (o_arg->open_flags & O_CREAT) {
2686 if (o_arg->open_flags & O_EXCL)
2687 data->file_created = true;
2688 else if (o_res->cinfo.before != o_res->cinfo.after)
2689 data->file_created = true;
2690 if (data->file_created ||
2691 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2692 nfs4_update_changeattr(dir, &o_res->cinfo,
2693 o_res->f_attr->time_start,
2694 NFS_INO_INVALID_DATA);
2696 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2697 server->caps &= ~NFS_CAP_POSIX_LOCK;
2698 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2699 status = _nfs4_proc_open_confirm(data);
2703 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2704 nfs4_sequence_free_slot(&o_res->seq_res);
2705 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, NULL);
2712 * reclaim state on the server after a network partition.
2713 * Assumes caller holds the appropriate lock
2715 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2717 struct nfs4_opendata *opendata;
2720 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2721 NFS4_OPEN_CLAIM_FH);
2722 if (IS_ERR(opendata))
2723 return PTR_ERR(opendata);
2724 ret = nfs4_open_recover(opendata, state);
2726 d_drop(ctx->dentry);
2727 nfs4_opendata_put(opendata);
2731 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2733 struct nfs_server *server = NFS_SERVER(state->inode);
2734 struct nfs4_exception exception = { };
2738 err = _nfs4_open_expired(ctx, state);
2739 trace_nfs4_open_expired(ctx, 0, err);
2740 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2745 case -NFS4ERR_GRACE:
2746 case -NFS4ERR_DELAY:
2747 nfs4_handle_exception(server, err, &exception);
2750 } while (exception.retry);
2755 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2757 struct nfs_open_context *ctx;
2760 ctx = nfs4_state_find_open_context(state);
2763 ret = nfs4_do_open_expired(ctx, state);
2764 put_nfs_open_context(ctx);
2768 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2769 const nfs4_stateid *stateid)
2771 nfs_remove_bad_delegation(state->inode, stateid);
2772 nfs_state_clear_delegation(state);
2775 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2777 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2778 nfs_finish_clear_delegation_stateid(state, NULL);
2781 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2783 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2784 nfs40_clear_delegation_stateid(state);
2785 nfs_state_clear_open_state_flags(state);
2786 return nfs4_open_expired(sp, state);
2789 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2790 nfs4_stateid *stateid,
2791 const struct cred *cred)
2793 return -NFS4ERR_BAD_STATEID;
2796 #if defined(CONFIG_NFS_V4_1)
2797 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2798 nfs4_stateid *stateid,
2799 const struct cred *cred)
2803 switch (stateid->type) {
2806 case NFS4_INVALID_STATEID_TYPE:
2807 case NFS4_SPECIAL_STATEID_TYPE:
2808 return -NFS4ERR_BAD_STATEID;
2809 case NFS4_REVOKED_STATEID_TYPE:
2813 status = nfs41_test_stateid(server, stateid, cred);
2815 case -NFS4ERR_EXPIRED:
2816 case -NFS4ERR_ADMIN_REVOKED:
2817 case -NFS4ERR_DELEG_REVOKED:
2823 /* Ack the revoked state to the server */
2824 nfs41_free_stateid(server, stateid, cred, true);
2825 return -NFS4ERR_EXPIRED;
2828 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2830 struct nfs_server *server = NFS_SERVER(state->inode);
2831 nfs4_stateid stateid;
2832 struct nfs_delegation *delegation;
2833 const struct cred *cred = NULL;
2834 int status, ret = NFS_OK;
2836 /* Get the delegation credential for use by test/free_stateid */
2838 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2839 if (delegation == NULL) {
2841 nfs_state_clear_delegation(state);
2845 spin_lock(&delegation->lock);
2846 nfs4_stateid_copy(&stateid, &delegation->stateid);
2848 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2849 &delegation->flags)) {
2850 spin_unlock(&delegation->lock);
2855 if (delegation->cred)
2856 cred = get_cred(delegation->cred);
2857 spin_unlock(&delegation->lock);
2859 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2860 trace_nfs4_test_delegation_stateid(state, NULL, status);
2861 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2862 nfs_finish_clear_delegation_stateid(state, &stateid);
2870 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2874 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2875 nfs4_copy_delegation_stateid(state->inode, state->state,
2877 nfs4_stateid_match_other(&state->stateid, &tmp))
2878 nfs_state_set_delegation(state, &tmp, state->state);
2880 nfs_state_clear_delegation(state);
2884 * nfs41_check_expired_locks - possibly free a lock stateid
2886 * @state: NFSv4 state for an inode
2888 * Returns NFS_OK if recovery for this stateid is now finished.
2889 * Otherwise a negative NFS4ERR value is returned.
2891 static int nfs41_check_expired_locks(struct nfs4_state *state)
2893 int status, ret = NFS_OK;
2894 struct nfs4_lock_state *lsp, *prev = NULL;
2895 struct nfs_server *server = NFS_SERVER(state->inode);
2897 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2900 spin_lock(&state->state_lock);
2901 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2902 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2903 const struct cred *cred = lsp->ls_state->owner->so_cred;
2905 refcount_inc(&lsp->ls_count);
2906 spin_unlock(&state->state_lock);
2908 nfs4_put_lock_state(prev);
2911 status = nfs41_test_and_free_expired_stateid(server,
2914 trace_nfs4_test_lock_stateid(state, lsp, status);
2915 if (status == -NFS4ERR_EXPIRED ||
2916 status == -NFS4ERR_BAD_STATEID) {
2917 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2918 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2919 if (!recover_lost_locks)
2920 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2921 } else if (status != NFS_OK) {
2923 nfs4_put_lock_state(prev);
2926 spin_lock(&state->state_lock);
2929 spin_unlock(&state->state_lock);
2930 nfs4_put_lock_state(prev);
2936 * nfs41_check_open_stateid - possibly free an open stateid
2938 * @state: NFSv4 state for an inode
2940 * Returns NFS_OK if recovery for this stateid is now finished.
2941 * Otherwise a negative NFS4ERR value is returned.
2943 static int nfs41_check_open_stateid(struct nfs4_state *state)
2945 struct nfs_server *server = NFS_SERVER(state->inode);
2946 nfs4_stateid *stateid = &state->open_stateid;
2947 const struct cred *cred = state->owner->so_cred;
2950 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2951 return -NFS4ERR_BAD_STATEID;
2952 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2953 trace_nfs4_test_open_stateid(state, NULL, status);
2954 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2955 nfs_state_clear_open_state_flags(state);
2956 stateid->type = NFS4_INVALID_STATEID_TYPE;
2959 if (nfs_open_stateid_recover_openmode(state))
2960 return -NFS4ERR_OPENMODE;
2964 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2968 status = nfs41_check_delegation_stateid(state);
2969 if (status != NFS_OK)
2971 nfs41_delegation_recover_stateid(state);
2973 status = nfs41_check_expired_locks(state);
2974 if (status != NFS_OK)
2976 status = nfs41_check_open_stateid(state);
2977 if (status != NFS_OK)
2978 status = nfs4_open_expired(sp, state);
2984 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2985 * fields corresponding to attributes that were used to store the verifier.
2986 * Make sure we clobber those fields in the later setattr call
2988 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2989 struct iattr *sattr, struct nfs4_label **label)
2991 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2996 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2997 attrset[i] = opendata->o_res.attrset[i];
2998 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2999 attrset[i] &= ~bitmask[i];
3002 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3003 sattr->ia_valid : 0;
3005 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3006 if (sattr->ia_valid & ATTR_ATIME_SET)
3007 ret |= ATTR_ATIME_SET;
3012 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3013 if (sattr->ia_valid & ATTR_MTIME_SET)
3014 ret |= ATTR_MTIME_SET;
3019 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3024 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3025 int flags, struct nfs_open_context *ctx)
3027 struct nfs4_state_owner *sp = opendata->owner;
3028 struct nfs_server *server = sp->so_server;
3029 struct dentry *dentry;
3030 struct nfs4_state *state;
3031 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3032 struct inode *dir = d_inode(opendata->dir);
3033 unsigned long dir_verifier;
3037 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3038 dir_verifier = nfs_save_change_attribute(dir);
3040 ret = _nfs4_proc_open(opendata, ctx);
3044 state = _nfs4_opendata_to_nfs4_state(opendata);
3045 ret = PTR_ERR(state);
3049 if (server->caps & NFS_CAP_POSIX_LOCK)
3050 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3051 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3052 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3054 dentry = opendata->dentry;
3055 if (d_really_is_negative(dentry)) {
3056 struct dentry *alias;
3058 alias = d_exact_alias(dentry, state->inode);
3060 alias = d_splice_alias(igrab(state->inode), dentry);
3061 /* d_splice_alias() can't fail here - it's a non-directory */
3064 ctx->dentry = dentry = alias;
3068 switch(opendata->o_arg.claim) {
3071 case NFS4_OPEN_CLAIM_NULL:
3072 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3073 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3074 if (!opendata->rpc_done)
3076 if (opendata->o_res.delegation_type != 0)
3077 dir_verifier = nfs_save_change_attribute(dir);
3078 nfs_set_verifier(dentry, dir_verifier);
3081 /* Parse layoutget results before we check for access */
3082 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3084 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3089 if (d_inode(dentry) == state->inode) {
3090 nfs_inode_attach_open_context(ctx);
3091 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3092 nfs4_schedule_stateid_recovery(server, state);
3096 if (!opendata->cancelled)
3097 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3102 * Returns a referenced nfs4_state
3104 static int _nfs4_do_open(struct inode *dir,
3105 struct nfs_open_context *ctx,
3107 const struct nfs4_open_createattrs *c,
3110 struct nfs4_state_owner *sp;
3111 struct nfs4_state *state = NULL;
3112 struct nfs_server *server = NFS_SERVER(dir);
3113 struct nfs4_opendata *opendata;
3114 struct dentry *dentry = ctx->dentry;
3115 const struct cred *cred = ctx->cred;
3116 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3117 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3118 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3119 struct iattr *sattr = c->sattr;
3120 struct nfs4_label *label = c->label;
3123 /* Protect against reboot recovery conflicts */
3125 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3127 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3130 status = nfs4_client_recover_expired_lease(server->nfs_client);
3132 goto err_put_state_owner;
3133 if (d_really_is_positive(dentry))
3134 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3136 if (d_really_is_positive(dentry))
3137 claim = NFS4_OPEN_CLAIM_FH;
3138 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3139 c, claim, GFP_KERNEL);
3140 if (opendata == NULL)
3141 goto err_put_state_owner;
3143 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3144 if (!opendata->f_attr.mdsthreshold) {
3145 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3146 if (!opendata->f_attr.mdsthreshold)
3147 goto err_opendata_put;
3149 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3151 if (d_really_is_positive(dentry))
3152 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3154 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3156 goto err_opendata_put;
3159 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3160 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3161 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3163 * send create attributes which was not set by open
3164 * with an extra setattr.
3166 if (attrs || label) {
3167 unsigned ia_old = sattr->ia_valid;
3169 sattr->ia_valid = attrs;
3170 nfs_fattr_init(opendata->o_res.f_attr);
3171 status = nfs4_do_setattr(state->inode, cred,
3172 opendata->o_res.f_attr, sattr,
3175 nfs_setattr_update_inode(state->inode, sattr,
3176 opendata->o_res.f_attr);
3177 nfs_setsecurity(state->inode, opendata->o_res.f_attr);
3179 sattr->ia_valid = ia_old;
3182 if (opened && opendata->file_created)
3185 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3186 *ctx_th = opendata->f_attr.mdsthreshold;
3187 opendata->f_attr.mdsthreshold = NULL;
3190 nfs4_opendata_put(opendata);
3191 nfs4_put_state_owner(sp);
3194 nfs4_opendata_put(opendata);
3195 err_put_state_owner:
3196 nfs4_put_state_owner(sp);
3202 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3203 struct nfs_open_context *ctx,
3205 struct iattr *sattr,
3206 struct nfs4_label *label,
3209 struct nfs_server *server = NFS_SERVER(dir);
3210 struct nfs4_exception exception = {
3211 .interruptible = true,
3213 struct nfs4_state *res;
3214 struct nfs4_open_createattrs c = {
3218 [0] = (__u32)jiffies,
3219 [1] = (__u32)current->pid,
3225 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3227 trace_nfs4_open_file(ctx, flags, status);
3230 /* NOTE: BAD_SEQID means the server and client disagree about the
3231 * book-keeping w.r.t. state-changing operations
3232 * (OPEN/CLOSE/LOCK/LOCKU...)
3233 * It is actually a sign of a bug on the client or on the server.
3235 * If we receive a BAD_SEQID error in the particular case of
3236 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3237 * have unhashed the old state_owner for us, and that we can
3238 * therefore safely retry using a new one. We should still warn
3239 * the user though...
3241 if (status == -NFS4ERR_BAD_SEQID) {
3242 pr_warn_ratelimited("NFS: v4 server %s "
3243 " returned a bad sequence-id error!\n",
3244 NFS_SERVER(dir)->nfs_client->cl_hostname);
3245 exception.retry = 1;
3249 * BAD_STATEID on OPEN means that the server cancelled our
3250 * state before it received the OPEN_CONFIRM.
3251 * Recover by retrying the request as per the discussion
3252 * on Page 181 of RFC3530.
3254 if (status == -NFS4ERR_BAD_STATEID) {
3255 exception.retry = 1;
3258 if (status == -NFS4ERR_EXPIRED) {
3259 nfs4_schedule_lease_recovery(server->nfs_client);
3260 exception.retry = 1;
3263 if (status == -EAGAIN) {
3264 /* We must have found a delegation */
3265 exception.retry = 1;
3268 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3270 res = ERR_PTR(nfs4_handle_exception(server,
3271 status, &exception));
3272 } while (exception.retry);
3276 static int _nfs4_do_setattr(struct inode *inode,
3277 struct nfs_setattrargs *arg,
3278 struct nfs_setattrres *res,
3279 const struct cred *cred,
3280 struct nfs_open_context *ctx)
3282 struct nfs_server *server = NFS_SERVER(inode);
3283 struct rpc_message msg = {
3284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3289 const struct cred *delegation_cred = NULL;
3290 unsigned long timestamp = jiffies;
3294 nfs_fattr_init(res->fattr);
3296 /* Servers should only apply open mode checks for file size changes */
3297 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3299 nfs4_inode_make_writeable(inode);
3303 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3304 /* Use that stateid */
3305 } else if (ctx != NULL && ctx->state) {
3306 struct nfs_lock_context *l_ctx;
3307 if (!nfs4_valid_open_stateid(ctx->state))
3309 l_ctx = nfs_get_lock_context(ctx);
3311 return PTR_ERR(l_ctx);
3312 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3313 &arg->stateid, &delegation_cred);
3314 nfs_put_lock_context(l_ctx);
3317 else if (status == -EAGAIN)
3321 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3323 if (delegation_cred)
3324 msg.rpc_cred = delegation_cred;
3326 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3328 put_cred(delegation_cred);
3329 if (status == 0 && ctx != NULL)
3330 renew_lease(server, timestamp);
3331 trace_nfs4_setattr(inode, &arg->stateid, status);
3335 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3336 struct nfs_fattr *fattr, struct iattr *sattr,
3337 struct nfs_open_context *ctx, struct nfs4_label *ilabel)
3339 struct nfs_server *server = NFS_SERVER(inode);
3340 __u32 bitmask[NFS4_BITMASK_SZ];
3341 struct nfs4_state *state = ctx ? ctx->state : NULL;
3342 struct nfs_setattrargs arg = {
3343 .fh = NFS_FH(inode),
3349 struct nfs_setattrres res = {
3353 struct nfs4_exception exception = {
3356 .stateid = &arg.stateid,
3358 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
3361 if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3362 adjust_flags |= NFS_INO_INVALID_MODE;
3363 if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
3364 adjust_flags |= NFS_INO_INVALID_OTHER;
3367 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label),
3368 inode, adjust_flags);
3370 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3372 case -NFS4ERR_OPENMODE:
3373 if (!(sattr->ia_valid & ATTR_SIZE)) {
3374 pr_warn_once("NFSv4: server %s is incorrectly "
3375 "applying open mode checks to "
3376 "a SETATTR that is not "
3377 "changing file size.\n",
3378 server->nfs_client->cl_hostname);
3380 if (state && !(state->state & FMODE_WRITE)) {
3382 if (sattr->ia_valid & ATTR_OPEN)
3387 err = nfs4_handle_exception(server, err, &exception);
3388 } while (exception.retry);
3394 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3396 if (inode == NULL || !nfs_have_layout(inode))
3399 return pnfs_wait_on_layoutreturn(inode, task);
3403 * Update the seqid of an open stateid
3405 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3406 struct nfs4_state *state)
3413 if (!nfs4_valid_open_stateid(state))
3415 seq = read_seqbegin(&state->seqlock);
3416 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3417 nfs4_stateid_copy(dst, &state->open_stateid);
3418 if (read_seqretry(&state->seqlock, seq))
3422 seqid_open = state->open_stateid.seqid;
3423 if (read_seqretry(&state->seqlock, seq))
3426 dst_seqid = be32_to_cpu(dst->seqid);
3427 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3428 dst->seqid = seqid_open;
3434 * Update the seqid of an open stateid after receiving
3435 * NFS4ERR_OLD_STATEID
3437 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3438 struct nfs4_state *state)
3443 int seq, status = -EAGAIN;
3448 if (!nfs4_valid_open_stateid(state))
3450 seq = read_seqbegin(&state->seqlock);
3451 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3452 if (read_seqretry(&state->seqlock, seq))
3457 write_seqlock(&state->seqlock);
3458 seqid_open = state->open_stateid.seqid;
3460 dst_seqid = be32_to_cpu(dst->seqid);
3462 /* Did another OPEN bump the state's seqid? try again: */
3463 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3464 dst->seqid = seqid_open;
3465 write_sequnlock(&state->seqlock);
3470 /* server says we're behind but we haven't seen the update yet */
3471 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3472 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3473 write_sequnlock(&state->seqlock);
3474 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3476 if (fatal_signal_pending(current))
3479 if (schedule_timeout(5*HZ) != 0)
3482 finish_wait(&state->waitq, &wait);
3486 if (status == -EINTR)
3489 /* we slept the whole 5 seconds, we must have lost a seqid */
3490 dst->seqid = cpu_to_be32(dst_seqid + 1);
3498 struct nfs4_closedata {
3499 struct inode *inode;
3500 struct nfs4_state *state;
3501 struct nfs_closeargs arg;
3502 struct nfs_closeres res;
3504 struct nfs4_layoutreturn_args arg;
3505 struct nfs4_layoutreturn_res res;
3506 struct nfs4_xdr_opaque_data ld_private;
3510 struct nfs_fattr fattr;
3511 unsigned long timestamp;
3514 static void nfs4_free_closedata(void *data)
3516 struct nfs4_closedata *calldata = data;
3517 struct nfs4_state_owner *sp = calldata->state->owner;
3518 struct super_block *sb = calldata->state->inode->i_sb;
3520 if (calldata->lr.roc)
3521 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3522 calldata->res.lr_ret);
3523 nfs4_put_open_state(calldata->state);
3524 nfs_free_seqid(calldata->arg.seqid);
3525 nfs4_put_state_owner(sp);
3526 nfs_sb_deactive(sb);
3530 static void nfs4_close_done(struct rpc_task *task, void *data)
3532 struct nfs4_closedata *calldata = data;
3533 struct nfs4_state *state = calldata->state;
3534 struct nfs_server *server = NFS_SERVER(calldata->inode);
3535 nfs4_stateid *res_stateid = NULL;
3536 struct nfs4_exception exception = {
3538 .inode = calldata->inode,
3539 .stateid = &calldata->arg.stateid,
3542 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3544 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3546 /* Handle Layoutreturn errors */
3547 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3548 &calldata->res.lr_ret) == -EAGAIN)
3551 /* hmm. we are done with the inode, and in the process of freeing
3552 * the state_owner. we keep this around to process errors
3554 switch (task->tk_status) {
3556 res_stateid = &calldata->res.stateid;
3557 renew_lease(server, calldata->timestamp);
3559 case -NFS4ERR_ACCESS:
3560 if (calldata->arg.bitmask != NULL) {
3561 calldata->arg.bitmask = NULL;
3562 calldata->res.fattr = NULL;
3567 case -NFS4ERR_OLD_STATEID:
3568 /* Did we race with OPEN? */
3569 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3573 case -NFS4ERR_ADMIN_REVOKED:
3574 case -NFS4ERR_STALE_STATEID:
3575 case -NFS4ERR_EXPIRED:
3576 nfs4_free_revoked_stateid(server,
3577 &calldata->arg.stateid,
3578 task->tk_msg.rpc_cred);
3580 case -NFS4ERR_BAD_STATEID:
3581 if (calldata->arg.fmode == 0)
3585 task->tk_status = nfs4_async_handle_exception(task,
3586 server, task->tk_status, &exception);
3587 if (exception.retry)
3590 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3591 res_stateid, calldata->arg.fmode);
3593 task->tk_status = 0;
3594 nfs_release_seqid(calldata->arg.seqid);
3595 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3596 dprintk("%s: ret = %d\n", __func__, task->tk_status);
3599 task->tk_status = 0;
3600 rpc_restart_call_prepare(task);
3604 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3606 struct nfs4_closedata *calldata = data;
3607 struct nfs4_state *state = calldata->state;
3608 struct inode *inode = calldata->inode;
3609 struct nfs_server *server = NFS_SERVER(inode);
3610 struct pnfs_layout_hdr *lo;
3611 bool is_rdonly, is_wronly, is_rdwr;
3614 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3617 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3618 spin_lock(&state->owner->so_lock);
3619 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3620 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3621 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3622 /* Calculate the change in open mode */
3623 calldata->arg.fmode = 0;
3624 if (state->n_rdwr == 0) {
3625 if (state->n_rdonly == 0)
3626 call_close |= is_rdonly;
3628 calldata->arg.fmode |= FMODE_READ;
3629 if (state->n_wronly == 0)
3630 call_close |= is_wronly;
3632 calldata->arg.fmode |= FMODE_WRITE;
3633 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3634 call_close |= is_rdwr;
3636 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3638 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3639 if (!nfs4_valid_open_stateid(state))
3641 spin_unlock(&state->owner->so_lock);
3644 /* Note: exit _without_ calling nfs4_close_done */
3648 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3649 nfs_release_seqid(calldata->arg.seqid);
3653 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3654 if (lo && !pnfs_layout_is_valid(lo)) {
3655 calldata->arg.lr_args = NULL;
3656 calldata->res.lr_res = NULL;
3659 if (calldata->arg.fmode == 0)
3660 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3662 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3663 /* Close-to-open cache consistency revalidation */
3664 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3665 nfs4_bitmask_set(calldata->arg.bitmask_store,
3666 server->cache_consistency_bitmask,
3668 calldata->arg.bitmask = calldata->arg.bitmask_store;
3670 calldata->arg.bitmask = NULL;
3673 calldata->arg.share_access =
3674 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3675 calldata->arg.fmode, 0);
3677 if (calldata->res.fattr == NULL)
3678 calldata->arg.bitmask = NULL;
3679 else if (calldata->arg.bitmask == NULL)
3680 calldata->res.fattr = NULL;
3681 calldata->timestamp = jiffies;
3682 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3683 &calldata->arg.seq_args,
3684 &calldata->res.seq_res,
3686 nfs_release_seqid(calldata->arg.seqid);
3689 task->tk_action = NULL;
3691 nfs4_sequence_done(task, &calldata->res.seq_res);
3694 static const struct rpc_call_ops nfs4_close_ops = {
3695 .rpc_call_prepare = nfs4_close_prepare,
3696 .rpc_call_done = nfs4_close_done,
3697 .rpc_release = nfs4_free_closedata,
3701 * It is possible for data to be read/written from a mem-mapped file
3702 * after the sys_close call (which hits the vfs layer as a flush).
3703 * This means that we can't safely call nfsv4 close on a file until
3704 * the inode is cleared. This in turn means that we are not good
3705 * NFSv4 citizens - we do not indicate to the server to update the file's
3706 * share state even when we are done with one of the three share
3707 * stateid's in the inode.
3709 * NOTE: Caller must be holding the sp->so_owner semaphore!
3711 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3713 struct nfs_server *server = NFS_SERVER(state->inode);
3714 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3715 struct nfs4_closedata *calldata;
3716 struct nfs4_state_owner *sp = state->owner;
3717 struct rpc_task *task;
3718 struct rpc_message msg = {
3719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3720 .rpc_cred = state->owner->so_cred,
3722 struct rpc_task_setup task_setup_data = {
3723 .rpc_client = server->client,
3724 .rpc_message = &msg,
3725 .callback_ops = &nfs4_close_ops,
3726 .workqueue = nfsiod_workqueue,
3727 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3729 int status = -ENOMEM;
3731 if (server->nfs_client->cl_minorversion)
3732 task_setup_data.flags |= RPC_TASK_MOVEABLE;
3734 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3735 &task_setup_data.rpc_client, &msg);
3737 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3738 if (calldata == NULL)
3740 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3741 calldata->inode = state->inode;
3742 calldata->state = state;
3743 calldata->arg.fh = NFS_FH(state->inode);
3744 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3745 goto out_free_calldata;
3746 /* Serialization for the sequence id */
3747 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3748 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3749 if (IS_ERR(calldata->arg.seqid))
3750 goto out_free_calldata;
3751 nfs_fattr_init(&calldata->fattr);
3752 calldata->arg.fmode = 0;
3753 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3754 calldata->res.fattr = &calldata->fattr;
3755 calldata->res.seqid = calldata->arg.seqid;
3756 calldata->res.server = server;
3757 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3758 calldata->lr.roc = pnfs_roc(state->inode,
3759 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3760 if (calldata->lr.roc) {
3761 calldata->arg.lr_args = &calldata->lr.arg;
3762 calldata->res.lr_res = &calldata->lr.res;
3764 nfs_sb_active(calldata->inode->i_sb);
3766 msg.rpc_argp = &calldata->arg;
3767 msg.rpc_resp = &calldata->res;
3768 task_setup_data.callback_data = calldata;
3769 task = rpc_run_task(&task_setup_data);
3771 return PTR_ERR(task);
3774 status = rpc_wait_for_completion_task(task);
3780 nfs4_put_open_state(state);
3781 nfs4_put_state_owner(sp);
3785 static struct inode *
3786 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3787 int open_flags, struct iattr *attr, int *opened)
3789 struct nfs4_state *state;
3790 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3792 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3794 /* Protect against concurrent sillydeletes */
3795 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3797 nfs4_label_release_security(label);
3800 return ERR_CAST(state);
3801 return state->inode;
3804 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3806 if (ctx->state == NULL)
3809 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3811 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3814 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3815 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3816 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3818 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3820 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3821 struct nfs4_server_caps_arg args = {
3825 struct nfs4_server_caps_res res = {};
3826 struct rpc_message msg = {
3827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3834 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3835 FATTR4_WORD0_FH_EXPIRE_TYPE |
3836 FATTR4_WORD0_LINK_SUPPORT |
3837 FATTR4_WORD0_SYMLINK_SUPPORT |
3838 FATTR4_WORD0_ACLSUPPORT |
3839 FATTR4_WORD0_CASE_INSENSITIVE |
3840 FATTR4_WORD0_CASE_PRESERVING;
3842 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3844 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3846 /* Sanity check the server answers */
3847 switch (minorversion) {
3849 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3850 res.attr_bitmask[2] = 0;
3853 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3856 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3858 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3859 server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3860 NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3861 server->fattr_valid = NFS_ATTR_FATTR_V4;
3862 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3863 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3864 server->caps |= NFS_CAP_ACLS;
3865 if (res.has_links != 0)
3866 server->caps |= NFS_CAP_HARDLINKS;
3867 if (res.has_symlinks != 0)
3868 server->caps |= NFS_CAP_SYMLINKS;
3869 if (res.case_insensitive)
3870 server->caps |= NFS_CAP_CASE_INSENSITIVE;
3871 if (res.case_preserving)
3872 server->caps |= NFS_CAP_CASE_PRESERVING;
3873 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3874 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3875 server->caps |= NFS_CAP_SECURITY_LABEL;
3877 if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS)
3878 server->caps |= NFS_CAP_FS_LOCATIONS;
3879 if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3880 server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3881 if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3882 server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3883 if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3884 server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3885 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3886 server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3887 NFS_ATTR_FATTR_OWNER_NAME);
3888 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3889 server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3890 NFS_ATTR_FATTR_GROUP_NAME);
3891 if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3892 server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3893 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3894 server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3895 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3896 server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3897 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3898 server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
3899 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3900 sizeof(server->attr_bitmask));
3901 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3903 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3904 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3905 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3906 server->cache_consistency_bitmask[2] = 0;
3908 /* Avoid a regression due to buggy server */
3909 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3910 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3911 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3912 sizeof(server->exclcreat_bitmask));
3914 server->acl_bitmask = res.acl_bitmask;
3915 server->fh_expire_type = res.fh_expire_type;
3921 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3923 struct nfs4_exception exception = {
3924 .interruptible = true,
3928 nfs4_server_set_init_caps(server);
3930 err = nfs4_handle_exception(server,
3931 _nfs4_server_capabilities(server, fhandle),
3933 } while (exception.retry);
3937 static void test_fs_location_for_trunking(struct nfs4_fs_location *location,
3938 struct nfs_client *clp,
3939 struct nfs_server *server)
3943 for (i = 0; i < location->nservers; i++) {
3944 struct nfs4_string *srv_loc = &location->servers[i];
3945 struct sockaddr addr;
3947 struct xprt_create xprt_args = {
3951 struct nfs4_add_xprt_data xprtdata = {
3954 struct rpc_add_xprt_test rpcdata = {
3955 .add_xprt_test = clp->cl_mvops->session_trunk,
3958 char *servername = NULL;
3963 addrlen = nfs_parse_server_name(srv_loc->data, srv_loc->len,
3964 &addr, sizeof(addr),
3965 clp->cl_net, server->port);
3968 xprt_args.dstaddr = &addr;
3969 xprt_args.addrlen = addrlen;
3970 servername = kmalloc(srv_loc->len + 1, GFP_KERNEL);
3973 memcpy(servername, srv_loc->data, srv_loc->len);
3974 servername[srv_loc->len] = '\0';
3975 xprt_args.servername = servername;
3977 xprtdata.cred = nfs4_get_clid_cred(clp);
3978 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
3979 rpc_clnt_setup_test_and_add_xprt,
3982 put_cred(xprtdata.cred);
3987 static int _nfs4_discover_trunking(struct nfs_server *server,
3988 struct nfs_fh *fhandle)
3990 struct nfs4_fs_locations *locations = NULL;
3992 const struct cred *cred;
3993 struct nfs_client *clp = server->nfs_client;
3994 const struct nfs4_state_maintenance_ops *ops =
3995 clp->cl_mvops->state_renewal_ops;
3996 int status = -ENOMEM, i;
3998 cred = ops->get_state_renewal_cred(clp);
4000 cred = nfs4_get_clid_cred(clp);
4005 page = alloc_page(GFP_KERNEL);
4006 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4007 if (page == NULL || locations == NULL)
4010 status = nfs4_proc_get_locations(server, fhandle, locations, page,
4015 for (i = 0; i < locations->nlocations; i++)
4016 test_fs_location_for_trunking(&locations->locations[i], clp,
4025 static int nfs4_discover_trunking(struct nfs_server *server,
4026 struct nfs_fh *fhandle)
4028 struct nfs4_exception exception = {
4029 .interruptible = true,
4031 struct nfs_client *clp = server->nfs_client;
4034 if (!nfs4_has_session(clp))
4037 err = nfs4_handle_exception(server,
4038 _nfs4_discover_trunking(server, fhandle),
4040 } while (exception.retry);
4045 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4046 struct nfs_fsinfo *info)
4049 struct nfs4_lookup_root_arg args = {
4052 struct nfs4_lookup_res res = {
4054 .fattr = info->fattr,
4057 struct rpc_message msg = {
4058 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
4063 bitmask[0] = nfs4_fattr_bitmap[0];
4064 bitmask[1] = nfs4_fattr_bitmap[1];
4066 * Process the label in the upcoming getfattr
4068 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
4070 nfs_fattr_init(info->fattr);
4071 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4074 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4075 struct nfs_fsinfo *info)
4077 struct nfs4_exception exception = {
4078 .interruptible = true,
4082 err = _nfs4_lookup_root(server, fhandle, info);
4083 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
4086 case -NFS4ERR_WRONGSEC:
4089 err = nfs4_handle_exception(server, err, &exception);
4091 } while (exception.retry);
4096 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4097 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4099 struct rpc_auth_create_args auth_args = {
4100 .pseudoflavor = flavor,
4102 struct rpc_auth *auth;
4104 auth = rpcauth_create(&auth_args, server->client);
4107 return nfs4_lookup_root(server, fhandle, info);
4111 * Retry pseudoroot lookup with various security flavors. We do this when:
4113 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4114 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4116 * Returns zero on success, or a negative NFS4ERR value, or a
4117 * negative errno value.
4119 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4120 struct nfs_fsinfo *info)
4122 /* Per 3530bis 15.33.5 */
4123 static const rpc_authflavor_t flav_array[] = {
4127 RPC_AUTH_UNIX, /* courtesy */
4130 int status = -EPERM;
4133 if (server->auth_info.flavor_len > 0) {
4134 /* try each flavor specified by user */
4135 for (i = 0; i < server->auth_info.flavor_len; i++) {
4136 status = nfs4_lookup_root_sec(server, fhandle, info,
4137 server->auth_info.flavors[i]);
4138 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4143 /* no flavors specified by user, try default list */
4144 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4145 status = nfs4_lookup_root_sec(server, fhandle, info,
4147 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4154 * -EACCES could mean that the user doesn't have correct permissions
4155 * to access the mount. It could also mean that we tried to mount
4156 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4157 * existing mount programs don't handle -EACCES very well so it should
4158 * be mapped to -EPERM instead.
4160 if (status == -EACCES)
4166 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4167 * @server: initialized nfs_server handle
4168 * @fhandle: we fill in the pseudo-fs root file handle
4169 * @info: we fill in an FSINFO struct
4170 * @auth_probe: probe the auth flavours
4172 * Returns zero on success, or a negative errno.
4174 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4175 struct nfs_fsinfo *info,
4181 status = nfs4_lookup_root(server, fhandle, info);
4183 if (auth_probe || status == NFS4ERR_WRONGSEC)
4184 status = server->nfs_client->cl_mvops->find_root_sec(server,
4188 status = nfs4_server_capabilities(server, fhandle);
4190 status = nfs4_do_fsinfo(server, fhandle, info);
4192 return nfs4_map_errors(status);
4195 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4196 struct nfs_fsinfo *info)
4199 struct nfs_fattr *fattr = info->fattr;
4201 error = nfs4_server_capabilities(server, mntfh);
4203 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4207 error = nfs4_proc_getattr(server, mntfh, fattr, NULL);
4209 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4213 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4214 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4215 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4222 * Get locations and (maybe) other attributes of a referral.
4223 * Note that we'll actually follow the referral later when
4224 * we detect fsid mismatch in inode revalidation
4226 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4227 const struct qstr *name, struct nfs_fattr *fattr,
4228 struct nfs_fh *fhandle)
4230 int status = -ENOMEM;
4231 struct page *page = NULL;
4232 struct nfs4_fs_locations *locations = NULL;
4234 page = alloc_page(GFP_KERNEL);
4237 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4238 if (locations == NULL)
4241 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4246 * If the fsid didn't change, this is a migration event, not a
4247 * referral. Cause us to drop into the exception handler, which
4248 * will kick off migration recovery.
4250 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4251 dprintk("%s: server did not return a different fsid for"
4252 " a referral at %s\n", __func__, name->name);
4253 status = -NFS4ERR_MOVED;
4256 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4257 nfs_fixup_referral_attributes(&locations->fattr);
4259 /* replace the lookup nfs_fattr with the locations nfs_fattr */
4260 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4261 memset(fhandle, 0, sizeof(struct nfs_fh));
4269 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4270 struct nfs_fattr *fattr, struct inode *inode)
4272 __u32 bitmask[NFS4_BITMASK_SZ];
4273 struct nfs4_getattr_arg args = {
4277 struct nfs4_getattr_res res = {
4281 struct rpc_message msg = {
4282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4286 unsigned short task_flags = 0;
4288 if (nfs4_has_session(server->nfs_client))
4289 task_flags = RPC_TASK_MOVEABLE;
4291 /* Is this is an attribute revalidation, subject to softreval? */
4292 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4293 task_flags |= RPC_TASK_TIMEOUT;
4295 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0);
4296 nfs_fattr_init(fattr);
4297 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4298 return nfs4_do_call_sync(server->client, server, &msg,
4299 &args.seq_args, &res.seq_res, task_flags);
4302 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4303 struct nfs_fattr *fattr, struct inode *inode)
4305 struct nfs4_exception exception = {
4306 .interruptible = true,
4310 err = _nfs4_proc_getattr(server, fhandle, fattr, inode);
4311 trace_nfs4_getattr(server, fhandle, fattr, err);
4312 err = nfs4_handle_exception(server, err,
4314 } while (exception.retry);
4319 * The file is not closed if it is opened due to the a request to change
4320 * the size of the file. The open call will not be needed once the
4321 * VFS layer lookup-intents are implemented.
4323 * Close is called when the inode is destroyed.
4324 * If we haven't opened the file for O_WRONLY, we
4325 * need to in the size_change case to obtain a stateid.
4328 * Because OPEN is always done by name in nfsv4, it is
4329 * possible that we opened a different file by the same
4330 * name. We can recognize this race condition, but we
4331 * can't do anything about it besides returning an error.
4333 * This will be fixed with VFS changes (lookup-intent).
4336 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4337 struct iattr *sattr)
4339 struct inode *inode = d_inode(dentry);
4340 const struct cred *cred = NULL;
4341 struct nfs_open_context *ctx = NULL;
4344 if (pnfs_ld_layoutret_on_setattr(inode) &&
4345 sattr->ia_valid & ATTR_SIZE &&
4346 sattr->ia_size < i_size_read(inode))
4347 pnfs_commit_and_return_layout(inode);
4349 nfs_fattr_init(fattr);
4351 /* Deal with open(O_TRUNC) */
4352 if (sattr->ia_valid & ATTR_OPEN)
4353 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4355 /* Optimization: if the end result is no change, don't RPC */
4356 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4359 /* Search for an existing open(O_WRITE) file */
4360 if (sattr->ia_valid & ATTR_FILE) {
4362 ctx = nfs_file_open_context(sattr->ia_file);
4367 /* Return any delegations if we're going to change ACLs */
4368 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4369 nfs4_inode_make_writeable(inode);
4371 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL);
4373 nfs_setattr_update_inode(inode, sattr, fattr);
4374 nfs_setsecurity(inode, fattr);
4379 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4380 struct dentry *dentry, struct nfs_fh *fhandle,
4381 struct nfs_fattr *fattr)
4383 struct nfs_server *server = NFS_SERVER(dir);
4385 struct nfs4_lookup_arg args = {
4386 .bitmask = server->attr_bitmask,
4387 .dir_fh = NFS_FH(dir),
4388 .name = &dentry->d_name,
4390 struct nfs4_lookup_res res = {
4395 struct rpc_message msg = {
4396 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4400 unsigned short task_flags = 0;
4402 if (server->nfs_client->cl_minorversion)
4403 task_flags = RPC_TASK_MOVEABLE;
4405 /* Is this is an attribute revalidation, subject to softreval? */
4406 if (nfs_lookup_is_soft_revalidate(dentry))
4407 task_flags |= RPC_TASK_TIMEOUT;
4409 args.bitmask = nfs4_bitmask(server, fattr->label);
4411 nfs_fattr_init(fattr);
4413 dprintk("NFS call lookup %pd2\n", dentry);
4414 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4415 status = nfs4_do_call_sync(clnt, server, &msg,
4416 &args.seq_args, &res.seq_res, task_flags);
4417 dprintk("NFS reply lookup: %d\n", status);
4421 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4423 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4424 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4425 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4429 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4430 struct dentry *dentry, struct nfs_fh *fhandle,
4431 struct nfs_fattr *fattr)
4433 struct nfs4_exception exception = {
4434 .interruptible = true,
4436 struct rpc_clnt *client = *clnt;
4437 const struct qstr *name = &dentry->d_name;
4440 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr);
4441 trace_nfs4_lookup(dir, name, err);
4443 case -NFS4ERR_BADNAME:
4446 case -NFS4ERR_MOVED:
4447 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4448 if (err == -NFS4ERR_MOVED)
4449 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4451 case -NFS4ERR_WRONGSEC:
4453 if (client != *clnt)
4455 client = nfs4_negotiate_security(client, dir, name);
4457 return PTR_ERR(client);
4459 exception.retry = 1;
4462 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4464 } while (exception.retry);
4469 else if (client != *clnt)
4470 rpc_shutdown_client(client);
4475 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4476 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4479 struct rpc_clnt *client = NFS_CLIENT(dir);
4481 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4482 if (client != NFS_CLIENT(dir)) {
4483 rpc_shutdown_client(client);
4484 nfs_fixup_secinfo_attributes(fattr);
4490 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4491 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4493 struct rpc_clnt *client = NFS_CLIENT(dir);
4496 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4498 return ERR_PTR(status);
4499 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4502 static int _nfs4_proc_lookupp(struct inode *inode,
4503 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4505 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4506 struct nfs_server *server = NFS_SERVER(inode);
4508 struct nfs4_lookupp_arg args = {
4509 .bitmask = server->attr_bitmask,
4510 .fh = NFS_FH(inode),
4512 struct nfs4_lookupp_res res = {
4517 struct rpc_message msg = {
4518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4522 unsigned short task_flags = 0;
4524 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4525 task_flags |= RPC_TASK_TIMEOUT;
4527 args.bitmask = nfs4_bitmask(server, fattr->label);
4529 nfs_fattr_init(fattr);
4531 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4532 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4533 &res.seq_res, task_flags);
4534 dprintk("NFS reply lookupp: %d\n", status);
4538 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4539 struct nfs_fattr *fattr)
4541 struct nfs4_exception exception = {
4542 .interruptible = true,
4546 err = _nfs4_proc_lookupp(inode, fhandle, fattr);
4547 trace_nfs4_lookupp(inode, err);
4548 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4550 } while (exception.retry);
4554 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4555 const struct cred *cred)
4557 struct nfs_server *server = NFS_SERVER(inode);
4558 struct nfs4_accessargs args = {
4559 .fh = NFS_FH(inode),
4560 .access = entry->mask,
4562 struct nfs4_accessres res = {
4565 struct rpc_message msg = {
4566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4573 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4574 res.fattr = nfs_alloc_fattr();
4575 if (res.fattr == NULL)
4577 args.bitmask = server->cache_consistency_bitmask;
4579 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4581 nfs_access_set_mask(entry, res.access);
4583 nfs_refresh_inode(inode, res.fattr);
4585 nfs_free_fattr(res.fattr);
4589 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4590 const struct cred *cred)
4592 struct nfs4_exception exception = {
4593 .interruptible = true,
4597 err = _nfs4_proc_access(inode, entry, cred);
4598 trace_nfs4_access(inode, err);
4599 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4601 } while (exception.retry);
4606 * TODO: For the time being, we don't try to get any attributes
4607 * along with any of the zero-copy operations READ, READDIR,
4610 * In the case of the first three, we want to put the GETATTR
4611 * after the read-type operation -- this is because it is hard
4612 * to predict the length of a GETATTR response in v4, and thus
4613 * align the READ data correctly. This means that the GETATTR
4614 * may end up partially falling into the page cache, and we should
4615 * shift it into the 'tail' of the xdr_buf before processing.
4616 * To do this efficiently, we need to know the total length
4617 * of data received, which doesn't seem to be available outside
4620 * In the case of WRITE, we also want to put the GETATTR after
4621 * the operation -- in this case because we want to make sure
4622 * we get the post-operation mtime and size.
4624 * Both of these changes to the XDR layer would in fact be quite
4625 * minor, but I decided to leave them for a subsequent patch.
4627 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4628 unsigned int pgbase, unsigned int pglen)
4630 struct nfs4_readlink args = {
4631 .fh = NFS_FH(inode),
4636 struct nfs4_readlink_res res;
4637 struct rpc_message msg = {
4638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4643 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4646 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4647 unsigned int pgbase, unsigned int pglen)
4649 struct nfs4_exception exception = {
4650 .interruptible = true,
4654 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4655 trace_nfs4_readlink(inode, err);
4656 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4658 } while (exception.retry);
4663 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4666 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4669 struct nfs_server *server = NFS_SERVER(dir);
4670 struct nfs4_label l, *ilabel = NULL;
4671 struct nfs_open_context *ctx;
4672 struct nfs4_state *state;
4675 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4677 return PTR_ERR(ctx);
4679 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4681 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4682 sattr->ia_mode &= ~current_umask();
4683 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4684 if (IS_ERR(state)) {
4685 status = PTR_ERR(state);
4689 nfs4_label_release_security(ilabel);
4690 put_nfs_open_context(ctx);
4695 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4697 struct nfs_server *server = NFS_SERVER(dir);
4698 struct nfs_removeargs args = {
4702 struct nfs_removeres res = {
4705 struct rpc_message msg = {
4706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4710 unsigned long timestamp = jiffies;
4713 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4715 spin_lock(&dir->i_lock);
4716 /* Removing a directory decrements nlink in the parent */
4717 if (ftype == NF4DIR && dir->i_nlink > 2)
4718 nfs4_dec_nlink_locked(dir);
4719 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4720 NFS_INO_INVALID_DATA);
4721 spin_unlock(&dir->i_lock);
4726 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4728 struct nfs4_exception exception = {
4729 .interruptible = true,
4731 struct inode *inode = d_inode(dentry);
4735 if (inode->i_nlink == 1)
4736 nfs4_inode_return_delegation(inode);
4738 nfs4_inode_make_writeable(inode);
4741 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4742 trace_nfs4_remove(dir, &dentry->d_name, err);
4743 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4745 } while (exception.retry);
4749 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4751 struct nfs4_exception exception = {
4752 .interruptible = true,
4757 err = _nfs4_proc_remove(dir, name, NF4DIR);
4758 trace_nfs4_remove(dir, name, err);
4759 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4761 } while (exception.retry);
4765 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4766 struct dentry *dentry,
4767 struct inode *inode)
4769 struct nfs_removeargs *args = msg->rpc_argp;
4770 struct nfs_removeres *res = msg->rpc_resp;
4772 res->server = NFS_SB(dentry->d_sb);
4773 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4774 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4776 nfs_fattr_init(res->dir_attr);
4779 nfs4_inode_return_delegation(inode);
4780 nfs_d_prune_case_insensitive_aliases(inode);
4784 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4786 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4787 &data->args.seq_args,
4792 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4794 struct nfs_unlinkdata *data = task->tk_calldata;
4795 struct nfs_removeres *res = &data->res;
4797 if (!nfs4_sequence_done(task, &res->seq_res))
4799 if (nfs4_async_handle_error(task, res->server, NULL,
4800 &data->timeout) == -EAGAIN)
4802 if (task->tk_status == 0)
4803 nfs4_update_changeattr(dir, &res->cinfo,
4804 res->dir_attr->time_start,
4805 NFS_INO_INVALID_DATA);
4809 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4810 struct dentry *old_dentry,
4811 struct dentry *new_dentry)
4813 struct nfs_renameargs *arg = msg->rpc_argp;
4814 struct nfs_renameres *res = msg->rpc_resp;
4815 struct inode *old_inode = d_inode(old_dentry);
4816 struct inode *new_inode = d_inode(new_dentry);
4819 nfs4_inode_make_writeable(old_inode);
4821 nfs4_inode_return_delegation(new_inode);
4822 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4823 res->server = NFS_SB(old_dentry->d_sb);
4824 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4827 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4829 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4830 &data->args.seq_args,
4835 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4836 struct inode *new_dir)
4838 struct nfs_renamedata *data = task->tk_calldata;
4839 struct nfs_renameres *res = &data->res;
4841 if (!nfs4_sequence_done(task, &res->seq_res))
4843 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4846 if (task->tk_status == 0) {
4847 nfs_d_prune_case_insensitive_aliases(d_inode(data->old_dentry));
4848 if (new_dir != old_dir) {
4849 /* Note: If we moved a directory, nlink will change */
4850 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4851 res->old_fattr->time_start,
4852 NFS_INO_INVALID_NLINK |
4853 NFS_INO_INVALID_DATA);
4854 nfs4_update_changeattr(new_dir, &res->new_cinfo,
4855 res->new_fattr->time_start,
4856 NFS_INO_INVALID_NLINK |
4857 NFS_INO_INVALID_DATA);
4859 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4860 res->old_fattr->time_start,
4861 NFS_INO_INVALID_DATA);
4866 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4868 struct nfs_server *server = NFS_SERVER(inode);
4869 __u32 bitmask[NFS4_BITMASK_SZ];
4870 struct nfs4_link_arg arg = {
4871 .fh = NFS_FH(inode),
4872 .dir_fh = NFS_FH(dir),
4876 struct nfs4_link_res res = {
4879 struct rpc_message msg = {
4880 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4884 int status = -ENOMEM;
4886 res.fattr = nfs_alloc_fattr_with_label(server);
4887 if (res.fattr == NULL)
4890 nfs4_inode_make_writeable(inode);
4891 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label), inode,
4892 NFS_INO_INVALID_CHANGE);
4893 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4895 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4896 NFS_INO_INVALID_DATA);
4897 nfs4_inc_nlink(inode);
4898 status = nfs_post_op_update_inode(inode, res.fattr);
4900 nfs_setsecurity(inode, res.fattr);
4904 nfs_free_fattr(res.fattr);
4908 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4910 struct nfs4_exception exception = {
4911 .interruptible = true,
4915 err = nfs4_handle_exception(NFS_SERVER(inode),
4916 _nfs4_proc_link(inode, dir, name),
4918 } while (exception.retry);
4922 struct nfs4_createdata {
4923 struct rpc_message msg;
4924 struct nfs4_create_arg arg;
4925 struct nfs4_create_res res;
4927 struct nfs_fattr fattr;
4930 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4931 const struct qstr *name, struct iattr *sattr, u32 ftype)
4933 struct nfs4_createdata *data;
4935 data = kzalloc(sizeof(*data), GFP_KERNEL);
4937 struct nfs_server *server = NFS_SERVER(dir);
4939 data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL);
4940 if (IS_ERR(data->fattr.label))
4943 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4944 data->msg.rpc_argp = &data->arg;
4945 data->msg.rpc_resp = &data->res;
4946 data->arg.dir_fh = NFS_FH(dir);
4947 data->arg.server = server;
4948 data->arg.name = name;
4949 data->arg.attrs = sattr;
4950 data->arg.ftype = ftype;
4951 data->arg.bitmask = nfs4_bitmask(server, data->fattr.label);
4952 data->arg.umask = current_umask();
4953 data->res.server = server;
4954 data->res.fh = &data->fh;
4955 data->res.fattr = &data->fattr;
4956 nfs_fattr_init(data->res.fattr);
4964 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4966 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4967 &data->arg.seq_args, &data->res.seq_res, 1);
4969 spin_lock(&dir->i_lock);
4970 /* Creating a directory bumps nlink in the parent */
4971 if (data->arg.ftype == NF4DIR)
4972 nfs4_inc_nlink_locked(dir);
4973 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4974 data->res.fattr->time_start,
4975 NFS_INO_INVALID_DATA);
4976 spin_unlock(&dir->i_lock);
4977 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
4982 static void nfs4_free_createdata(struct nfs4_createdata *data)
4984 nfs4_label_free(data->fattr.label);
4988 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4989 struct page *page, unsigned int len, struct iattr *sattr,
4990 struct nfs4_label *label)
4992 struct nfs4_createdata *data;
4993 int status = -ENAMETOOLONG;
4995 if (len > NFS4_MAXPATHLEN)
4999 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
5003 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
5004 data->arg.u.symlink.pages = &page;
5005 data->arg.u.symlink.len = len;
5006 data->arg.label = label;
5008 status = nfs4_do_create(dir, dentry, data);
5010 nfs4_free_createdata(data);
5015 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5016 struct page *page, unsigned int len, struct iattr *sattr)
5018 struct nfs4_exception exception = {
5019 .interruptible = true,
5021 struct nfs4_label l, *label = NULL;
5024 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5027 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
5028 trace_nfs4_symlink(dir, &dentry->d_name, err);
5029 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5031 } while (exception.retry);
5033 nfs4_label_release_security(label);
5037 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5038 struct iattr *sattr, struct nfs4_label *label)
5040 struct nfs4_createdata *data;
5041 int status = -ENOMEM;
5043 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
5047 data->arg.label = label;
5048 status = nfs4_do_create(dir, dentry, data);
5050 nfs4_free_createdata(data);
5055 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5056 struct iattr *sattr)
5058 struct nfs_server *server = NFS_SERVER(dir);
5059 struct nfs4_exception exception = {
5060 .interruptible = true,
5062 struct nfs4_label l, *label = NULL;
5065 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5067 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5068 sattr->ia_mode &= ~current_umask();
5070 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
5071 trace_nfs4_mkdir(dir, &dentry->d_name, err);
5072 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5074 } while (exception.retry);
5075 nfs4_label_release_security(label);
5080 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5081 struct nfs_readdir_res *nr_res)
5083 struct inode *dir = d_inode(nr_arg->dentry);
5084 struct nfs_server *server = NFS_SERVER(dir);
5085 struct nfs4_readdir_arg args = {
5087 .pages = nr_arg->pages,
5089 .count = nr_arg->page_len,
5090 .plus = nr_arg->plus,
5092 struct nfs4_readdir_res res;
5093 struct rpc_message msg = {
5094 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5097 .rpc_cred = nr_arg->cred,
5101 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5102 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5103 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5104 args.bitmask = server->attr_bitmask_nl;
5106 args.bitmask = server->attr_bitmask;
5108 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5109 res.pgbase = args.pgbase;
5110 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5113 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5114 status += args.pgbase;
5117 nfs_invalidate_atime(dir);
5119 dprintk("%s: returns %d\n", __func__, status);
5123 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5124 struct nfs_readdir_res *res)
5126 struct nfs4_exception exception = {
5127 .interruptible = true,
5131 err = _nfs4_proc_readdir(arg, res);
5132 trace_nfs4_readdir(d_inode(arg->dentry), err);
5133 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5135 } while (exception.retry);
5139 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5140 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5142 struct nfs4_createdata *data;
5143 int mode = sattr->ia_mode;
5144 int status = -ENOMEM;
5146 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5151 data->arg.ftype = NF4FIFO;
5152 else if (S_ISBLK(mode)) {
5153 data->arg.ftype = NF4BLK;
5154 data->arg.u.device.specdata1 = MAJOR(rdev);
5155 data->arg.u.device.specdata2 = MINOR(rdev);
5157 else if (S_ISCHR(mode)) {
5158 data->arg.ftype = NF4CHR;
5159 data->arg.u.device.specdata1 = MAJOR(rdev);
5160 data->arg.u.device.specdata2 = MINOR(rdev);
5161 } else if (!S_ISSOCK(mode)) {
5166 data->arg.label = label;
5167 status = nfs4_do_create(dir, dentry, data);
5169 nfs4_free_createdata(data);
5174 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5175 struct iattr *sattr, dev_t rdev)
5177 struct nfs_server *server = NFS_SERVER(dir);
5178 struct nfs4_exception exception = {
5179 .interruptible = true,
5181 struct nfs4_label l, *label = NULL;
5184 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5186 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5187 sattr->ia_mode &= ~current_umask();
5189 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5190 trace_nfs4_mknod(dir, &dentry->d_name, err);
5191 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5193 } while (exception.retry);
5195 nfs4_label_release_security(label);
5200 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5201 struct nfs_fsstat *fsstat)
5203 struct nfs4_statfs_arg args = {
5205 .bitmask = server->attr_bitmask,
5207 struct nfs4_statfs_res res = {
5210 struct rpc_message msg = {
5211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5216 nfs_fattr_init(fsstat->fattr);
5217 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5220 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5222 struct nfs4_exception exception = {
5223 .interruptible = true,
5227 err = nfs4_handle_exception(server,
5228 _nfs4_proc_statfs(server, fhandle, fsstat),
5230 } while (exception.retry);
5234 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5235 struct nfs_fsinfo *fsinfo)
5237 struct nfs4_fsinfo_arg args = {
5239 .bitmask = server->attr_bitmask,
5241 struct nfs4_fsinfo_res res = {
5244 struct rpc_message msg = {
5245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5250 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5253 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5255 struct nfs4_exception exception = {
5256 .interruptible = true,
5261 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5262 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5264 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5267 err = nfs4_handle_exception(server, err, &exception);
5268 } while (exception.retry);
5272 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5276 nfs_fattr_init(fsinfo->fattr);
5277 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5279 /* block layout checks this! */
5280 server->pnfs_blksize = fsinfo->blksize;
5281 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5287 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5288 struct nfs_pathconf *pathconf)
5290 struct nfs4_pathconf_arg args = {
5292 .bitmask = server->attr_bitmask,
5294 struct nfs4_pathconf_res res = {
5295 .pathconf = pathconf,
5297 struct rpc_message msg = {
5298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5303 /* None of the pathconf attributes are mandatory to implement */
5304 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5305 memset(pathconf, 0, sizeof(*pathconf));
5309 nfs_fattr_init(pathconf->fattr);
5310 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5313 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5314 struct nfs_pathconf *pathconf)
5316 struct nfs4_exception exception = {
5317 .interruptible = true,
5322 err = nfs4_handle_exception(server,
5323 _nfs4_proc_pathconf(server, fhandle, pathconf),
5325 } while (exception.retry);
5329 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5330 const struct nfs_open_context *ctx,
5331 const struct nfs_lock_context *l_ctx,
5334 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5336 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5338 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5339 const struct nfs_open_context *ctx,
5340 const struct nfs_lock_context *l_ctx,
5343 nfs4_stateid _current_stateid;
5345 /* If the current stateid represents a lost lock, then exit */
5346 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5348 return nfs4_stateid_match(stateid, &_current_stateid);
5351 static bool nfs4_error_stateid_expired(int err)
5354 case -NFS4ERR_DELEG_REVOKED:
5355 case -NFS4ERR_ADMIN_REVOKED:
5356 case -NFS4ERR_BAD_STATEID:
5357 case -NFS4ERR_STALE_STATEID:
5358 case -NFS4ERR_OLD_STATEID:
5359 case -NFS4ERR_OPENMODE:
5360 case -NFS4ERR_EXPIRED:
5366 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5368 struct nfs_server *server = NFS_SERVER(hdr->inode);
5370 trace_nfs4_read(hdr, task->tk_status);
5371 if (task->tk_status < 0) {
5372 struct nfs4_exception exception = {
5373 .inode = hdr->inode,
5374 .state = hdr->args.context->state,
5375 .stateid = &hdr->args.stateid,
5377 task->tk_status = nfs4_async_handle_exception(task,
5378 server, task->tk_status, &exception);
5379 if (exception.retry) {
5380 rpc_restart_call_prepare(task);
5385 if (task->tk_status > 0)
5386 renew_lease(server, hdr->timestamp);
5390 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5391 struct nfs_pgio_args *args)
5394 if (!nfs4_error_stateid_expired(task->tk_status) ||
5395 nfs4_stateid_is_current(&args->stateid,
5400 rpc_restart_call_prepare(task);
5404 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5405 struct nfs_pgio_header *hdr)
5407 struct nfs_server *server = NFS_SERVER(hdr->inode);
5408 struct rpc_message *msg = &task->tk_msg;
5410 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5411 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5412 server->caps &= ~NFS_CAP_READ_PLUS;
5413 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5414 rpc_restart_call_prepare(task);
5420 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5422 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5424 if (nfs4_read_stateid_changed(task, &hdr->args))
5426 if (nfs4_read_plus_not_supported(task, hdr))
5428 if (task->tk_status > 0)
5429 nfs_invalidate_atime(hdr->inode);
5430 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5431 nfs4_read_done_cb(task, hdr);
5434 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5435 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5436 struct rpc_message *msg)
5438 /* Note: We don't use READ_PLUS with pNFS yet */
5439 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
5440 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5443 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5444 struct rpc_message *msg)
5447 #endif /* CONFIG_NFS_V4_2 */
5449 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5450 struct rpc_message *msg)
5452 hdr->timestamp = jiffies;
5453 if (!hdr->pgio_done_cb)
5454 hdr->pgio_done_cb = nfs4_read_done_cb;
5455 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5456 nfs42_read_plus_support(hdr, msg);
5457 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5460 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5461 struct nfs_pgio_header *hdr)
5463 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5464 &hdr->args.seq_args,
5468 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5469 hdr->args.lock_context,
5470 hdr->rw_mode) == -EIO)
5472 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5477 static int nfs4_write_done_cb(struct rpc_task *task,
5478 struct nfs_pgio_header *hdr)
5480 struct inode *inode = hdr->inode;
5482 trace_nfs4_write(hdr, task->tk_status);
5483 if (task->tk_status < 0) {
5484 struct nfs4_exception exception = {
5485 .inode = hdr->inode,
5486 .state = hdr->args.context->state,
5487 .stateid = &hdr->args.stateid,
5489 task->tk_status = nfs4_async_handle_exception(task,
5490 NFS_SERVER(inode), task->tk_status,
5492 if (exception.retry) {
5493 rpc_restart_call_prepare(task);
5497 if (task->tk_status >= 0) {
5498 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5499 nfs_writeback_update_inode(hdr);
5504 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5505 struct nfs_pgio_args *args)
5508 if (!nfs4_error_stateid_expired(task->tk_status) ||
5509 nfs4_stateid_is_current(&args->stateid,
5514 rpc_restart_call_prepare(task);
5518 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5520 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5522 if (nfs4_write_stateid_changed(task, &hdr->args))
5524 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5525 nfs4_write_done_cb(task, hdr);
5529 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5531 /* Don't request attributes for pNFS or O_DIRECT writes */
5532 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5534 /* Otherwise, request attributes if and only if we don't hold
5537 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5540 void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[],
5541 struct inode *inode, unsigned long cache_validity)
5543 struct nfs_server *server = NFS_SERVER(inode);
5546 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5547 cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity);
5549 if (cache_validity & NFS_INO_INVALID_CHANGE)
5550 bitmask[0] |= FATTR4_WORD0_CHANGE;
5551 if (cache_validity & NFS_INO_INVALID_ATIME)
5552 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5553 if (cache_validity & NFS_INO_INVALID_MODE)
5554 bitmask[1] |= FATTR4_WORD1_MODE;
5555 if (cache_validity & NFS_INO_INVALID_OTHER)
5556 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5557 if (cache_validity & NFS_INO_INVALID_NLINK)
5558 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5559 if (cache_validity & NFS_INO_INVALID_CTIME)
5560 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5561 if (cache_validity & NFS_INO_INVALID_MTIME)
5562 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5563 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5564 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5566 if (cache_validity & NFS_INO_INVALID_SIZE)
5567 bitmask[0] |= FATTR4_WORD0_SIZE;
5569 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5570 bitmask[i] &= server->attr_bitmask[i];
5573 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5574 struct rpc_message *msg,
5575 struct rpc_clnt **clnt)
5577 struct nfs_server *server = NFS_SERVER(hdr->inode);
5579 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5580 hdr->args.bitmask = NULL;
5581 hdr->res.fattr = NULL;
5583 nfs4_bitmask_set(hdr->args.bitmask_store,
5584 server->cache_consistency_bitmask,
5585 hdr->inode, NFS_INO_INVALID_BLOCKS);
5586 hdr->args.bitmask = hdr->args.bitmask_store;
5589 if (!hdr->pgio_done_cb)
5590 hdr->pgio_done_cb = nfs4_write_done_cb;
5591 hdr->res.server = server;
5592 hdr->timestamp = jiffies;
5594 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5595 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5596 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5599 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5601 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5602 &data->args.seq_args,
5607 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5609 struct inode *inode = data->inode;
5611 trace_nfs4_commit(data, task->tk_status);
5612 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5613 NULL, NULL) == -EAGAIN) {
5614 rpc_restart_call_prepare(task);
5620 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5622 if (!nfs4_sequence_done(task, &data->res.seq_res))
5624 return data->commit_done_cb(task, data);
5627 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5628 struct rpc_clnt **clnt)
5630 struct nfs_server *server = NFS_SERVER(data->inode);
5632 if (data->commit_done_cb == NULL)
5633 data->commit_done_cb = nfs4_commit_done_cb;
5634 data->res.server = server;
5635 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5636 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5637 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5640 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5641 struct nfs_commitres *res)
5643 struct inode *dst_inode = file_inode(dst);
5644 struct nfs_server *server = NFS_SERVER(dst_inode);
5645 struct rpc_message msg = {
5646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5651 args->fh = NFS_FH(dst_inode);
5652 return nfs4_call_sync(server->client, server, &msg,
5653 &args->seq_args, &res->seq_res, 1);
5656 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5658 struct nfs_commitargs args = {
5662 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5663 struct nfs4_exception exception = { };
5667 status = _nfs4_proc_commit(dst, &args, res);
5668 status = nfs4_handle_exception(dst_server, status, &exception);
5669 } while (exception.retry);
5674 struct nfs4_renewdata {
5675 struct nfs_client *client;
5676 unsigned long timestamp;
5680 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5681 * standalone procedure for queueing an asynchronous RENEW.
5683 static void nfs4_renew_release(void *calldata)
5685 struct nfs4_renewdata *data = calldata;
5686 struct nfs_client *clp = data->client;
5688 if (refcount_read(&clp->cl_count) > 1)
5689 nfs4_schedule_state_renewal(clp);
5690 nfs_put_client(clp);
5694 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5696 struct nfs4_renewdata *data = calldata;
5697 struct nfs_client *clp = data->client;
5698 unsigned long timestamp = data->timestamp;
5700 trace_nfs4_renew_async(clp, task->tk_status);
5701 switch (task->tk_status) {
5704 case -NFS4ERR_LEASE_MOVED:
5705 nfs4_schedule_lease_moved_recovery(clp);
5708 /* Unless we're shutting down, schedule state recovery! */
5709 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5711 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5712 nfs4_schedule_lease_recovery(clp);
5715 nfs4_schedule_path_down_recovery(clp);
5717 do_renew_lease(clp, timestamp);
5720 static const struct rpc_call_ops nfs4_renew_ops = {
5721 .rpc_call_done = nfs4_renew_done,
5722 .rpc_release = nfs4_renew_release,
5725 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5727 struct rpc_message msg = {
5728 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5732 struct nfs4_renewdata *data;
5734 if (renew_flags == 0)
5736 if (!refcount_inc_not_zero(&clp->cl_count))
5738 data = kmalloc(sizeof(*data), GFP_NOFS);
5740 nfs_put_client(clp);
5744 data->timestamp = jiffies;
5745 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5746 &nfs4_renew_ops, data);
5749 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5751 struct rpc_message msg = {
5752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5756 unsigned long now = jiffies;
5759 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5762 do_renew_lease(clp, now);
5766 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5768 return server->caps & NFS_CAP_ACLS;
5771 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5772 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5775 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5777 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5778 struct page **pages)
5780 struct page *newpage, **spages;
5786 len = min_t(size_t, PAGE_SIZE, buflen);
5787 newpage = alloc_page(GFP_KERNEL);
5789 if (newpage == NULL)
5791 memcpy(page_address(newpage), buf, len);
5796 } while (buflen != 0);
5802 __free_page(spages[rc-1]);
5806 struct nfs4_cached_acl {
5812 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5814 struct nfs_inode *nfsi = NFS_I(inode);
5816 spin_lock(&inode->i_lock);
5817 kfree(nfsi->nfs4_acl);
5818 nfsi->nfs4_acl = acl;
5819 spin_unlock(&inode->i_lock);
5822 static void nfs4_zap_acl_attr(struct inode *inode)
5824 nfs4_set_cached_acl(inode, NULL);
5827 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5829 struct nfs_inode *nfsi = NFS_I(inode);
5830 struct nfs4_cached_acl *acl;
5833 spin_lock(&inode->i_lock);
5834 acl = nfsi->nfs4_acl;
5837 if (buf == NULL) /* user is just asking for length */
5839 if (acl->cached == 0)
5841 ret = -ERANGE; /* see getxattr(2) man page */
5842 if (acl->len > buflen)
5844 memcpy(buf, acl->data, acl->len);
5848 spin_unlock(&inode->i_lock);
5852 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5854 struct nfs4_cached_acl *acl;
5855 size_t buflen = sizeof(*acl) + acl_len;
5857 if (buflen <= PAGE_SIZE) {
5858 acl = kmalloc(buflen, GFP_KERNEL);
5862 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5864 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5871 nfs4_set_cached_acl(inode, acl);
5875 * The getxattr API returns the required buffer length when called with a
5876 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5877 * the required buf. On a NULL buf, we send a page of data to the server
5878 * guessing that the ACL request can be serviced by a page. If so, we cache
5879 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5880 * the cache. If not so, we throw away the page, and cache the required
5881 * length. The next getxattr call will then produce another round trip to
5882 * the server, this time with the input buf of the required size.
5884 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5886 struct page **pages;
5887 struct nfs_getaclargs args = {
5888 .fh = NFS_FH(inode),
5891 struct nfs_getaclres res = {
5894 struct rpc_message msg = {
5895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5899 unsigned int npages;
5900 int ret = -ENOMEM, i;
5901 struct nfs_server *server = NFS_SERVER(inode);
5904 buflen = server->rsize;
5906 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5907 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5911 args.acl_pages = pages;
5913 for (i = 0; i < npages; i++) {
5914 pages[i] = alloc_page(GFP_KERNEL);
5919 /* for decoding across pages */
5920 res.acl_scratch = alloc_page(GFP_KERNEL);
5921 if (!res.acl_scratch)
5924 args.acl_len = npages * PAGE_SIZE;
5926 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5927 __func__, buf, buflen, npages, args.acl_len);
5928 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5929 &msg, &args.seq_args, &res.seq_res, 0);
5933 /* Handle the case where the passed-in buffer is too short */
5934 if (res.acl_flags & NFS4_ACL_TRUNC) {
5935 /* Did the user only issue a request for the acl length? */
5941 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5943 if (res.acl_len > buflen) {
5947 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5952 for (i = 0; i < npages; i++)
5954 __free_page(pages[i]);
5955 if (res.acl_scratch)
5956 __free_page(res.acl_scratch);
5961 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5963 struct nfs4_exception exception = {
5964 .interruptible = true,
5968 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5969 trace_nfs4_get_acl(inode, ret);
5972 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5973 } while (exception.retry);
5977 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5979 struct nfs_server *server = NFS_SERVER(inode);
5982 if (!nfs4_server_supports_acls(server))
5984 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
5987 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5988 nfs_zap_acl_cache(inode);
5989 ret = nfs4_read_cached_acl(inode, buf, buflen);
5991 /* -ENOENT is returned if there is no ACL or if there is an ACL
5992 * but no cached acl data, just the acl length */
5994 return nfs4_get_acl_uncached(inode, buf, buflen);
5997 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5999 struct nfs_server *server = NFS_SERVER(inode);
6000 struct page *pages[NFS4ACL_MAXPAGES];
6001 struct nfs_setaclargs arg = {
6002 .fh = NFS_FH(inode),
6006 struct nfs_setaclres res;
6007 struct rpc_message msg = {
6008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
6012 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
6015 /* You can't remove system.nfs4_acl: */
6018 if (!nfs4_server_supports_acls(server))
6020 if (npages > ARRAY_SIZE(pages))
6022 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
6025 nfs4_inode_make_writeable(inode);
6026 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6029 * Free each page after tx, so the only ref left is
6030 * held by the network stack
6033 put_page(pages[i-1]);
6036 * Acl update can result in inode attribute update.
6037 * so mark the attribute cache invalid.
6039 spin_lock(&inode->i_lock);
6040 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
6041 NFS_INO_INVALID_CTIME |
6042 NFS_INO_REVAL_FORCED);
6043 spin_unlock(&inode->i_lock);
6044 nfs_access_zap_cache(inode);
6045 nfs_zap_acl_cache(inode);
6049 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
6051 struct nfs4_exception exception = { };
6054 err = __nfs4_proc_set_acl(inode, buf, buflen);
6055 trace_nfs4_set_acl(inode, err);
6056 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
6058 * no need to retry since the kernel
6059 * isn't involved in encoding the ACEs.
6064 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6066 } while (exception.retry);
6070 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6071 static int _nfs4_get_security_label(struct inode *inode, void *buf,
6074 struct nfs_server *server = NFS_SERVER(inode);
6075 struct nfs4_label label = {0, 0, buflen, buf};
6077 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6078 struct nfs_fattr fattr = {
6081 struct nfs4_getattr_arg arg = {
6082 .fh = NFS_FH(inode),
6085 struct nfs4_getattr_res res = {
6089 struct rpc_message msg = {
6090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6096 nfs_fattr_init(&fattr);
6098 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6101 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6106 static int nfs4_get_security_label(struct inode *inode, void *buf,
6109 struct nfs4_exception exception = {
6110 .interruptible = true,
6114 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6118 err = _nfs4_get_security_label(inode, buf, buflen);
6119 trace_nfs4_get_security_label(inode, err);
6120 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6122 } while (exception.retry);
6126 static int _nfs4_do_set_security_label(struct inode *inode,
6127 struct nfs4_label *ilabel,
6128 struct nfs_fattr *fattr)
6131 struct iattr sattr = {0};
6132 struct nfs_server *server = NFS_SERVER(inode);
6133 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6134 struct nfs_setattrargs arg = {
6135 .fh = NFS_FH(inode),
6141 struct nfs_setattrres res = {
6145 struct rpc_message msg = {
6146 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6152 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6154 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6156 dprintk("%s failed: %d\n", __func__, status);
6161 static int nfs4_do_set_security_label(struct inode *inode,
6162 struct nfs4_label *ilabel,
6163 struct nfs_fattr *fattr)
6165 struct nfs4_exception exception = { };
6169 err = _nfs4_do_set_security_label(inode, ilabel, fattr);
6170 trace_nfs4_set_security_label(inode, err);
6171 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6173 } while (exception.retry);
6178 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6180 struct nfs4_label ilabel = {0, 0, buflen, (char *)buf };
6181 struct nfs_fattr *fattr;
6184 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6187 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
6191 status = nfs4_do_set_security_label(inode, &ilabel, fattr);
6193 nfs_setsecurity(inode, fattr);
6197 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6200 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6201 nfs4_verifier *bootverf)
6205 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6206 /* An impossible timestamp guarantees this value
6207 * will never match a generated boot time. */
6208 verf[0] = cpu_to_be32(U32_MAX);
6209 verf[1] = cpu_to_be32(U32_MAX);
6211 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6212 u64 ns = ktime_to_ns(nn->boot_time);
6214 verf[0] = cpu_to_be32(ns >> 32);
6215 verf[1] = cpu_to_be32(ns);
6217 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6221 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6223 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6224 struct nfs_netns_client *nn_clp = nn->nfs_client;
6231 id = rcu_dereference(nn_clp->identifier);
6233 strscpy(buf, id, buflen);
6237 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6238 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6244 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6246 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6251 if (clp->cl_owner_id != NULL)
6256 strlen(clp->cl_rpcclient->cl_nodename) +
6258 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6262 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6266 if (len > NFS4_OPAQUE_LIMIT + 1)
6270 * Since this string is allocated at mount time, and held until the
6271 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6272 * about a memory-reclaim deadlock.
6274 str = kmalloc(len, GFP_KERNEL);
6280 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6281 clp->cl_rpcclient->cl_nodename, buf,
6282 rpc_peeraddr2str(clp->cl_rpcclient,
6285 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6286 clp->cl_rpcclient->cl_nodename,
6287 rpc_peeraddr2str(clp->cl_rpcclient,
6291 clp->cl_owner_id = str;
6296 nfs4_init_uniform_client_string(struct nfs_client *clp)
6298 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6303 if (clp->cl_owner_id != NULL)
6306 len = 10 + 10 + 1 + 10 + 1 +
6307 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6309 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6313 if (len > NFS4_OPAQUE_LIMIT + 1)
6317 * Since this string is allocated at mount time, and held until the
6318 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6319 * about a memory-reclaim deadlock.
6321 str = kmalloc(len, GFP_KERNEL);
6326 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6327 clp->rpc_ops->version, clp->cl_minorversion,
6328 buf, clp->cl_rpcclient->cl_nodename);
6330 scnprintf(str, len, "Linux NFSv%u.%u %s",
6331 clp->rpc_ops->version, clp->cl_minorversion,
6332 clp->cl_rpcclient->cl_nodename);
6333 clp->cl_owner_id = str;
6338 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6339 * services. Advertise one based on the address family of the
6343 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6345 if (strchr(clp->cl_ipaddr, ':') != NULL)
6346 return scnprintf(buf, len, "tcp6");
6348 return scnprintf(buf, len, "tcp");
6351 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6353 struct nfs4_setclientid *sc = calldata;
6355 if (task->tk_status == 0)
6356 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6359 static const struct rpc_call_ops nfs4_setclientid_ops = {
6360 .rpc_call_done = nfs4_setclientid_done,
6364 * nfs4_proc_setclientid - Negotiate client ID
6365 * @clp: state data structure
6366 * @program: RPC program for NFSv4 callback service
6367 * @port: IP port number for NFS4 callback service
6368 * @cred: credential to use for this call
6369 * @res: where to place the result
6371 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6373 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6374 unsigned short port, const struct cred *cred,
6375 struct nfs4_setclientid_res *res)
6377 nfs4_verifier sc_verifier;
6378 struct nfs4_setclientid setclientid = {
6379 .sc_verifier = &sc_verifier,
6383 struct rpc_message msg = {
6384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6385 .rpc_argp = &setclientid,
6389 struct rpc_task_setup task_setup_data = {
6390 .rpc_client = clp->cl_rpcclient,
6391 .rpc_message = &msg,
6392 .callback_ops = &nfs4_setclientid_ops,
6393 .callback_data = &setclientid,
6394 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6396 unsigned long now = jiffies;
6399 /* nfs_client_id4 */
6400 nfs4_init_boot_verifier(clp, &sc_verifier);
6402 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6403 status = nfs4_init_uniform_client_string(clp);
6405 status = nfs4_init_nonuniform_client_string(clp);
6411 setclientid.sc_netid_len =
6412 nfs4_init_callback_netid(clp,
6413 setclientid.sc_netid,
6414 sizeof(setclientid.sc_netid));
6415 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6416 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6417 clp->cl_ipaddr, port >> 8, port & 255);
6419 dprintk("NFS call setclientid auth=%s, '%s'\n",
6420 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6423 status = nfs4_call_sync_custom(&task_setup_data);
6424 if (setclientid.sc_cred) {
6425 kfree(clp->cl_acceptor);
6426 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6427 put_rpccred(setclientid.sc_cred);
6431 do_renew_lease(clp, now);
6433 trace_nfs4_setclientid(clp, status);
6434 dprintk("NFS reply setclientid: %d\n", status);
6439 * nfs4_proc_setclientid_confirm - Confirm client ID
6440 * @clp: state data structure
6441 * @arg: result of a previous SETCLIENTID
6442 * @cred: credential to use for this call
6444 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6446 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6447 struct nfs4_setclientid_res *arg,
6448 const struct cred *cred)
6450 struct rpc_message msg = {
6451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6457 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6458 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6460 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6461 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6462 trace_nfs4_setclientid_confirm(clp, status);
6463 dprintk("NFS reply setclientid_confirm: %d\n", status);
6467 struct nfs4_delegreturndata {
6468 struct nfs4_delegreturnargs args;
6469 struct nfs4_delegreturnres res;
6471 nfs4_stateid stateid;
6472 unsigned long timestamp;
6474 struct nfs4_layoutreturn_args arg;
6475 struct nfs4_layoutreturn_res res;
6476 struct nfs4_xdr_opaque_data ld_private;
6480 struct nfs_fattr fattr;
6482 struct inode *inode;
6485 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6487 struct nfs4_delegreturndata *data = calldata;
6488 struct nfs4_exception exception = {
6489 .inode = data->inode,
6490 .stateid = &data->stateid,
6491 .task_is_privileged = data->args.seq_args.sa_privileged,
6494 if (!nfs4_sequence_done(task, &data->res.seq_res))
6497 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6499 /* Handle Layoutreturn errors */
6500 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6501 &data->res.lr_ret) == -EAGAIN)
6504 switch (task->tk_status) {
6506 renew_lease(data->res.server, data->timestamp);
6508 case -NFS4ERR_ADMIN_REVOKED:
6509 case -NFS4ERR_DELEG_REVOKED:
6510 case -NFS4ERR_EXPIRED:
6511 nfs4_free_revoked_stateid(data->res.server,
6513 task->tk_msg.rpc_cred);
6515 case -NFS4ERR_BAD_STATEID:
6516 case -NFS4ERR_STALE_STATEID:
6518 task->tk_status = 0;
6520 case -NFS4ERR_OLD_STATEID:
6521 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6522 nfs4_stateid_seqid_inc(&data->stateid);
6523 if (data->args.bitmask) {
6524 data->args.bitmask = NULL;
6525 data->res.fattr = NULL;
6528 case -NFS4ERR_ACCESS:
6529 if (data->args.bitmask) {
6530 data->args.bitmask = NULL;
6531 data->res.fattr = NULL;
6536 task->tk_status = nfs4_async_handle_exception(task,
6537 data->res.server, task->tk_status,
6539 if (exception.retry)
6542 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6543 data->rpc_status = task->tk_status;
6546 task->tk_status = 0;
6547 rpc_restart_call_prepare(task);
6550 static void nfs4_delegreturn_release(void *calldata)
6552 struct nfs4_delegreturndata *data = calldata;
6553 struct inode *inode = data->inode;
6556 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6559 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6560 nfs_iput_and_deactive(inode);
6565 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6567 struct nfs4_delegreturndata *d_data;
6568 struct pnfs_layout_hdr *lo;
6570 d_data = (struct nfs4_delegreturndata *)data;
6572 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6573 nfs4_sequence_done(task, &d_data->res.seq_res);
6577 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6578 if (lo && !pnfs_layout_is_valid(lo)) {
6579 d_data->args.lr_args = NULL;
6580 d_data->res.lr_res = NULL;
6583 nfs4_setup_sequence(d_data->res.server->nfs_client,
6584 &d_data->args.seq_args,
6585 &d_data->res.seq_res,
6589 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6590 .rpc_call_prepare = nfs4_delegreturn_prepare,
6591 .rpc_call_done = nfs4_delegreturn_done,
6592 .rpc_release = nfs4_delegreturn_release,
6595 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6597 struct nfs4_delegreturndata *data;
6598 struct nfs_server *server = NFS_SERVER(inode);
6599 struct rpc_task *task;
6600 struct rpc_message msg = {
6601 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6604 struct rpc_task_setup task_setup_data = {
6605 .rpc_client = server->client,
6606 .rpc_message = &msg,
6607 .callback_ops = &nfs4_delegreturn_ops,
6608 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
6612 data = kzalloc(sizeof(*data), GFP_NOFS);
6616 nfs4_state_protect(server->nfs_client,
6617 NFS_SP4_MACH_CRED_CLEANUP,
6618 &task_setup_data.rpc_client, &msg);
6620 data->args.fhandle = &data->fh;
6621 data->args.stateid = &data->stateid;
6622 nfs4_bitmask_set(data->args.bitmask_store,
6623 server->cache_consistency_bitmask, inode, 0);
6624 data->args.bitmask = data->args.bitmask_store;
6625 nfs_copy_fh(&data->fh, NFS_FH(inode));
6626 nfs4_stateid_copy(&data->stateid, stateid);
6627 data->res.fattr = &data->fattr;
6628 data->res.server = server;
6629 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6630 data->lr.arg.ld_private = &data->lr.ld_private;
6631 nfs_fattr_init(data->res.fattr);
6632 data->timestamp = jiffies;
6633 data->rpc_status = 0;
6634 data->inode = nfs_igrab_and_active(inode);
6635 if (data->inode || issync) {
6636 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6639 data->args.lr_args = &data->lr.arg;
6640 data->res.lr_res = &data->lr.res;
6645 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6648 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6650 task_setup_data.callback_data = data;
6651 msg.rpc_argp = &data->args;
6652 msg.rpc_resp = &data->res;
6653 task = rpc_run_task(&task_setup_data);
6655 return PTR_ERR(task);
6658 status = rpc_wait_for_completion_task(task);
6661 status = data->rpc_status;
6667 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6669 struct nfs_server *server = NFS_SERVER(inode);
6670 struct nfs4_exception exception = { };
6673 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6674 trace_nfs4_delegreturn(inode, stateid, err);
6676 case -NFS4ERR_STALE_STATEID:
6677 case -NFS4ERR_EXPIRED:
6681 err = nfs4_handle_exception(server, err, &exception);
6682 } while (exception.retry);
6686 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6688 struct inode *inode = state->inode;
6689 struct nfs_server *server = NFS_SERVER(inode);
6690 struct nfs_client *clp = server->nfs_client;
6691 struct nfs_lockt_args arg = {
6692 .fh = NFS_FH(inode),
6695 struct nfs_lockt_res res = {
6698 struct rpc_message msg = {
6699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6702 .rpc_cred = state->owner->so_cred,
6704 struct nfs4_lock_state *lsp;
6707 arg.lock_owner.clientid = clp->cl_clientid;
6708 status = nfs4_set_lock_state(state, request);
6711 lsp = request->fl_u.nfs4_fl.owner;
6712 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6713 arg.lock_owner.s_dev = server->s_dev;
6714 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6717 request->fl_type = F_UNLCK;
6719 case -NFS4ERR_DENIED:
6722 request->fl_ops->fl_release_private(request);
6723 request->fl_ops = NULL;
6728 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6730 struct nfs4_exception exception = {
6731 .interruptible = true,
6736 err = _nfs4_proc_getlk(state, cmd, request);
6737 trace_nfs4_get_lock(request, state, cmd, err);
6738 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6740 } while (exception.retry);
6745 * Update the seqid of a lock stateid after receiving
6746 * NFS4ERR_OLD_STATEID
6748 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6749 struct nfs4_lock_state *lsp)
6751 struct nfs4_state *state = lsp->ls_state;
6754 spin_lock(&state->state_lock);
6755 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6757 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6758 nfs4_stateid_seqid_inc(dst);
6760 dst->seqid = lsp->ls_stateid.seqid;
6763 spin_unlock(&state->state_lock);
6767 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6768 struct nfs4_lock_state *lsp)
6770 struct nfs4_state *state = lsp->ls_state;
6773 spin_lock(&state->state_lock);
6774 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6775 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6776 spin_unlock(&state->state_lock);
6780 struct nfs4_unlockdata {
6781 struct nfs_locku_args arg;
6782 struct nfs_locku_res res;
6783 struct nfs4_lock_state *lsp;
6784 struct nfs_open_context *ctx;
6785 struct nfs_lock_context *l_ctx;
6786 struct file_lock fl;
6787 struct nfs_server *server;
6788 unsigned long timestamp;
6791 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6792 struct nfs_open_context *ctx,
6793 struct nfs4_lock_state *lsp,
6794 struct nfs_seqid *seqid)
6796 struct nfs4_unlockdata *p;
6797 struct nfs4_state *state = lsp->ls_state;
6798 struct inode *inode = state->inode;
6800 p = kzalloc(sizeof(*p), GFP_NOFS);
6803 p->arg.fh = NFS_FH(inode);
6805 p->arg.seqid = seqid;
6806 p->res.seqid = seqid;
6808 /* Ensure we don't close file until we're done freeing locks! */
6809 p->ctx = get_nfs_open_context(ctx);
6810 p->l_ctx = nfs_get_lock_context(ctx);
6811 locks_init_lock(&p->fl);
6812 locks_copy_lock(&p->fl, fl);
6813 p->server = NFS_SERVER(inode);
6814 spin_lock(&state->state_lock);
6815 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6816 spin_unlock(&state->state_lock);
6820 static void nfs4_locku_release_calldata(void *data)
6822 struct nfs4_unlockdata *calldata = data;
6823 nfs_free_seqid(calldata->arg.seqid);
6824 nfs4_put_lock_state(calldata->lsp);
6825 nfs_put_lock_context(calldata->l_ctx);
6826 put_nfs_open_context(calldata->ctx);
6830 static void nfs4_locku_done(struct rpc_task *task, void *data)
6832 struct nfs4_unlockdata *calldata = data;
6833 struct nfs4_exception exception = {
6834 .inode = calldata->lsp->ls_state->inode,
6835 .stateid = &calldata->arg.stateid,
6838 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6840 switch (task->tk_status) {
6842 renew_lease(calldata->server, calldata->timestamp);
6843 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6844 if (nfs4_update_lock_stateid(calldata->lsp,
6845 &calldata->res.stateid))
6848 case -NFS4ERR_ADMIN_REVOKED:
6849 case -NFS4ERR_EXPIRED:
6850 nfs4_free_revoked_stateid(calldata->server,
6851 &calldata->arg.stateid,
6852 task->tk_msg.rpc_cred);
6854 case -NFS4ERR_BAD_STATEID:
6855 case -NFS4ERR_STALE_STATEID:
6856 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6858 rpc_restart_call_prepare(task);
6860 case -NFS4ERR_OLD_STATEID:
6861 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6863 rpc_restart_call_prepare(task);
6866 task->tk_status = nfs4_async_handle_exception(task,
6867 calldata->server, task->tk_status,
6869 if (exception.retry)
6870 rpc_restart_call_prepare(task);
6872 nfs_release_seqid(calldata->arg.seqid);
6875 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6877 struct nfs4_unlockdata *calldata = data;
6879 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6880 nfs_async_iocounter_wait(task, calldata->l_ctx))
6883 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6885 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6886 /* Note: exit _without_ running nfs4_locku_done */
6889 calldata->timestamp = jiffies;
6890 if (nfs4_setup_sequence(calldata->server->nfs_client,
6891 &calldata->arg.seq_args,
6892 &calldata->res.seq_res,
6894 nfs_release_seqid(calldata->arg.seqid);
6897 task->tk_action = NULL;
6899 nfs4_sequence_done(task, &calldata->res.seq_res);
6902 static const struct rpc_call_ops nfs4_locku_ops = {
6903 .rpc_call_prepare = nfs4_locku_prepare,
6904 .rpc_call_done = nfs4_locku_done,
6905 .rpc_release = nfs4_locku_release_calldata,
6908 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6909 struct nfs_open_context *ctx,
6910 struct nfs4_lock_state *lsp,
6911 struct nfs_seqid *seqid)
6913 struct nfs4_unlockdata *data;
6914 struct rpc_message msg = {
6915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6916 .rpc_cred = ctx->cred,
6918 struct rpc_task_setup task_setup_data = {
6919 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6920 .rpc_message = &msg,
6921 .callback_ops = &nfs4_locku_ops,
6922 .workqueue = nfsiod_workqueue,
6923 .flags = RPC_TASK_ASYNC,
6925 struct nfs_client *client =
6926 NFS_SERVER(lsp->ls_state->inode)->nfs_client;
6928 if (client->cl_minorversion)
6929 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6931 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6932 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6934 /* Ensure this is an unlock - when canceling a lock, the
6935 * canceled lock is passed in, and it won't be an unlock.
6937 fl->fl_type = F_UNLCK;
6938 if (fl->fl_flags & FL_CLOSE)
6939 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6941 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6943 nfs_free_seqid(seqid);
6944 return ERR_PTR(-ENOMEM);
6947 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6948 msg.rpc_argp = &data->arg;
6949 msg.rpc_resp = &data->res;
6950 task_setup_data.callback_data = data;
6951 return rpc_run_task(&task_setup_data);
6954 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6956 struct inode *inode = state->inode;
6957 struct nfs4_state_owner *sp = state->owner;
6958 struct nfs_inode *nfsi = NFS_I(inode);
6959 struct nfs_seqid *seqid;
6960 struct nfs4_lock_state *lsp;
6961 struct rpc_task *task;
6962 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6964 unsigned char fl_flags = request->fl_flags;
6966 status = nfs4_set_lock_state(state, request);
6967 /* Unlock _before_ we do the RPC call */
6968 request->fl_flags |= FL_EXISTS;
6969 /* Exclude nfs_delegation_claim_locks() */
6970 mutex_lock(&sp->so_delegreturn_mutex);
6971 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6972 down_read(&nfsi->rwsem);
6973 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6974 up_read(&nfsi->rwsem);
6975 mutex_unlock(&sp->so_delegreturn_mutex);
6978 up_read(&nfsi->rwsem);
6979 mutex_unlock(&sp->so_delegreturn_mutex);
6982 /* Is this a delegated lock? */
6983 lsp = request->fl_u.nfs4_fl.owner;
6984 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6986 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6987 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6991 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6992 status = PTR_ERR(task);
6995 status = rpc_wait_for_completion_task(task);
6998 request->fl_flags = fl_flags;
6999 trace_nfs4_unlock(request, state, F_SETLK, status);
7003 struct nfs4_lockdata {
7004 struct nfs_lock_args arg;
7005 struct nfs_lock_res res;
7006 struct nfs4_lock_state *lsp;
7007 struct nfs_open_context *ctx;
7008 struct file_lock fl;
7009 unsigned long timestamp;
7012 struct nfs_server *server;
7015 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
7016 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
7019 struct nfs4_lockdata *p;
7020 struct inode *inode = lsp->ls_state->inode;
7021 struct nfs_server *server = NFS_SERVER(inode);
7022 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7024 p = kzalloc(sizeof(*p), gfp_mask);
7028 p->arg.fh = NFS_FH(inode);
7030 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
7031 if (IS_ERR(p->arg.open_seqid))
7033 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
7034 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
7035 if (IS_ERR(p->arg.lock_seqid))
7036 goto out_free_seqid;
7037 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
7038 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
7039 p->arg.lock_owner.s_dev = server->s_dev;
7040 p->res.lock_seqid = p->arg.lock_seqid;
7043 p->ctx = get_nfs_open_context(ctx);
7044 locks_init_lock(&p->fl);
7045 locks_copy_lock(&p->fl, fl);
7048 nfs_free_seqid(p->arg.open_seqid);
7054 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7056 struct nfs4_lockdata *data = calldata;
7057 struct nfs4_state *state = data->lsp->ls_state;
7059 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
7061 /* Do we need to do an open_to_lock_owner? */
7062 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
7063 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
7064 goto out_release_lock_seqid;
7066 nfs4_stateid_copy(&data->arg.open_stateid,
7067 &state->open_stateid);
7068 data->arg.new_lock_owner = 1;
7069 data->res.open_seqid = data->arg.open_seqid;
7071 data->arg.new_lock_owner = 0;
7072 nfs4_stateid_copy(&data->arg.lock_stateid,
7073 &data->lsp->ls_stateid);
7075 if (!nfs4_valid_open_stateid(state)) {
7076 data->rpc_status = -EBADF;
7077 task->tk_action = NULL;
7078 goto out_release_open_seqid;
7080 data->timestamp = jiffies;
7081 if (nfs4_setup_sequence(data->server->nfs_client,
7082 &data->arg.seq_args,
7086 out_release_open_seqid:
7087 nfs_release_seqid(data->arg.open_seqid);
7088 out_release_lock_seqid:
7089 nfs_release_seqid(data->arg.lock_seqid);
7091 nfs4_sequence_done(task, &data->res.seq_res);
7092 dprintk("%s: ret = %d\n", __func__, data->rpc_status);
7095 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7097 struct nfs4_lockdata *data = calldata;
7098 struct nfs4_lock_state *lsp = data->lsp;
7100 if (!nfs4_sequence_done(task, &data->res.seq_res))
7103 data->rpc_status = task->tk_status;
7104 switch (task->tk_status) {
7106 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
7108 if (data->arg.new_lock && !data->cancelled) {
7109 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
7110 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7113 if (data->arg.new_lock_owner != 0) {
7114 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7115 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7116 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7117 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7120 case -NFS4ERR_BAD_STATEID:
7121 case -NFS4ERR_OLD_STATEID:
7122 case -NFS4ERR_STALE_STATEID:
7123 case -NFS4ERR_EXPIRED:
7124 if (data->arg.new_lock_owner != 0) {
7125 if (!nfs4_stateid_match(&data->arg.open_stateid,
7126 &lsp->ls_state->open_stateid))
7128 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7133 dprintk("%s: ret = %d!\n", __func__, data->rpc_status);
7136 if (!data->cancelled)
7137 rpc_restart_call_prepare(task);
7141 static void nfs4_lock_release(void *calldata)
7143 struct nfs4_lockdata *data = calldata;
7145 nfs_free_seqid(data->arg.open_seqid);
7146 if (data->cancelled && data->rpc_status == 0) {
7147 struct rpc_task *task;
7148 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7149 data->arg.lock_seqid);
7151 rpc_put_task_async(task);
7152 dprintk("%s: cancelling lock!\n", __func__);
7154 nfs_free_seqid(data->arg.lock_seqid);
7155 nfs4_put_lock_state(data->lsp);
7156 put_nfs_open_context(data->ctx);
7160 static const struct rpc_call_ops nfs4_lock_ops = {
7161 .rpc_call_prepare = nfs4_lock_prepare,
7162 .rpc_call_done = nfs4_lock_done,
7163 .rpc_release = nfs4_lock_release,
7166 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7169 case -NFS4ERR_ADMIN_REVOKED:
7170 case -NFS4ERR_EXPIRED:
7171 case -NFS4ERR_BAD_STATEID:
7172 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7173 if (new_lock_owner != 0 ||
7174 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7175 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7177 case -NFS4ERR_STALE_STATEID:
7178 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7179 nfs4_schedule_lease_recovery(server->nfs_client);
7183 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7185 struct nfs4_lockdata *data;
7186 struct rpc_task *task;
7187 struct rpc_message msg = {
7188 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7189 .rpc_cred = state->owner->so_cred,
7191 struct rpc_task_setup task_setup_data = {
7192 .rpc_client = NFS_CLIENT(state->inode),
7193 .rpc_message = &msg,
7194 .callback_ops = &nfs4_lock_ops,
7195 .workqueue = nfsiod_workqueue,
7196 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7199 struct nfs_client *client = NFS_SERVER(state->inode)->nfs_client;
7201 if (client->cl_minorversion)
7202 task_setup_data.flags |= RPC_TASK_MOVEABLE;
7204 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7205 fl->fl_u.nfs4_fl.owner,
7206 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
7210 data->arg.block = 1;
7211 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7212 recovery_type > NFS_LOCK_NEW);
7213 msg.rpc_argp = &data->arg;
7214 msg.rpc_resp = &data->res;
7215 task_setup_data.callback_data = data;
7216 if (recovery_type > NFS_LOCK_NEW) {
7217 if (recovery_type == NFS_LOCK_RECLAIM)
7218 data->arg.reclaim = NFS_LOCK_RECLAIM;
7220 data->arg.new_lock = 1;
7221 task = rpc_run_task(&task_setup_data);
7223 return PTR_ERR(task);
7224 ret = rpc_wait_for_completion_task(task);
7226 ret = data->rpc_status;
7228 nfs4_handle_setlk_error(data->server, data->lsp,
7229 data->arg.new_lock_owner, ret);
7231 data->cancelled = true;
7232 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7234 dprintk("%s: ret = %d\n", __func__, ret);
7238 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7240 struct nfs_server *server = NFS_SERVER(state->inode);
7241 struct nfs4_exception exception = {
7242 .inode = state->inode,
7247 /* Cache the lock if possible... */
7248 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7250 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7251 if (err != -NFS4ERR_DELAY)
7253 nfs4_handle_exception(server, err, &exception);
7254 } while (exception.retry);
7258 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7260 struct nfs_server *server = NFS_SERVER(state->inode);
7261 struct nfs4_exception exception = {
7262 .inode = state->inode,
7266 err = nfs4_set_lock_state(state, request);
7269 if (!recover_lost_locks) {
7270 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7274 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7276 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7280 case -NFS4ERR_GRACE:
7281 case -NFS4ERR_DELAY:
7282 nfs4_handle_exception(server, err, &exception);
7285 } while (exception.retry);
7290 #if defined(CONFIG_NFS_V4_1)
7291 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7293 struct nfs4_lock_state *lsp;
7296 status = nfs4_set_lock_state(state, request);
7299 lsp = request->fl_u.nfs4_fl.owner;
7300 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7301 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7303 return nfs4_lock_expired(state, request);
7307 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7309 struct nfs_inode *nfsi = NFS_I(state->inode);
7310 struct nfs4_state_owner *sp = state->owner;
7311 unsigned char fl_flags = request->fl_flags;
7314 request->fl_flags |= FL_ACCESS;
7315 status = locks_lock_inode_wait(state->inode, request);
7318 mutex_lock(&sp->so_delegreturn_mutex);
7319 down_read(&nfsi->rwsem);
7320 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7321 /* Yes: cache locks! */
7322 /* ...but avoid races with delegation recall... */
7323 request->fl_flags = fl_flags & ~FL_SLEEP;
7324 status = locks_lock_inode_wait(state->inode, request);
7325 up_read(&nfsi->rwsem);
7326 mutex_unlock(&sp->so_delegreturn_mutex);
7329 up_read(&nfsi->rwsem);
7330 mutex_unlock(&sp->so_delegreturn_mutex);
7331 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7333 request->fl_flags = fl_flags;
7337 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7339 struct nfs4_exception exception = {
7341 .inode = state->inode,
7342 .interruptible = true,
7347 err = _nfs4_proc_setlk(state, cmd, request);
7348 if (err == -NFS4ERR_DENIED)
7350 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7352 } while (exception.retry);
7356 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7357 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7360 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7361 struct file_lock *request)
7363 int status = -ERESTARTSYS;
7364 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7366 while(!signalled()) {
7367 status = nfs4_proc_setlk(state, cmd, request);
7368 if ((status != -EAGAIN) || IS_SETLK(cmd))
7370 freezable_schedule_timeout_interruptible(timeout);
7372 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7373 status = -ERESTARTSYS;
7378 #ifdef CONFIG_NFS_V4_1
7379 struct nfs4_lock_waiter {
7380 struct inode *inode;
7381 struct nfs_lowner owner;
7382 wait_queue_entry_t wait;
7386 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7388 struct nfs4_lock_waiter *waiter =
7389 container_of(wait, struct nfs4_lock_waiter, wait);
7391 /* NULL key means to wake up everyone */
7393 struct cb_notify_lock_args *cbnl = key;
7394 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7395 *wowner = &waiter->owner;
7397 /* Only wake if the callback was for the same owner. */
7398 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7401 /* Make sure it's for the right inode */
7402 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7406 return woken_wake_function(wait, mode, flags, key);
7410 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7412 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7413 struct nfs_server *server = NFS_SERVER(state->inode);
7414 struct nfs_client *clp = server->nfs_client;
7415 wait_queue_head_t *q = &clp->cl_lock_waitq;
7416 struct nfs4_lock_waiter waiter = {
7417 .inode = state->inode,
7418 .owner = { .clientid = clp->cl_clientid,
7419 .id = lsp->ls_seqid.owner_id,
7420 .s_dev = server->s_dev },
7424 /* Don't bother with waitqueue if we don't expect a callback */
7425 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7426 return nfs4_retry_setlk_simple(state, cmd, request);
7428 init_wait(&waiter.wait);
7429 waiter.wait.func = nfs4_wake_lock_waiter;
7430 add_wait_queue(q, &waiter.wait);
7433 status = nfs4_proc_setlk(state, cmd, request);
7434 if (status != -EAGAIN || IS_SETLK(cmd))
7437 status = -ERESTARTSYS;
7438 freezer_do_not_count();
7439 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE,
7440 NFS4_LOCK_MAXTIMEOUT);
7442 } while (!signalled());
7444 remove_wait_queue(q, &waiter.wait);
7448 #else /* !CONFIG_NFS_V4_1 */
7450 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7452 return nfs4_retry_setlk_simple(state, cmd, request);
7457 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7459 struct nfs_open_context *ctx;
7460 struct nfs4_state *state;
7463 /* verify open state */
7464 ctx = nfs_file_open_context(filp);
7467 if (IS_GETLK(cmd)) {
7469 return nfs4_proc_getlk(state, F_GETLK, request);
7473 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7476 if (request->fl_type == F_UNLCK) {
7478 return nfs4_proc_unlck(state, cmd, request);
7485 if ((request->fl_flags & FL_POSIX) &&
7486 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7490 * Don't rely on the VFS having checked the file open mode,
7491 * since it won't do this for flock() locks.
7493 switch (request->fl_type) {
7495 if (!(filp->f_mode & FMODE_READ))
7499 if (!(filp->f_mode & FMODE_WRITE))
7503 status = nfs4_set_lock_state(state, request);
7507 return nfs4_retry_setlk(state, cmd, request);
7510 static int nfs4_delete_lease(struct file *file, void **priv)
7512 return generic_setlease(file, F_UNLCK, NULL, priv);
7515 static int nfs4_add_lease(struct file *file, long arg, struct file_lock **lease,
7518 struct inode *inode = file_inode(file);
7519 fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7522 /* No delegation, no lease */
7523 if (!nfs4_have_delegation(inode, type))
7525 ret = generic_setlease(file, arg, lease, priv);
7526 if (ret || nfs4_have_delegation(inode, type))
7528 /* We raced with a delegation return */
7529 nfs4_delete_lease(file, priv);
7533 int nfs4_proc_setlease(struct file *file, long arg, struct file_lock **lease,
7539 return nfs4_add_lease(file, arg, lease, priv);
7541 return nfs4_delete_lease(file, priv);
7547 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7549 struct nfs_server *server = NFS_SERVER(state->inode);
7552 err = nfs4_set_lock_state(state, fl);
7556 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7557 if (err != -NFS4ERR_DELAY)
7560 } while (err == -NFS4ERR_DELAY);
7561 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7564 struct nfs_release_lockowner_data {
7565 struct nfs4_lock_state *lsp;
7566 struct nfs_server *server;
7567 struct nfs_release_lockowner_args args;
7568 struct nfs_release_lockowner_res res;
7569 unsigned long timestamp;
7572 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7574 struct nfs_release_lockowner_data *data = calldata;
7575 struct nfs_server *server = data->server;
7576 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7577 &data->res.seq_res, task);
7578 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7579 data->timestamp = jiffies;
7582 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7584 struct nfs_release_lockowner_data *data = calldata;
7585 struct nfs_server *server = data->server;
7587 nfs40_sequence_done(task, &data->res.seq_res);
7589 switch (task->tk_status) {
7591 renew_lease(server, data->timestamp);
7593 case -NFS4ERR_STALE_CLIENTID:
7594 case -NFS4ERR_EXPIRED:
7595 nfs4_schedule_lease_recovery(server->nfs_client);
7597 case -NFS4ERR_LEASE_MOVED:
7598 case -NFS4ERR_DELAY:
7599 if (nfs4_async_handle_error(task, server,
7600 NULL, NULL) == -EAGAIN)
7601 rpc_restart_call_prepare(task);
7605 static void nfs4_release_lockowner_release(void *calldata)
7607 struct nfs_release_lockowner_data *data = calldata;
7608 nfs4_free_lock_state(data->server, data->lsp);
7612 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7613 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7614 .rpc_call_done = nfs4_release_lockowner_done,
7615 .rpc_release = nfs4_release_lockowner_release,
7619 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7621 struct nfs_release_lockowner_data *data;
7622 struct rpc_message msg = {
7623 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7626 if (server->nfs_client->cl_mvops->minor_version != 0)
7629 data = kmalloc(sizeof(*data), GFP_NOFS);
7633 data->server = server;
7634 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7635 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7636 data->args.lock_owner.s_dev = server->s_dev;
7638 msg.rpc_argp = &data->args;
7639 msg.rpc_resp = &data->res;
7640 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7641 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7644 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7646 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7647 struct user_namespace *mnt_userns,
7648 struct dentry *unused, struct inode *inode,
7649 const char *key, const void *buf,
7650 size_t buflen, int flags)
7652 return nfs4_proc_set_acl(inode, buf, buflen);
7655 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7656 struct dentry *unused, struct inode *inode,
7657 const char *key, void *buf, size_t buflen)
7659 return nfs4_proc_get_acl(inode, buf, buflen);
7662 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7664 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7667 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7669 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7670 struct user_namespace *mnt_userns,
7671 struct dentry *unused, struct inode *inode,
7672 const char *key, const void *buf,
7673 size_t buflen, int flags)
7675 if (security_ismaclabel(key))
7676 return nfs4_set_security_label(inode, buf, buflen);
7681 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7682 struct dentry *unused, struct inode *inode,
7683 const char *key, void *buf, size_t buflen)
7685 if (security_ismaclabel(key))
7686 return nfs4_get_security_label(inode, buf, buflen);
7691 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7695 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7696 len = security_inode_listsecurity(inode, list, list_len);
7697 if (len >= 0 && list_len && len > list_len)
7703 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7704 .prefix = XATTR_SECURITY_PREFIX,
7705 .get = nfs4_xattr_get_nfs4_label,
7706 .set = nfs4_xattr_set_nfs4_label,
7712 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7719 #ifdef CONFIG_NFS_V4_2
7720 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7721 struct user_namespace *mnt_userns,
7722 struct dentry *unused, struct inode *inode,
7723 const char *key, const void *buf,
7724 size_t buflen, int flags)
7729 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7733 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7734 * flags right now. Handling of xattr operations use the normal
7735 * file read/write permissions.
7737 * Just in case the server has other ideas (which RFC 8276 allows),
7738 * do a cached access check for the XA* flags to possibly avoid
7739 * doing an RPC and getting EACCES back.
7741 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7742 if (!(mask & NFS_ACCESS_XAWRITE))
7747 ret = nfs42_proc_removexattr(inode, key);
7749 nfs4_xattr_cache_remove(inode, key);
7751 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7753 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7759 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7760 struct dentry *unused, struct inode *inode,
7761 const char *key, void *buf, size_t buflen)
7766 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7769 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7770 if (!(mask & NFS_ACCESS_XAREAD))
7774 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7778 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7779 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7782 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7788 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7797 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7800 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7801 if (!(mask & NFS_ACCESS_XALIST))
7805 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7809 ret = nfs4_xattr_cache_list(inode, list, list_len);
7810 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7815 buflen = list_len ? list_len : XATTR_LIST_MAX;
7816 buf = list_len ? list : NULL;
7820 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7833 nfs4_xattr_cache_set_list(inode, list, size);
7841 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7845 #endif /* CONFIG_NFS_V4_2 */
7848 * nfs_fhget will use either the mounted_on_fileid or the fileid
7850 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7852 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7853 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7854 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7855 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7858 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7859 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7860 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7864 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7865 const struct qstr *name,
7866 struct nfs4_fs_locations *fs_locations,
7869 struct nfs_server *server = NFS_SERVER(dir);
7871 struct nfs4_fs_locations_arg args = {
7872 .dir_fh = NFS_FH(dir),
7877 struct nfs4_fs_locations_res res = {
7878 .fs_locations = fs_locations,
7880 struct rpc_message msg = {
7881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7887 dprintk("%s: start\n", __func__);
7889 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7890 bitmask[1] = nfs4_fattr_bitmap[1];
7892 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7893 * is not supported */
7894 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7895 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7897 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7899 nfs_fattr_init(&fs_locations->fattr);
7900 fs_locations->server = server;
7901 fs_locations->nlocations = 0;
7902 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7903 dprintk("%s: returned status = %d\n", __func__, status);
7907 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7908 const struct qstr *name,
7909 struct nfs4_fs_locations *fs_locations,
7912 struct nfs4_exception exception = {
7913 .interruptible = true,
7917 err = _nfs4_proc_fs_locations(client, dir, name,
7918 fs_locations, page);
7919 trace_nfs4_get_fs_locations(dir, name, err);
7920 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7922 } while (exception.retry);
7927 * This operation also signals the server that this client is
7928 * performing migration recovery. The server can stop returning
7929 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7930 * appended to this compound to identify the client ID which is
7931 * performing recovery.
7933 static int _nfs40_proc_get_locations(struct nfs_server *server,
7934 struct nfs_fh *fhandle,
7935 struct nfs4_fs_locations *locations,
7936 struct page *page, const struct cred *cred)
7938 struct rpc_clnt *clnt = server->client;
7940 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7942 struct nfs4_fs_locations_arg args = {
7943 .clientid = server->nfs_client->cl_clientid,
7947 .migration = 1, /* skip LOOKUP */
7948 .renew = 1, /* append RENEW */
7950 struct nfs4_fs_locations_res res = {
7951 .fs_locations = locations,
7955 struct rpc_message msg = {
7956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7961 unsigned long now = jiffies;
7964 nfs_fattr_init(&locations->fattr);
7965 locations->server = server;
7966 locations->nlocations = 0;
7968 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7969 status = nfs4_call_sync_sequence(clnt, server, &msg,
7970 &args.seq_args, &res.seq_res);
7974 renew_lease(server, now);
7978 #ifdef CONFIG_NFS_V4_1
7981 * This operation also signals the server that this client is
7982 * performing migration recovery. The server can stop asserting
7983 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7984 * performing this operation is identified in the SEQUENCE
7985 * operation in this compound.
7987 * When the client supports GETATTR(fs_locations_info), it can
7988 * be plumbed in here.
7990 static int _nfs41_proc_get_locations(struct nfs_server *server,
7991 struct nfs_fh *fhandle,
7992 struct nfs4_fs_locations *locations,
7993 struct page *page, const struct cred *cred)
7995 struct rpc_clnt *clnt = server->client;
7997 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7999 struct nfs4_fs_locations_arg args = {
8003 .migration = 1, /* skip LOOKUP */
8005 struct nfs4_fs_locations_res res = {
8006 .fs_locations = locations,
8009 struct rpc_message msg = {
8010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8017 nfs_fattr_init(&locations->fattr);
8018 locations->server = server;
8019 locations->nlocations = 0;
8021 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8022 status = nfs4_call_sync_sequence(clnt, server, &msg,
8023 &args.seq_args, &res.seq_res);
8024 if (status == NFS4_OK &&
8025 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8026 status = -NFS4ERR_LEASE_MOVED;
8030 #endif /* CONFIG_NFS_V4_1 */
8033 * nfs4_proc_get_locations - discover locations for a migrated FSID
8034 * @server: pointer to nfs_server to process
8035 * @fhandle: pointer to the kernel NFS client file handle
8036 * @locations: result of query
8038 * @cred: credential to use for this operation
8040 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
8041 * operation failed, or a negative errno if a local error occurred.
8043 * On success, "locations" is filled in, but if the server has
8044 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
8047 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
8048 * from this client that require migration recovery.
8050 int nfs4_proc_get_locations(struct nfs_server *server,
8051 struct nfs_fh *fhandle,
8052 struct nfs4_fs_locations *locations,
8053 struct page *page, const struct cred *cred)
8055 struct nfs_client *clp = server->nfs_client;
8056 const struct nfs4_mig_recovery_ops *ops =
8057 clp->cl_mvops->mig_recovery_ops;
8058 struct nfs4_exception exception = {
8059 .interruptible = true,
8063 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8064 (unsigned long long)server->fsid.major,
8065 (unsigned long long)server->fsid.minor,
8067 nfs_display_fhandle(fhandle, __func__);
8070 status = ops->get_locations(server, fhandle, locations, page,
8072 if (status != -NFS4ERR_DELAY)
8074 nfs4_handle_exception(server, status, &exception);
8075 } while (exception.retry);
8080 * This operation also signals the server that this client is
8081 * performing "lease moved" recovery. The server can stop
8082 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
8083 * is appended to this compound to identify the client ID which is
8084 * performing recovery.
8086 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
8088 struct nfs_server *server = NFS_SERVER(inode);
8089 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8090 struct rpc_clnt *clnt = server->client;
8091 struct nfs4_fsid_present_arg args = {
8092 .fh = NFS_FH(inode),
8093 .clientid = clp->cl_clientid,
8094 .renew = 1, /* append RENEW */
8096 struct nfs4_fsid_present_res res = {
8099 struct rpc_message msg = {
8100 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8105 unsigned long now = jiffies;
8108 res.fh = nfs_alloc_fhandle();
8112 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8113 status = nfs4_call_sync_sequence(clnt, server, &msg,
8114 &args.seq_args, &res.seq_res);
8115 nfs_free_fhandle(res.fh);
8119 do_renew_lease(clp, now);
8123 #ifdef CONFIG_NFS_V4_1
8126 * This operation also signals the server that this client is
8127 * performing "lease moved" recovery. The server can stop asserting
8128 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8129 * this operation is identified in the SEQUENCE operation in this
8132 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8134 struct nfs_server *server = NFS_SERVER(inode);
8135 struct rpc_clnt *clnt = server->client;
8136 struct nfs4_fsid_present_arg args = {
8137 .fh = NFS_FH(inode),
8139 struct nfs4_fsid_present_res res = {
8141 struct rpc_message msg = {
8142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8149 res.fh = nfs_alloc_fhandle();
8153 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8154 status = nfs4_call_sync_sequence(clnt, server, &msg,
8155 &args.seq_args, &res.seq_res);
8156 nfs_free_fhandle(res.fh);
8157 if (status == NFS4_OK &&
8158 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8159 status = -NFS4ERR_LEASE_MOVED;
8163 #endif /* CONFIG_NFS_V4_1 */
8166 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8167 * @inode: inode on FSID to check
8168 * @cred: credential to use for this operation
8170 * Server indicates whether the FSID is present, moved, or not
8171 * recognized. This operation is necessary to clear a LEASE_MOVED
8172 * condition for this client ID.
8174 * Returns NFS4_OK if the FSID is present on this server,
8175 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8176 * NFS4ERR code if some error occurred on the server, or a
8177 * negative errno if a local failure occurred.
8179 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8181 struct nfs_server *server = NFS_SERVER(inode);
8182 struct nfs_client *clp = server->nfs_client;
8183 const struct nfs4_mig_recovery_ops *ops =
8184 clp->cl_mvops->mig_recovery_ops;
8185 struct nfs4_exception exception = {
8186 .interruptible = true,
8190 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8191 (unsigned long long)server->fsid.major,
8192 (unsigned long long)server->fsid.minor,
8194 nfs_display_fhandle(NFS_FH(inode), __func__);
8197 status = ops->fsid_present(inode, cred);
8198 if (status != -NFS4ERR_DELAY)
8200 nfs4_handle_exception(server, status, &exception);
8201 } while (exception.retry);
8206 * If 'use_integrity' is true and the state managment nfs_client
8207 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8208 * and the machine credential as per RFC3530bis and RFC5661 Security
8209 * Considerations sections. Otherwise, just use the user cred with the
8210 * filesystem's rpc_client.
8212 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8215 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8216 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8217 struct nfs4_secinfo_arg args = {
8218 .dir_fh = NFS_FH(dir),
8221 struct nfs4_secinfo_res res = {
8224 struct rpc_message msg = {
8225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8229 struct nfs4_call_sync_data data = {
8230 .seq_server = NFS_SERVER(dir),
8231 .seq_args = &args.seq_args,
8232 .seq_res = &res.seq_res,
8234 struct rpc_task_setup task_setup = {
8236 .rpc_message = &msg,
8237 .callback_ops = clp->cl_mvops->call_sync_ops,
8238 .callback_data = &data,
8239 .flags = RPC_TASK_NO_ROUND_ROBIN,
8241 const struct cred *cred = NULL;
8243 if (use_integrity) {
8244 clnt = clp->cl_rpcclient;
8245 task_setup.rpc_client = clnt;
8247 cred = nfs4_get_clid_cred(clp);
8248 msg.rpc_cred = cred;
8251 dprintk("NFS call secinfo %s\n", name->name);
8253 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8254 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8255 status = nfs4_call_sync_custom(&task_setup);
8257 dprintk("NFS reply secinfo: %d\n", status);
8263 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8264 struct nfs4_secinfo_flavors *flavors)
8266 struct nfs4_exception exception = {
8267 .interruptible = true,
8271 err = -NFS4ERR_WRONGSEC;
8273 /* try to use integrity protection with machine cred */
8274 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8275 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8278 * if unable to use integrity protection, or SECINFO with
8279 * integrity protection returns NFS4ERR_WRONGSEC (which is
8280 * disallowed by spec, but exists in deployed servers) use
8281 * the current filesystem's rpc_client and the user cred.
8283 if (err == -NFS4ERR_WRONGSEC)
8284 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8286 trace_nfs4_secinfo(dir, name, err);
8287 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8289 } while (exception.retry);
8293 #ifdef CONFIG_NFS_V4_1
8295 * Check the exchange flags returned by the server for invalid flags, having
8296 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8299 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8301 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8303 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8305 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8306 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8308 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8312 return -NFS4ERR_INVAL;
8316 nfs41_same_server_scope(struct nfs41_server_scope *a,
8317 struct nfs41_server_scope *b)
8319 if (a->server_scope_sz != b->server_scope_sz)
8321 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8325 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8327 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8328 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8329 struct nfs_client *clp = args->client;
8331 switch (task->tk_status) {
8332 case -NFS4ERR_BADSESSION:
8333 case -NFS4ERR_DEADSESSION:
8334 nfs4_schedule_session_recovery(clp->cl_session,
8337 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8338 res->dir != NFS4_CDFS4_BOTH) {
8339 rpc_task_close_connection(task);
8340 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8341 rpc_restart_call(task);
8345 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8346 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8350 * nfs4_proc_bind_one_conn_to_session()
8352 * The 4.1 client currently uses the same TCP connection for the
8353 * fore and backchannel.
8356 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8357 struct rpc_xprt *xprt,
8358 struct nfs_client *clp,
8359 const struct cred *cred)
8362 struct nfs41_bind_conn_to_session_args args = {
8364 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8367 struct nfs41_bind_conn_to_session_res res;
8368 struct rpc_message msg = {
8370 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8375 struct rpc_task_setup task_setup_data = {
8378 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8379 .rpc_message = &msg,
8380 .flags = RPC_TASK_TIMEOUT,
8382 struct rpc_task *task;
8384 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8385 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8386 args.dir = NFS4_CDFC4_FORE;
8388 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8389 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8390 args.dir = NFS4_CDFC4_FORE;
8392 task = rpc_run_task(&task_setup_data);
8393 if (!IS_ERR(task)) {
8394 status = task->tk_status;
8397 status = PTR_ERR(task);
8398 trace_nfs4_bind_conn_to_session(clp, status);
8400 if (memcmp(res.sessionid.data,
8401 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8402 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8405 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8406 dprintk("NFS: %s: Unexpected direction from server\n",
8410 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8411 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8420 struct rpc_bind_conn_calldata {
8421 struct nfs_client *clp;
8422 const struct cred *cred;
8426 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8427 struct rpc_xprt *xprt,
8430 struct rpc_bind_conn_calldata *p = calldata;
8432 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8435 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8437 struct rpc_bind_conn_calldata data = {
8441 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8442 nfs4_proc_bind_conn_to_session_callback, &data);
8446 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8447 * and operations we'd like to see to enable certain features in the allow map
8449 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8450 .how = SP4_MACH_CRED,
8451 .enforce.u.words = {
8452 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8453 1 << (OP_EXCHANGE_ID - 32) |
8454 1 << (OP_CREATE_SESSION - 32) |
8455 1 << (OP_DESTROY_SESSION - 32) |
8456 1 << (OP_DESTROY_CLIENTID - 32)
8459 [0] = 1 << (OP_CLOSE) |
8460 1 << (OP_OPEN_DOWNGRADE) |
8462 1 << (OP_DELEGRETURN) |
8464 [1] = 1 << (OP_SECINFO - 32) |
8465 1 << (OP_SECINFO_NO_NAME - 32) |
8466 1 << (OP_LAYOUTRETURN - 32) |
8467 1 << (OP_TEST_STATEID - 32) |
8468 1 << (OP_FREE_STATEID - 32) |
8469 1 << (OP_WRITE - 32)
8474 * Select the state protection mode for client `clp' given the server results
8475 * from exchange_id in `sp'.
8477 * Returns 0 on success, negative errno otherwise.
8479 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8480 struct nfs41_state_protection *sp)
8482 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8483 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8484 1 << (OP_EXCHANGE_ID - 32) |
8485 1 << (OP_CREATE_SESSION - 32) |
8486 1 << (OP_DESTROY_SESSION - 32) |
8487 1 << (OP_DESTROY_CLIENTID - 32)
8489 unsigned long flags = 0;
8493 if (sp->how == SP4_MACH_CRED) {
8494 /* Print state protect result */
8495 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8496 for (i = 0; i <= LAST_NFS4_OP; i++) {
8497 if (test_bit(i, sp->enforce.u.longs))
8498 dfprintk(MOUNT, " enforce op %d\n", i);
8499 if (test_bit(i, sp->allow.u.longs))
8500 dfprintk(MOUNT, " allow op %d\n", i);
8503 /* make sure nothing is on enforce list that isn't supported */
8504 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8505 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8506 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8513 * Minimal mode - state operations are allowed to use machine
8514 * credential. Note this already happens by default, so the
8515 * client doesn't have to do anything more than the negotiation.
8517 * NOTE: we don't care if EXCHANGE_ID is in the list -
8518 * we're already using the machine cred for exchange_id
8519 * and will never use a different cred.
8521 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8522 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8523 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8524 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8525 dfprintk(MOUNT, "sp4_mach_cred:\n");
8526 dfprintk(MOUNT, " minimal mode enabled\n");
8527 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8529 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8534 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8535 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8536 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8537 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8538 dfprintk(MOUNT, " cleanup mode enabled\n");
8539 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8542 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8543 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8544 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8547 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8548 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8549 dfprintk(MOUNT, " secinfo mode enabled\n");
8550 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8553 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8554 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8555 dfprintk(MOUNT, " stateid mode enabled\n");
8556 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8559 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8560 dfprintk(MOUNT, " write mode enabled\n");
8561 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8564 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8565 dfprintk(MOUNT, " commit mode enabled\n");
8566 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8570 clp->cl_sp4_flags = flags;
8574 struct nfs41_exchange_id_data {
8575 struct nfs41_exchange_id_res res;
8576 struct nfs41_exchange_id_args args;
8579 static void nfs4_exchange_id_release(void *data)
8581 struct nfs41_exchange_id_data *cdata =
8582 (struct nfs41_exchange_id_data *)data;
8584 nfs_put_client(cdata->args.client);
8585 kfree(cdata->res.impl_id);
8586 kfree(cdata->res.server_scope);
8587 kfree(cdata->res.server_owner);
8591 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8592 .rpc_release = nfs4_exchange_id_release,
8596 * _nfs4_proc_exchange_id()
8598 * Wrapper for EXCHANGE_ID operation.
8600 static struct rpc_task *
8601 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8602 u32 sp4_how, struct rpc_xprt *xprt)
8604 struct rpc_message msg = {
8605 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8608 struct rpc_task_setup task_setup_data = {
8609 .rpc_client = clp->cl_rpcclient,
8610 .callback_ops = &nfs4_exchange_id_call_ops,
8611 .rpc_message = &msg,
8612 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8614 struct nfs41_exchange_id_data *calldata;
8617 if (!refcount_inc_not_zero(&clp->cl_count))
8618 return ERR_PTR(-EIO);
8621 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8625 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8627 status = nfs4_init_uniform_client_string(clp);
8631 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8634 if (unlikely(calldata->res.server_owner == NULL))
8637 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8639 if (unlikely(calldata->res.server_scope == NULL))
8640 goto out_server_owner;
8642 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8643 if (unlikely(calldata->res.impl_id == NULL))
8644 goto out_server_scope;
8648 calldata->args.state_protect.how = SP4_NONE;
8652 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8662 task_setup_data.rpc_xprt = xprt;
8663 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8664 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8665 sizeof(calldata->args.verifier.data));
8667 calldata->args.client = clp;
8668 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8669 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8670 #ifdef CONFIG_NFS_V4_1_MIGRATION
8671 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8673 msg.rpc_argp = &calldata->args;
8674 msg.rpc_resp = &calldata->res;
8675 task_setup_data.callback_data = calldata;
8677 return rpc_run_task(&task_setup_data);
8680 kfree(calldata->res.impl_id);
8682 kfree(calldata->res.server_scope);
8684 kfree(calldata->res.server_owner);
8688 nfs_put_client(clp);
8689 return ERR_PTR(status);
8693 * _nfs4_proc_exchange_id()
8695 * Wrapper for EXCHANGE_ID operation.
8697 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8700 struct rpc_task *task;
8701 struct nfs41_exchange_id_args *argp;
8702 struct nfs41_exchange_id_res *resp;
8703 unsigned long now = jiffies;
8706 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8708 return PTR_ERR(task);
8710 argp = task->tk_msg.rpc_argp;
8711 resp = task->tk_msg.rpc_resp;
8712 status = task->tk_status;
8716 status = nfs4_check_cl_exchange_flags(resp->flags,
8717 clp->cl_mvops->minor_version);
8721 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8725 do_renew_lease(clp, now);
8727 clp->cl_clientid = resp->clientid;
8728 clp->cl_exchange_flags = resp->flags;
8729 clp->cl_seqid = resp->seqid;
8730 /* Client ID is not confirmed */
8731 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8732 clear_bit(NFS4_SESSION_ESTABLISHED,
8733 &clp->cl_session->session_state);
8735 if (clp->cl_serverscope != NULL &&
8736 !nfs41_same_server_scope(clp->cl_serverscope,
8737 resp->server_scope)) {
8738 dprintk("%s: server_scope mismatch detected\n",
8740 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8743 swap(clp->cl_serverowner, resp->server_owner);
8744 swap(clp->cl_serverscope, resp->server_scope);
8745 swap(clp->cl_implid, resp->impl_id);
8747 /* Save the EXCHANGE_ID verifier session trunk tests */
8748 memcpy(clp->cl_confirm.data, argp->verifier.data,
8749 sizeof(clp->cl_confirm.data));
8751 trace_nfs4_exchange_id(clp, status);
8757 * nfs4_proc_exchange_id()
8759 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8761 * Since the clientid has expired, all compounds using sessions
8762 * associated with the stale clientid will be returning
8763 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8764 * be in some phase of session reset.
8766 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8768 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8770 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8773 /* try SP4_MACH_CRED if krb5i/p */
8774 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8775 authflavor == RPC_AUTH_GSS_KRB5P) {
8776 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8782 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8786 * nfs4_test_session_trunk
8788 * This is an add_xprt_test() test function called from
8789 * rpc_clnt_setup_test_and_add_xprt.
8791 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8792 * and is dereferrenced in nfs4_exchange_id_release
8794 * Upon success, add the new transport to the rpc_clnt
8796 * @clnt: struct rpc_clnt to get new transport
8797 * @xprt: the rpc_xprt to test
8798 * @data: call data for _nfs4_proc_exchange_id.
8800 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8803 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8804 struct rpc_task *task;
8809 dprintk("--> %s try %s\n", __func__,
8810 xprt->address_strings[RPC_DISPLAY_ADDR]);
8812 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8814 /* Test connection for session trunking. Async exchange_id call */
8815 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8819 status = task->tk_status;
8821 status = nfs4_detect_session_trunking(adata->clp,
8822 task->tk_msg.rpc_resp, xprt);
8825 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8829 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8831 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8832 const struct cred *cred)
8834 struct rpc_message msg = {
8835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8841 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8842 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8843 trace_nfs4_destroy_clientid(clp, status);
8845 dprintk("NFS: Got error %d from the server %s on "
8846 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8850 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8851 const struct cred *cred)
8856 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8857 ret = _nfs4_proc_destroy_clientid(clp, cred);
8859 case -NFS4ERR_DELAY:
8860 case -NFS4ERR_CLIENTID_BUSY:
8870 int nfs4_destroy_clientid(struct nfs_client *clp)
8872 const struct cred *cred;
8875 if (clp->cl_mvops->minor_version < 1)
8877 if (clp->cl_exchange_flags == 0)
8879 if (clp->cl_preserve_clid)
8881 cred = nfs4_get_clid_cred(clp);
8882 ret = nfs4_proc_destroy_clientid(clp, cred);
8886 case -NFS4ERR_STALE_CLIENTID:
8887 clp->cl_exchange_flags = 0;
8893 #endif /* CONFIG_NFS_V4_1 */
8895 struct nfs4_get_lease_time_data {
8896 struct nfs4_get_lease_time_args *args;
8897 struct nfs4_get_lease_time_res *res;
8898 struct nfs_client *clp;
8901 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8904 struct nfs4_get_lease_time_data *data =
8905 (struct nfs4_get_lease_time_data *)calldata;
8907 /* just setup sequence, do not trigger session recovery
8908 since we're invoked within one */
8909 nfs4_setup_sequence(data->clp,
8910 &data->args->la_seq_args,
8911 &data->res->lr_seq_res,
8916 * Called from nfs4_state_manager thread for session setup, so don't recover
8917 * from sequence operation or clientid errors.
8919 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8921 struct nfs4_get_lease_time_data *data =
8922 (struct nfs4_get_lease_time_data *)calldata;
8924 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8926 switch (task->tk_status) {
8927 case -NFS4ERR_DELAY:
8928 case -NFS4ERR_GRACE:
8929 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8930 task->tk_status = 0;
8932 case -NFS4ERR_RETRY_UNCACHED_REP:
8933 rpc_restart_call_prepare(task);
8938 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8939 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8940 .rpc_call_done = nfs4_get_lease_time_done,
8943 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8945 struct nfs4_get_lease_time_args args;
8946 struct nfs4_get_lease_time_res res = {
8947 .lr_fsinfo = fsinfo,
8949 struct nfs4_get_lease_time_data data = {
8954 struct rpc_message msg = {
8955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8959 struct rpc_task_setup task_setup = {
8960 .rpc_client = clp->cl_rpcclient,
8961 .rpc_message = &msg,
8962 .callback_ops = &nfs4_get_lease_time_ops,
8963 .callback_data = &data,
8964 .flags = RPC_TASK_TIMEOUT,
8967 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8968 return nfs4_call_sync_custom(&task_setup);
8971 #ifdef CONFIG_NFS_V4_1
8974 * Initialize the values to be used by the client in CREATE_SESSION
8975 * If nfs4_init_session set the fore channel request and response sizes,
8978 * Set the back channel max_resp_sz_cached to zero to force the client to
8979 * always set csa_cachethis to FALSE because the current implementation
8980 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8982 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8983 struct rpc_clnt *clnt)
8985 unsigned int max_rqst_sz, max_resp_sz;
8986 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8987 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8989 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8990 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8992 /* Fore channel attributes */
8993 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8994 args->fc_attrs.max_resp_sz = max_resp_sz;
8995 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8996 args->fc_attrs.max_reqs = max_session_slots;
8998 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8999 "max_ops=%u max_reqs=%u\n",
9001 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
9002 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
9004 /* Back channel attributes */
9005 args->bc_attrs.max_rqst_sz = max_bc_payload;
9006 args->bc_attrs.max_resp_sz = max_bc_payload;
9007 args->bc_attrs.max_resp_sz_cached = 0;
9008 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
9009 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
9010 if (args->bc_attrs.max_reqs > max_bc_slots)
9011 args->bc_attrs.max_reqs = max_bc_slots;
9013 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
9014 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
9016 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
9017 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
9018 args->bc_attrs.max_reqs);
9021 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
9022 struct nfs41_create_session_res *res)
9024 struct nfs4_channel_attrs *sent = &args->fc_attrs;
9025 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
9027 if (rcvd->max_resp_sz > sent->max_resp_sz)
9030 * Our requested max_ops is the minimum we need; we're not
9031 * prepared to break up compounds into smaller pieces than that.
9032 * So, no point even trying to continue if the server won't
9035 if (rcvd->max_ops < sent->max_ops)
9037 if (rcvd->max_reqs == 0)
9039 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
9040 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
9044 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
9045 struct nfs41_create_session_res *res)
9047 struct nfs4_channel_attrs *sent = &args->bc_attrs;
9048 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
9050 if (!(res->flags & SESSION4_BACK_CHAN))
9052 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
9054 if (rcvd->max_resp_sz < sent->max_resp_sz)
9056 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
9058 if (rcvd->max_ops > sent->max_ops)
9060 if (rcvd->max_reqs > sent->max_reqs)
9066 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
9067 struct nfs41_create_session_res *res)
9071 ret = nfs4_verify_fore_channel_attrs(args, res);
9074 return nfs4_verify_back_channel_attrs(args, res);
9077 static void nfs4_update_session(struct nfs4_session *session,
9078 struct nfs41_create_session_res *res)
9080 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
9081 /* Mark client id and session as being confirmed */
9082 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
9083 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
9084 session->flags = res->flags;
9085 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
9086 if (res->flags & SESSION4_BACK_CHAN)
9087 memcpy(&session->bc_attrs, &res->bc_attrs,
9088 sizeof(session->bc_attrs));
9091 static int _nfs4_proc_create_session(struct nfs_client *clp,
9092 const struct cred *cred)
9094 struct nfs4_session *session = clp->cl_session;
9095 struct nfs41_create_session_args args = {
9097 .clientid = clp->cl_clientid,
9098 .seqid = clp->cl_seqid,
9099 .cb_program = NFS4_CALLBACK,
9101 struct nfs41_create_session_res res;
9103 struct rpc_message msg = {
9104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9111 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
9112 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
9114 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9115 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9116 trace_nfs4_create_session(clp, status);
9119 case -NFS4ERR_STALE_CLIENTID:
9120 case -NFS4ERR_DELAY:
9129 /* Verify the session's negotiated channel_attrs values */
9130 status = nfs4_verify_channel_attrs(&args, &res);
9131 /* Increment the clientid slot sequence id */
9134 nfs4_update_session(session, &res);
9141 * Issues a CREATE_SESSION operation to the server.
9142 * It is the responsibility of the caller to verify the session is
9143 * expired before calling this routine.
9145 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9149 struct nfs4_session *session = clp->cl_session;
9151 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9153 status = _nfs4_proc_create_session(clp, cred);
9157 /* Init or reset the session slot tables */
9158 status = nfs4_setup_session_slot_tables(session);
9159 dprintk("slot table setup returned %d\n", status);
9163 ptr = (unsigned *)&session->sess_id.data[0];
9164 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9165 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9171 * Issue the over-the-wire RPC DESTROY_SESSION.
9172 * The caller must serialize access to this routine.
9174 int nfs4_proc_destroy_session(struct nfs4_session *session,
9175 const struct cred *cred)
9177 struct rpc_message msg = {
9178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9179 .rpc_argp = session,
9184 /* session is still being setup */
9185 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9188 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9189 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9190 trace_nfs4_destroy_session(session->clp, status);
9193 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9194 "Session has been destroyed regardless...\n", status);
9199 * Renew the cl_session lease.
9201 struct nfs4_sequence_data {
9202 struct nfs_client *clp;
9203 struct nfs4_sequence_args args;
9204 struct nfs4_sequence_res res;
9207 static void nfs41_sequence_release(void *data)
9209 struct nfs4_sequence_data *calldata = data;
9210 struct nfs_client *clp = calldata->clp;
9212 if (refcount_read(&clp->cl_count) > 1)
9213 nfs4_schedule_state_renewal(clp);
9214 nfs_put_client(clp);
9218 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9220 switch(task->tk_status) {
9221 case -NFS4ERR_DELAY:
9222 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9225 nfs4_schedule_lease_recovery(clp);
9230 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9232 struct nfs4_sequence_data *calldata = data;
9233 struct nfs_client *clp = calldata->clp;
9235 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9238 trace_nfs4_sequence(clp, task->tk_status);
9239 if (task->tk_status < 0) {
9240 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9241 if (refcount_read(&clp->cl_count) == 1)
9244 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9245 rpc_restart_call_prepare(task);
9249 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9252 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9254 struct nfs4_sequence_data *calldata = data;
9255 struct nfs_client *clp = calldata->clp;
9256 struct nfs4_sequence_args *args;
9257 struct nfs4_sequence_res *res;
9259 args = task->tk_msg.rpc_argp;
9260 res = task->tk_msg.rpc_resp;
9262 nfs4_setup_sequence(clp, args, res, task);
9265 static const struct rpc_call_ops nfs41_sequence_ops = {
9266 .rpc_call_done = nfs41_sequence_call_done,
9267 .rpc_call_prepare = nfs41_sequence_prepare,
9268 .rpc_release = nfs41_sequence_release,
9271 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9272 const struct cred *cred,
9273 struct nfs4_slot *slot,
9276 struct nfs4_sequence_data *calldata;
9277 struct rpc_message msg = {
9278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9281 struct rpc_task_setup task_setup_data = {
9282 .rpc_client = clp->cl_rpcclient,
9283 .rpc_message = &msg,
9284 .callback_ops = &nfs41_sequence_ops,
9285 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
9287 struct rpc_task *ret;
9289 ret = ERR_PTR(-EIO);
9290 if (!refcount_inc_not_zero(&clp->cl_count))
9293 ret = ERR_PTR(-ENOMEM);
9294 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9295 if (calldata == NULL)
9297 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9298 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9299 msg.rpc_argp = &calldata->args;
9300 msg.rpc_resp = &calldata->res;
9301 calldata->clp = clp;
9302 task_setup_data.callback_data = calldata;
9304 ret = rpc_run_task(&task_setup_data);
9309 nfs_put_client(clp);
9311 nfs41_release_slot(slot);
9315 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9317 struct rpc_task *task;
9320 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9322 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9324 ret = PTR_ERR(task);
9326 rpc_put_task_async(task);
9327 dprintk("<-- %s status=%d\n", __func__, ret);
9331 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9333 struct rpc_task *task;
9336 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9338 ret = PTR_ERR(task);
9341 ret = rpc_wait_for_completion_task(task);
9343 ret = task->tk_status;
9346 dprintk("<-- %s status=%d\n", __func__, ret);
9350 struct nfs4_reclaim_complete_data {
9351 struct nfs_client *clp;
9352 struct nfs41_reclaim_complete_args arg;
9353 struct nfs41_reclaim_complete_res res;
9356 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9358 struct nfs4_reclaim_complete_data *calldata = data;
9360 nfs4_setup_sequence(calldata->clp,
9361 &calldata->arg.seq_args,
9362 &calldata->res.seq_res,
9366 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9368 switch(task->tk_status) {
9370 wake_up_all(&clp->cl_lock_waitq);
9372 case -NFS4ERR_COMPLETE_ALREADY:
9373 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9375 case -NFS4ERR_DELAY:
9376 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9378 case -NFS4ERR_RETRY_UNCACHED_REP:
9380 case -NFS4ERR_BADSESSION:
9381 case -NFS4ERR_DEADSESSION:
9382 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9385 nfs4_schedule_lease_recovery(clp);
9390 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9392 struct nfs4_reclaim_complete_data *calldata = data;
9393 struct nfs_client *clp = calldata->clp;
9394 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9396 if (!nfs41_sequence_done(task, res))
9399 trace_nfs4_reclaim_complete(clp, task->tk_status);
9400 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9401 rpc_restart_call_prepare(task);
9406 static void nfs4_free_reclaim_complete_data(void *data)
9408 struct nfs4_reclaim_complete_data *calldata = data;
9413 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9414 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9415 .rpc_call_done = nfs4_reclaim_complete_done,
9416 .rpc_release = nfs4_free_reclaim_complete_data,
9420 * Issue a global reclaim complete.
9422 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9423 const struct cred *cred)
9425 struct nfs4_reclaim_complete_data *calldata;
9426 struct rpc_message msg = {
9427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9430 struct rpc_task_setup task_setup_data = {
9431 .rpc_client = clp->cl_rpcclient,
9432 .rpc_message = &msg,
9433 .callback_ops = &nfs4_reclaim_complete_call_ops,
9434 .flags = RPC_TASK_NO_ROUND_ROBIN,
9436 int status = -ENOMEM;
9438 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9439 if (calldata == NULL)
9441 calldata->clp = clp;
9442 calldata->arg.one_fs = 0;
9444 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9445 msg.rpc_argp = &calldata->arg;
9446 msg.rpc_resp = &calldata->res;
9447 task_setup_data.callback_data = calldata;
9448 status = nfs4_call_sync_custom(&task_setup_data);
9450 dprintk("<-- %s status=%d\n", __func__, status);
9455 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9457 struct nfs4_layoutget *lgp = calldata;
9458 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9460 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9461 &lgp->res.seq_res, task);
9464 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9466 struct nfs4_layoutget *lgp = calldata;
9468 nfs41_sequence_process(task, &lgp->res.seq_res);
9472 nfs4_layoutget_handle_exception(struct rpc_task *task,
9473 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9475 struct inode *inode = lgp->args.inode;
9476 struct nfs_server *server = NFS_SERVER(inode);
9477 struct pnfs_layout_hdr *lo = lgp->lo;
9478 int nfs4err = task->tk_status;
9479 int err, status = 0;
9482 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9484 nfs4_sequence_free_slot(&lgp->res.seq_res);
9491 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9492 * on the file. set tk_status to -ENODATA to tell upper layer to
9495 case -NFS4ERR_LAYOUTUNAVAILABLE:
9499 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9500 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9502 case -NFS4ERR_BADLAYOUT:
9503 status = -EOVERFLOW;
9506 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9507 * (or clients) writing to the same RAID stripe except when
9508 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9510 * Treat it like we would RECALLCONFLICT -- we retry for a little
9511 * while, and then eventually give up.
9513 case -NFS4ERR_LAYOUTTRYLATER:
9514 if (lgp->args.minlength == 0) {
9515 status = -EOVERFLOW;
9520 case -NFS4ERR_RECALLCONFLICT:
9521 status = -ERECALLCONFLICT;
9523 case -NFS4ERR_DELEG_REVOKED:
9524 case -NFS4ERR_ADMIN_REVOKED:
9525 case -NFS4ERR_EXPIRED:
9526 case -NFS4ERR_BAD_STATEID:
9527 exception->timeout = 0;
9528 spin_lock(&inode->i_lock);
9529 /* If the open stateid was bad, then recover it. */
9530 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9531 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9532 spin_unlock(&inode->i_lock);
9533 exception->state = lgp->args.ctx->state;
9534 exception->stateid = &lgp->args.stateid;
9539 * Mark the bad layout state as invalid, then retry
9541 pnfs_mark_layout_stateid_invalid(lo, &head);
9542 spin_unlock(&inode->i_lock);
9543 nfs_commit_inode(inode, 0);
9544 pnfs_free_lseg_list(&head);
9549 err = nfs4_handle_exception(server, nfs4err, exception);
9551 if (exception->retry)
9560 size_t max_response_pages(struct nfs_server *server)
9562 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9563 return nfs_page_array_len(0, max_resp_sz);
9566 static void nfs4_layoutget_release(void *calldata)
9568 struct nfs4_layoutget *lgp = calldata;
9570 nfs4_sequence_free_slot(&lgp->res.seq_res);
9571 pnfs_layoutget_free(lgp);
9574 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9575 .rpc_call_prepare = nfs4_layoutget_prepare,
9576 .rpc_call_done = nfs4_layoutget_done,
9577 .rpc_release = nfs4_layoutget_release,
9580 struct pnfs_layout_segment *
9581 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9583 struct inode *inode = lgp->args.inode;
9584 struct nfs_server *server = NFS_SERVER(inode);
9585 struct rpc_task *task;
9586 struct rpc_message msg = {
9587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9588 .rpc_argp = &lgp->args,
9589 .rpc_resp = &lgp->res,
9590 .rpc_cred = lgp->cred,
9592 struct rpc_task_setup task_setup_data = {
9593 .rpc_client = server->client,
9594 .rpc_message = &msg,
9595 .callback_ops = &nfs4_layoutget_call_ops,
9596 .callback_data = lgp,
9597 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
9600 struct pnfs_layout_segment *lseg = NULL;
9601 struct nfs4_exception exception = {
9603 .timeout = *timeout,
9607 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9609 task = rpc_run_task(&task_setup_data);
9611 status = rpc_wait_for_completion_task(task);
9615 if (task->tk_status < 0) {
9616 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9617 *timeout = exception.timeout;
9618 } else if (lgp->res.layoutp->len == 0) {
9620 *timeout = nfs4_update_delay(&exception.timeout);
9622 lseg = pnfs_layout_process(lgp);
9624 trace_nfs4_layoutget(lgp->args.ctx,
9631 dprintk("<-- %s status=%d\n", __func__, status);
9633 return ERR_PTR(status);
9638 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9640 struct nfs4_layoutreturn *lrp = calldata;
9642 nfs4_setup_sequence(lrp->clp,
9643 &lrp->args.seq_args,
9646 if (!pnfs_layout_is_valid(lrp->args.layout))
9650 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9652 struct nfs4_layoutreturn *lrp = calldata;
9653 struct nfs_server *server;
9655 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9659 * Was there an RPC level error? Assume the call succeeded,
9660 * and that we need to release the layout
9662 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9663 lrp->res.lrs_present = 0;
9667 server = NFS_SERVER(lrp->args.inode);
9668 switch (task->tk_status) {
9669 case -NFS4ERR_OLD_STATEID:
9670 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9676 task->tk_status = 0;
9680 case -NFS4ERR_DELAY:
9681 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9687 task->tk_status = 0;
9688 nfs4_sequence_free_slot(&lrp->res.seq_res);
9689 rpc_restart_call_prepare(task);
9692 static void nfs4_layoutreturn_release(void *calldata)
9694 struct nfs4_layoutreturn *lrp = calldata;
9695 struct pnfs_layout_hdr *lo = lrp->args.layout;
9697 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9698 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9699 nfs4_sequence_free_slot(&lrp->res.seq_res);
9700 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9701 lrp->ld_private.ops->free(&lrp->ld_private);
9702 pnfs_put_layout_hdr(lrp->args.layout);
9703 nfs_iput_and_deactive(lrp->inode);
9704 put_cred(lrp->cred);
9708 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9709 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9710 .rpc_call_done = nfs4_layoutreturn_done,
9711 .rpc_release = nfs4_layoutreturn_release,
9714 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9716 struct rpc_task *task;
9717 struct rpc_message msg = {
9718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9719 .rpc_argp = &lrp->args,
9720 .rpc_resp = &lrp->res,
9721 .rpc_cred = lrp->cred,
9723 struct rpc_task_setup task_setup_data = {
9724 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9725 .rpc_message = &msg,
9726 .callback_ops = &nfs4_layoutreturn_call_ops,
9727 .callback_data = lrp,
9728 .flags = RPC_TASK_MOVEABLE,
9732 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9733 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9734 &task_setup_data.rpc_client, &msg);
9736 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9739 nfs4_layoutreturn_release(lrp);
9742 task_setup_data.flags |= RPC_TASK_ASYNC;
9745 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9748 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9750 task = rpc_run_task(&task_setup_data);
9752 return PTR_ERR(task);
9754 status = task->tk_status;
9755 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9756 dprintk("<-- %s status=%d\n", __func__, status);
9762 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9763 struct pnfs_device *pdev,
9764 const struct cred *cred)
9766 struct nfs4_getdeviceinfo_args args = {
9768 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9769 NOTIFY_DEVICEID4_DELETE,
9771 struct nfs4_getdeviceinfo_res res = {
9774 struct rpc_message msg = {
9775 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9782 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9783 if (res.notification & ~args.notify_types)
9784 dprintk("%s: unsupported notification\n", __func__);
9785 if (res.notification != args.notify_types)
9788 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9790 dprintk("<-- %s status=%d\n", __func__, status);
9795 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9796 struct pnfs_device *pdev,
9797 const struct cred *cred)
9799 struct nfs4_exception exception = { };
9803 err = nfs4_handle_exception(server,
9804 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9806 } while (exception.retry);
9809 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9811 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9813 struct nfs4_layoutcommit_data *data = calldata;
9814 struct nfs_server *server = NFS_SERVER(data->args.inode);
9816 nfs4_setup_sequence(server->nfs_client,
9817 &data->args.seq_args,
9823 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9825 struct nfs4_layoutcommit_data *data = calldata;
9826 struct nfs_server *server = NFS_SERVER(data->args.inode);
9828 if (!nfs41_sequence_done(task, &data->res.seq_res))
9831 switch (task->tk_status) { /* Just ignore these failures */
9832 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9833 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9834 case -NFS4ERR_BADLAYOUT: /* no layout */
9835 case -NFS4ERR_GRACE: /* loca_recalim always false */
9836 task->tk_status = 0;
9841 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9842 rpc_restart_call_prepare(task);
9848 static void nfs4_layoutcommit_release(void *calldata)
9850 struct nfs4_layoutcommit_data *data = calldata;
9852 pnfs_cleanup_layoutcommit(data);
9853 nfs_post_op_update_inode_force_wcc(data->args.inode,
9855 put_cred(data->cred);
9856 nfs_iput_and_deactive(data->inode);
9860 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9861 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9862 .rpc_call_done = nfs4_layoutcommit_done,
9863 .rpc_release = nfs4_layoutcommit_release,
9867 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9869 struct rpc_message msg = {
9870 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9871 .rpc_argp = &data->args,
9872 .rpc_resp = &data->res,
9873 .rpc_cred = data->cred,
9875 struct rpc_task_setup task_setup_data = {
9876 .task = &data->task,
9877 .rpc_client = NFS_CLIENT(data->args.inode),
9878 .rpc_message = &msg,
9879 .callback_ops = &nfs4_layoutcommit_ops,
9880 .callback_data = data,
9881 .flags = RPC_TASK_MOVEABLE,
9883 struct rpc_task *task;
9886 dprintk("NFS: initiating layoutcommit call. sync %d "
9887 "lbw: %llu inode %lu\n", sync,
9888 data->args.lastbytewritten,
9889 data->args.inode->i_ino);
9892 data->inode = nfs_igrab_and_active(data->args.inode);
9893 if (data->inode == NULL) {
9894 nfs4_layoutcommit_release(data);
9897 task_setup_data.flags = RPC_TASK_ASYNC;
9899 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9900 task = rpc_run_task(&task_setup_data);
9902 return PTR_ERR(task);
9904 status = task->tk_status;
9905 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9906 dprintk("%s: status %d\n", __func__, status);
9912 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9913 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9916 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9917 struct nfs_fsinfo *info,
9918 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9920 struct nfs41_secinfo_no_name_args args = {
9921 .style = SECINFO_STYLE_CURRENT_FH,
9923 struct nfs4_secinfo_res res = {
9926 struct rpc_message msg = {
9927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9931 struct nfs4_call_sync_data data = {
9932 .seq_server = server,
9933 .seq_args = &args.seq_args,
9934 .seq_res = &res.seq_res,
9936 struct rpc_task_setup task_setup = {
9937 .rpc_client = server->client,
9938 .rpc_message = &msg,
9939 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9940 .callback_data = &data,
9941 .flags = RPC_TASK_NO_ROUND_ROBIN,
9943 const struct cred *cred = NULL;
9946 if (use_integrity) {
9947 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
9949 cred = nfs4_get_clid_cred(server->nfs_client);
9950 msg.rpc_cred = cred;
9953 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9954 status = nfs4_call_sync_custom(&task_setup);
9955 dprintk("<-- %s status=%d\n", __func__, status);
9963 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9964 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9966 struct nfs4_exception exception = {
9967 .interruptible = true,
9971 /* first try using integrity protection */
9972 err = -NFS4ERR_WRONGSEC;
9974 /* try to use integrity protection with machine cred */
9975 if (_nfs4_is_integrity_protected(server->nfs_client))
9976 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9980 * if unable to use integrity protection, or SECINFO with
9981 * integrity protection returns NFS4ERR_WRONGSEC (which is
9982 * disallowed by spec, but exists in deployed servers) use
9983 * the current filesystem's rpc_client and the user cred.
9985 if (err == -NFS4ERR_WRONGSEC)
9986 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9991 case -NFS4ERR_WRONGSEC:
9995 err = nfs4_handle_exception(server, err, &exception);
9997 } while (exception.retry);
10003 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
10004 struct nfs_fsinfo *info)
10008 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
10009 struct nfs4_secinfo_flavors *flavors;
10010 struct nfs4_secinfo4 *secinfo;
10013 page = alloc_page(GFP_KERNEL);
10019 flavors = page_address(page);
10020 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
10023 * Fall back on "guess and check" method if
10024 * the server doesn't support SECINFO_NO_NAME
10026 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
10027 err = nfs4_find_root_sec(server, fhandle, info);
10033 for (i = 0; i < flavors->num_flavors; i++) {
10034 secinfo = &flavors->flavors[i];
10036 switch (secinfo->flavor) {
10037 case RPC_AUTH_NULL:
10038 case RPC_AUTH_UNIX:
10040 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
10041 &secinfo->flavor_info);
10044 flavor = RPC_AUTH_MAXFLAVOR;
10048 if (!nfs_auth_info_match(&server->auth_info, flavor))
10049 flavor = RPC_AUTH_MAXFLAVOR;
10051 if (flavor != RPC_AUTH_MAXFLAVOR) {
10052 err = nfs4_lookup_root_sec(server, fhandle,
10059 if (flavor == RPC_AUTH_MAXFLAVOR)
10064 if (err == -EACCES)
10070 static int _nfs41_test_stateid(struct nfs_server *server,
10071 nfs4_stateid *stateid,
10072 const struct cred *cred)
10075 struct nfs41_test_stateid_args args = {
10076 .stateid = stateid,
10078 struct nfs41_test_stateid_res res;
10079 struct rpc_message msg = {
10080 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10085 struct rpc_clnt *rpc_client = server->client;
10087 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10088 &rpc_client, &msg);
10090 dprintk("NFS call test_stateid %p\n", stateid);
10091 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
10092 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
10093 &args.seq_args, &res.seq_res);
10094 if (status != NFS_OK) {
10095 dprintk("NFS reply test_stateid: failed, %d\n", status);
10098 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10099 return -res.status;
10102 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10103 int err, struct nfs4_exception *exception)
10105 exception->retry = 0;
10107 case -NFS4ERR_DELAY:
10108 case -NFS4ERR_RETRY_UNCACHED_REP:
10109 nfs4_handle_exception(server, err, exception);
10111 case -NFS4ERR_BADSESSION:
10112 case -NFS4ERR_BADSLOT:
10113 case -NFS4ERR_BAD_HIGH_SLOT:
10114 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10115 case -NFS4ERR_DEADSESSION:
10116 nfs4_do_handle_exception(server, err, exception);
10121 * nfs41_test_stateid - perform a TEST_STATEID operation
10123 * @server: server / transport on which to perform the operation
10124 * @stateid: state ID to test
10125 * @cred: credential
10127 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10128 * Otherwise a negative NFS4ERR value is returned if the operation
10129 * failed or the state ID is not currently valid.
10131 static int nfs41_test_stateid(struct nfs_server *server,
10132 nfs4_stateid *stateid,
10133 const struct cred *cred)
10135 struct nfs4_exception exception = {
10136 .interruptible = true,
10140 err = _nfs41_test_stateid(server, stateid, cred);
10141 nfs4_handle_delay_or_session_error(server, err, &exception);
10142 } while (exception.retry);
10146 struct nfs_free_stateid_data {
10147 struct nfs_server *server;
10148 struct nfs41_free_stateid_args args;
10149 struct nfs41_free_stateid_res res;
10152 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10154 struct nfs_free_stateid_data *data = calldata;
10155 nfs4_setup_sequence(data->server->nfs_client,
10156 &data->args.seq_args,
10157 &data->res.seq_res,
10161 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10163 struct nfs_free_stateid_data *data = calldata;
10165 nfs41_sequence_done(task, &data->res.seq_res);
10167 switch (task->tk_status) {
10168 case -NFS4ERR_DELAY:
10169 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10170 rpc_restart_call_prepare(task);
10174 static void nfs41_free_stateid_release(void *calldata)
10176 struct nfs_free_stateid_data *data = calldata;
10177 struct nfs_client *clp = data->server->nfs_client;
10179 nfs_put_client(clp);
10183 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10184 .rpc_call_prepare = nfs41_free_stateid_prepare,
10185 .rpc_call_done = nfs41_free_stateid_done,
10186 .rpc_release = nfs41_free_stateid_release,
10190 * nfs41_free_stateid - perform a FREE_STATEID operation
10192 * @server: server / transport on which to perform the operation
10193 * @stateid: state ID to release
10194 * @cred: credential
10195 * @privileged: set to true if this call needs to be privileged
10197 * Note: this function is always asynchronous.
10199 static int nfs41_free_stateid(struct nfs_server *server,
10200 const nfs4_stateid *stateid,
10201 const struct cred *cred,
10204 struct rpc_message msg = {
10205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10208 struct rpc_task_setup task_setup = {
10209 .rpc_client = server->client,
10210 .rpc_message = &msg,
10211 .callback_ops = &nfs41_free_stateid_ops,
10212 .flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
10214 struct nfs_free_stateid_data *data;
10215 struct rpc_task *task;
10216 struct nfs_client *clp = server->nfs_client;
10218 if (!refcount_inc_not_zero(&clp->cl_count))
10221 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10222 &task_setup.rpc_client, &msg);
10224 dprintk("NFS call free_stateid %p\n", stateid);
10225 data = kmalloc(sizeof(*data), GFP_NOFS);
10228 data->server = server;
10229 nfs4_stateid_copy(&data->args.stateid, stateid);
10231 task_setup.callback_data = data;
10233 msg.rpc_argp = &data->args;
10234 msg.rpc_resp = &data->res;
10235 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10236 task = rpc_run_task(&task_setup);
10238 return PTR_ERR(task);
10239 rpc_put_task(task);
10244 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10246 const struct cred *cred = lsp->ls_state->owner->so_cred;
10248 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10249 nfs4_free_lock_state(server, lsp);
10252 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10253 const nfs4_stateid *s2)
10255 if (s1->type != s2->type)
10258 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10261 if (s1->seqid == s2->seqid)
10264 return s1->seqid == 0 || s2->seqid == 0;
10267 #endif /* CONFIG_NFS_V4_1 */
10269 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10270 const nfs4_stateid *s2)
10272 return nfs4_stateid_match(s1, s2);
10276 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10277 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10278 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10279 .recover_open = nfs4_open_reclaim,
10280 .recover_lock = nfs4_lock_reclaim,
10281 .establish_clid = nfs4_init_clientid,
10282 .detect_trunking = nfs40_discover_server_trunking,
10285 #if defined(CONFIG_NFS_V4_1)
10286 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10287 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10288 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10289 .recover_open = nfs4_open_reclaim,
10290 .recover_lock = nfs4_lock_reclaim,
10291 .establish_clid = nfs41_init_clientid,
10292 .reclaim_complete = nfs41_proc_reclaim_complete,
10293 .detect_trunking = nfs41_discover_server_trunking,
10295 #endif /* CONFIG_NFS_V4_1 */
10297 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10298 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10299 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10300 .recover_open = nfs40_open_expired,
10301 .recover_lock = nfs4_lock_expired,
10302 .establish_clid = nfs4_init_clientid,
10305 #if defined(CONFIG_NFS_V4_1)
10306 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10307 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10308 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10309 .recover_open = nfs41_open_expired,
10310 .recover_lock = nfs41_lock_expired,
10311 .establish_clid = nfs41_init_clientid,
10313 #endif /* CONFIG_NFS_V4_1 */
10315 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10316 .sched_state_renewal = nfs4_proc_async_renew,
10317 .get_state_renewal_cred = nfs4_get_renew_cred,
10318 .renew_lease = nfs4_proc_renew,
10321 #if defined(CONFIG_NFS_V4_1)
10322 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10323 .sched_state_renewal = nfs41_proc_async_sequence,
10324 .get_state_renewal_cred = nfs4_get_machine_cred,
10325 .renew_lease = nfs4_proc_sequence,
10329 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10330 .get_locations = _nfs40_proc_get_locations,
10331 .fsid_present = _nfs40_proc_fsid_present,
10334 #if defined(CONFIG_NFS_V4_1)
10335 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10336 .get_locations = _nfs41_proc_get_locations,
10337 .fsid_present = _nfs41_proc_fsid_present,
10339 #endif /* CONFIG_NFS_V4_1 */
10341 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10342 .minor_version = 0,
10343 .init_caps = NFS_CAP_READDIRPLUS
10344 | NFS_CAP_ATOMIC_OPEN
10345 | NFS_CAP_POSIX_LOCK,
10346 .init_client = nfs40_init_client,
10347 .shutdown_client = nfs40_shutdown_client,
10348 .match_stateid = nfs4_match_stateid,
10349 .find_root_sec = nfs4_find_root_sec,
10350 .free_lock_state = nfs4_release_lockowner,
10351 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10352 .alloc_seqid = nfs_alloc_seqid,
10353 .call_sync_ops = &nfs40_call_sync_ops,
10354 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10355 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10356 .state_renewal_ops = &nfs40_state_renewal_ops,
10357 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10360 #if defined(CONFIG_NFS_V4_1)
10361 static struct nfs_seqid *
10362 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10367 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10368 .minor_version = 1,
10369 .init_caps = NFS_CAP_READDIRPLUS
10370 | NFS_CAP_ATOMIC_OPEN
10371 | NFS_CAP_POSIX_LOCK
10372 | NFS_CAP_STATEID_NFSV41
10373 | NFS_CAP_ATOMIC_OPEN_V1
10375 .init_client = nfs41_init_client,
10376 .shutdown_client = nfs41_shutdown_client,
10377 .match_stateid = nfs41_match_stateid,
10378 .find_root_sec = nfs41_find_root_sec,
10379 .free_lock_state = nfs41_free_lock_state,
10380 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10381 .alloc_seqid = nfs_alloc_no_seqid,
10382 .session_trunk = nfs4_test_session_trunk,
10383 .call_sync_ops = &nfs41_call_sync_ops,
10384 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10385 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10386 .state_renewal_ops = &nfs41_state_renewal_ops,
10387 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10391 #if defined(CONFIG_NFS_V4_2)
10392 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10393 .minor_version = 2,
10394 .init_caps = NFS_CAP_READDIRPLUS
10395 | NFS_CAP_ATOMIC_OPEN
10396 | NFS_CAP_POSIX_LOCK
10397 | NFS_CAP_STATEID_NFSV41
10398 | NFS_CAP_ATOMIC_OPEN_V1
10402 | NFS_CAP_OFFLOAD_CANCEL
10403 | NFS_CAP_COPY_NOTIFY
10404 | NFS_CAP_DEALLOCATE
10406 | NFS_CAP_LAYOUTSTATS
10408 | NFS_CAP_LAYOUTERROR
10409 | NFS_CAP_READ_PLUS,
10410 .init_client = nfs41_init_client,
10411 .shutdown_client = nfs41_shutdown_client,
10412 .match_stateid = nfs41_match_stateid,
10413 .find_root_sec = nfs41_find_root_sec,
10414 .free_lock_state = nfs41_free_lock_state,
10415 .call_sync_ops = &nfs41_call_sync_ops,
10416 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10417 .alloc_seqid = nfs_alloc_no_seqid,
10418 .session_trunk = nfs4_test_session_trunk,
10419 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10420 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10421 .state_renewal_ops = &nfs41_state_renewal_ops,
10422 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10426 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10427 [0] = &nfs_v4_0_minor_ops,
10428 #if defined(CONFIG_NFS_V4_1)
10429 [1] = &nfs_v4_1_minor_ops,
10431 #if defined(CONFIG_NFS_V4_2)
10432 [2] = &nfs_v4_2_minor_ops,
10436 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10438 ssize_t error, error2, error3;
10440 error = generic_listxattr(dentry, list, size);
10448 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10457 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10461 return error + error2 + error3;
10464 static const struct inode_operations nfs4_dir_inode_operations = {
10465 .create = nfs_create,
10466 .lookup = nfs_lookup,
10467 .atomic_open = nfs_atomic_open,
10469 .unlink = nfs_unlink,
10470 .symlink = nfs_symlink,
10471 .mkdir = nfs_mkdir,
10472 .rmdir = nfs_rmdir,
10473 .mknod = nfs_mknod,
10474 .rename = nfs_rename,
10475 .permission = nfs_permission,
10476 .getattr = nfs_getattr,
10477 .setattr = nfs_setattr,
10478 .listxattr = nfs4_listxattr,
10481 static const struct inode_operations nfs4_file_inode_operations = {
10482 .permission = nfs_permission,
10483 .getattr = nfs_getattr,
10484 .setattr = nfs_setattr,
10485 .listxattr = nfs4_listxattr,
10488 const struct nfs_rpc_ops nfs_v4_clientops = {
10489 .version = 4, /* protocol version */
10490 .dentry_ops = &nfs4_dentry_operations,
10491 .dir_inode_ops = &nfs4_dir_inode_operations,
10492 .file_inode_ops = &nfs4_file_inode_operations,
10493 .file_ops = &nfs4_file_operations,
10494 .getroot = nfs4_proc_get_root,
10495 .submount = nfs4_submount,
10496 .try_get_tree = nfs4_try_get_tree,
10497 .getattr = nfs4_proc_getattr,
10498 .setattr = nfs4_proc_setattr,
10499 .lookup = nfs4_proc_lookup,
10500 .lookupp = nfs4_proc_lookupp,
10501 .access = nfs4_proc_access,
10502 .readlink = nfs4_proc_readlink,
10503 .create = nfs4_proc_create,
10504 .remove = nfs4_proc_remove,
10505 .unlink_setup = nfs4_proc_unlink_setup,
10506 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10507 .unlink_done = nfs4_proc_unlink_done,
10508 .rename_setup = nfs4_proc_rename_setup,
10509 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10510 .rename_done = nfs4_proc_rename_done,
10511 .link = nfs4_proc_link,
10512 .symlink = nfs4_proc_symlink,
10513 .mkdir = nfs4_proc_mkdir,
10514 .rmdir = nfs4_proc_rmdir,
10515 .readdir = nfs4_proc_readdir,
10516 .mknod = nfs4_proc_mknod,
10517 .statfs = nfs4_proc_statfs,
10518 .fsinfo = nfs4_proc_fsinfo,
10519 .pathconf = nfs4_proc_pathconf,
10520 .set_capabilities = nfs4_server_capabilities,
10521 .decode_dirent = nfs4_decode_dirent,
10522 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10523 .read_setup = nfs4_proc_read_setup,
10524 .read_done = nfs4_read_done,
10525 .write_setup = nfs4_proc_write_setup,
10526 .write_done = nfs4_write_done,
10527 .commit_setup = nfs4_proc_commit_setup,
10528 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10529 .commit_done = nfs4_commit_done,
10530 .lock = nfs4_proc_lock,
10531 .clear_acl_cache = nfs4_zap_acl_attr,
10532 .close_context = nfs4_close_context,
10533 .open_context = nfs4_atomic_open,
10534 .have_delegation = nfs4_have_delegation,
10535 .alloc_client = nfs4_alloc_client,
10536 .init_client = nfs4_init_client,
10537 .free_client = nfs4_free_client,
10538 .create_server = nfs4_create_server,
10539 .clone_server = nfs_clone_server,
10540 .discover_trunking = nfs4_discover_trunking,
10543 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10544 .name = XATTR_NAME_NFSV4_ACL,
10545 .list = nfs4_xattr_list_nfs4_acl,
10546 .get = nfs4_xattr_get_nfs4_acl,
10547 .set = nfs4_xattr_set_nfs4_acl,
10550 #ifdef CONFIG_NFS_V4_2
10551 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10552 .prefix = XATTR_USER_PREFIX,
10553 .get = nfs4_xattr_get_nfs4_user,
10554 .set = nfs4_xattr_set_nfs4_user,
10558 const struct xattr_handler *nfs4_xattr_handlers[] = {
10559 &nfs4_xattr_nfs4_acl_handler,
10560 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10561 &nfs4_xattr_nfs4_label_handler,
10563 #ifdef CONFIG_NFS_V4_2
10564 &nfs4_xattr_nfs4_user_handler,