4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
57 #include <linux/iversion.h>
60 #include "delegation.h"
67 #include "nfs4idmap.h"
68 #include "nfs4session.h"
72 #include "nfs4trace.h"
74 #define NFSDBG_FACILITY NFSDBG_PROC
76 #define NFS4_BITMASK_SZ 3
78 #define NFS4_POLL_RETRY_MIN (HZ/10)
79 #define NFS4_POLL_RETRY_MAX (15*HZ)
81 /* file attributes which can be mapped to nfs attributes */
82 #define NFS4_VALID_ATTRS (ATTR_MODE \
93 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
94 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
95 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
96 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
97 struct nfs_fattr *fattr, struct inode *inode);
98 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
99 struct nfs_fattr *fattr, struct iattr *sattr,
100 struct nfs_open_context *ctx, struct nfs4_label *ilabel);
101 #ifdef CONFIG_NFS_V4_1
102 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
103 const struct cred *cred,
104 struct nfs4_slot *slot,
106 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
107 const struct cred *);
108 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
109 const struct cred *, bool);
112 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
113 static inline struct nfs4_label *
114 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
115 struct iattr *sattr, struct nfs4_label *label)
122 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
130 err = security_dentry_init_security(dentry, sattr->ia_mode,
131 &dentry->d_name, NULL,
132 (void **)&label->label, &label->len);
139 nfs4_label_release_security(struct nfs4_label *label)
142 security_release_secctx(label->label, label->len);
144 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
147 return server->attr_bitmask;
149 return server->attr_bitmask_nl;
152 static inline struct nfs4_label *
153 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
154 struct iattr *sattr, struct nfs4_label *l)
157 nfs4_label_release_security(struct nfs4_label *label)
160 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
161 { return server->attr_bitmask; }
164 /* Prevent leaks of NFSv4 errors into userland */
165 static int nfs4_map_errors(int err)
170 case -NFS4ERR_RESOURCE:
171 case -NFS4ERR_LAYOUTTRYLATER:
172 case -NFS4ERR_RECALLCONFLICT:
174 case -NFS4ERR_WRONGSEC:
175 case -NFS4ERR_WRONG_CRED:
177 case -NFS4ERR_BADOWNER:
178 case -NFS4ERR_BADNAME:
180 case -NFS4ERR_SHARE_DENIED:
182 case -NFS4ERR_MINOR_VERS_MISMATCH:
183 return -EPROTONOSUPPORT;
184 case -NFS4ERR_FILE_OPEN:
186 case -NFS4ERR_NOT_SAME:
189 dprintk("%s could not handle NFSv4 error %d\n",
197 * This is our standard bitmap for GETATTR requests.
199 const u32 nfs4_fattr_bitmap[3] = {
201 | FATTR4_WORD0_CHANGE
204 | FATTR4_WORD0_FILEID,
206 | FATTR4_WORD1_NUMLINKS
208 | FATTR4_WORD1_OWNER_GROUP
209 | FATTR4_WORD1_RAWDEV
210 | FATTR4_WORD1_SPACE_USED
211 | FATTR4_WORD1_TIME_ACCESS
212 | FATTR4_WORD1_TIME_METADATA
213 | FATTR4_WORD1_TIME_MODIFY
214 | FATTR4_WORD1_MOUNTED_ON_FILEID,
215 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
216 FATTR4_WORD2_SECURITY_LABEL
220 static const u32 nfs4_pnfs_open_bitmap[3] = {
222 | FATTR4_WORD0_CHANGE
225 | FATTR4_WORD0_FILEID,
227 | FATTR4_WORD1_NUMLINKS
229 | FATTR4_WORD1_OWNER_GROUP
230 | FATTR4_WORD1_RAWDEV
231 | FATTR4_WORD1_SPACE_USED
232 | FATTR4_WORD1_TIME_ACCESS
233 | FATTR4_WORD1_TIME_METADATA
234 | FATTR4_WORD1_TIME_MODIFY,
235 FATTR4_WORD2_MDSTHRESHOLD
236 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
237 | FATTR4_WORD2_SECURITY_LABEL
241 static const u32 nfs4_open_noattr_bitmap[3] = {
243 | FATTR4_WORD0_FILEID,
246 const u32 nfs4_statfs_bitmap[3] = {
247 FATTR4_WORD0_FILES_AVAIL
248 | FATTR4_WORD0_FILES_FREE
249 | FATTR4_WORD0_FILES_TOTAL,
250 FATTR4_WORD1_SPACE_AVAIL
251 | FATTR4_WORD1_SPACE_FREE
252 | FATTR4_WORD1_SPACE_TOTAL
255 const u32 nfs4_pathconf_bitmap[3] = {
257 | FATTR4_WORD0_MAXNAME,
261 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
262 | FATTR4_WORD0_MAXREAD
263 | FATTR4_WORD0_MAXWRITE
264 | FATTR4_WORD0_LEASE_TIME,
265 FATTR4_WORD1_TIME_DELTA
266 | FATTR4_WORD1_FS_LAYOUT_TYPES,
267 FATTR4_WORD2_LAYOUT_BLKSIZE
268 | FATTR4_WORD2_CLONE_BLKSIZE
269 | FATTR4_WORD2_CHANGE_ATTR_TYPE
270 | FATTR4_WORD2_XATTR_SUPPORT
273 const u32 nfs4_fs_locations_bitmap[3] = {
277 | FATTR4_WORD0_FILEID
278 | FATTR4_WORD0_FS_LOCATIONS,
280 | FATTR4_WORD1_OWNER_GROUP
281 | FATTR4_WORD1_RAWDEV
282 | FATTR4_WORD1_SPACE_USED
283 | FATTR4_WORD1_TIME_ACCESS
284 | FATTR4_WORD1_TIME_METADATA
285 | FATTR4_WORD1_TIME_MODIFY
286 | FATTR4_WORD1_MOUNTED_ON_FILEID,
289 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
290 struct inode *inode, unsigned long flags)
292 unsigned long cache_validity;
294 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
295 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
298 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags;
300 /* Remove the attributes over which we have full control */
301 dst[1] &= ~FATTR4_WORD1_RAWDEV;
302 if (!(cache_validity & NFS_INO_INVALID_SIZE))
303 dst[0] &= ~FATTR4_WORD0_SIZE;
305 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
306 dst[0] &= ~FATTR4_WORD0_CHANGE;
308 if (!(cache_validity & NFS_INO_INVALID_MODE))
309 dst[1] &= ~FATTR4_WORD1_MODE;
310 if (!(cache_validity & NFS_INO_INVALID_OTHER))
311 dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP);
314 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
315 struct nfs4_readdir_arg *readdir)
317 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
321 readdir->cookie = cookie;
322 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
327 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
332 * NFSv4 servers do not return entries for '.' and '..'
333 * Therefore, we fake these entries here. We let '.'
334 * have cookie 0 and '..' have cookie 1. Note that
335 * when talking to the server, we always send cookie 0
338 start = p = kmap_atomic(*readdir->pages);
341 *p++ = xdr_one; /* next */
342 *p++ = xdr_zero; /* cookie, first word */
343 *p++ = xdr_one; /* cookie, second word */
344 *p++ = xdr_one; /* entry len */
345 memcpy(p, ".\0\0\0", 4); /* entry */
347 *p++ = xdr_one; /* bitmap length */
348 *p++ = htonl(attrs); /* bitmap */
349 *p++ = htonl(12); /* attribute buffer length */
350 *p++ = htonl(NF4DIR);
351 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
354 *p++ = xdr_one; /* next */
355 *p++ = xdr_zero; /* cookie, first word */
356 *p++ = xdr_two; /* cookie, second word */
357 *p++ = xdr_two; /* entry len */
358 memcpy(p, "..\0\0", 4); /* entry */
360 *p++ = xdr_one; /* bitmap length */
361 *p++ = htonl(attrs); /* bitmap */
362 *p++ = htonl(12); /* attribute buffer length */
363 *p++ = htonl(NF4DIR);
364 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
366 readdir->pgbase = (char *)p - (char *)start;
367 readdir->count -= readdir->pgbase;
368 kunmap_atomic(start);
371 static void nfs4_fattr_set_prechange(struct nfs_fattr *fattr, u64 version)
373 if (!(fattr->valid & NFS_ATTR_FATTR_PRECHANGE)) {
374 fattr->pre_change_attr = version;
375 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
379 static void nfs4_test_and_free_stateid(struct nfs_server *server,
380 nfs4_stateid *stateid,
381 const struct cred *cred)
383 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
385 ops->test_and_free_expired(server, stateid, cred);
388 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
389 nfs4_stateid *stateid,
390 const struct cred *cred)
392 stateid->type = NFS4_REVOKED_STATEID_TYPE;
393 nfs4_test_and_free_stateid(server, stateid, cred);
396 static void nfs4_free_revoked_stateid(struct nfs_server *server,
397 const nfs4_stateid *stateid,
398 const struct cred *cred)
402 nfs4_stateid_copy(&tmp, stateid);
403 __nfs4_free_revoked_stateid(server, &tmp, cred);
406 static long nfs4_update_delay(long *timeout)
410 return NFS4_POLL_RETRY_MAX;
412 *timeout = NFS4_POLL_RETRY_MIN;
413 if (*timeout > NFS4_POLL_RETRY_MAX)
414 *timeout = NFS4_POLL_RETRY_MAX;
420 static int nfs4_delay_killable(long *timeout)
424 __set_current_state(TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
425 schedule_timeout(nfs4_update_delay(timeout));
426 if (!__fatal_signal_pending(current))
431 static int nfs4_delay_interruptible(long *timeout)
435 __set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE_UNSAFE);
436 schedule_timeout(nfs4_update_delay(timeout));
437 if (!signal_pending(current))
439 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
442 static int nfs4_delay(long *timeout, bool interruptible)
445 return nfs4_delay_interruptible(timeout);
446 return nfs4_delay_killable(timeout);
449 static const nfs4_stateid *
450 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
454 switch (stateid->type) {
455 case NFS4_OPEN_STATEID_TYPE:
456 case NFS4_LOCK_STATEID_TYPE:
457 case NFS4_DELEGATION_STATEID_TYPE:
465 /* This is the error handling routine for processes that are allowed
468 static int nfs4_do_handle_exception(struct nfs_server *server,
469 int errorcode, struct nfs4_exception *exception)
471 struct nfs_client *clp = server->nfs_client;
472 struct nfs4_state *state = exception->state;
473 const nfs4_stateid *stateid;
474 struct inode *inode = exception->inode;
477 exception->delay = 0;
478 exception->recovering = 0;
479 exception->retry = 0;
481 stateid = nfs4_recoverable_stateid(exception->stateid);
482 if (stateid == NULL && state != NULL)
483 stateid = nfs4_recoverable_stateid(&state->stateid);
488 case -NFS4ERR_BADHANDLE:
490 if (inode != NULL && S_ISREG(inode->i_mode))
491 pnfs_destroy_layout(NFS_I(inode));
493 case -NFS4ERR_DELEG_REVOKED:
494 case -NFS4ERR_ADMIN_REVOKED:
495 case -NFS4ERR_EXPIRED:
496 case -NFS4ERR_BAD_STATEID:
497 case -NFS4ERR_PARTNER_NO_AUTH:
498 if (inode != NULL && stateid != NULL) {
499 nfs_inode_find_state_and_recover(inode,
501 goto wait_on_recovery;
504 case -NFS4ERR_OPENMODE:
508 err = nfs_async_inode_return_delegation(inode,
511 goto wait_on_recovery;
512 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
513 exception->retry = 1;
519 ret = nfs4_schedule_stateid_recovery(server, state);
522 goto wait_on_recovery;
523 case -NFS4ERR_STALE_STATEID:
524 case -NFS4ERR_STALE_CLIENTID:
525 nfs4_schedule_lease_recovery(clp);
526 goto wait_on_recovery;
528 ret = nfs4_schedule_migration_recovery(server);
531 goto wait_on_recovery;
532 case -NFS4ERR_LEASE_MOVED:
533 nfs4_schedule_lease_moved_recovery(clp);
534 goto wait_on_recovery;
535 #if defined(CONFIG_NFS_V4_1)
536 case -NFS4ERR_BADSESSION:
537 case -NFS4ERR_BADSLOT:
538 case -NFS4ERR_BAD_HIGH_SLOT:
539 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
540 case -NFS4ERR_DEADSESSION:
541 case -NFS4ERR_SEQ_FALSE_RETRY:
542 case -NFS4ERR_SEQ_MISORDERED:
543 /* Handled in nfs41_sequence_process() */
544 goto wait_on_recovery;
545 #endif /* defined(CONFIG_NFS_V4_1) */
546 case -NFS4ERR_FILE_OPEN:
547 if (exception->timeout > HZ) {
548 /* We have retried a decent amount, time to
556 nfs_inc_server_stats(server, NFSIOS_DELAY);
559 case -NFS4ERR_LAYOUTTRYLATER:
560 case -NFS4ERR_RECALLCONFLICT:
561 exception->delay = 1;
564 case -NFS4ERR_RETRY_UNCACHED_REP:
565 case -NFS4ERR_OLD_STATEID:
566 exception->retry = 1;
568 case -NFS4ERR_BADOWNER:
569 /* The following works around a Linux server bug! */
570 case -NFS4ERR_BADNAME:
571 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
572 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
573 exception->retry = 1;
574 printk(KERN_WARNING "NFS: v4 server %s "
575 "does not accept raw "
577 "Reenabling the idmapper.\n",
578 server->nfs_client->cl_hostname);
581 /* We failed to handle the error */
582 return nfs4_map_errors(ret);
584 exception->recovering = 1;
588 /* This is the error handling routine for processes that are allowed
591 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
593 struct nfs_client *clp = server->nfs_client;
596 ret = nfs4_do_handle_exception(server, errorcode, exception);
597 if (exception->delay) {
598 ret = nfs4_delay(&exception->timeout,
599 exception->interruptible);
602 if (exception->recovering) {
603 if (exception->task_is_privileged)
605 ret = nfs4_wait_clnt_recover(clp);
606 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
613 exception->retry = 1;
618 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
619 int errorcode, struct nfs4_exception *exception)
621 struct nfs_client *clp = server->nfs_client;
624 ret = nfs4_do_handle_exception(server, errorcode, exception);
625 if (exception->delay) {
626 rpc_delay(task, nfs4_update_delay(&exception->timeout));
629 if (exception->recovering) {
630 if (exception->task_is_privileged)
632 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
633 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
634 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
637 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
642 exception->retry = 1;
644 * For NFS4ERR_MOVED, the client transport will need to
645 * be recomputed after migration recovery has completed.
647 if (errorcode == -NFS4ERR_MOVED)
648 rpc_task_release_transport(task);
654 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
655 struct nfs4_state *state, long *timeout)
657 struct nfs4_exception exception = {
661 if (task->tk_status >= 0)
664 exception.timeout = *timeout;
665 task->tk_status = nfs4_async_handle_exception(task, server,
668 if (exception.delay && timeout)
669 *timeout = exception.timeout;
676 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
677 * or 'false' otherwise.
679 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
681 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
682 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
685 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
687 spin_lock(&clp->cl_lock);
688 if (time_before(clp->cl_last_renewal,timestamp))
689 clp->cl_last_renewal = timestamp;
690 spin_unlock(&clp->cl_lock);
693 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
695 struct nfs_client *clp = server->nfs_client;
697 if (!nfs4_has_session(clp))
698 do_renew_lease(clp, timestamp);
701 struct nfs4_call_sync_data {
702 const struct nfs_server *seq_server;
703 struct nfs4_sequence_args *seq_args;
704 struct nfs4_sequence_res *seq_res;
707 void nfs4_init_sequence(struct nfs4_sequence_args *args,
708 struct nfs4_sequence_res *res, int cache_reply,
711 args->sa_slot = NULL;
712 args->sa_cache_this = cache_reply;
713 args->sa_privileged = privileged;
718 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
720 struct nfs4_slot *slot = res->sr_slot;
721 struct nfs4_slot_table *tbl;
724 spin_lock(&tbl->slot_tbl_lock);
725 if (!nfs41_wake_and_assign_slot(tbl, slot))
726 nfs4_free_slot(tbl, slot);
727 spin_unlock(&tbl->slot_tbl_lock);
732 static int nfs40_sequence_done(struct rpc_task *task,
733 struct nfs4_sequence_res *res)
735 if (res->sr_slot != NULL)
736 nfs40_sequence_free_slot(res);
740 #if defined(CONFIG_NFS_V4_1)
742 static void nfs41_release_slot(struct nfs4_slot *slot)
744 struct nfs4_session *session;
745 struct nfs4_slot_table *tbl;
746 bool send_new_highest_used_slotid = false;
751 session = tbl->session;
753 /* Bump the slot sequence number */
758 spin_lock(&tbl->slot_tbl_lock);
759 /* Be nice to the server: try to ensure that the last transmitted
760 * value for highest_user_slotid <= target_highest_slotid
762 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
763 send_new_highest_used_slotid = true;
765 if (nfs41_wake_and_assign_slot(tbl, slot)) {
766 send_new_highest_used_slotid = false;
769 nfs4_free_slot(tbl, slot);
771 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
772 send_new_highest_used_slotid = false;
774 spin_unlock(&tbl->slot_tbl_lock);
775 if (send_new_highest_used_slotid)
776 nfs41_notify_server(session->clp);
777 if (waitqueue_active(&tbl->slot_waitq))
778 wake_up_all(&tbl->slot_waitq);
781 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
783 nfs41_release_slot(res->sr_slot);
787 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
790 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
791 slot->seq_nr_highest_sent = seqnr;
793 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot, u32 seqnr)
795 nfs4_slot_sequence_record_sent(slot, seqnr);
796 slot->seq_nr_last_acked = seqnr;
799 static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
800 struct nfs4_slot *slot)
802 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
804 rpc_put_task_async(task);
807 static int nfs41_sequence_process(struct rpc_task *task,
808 struct nfs4_sequence_res *res)
810 struct nfs4_session *session;
811 struct nfs4_slot *slot = res->sr_slot;
812 struct nfs_client *clp;
818 /* don't increment the sequence number if the task wasn't sent */
819 if (!RPC_WAS_SENT(task) || slot->seq_done)
822 session = slot->table->session;
825 trace_nfs4_sequence_done(session, res);
827 status = res->sr_status;
828 if (task->tk_status == -NFS4ERR_DEADSESSION)
829 status = -NFS4ERR_DEADSESSION;
831 /* Check the SEQUENCE operation status */
834 /* Mark this sequence number as having been acked */
835 nfs4_slot_sequence_acked(slot, slot->seq_nr);
836 /* Update the slot's sequence and clientid lease timer */
838 do_renew_lease(clp, res->sr_timestamp);
839 /* Check sequence flags */
840 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
842 nfs41_update_target_slotid(slot->table, slot, res);
846 * sr_status remains 1 if an RPC level error occurred.
847 * The server may or may not have processed the sequence
850 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
854 /* The server detected a resend of the RPC call and
855 * returned NFS4ERR_DELAY as per Section 2.10.6.2
858 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
863 case -NFS4ERR_RETRY_UNCACHED_REP:
864 case -NFS4ERR_SEQ_FALSE_RETRY:
866 * The server thinks we tried to replay a request.
867 * Retry the call after bumping the sequence ID.
869 nfs4_slot_sequence_acked(slot, slot->seq_nr);
871 case -NFS4ERR_BADSLOT:
873 * The slot id we used was probably retired. Try again
874 * using a different slot id.
876 if (slot->slot_nr < slot->table->target_highest_slotid)
877 goto session_recover;
879 case -NFS4ERR_SEQ_MISORDERED:
880 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
882 * Were one or more calls using this slot interrupted?
883 * If the server never received the request, then our
884 * transmitted slot sequence number may be too high. However,
885 * if the server did receive the request then it might
886 * accidentally give us a reply with a mismatched operation.
887 * We can sort this out by sending a lone sequence operation
888 * to the server on the same slot.
890 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
892 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
893 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
900 * A retry might be sent while the original request is
901 * still in progress on the replier. The replier SHOULD
902 * deal with the issue by returning NFS4ERR_DELAY as the
903 * reply to SEQUENCE or CB_SEQUENCE operation, but
904 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
906 * Restart the search after a delay.
908 slot->seq_nr = slot->seq_nr_highest_sent;
910 case -NFS4ERR_BADSESSION:
911 case -NFS4ERR_DEADSESSION:
912 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
913 goto session_recover;
915 /* Just update the slot sequence no. */
919 /* The session may be reset by one of the error handlers. */
920 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
924 nfs4_schedule_session_recovery(session, status);
925 dprintk("%s ERROR: %d Reset session\n", __func__, status);
926 nfs41_sequence_free_slot(res);
931 if (rpc_restart_call_prepare(task)) {
932 nfs41_sequence_free_slot(res);
938 if (!rpc_restart_call(task))
940 rpc_delay(task, NFS4_POLL_RETRY_MAX);
944 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
946 if (!nfs41_sequence_process(task, res))
948 if (res->sr_slot != NULL)
949 nfs41_sequence_free_slot(res);
953 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
955 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
957 if (res->sr_slot == NULL)
959 if (res->sr_slot->table->session != NULL)
960 return nfs41_sequence_process(task, res);
961 return nfs40_sequence_done(task, res);
964 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
966 if (res->sr_slot != NULL) {
967 if (res->sr_slot->table->session != NULL)
968 nfs41_sequence_free_slot(res);
970 nfs40_sequence_free_slot(res);
974 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
976 if (res->sr_slot == NULL)
978 if (!res->sr_slot->table->session)
979 return nfs40_sequence_done(task, res);
980 return nfs41_sequence_done(task, res);
982 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
984 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
986 struct nfs4_call_sync_data *data = calldata;
988 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
990 nfs4_setup_sequence(data->seq_server->nfs_client,
991 data->seq_args, data->seq_res, task);
994 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
996 struct nfs4_call_sync_data *data = calldata;
998 nfs41_sequence_done(task, data->seq_res);
1001 static const struct rpc_call_ops nfs41_call_sync_ops = {
1002 .rpc_call_prepare = nfs41_call_sync_prepare,
1003 .rpc_call_done = nfs41_call_sync_done,
1006 #else /* !CONFIG_NFS_V4_1 */
1008 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1010 return nfs40_sequence_done(task, res);
1013 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1015 if (res->sr_slot != NULL)
1016 nfs40_sequence_free_slot(res);
1019 int nfs4_sequence_done(struct rpc_task *task,
1020 struct nfs4_sequence_res *res)
1022 return nfs40_sequence_done(task, res);
1024 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1026 #endif /* !CONFIG_NFS_V4_1 */
1028 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1030 res->sr_timestamp = jiffies;
1031 res->sr_status_flags = 0;
1036 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1037 struct nfs4_sequence_res *res,
1038 struct nfs4_slot *slot)
1042 slot->privileged = args->sa_privileged ? 1 : 0;
1043 args->sa_slot = slot;
1045 res->sr_slot = slot;
1048 int nfs4_setup_sequence(struct nfs_client *client,
1049 struct nfs4_sequence_args *args,
1050 struct nfs4_sequence_res *res,
1051 struct rpc_task *task)
1053 struct nfs4_session *session = nfs4_get_session(client);
1054 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1055 struct nfs4_slot *slot;
1057 /* slot already allocated? */
1058 if (res->sr_slot != NULL)
1062 tbl = &session->fc_slot_table;
1064 spin_lock(&tbl->slot_tbl_lock);
1065 /* The state manager will wait until the slot table is empty */
1066 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1069 slot = nfs4_alloc_slot(tbl);
1071 if (slot == ERR_PTR(-ENOMEM))
1072 goto out_sleep_timeout;
1075 spin_unlock(&tbl->slot_tbl_lock);
1077 nfs4_sequence_attach_slot(args, res, slot);
1079 trace_nfs4_setup_sequence(session, args);
1081 nfs41_sequence_res_init(res);
1082 rpc_call_start(task);
1085 /* Try again in 1/4 second */
1086 if (args->sa_privileged)
1087 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1088 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1090 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1091 NULL, jiffies + (HZ >> 2));
1092 spin_unlock(&tbl->slot_tbl_lock);
1095 if (args->sa_privileged)
1096 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1097 RPC_PRIORITY_PRIVILEGED);
1099 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1100 spin_unlock(&tbl->slot_tbl_lock);
1103 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1105 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1107 struct nfs4_call_sync_data *data = calldata;
1108 nfs4_setup_sequence(data->seq_server->nfs_client,
1109 data->seq_args, data->seq_res, task);
1112 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1114 struct nfs4_call_sync_data *data = calldata;
1115 nfs4_sequence_done(task, data->seq_res);
1118 static const struct rpc_call_ops nfs40_call_sync_ops = {
1119 .rpc_call_prepare = nfs40_call_sync_prepare,
1120 .rpc_call_done = nfs40_call_sync_done,
1123 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1126 struct rpc_task *task;
1128 task = rpc_run_task(task_setup);
1130 return PTR_ERR(task);
1132 ret = task->tk_status;
1137 static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1138 struct nfs_server *server,
1139 struct rpc_message *msg,
1140 struct nfs4_sequence_args *args,
1141 struct nfs4_sequence_res *res,
1142 unsigned short task_flags)
1144 struct nfs_client *clp = server->nfs_client;
1145 struct nfs4_call_sync_data data = {
1146 .seq_server = server,
1150 struct rpc_task_setup task_setup = {
1153 .callback_ops = clp->cl_mvops->call_sync_ops,
1154 .callback_data = &data,
1155 .flags = task_flags,
1158 return nfs4_call_sync_custom(&task_setup);
1161 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1162 struct nfs_server *server,
1163 struct rpc_message *msg,
1164 struct nfs4_sequence_args *args,
1165 struct nfs4_sequence_res *res)
1167 unsigned short task_flags = 0;
1169 if (server->caps & NFS_CAP_MOVEABLE)
1170 task_flags = RPC_TASK_MOVEABLE;
1171 return nfs4_do_call_sync(clnt, server, msg, args, res, task_flags);
1175 int nfs4_call_sync(struct rpc_clnt *clnt,
1176 struct nfs_server *server,
1177 struct rpc_message *msg,
1178 struct nfs4_sequence_args *args,
1179 struct nfs4_sequence_res *res,
1182 nfs4_init_sequence(args, res, cache_reply, 0);
1183 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1187 nfs4_inc_nlink_locked(struct inode *inode)
1189 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1190 NFS_INO_INVALID_CTIME |
1191 NFS_INO_INVALID_NLINK);
1196 nfs4_inc_nlink(struct inode *inode)
1198 spin_lock(&inode->i_lock);
1199 nfs4_inc_nlink_locked(inode);
1200 spin_unlock(&inode->i_lock);
1204 nfs4_dec_nlink_locked(struct inode *inode)
1206 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1207 NFS_INO_INVALID_CTIME |
1208 NFS_INO_INVALID_NLINK);
1213 nfs4_update_changeattr_locked(struct inode *inode,
1214 struct nfs4_change_info *cinfo,
1215 unsigned long timestamp, unsigned long cache_validity)
1217 struct nfs_inode *nfsi = NFS_I(inode);
1218 u64 change_attr = inode_peek_iversion_raw(inode);
1220 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
1221 if (S_ISDIR(inode->i_mode))
1222 cache_validity |= NFS_INO_INVALID_DATA;
1224 switch (NFS_SERVER(inode)->change_attr_type) {
1225 case NFS4_CHANGE_TYPE_IS_UNDEFINED:
1226 if (cinfo->after == change_attr)
1230 if ((s64)(change_attr - cinfo->after) >= 0)
1234 inode_set_iversion_raw(inode, cinfo->after);
1235 if (!cinfo->atomic || cinfo->before != change_attr) {
1236 if (S_ISDIR(inode->i_mode))
1237 nfs_force_lookup_revalidate(inode);
1239 if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1241 NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
1242 NFS_INO_INVALID_SIZE | NFS_INO_INVALID_OTHER |
1243 NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
1244 NFS_INO_INVALID_MODE | NFS_INO_INVALID_XATTR;
1245 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1247 nfsi->attrtimeo_timestamp = jiffies;
1248 nfsi->read_cache_jiffies = timestamp;
1249 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1250 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1252 nfs_set_cache_invalid(inode, cache_validity);
1256 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1257 unsigned long timestamp, unsigned long cache_validity)
1259 spin_lock(&dir->i_lock);
1260 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1261 spin_unlock(&dir->i_lock);
1264 struct nfs4_open_createattrs {
1265 struct nfs4_label *label;
1266 struct iattr *sattr;
1267 const __u32 verf[2];
1270 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1271 int err, struct nfs4_exception *exception)
1275 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1277 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1278 exception->retry = 1;
1282 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1284 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1287 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1289 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1291 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1295 nfs4_map_atomic_open_share(struct nfs_server *server,
1296 fmode_t fmode, int openflags)
1300 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1302 res = NFS4_SHARE_ACCESS_READ;
1305 res = NFS4_SHARE_ACCESS_WRITE;
1307 case FMODE_READ|FMODE_WRITE:
1308 res = NFS4_SHARE_ACCESS_BOTH;
1310 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1312 /* Want no delegation if we're using O_DIRECT */
1313 if (openflags & O_DIRECT)
1314 res |= NFS4_SHARE_WANT_NO_DELEG;
1319 static enum open_claim_type4
1320 nfs4_map_atomic_open_claim(struct nfs_server *server,
1321 enum open_claim_type4 claim)
1323 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1328 case NFS4_OPEN_CLAIM_FH:
1329 return NFS4_OPEN_CLAIM_NULL;
1330 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1331 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1332 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1333 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1337 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1339 p->o_res.f_attr = &p->f_attr;
1340 p->o_res.seqid = p->o_arg.seqid;
1341 p->c_res.seqid = p->c_arg.seqid;
1342 p->o_res.server = p->o_arg.server;
1343 p->o_res.access_request = p->o_arg.access;
1344 nfs_fattr_init(&p->f_attr);
1345 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1348 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1349 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1350 const struct nfs4_open_createattrs *c,
1351 enum open_claim_type4 claim,
1354 struct dentry *parent = dget_parent(dentry);
1355 struct inode *dir = d_inode(parent);
1356 struct nfs_server *server = NFS_SERVER(dir);
1357 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1358 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1359 struct nfs4_opendata *p;
1361 p = kzalloc(sizeof(*p), gfp_mask);
1365 p->f_attr.label = nfs4_label_alloc(server, gfp_mask);
1366 if (IS_ERR(p->f_attr.label))
1369 p->a_label = nfs4_label_alloc(server, gfp_mask);
1370 if (IS_ERR(p->a_label))
1373 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1374 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1375 if (IS_ERR(p->o_arg.seqid))
1376 goto err_free_label;
1377 nfs_sb_active(dentry->d_sb);
1378 p->dentry = dget(dentry);
1381 atomic_inc(&sp->so_count);
1382 p->o_arg.open_flags = flags;
1383 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1384 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1385 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1387 if (flags & O_CREAT) {
1388 p->o_arg.umask = current_umask();
1389 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1390 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1391 p->o_arg.u.attrs = &p->attrs;
1392 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1394 memcpy(p->o_arg.u.verifier.data, c->verf,
1395 sizeof(p->o_arg.u.verifier.data));
1398 /* ask server to check for all possible rights as results
1400 switch (p->o_arg.claim) {
1403 case NFS4_OPEN_CLAIM_NULL:
1404 case NFS4_OPEN_CLAIM_FH:
1405 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1406 NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE |
1407 NFS4_ACCESS_EXECUTE |
1408 nfs_access_xattr_mask(server);
1410 p->o_arg.clientid = server->nfs_client->cl_clientid;
1411 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1412 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1413 p->o_arg.name = &dentry->d_name;
1414 p->o_arg.server = server;
1415 p->o_arg.bitmask = nfs4_bitmask(server, label);
1416 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1417 switch (p->o_arg.claim) {
1418 case NFS4_OPEN_CLAIM_NULL:
1419 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1420 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1421 p->o_arg.fh = NFS_FH(dir);
1423 case NFS4_OPEN_CLAIM_PREVIOUS:
1424 case NFS4_OPEN_CLAIM_FH:
1425 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1426 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1427 p->o_arg.fh = NFS_FH(d_inode(dentry));
1429 p->c_arg.fh = &p->o_res.fh;
1430 p->c_arg.stateid = &p->o_res.stateid;
1431 p->c_arg.seqid = p->o_arg.seqid;
1432 nfs4_init_opendata_res(p);
1433 kref_init(&p->kref);
1437 nfs4_label_free(p->a_label);
1439 nfs4_label_free(p->f_attr.label);
1447 static void nfs4_opendata_free(struct kref *kref)
1449 struct nfs4_opendata *p = container_of(kref,
1450 struct nfs4_opendata, kref);
1451 struct super_block *sb = p->dentry->d_sb;
1453 nfs4_lgopen_release(p->lgp);
1454 nfs_free_seqid(p->o_arg.seqid);
1455 nfs4_sequence_free_slot(&p->o_res.seq_res);
1456 if (p->state != NULL)
1457 nfs4_put_open_state(p->state);
1458 nfs4_put_state_owner(p->owner);
1460 nfs4_label_free(p->a_label);
1461 nfs4_label_free(p->f_attr.label);
1465 nfs_sb_deactive(sb);
1466 nfs_fattr_free_names(&p->f_attr);
1467 kfree(p->f_attr.mdsthreshold);
1471 static void nfs4_opendata_put(struct nfs4_opendata *p)
1474 kref_put(&p->kref, nfs4_opendata_free);
1477 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1480 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1481 case FMODE_READ|FMODE_WRITE:
1482 return state->n_rdwr != 0;
1484 return state->n_wronly != 0;
1486 return state->n_rdonly != 0;
1492 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1493 int open_mode, enum open_claim_type4 claim)
1497 if (open_mode & (O_EXCL|O_TRUNC))
1500 case NFS4_OPEN_CLAIM_NULL:
1501 case NFS4_OPEN_CLAIM_FH:
1506 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1508 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1509 && state->n_rdonly != 0;
1512 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1513 && state->n_wronly != 0;
1515 case FMODE_READ|FMODE_WRITE:
1516 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1517 && state->n_rdwr != 0;
1523 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1524 enum open_claim_type4 claim)
1526 if (delegation == NULL)
1528 if ((delegation->type & fmode) != fmode)
1531 case NFS4_OPEN_CLAIM_NULL:
1532 case NFS4_OPEN_CLAIM_FH:
1534 case NFS4_OPEN_CLAIM_PREVIOUS:
1535 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1541 nfs_mark_delegation_referenced(delegation);
1545 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1554 case FMODE_READ|FMODE_WRITE:
1557 nfs4_state_set_mode_locked(state, state->state | fmode);
1560 #ifdef CONFIG_NFS_V4_1
1561 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1563 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1565 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1567 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1571 #endif /* CONFIG_NFS_V4_1 */
1573 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1575 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1576 wake_up_all(&state->waitq);
1579 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1581 struct nfs_client *clp = state->owner->so_server->nfs_client;
1582 bool need_recover = false;
1584 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1585 need_recover = true;
1586 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1587 need_recover = true;
1588 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1589 need_recover = true;
1591 nfs4_state_mark_reclaim_nograce(clp, state);
1595 * Check for whether or not the caller may update the open stateid
1596 * to the value passed in by stateid.
1598 * Note: This function relies heavily on the server implementing
1599 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1601 * i.e. The stateid seqids have to be initialised to 1, and
1602 * are then incremented on every state transition.
1604 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1605 const nfs4_stateid *stateid)
1607 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1608 /* The common case - we're updating to a new sequence number */
1609 if (nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1610 if (nfs4_stateid_is_next(&state->open_stateid, stateid))
1614 /* The server returned a new stateid */
1616 /* This is the first OPEN in this generation */
1617 if (stateid->seqid == cpu_to_be32(1))
1622 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1624 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1626 if (state->n_wronly)
1627 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1628 if (state->n_rdonly)
1629 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1631 set_bit(NFS_O_RDWR_STATE, &state->flags);
1632 set_bit(NFS_OPEN_STATE, &state->flags);
1635 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1636 nfs4_stateid *stateid, fmode_t fmode)
1638 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1639 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1641 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1644 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1647 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1648 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1649 clear_bit(NFS_OPEN_STATE, &state->flags);
1651 if (stateid == NULL)
1653 /* Handle OPEN+OPEN_DOWNGRADE races */
1654 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1655 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1656 nfs_resync_open_stateid_locked(state);
1659 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1660 nfs4_stateid_copy(&state->stateid, stateid);
1661 nfs4_stateid_copy(&state->open_stateid, stateid);
1662 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1664 nfs_state_log_update_open_stateid(state);
1667 static void nfs_clear_open_stateid(struct nfs4_state *state,
1668 nfs4_stateid *arg_stateid,
1669 nfs4_stateid *stateid, fmode_t fmode)
1671 write_seqlock(&state->seqlock);
1672 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1673 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1674 nfs_clear_open_stateid_locked(state, stateid, fmode);
1675 write_sequnlock(&state->seqlock);
1676 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1677 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1680 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1681 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1682 __must_hold(&state->owner->so_lock)
1683 __must_hold(&state->seqlock)
1691 if (nfs_stateid_is_sequential(state, stateid))
1696 /* Rely on seqids for serialisation with NFSv4.0 */
1697 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1700 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1701 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1703 * Ensure we process the state changes in the same order
1704 * in which the server processed them by delaying the
1705 * update of the stateid until we are in sequence.
1707 write_sequnlock(&state->seqlock);
1708 spin_unlock(&state->owner->so_lock);
1710 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1712 if (!fatal_signal_pending(current)) {
1713 if (schedule_timeout(5*HZ) == 0)
1719 finish_wait(&state->waitq, &wait);
1721 spin_lock(&state->owner->so_lock);
1722 write_seqlock(&state->seqlock);
1725 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1726 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1727 nfs4_stateid_copy(freeme, &state->open_stateid);
1728 nfs_test_and_clear_all_open_stateid(state);
1731 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1732 nfs4_stateid_copy(&state->stateid, stateid);
1733 nfs4_stateid_copy(&state->open_stateid, stateid);
1734 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1735 nfs_state_log_update_open_stateid(state);
1738 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1739 const nfs4_stateid *open_stateid,
1741 nfs4_stateid *freeme)
1744 * Protect the call to nfs4_state_set_mode_locked and
1745 * serialise the stateid update
1747 write_seqlock(&state->seqlock);
1748 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1751 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1754 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1756 case FMODE_READ|FMODE_WRITE:
1757 set_bit(NFS_O_RDWR_STATE, &state->flags);
1759 set_bit(NFS_OPEN_STATE, &state->flags);
1760 write_sequnlock(&state->seqlock);
1763 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1765 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1766 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1767 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1768 clear_bit(NFS_OPEN_STATE, &state->flags);
1771 static void nfs_state_set_delegation(struct nfs4_state *state,
1772 const nfs4_stateid *deleg_stateid,
1776 * Protect the call to nfs4_state_set_mode_locked and
1777 * serialise the stateid update
1779 write_seqlock(&state->seqlock);
1780 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1781 set_bit(NFS_DELEGATED_STATE, &state->flags);
1782 write_sequnlock(&state->seqlock);
1785 static void nfs_state_clear_delegation(struct nfs4_state *state)
1787 write_seqlock(&state->seqlock);
1788 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1789 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1790 write_sequnlock(&state->seqlock);
1793 int update_open_stateid(struct nfs4_state *state,
1794 const nfs4_stateid *open_stateid,
1795 const nfs4_stateid *delegation,
1798 struct nfs_server *server = NFS_SERVER(state->inode);
1799 struct nfs_client *clp = server->nfs_client;
1800 struct nfs_inode *nfsi = NFS_I(state->inode);
1801 struct nfs_delegation *deleg_cur;
1802 nfs4_stateid freeme = { };
1805 fmode &= (FMODE_READ|FMODE_WRITE);
1808 spin_lock(&state->owner->so_lock);
1809 if (open_stateid != NULL) {
1810 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1814 deleg_cur = nfs4_get_valid_delegation(state->inode);
1815 if (deleg_cur == NULL)
1818 spin_lock(&deleg_cur->lock);
1819 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1820 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1821 (deleg_cur->type & fmode) != fmode)
1822 goto no_delegation_unlock;
1824 if (delegation == NULL)
1825 delegation = &deleg_cur->stateid;
1826 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1827 goto no_delegation_unlock;
1829 nfs_mark_delegation_referenced(deleg_cur);
1830 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1832 no_delegation_unlock:
1833 spin_unlock(&deleg_cur->lock);
1836 update_open_stateflags(state, fmode);
1837 spin_unlock(&state->owner->so_lock);
1840 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1841 nfs4_schedule_state_manager(clp);
1842 if (freeme.type != 0)
1843 nfs4_test_and_free_stateid(server, &freeme,
1844 state->owner->so_cred);
1849 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1850 const nfs4_stateid *stateid)
1852 struct nfs4_state *state = lsp->ls_state;
1855 spin_lock(&state->state_lock);
1856 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1858 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1860 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1863 spin_unlock(&state->state_lock);
1867 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1869 struct nfs_delegation *delegation;
1871 fmode &= FMODE_READ|FMODE_WRITE;
1873 delegation = nfs4_get_valid_delegation(inode);
1874 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1879 nfs4_inode_return_delegation(inode);
1882 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1884 struct nfs4_state *state = opendata->state;
1885 struct nfs_delegation *delegation;
1886 int open_mode = opendata->o_arg.open_flags;
1887 fmode_t fmode = opendata->o_arg.fmode;
1888 enum open_claim_type4 claim = opendata->o_arg.claim;
1889 nfs4_stateid stateid;
1893 spin_lock(&state->owner->so_lock);
1894 if (can_open_cached(state, fmode, open_mode, claim)) {
1895 update_open_stateflags(state, fmode);
1896 spin_unlock(&state->owner->so_lock);
1897 goto out_return_state;
1899 spin_unlock(&state->owner->so_lock);
1901 delegation = nfs4_get_valid_delegation(state->inode);
1902 if (!can_open_delegated(delegation, fmode, claim)) {
1906 /* Save the delegation */
1907 nfs4_stateid_copy(&stateid, &delegation->stateid);
1909 nfs_release_seqid(opendata->o_arg.seqid);
1910 if (!opendata->is_recover) {
1911 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1917 /* Try to update the stateid using the delegation */
1918 if (update_open_stateid(state, NULL, &stateid, fmode))
1919 goto out_return_state;
1922 return ERR_PTR(ret);
1924 refcount_inc(&state->count);
1929 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1931 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1932 struct nfs_delegation *delegation;
1933 int delegation_flags = 0;
1936 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1938 delegation_flags = delegation->flags;
1940 switch (data->o_arg.claim) {
1943 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1944 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1945 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1946 "returning a delegation for "
1947 "OPEN(CLAIM_DELEGATE_CUR)\n",
1951 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1952 nfs_inode_set_delegation(state->inode,
1953 data->owner->so_cred,
1954 data->o_res.delegation_type,
1955 &data->o_res.delegation,
1956 data->o_res.pagemod_limit);
1958 nfs_inode_reclaim_delegation(state->inode,
1959 data->owner->so_cred,
1960 data->o_res.delegation_type,
1961 &data->o_res.delegation,
1962 data->o_res.pagemod_limit);
1964 if (data->o_res.do_recall)
1965 nfs_async_inode_return_delegation(state->inode,
1966 &data->o_res.delegation);
1970 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1971 * and update the nfs4_state.
1973 static struct nfs4_state *
1974 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1976 struct inode *inode = data->state->inode;
1977 struct nfs4_state *state = data->state;
1980 if (!data->rpc_done) {
1981 if (data->rpc_status)
1982 return ERR_PTR(data->rpc_status);
1983 return nfs4_try_open_cached(data);
1986 ret = nfs_refresh_inode(inode, &data->f_attr);
1988 return ERR_PTR(ret);
1990 if (data->o_res.delegation_type != 0)
1991 nfs4_opendata_check_deleg(data, state);
1993 if (!update_open_stateid(state, &data->o_res.stateid,
1994 NULL, data->o_arg.fmode))
1995 return ERR_PTR(-EAGAIN);
1996 refcount_inc(&state->count);
2001 static struct inode *
2002 nfs4_opendata_get_inode(struct nfs4_opendata *data)
2004 struct inode *inode;
2006 switch (data->o_arg.claim) {
2007 case NFS4_OPEN_CLAIM_NULL:
2008 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2009 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2010 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2011 return ERR_PTR(-EAGAIN);
2012 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2016 inode = d_inode(data->dentry);
2018 nfs_refresh_inode(inode, &data->f_attr);
2023 static struct nfs4_state *
2024 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2026 struct nfs4_state *state;
2027 struct inode *inode;
2029 inode = nfs4_opendata_get_inode(data);
2031 return ERR_CAST(inode);
2032 if (data->state != NULL && data->state->inode == inode) {
2033 state = data->state;
2034 refcount_inc(&state->count);
2036 state = nfs4_get_open_state(inode, data->owner);
2039 state = ERR_PTR(-ENOMEM);
2043 static struct nfs4_state *
2044 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2046 struct nfs4_state *state;
2048 if (!data->rpc_done) {
2049 state = nfs4_try_open_cached(data);
2050 trace_nfs4_cached_open(data->state);
2054 state = nfs4_opendata_find_nfs4_state(data);
2058 if (data->o_res.delegation_type != 0)
2059 nfs4_opendata_check_deleg(data, state);
2060 if (!update_open_stateid(state, &data->o_res.stateid,
2061 NULL, data->o_arg.fmode)) {
2062 nfs4_put_open_state(state);
2063 state = ERR_PTR(-EAGAIN);
2066 nfs_release_seqid(data->o_arg.seqid);
2070 static struct nfs4_state *
2071 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2073 struct nfs4_state *ret;
2075 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2076 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2078 ret = _nfs4_opendata_to_nfs4_state(data);
2079 nfs4_sequence_free_slot(&data->o_res.seq_res);
2083 static struct nfs_open_context *
2084 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2086 struct nfs_inode *nfsi = NFS_I(state->inode);
2087 struct nfs_open_context *ctx;
2090 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2091 if (ctx->state != state)
2093 if ((ctx->mode & mode) != mode)
2095 if (!get_nfs_open_context(ctx))
2101 return ERR_PTR(-ENOENT);
2104 static struct nfs_open_context *
2105 nfs4_state_find_open_context(struct nfs4_state *state)
2107 struct nfs_open_context *ctx;
2109 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2112 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2115 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2118 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2119 struct nfs4_state *state, enum open_claim_type4 claim)
2121 struct nfs4_opendata *opendata;
2123 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2124 NULL, claim, GFP_NOFS);
2125 if (opendata == NULL)
2126 return ERR_PTR(-ENOMEM);
2127 opendata->state = state;
2128 refcount_inc(&state->count);
2132 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2135 struct nfs4_state *newstate;
2136 struct nfs_server *server = NFS_SB(opendata->dentry->d_sb);
2137 int openflags = opendata->o_arg.open_flags;
2140 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2142 opendata->o_arg.fmode = fmode;
2143 opendata->o_arg.share_access =
2144 nfs4_map_atomic_open_share(server, fmode, openflags);
2145 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2146 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2147 nfs4_init_opendata_res(opendata);
2148 ret = _nfs4_recover_proc_open(opendata);
2151 newstate = nfs4_opendata_to_nfs4_state(opendata);
2152 if (IS_ERR(newstate))
2153 return PTR_ERR(newstate);
2154 if (newstate != opendata->state)
2156 nfs4_close_state(newstate, fmode);
2160 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2164 /* memory barrier prior to reading state->n_* */
2166 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2169 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2172 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2176 * We may have performed cached opens for all three recoveries.
2177 * Check if we need to update the current stateid.
2179 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2180 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2181 write_seqlock(&state->seqlock);
2182 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2183 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2184 write_sequnlock(&state->seqlock);
2191 * reclaim state on the server after a reboot.
2193 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2195 struct nfs_delegation *delegation;
2196 struct nfs4_opendata *opendata;
2197 fmode_t delegation_type = 0;
2200 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2201 NFS4_OPEN_CLAIM_PREVIOUS);
2202 if (IS_ERR(opendata))
2203 return PTR_ERR(opendata);
2205 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2206 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2207 delegation_type = delegation->type;
2209 opendata->o_arg.u.delegation_type = delegation_type;
2210 status = nfs4_open_recover(opendata, state);
2211 nfs4_opendata_put(opendata);
2215 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2217 struct nfs_server *server = NFS_SERVER(state->inode);
2218 struct nfs4_exception exception = { };
2221 err = _nfs4_do_open_reclaim(ctx, state);
2222 trace_nfs4_open_reclaim(ctx, 0, err);
2223 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2225 if (err != -NFS4ERR_DELAY)
2227 nfs4_handle_exception(server, err, &exception);
2228 } while (exception.retry);
2232 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2234 struct nfs_open_context *ctx;
2237 ctx = nfs4_state_find_open_context(state);
2240 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2241 nfs_state_clear_open_state_flags(state);
2242 ret = nfs4_do_open_reclaim(ctx, state);
2243 put_nfs_open_context(ctx);
2247 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)
2251 printk(KERN_ERR "NFS: %s: unhandled error "
2252 "%d.\n", __func__, err);
2260 case -NFS4ERR_BADSESSION:
2261 case -NFS4ERR_BADSLOT:
2262 case -NFS4ERR_BAD_HIGH_SLOT:
2263 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2264 case -NFS4ERR_DEADSESSION:
2266 case -NFS4ERR_STALE_CLIENTID:
2267 case -NFS4ERR_STALE_STATEID:
2268 /* Don't recall a delegation if it was lost */
2269 nfs4_schedule_lease_recovery(server->nfs_client);
2271 case -NFS4ERR_MOVED:
2272 nfs4_schedule_migration_recovery(server);
2274 case -NFS4ERR_LEASE_MOVED:
2275 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2277 case -NFS4ERR_DELEG_REVOKED:
2278 case -NFS4ERR_ADMIN_REVOKED:
2279 case -NFS4ERR_EXPIRED:
2280 case -NFS4ERR_BAD_STATEID:
2281 case -NFS4ERR_OPENMODE:
2282 nfs_inode_find_state_and_recover(state->inode,
2284 nfs4_schedule_stateid_recovery(server, state);
2286 case -NFS4ERR_DELAY:
2287 case -NFS4ERR_GRACE:
2291 case -NFS4ERR_DENIED:
2293 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2295 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2302 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2303 struct nfs4_state *state, const nfs4_stateid *stateid)
2305 struct nfs_server *server = NFS_SERVER(state->inode);
2306 struct nfs4_opendata *opendata;
2309 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2310 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2311 if (IS_ERR(opendata))
2312 return PTR_ERR(opendata);
2313 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2314 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2315 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2319 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2320 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2324 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2325 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2329 nfs_state_clear_delegation(state);
2331 nfs4_opendata_put(opendata);
2332 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2335 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2337 struct nfs4_opendata *data = calldata;
2339 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2340 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2343 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2345 struct nfs4_opendata *data = calldata;
2347 nfs40_sequence_done(task, &data->c_res.seq_res);
2349 data->rpc_status = task->tk_status;
2350 if (data->rpc_status == 0) {
2351 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2352 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2353 renew_lease(data->o_res.server, data->timestamp);
2354 data->rpc_done = true;
2358 static void nfs4_open_confirm_release(void *calldata)
2360 struct nfs4_opendata *data = calldata;
2361 struct nfs4_state *state = NULL;
2363 /* If this request hasn't been cancelled, do nothing */
2364 if (!data->cancelled)
2366 /* In case of error, no cleanup! */
2367 if (!data->rpc_done)
2369 state = nfs4_opendata_to_nfs4_state(data);
2371 nfs4_close_state(state, data->o_arg.fmode);
2373 nfs4_opendata_put(data);
2376 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2377 .rpc_call_prepare = nfs4_open_confirm_prepare,
2378 .rpc_call_done = nfs4_open_confirm_done,
2379 .rpc_release = nfs4_open_confirm_release,
2383 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2385 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2387 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2388 struct rpc_task *task;
2389 struct rpc_message msg = {
2390 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2391 .rpc_argp = &data->c_arg,
2392 .rpc_resp = &data->c_res,
2393 .rpc_cred = data->owner->so_cred,
2395 struct rpc_task_setup task_setup_data = {
2396 .rpc_client = server->client,
2397 .rpc_message = &msg,
2398 .callback_ops = &nfs4_open_confirm_ops,
2399 .callback_data = data,
2400 .workqueue = nfsiod_workqueue,
2401 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2405 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2407 kref_get(&data->kref);
2408 data->rpc_done = false;
2409 data->rpc_status = 0;
2410 data->timestamp = jiffies;
2411 task = rpc_run_task(&task_setup_data);
2413 return PTR_ERR(task);
2414 status = rpc_wait_for_completion_task(task);
2416 data->cancelled = true;
2419 status = data->rpc_status;
2424 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2426 struct nfs4_opendata *data = calldata;
2427 struct nfs4_state_owner *sp = data->owner;
2428 struct nfs_client *clp = sp->so_server->nfs_client;
2429 enum open_claim_type4 claim = data->o_arg.claim;
2431 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2434 * Check if we still need to send an OPEN call, or if we can use
2435 * a delegation instead.
2437 if (data->state != NULL) {
2438 struct nfs_delegation *delegation;
2440 if (can_open_cached(data->state, data->o_arg.fmode,
2441 data->o_arg.open_flags, claim))
2444 delegation = nfs4_get_valid_delegation(data->state->inode);
2445 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2446 goto unlock_no_action;
2449 /* Update client id. */
2450 data->o_arg.clientid = clp->cl_clientid;
2454 case NFS4_OPEN_CLAIM_PREVIOUS:
2455 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2456 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2457 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2459 case NFS4_OPEN_CLAIM_FH:
2460 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2462 data->timestamp = jiffies;
2463 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2464 &data->o_arg.seq_args,
2465 &data->o_res.seq_res,
2467 nfs_release_seqid(data->o_arg.seqid);
2469 /* Set the create mode (note dependency on the session type) */
2470 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2471 if (data->o_arg.open_flags & O_EXCL) {
2472 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2473 if (clp->cl_mvops->minor_version == 0) {
2474 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2475 /* don't put an ACCESS op in OPEN compound if O_EXCL,
2476 * because ACCESS will return permission denied for
2477 * all bits until close */
2478 data->o_res.access_request = data->o_arg.access = 0;
2479 } else if (nfs4_has_persistent_session(clp))
2480 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2484 trace_nfs4_cached_open(data->state);
2487 task->tk_action = NULL;
2489 nfs4_sequence_done(task, &data->o_res.seq_res);
2492 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2494 struct nfs4_opendata *data = calldata;
2496 data->rpc_status = task->tk_status;
2498 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2501 if (task->tk_status == 0) {
2502 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2503 switch (data->o_res.f_attr->mode & S_IFMT) {
2507 data->rpc_status = -ELOOP;
2510 data->rpc_status = -EISDIR;
2513 data->rpc_status = -ENOTDIR;
2516 renew_lease(data->o_res.server, data->timestamp);
2517 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2518 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2520 data->rpc_done = true;
2523 static void nfs4_open_release(void *calldata)
2525 struct nfs4_opendata *data = calldata;
2526 struct nfs4_state *state = NULL;
2528 /* If this request hasn't been cancelled, do nothing */
2529 if (!data->cancelled)
2531 /* In case of error, no cleanup! */
2532 if (data->rpc_status != 0 || !data->rpc_done)
2534 /* In case we need an open_confirm, no cleanup! */
2535 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2537 state = nfs4_opendata_to_nfs4_state(data);
2539 nfs4_close_state(state, data->o_arg.fmode);
2541 nfs4_opendata_put(data);
2544 static const struct rpc_call_ops nfs4_open_ops = {
2545 .rpc_call_prepare = nfs4_open_prepare,
2546 .rpc_call_done = nfs4_open_done,
2547 .rpc_release = nfs4_open_release,
2550 static int nfs4_run_open_task(struct nfs4_opendata *data,
2551 struct nfs_open_context *ctx)
2553 struct inode *dir = d_inode(data->dir);
2554 struct nfs_server *server = NFS_SERVER(dir);
2555 struct nfs_openargs *o_arg = &data->o_arg;
2556 struct nfs_openres *o_res = &data->o_res;
2557 struct rpc_task *task;
2558 struct rpc_message msg = {
2559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2562 .rpc_cred = data->owner->so_cred,
2564 struct rpc_task_setup task_setup_data = {
2565 .rpc_client = server->client,
2566 .rpc_message = &msg,
2567 .callback_ops = &nfs4_open_ops,
2568 .callback_data = data,
2569 .workqueue = nfsiod_workqueue,
2570 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2574 if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
2575 task_setup_data.flags |= RPC_TASK_MOVEABLE;
2577 kref_get(&data->kref);
2578 data->rpc_done = false;
2579 data->rpc_status = 0;
2580 data->cancelled = false;
2581 data->is_recover = false;
2583 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2584 data->is_recover = true;
2585 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2587 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2588 pnfs_lgopen_prepare(data, ctx);
2590 task = rpc_run_task(&task_setup_data);
2592 return PTR_ERR(task);
2593 status = rpc_wait_for_completion_task(task);
2595 data->cancelled = true;
2598 status = data->rpc_status;
2604 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2606 struct inode *dir = d_inode(data->dir);
2607 struct nfs_openres *o_res = &data->o_res;
2610 status = nfs4_run_open_task(data, NULL);
2611 if (status != 0 || !data->rpc_done)
2614 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2616 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2617 status = _nfs4_proc_open_confirm(data);
2623 * Additional permission checks in order to distinguish between an
2624 * open for read, and an open for execute. This works around the
2625 * fact that NFSv4 OPEN treats read and execute permissions as being
2627 * Note that in the non-execute case, we want to turn off permission
2628 * checking if we just created a new file (POSIX open() semantics).
2630 static int nfs4_opendata_access(const struct cred *cred,
2631 struct nfs4_opendata *opendata,
2632 struct nfs4_state *state, fmode_t fmode)
2634 struct nfs_access_entry cache;
2637 /* access call failed or for some reason the server doesn't
2638 * support any access modes -- defer access call until later */
2639 if (opendata->o_res.access_supported == 0)
2643 if (fmode & FMODE_EXEC) {
2644 /* ONLY check for exec rights */
2645 if (S_ISDIR(state->inode->i_mode))
2646 mask = NFS4_ACCESS_LOOKUP;
2648 mask = NFS4_ACCESS_EXECUTE;
2649 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2650 mask = NFS4_ACCESS_READ;
2652 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2653 nfs_access_add_cache(state->inode, &cache, cred);
2655 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2656 if ((mask & ~cache.mask & flags) == 0)
2663 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2665 static int _nfs4_proc_open(struct nfs4_opendata *data,
2666 struct nfs_open_context *ctx)
2668 struct inode *dir = d_inode(data->dir);
2669 struct nfs_server *server = NFS_SERVER(dir);
2670 struct nfs_openargs *o_arg = &data->o_arg;
2671 struct nfs_openres *o_res = &data->o_res;
2674 status = nfs4_run_open_task(data, ctx);
2675 if (!data->rpc_done)
2678 if (status == -NFS4ERR_BADNAME &&
2679 !(o_arg->open_flags & O_CREAT))
2684 nfs_fattr_map_and_free_names(server, &data->f_attr);
2686 if (o_arg->open_flags & O_CREAT) {
2687 if (o_arg->open_flags & O_EXCL)
2688 data->file_created = true;
2689 else if (o_res->cinfo.before != o_res->cinfo.after)
2690 data->file_created = true;
2691 if (data->file_created ||
2692 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2693 nfs4_update_changeattr(dir, &o_res->cinfo,
2694 o_res->f_attr->time_start,
2695 NFS_INO_INVALID_DATA);
2697 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2698 server->caps &= ~NFS_CAP_POSIX_LOCK;
2699 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2700 status = _nfs4_proc_open_confirm(data);
2704 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2705 nfs4_sequence_free_slot(&o_res->seq_res);
2706 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, NULL);
2713 * reclaim state on the server after a network partition.
2714 * Assumes caller holds the appropriate lock
2716 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2718 struct nfs4_opendata *opendata;
2721 opendata = nfs4_open_recoverdata_alloc(ctx, state, NFS4_OPEN_CLAIM_FH);
2722 if (IS_ERR(opendata))
2723 return PTR_ERR(opendata);
2725 * We're not recovering a delegation, so ask for no delegation.
2726 * Otherwise the recovery thread could deadlock with an outstanding
2727 * delegation return.
2729 opendata->o_arg.open_flags = O_DIRECT;
2730 ret = nfs4_open_recover(opendata, state);
2732 d_drop(ctx->dentry);
2733 nfs4_opendata_put(opendata);
2737 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2739 struct nfs_server *server = NFS_SERVER(state->inode);
2740 struct nfs4_exception exception = { };
2744 err = _nfs4_open_expired(ctx, state);
2745 trace_nfs4_open_expired(ctx, 0, err);
2746 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2751 case -NFS4ERR_GRACE:
2752 case -NFS4ERR_DELAY:
2753 nfs4_handle_exception(server, err, &exception);
2756 } while (exception.retry);
2761 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2763 struct nfs_open_context *ctx;
2766 ctx = nfs4_state_find_open_context(state);
2769 ret = nfs4_do_open_expired(ctx, state);
2770 put_nfs_open_context(ctx);
2774 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2775 const nfs4_stateid *stateid)
2777 nfs_remove_bad_delegation(state->inode, stateid);
2778 nfs_state_clear_delegation(state);
2781 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2783 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2784 nfs_finish_clear_delegation_stateid(state, NULL);
2787 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2789 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2790 nfs40_clear_delegation_stateid(state);
2791 nfs_state_clear_open_state_flags(state);
2792 return nfs4_open_expired(sp, state);
2795 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2796 nfs4_stateid *stateid,
2797 const struct cred *cred)
2799 return -NFS4ERR_BAD_STATEID;
2802 #if defined(CONFIG_NFS_V4_1)
2803 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2804 nfs4_stateid *stateid,
2805 const struct cred *cred)
2809 switch (stateid->type) {
2812 case NFS4_INVALID_STATEID_TYPE:
2813 case NFS4_SPECIAL_STATEID_TYPE:
2814 return -NFS4ERR_BAD_STATEID;
2815 case NFS4_REVOKED_STATEID_TYPE:
2819 status = nfs41_test_stateid(server, stateid, cred);
2821 case -NFS4ERR_EXPIRED:
2822 case -NFS4ERR_ADMIN_REVOKED:
2823 case -NFS4ERR_DELEG_REVOKED:
2829 /* Ack the revoked state to the server */
2830 nfs41_free_stateid(server, stateid, cred, true);
2831 return -NFS4ERR_EXPIRED;
2834 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2836 struct nfs_server *server = NFS_SERVER(state->inode);
2837 nfs4_stateid stateid;
2838 struct nfs_delegation *delegation;
2839 const struct cred *cred = NULL;
2840 int status, ret = NFS_OK;
2842 /* Get the delegation credential for use by test/free_stateid */
2844 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2845 if (delegation == NULL) {
2847 nfs_state_clear_delegation(state);
2851 spin_lock(&delegation->lock);
2852 nfs4_stateid_copy(&stateid, &delegation->stateid);
2854 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2855 &delegation->flags)) {
2856 spin_unlock(&delegation->lock);
2861 if (delegation->cred)
2862 cred = get_cred(delegation->cred);
2863 spin_unlock(&delegation->lock);
2865 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2866 trace_nfs4_test_delegation_stateid(state, NULL, status);
2867 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2868 nfs_finish_clear_delegation_stateid(state, &stateid);
2876 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2880 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2881 nfs4_copy_delegation_stateid(state->inode, state->state,
2883 nfs4_stateid_match_other(&state->stateid, &tmp))
2884 nfs_state_set_delegation(state, &tmp, state->state);
2886 nfs_state_clear_delegation(state);
2890 * nfs41_check_expired_locks - possibly free a lock stateid
2892 * @state: NFSv4 state for an inode
2894 * Returns NFS_OK if recovery for this stateid is now finished.
2895 * Otherwise a negative NFS4ERR value is returned.
2897 static int nfs41_check_expired_locks(struct nfs4_state *state)
2899 int status, ret = NFS_OK;
2900 struct nfs4_lock_state *lsp, *prev = NULL;
2901 struct nfs_server *server = NFS_SERVER(state->inode);
2903 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2906 spin_lock(&state->state_lock);
2907 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2908 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2909 const struct cred *cred = lsp->ls_state->owner->so_cred;
2911 refcount_inc(&lsp->ls_count);
2912 spin_unlock(&state->state_lock);
2914 nfs4_put_lock_state(prev);
2917 status = nfs41_test_and_free_expired_stateid(server,
2920 trace_nfs4_test_lock_stateid(state, lsp, status);
2921 if (status == -NFS4ERR_EXPIRED ||
2922 status == -NFS4ERR_BAD_STATEID) {
2923 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2924 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2925 if (!recover_lost_locks)
2926 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2927 } else if (status != NFS_OK) {
2929 nfs4_put_lock_state(prev);
2932 spin_lock(&state->state_lock);
2935 spin_unlock(&state->state_lock);
2936 nfs4_put_lock_state(prev);
2942 * nfs41_check_open_stateid - possibly free an open stateid
2944 * @state: NFSv4 state for an inode
2946 * Returns NFS_OK if recovery for this stateid is now finished.
2947 * Otherwise a negative NFS4ERR value is returned.
2949 static int nfs41_check_open_stateid(struct nfs4_state *state)
2951 struct nfs_server *server = NFS_SERVER(state->inode);
2952 nfs4_stateid *stateid = &state->open_stateid;
2953 const struct cred *cred = state->owner->so_cred;
2956 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2957 return -NFS4ERR_BAD_STATEID;
2958 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2959 trace_nfs4_test_open_stateid(state, NULL, status);
2960 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2961 nfs_state_clear_open_state_flags(state);
2962 stateid->type = NFS4_INVALID_STATEID_TYPE;
2965 if (nfs_open_stateid_recover_openmode(state))
2966 return -NFS4ERR_OPENMODE;
2970 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2974 status = nfs41_check_delegation_stateid(state);
2975 if (status != NFS_OK)
2977 nfs41_delegation_recover_stateid(state);
2979 status = nfs41_check_expired_locks(state);
2980 if (status != NFS_OK)
2982 status = nfs41_check_open_stateid(state);
2983 if (status != NFS_OK)
2984 status = nfs4_open_expired(sp, state);
2990 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2991 * fields corresponding to attributes that were used to store the verifier.
2992 * Make sure we clobber those fields in the later setattr call
2994 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2995 struct iattr *sattr, struct nfs4_label **label)
2997 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
3002 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
3003 attrset[i] = opendata->o_res.attrset[i];
3004 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3005 attrset[i] &= ~bitmask[i];
3008 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3009 sattr->ia_valid : 0;
3011 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3012 if (sattr->ia_valid & ATTR_ATIME_SET)
3013 ret |= ATTR_ATIME_SET;
3018 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3019 if (sattr->ia_valid & ATTR_MTIME_SET)
3020 ret |= ATTR_MTIME_SET;
3025 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3030 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3031 struct nfs_open_context *ctx)
3033 struct nfs4_state_owner *sp = opendata->owner;
3034 struct nfs_server *server = sp->so_server;
3035 struct dentry *dentry;
3036 struct nfs4_state *state;
3037 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3038 struct inode *dir = d_inode(opendata->dir);
3039 unsigned long dir_verifier;
3043 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3044 dir_verifier = nfs_save_change_attribute(dir);
3046 ret = _nfs4_proc_open(opendata, ctx);
3050 state = _nfs4_opendata_to_nfs4_state(opendata);
3051 ret = PTR_ERR(state);
3055 if (server->caps & NFS_CAP_POSIX_LOCK)
3056 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3057 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3058 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3059 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_PRESERVE_UNLINKED)
3060 set_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(state->inode)->flags);
3062 dentry = opendata->dentry;
3063 if (d_really_is_negative(dentry)) {
3064 struct dentry *alias;
3066 alias = d_exact_alias(dentry, state->inode);
3068 alias = d_splice_alias(igrab(state->inode), dentry);
3069 /* d_splice_alias() can't fail here - it's a non-directory */
3072 ctx->dentry = dentry = alias;
3076 switch(opendata->o_arg.claim) {
3079 case NFS4_OPEN_CLAIM_NULL:
3080 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3081 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3082 if (!opendata->rpc_done)
3084 if (opendata->o_res.delegation_type != 0)
3085 dir_verifier = nfs_save_change_attribute(dir);
3086 nfs_set_verifier(dentry, dir_verifier);
3089 /* Parse layoutget results before we check for access */
3090 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3092 ret = nfs4_opendata_access(sp->so_cred, opendata, state, acc_mode);
3096 if (d_inode(dentry) == state->inode) {
3097 nfs_inode_attach_open_context(ctx);
3098 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3099 nfs4_schedule_stateid_recovery(server, state);
3103 if (!opendata->cancelled) {
3104 if (opendata->lgp) {
3105 nfs4_lgopen_release(opendata->lgp);
3106 opendata->lgp = NULL;
3108 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3114 * Returns a referenced nfs4_state
3116 static int _nfs4_do_open(struct inode *dir,
3117 struct nfs_open_context *ctx,
3119 const struct nfs4_open_createattrs *c,
3122 struct nfs4_state_owner *sp;
3123 struct nfs4_state *state = NULL;
3124 struct nfs_server *server = NFS_SERVER(dir);
3125 struct nfs4_opendata *opendata;
3126 struct dentry *dentry = ctx->dentry;
3127 const struct cred *cred = ctx->cred;
3128 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3129 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3130 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3131 struct iattr *sattr = c->sattr;
3132 struct nfs4_label *label = c->label;
3135 /* Protect against reboot recovery conflicts */
3137 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3139 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3142 status = nfs4_client_recover_expired_lease(server->nfs_client);
3144 goto err_put_state_owner;
3145 if (d_really_is_positive(dentry))
3146 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3148 if (d_really_is_positive(dentry))
3149 claim = NFS4_OPEN_CLAIM_FH;
3150 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3151 c, claim, GFP_KERNEL);
3152 if (opendata == NULL)
3153 goto err_put_state_owner;
3155 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3156 if (!opendata->f_attr.mdsthreshold) {
3157 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3158 if (!opendata->f_attr.mdsthreshold)
3159 goto err_opendata_put;
3161 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3163 if (d_really_is_positive(dentry))
3164 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3166 status = _nfs4_open_and_get_state(opendata, ctx);
3168 goto err_opendata_put;
3171 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3172 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3173 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3175 * send create attributes which was not set by open
3176 * with an extra setattr.
3178 if (attrs || label) {
3179 unsigned ia_old = sattr->ia_valid;
3181 sattr->ia_valid = attrs;
3182 nfs_fattr_init(opendata->o_res.f_attr);
3183 status = nfs4_do_setattr(state->inode, cred,
3184 opendata->o_res.f_attr, sattr,
3187 nfs_setattr_update_inode(state->inode, sattr,
3188 opendata->o_res.f_attr);
3189 nfs_setsecurity(state->inode, opendata->o_res.f_attr);
3191 sattr->ia_valid = ia_old;
3194 if (opened && opendata->file_created)
3197 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3198 *ctx_th = opendata->f_attr.mdsthreshold;
3199 opendata->f_attr.mdsthreshold = NULL;
3202 nfs4_opendata_put(opendata);
3203 nfs4_put_state_owner(sp);
3206 nfs4_opendata_put(opendata);
3207 err_put_state_owner:
3208 nfs4_put_state_owner(sp);
3214 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3215 struct nfs_open_context *ctx,
3217 struct iattr *sattr,
3218 struct nfs4_label *label,
3221 struct nfs_server *server = NFS_SERVER(dir);
3222 struct nfs4_exception exception = {
3223 .interruptible = true,
3225 struct nfs4_state *res;
3226 struct nfs4_open_createattrs c = {
3230 [0] = (__u32)jiffies,
3231 [1] = (__u32)current->pid,
3237 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3239 trace_nfs4_open_file(ctx, flags, status);
3242 /* NOTE: BAD_SEQID means the server and client disagree about the
3243 * book-keeping w.r.t. state-changing operations
3244 * (OPEN/CLOSE/LOCK/LOCKU...)
3245 * It is actually a sign of a bug on the client or on the server.
3247 * If we receive a BAD_SEQID error in the particular case of
3248 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3249 * have unhashed the old state_owner for us, and that we can
3250 * therefore safely retry using a new one. We should still warn
3251 * the user though...
3253 if (status == -NFS4ERR_BAD_SEQID) {
3254 pr_warn_ratelimited("NFS: v4 server %s "
3255 " returned a bad sequence-id error!\n",
3256 NFS_SERVER(dir)->nfs_client->cl_hostname);
3257 exception.retry = 1;
3261 * BAD_STATEID on OPEN means that the server cancelled our
3262 * state before it received the OPEN_CONFIRM.
3263 * Recover by retrying the request as per the discussion
3264 * on Page 181 of RFC3530.
3266 if (status == -NFS4ERR_BAD_STATEID) {
3267 exception.retry = 1;
3270 if (status == -NFS4ERR_EXPIRED) {
3271 nfs4_schedule_lease_recovery(server->nfs_client);
3272 exception.retry = 1;
3275 if (status == -EAGAIN) {
3276 /* We must have found a delegation */
3277 exception.retry = 1;
3280 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3282 res = ERR_PTR(nfs4_handle_exception(server,
3283 status, &exception));
3284 } while (exception.retry);
3288 static int _nfs4_do_setattr(struct inode *inode,
3289 struct nfs_setattrargs *arg,
3290 struct nfs_setattrres *res,
3291 const struct cred *cred,
3292 struct nfs_open_context *ctx)
3294 struct nfs_server *server = NFS_SERVER(inode);
3295 struct rpc_message msg = {
3296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3301 const struct cred *delegation_cred = NULL;
3302 unsigned long timestamp = jiffies;
3306 nfs_fattr_init(res->fattr);
3308 /* Servers should only apply open mode checks for file size changes */
3309 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3311 nfs4_inode_make_writeable(inode);
3315 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3316 /* Use that stateid */
3317 } else if (ctx != NULL && ctx->state) {
3318 struct nfs_lock_context *l_ctx;
3319 if (!nfs4_valid_open_stateid(ctx->state))
3321 l_ctx = nfs_get_lock_context(ctx);
3323 return PTR_ERR(l_ctx);
3324 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3325 &arg->stateid, &delegation_cred);
3326 nfs_put_lock_context(l_ctx);
3329 else if (status == -EAGAIN)
3333 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3335 if (delegation_cred)
3336 msg.rpc_cred = delegation_cred;
3338 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3340 put_cred(delegation_cred);
3341 if (status == 0 && ctx != NULL)
3342 renew_lease(server, timestamp);
3343 trace_nfs4_setattr(inode, &arg->stateid, status);
3347 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3348 struct nfs_fattr *fattr, struct iattr *sattr,
3349 struct nfs_open_context *ctx, struct nfs4_label *ilabel)
3351 struct nfs_server *server = NFS_SERVER(inode);
3352 __u32 bitmask[NFS4_BITMASK_SZ];
3353 struct nfs4_state *state = ctx ? ctx->state : NULL;
3354 struct nfs_setattrargs arg = {
3355 .fh = NFS_FH(inode),
3361 struct nfs_setattrres res = {
3365 struct nfs4_exception exception = {
3368 .stateid = &arg.stateid,
3370 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
3373 if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3374 adjust_flags |= NFS_INO_INVALID_MODE;
3375 if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
3376 adjust_flags |= NFS_INO_INVALID_OTHER;
3379 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label),
3380 inode, adjust_flags);
3382 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3384 case -NFS4ERR_OPENMODE:
3385 if (!(sattr->ia_valid & ATTR_SIZE)) {
3386 pr_warn_once("NFSv4: server %s is incorrectly "
3387 "applying open mode checks to "
3388 "a SETATTR that is not "
3389 "changing file size.\n",
3390 server->nfs_client->cl_hostname);
3392 if (state && !(state->state & FMODE_WRITE)) {
3394 if (sattr->ia_valid & ATTR_OPEN)
3399 err = nfs4_handle_exception(server, err, &exception);
3400 } while (exception.retry);
3406 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3408 if (inode == NULL || !nfs_have_layout(inode))
3411 return pnfs_wait_on_layoutreturn(inode, task);
3415 * Update the seqid of an open stateid
3417 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3418 struct nfs4_state *state)
3425 if (!nfs4_valid_open_stateid(state))
3427 seq = read_seqbegin(&state->seqlock);
3428 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3429 nfs4_stateid_copy(dst, &state->open_stateid);
3430 if (read_seqretry(&state->seqlock, seq))
3434 seqid_open = state->open_stateid.seqid;
3435 if (read_seqretry(&state->seqlock, seq))
3438 dst_seqid = be32_to_cpu(dst->seqid);
3439 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3440 dst->seqid = seqid_open;
3446 * Update the seqid of an open stateid after receiving
3447 * NFS4ERR_OLD_STATEID
3449 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3450 struct nfs4_state *state)
3455 int seq, status = -EAGAIN;
3460 if (!nfs4_valid_open_stateid(state))
3462 seq = read_seqbegin(&state->seqlock);
3463 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3464 if (read_seqretry(&state->seqlock, seq))
3469 write_seqlock(&state->seqlock);
3470 seqid_open = state->open_stateid.seqid;
3472 dst_seqid = be32_to_cpu(dst->seqid);
3474 /* Did another OPEN bump the state's seqid? try again: */
3475 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3476 dst->seqid = seqid_open;
3477 write_sequnlock(&state->seqlock);
3482 /* server says we're behind but we haven't seen the update yet */
3483 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3484 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3485 write_sequnlock(&state->seqlock);
3486 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3488 if (fatal_signal_pending(current))
3491 if (schedule_timeout(5*HZ) != 0)
3494 finish_wait(&state->waitq, &wait);
3498 if (status == -EINTR)
3501 /* we slept the whole 5 seconds, we must have lost a seqid */
3502 dst->seqid = cpu_to_be32(dst_seqid + 1);
3510 struct nfs4_closedata {
3511 struct inode *inode;
3512 struct nfs4_state *state;
3513 struct nfs_closeargs arg;
3514 struct nfs_closeres res;
3516 struct nfs4_layoutreturn_args arg;
3517 struct nfs4_layoutreturn_res res;
3518 struct nfs4_xdr_opaque_data ld_private;
3522 struct nfs_fattr fattr;
3523 unsigned long timestamp;
3526 static void nfs4_free_closedata(void *data)
3528 struct nfs4_closedata *calldata = data;
3529 struct nfs4_state_owner *sp = calldata->state->owner;
3530 struct super_block *sb = calldata->state->inode->i_sb;
3532 if (calldata->lr.roc)
3533 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3534 calldata->res.lr_ret);
3535 nfs4_put_open_state(calldata->state);
3536 nfs_free_seqid(calldata->arg.seqid);
3537 nfs4_put_state_owner(sp);
3538 nfs_sb_deactive(sb);
3542 static void nfs4_close_done(struct rpc_task *task, void *data)
3544 struct nfs4_closedata *calldata = data;
3545 struct nfs4_state *state = calldata->state;
3546 struct nfs_server *server = NFS_SERVER(calldata->inode);
3547 nfs4_stateid *res_stateid = NULL;
3548 struct nfs4_exception exception = {
3550 .inode = calldata->inode,
3551 .stateid = &calldata->arg.stateid,
3554 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3556 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3558 /* Handle Layoutreturn errors */
3559 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3560 &calldata->res.lr_ret) == -EAGAIN)
3563 /* hmm. we are done with the inode, and in the process of freeing
3564 * the state_owner. we keep this around to process errors
3566 switch (task->tk_status) {
3568 res_stateid = &calldata->res.stateid;
3569 renew_lease(server, calldata->timestamp);
3571 case -NFS4ERR_ACCESS:
3572 if (calldata->arg.bitmask != NULL) {
3573 calldata->arg.bitmask = NULL;
3574 calldata->res.fattr = NULL;
3579 case -NFS4ERR_OLD_STATEID:
3580 /* Did we race with OPEN? */
3581 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3585 case -NFS4ERR_ADMIN_REVOKED:
3586 case -NFS4ERR_STALE_STATEID:
3587 case -NFS4ERR_EXPIRED:
3588 nfs4_free_revoked_stateid(server,
3589 &calldata->arg.stateid,
3590 task->tk_msg.rpc_cred);
3592 case -NFS4ERR_BAD_STATEID:
3593 if (calldata->arg.fmode == 0)
3597 task->tk_status = nfs4_async_handle_exception(task,
3598 server, task->tk_status, &exception);
3599 if (exception.retry)
3602 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3603 res_stateid, calldata->arg.fmode);
3605 task->tk_status = 0;
3606 nfs_release_seqid(calldata->arg.seqid);
3607 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3608 dprintk("%s: ret = %d\n", __func__, task->tk_status);
3611 task->tk_status = 0;
3612 rpc_restart_call_prepare(task);
3616 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3618 struct nfs4_closedata *calldata = data;
3619 struct nfs4_state *state = calldata->state;
3620 struct inode *inode = calldata->inode;
3621 struct nfs_server *server = NFS_SERVER(inode);
3622 struct pnfs_layout_hdr *lo;
3623 bool is_rdonly, is_wronly, is_rdwr;
3626 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3629 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3630 spin_lock(&state->owner->so_lock);
3631 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3632 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3633 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3634 /* Calculate the change in open mode */
3635 calldata->arg.fmode = 0;
3636 if (state->n_rdwr == 0) {
3637 if (state->n_rdonly == 0)
3638 call_close |= is_rdonly;
3640 calldata->arg.fmode |= FMODE_READ;
3641 if (state->n_wronly == 0)
3642 call_close |= is_wronly;
3644 calldata->arg.fmode |= FMODE_WRITE;
3645 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3646 call_close |= is_rdwr;
3648 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3650 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3651 if (!nfs4_valid_open_stateid(state))
3653 spin_unlock(&state->owner->so_lock);
3656 /* Note: exit _without_ calling nfs4_close_done */
3660 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3661 nfs_release_seqid(calldata->arg.seqid);
3665 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3666 if (lo && !pnfs_layout_is_valid(lo)) {
3667 calldata->arg.lr_args = NULL;
3668 calldata->res.lr_res = NULL;
3671 if (calldata->arg.fmode == 0)
3672 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3674 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3675 /* Close-to-open cache consistency revalidation */
3676 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3677 nfs4_bitmask_set(calldata->arg.bitmask_store,
3678 server->cache_consistency_bitmask,
3680 calldata->arg.bitmask = calldata->arg.bitmask_store;
3682 calldata->arg.bitmask = NULL;
3685 calldata->arg.share_access =
3686 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3687 calldata->arg.fmode, 0);
3689 if (calldata->res.fattr == NULL)
3690 calldata->arg.bitmask = NULL;
3691 else if (calldata->arg.bitmask == NULL)
3692 calldata->res.fattr = NULL;
3693 calldata->timestamp = jiffies;
3694 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3695 &calldata->arg.seq_args,
3696 &calldata->res.seq_res,
3698 nfs_release_seqid(calldata->arg.seqid);
3701 task->tk_action = NULL;
3703 nfs4_sequence_done(task, &calldata->res.seq_res);
3706 static const struct rpc_call_ops nfs4_close_ops = {
3707 .rpc_call_prepare = nfs4_close_prepare,
3708 .rpc_call_done = nfs4_close_done,
3709 .rpc_release = nfs4_free_closedata,
3713 * It is possible for data to be read/written from a mem-mapped file
3714 * after the sys_close call (which hits the vfs layer as a flush).
3715 * This means that we can't safely call nfsv4 close on a file until
3716 * the inode is cleared. This in turn means that we are not good
3717 * NFSv4 citizens - we do not indicate to the server to update the file's
3718 * share state even when we are done with one of the three share
3719 * stateid's in the inode.
3721 * NOTE: Caller must be holding the sp->so_owner semaphore!
3723 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3725 struct nfs_server *server = NFS_SERVER(state->inode);
3726 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3727 struct nfs4_closedata *calldata;
3728 struct nfs4_state_owner *sp = state->owner;
3729 struct rpc_task *task;
3730 struct rpc_message msg = {
3731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3732 .rpc_cred = state->owner->so_cred,
3734 struct rpc_task_setup task_setup_data = {
3735 .rpc_client = server->client,
3736 .rpc_message = &msg,
3737 .callback_ops = &nfs4_close_ops,
3738 .workqueue = nfsiod_workqueue,
3739 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3741 int status = -ENOMEM;
3743 if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
3744 task_setup_data.flags |= RPC_TASK_MOVEABLE;
3746 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3747 &task_setup_data.rpc_client, &msg);
3749 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3750 if (calldata == NULL)
3752 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3753 calldata->inode = state->inode;
3754 calldata->state = state;
3755 calldata->arg.fh = NFS_FH(state->inode);
3756 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3757 goto out_free_calldata;
3758 /* Serialization for the sequence id */
3759 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3760 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3761 if (IS_ERR(calldata->arg.seqid))
3762 goto out_free_calldata;
3763 nfs_fattr_init(&calldata->fattr);
3764 calldata->arg.fmode = 0;
3765 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3766 calldata->res.fattr = &calldata->fattr;
3767 calldata->res.seqid = calldata->arg.seqid;
3768 calldata->res.server = server;
3769 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3770 calldata->lr.roc = pnfs_roc(state->inode,
3771 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3772 if (calldata->lr.roc) {
3773 calldata->arg.lr_args = &calldata->lr.arg;
3774 calldata->res.lr_res = &calldata->lr.res;
3776 nfs_sb_active(calldata->inode->i_sb);
3778 msg.rpc_argp = &calldata->arg;
3779 msg.rpc_resp = &calldata->res;
3780 task_setup_data.callback_data = calldata;
3781 task = rpc_run_task(&task_setup_data);
3783 return PTR_ERR(task);
3786 status = rpc_wait_for_completion_task(task);
3792 nfs4_put_open_state(state);
3793 nfs4_put_state_owner(sp);
3797 static struct inode *
3798 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3799 int open_flags, struct iattr *attr, int *opened)
3801 struct nfs4_state *state;
3802 struct nfs4_label l, *label;
3804 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3806 /* Protect against concurrent sillydeletes */
3807 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3809 nfs4_label_release_security(label);
3812 return ERR_CAST(state);
3813 return state->inode;
3816 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3818 if (ctx->state == NULL)
3821 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3823 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3826 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3827 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3828 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3830 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3832 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3833 struct nfs4_server_caps_arg args = {
3837 struct nfs4_server_caps_res res = {};
3838 struct rpc_message msg = {
3839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3846 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3847 FATTR4_WORD0_FH_EXPIRE_TYPE |
3848 FATTR4_WORD0_LINK_SUPPORT |
3849 FATTR4_WORD0_SYMLINK_SUPPORT |
3850 FATTR4_WORD0_ACLSUPPORT |
3851 FATTR4_WORD0_CASE_INSENSITIVE |
3852 FATTR4_WORD0_CASE_PRESERVING;
3854 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3856 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3858 /* Sanity check the server answers */
3859 switch (minorversion) {
3861 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3862 res.attr_bitmask[2] = 0;
3865 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3868 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3870 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3871 server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3872 NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3873 server->fattr_valid = NFS_ATTR_FATTR_V4;
3874 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3875 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3876 server->caps |= NFS_CAP_ACLS;
3877 if (res.has_links != 0)
3878 server->caps |= NFS_CAP_HARDLINKS;
3879 if (res.has_symlinks != 0)
3880 server->caps |= NFS_CAP_SYMLINKS;
3881 if (res.case_insensitive)
3882 server->caps |= NFS_CAP_CASE_INSENSITIVE;
3883 if (res.case_preserving)
3884 server->caps |= NFS_CAP_CASE_PRESERVING;
3885 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3886 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3887 server->caps |= NFS_CAP_SECURITY_LABEL;
3889 if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS)
3890 server->caps |= NFS_CAP_FS_LOCATIONS;
3891 if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3892 server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3893 if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3894 server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3895 if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3896 server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3897 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3898 server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3899 NFS_ATTR_FATTR_OWNER_NAME);
3900 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3901 server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3902 NFS_ATTR_FATTR_GROUP_NAME);
3903 if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3904 server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3905 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3906 server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3907 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3908 server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3909 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3910 server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
3911 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3912 sizeof(server->attr_bitmask));
3913 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3915 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3916 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3917 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3918 server->cache_consistency_bitmask[2] = 0;
3920 /* Avoid a regression due to buggy server */
3921 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3922 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3923 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3924 sizeof(server->exclcreat_bitmask));
3926 server->acl_bitmask = res.acl_bitmask;
3927 server->fh_expire_type = res.fh_expire_type;
3933 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3935 struct nfs4_exception exception = {
3936 .interruptible = true,
3940 nfs4_server_set_init_caps(server);
3942 err = nfs4_handle_exception(server,
3943 _nfs4_server_capabilities(server, fhandle),
3945 } while (exception.retry);
3949 static void test_fs_location_for_trunking(struct nfs4_fs_location *location,
3950 struct nfs_client *clp,
3951 struct nfs_server *server)
3955 for (i = 0; i < location->nservers; i++) {
3956 struct nfs4_string *srv_loc = &location->servers[i];
3957 struct sockaddr_storage addr;
3959 struct xprt_create xprt_args = {
3963 struct nfs4_add_xprt_data xprtdata = {
3966 struct rpc_add_xprt_test rpcdata = {
3967 .add_xprt_test = clp->cl_mvops->session_trunk,
3970 char *servername = NULL;
3975 addrlen = nfs_parse_server_name(srv_loc->data, srv_loc->len,
3976 &addr, sizeof(addr),
3977 clp->cl_net, server->port);
3980 xprt_args.dstaddr = (struct sockaddr *)&addr;
3981 xprt_args.addrlen = addrlen;
3982 servername = kmalloc(srv_loc->len + 1, GFP_KERNEL);
3985 memcpy(servername, srv_loc->data, srv_loc->len);
3986 servername[srv_loc->len] = '\0';
3987 xprt_args.servername = servername;
3989 xprtdata.cred = nfs4_get_clid_cred(clp);
3990 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
3991 rpc_clnt_setup_test_and_add_xprt,
3994 put_cred(xprtdata.cred);
3999 static int _nfs4_discover_trunking(struct nfs_server *server,
4000 struct nfs_fh *fhandle)
4002 struct nfs4_fs_locations *locations = NULL;
4004 const struct cred *cred;
4005 struct nfs_client *clp = server->nfs_client;
4006 const struct nfs4_state_maintenance_ops *ops =
4007 clp->cl_mvops->state_renewal_ops;
4008 int status = -ENOMEM, i;
4010 cred = ops->get_state_renewal_cred(clp);
4012 cred = nfs4_get_clid_cred(clp);
4017 page = alloc_page(GFP_KERNEL);
4020 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4023 locations->fattr = nfs_alloc_fattr();
4024 if (!locations->fattr)
4027 status = nfs4_proc_get_locations(server, fhandle, locations, page,
4032 for (i = 0; i < locations->nlocations; i++)
4033 test_fs_location_for_trunking(&locations->locations[i], clp,
4036 kfree(locations->fattr);
4046 static int nfs4_discover_trunking(struct nfs_server *server,
4047 struct nfs_fh *fhandle)
4049 struct nfs4_exception exception = {
4050 .interruptible = true,
4052 struct nfs_client *clp = server->nfs_client;
4055 if (!nfs4_has_session(clp))
4058 err = nfs4_handle_exception(server,
4059 _nfs4_discover_trunking(server, fhandle),
4061 } while (exception.retry);
4066 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4067 struct nfs_fsinfo *info)
4070 struct nfs4_lookup_root_arg args = {
4073 struct nfs4_lookup_res res = {
4075 .fattr = info->fattr,
4078 struct rpc_message msg = {
4079 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
4084 bitmask[0] = nfs4_fattr_bitmap[0];
4085 bitmask[1] = nfs4_fattr_bitmap[1];
4087 * Process the label in the upcoming getfattr
4089 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
4091 nfs_fattr_init(info->fattr);
4092 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4095 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4096 struct nfs_fsinfo *info)
4098 struct nfs4_exception exception = {
4099 .interruptible = true,
4103 err = _nfs4_lookup_root(server, fhandle, info);
4104 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
4107 case -NFS4ERR_WRONGSEC:
4110 err = nfs4_handle_exception(server, err, &exception);
4112 } while (exception.retry);
4117 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4118 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4120 struct rpc_auth_create_args auth_args = {
4121 .pseudoflavor = flavor,
4123 struct rpc_auth *auth;
4125 auth = rpcauth_create(&auth_args, server->client);
4128 return nfs4_lookup_root(server, fhandle, info);
4132 * Retry pseudoroot lookup with various security flavors. We do this when:
4134 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4135 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4137 * Returns zero on success, or a negative NFS4ERR value, or a
4138 * negative errno value.
4140 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4141 struct nfs_fsinfo *info)
4143 /* Per 3530bis 15.33.5 */
4144 static const rpc_authflavor_t flav_array[] = {
4148 RPC_AUTH_UNIX, /* courtesy */
4151 int status = -EPERM;
4154 if (server->auth_info.flavor_len > 0) {
4155 /* try each flavor specified by user */
4156 for (i = 0; i < server->auth_info.flavor_len; i++) {
4157 status = nfs4_lookup_root_sec(server, fhandle, info,
4158 server->auth_info.flavors[i]);
4159 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4164 /* no flavors specified by user, try default list */
4165 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4166 status = nfs4_lookup_root_sec(server, fhandle, info,
4168 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4175 * -EACCES could mean that the user doesn't have correct permissions
4176 * to access the mount. It could also mean that we tried to mount
4177 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4178 * existing mount programs don't handle -EACCES very well so it should
4179 * be mapped to -EPERM instead.
4181 if (status == -EACCES)
4187 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4188 * @server: initialized nfs_server handle
4189 * @fhandle: we fill in the pseudo-fs root file handle
4190 * @info: we fill in an FSINFO struct
4191 * @auth_probe: probe the auth flavours
4193 * Returns zero on success, or a negative errno.
4195 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4196 struct nfs_fsinfo *info,
4202 status = nfs4_lookup_root(server, fhandle, info);
4204 if (auth_probe || status == NFS4ERR_WRONGSEC)
4205 status = server->nfs_client->cl_mvops->find_root_sec(server,
4209 status = nfs4_server_capabilities(server, fhandle);
4211 status = nfs4_do_fsinfo(server, fhandle, info);
4213 return nfs4_map_errors(status);
4216 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4217 struct nfs_fsinfo *info)
4220 struct nfs_fattr *fattr = info->fattr;
4222 error = nfs4_server_capabilities(server, mntfh);
4224 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4228 error = nfs4_proc_getattr(server, mntfh, fattr, NULL);
4230 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4234 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4235 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4236 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4243 * Get locations and (maybe) other attributes of a referral.
4244 * Note that we'll actually follow the referral later when
4245 * we detect fsid mismatch in inode revalidation
4247 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4248 const struct qstr *name, struct nfs_fattr *fattr,
4249 struct nfs_fh *fhandle)
4251 int status = -ENOMEM;
4252 struct page *page = NULL;
4253 struct nfs4_fs_locations *locations = NULL;
4255 page = alloc_page(GFP_KERNEL);
4258 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4259 if (locations == NULL)
4262 locations->fattr = fattr;
4264 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4269 * If the fsid didn't change, this is a migration event, not a
4270 * referral. Cause us to drop into the exception handler, which
4271 * will kick off migration recovery.
4273 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &fattr->fsid)) {
4274 dprintk("%s: server did not return a different fsid for"
4275 " a referral at %s\n", __func__, name->name);
4276 status = -NFS4ERR_MOVED;
4279 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4280 nfs_fixup_referral_attributes(fattr);
4281 memset(fhandle, 0, sizeof(struct nfs_fh));
4289 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4290 struct nfs_fattr *fattr, struct inode *inode)
4292 __u32 bitmask[NFS4_BITMASK_SZ];
4293 struct nfs4_getattr_arg args = {
4297 struct nfs4_getattr_res res = {
4301 struct rpc_message msg = {
4302 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4306 unsigned short task_flags = 0;
4308 if (nfs4_has_session(server->nfs_client))
4309 task_flags = RPC_TASK_MOVEABLE;
4311 /* Is this is an attribute revalidation, subject to softreval? */
4312 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4313 task_flags |= RPC_TASK_TIMEOUT;
4315 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0);
4316 nfs_fattr_init(fattr);
4317 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4318 return nfs4_do_call_sync(server->client, server, &msg,
4319 &args.seq_args, &res.seq_res, task_flags);
4322 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4323 struct nfs_fattr *fattr, struct inode *inode)
4325 struct nfs4_exception exception = {
4326 .interruptible = true,
4330 err = _nfs4_proc_getattr(server, fhandle, fattr, inode);
4331 trace_nfs4_getattr(server, fhandle, fattr, err);
4332 err = nfs4_handle_exception(server, err,
4334 } while (exception.retry);
4339 * The file is not closed if it is opened due to the a request to change
4340 * the size of the file. The open call will not be needed once the
4341 * VFS layer lookup-intents are implemented.
4343 * Close is called when the inode is destroyed.
4344 * If we haven't opened the file for O_WRONLY, we
4345 * need to in the size_change case to obtain a stateid.
4348 * Because OPEN is always done by name in nfsv4, it is
4349 * possible that we opened a different file by the same
4350 * name. We can recognize this race condition, but we
4351 * can't do anything about it besides returning an error.
4353 * This will be fixed with VFS changes (lookup-intent).
4356 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4357 struct iattr *sattr)
4359 struct inode *inode = d_inode(dentry);
4360 const struct cred *cred = NULL;
4361 struct nfs_open_context *ctx = NULL;
4364 if (pnfs_ld_layoutret_on_setattr(inode) &&
4365 sattr->ia_valid & ATTR_SIZE &&
4366 sattr->ia_size < i_size_read(inode))
4367 pnfs_commit_and_return_layout(inode);
4369 nfs_fattr_init(fattr);
4371 /* Deal with open(O_TRUNC) */
4372 if (sattr->ia_valid & ATTR_OPEN)
4373 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4375 /* Optimization: if the end result is no change, don't RPC */
4376 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4379 /* Search for an existing open(O_WRITE) file */
4380 if (sattr->ia_valid & ATTR_FILE) {
4382 ctx = nfs_file_open_context(sattr->ia_file);
4387 /* Return any delegations if we're going to change ACLs */
4388 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4389 nfs4_inode_make_writeable(inode);
4391 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL);
4393 nfs_setattr_update_inode(inode, sattr, fattr);
4394 nfs_setsecurity(inode, fattr);
4399 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4400 struct dentry *dentry, struct nfs_fh *fhandle,
4401 struct nfs_fattr *fattr)
4403 struct nfs_server *server = NFS_SERVER(dir);
4405 struct nfs4_lookup_arg args = {
4406 .bitmask = server->attr_bitmask,
4407 .dir_fh = NFS_FH(dir),
4408 .name = &dentry->d_name,
4410 struct nfs4_lookup_res res = {
4415 struct rpc_message msg = {
4416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4420 unsigned short task_flags = 0;
4422 if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
4423 task_flags = RPC_TASK_MOVEABLE;
4425 /* Is this is an attribute revalidation, subject to softreval? */
4426 if (nfs_lookup_is_soft_revalidate(dentry))
4427 task_flags |= RPC_TASK_TIMEOUT;
4429 args.bitmask = nfs4_bitmask(server, fattr->label);
4431 nfs_fattr_init(fattr);
4433 dprintk("NFS call lookup %pd2\n", dentry);
4434 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4435 status = nfs4_do_call_sync(clnt, server, &msg,
4436 &args.seq_args, &res.seq_res, task_flags);
4437 dprintk("NFS reply lookup: %d\n", status);
4441 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4443 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4444 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4445 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4449 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4450 struct dentry *dentry, struct nfs_fh *fhandle,
4451 struct nfs_fattr *fattr)
4453 struct nfs4_exception exception = {
4454 .interruptible = true,
4456 struct rpc_clnt *client = *clnt;
4457 const struct qstr *name = &dentry->d_name;
4460 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr);
4461 trace_nfs4_lookup(dir, name, err);
4463 case -NFS4ERR_BADNAME:
4466 case -NFS4ERR_MOVED:
4467 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4468 if (err == -NFS4ERR_MOVED)
4469 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4471 case -NFS4ERR_WRONGSEC:
4473 if (client != *clnt)
4475 client = nfs4_negotiate_security(client, dir, name);
4477 return PTR_ERR(client);
4479 exception.retry = 1;
4482 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4484 } while (exception.retry);
4489 else if (client != *clnt)
4490 rpc_shutdown_client(client);
4495 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4496 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4499 struct rpc_clnt *client = NFS_CLIENT(dir);
4501 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4502 if (client != NFS_CLIENT(dir)) {
4503 rpc_shutdown_client(client);
4504 nfs_fixup_secinfo_attributes(fattr);
4510 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4511 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4513 struct rpc_clnt *client = NFS_CLIENT(dir);
4516 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4518 return ERR_PTR(status);
4519 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4522 static int _nfs4_proc_lookupp(struct inode *inode,
4523 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4525 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4526 struct nfs_server *server = NFS_SERVER(inode);
4528 struct nfs4_lookupp_arg args = {
4529 .bitmask = server->attr_bitmask,
4530 .fh = NFS_FH(inode),
4532 struct nfs4_lookupp_res res = {
4537 struct rpc_message msg = {
4538 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4542 unsigned short task_flags = 0;
4544 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4545 task_flags |= RPC_TASK_TIMEOUT;
4547 args.bitmask = nfs4_bitmask(server, fattr->label);
4549 nfs_fattr_init(fattr);
4551 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4552 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4553 &res.seq_res, task_flags);
4554 dprintk("NFS reply lookupp: %d\n", status);
4558 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4559 struct nfs_fattr *fattr)
4561 struct nfs4_exception exception = {
4562 .interruptible = true,
4566 err = _nfs4_proc_lookupp(inode, fhandle, fattr);
4567 trace_nfs4_lookupp(inode, err);
4568 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4570 } while (exception.retry);
4574 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4575 const struct cred *cred)
4577 struct nfs_server *server = NFS_SERVER(inode);
4578 struct nfs4_accessargs args = {
4579 .fh = NFS_FH(inode),
4580 .access = entry->mask,
4582 struct nfs4_accessres res = {
4585 struct rpc_message msg = {
4586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4593 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4594 res.fattr = nfs_alloc_fattr();
4595 if (res.fattr == NULL)
4597 args.bitmask = server->cache_consistency_bitmask;
4599 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4601 nfs_access_set_mask(entry, res.access);
4603 nfs_refresh_inode(inode, res.fattr);
4605 nfs_free_fattr(res.fattr);
4609 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4610 const struct cred *cred)
4612 struct nfs4_exception exception = {
4613 .interruptible = true,
4617 err = _nfs4_proc_access(inode, entry, cred);
4618 trace_nfs4_access(inode, err);
4619 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4621 } while (exception.retry);
4626 * TODO: For the time being, we don't try to get any attributes
4627 * along with any of the zero-copy operations READ, READDIR,
4630 * In the case of the first three, we want to put the GETATTR
4631 * after the read-type operation -- this is because it is hard
4632 * to predict the length of a GETATTR response in v4, and thus
4633 * align the READ data correctly. This means that the GETATTR
4634 * may end up partially falling into the page cache, and we should
4635 * shift it into the 'tail' of the xdr_buf before processing.
4636 * To do this efficiently, we need to know the total length
4637 * of data received, which doesn't seem to be available outside
4640 * In the case of WRITE, we also want to put the GETATTR after
4641 * the operation -- in this case because we want to make sure
4642 * we get the post-operation mtime and size.
4644 * Both of these changes to the XDR layer would in fact be quite
4645 * minor, but I decided to leave them for a subsequent patch.
4647 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4648 unsigned int pgbase, unsigned int pglen)
4650 struct nfs4_readlink args = {
4651 .fh = NFS_FH(inode),
4656 struct nfs4_readlink_res res;
4657 struct rpc_message msg = {
4658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4663 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4666 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4667 unsigned int pgbase, unsigned int pglen)
4669 struct nfs4_exception exception = {
4670 .interruptible = true,
4674 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4675 trace_nfs4_readlink(inode, err);
4676 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4678 } while (exception.retry);
4683 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4686 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4689 struct nfs_server *server = NFS_SERVER(dir);
4690 struct nfs4_label l, *ilabel;
4691 struct nfs_open_context *ctx;
4692 struct nfs4_state *state;
4695 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4697 return PTR_ERR(ctx);
4699 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4701 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4702 sattr->ia_mode &= ~current_umask();
4703 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4704 if (IS_ERR(state)) {
4705 status = PTR_ERR(state);
4709 nfs4_label_release_security(ilabel);
4710 put_nfs_open_context(ctx);
4715 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4717 struct nfs_server *server = NFS_SERVER(dir);
4718 struct nfs_removeargs args = {
4722 struct nfs_removeres res = {
4725 struct rpc_message msg = {
4726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4730 unsigned long timestamp = jiffies;
4733 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4735 spin_lock(&dir->i_lock);
4736 /* Removing a directory decrements nlink in the parent */
4737 if (ftype == NF4DIR && dir->i_nlink > 2)
4738 nfs4_dec_nlink_locked(dir);
4739 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4740 NFS_INO_INVALID_DATA);
4741 spin_unlock(&dir->i_lock);
4746 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4748 struct nfs4_exception exception = {
4749 .interruptible = true,
4751 struct inode *inode = d_inode(dentry);
4755 if (inode->i_nlink == 1)
4756 nfs4_inode_return_delegation(inode);
4758 nfs4_inode_make_writeable(inode);
4761 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4762 trace_nfs4_remove(dir, &dentry->d_name, err);
4763 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4765 } while (exception.retry);
4769 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4771 struct nfs4_exception exception = {
4772 .interruptible = true,
4777 err = _nfs4_proc_remove(dir, name, NF4DIR);
4778 trace_nfs4_remove(dir, name, err);
4779 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4781 } while (exception.retry);
4785 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4786 struct dentry *dentry,
4787 struct inode *inode)
4789 struct nfs_removeargs *args = msg->rpc_argp;
4790 struct nfs_removeres *res = msg->rpc_resp;
4792 res->server = NFS_SB(dentry->d_sb);
4793 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4794 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4796 nfs_fattr_init(res->dir_attr);
4799 nfs4_inode_return_delegation(inode);
4800 nfs_d_prune_case_insensitive_aliases(inode);
4804 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4806 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4807 &data->args.seq_args,
4812 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4814 struct nfs_unlinkdata *data = task->tk_calldata;
4815 struct nfs_removeres *res = &data->res;
4817 if (!nfs4_sequence_done(task, &res->seq_res))
4819 if (nfs4_async_handle_error(task, res->server, NULL,
4820 &data->timeout) == -EAGAIN)
4822 if (task->tk_status == 0)
4823 nfs4_update_changeattr(dir, &res->cinfo,
4824 res->dir_attr->time_start,
4825 NFS_INO_INVALID_DATA);
4829 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4830 struct dentry *old_dentry,
4831 struct dentry *new_dentry)
4833 struct nfs_renameargs *arg = msg->rpc_argp;
4834 struct nfs_renameres *res = msg->rpc_resp;
4835 struct inode *old_inode = d_inode(old_dentry);
4836 struct inode *new_inode = d_inode(new_dentry);
4839 nfs4_inode_make_writeable(old_inode);
4841 nfs4_inode_return_delegation(new_inode);
4842 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4843 res->server = NFS_SB(old_dentry->d_sb);
4844 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4847 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4849 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4850 &data->args.seq_args,
4855 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4856 struct inode *new_dir)
4858 struct nfs_renamedata *data = task->tk_calldata;
4859 struct nfs_renameres *res = &data->res;
4861 if (!nfs4_sequence_done(task, &res->seq_res))
4863 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4866 if (task->tk_status == 0) {
4867 nfs_d_prune_case_insensitive_aliases(d_inode(data->old_dentry));
4868 if (new_dir != old_dir) {
4869 /* Note: If we moved a directory, nlink will change */
4870 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4871 res->old_fattr->time_start,
4872 NFS_INO_INVALID_NLINK |
4873 NFS_INO_INVALID_DATA);
4874 nfs4_update_changeattr(new_dir, &res->new_cinfo,
4875 res->new_fattr->time_start,
4876 NFS_INO_INVALID_NLINK |
4877 NFS_INO_INVALID_DATA);
4879 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4880 res->old_fattr->time_start,
4881 NFS_INO_INVALID_DATA);
4886 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4888 struct nfs_server *server = NFS_SERVER(inode);
4889 __u32 bitmask[NFS4_BITMASK_SZ];
4890 struct nfs4_link_arg arg = {
4891 .fh = NFS_FH(inode),
4892 .dir_fh = NFS_FH(dir),
4896 struct nfs4_link_res res = {
4899 struct rpc_message msg = {
4900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4904 int status = -ENOMEM;
4906 res.fattr = nfs_alloc_fattr_with_label(server);
4907 if (res.fattr == NULL)
4910 nfs4_inode_make_writeable(inode);
4911 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label), inode,
4912 NFS_INO_INVALID_CHANGE);
4913 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4915 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4916 NFS_INO_INVALID_DATA);
4917 nfs4_inc_nlink(inode);
4918 status = nfs_post_op_update_inode(inode, res.fattr);
4920 nfs_setsecurity(inode, res.fattr);
4924 nfs_free_fattr(res.fattr);
4928 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4930 struct nfs4_exception exception = {
4931 .interruptible = true,
4935 err = nfs4_handle_exception(NFS_SERVER(inode),
4936 _nfs4_proc_link(inode, dir, name),
4938 } while (exception.retry);
4942 struct nfs4_createdata {
4943 struct rpc_message msg;
4944 struct nfs4_create_arg arg;
4945 struct nfs4_create_res res;
4947 struct nfs_fattr fattr;
4950 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4951 const struct qstr *name, struct iattr *sattr, u32 ftype)
4953 struct nfs4_createdata *data;
4955 data = kzalloc(sizeof(*data), GFP_KERNEL);
4957 struct nfs_server *server = NFS_SERVER(dir);
4959 data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL);
4960 if (IS_ERR(data->fattr.label))
4963 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4964 data->msg.rpc_argp = &data->arg;
4965 data->msg.rpc_resp = &data->res;
4966 data->arg.dir_fh = NFS_FH(dir);
4967 data->arg.server = server;
4968 data->arg.name = name;
4969 data->arg.attrs = sattr;
4970 data->arg.ftype = ftype;
4971 data->arg.bitmask = nfs4_bitmask(server, data->fattr.label);
4972 data->arg.umask = current_umask();
4973 data->res.server = server;
4974 data->res.fh = &data->fh;
4975 data->res.fattr = &data->fattr;
4976 nfs_fattr_init(data->res.fattr);
4984 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4986 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4987 &data->arg.seq_args, &data->res.seq_res, 1);
4989 spin_lock(&dir->i_lock);
4990 /* Creating a directory bumps nlink in the parent */
4991 if (data->arg.ftype == NF4DIR)
4992 nfs4_inc_nlink_locked(dir);
4993 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4994 data->res.fattr->time_start,
4995 NFS_INO_INVALID_DATA);
4996 spin_unlock(&dir->i_lock);
4997 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
5002 static void nfs4_free_createdata(struct nfs4_createdata *data)
5004 nfs4_label_free(data->fattr.label);
5008 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5009 struct page *page, unsigned int len, struct iattr *sattr,
5010 struct nfs4_label *label)
5012 struct nfs4_createdata *data;
5013 int status = -ENAMETOOLONG;
5015 if (len > NFS4_MAXPATHLEN)
5019 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
5023 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
5024 data->arg.u.symlink.pages = &page;
5025 data->arg.u.symlink.len = len;
5026 data->arg.label = label;
5028 status = nfs4_do_create(dir, dentry, data);
5030 nfs4_free_createdata(data);
5035 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5036 struct page *page, unsigned int len, struct iattr *sattr)
5038 struct nfs4_exception exception = {
5039 .interruptible = true,
5041 struct nfs4_label l, *label;
5044 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5047 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
5048 trace_nfs4_symlink(dir, &dentry->d_name, err);
5049 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5051 } while (exception.retry);
5053 nfs4_label_release_security(label);
5057 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5058 struct iattr *sattr, struct nfs4_label *label)
5060 struct nfs4_createdata *data;
5061 int status = -ENOMEM;
5063 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
5067 data->arg.label = label;
5068 status = nfs4_do_create(dir, dentry, data);
5070 nfs4_free_createdata(data);
5075 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5076 struct iattr *sattr)
5078 struct nfs_server *server = NFS_SERVER(dir);
5079 struct nfs4_exception exception = {
5080 .interruptible = true,
5082 struct nfs4_label l, *label;
5085 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5087 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5088 sattr->ia_mode &= ~current_umask();
5090 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
5091 trace_nfs4_mkdir(dir, &dentry->d_name, err);
5092 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5094 } while (exception.retry);
5095 nfs4_label_release_security(label);
5100 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5101 struct nfs_readdir_res *nr_res)
5103 struct inode *dir = d_inode(nr_arg->dentry);
5104 struct nfs_server *server = NFS_SERVER(dir);
5105 struct nfs4_readdir_arg args = {
5107 .pages = nr_arg->pages,
5109 .count = nr_arg->page_len,
5110 .plus = nr_arg->plus,
5112 struct nfs4_readdir_res res;
5113 struct rpc_message msg = {
5114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5117 .rpc_cred = nr_arg->cred,
5121 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5122 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5123 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5124 args.bitmask = server->attr_bitmask_nl;
5126 args.bitmask = server->attr_bitmask;
5128 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5129 res.pgbase = args.pgbase;
5130 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5133 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5134 status += args.pgbase;
5137 nfs_invalidate_atime(dir);
5139 dprintk("%s: returns %d\n", __func__, status);
5143 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5144 struct nfs_readdir_res *res)
5146 struct nfs4_exception exception = {
5147 .interruptible = true,
5151 err = _nfs4_proc_readdir(arg, res);
5152 trace_nfs4_readdir(d_inode(arg->dentry), err);
5153 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5155 } while (exception.retry);
5159 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5160 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5162 struct nfs4_createdata *data;
5163 int mode = sattr->ia_mode;
5164 int status = -ENOMEM;
5166 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5171 data->arg.ftype = NF4FIFO;
5172 else if (S_ISBLK(mode)) {
5173 data->arg.ftype = NF4BLK;
5174 data->arg.u.device.specdata1 = MAJOR(rdev);
5175 data->arg.u.device.specdata2 = MINOR(rdev);
5177 else if (S_ISCHR(mode)) {
5178 data->arg.ftype = NF4CHR;
5179 data->arg.u.device.specdata1 = MAJOR(rdev);
5180 data->arg.u.device.specdata2 = MINOR(rdev);
5181 } else if (!S_ISSOCK(mode)) {
5186 data->arg.label = label;
5187 status = nfs4_do_create(dir, dentry, data);
5189 nfs4_free_createdata(data);
5194 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5195 struct iattr *sattr, dev_t rdev)
5197 struct nfs_server *server = NFS_SERVER(dir);
5198 struct nfs4_exception exception = {
5199 .interruptible = true,
5201 struct nfs4_label l, *label;
5204 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5206 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5207 sattr->ia_mode &= ~current_umask();
5209 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5210 trace_nfs4_mknod(dir, &dentry->d_name, err);
5211 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5213 } while (exception.retry);
5215 nfs4_label_release_security(label);
5220 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5221 struct nfs_fsstat *fsstat)
5223 struct nfs4_statfs_arg args = {
5225 .bitmask = server->attr_bitmask,
5227 struct nfs4_statfs_res res = {
5230 struct rpc_message msg = {
5231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5236 nfs_fattr_init(fsstat->fattr);
5237 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5240 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5242 struct nfs4_exception exception = {
5243 .interruptible = true,
5247 err = nfs4_handle_exception(server,
5248 _nfs4_proc_statfs(server, fhandle, fsstat),
5250 } while (exception.retry);
5254 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5255 struct nfs_fsinfo *fsinfo)
5257 struct nfs4_fsinfo_arg args = {
5259 .bitmask = server->attr_bitmask,
5261 struct nfs4_fsinfo_res res = {
5264 struct rpc_message msg = {
5265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5270 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5273 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5275 struct nfs4_exception exception = {
5276 .interruptible = true,
5281 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5282 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5284 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5287 err = nfs4_handle_exception(server, err, &exception);
5288 } while (exception.retry);
5292 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5296 nfs_fattr_init(fsinfo->fattr);
5297 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5299 /* block layout checks this! */
5300 server->pnfs_blksize = fsinfo->blksize;
5301 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5307 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5308 struct nfs_pathconf *pathconf)
5310 struct nfs4_pathconf_arg args = {
5312 .bitmask = server->attr_bitmask,
5314 struct nfs4_pathconf_res res = {
5315 .pathconf = pathconf,
5317 struct rpc_message msg = {
5318 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5323 /* None of the pathconf attributes are mandatory to implement */
5324 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5325 memset(pathconf, 0, sizeof(*pathconf));
5329 nfs_fattr_init(pathconf->fattr);
5330 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5333 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5334 struct nfs_pathconf *pathconf)
5336 struct nfs4_exception exception = {
5337 .interruptible = true,
5342 err = nfs4_handle_exception(server,
5343 _nfs4_proc_pathconf(server, fhandle, pathconf),
5345 } while (exception.retry);
5349 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5350 const struct nfs_open_context *ctx,
5351 const struct nfs_lock_context *l_ctx,
5354 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5356 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5358 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5359 const struct nfs_open_context *ctx,
5360 const struct nfs_lock_context *l_ctx,
5363 nfs4_stateid _current_stateid;
5365 /* If the current stateid represents a lost lock, then exit */
5366 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5368 return nfs4_stateid_match(stateid, &_current_stateid);
5371 static bool nfs4_error_stateid_expired(int err)
5374 case -NFS4ERR_DELEG_REVOKED:
5375 case -NFS4ERR_ADMIN_REVOKED:
5376 case -NFS4ERR_BAD_STATEID:
5377 case -NFS4ERR_STALE_STATEID:
5378 case -NFS4ERR_OLD_STATEID:
5379 case -NFS4ERR_OPENMODE:
5380 case -NFS4ERR_EXPIRED:
5386 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5388 struct nfs_server *server = NFS_SERVER(hdr->inode);
5390 trace_nfs4_read(hdr, task->tk_status);
5391 if (task->tk_status < 0) {
5392 struct nfs4_exception exception = {
5393 .inode = hdr->inode,
5394 .state = hdr->args.context->state,
5395 .stateid = &hdr->args.stateid,
5397 task->tk_status = nfs4_async_handle_exception(task,
5398 server, task->tk_status, &exception);
5399 if (exception.retry) {
5400 rpc_restart_call_prepare(task);
5405 if (task->tk_status > 0)
5406 renew_lease(server, hdr->timestamp);
5410 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5411 struct nfs_pgio_args *args)
5414 if (!nfs4_error_stateid_expired(task->tk_status) ||
5415 nfs4_stateid_is_current(&args->stateid,
5420 rpc_restart_call_prepare(task);
5424 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5425 struct nfs_pgio_header *hdr)
5427 struct nfs_server *server = NFS_SERVER(hdr->inode);
5428 struct rpc_message *msg = &task->tk_msg;
5430 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5431 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5432 server->caps &= ~NFS_CAP_READ_PLUS;
5433 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5434 rpc_restart_call_prepare(task);
5440 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5442 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5444 if (nfs4_read_stateid_changed(task, &hdr->args))
5446 if (nfs4_read_plus_not_supported(task, hdr))
5448 if (task->tk_status > 0)
5449 nfs_invalidate_atime(hdr->inode);
5450 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5451 nfs4_read_done_cb(task, hdr);
5454 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5455 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5456 struct rpc_message *msg)
5458 /* Note: We don't use READ_PLUS with pNFS yet */
5459 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
5460 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5463 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5464 struct rpc_message *msg)
5467 #endif /* CONFIG_NFS_V4_2 */
5469 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5470 struct rpc_message *msg)
5472 hdr->timestamp = jiffies;
5473 if (!hdr->pgio_done_cb)
5474 hdr->pgio_done_cb = nfs4_read_done_cb;
5475 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5476 nfs42_read_plus_support(hdr, msg);
5477 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5480 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5481 struct nfs_pgio_header *hdr)
5483 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5484 &hdr->args.seq_args,
5488 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5489 hdr->args.lock_context,
5490 hdr->rw_mode) == -EIO)
5492 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5497 static int nfs4_write_done_cb(struct rpc_task *task,
5498 struct nfs_pgio_header *hdr)
5500 struct inode *inode = hdr->inode;
5502 trace_nfs4_write(hdr, task->tk_status);
5503 if (task->tk_status < 0) {
5504 struct nfs4_exception exception = {
5505 .inode = hdr->inode,
5506 .state = hdr->args.context->state,
5507 .stateid = &hdr->args.stateid,
5509 task->tk_status = nfs4_async_handle_exception(task,
5510 NFS_SERVER(inode), task->tk_status,
5512 if (exception.retry) {
5513 rpc_restart_call_prepare(task);
5517 if (task->tk_status >= 0) {
5518 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5519 nfs_writeback_update_inode(hdr);
5524 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5525 struct nfs_pgio_args *args)
5528 if (!nfs4_error_stateid_expired(task->tk_status) ||
5529 nfs4_stateid_is_current(&args->stateid,
5534 rpc_restart_call_prepare(task);
5538 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5540 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5542 if (nfs4_write_stateid_changed(task, &hdr->args))
5544 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5545 nfs4_write_done_cb(task, hdr);
5549 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5551 /* Don't request attributes for pNFS or O_DIRECT writes */
5552 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5554 /* Otherwise, request attributes if and only if we don't hold
5557 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5560 void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[],
5561 struct inode *inode, unsigned long cache_validity)
5563 struct nfs_server *server = NFS_SERVER(inode);
5566 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5567 cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity);
5569 if (cache_validity & NFS_INO_INVALID_CHANGE)
5570 bitmask[0] |= FATTR4_WORD0_CHANGE;
5571 if (cache_validity & NFS_INO_INVALID_ATIME)
5572 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5573 if (cache_validity & NFS_INO_INVALID_MODE)
5574 bitmask[1] |= FATTR4_WORD1_MODE;
5575 if (cache_validity & NFS_INO_INVALID_OTHER)
5576 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5577 if (cache_validity & NFS_INO_INVALID_NLINK)
5578 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5579 if (cache_validity & NFS_INO_INVALID_CTIME)
5580 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5581 if (cache_validity & NFS_INO_INVALID_MTIME)
5582 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5583 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5584 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5586 if (cache_validity & NFS_INO_INVALID_SIZE)
5587 bitmask[0] |= FATTR4_WORD0_SIZE;
5589 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5590 bitmask[i] &= server->attr_bitmask[i];
5593 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5594 struct rpc_message *msg,
5595 struct rpc_clnt **clnt)
5597 struct nfs_server *server = NFS_SERVER(hdr->inode);
5599 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5600 hdr->args.bitmask = NULL;
5601 hdr->res.fattr = NULL;
5603 nfs4_bitmask_set(hdr->args.bitmask_store,
5604 server->cache_consistency_bitmask,
5605 hdr->inode, NFS_INO_INVALID_BLOCKS);
5606 hdr->args.bitmask = hdr->args.bitmask_store;
5609 if (!hdr->pgio_done_cb)
5610 hdr->pgio_done_cb = nfs4_write_done_cb;
5611 hdr->res.server = server;
5612 hdr->timestamp = jiffies;
5614 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5615 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5616 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5619 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5621 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5622 &data->args.seq_args,
5627 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5629 struct inode *inode = data->inode;
5631 trace_nfs4_commit(data, task->tk_status);
5632 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5633 NULL, NULL) == -EAGAIN) {
5634 rpc_restart_call_prepare(task);
5640 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5642 if (!nfs4_sequence_done(task, &data->res.seq_res))
5644 return data->commit_done_cb(task, data);
5647 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5648 struct rpc_clnt **clnt)
5650 struct nfs_server *server = NFS_SERVER(data->inode);
5652 if (data->commit_done_cb == NULL)
5653 data->commit_done_cb = nfs4_commit_done_cb;
5654 data->res.server = server;
5655 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5656 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5657 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5660 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5661 struct nfs_commitres *res)
5663 struct inode *dst_inode = file_inode(dst);
5664 struct nfs_server *server = NFS_SERVER(dst_inode);
5665 struct rpc_message msg = {
5666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5671 args->fh = NFS_FH(dst_inode);
5672 return nfs4_call_sync(server->client, server, &msg,
5673 &args->seq_args, &res->seq_res, 1);
5676 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5678 struct nfs_commitargs args = {
5682 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5683 struct nfs4_exception exception = { };
5687 status = _nfs4_proc_commit(dst, &args, res);
5688 status = nfs4_handle_exception(dst_server, status, &exception);
5689 } while (exception.retry);
5694 struct nfs4_renewdata {
5695 struct nfs_client *client;
5696 unsigned long timestamp;
5700 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5701 * standalone procedure for queueing an asynchronous RENEW.
5703 static void nfs4_renew_release(void *calldata)
5705 struct nfs4_renewdata *data = calldata;
5706 struct nfs_client *clp = data->client;
5708 if (refcount_read(&clp->cl_count) > 1)
5709 nfs4_schedule_state_renewal(clp);
5710 nfs_put_client(clp);
5714 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5716 struct nfs4_renewdata *data = calldata;
5717 struct nfs_client *clp = data->client;
5718 unsigned long timestamp = data->timestamp;
5720 trace_nfs4_renew_async(clp, task->tk_status);
5721 switch (task->tk_status) {
5724 case -NFS4ERR_LEASE_MOVED:
5725 nfs4_schedule_lease_moved_recovery(clp);
5728 /* Unless we're shutting down, schedule state recovery! */
5729 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5731 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5732 nfs4_schedule_lease_recovery(clp);
5735 nfs4_schedule_path_down_recovery(clp);
5737 do_renew_lease(clp, timestamp);
5740 static const struct rpc_call_ops nfs4_renew_ops = {
5741 .rpc_call_done = nfs4_renew_done,
5742 .rpc_release = nfs4_renew_release,
5745 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5747 struct rpc_message msg = {
5748 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5752 struct nfs4_renewdata *data;
5754 if (renew_flags == 0)
5756 if (!refcount_inc_not_zero(&clp->cl_count))
5758 data = kmalloc(sizeof(*data), GFP_NOFS);
5760 nfs_put_client(clp);
5764 data->timestamp = jiffies;
5765 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5766 &nfs4_renew_ops, data);
5769 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5771 struct rpc_message msg = {
5772 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5776 unsigned long now = jiffies;
5779 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5782 do_renew_lease(clp, now);
5786 static bool nfs4_server_supports_acls(const struct nfs_server *server,
5787 enum nfs4_acl_type type)
5791 return server->attr_bitmask[0] & FATTR4_WORD0_ACL;
5793 return server->attr_bitmask[1] & FATTR4_WORD1_DACL;
5795 return server->attr_bitmask[1] & FATTR4_WORD1_SACL;
5799 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5800 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5803 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5805 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5806 struct page **pages)
5808 struct page *newpage, **spages;
5814 len = min_t(size_t, PAGE_SIZE, buflen);
5815 newpage = alloc_page(GFP_KERNEL);
5817 if (newpage == NULL)
5819 memcpy(page_address(newpage), buf, len);
5824 } while (buflen != 0);
5830 __free_page(spages[rc-1]);
5834 struct nfs4_cached_acl {
5835 enum nfs4_acl_type type;
5841 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5843 struct nfs_inode *nfsi = NFS_I(inode);
5845 spin_lock(&inode->i_lock);
5846 kfree(nfsi->nfs4_acl);
5847 nfsi->nfs4_acl = acl;
5848 spin_unlock(&inode->i_lock);
5851 static void nfs4_zap_acl_attr(struct inode *inode)
5853 nfs4_set_cached_acl(inode, NULL);
5856 static ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf,
5857 size_t buflen, enum nfs4_acl_type type)
5859 struct nfs_inode *nfsi = NFS_I(inode);
5860 struct nfs4_cached_acl *acl;
5863 spin_lock(&inode->i_lock);
5864 acl = nfsi->nfs4_acl;
5867 if (acl->type != type)
5869 if (buf == NULL) /* user is just asking for length */
5871 if (acl->cached == 0)
5873 ret = -ERANGE; /* see getxattr(2) man page */
5874 if (acl->len > buflen)
5876 memcpy(buf, acl->data, acl->len);
5880 spin_unlock(&inode->i_lock);
5884 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages,
5885 size_t pgbase, size_t acl_len,
5886 enum nfs4_acl_type type)
5888 struct nfs4_cached_acl *acl;
5889 size_t buflen = sizeof(*acl) + acl_len;
5891 if (buflen <= PAGE_SIZE) {
5892 acl = kmalloc(buflen, GFP_KERNEL);
5896 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5898 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5906 nfs4_set_cached_acl(inode, acl);
5910 * The getxattr API returns the required buffer length when called with a
5911 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5912 * the required buf. On a NULL buf, we send a page of data to the server
5913 * guessing that the ACL request can be serviced by a page. If so, we cache
5914 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5915 * the cache. If not so, we throw away the page, and cache the required
5916 * length. The next getxattr call will then produce another round trip to
5917 * the server, this time with the input buf of the required size.
5919 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf,
5920 size_t buflen, enum nfs4_acl_type type)
5922 struct page **pages;
5923 struct nfs_getaclargs args = {
5924 .fh = NFS_FH(inode),
5928 struct nfs_getaclres res = {
5932 struct rpc_message msg = {
5933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5937 unsigned int npages;
5938 int ret = -ENOMEM, i;
5939 struct nfs_server *server = NFS_SERVER(inode);
5942 buflen = server->rsize;
5944 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5945 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
5949 args.acl_pages = pages;
5951 for (i = 0; i < npages; i++) {
5952 pages[i] = alloc_page(GFP_KERNEL);
5957 /* for decoding across pages */
5958 res.acl_scratch = alloc_page(GFP_KERNEL);
5959 if (!res.acl_scratch)
5962 args.acl_len = npages * PAGE_SIZE;
5964 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5965 __func__, buf, buflen, npages, args.acl_len);
5966 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5967 &msg, &args.seq_args, &res.seq_res, 0);
5971 /* Handle the case where the passed-in buffer is too short */
5972 if (res.acl_flags & NFS4_ACL_TRUNC) {
5973 /* Did the user only issue a request for the acl length? */
5979 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len,
5982 if (res.acl_len > buflen) {
5986 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5991 for (i = 0; i < npages; i++)
5993 __free_page(pages[i]);
5994 if (res.acl_scratch)
5995 __free_page(res.acl_scratch);
6000 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf,
6001 size_t buflen, enum nfs4_acl_type type)
6003 struct nfs4_exception exception = {
6004 .interruptible = true,
6008 ret = __nfs4_get_acl_uncached(inode, buf, buflen, type);
6009 trace_nfs4_get_acl(inode, ret);
6012 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
6013 } while (exception.retry);
6017 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen,
6018 enum nfs4_acl_type type)
6020 struct nfs_server *server = NFS_SERVER(inode);
6023 if (!nfs4_server_supports_acls(server, type))
6025 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
6028 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
6029 nfs_zap_acl_cache(inode);
6030 ret = nfs4_read_cached_acl(inode, buf, buflen, type);
6032 /* -ENOENT is returned if there is no ACL or if there is an ACL
6033 * but no cached acl data, just the acl length */
6035 return nfs4_get_acl_uncached(inode, buf, buflen, type);
6038 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf,
6039 size_t buflen, enum nfs4_acl_type type)
6041 struct nfs_server *server = NFS_SERVER(inode);
6042 struct page *pages[NFS4ACL_MAXPAGES];
6043 struct nfs_setaclargs arg = {
6044 .fh = NFS_FH(inode),
6049 struct nfs_setaclres res;
6050 struct rpc_message msg = {
6051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
6055 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
6058 /* You can't remove system.nfs4_acl: */
6061 if (!nfs4_server_supports_acls(server, type))
6063 if (npages > ARRAY_SIZE(pages))
6065 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
6068 nfs4_inode_make_writeable(inode);
6069 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6072 * Free each page after tx, so the only ref left is
6073 * held by the network stack
6076 put_page(pages[i-1]);
6079 * Acl update can result in inode attribute update.
6080 * so mark the attribute cache invalid.
6082 spin_lock(&inode->i_lock);
6083 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
6084 NFS_INO_INVALID_CTIME |
6085 NFS_INO_REVAL_FORCED);
6086 spin_unlock(&inode->i_lock);
6087 nfs_access_zap_cache(inode);
6088 nfs_zap_acl_cache(inode);
6092 static int nfs4_proc_set_acl(struct inode *inode, const void *buf,
6093 size_t buflen, enum nfs4_acl_type type)
6095 struct nfs4_exception exception = { };
6098 err = __nfs4_proc_set_acl(inode, buf, buflen, type);
6099 trace_nfs4_set_acl(inode, err);
6100 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
6102 * no need to retry since the kernel
6103 * isn't involved in encoding the ACEs.
6108 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6110 } while (exception.retry);
6114 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6115 static int _nfs4_get_security_label(struct inode *inode, void *buf,
6118 struct nfs_server *server = NFS_SERVER(inode);
6119 struct nfs4_label label = {0, 0, buflen, buf};
6121 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6122 struct nfs_fattr fattr = {
6125 struct nfs4_getattr_arg arg = {
6126 .fh = NFS_FH(inode),
6129 struct nfs4_getattr_res res = {
6133 struct rpc_message msg = {
6134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6140 nfs_fattr_init(&fattr);
6142 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6145 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6150 static int nfs4_get_security_label(struct inode *inode, void *buf,
6153 struct nfs4_exception exception = {
6154 .interruptible = true,
6158 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6162 err = _nfs4_get_security_label(inode, buf, buflen);
6163 trace_nfs4_get_security_label(inode, err);
6164 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6166 } while (exception.retry);
6170 static int _nfs4_do_set_security_label(struct inode *inode,
6171 struct nfs4_label *ilabel,
6172 struct nfs_fattr *fattr)
6175 struct iattr sattr = {0};
6176 struct nfs_server *server = NFS_SERVER(inode);
6177 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6178 struct nfs_setattrargs arg = {
6179 .fh = NFS_FH(inode),
6185 struct nfs_setattrres res = {
6189 struct rpc_message msg = {
6190 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6196 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6198 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6200 dprintk("%s failed: %d\n", __func__, status);
6205 static int nfs4_do_set_security_label(struct inode *inode,
6206 struct nfs4_label *ilabel,
6207 struct nfs_fattr *fattr)
6209 struct nfs4_exception exception = { };
6213 err = _nfs4_do_set_security_label(inode, ilabel, fattr);
6214 trace_nfs4_set_security_label(inode, err);
6215 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6217 } while (exception.retry);
6222 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6224 struct nfs4_label ilabel = {0, 0, buflen, (char *)buf };
6225 struct nfs_fattr *fattr;
6228 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6231 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
6235 status = nfs4_do_set_security_label(inode, &ilabel, fattr);
6237 nfs_setsecurity(inode, fattr);
6241 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6244 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6245 nfs4_verifier *bootverf)
6249 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6250 /* An impossible timestamp guarantees this value
6251 * will never match a generated boot time. */
6252 verf[0] = cpu_to_be32(U32_MAX);
6253 verf[1] = cpu_to_be32(U32_MAX);
6255 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6256 u64 ns = ktime_to_ns(nn->boot_time);
6258 verf[0] = cpu_to_be32(ns >> 32);
6259 verf[1] = cpu_to_be32(ns);
6261 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6265 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6267 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6268 struct nfs_netns_client *nn_clp = nn->nfs_client;
6275 id = rcu_dereference(nn_clp->identifier);
6277 strscpy(buf, id, buflen);
6281 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6282 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6288 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6290 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6295 if (clp->cl_owner_id != NULL)
6300 strlen(clp->cl_rpcclient->cl_nodename) +
6302 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6306 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6310 if (len > NFS4_OPAQUE_LIMIT + 1)
6314 * Since this string is allocated at mount time, and held until the
6315 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6316 * about a memory-reclaim deadlock.
6318 str = kmalloc(len, GFP_KERNEL);
6324 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6325 clp->cl_rpcclient->cl_nodename, buf,
6326 rpc_peeraddr2str(clp->cl_rpcclient,
6329 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6330 clp->cl_rpcclient->cl_nodename,
6331 rpc_peeraddr2str(clp->cl_rpcclient,
6335 clp->cl_owner_id = str;
6340 nfs4_init_uniform_client_string(struct nfs_client *clp)
6342 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6347 if (clp->cl_owner_id != NULL)
6350 len = 10 + 10 + 1 + 10 + 1 +
6351 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6353 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6357 if (len > NFS4_OPAQUE_LIMIT + 1)
6361 * Since this string is allocated at mount time, and held until the
6362 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6363 * about a memory-reclaim deadlock.
6365 str = kmalloc(len, GFP_KERNEL);
6370 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6371 clp->rpc_ops->version, clp->cl_minorversion,
6372 buf, clp->cl_rpcclient->cl_nodename);
6374 scnprintf(str, len, "Linux NFSv%u.%u %s",
6375 clp->rpc_ops->version, clp->cl_minorversion,
6376 clp->cl_rpcclient->cl_nodename);
6377 clp->cl_owner_id = str;
6382 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6383 * services. Advertise one based on the address family of the
6387 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6389 if (strchr(clp->cl_ipaddr, ':') != NULL)
6390 return scnprintf(buf, len, "tcp6");
6392 return scnprintf(buf, len, "tcp");
6395 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6397 struct nfs4_setclientid *sc = calldata;
6399 if (task->tk_status == 0)
6400 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6403 static const struct rpc_call_ops nfs4_setclientid_ops = {
6404 .rpc_call_done = nfs4_setclientid_done,
6408 * nfs4_proc_setclientid - Negotiate client ID
6409 * @clp: state data structure
6410 * @program: RPC program for NFSv4 callback service
6411 * @port: IP port number for NFS4 callback service
6412 * @cred: credential to use for this call
6413 * @res: where to place the result
6415 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6417 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6418 unsigned short port, const struct cred *cred,
6419 struct nfs4_setclientid_res *res)
6421 nfs4_verifier sc_verifier;
6422 struct nfs4_setclientid setclientid = {
6423 .sc_verifier = &sc_verifier,
6427 struct rpc_message msg = {
6428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6429 .rpc_argp = &setclientid,
6433 struct rpc_task_setup task_setup_data = {
6434 .rpc_client = clp->cl_rpcclient,
6435 .rpc_message = &msg,
6436 .callback_ops = &nfs4_setclientid_ops,
6437 .callback_data = &setclientid,
6438 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6440 unsigned long now = jiffies;
6443 /* nfs_client_id4 */
6444 nfs4_init_boot_verifier(clp, &sc_verifier);
6446 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6447 status = nfs4_init_uniform_client_string(clp);
6449 status = nfs4_init_nonuniform_client_string(clp);
6455 setclientid.sc_netid_len =
6456 nfs4_init_callback_netid(clp,
6457 setclientid.sc_netid,
6458 sizeof(setclientid.sc_netid));
6459 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6460 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6461 clp->cl_ipaddr, port >> 8, port & 255);
6463 dprintk("NFS call setclientid auth=%s, '%s'\n",
6464 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6467 status = nfs4_call_sync_custom(&task_setup_data);
6468 if (setclientid.sc_cred) {
6469 kfree(clp->cl_acceptor);
6470 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6471 put_rpccred(setclientid.sc_cred);
6475 do_renew_lease(clp, now);
6477 trace_nfs4_setclientid(clp, status);
6478 dprintk("NFS reply setclientid: %d\n", status);
6483 * nfs4_proc_setclientid_confirm - Confirm client ID
6484 * @clp: state data structure
6485 * @arg: result of a previous SETCLIENTID
6486 * @cred: credential to use for this call
6488 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6490 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6491 struct nfs4_setclientid_res *arg,
6492 const struct cred *cred)
6494 struct rpc_message msg = {
6495 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6501 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6502 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6504 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6505 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6506 trace_nfs4_setclientid_confirm(clp, status);
6507 dprintk("NFS reply setclientid_confirm: %d\n", status);
6511 struct nfs4_delegreturndata {
6512 struct nfs4_delegreturnargs args;
6513 struct nfs4_delegreturnres res;
6515 nfs4_stateid stateid;
6516 unsigned long timestamp;
6518 struct nfs4_layoutreturn_args arg;
6519 struct nfs4_layoutreturn_res res;
6520 struct nfs4_xdr_opaque_data ld_private;
6524 struct nfs_fattr fattr;
6526 struct inode *inode;
6529 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6531 struct nfs4_delegreturndata *data = calldata;
6532 struct nfs4_exception exception = {
6533 .inode = data->inode,
6534 .stateid = &data->stateid,
6535 .task_is_privileged = data->args.seq_args.sa_privileged,
6538 if (!nfs4_sequence_done(task, &data->res.seq_res))
6541 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6543 /* Handle Layoutreturn errors */
6544 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6545 &data->res.lr_ret) == -EAGAIN)
6548 switch (task->tk_status) {
6550 renew_lease(data->res.server, data->timestamp);
6552 case -NFS4ERR_ADMIN_REVOKED:
6553 case -NFS4ERR_DELEG_REVOKED:
6554 case -NFS4ERR_EXPIRED:
6555 nfs4_free_revoked_stateid(data->res.server,
6557 task->tk_msg.rpc_cred);
6559 case -NFS4ERR_BAD_STATEID:
6560 case -NFS4ERR_STALE_STATEID:
6562 task->tk_status = 0;
6564 case -NFS4ERR_OLD_STATEID:
6565 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6566 nfs4_stateid_seqid_inc(&data->stateid);
6567 if (data->args.bitmask) {
6568 data->args.bitmask = NULL;
6569 data->res.fattr = NULL;
6572 case -NFS4ERR_ACCESS:
6573 if (data->args.bitmask) {
6574 data->args.bitmask = NULL;
6575 data->res.fattr = NULL;
6580 task->tk_status = nfs4_async_handle_exception(task,
6581 data->res.server, task->tk_status,
6583 if (exception.retry)
6586 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6587 data->rpc_status = task->tk_status;
6590 task->tk_status = 0;
6591 rpc_restart_call_prepare(task);
6594 static void nfs4_delegreturn_release(void *calldata)
6596 struct nfs4_delegreturndata *data = calldata;
6597 struct inode *inode = data->inode;
6600 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6603 nfs4_fattr_set_prechange(&data->fattr,
6604 inode_peek_iversion_raw(inode));
6605 nfs_refresh_inode(inode, &data->fattr);
6606 nfs_iput_and_deactive(inode);
6611 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6613 struct nfs4_delegreturndata *d_data;
6614 struct pnfs_layout_hdr *lo;
6618 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6619 nfs4_sequence_done(task, &d_data->res.seq_res);
6623 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6624 if (lo && !pnfs_layout_is_valid(lo)) {
6625 d_data->args.lr_args = NULL;
6626 d_data->res.lr_res = NULL;
6629 nfs4_setup_sequence(d_data->res.server->nfs_client,
6630 &d_data->args.seq_args,
6631 &d_data->res.seq_res,
6635 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6636 .rpc_call_prepare = nfs4_delegreturn_prepare,
6637 .rpc_call_done = nfs4_delegreturn_done,
6638 .rpc_release = nfs4_delegreturn_release,
6641 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6643 struct nfs4_delegreturndata *data;
6644 struct nfs_server *server = NFS_SERVER(inode);
6645 struct rpc_task *task;
6646 struct rpc_message msg = {
6647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6650 struct rpc_task_setup task_setup_data = {
6651 .rpc_client = server->client,
6652 .rpc_message = &msg,
6653 .callback_ops = &nfs4_delegreturn_ops,
6654 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6658 if (nfs_server_capable(inode, NFS_CAP_MOVEABLE))
6659 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6661 data = kzalloc(sizeof(*data), GFP_KERNEL);
6665 nfs4_state_protect(server->nfs_client,
6666 NFS_SP4_MACH_CRED_CLEANUP,
6667 &task_setup_data.rpc_client, &msg);
6669 data->args.fhandle = &data->fh;
6670 data->args.stateid = &data->stateid;
6671 nfs4_bitmask_set(data->args.bitmask_store,
6672 server->cache_consistency_bitmask, inode, 0);
6673 data->args.bitmask = data->args.bitmask_store;
6674 nfs_copy_fh(&data->fh, NFS_FH(inode));
6675 nfs4_stateid_copy(&data->stateid, stateid);
6676 data->res.fattr = &data->fattr;
6677 data->res.server = server;
6678 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6679 data->lr.arg.ld_private = &data->lr.ld_private;
6680 nfs_fattr_init(data->res.fattr);
6681 data->timestamp = jiffies;
6682 data->rpc_status = 0;
6683 data->inode = nfs_igrab_and_active(inode);
6684 if (data->inode || issync) {
6685 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6688 data->args.lr_args = &data->lr.arg;
6689 data->res.lr_res = &data->lr.res;
6694 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6697 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6699 task_setup_data.callback_data = data;
6700 msg.rpc_argp = &data->args;
6701 msg.rpc_resp = &data->res;
6702 task = rpc_run_task(&task_setup_data);
6704 return PTR_ERR(task);
6707 status = rpc_wait_for_completion_task(task);
6710 status = data->rpc_status;
6716 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6718 struct nfs_server *server = NFS_SERVER(inode);
6719 struct nfs4_exception exception = { };
6722 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6723 trace_nfs4_delegreturn(inode, stateid, err);
6725 case -NFS4ERR_STALE_STATEID:
6726 case -NFS4ERR_EXPIRED:
6730 err = nfs4_handle_exception(server, err, &exception);
6731 } while (exception.retry);
6735 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6737 struct inode *inode = state->inode;
6738 struct nfs_server *server = NFS_SERVER(inode);
6739 struct nfs_client *clp = server->nfs_client;
6740 struct nfs_lockt_args arg = {
6741 .fh = NFS_FH(inode),
6744 struct nfs_lockt_res res = {
6747 struct rpc_message msg = {
6748 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6751 .rpc_cred = state->owner->so_cred,
6753 struct nfs4_lock_state *lsp;
6756 arg.lock_owner.clientid = clp->cl_clientid;
6757 status = nfs4_set_lock_state(state, request);
6760 lsp = request->fl_u.nfs4_fl.owner;
6761 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6762 arg.lock_owner.s_dev = server->s_dev;
6763 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6766 request->fl_type = F_UNLCK;
6768 case -NFS4ERR_DENIED:
6771 request->fl_ops->fl_release_private(request);
6772 request->fl_ops = NULL;
6777 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6779 struct nfs4_exception exception = {
6780 .interruptible = true,
6785 err = _nfs4_proc_getlk(state, cmd, request);
6786 trace_nfs4_get_lock(request, state, cmd, err);
6787 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6789 } while (exception.retry);
6794 * Update the seqid of a lock stateid after receiving
6795 * NFS4ERR_OLD_STATEID
6797 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6798 struct nfs4_lock_state *lsp)
6800 struct nfs4_state *state = lsp->ls_state;
6803 spin_lock(&state->state_lock);
6804 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6806 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6807 nfs4_stateid_seqid_inc(dst);
6809 dst->seqid = lsp->ls_stateid.seqid;
6812 spin_unlock(&state->state_lock);
6816 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6817 struct nfs4_lock_state *lsp)
6819 struct nfs4_state *state = lsp->ls_state;
6822 spin_lock(&state->state_lock);
6823 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6824 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6825 spin_unlock(&state->state_lock);
6829 struct nfs4_unlockdata {
6830 struct nfs_locku_args arg;
6831 struct nfs_locku_res res;
6832 struct nfs4_lock_state *lsp;
6833 struct nfs_open_context *ctx;
6834 struct nfs_lock_context *l_ctx;
6835 struct file_lock fl;
6836 struct nfs_server *server;
6837 unsigned long timestamp;
6840 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6841 struct nfs_open_context *ctx,
6842 struct nfs4_lock_state *lsp,
6843 struct nfs_seqid *seqid)
6845 struct nfs4_unlockdata *p;
6846 struct nfs4_state *state = lsp->ls_state;
6847 struct inode *inode = state->inode;
6849 p = kzalloc(sizeof(*p), GFP_KERNEL);
6852 p->arg.fh = NFS_FH(inode);
6854 p->arg.seqid = seqid;
6855 p->res.seqid = seqid;
6857 /* Ensure we don't close file until we're done freeing locks! */
6858 p->ctx = get_nfs_open_context(ctx);
6859 p->l_ctx = nfs_get_lock_context(ctx);
6860 locks_init_lock(&p->fl);
6861 locks_copy_lock(&p->fl, fl);
6862 p->server = NFS_SERVER(inode);
6863 spin_lock(&state->state_lock);
6864 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6865 spin_unlock(&state->state_lock);
6869 static void nfs4_locku_release_calldata(void *data)
6871 struct nfs4_unlockdata *calldata = data;
6872 nfs_free_seqid(calldata->arg.seqid);
6873 nfs4_put_lock_state(calldata->lsp);
6874 nfs_put_lock_context(calldata->l_ctx);
6875 put_nfs_open_context(calldata->ctx);
6879 static void nfs4_locku_done(struct rpc_task *task, void *data)
6881 struct nfs4_unlockdata *calldata = data;
6882 struct nfs4_exception exception = {
6883 .inode = calldata->lsp->ls_state->inode,
6884 .stateid = &calldata->arg.stateid,
6887 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6889 switch (task->tk_status) {
6891 renew_lease(calldata->server, calldata->timestamp);
6892 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6893 if (nfs4_update_lock_stateid(calldata->lsp,
6894 &calldata->res.stateid))
6897 case -NFS4ERR_ADMIN_REVOKED:
6898 case -NFS4ERR_EXPIRED:
6899 nfs4_free_revoked_stateid(calldata->server,
6900 &calldata->arg.stateid,
6901 task->tk_msg.rpc_cred);
6903 case -NFS4ERR_BAD_STATEID:
6904 case -NFS4ERR_STALE_STATEID:
6905 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6907 rpc_restart_call_prepare(task);
6909 case -NFS4ERR_OLD_STATEID:
6910 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6912 rpc_restart_call_prepare(task);
6915 task->tk_status = nfs4_async_handle_exception(task,
6916 calldata->server, task->tk_status,
6918 if (exception.retry)
6919 rpc_restart_call_prepare(task);
6921 nfs_release_seqid(calldata->arg.seqid);
6924 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6926 struct nfs4_unlockdata *calldata = data;
6928 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6929 nfs_async_iocounter_wait(task, calldata->l_ctx))
6932 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6934 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6935 /* Note: exit _without_ running nfs4_locku_done */
6938 calldata->timestamp = jiffies;
6939 if (nfs4_setup_sequence(calldata->server->nfs_client,
6940 &calldata->arg.seq_args,
6941 &calldata->res.seq_res,
6943 nfs_release_seqid(calldata->arg.seqid);
6946 task->tk_action = NULL;
6948 nfs4_sequence_done(task, &calldata->res.seq_res);
6951 static const struct rpc_call_ops nfs4_locku_ops = {
6952 .rpc_call_prepare = nfs4_locku_prepare,
6953 .rpc_call_done = nfs4_locku_done,
6954 .rpc_release = nfs4_locku_release_calldata,
6957 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6958 struct nfs_open_context *ctx,
6959 struct nfs4_lock_state *lsp,
6960 struct nfs_seqid *seqid)
6962 struct nfs4_unlockdata *data;
6963 struct rpc_message msg = {
6964 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6965 .rpc_cred = ctx->cred,
6967 struct rpc_task_setup task_setup_data = {
6968 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6969 .rpc_message = &msg,
6970 .callback_ops = &nfs4_locku_ops,
6971 .workqueue = nfsiod_workqueue,
6972 .flags = RPC_TASK_ASYNC,
6975 if (nfs_server_capable(lsp->ls_state->inode, NFS_CAP_MOVEABLE))
6976 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6978 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6979 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6981 /* Ensure this is an unlock - when canceling a lock, the
6982 * canceled lock is passed in, and it won't be an unlock.
6984 fl->fl_type = F_UNLCK;
6985 if (fl->fl_flags & FL_CLOSE)
6986 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6988 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6990 nfs_free_seqid(seqid);
6991 return ERR_PTR(-ENOMEM);
6994 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6995 msg.rpc_argp = &data->arg;
6996 msg.rpc_resp = &data->res;
6997 task_setup_data.callback_data = data;
6998 return rpc_run_task(&task_setup_data);
7001 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
7003 struct inode *inode = state->inode;
7004 struct nfs4_state_owner *sp = state->owner;
7005 struct nfs_inode *nfsi = NFS_I(inode);
7006 struct nfs_seqid *seqid;
7007 struct nfs4_lock_state *lsp;
7008 struct rpc_task *task;
7009 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7011 unsigned char fl_flags = request->fl_flags;
7013 status = nfs4_set_lock_state(state, request);
7014 /* Unlock _before_ we do the RPC call */
7015 request->fl_flags |= FL_EXISTS;
7016 /* Exclude nfs_delegation_claim_locks() */
7017 mutex_lock(&sp->so_delegreturn_mutex);
7018 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
7019 down_read(&nfsi->rwsem);
7020 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
7021 up_read(&nfsi->rwsem);
7022 mutex_unlock(&sp->so_delegreturn_mutex);
7025 lsp = request->fl_u.nfs4_fl.owner;
7026 set_bit(NFS_LOCK_UNLOCKING, &lsp->ls_flags);
7027 up_read(&nfsi->rwsem);
7028 mutex_unlock(&sp->so_delegreturn_mutex);
7031 /* Is this a delegated lock? */
7032 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
7034 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
7035 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
7039 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
7040 status = PTR_ERR(task);
7043 status = rpc_wait_for_completion_task(task);
7046 request->fl_flags = fl_flags;
7047 trace_nfs4_unlock(request, state, F_SETLK, status);
7051 struct nfs4_lockdata {
7052 struct nfs_lock_args arg;
7053 struct nfs_lock_res res;
7054 struct nfs4_lock_state *lsp;
7055 struct nfs_open_context *ctx;
7056 struct file_lock fl;
7057 unsigned long timestamp;
7060 struct nfs_server *server;
7063 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
7064 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
7067 struct nfs4_lockdata *p;
7068 struct inode *inode = lsp->ls_state->inode;
7069 struct nfs_server *server = NFS_SERVER(inode);
7070 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7072 p = kzalloc(sizeof(*p), gfp_mask);
7076 p->arg.fh = NFS_FH(inode);
7078 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
7079 if (IS_ERR(p->arg.open_seqid))
7081 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
7082 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
7083 if (IS_ERR(p->arg.lock_seqid))
7084 goto out_free_seqid;
7085 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
7086 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
7087 p->arg.lock_owner.s_dev = server->s_dev;
7088 p->res.lock_seqid = p->arg.lock_seqid;
7091 p->ctx = get_nfs_open_context(ctx);
7092 locks_init_lock(&p->fl);
7093 locks_copy_lock(&p->fl, fl);
7096 nfs_free_seqid(p->arg.open_seqid);
7102 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7104 struct nfs4_lockdata *data = calldata;
7105 struct nfs4_state *state = data->lsp->ls_state;
7107 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
7109 /* Do we need to do an open_to_lock_owner? */
7110 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
7111 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
7112 goto out_release_lock_seqid;
7114 nfs4_stateid_copy(&data->arg.open_stateid,
7115 &state->open_stateid);
7116 data->arg.new_lock_owner = 1;
7117 data->res.open_seqid = data->arg.open_seqid;
7119 data->arg.new_lock_owner = 0;
7120 nfs4_stateid_copy(&data->arg.lock_stateid,
7121 &data->lsp->ls_stateid);
7123 if (!nfs4_valid_open_stateid(state)) {
7124 data->rpc_status = -EBADF;
7125 task->tk_action = NULL;
7126 goto out_release_open_seqid;
7128 data->timestamp = jiffies;
7129 if (nfs4_setup_sequence(data->server->nfs_client,
7130 &data->arg.seq_args,
7134 out_release_open_seqid:
7135 nfs_release_seqid(data->arg.open_seqid);
7136 out_release_lock_seqid:
7137 nfs_release_seqid(data->arg.lock_seqid);
7139 nfs4_sequence_done(task, &data->res.seq_res);
7140 dprintk("%s: ret = %d\n", __func__, data->rpc_status);
7143 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7145 struct nfs4_lockdata *data = calldata;
7146 struct nfs4_lock_state *lsp = data->lsp;
7147 struct nfs_server *server = NFS_SERVER(d_inode(data->ctx->dentry));
7149 if (!nfs4_sequence_done(task, &data->res.seq_res))
7152 data->rpc_status = task->tk_status;
7153 switch (task->tk_status) {
7155 renew_lease(server, data->timestamp);
7156 if (data->arg.new_lock && !data->cancelled) {
7157 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
7158 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7161 if (data->arg.new_lock_owner != 0) {
7162 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7163 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7164 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7165 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7168 case -NFS4ERR_BAD_STATEID:
7169 case -NFS4ERR_OLD_STATEID:
7170 case -NFS4ERR_STALE_STATEID:
7171 case -NFS4ERR_EXPIRED:
7172 if (data->arg.new_lock_owner != 0) {
7173 if (!nfs4_stateid_match(&data->arg.open_stateid,
7174 &lsp->ls_state->open_stateid))
7176 else if (nfs4_async_handle_error(task, server, lsp->ls_state, NULL) == -EAGAIN)
7178 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7183 dprintk("%s: ret = %d!\n", __func__, data->rpc_status);
7186 if (!data->cancelled)
7187 rpc_restart_call_prepare(task);
7191 static void nfs4_lock_release(void *calldata)
7193 struct nfs4_lockdata *data = calldata;
7195 nfs_free_seqid(data->arg.open_seqid);
7196 if (data->cancelled && data->rpc_status == 0) {
7197 struct rpc_task *task;
7198 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7199 data->arg.lock_seqid);
7201 rpc_put_task_async(task);
7202 dprintk("%s: cancelling lock!\n", __func__);
7204 nfs_free_seqid(data->arg.lock_seqid);
7205 nfs4_put_lock_state(data->lsp);
7206 put_nfs_open_context(data->ctx);
7210 static const struct rpc_call_ops nfs4_lock_ops = {
7211 .rpc_call_prepare = nfs4_lock_prepare,
7212 .rpc_call_done = nfs4_lock_done,
7213 .rpc_release = nfs4_lock_release,
7216 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7219 case -NFS4ERR_ADMIN_REVOKED:
7220 case -NFS4ERR_EXPIRED:
7221 case -NFS4ERR_BAD_STATEID:
7222 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7223 if (new_lock_owner != 0 ||
7224 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7225 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7227 case -NFS4ERR_STALE_STATEID:
7228 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7229 nfs4_schedule_lease_recovery(server->nfs_client);
7233 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7235 struct nfs4_lockdata *data;
7236 struct rpc_task *task;
7237 struct rpc_message msg = {
7238 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7239 .rpc_cred = state->owner->so_cred,
7241 struct rpc_task_setup task_setup_data = {
7242 .rpc_client = NFS_CLIENT(state->inode),
7243 .rpc_message = &msg,
7244 .callback_ops = &nfs4_lock_ops,
7245 .workqueue = nfsiod_workqueue,
7246 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7250 if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
7251 task_setup_data.flags |= RPC_TASK_MOVEABLE;
7253 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7254 fl->fl_u.nfs4_fl.owner, GFP_KERNEL);
7258 data->arg.block = 1;
7259 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7260 recovery_type > NFS_LOCK_NEW);
7261 msg.rpc_argp = &data->arg;
7262 msg.rpc_resp = &data->res;
7263 task_setup_data.callback_data = data;
7264 if (recovery_type > NFS_LOCK_NEW) {
7265 if (recovery_type == NFS_LOCK_RECLAIM)
7266 data->arg.reclaim = NFS_LOCK_RECLAIM;
7268 data->arg.new_lock = 1;
7269 task = rpc_run_task(&task_setup_data);
7271 return PTR_ERR(task);
7272 ret = rpc_wait_for_completion_task(task);
7274 ret = data->rpc_status;
7276 nfs4_handle_setlk_error(data->server, data->lsp,
7277 data->arg.new_lock_owner, ret);
7279 data->cancelled = true;
7280 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7282 dprintk("%s: ret = %d\n", __func__, ret);
7286 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7288 struct nfs_server *server = NFS_SERVER(state->inode);
7289 struct nfs4_exception exception = {
7290 .inode = state->inode,
7295 /* Cache the lock if possible... */
7296 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7298 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7299 if (err != -NFS4ERR_DELAY)
7301 nfs4_handle_exception(server, err, &exception);
7302 } while (exception.retry);
7306 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7308 struct nfs_server *server = NFS_SERVER(state->inode);
7309 struct nfs4_exception exception = {
7310 .inode = state->inode,
7314 err = nfs4_set_lock_state(state, request);
7317 if (!recover_lost_locks) {
7318 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7322 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7324 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7328 case -NFS4ERR_GRACE:
7329 case -NFS4ERR_DELAY:
7330 nfs4_handle_exception(server, err, &exception);
7333 } while (exception.retry);
7338 #if defined(CONFIG_NFS_V4_1)
7339 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7341 struct nfs4_lock_state *lsp;
7344 status = nfs4_set_lock_state(state, request);
7347 lsp = request->fl_u.nfs4_fl.owner;
7348 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7349 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7351 return nfs4_lock_expired(state, request);
7355 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7357 struct nfs_inode *nfsi = NFS_I(state->inode);
7358 struct nfs4_state_owner *sp = state->owner;
7359 unsigned char fl_flags = request->fl_flags;
7362 request->fl_flags |= FL_ACCESS;
7363 status = locks_lock_inode_wait(state->inode, request);
7366 mutex_lock(&sp->so_delegreturn_mutex);
7367 down_read(&nfsi->rwsem);
7368 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7369 /* Yes: cache locks! */
7370 /* ...but avoid races with delegation recall... */
7371 request->fl_flags = fl_flags & ~FL_SLEEP;
7372 status = locks_lock_inode_wait(state->inode, request);
7373 up_read(&nfsi->rwsem);
7374 mutex_unlock(&sp->so_delegreturn_mutex);
7377 up_read(&nfsi->rwsem);
7378 mutex_unlock(&sp->so_delegreturn_mutex);
7379 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7381 request->fl_flags = fl_flags;
7385 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7387 struct nfs4_exception exception = {
7389 .inode = state->inode,
7390 .interruptible = true,
7395 err = _nfs4_proc_setlk(state, cmd, request);
7396 if (err == -NFS4ERR_DENIED)
7398 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7400 } while (exception.retry);
7404 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7405 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7408 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7409 struct file_lock *request)
7411 int status = -ERESTARTSYS;
7412 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7414 while(!signalled()) {
7415 status = nfs4_proc_setlk(state, cmd, request);
7416 if ((status != -EAGAIN) || IS_SETLK(cmd))
7418 __set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
7419 schedule_timeout(timeout);
7421 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7422 status = -ERESTARTSYS;
7427 #ifdef CONFIG_NFS_V4_1
7428 struct nfs4_lock_waiter {
7429 struct inode *inode;
7430 struct nfs_lowner owner;
7431 wait_queue_entry_t wait;
7435 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7437 struct nfs4_lock_waiter *waiter =
7438 container_of(wait, struct nfs4_lock_waiter, wait);
7440 /* NULL key means to wake up everyone */
7442 struct cb_notify_lock_args *cbnl = key;
7443 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7444 *wowner = &waiter->owner;
7446 /* Only wake if the callback was for the same owner. */
7447 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7450 /* Make sure it's for the right inode */
7451 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7455 return woken_wake_function(wait, mode, flags, key);
7459 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7461 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7462 struct nfs_server *server = NFS_SERVER(state->inode);
7463 struct nfs_client *clp = server->nfs_client;
7464 wait_queue_head_t *q = &clp->cl_lock_waitq;
7465 struct nfs4_lock_waiter waiter = {
7466 .inode = state->inode,
7467 .owner = { .clientid = clp->cl_clientid,
7468 .id = lsp->ls_seqid.owner_id,
7469 .s_dev = server->s_dev },
7473 /* Don't bother with waitqueue if we don't expect a callback */
7474 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7475 return nfs4_retry_setlk_simple(state, cmd, request);
7477 init_wait(&waiter.wait);
7478 waiter.wait.func = nfs4_wake_lock_waiter;
7479 add_wait_queue(q, &waiter.wait);
7482 status = nfs4_proc_setlk(state, cmd, request);
7483 if (status != -EAGAIN || IS_SETLK(cmd))
7486 status = -ERESTARTSYS;
7487 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE|TASK_FREEZABLE,
7488 NFS4_LOCK_MAXTIMEOUT);
7489 } while (!signalled());
7491 remove_wait_queue(q, &waiter.wait);
7495 #else /* !CONFIG_NFS_V4_1 */
7497 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7499 return nfs4_retry_setlk_simple(state, cmd, request);
7504 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7506 struct nfs_open_context *ctx;
7507 struct nfs4_state *state;
7510 /* verify open state */
7511 ctx = nfs_file_open_context(filp);
7514 if (IS_GETLK(cmd)) {
7516 return nfs4_proc_getlk(state, F_GETLK, request);
7520 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7523 if (request->fl_type == F_UNLCK) {
7525 return nfs4_proc_unlck(state, cmd, request);
7532 if ((request->fl_flags & FL_POSIX) &&
7533 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7537 * Don't rely on the VFS having checked the file open mode,
7538 * since it won't do this for flock() locks.
7540 switch (request->fl_type) {
7542 if (!(filp->f_mode & FMODE_READ))
7546 if (!(filp->f_mode & FMODE_WRITE))
7550 status = nfs4_set_lock_state(state, request);
7554 return nfs4_retry_setlk(state, cmd, request);
7557 static int nfs4_delete_lease(struct file *file, void **priv)
7559 return generic_setlease(file, F_UNLCK, NULL, priv);
7562 static int nfs4_add_lease(struct file *file, long arg, struct file_lock **lease,
7565 struct inode *inode = file_inode(file);
7566 fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7569 /* No delegation, no lease */
7570 if (!nfs4_have_delegation(inode, type))
7572 ret = generic_setlease(file, arg, lease, priv);
7573 if (ret || nfs4_have_delegation(inode, type))
7575 /* We raced with a delegation return */
7576 nfs4_delete_lease(file, priv);
7580 int nfs4_proc_setlease(struct file *file, long arg, struct file_lock **lease,
7586 return nfs4_add_lease(file, arg, lease, priv);
7588 return nfs4_delete_lease(file, priv);
7594 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7596 struct nfs_server *server = NFS_SERVER(state->inode);
7599 err = nfs4_set_lock_state(state, fl);
7603 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7604 if (err != -NFS4ERR_DELAY)
7607 } while (err == -NFS4ERR_DELAY);
7608 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7611 struct nfs_release_lockowner_data {
7612 struct nfs4_lock_state *lsp;
7613 struct nfs_server *server;
7614 struct nfs_release_lockowner_args args;
7615 struct nfs_release_lockowner_res res;
7616 unsigned long timestamp;
7619 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7621 struct nfs_release_lockowner_data *data = calldata;
7622 struct nfs_server *server = data->server;
7623 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7624 &data->res.seq_res, task);
7625 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7626 data->timestamp = jiffies;
7629 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7631 struct nfs_release_lockowner_data *data = calldata;
7632 struct nfs_server *server = data->server;
7634 nfs40_sequence_done(task, &data->res.seq_res);
7636 switch (task->tk_status) {
7638 renew_lease(server, data->timestamp);
7640 case -NFS4ERR_STALE_CLIENTID:
7641 case -NFS4ERR_EXPIRED:
7642 nfs4_schedule_lease_recovery(server->nfs_client);
7644 case -NFS4ERR_LEASE_MOVED:
7645 case -NFS4ERR_DELAY:
7646 if (nfs4_async_handle_error(task, server,
7647 NULL, NULL) == -EAGAIN)
7648 rpc_restart_call_prepare(task);
7652 static void nfs4_release_lockowner_release(void *calldata)
7654 struct nfs_release_lockowner_data *data = calldata;
7655 nfs4_free_lock_state(data->server, data->lsp);
7659 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7660 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7661 .rpc_call_done = nfs4_release_lockowner_done,
7662 .rpc_release = nfs4_release_lockowner_release,
7666 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7668 struct nfs_release_lockowner_data *data;
7669 struct rpc_message msg = {
7670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7673 if (server->nfs_client->cl_mvops->minor_version != 0)
7676 data = kmalloc(sizeof(*data), GFP_KERNEL);
7680 data->server = server;
7681 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7682 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7683 data->args.lock_owner.s_dev = server->s_dev;
7685 msg.rpc_argp = &data->args;
7686 msg.rpc_resp = &data->res;
7687 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7688 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7691 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7693 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7694 struct mnt_idmap *idmap,
7695 struct dentry *unused, struct inode *inode,
7696 const char *key, const void *buf,
7697 size_t buflen, int flags)
7699 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_ACL);
7702 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7703 struct dentry *unused, struct inode *inode,
7704 const char *key, void *buf, size_t buflen)
7706 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_ACL);
7709 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7711 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_ACL);
7714 #if defined(CONFIG_NFS_V4_1)
7715 #define XATTR_NAME_NFSV4_DACL "system.nfs4_dacl"
7717 static int nfs4_xattr_set_nfs4_dacl(const struct xattr_handler *handler,
7718 struct mnt_idmap *idmap,
7719 struct dentry *unused, struct inode *inode,
7720 const char *key, const void *buf,
7721 size_t buflen, int flags)
7723 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_DACL);
7726 static int nfs4_xattr_get_nfs4_dacl(const struct xattr_handler *handler,
7727 struct dentry *unused, struct inode *inode,
7728 const char *key, void *buf, size_t buflen)
7730 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_DACL);
7733 static bool nfs4_xattr_list_nfs4_dacl(struct dentry *dentry)
7735 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_DACL);
7738 #define XATTR_NAME_NFSV4_SACL "system.nfs4_sacl"
7740 static int nfs4_xattr_set_nfs4_sacl(const struct xattr_handler *handler,
7741 struct mnt_idmap *idmap,
7742 struct dentry *unused, struct inode *inode,
7743 const char *key, const void *buf,
7744 size_t buflen, int flags)
7746 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_SACL);
7749 static int nfs4_xattr_get_nfs4_sacl(const struct xattr_handler *handler,
7750 struct dentry *unused, struct inode *inode,
7751 const char *key, void *buf, size_t buflen)
7753 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_SACL);
7756 static bool nfs4_xattr_list_nfs4_sacl(struct dentry *dentry)
7758 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_SACL);
7763 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7765 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7766 struct mnt_idmap *idmap,
7767 struct dentry *unused, struct inode *inode,
7768 const char *key, const void *buf,
7769 size_t buflen, int flags)
7771 if (security_ismaclabel(key))
7772 return nfs4_set_security_label(inode, buf, buflen);
7777 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7778 struct dentry *unused, struct inode *inode,
7779 const char *key, void *buf, size_t buflen)
7781 if (security_ismaclabel(key))
7782 return nfs4_get_security_label(inode, buf, buflen);
7787 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7791 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7792 len = security_inode_listsecurity(inode, list, list_len);
7793 if (len >= 0 && list_len && len > list_len)
7799 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7800 .prefix = XATTR_SECURITY_PREFIX,
7801 .get = nfs4_xattr_get_nfs4_label,
7802 .set = nfs4_xattr_set_nfs4_label,
7808 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7815 #ifdef CONFIG_NFS_V4_2
7816 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7817 struct mnt_idmap *idmap,
7818 struct dentry *unused, struct inode *inode,
7819 const char *key, const void *buf,
7820 size_t buflen, int flags)
7825 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7829 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7830 * flags right now. Handling of xattr operations use the normal
7831 * file read/write permissions.
7833 * Just in case the server has other ideas (which RFC 8276 allows),
7834 * do a cached access check for the XA* flags to possibly avoid
7835 * doing an RPC and getting EACCES back.
7837 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7838 if (!(mask & NFS_ACCESS_XAWRITE))
7843 ret = nfs42_proc_removexattr(inode, key);
7845 nfs4_xattr_cache_remove(inode, key);
7847 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7849 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7855 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7856 struct dentry *unused, struct inode *inode,
7857 const char *key, void *buf, size_t buflen)
7862 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7865 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7866 if (!(mask & NFS_ACCESS_XAREAD))
7870 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7874 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7875 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7878 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7884 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7893 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7896 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7897 if (!(mask & NFS_ACCESS_XALIST))
7901 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7905 ret = nfs4_xattr_cache_list(inode, list, list_len);
7906 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7911 buflen = list_len ? list_len : XATTR_LIST_MAX;
7912 buf = list_len ? list : NULL;
7916 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7929 nfs4_xattr_cache_set_list(inode, list, size);
7937 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7941 #endif /* CONFIG_NFS_V4_2 */
7944 * nfs_fhget will use either the mounted_on_fileid or the fileid
7946 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7948 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7949 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7950 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7951 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7954 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7955 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7956 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7960 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7961 const struct qstr *name,
7962 struct nfs4_fs_locations *fs_locations,
7965 struct nfs_server *server = NFS_SERVER(dir);
7967 struct nfs4_fs_locations_arg args = {
7968 .dir_fh = NFS_FH(dir),
7973 struct nfs4_fs_locations_res res = {
7974 .fs_locations = fs_locations,
7976 struct rpc_message msg = {
7977 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7983 dprintk("%s: start\n", __func__);
7985 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7986 bitmask[1] = nfs4_fattr_bitmap[1];
7988 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7989 * is not supported */
7990 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7991 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7993 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7995 nfs_fattr_init(fs_locations->fattr);
7996 fs_locations->server = server;
7997 fs_locations->nlocations = 0;
7998 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7999 dprintk("%s: returned status = %d\n", __func__, status);
8003 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
8004 const struct qstr *name,
8005 struct nfs4_fs_locations *fs_locations,
8008 struct nfs4_exception exception = {
8009 .interruptible = true,
8013 err = _nfs4_proc_fs_locations(client, dir, name,
8014 fs_locations, page);
8015 trace_nfs4_get_fs_locations(dir, name, err);
8016 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8018 } while (exception.retry);
8023 * This operation also signals the server that this client is
8024 * performing migration recovery. The server can stop returning
8025 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
8026 * appended to this compound to identify the client ID which is
8027 * performing recovery.
8029 static int _nfs40_proc_get_locations(struct nfs_server *server,
8030 struct nfs_fh *fhandle,
8031 struct nfs4_fs_locations *locations,
8032 struct page *page, const struct cred *cred)
8034 struct rpc_clnt *clnt = server->client;
8036 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8038 struct nfs4_fs_locations_arg args = {
8039 .clientid = server->nfs_client->cl_clientid,
8043 .migration = 1, /* skip LOOKUP */
8044 .renew = 1, /* append RENEW */
8046 struct nfs4_fs_locations_res res = {
8047 .fs_locations = locations,
8051 struct rpc_message msg = {
8052 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8057 unsigned long now = jiffies;
8060 nfs_fattr_init(locations->fattr);
8061 locations->server = server;
8062 locations->nlocations = 0;
8064 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8065 status = nfs4_call_sync_sequence(clnt, server, &msg,
8066 &args.seq_args, &res.seq_res);
8070 renew_lease(server, now);
8074 #ifdef CONFIG_NFS_V4_1
8077 * This operation also signals the server that this client is
8078 * performing migration recovery. The server can stop asserting
8079 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
8080 * performing this operation is identified in the SEQUENCE
8081 * operation in this compound.
8083 * When the client supports GETATTR(fs_locations_info), it can
8084 * be plumbed in here.
8086 static int _nfs41_proc_get_locations(struct nfs_server *server,
8087 struct nfs_fh *fhandle,
8088 struct nfs4_fs_locations *locations,
8089 struct page *page, const struct cred *cred)
8091 struct rpc_clnt *clnt = server->client;
8093 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8095 struct nfs4_fs_locations_arg args = {
8099 .migration = 1, /* skip LOOKUP */
8101 struct nfs4_fs_locations_res res = {
8102 .fs_locations = locations,
8105 struct rpc_message msg = {
8106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8111 struct nfs4_call_sync_data data = {
8112 .seq_server = server,
8113 .seq_args = &args.seq_args,
8114 .seq_res = &res.seq_res,
8116 struct rpc_task_setup task_setup_data = {
8118 .rpc_message = &msg,
8119 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
8120 .callback_data = &data,
8121 .flags = RPC_TASK_NO_ROUND_ROBIN,
8125 nfs_fattr_init(locations->fattr);
8126 locations->server = server;
8127 locations->nlocations = 0;
8129 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8130 status = nfs4_call_sync_custom(&task_setup_data);
8131 if (status == NFS4_OK &&
8132 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8133 status = -NFS4ERR_LEASE_MOVED;
8137 #endif /* CONFIG_NFS_V4_1 */
8140 * nfs4_proc_get_locations - discover locations for a migrated FSID
8141 * @server: pointer to nfs_server to process
8142 * @fhandle: pointer to the kernel NFS client file handle
8143 * @locations: result of query
8145 * @cred: credential to use for this operation
8147 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
8148 * operation failed, or a negative errno if a local error occurred.
8150 * On success, "locations" is filled in, but if the server has
8151 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
8154 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
8155 * from this client that require migration recovery.
8157 int nfs4_proc_get_locations(struct nfs_server *server,
8158 struct nfs_fh *fhandle,
8159 struct nfs4_fs_locations *locations,
8160 struct page *page, const struct cred *cred)
8162 struct nfs_client *clp = server->nfs_client;
8163 const struct nfs4_mig_recovery_ops *ops =
8164 clp->cl_mvops->mig_recovery_ops;
8165 struct nfs4_exception exception = {
8166 .interruptible = true,
8170 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8171 (unsigned long long)server->fsid.major,
8172 (unsigned long long)server->fsid.minor,
8174 nfs_display_fhandle(fhandle, __func__);
8177 status = ops->get_locations(server, fhandle, locations, page,
8179 if (status != -NFS4ERR_DELAY)
8181 nfs4_handle_exception(server, status, &exception);
8182 } while (exception.retry);
8187 * This operation also signals the server that this client is
8188 * performing "lease moved" recovery. The server can stop
8189 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
8190 * is appended to this compound to identify the client ID which is
8191 * performing recovery.
8193 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
8195 struct nfs_server *server = NFS_SERVER(inode);
8196 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8197 struct rpc_clnt *clnt = server->client;
8198 struct nfs4_fsid_present_arg args = {
8199 .fh = NFS_FH(inode),
8200 .clientid = clp->cl_clientid,
8201 .renew = 1, /* append RENEW */
8203 struct nfs4_fsid_present_res res = {
8206 struct rpc_message msg = {
8207 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8212 unsigned long now = jiffies;
8215 res.fh = nfs_alloc_fhandle();
8219 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8220 status = nfs4_call_sync_sequence(clnt, server, &msg,
8221 &args.seq_args, &res.seq_res);
8222 nfs_free_fhandle(res.fh);
8226 do_renew_lease(clp, now);
8230 #ifdef CONFIG_NFS_V4_1
8233 * This operation also signals the server that this client is
8234 * performing "lease moved" recovery. The server can stop asserting
8235 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8236 * this operation is identified in the SEQUENCE operation in this
8239 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8241 struct nfs_server *server = NFS_SERVER(inode);
8242 struct rpc_clnt *clnt = server->client;
8243 struct nfs4_fsid_present_arg args = {
8244 .fh = NFS_FH(inode),
8246 struct nfs4_fsid_present_res res = {
8248 struct rpc_message msg = {
8249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8256 res.fh = nfs_alloc_fhandle();
8260 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8261 status = nfs4_call_sync_sequence(clnt, server, &msg,
8262 &args.seq_args, &res.seq_res);
8263 nfs_free_fhandle(res.fh);
8264 if (status == NFS4_OK &&
8265 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8266 status = -NFS4ERR_LEASE_MOVED;
8270 #endif /* CONFIG_NFS_V4_1 */
8273 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8274 * @inode: inode on FSID to check
8275 * @cred: credential to use for this operation
8277 * Server indicates whether the FSID is present, moved, or not
8278 * recognized. This operation is necessary to clear a LEASE_MOVED
8279 * condition for this client ID.
8281 * Returns NFS4_OK if the FSID is present on this server,
8282 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8283 * NFS4ERR code if some error occurred on the server, or a
8284 * negative errno if a local failure occurred.
8286 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8288 struct nfs_server *server = NFS_SERVER(inode);
8289 struct nfs_client *clp = server->nfs_client;
8290 const struct nfs4_mig_recovery_ops *ops =
8291 clp->cl_mvops->mig_recovery_ops;
8292 struct nfs4_exception exception = {
8293 .interruptible = true,
8297 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8298 (unsigned long long)server->fsid.major,
8299 (unsigned long long)server->fsid.minor,
8301 nfs_display_fhandle(NFS_FH(inode), __func__);
8304 status = ops->fsid_present(inode, cred);
8305 if (status != -NFS4ERR_DELAY)
8307 nfs4_handle_exception(server, status, &exception);
8308 } while (exception.retry);
8313 * If 'use_integrity' is true and the state managment nfs_client
8314 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8315 * and the machine credential as per RFC3530bis and RFC5661 Security
8316 * Considerations sections. Otherwise, just use the user cred with the
8317 * filesystem's rpc_client.
8319 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8322 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8323 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8324 struct nfs4_secinfo_arg args = {
8325 .dir_fh = NFS_FH(dir),
8328 struct nfs4_secinfo_res res = {
8331 struct rpc_message msg = {
8332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8336 struct nfs4_call_sync_data data = {
8337 .seq_server = NFS_SERVER(dir),
8338 .seq_args = &args.seq_args,
8339 .seq_res = &res.seq_res,
8341 struct rpc_task_setup task_setup = {
8343 .rpc_message = &msg,
8344 .callback_ops = clp->cl_mvops->call_sync_ops,
8345 .callback_data = &data,
8346 .flags = RPC_TASK_NO_ROUND_ROBIN,
8348 const struct cred *cred = NULL;
8350 if (use_integrity) {
8351 clnt = clp->cl_rpcclient;
8352 task_setup.rpc_client = clnt;
8354 cred = nfs4_get_clid_cred(clp);
8355 msg.rpc_cred = cred;
8358 dprintk("NFS call secinfo %s\n", name->name);
8360 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8361 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8362 status = nfs4_call_sync_custom(&task_setup);
8364 dprintk("NFS reply secinfo: %d\n", status);
8370 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8371 struct nfs4_secinfo_flavors *flavors)
8373 struct nfs4_exception exception = {
8374 .interruptible = true,
8378 err = -NFS4ERR_WRONGSEC;
8380 /* try to use integrity protection with machine cred */
8381 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8382 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8385 * if unable to use integrity protection, or SECINFO with
8386 * integrity protection returns NFS4ERR_WRONGSEC (which is
8387 * disallowed by spec, but exists in deployed servers) use
8388 * the current filesystem's rpc_client and the user cred.
8390 if (err == -NFS4ERR_WRONGSEC)
8391 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8393 trace_nfs4_secinfo(dir, name, err);
8394 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8396 } while (exception.retry);
8400 #ifdef CONFIG_NFS_V4_1
8402 * Check the exchange flags returned by the server for invalid flags, having
8403 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8406 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8408 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8410 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8412 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8413 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8415 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8419 return -NFS4ERR_INVAL;
8423 nfs41_same_server_scope(struct nfs41_server_scope *a,
8424 struct nfs41_server_scope *b)
8426 if (a->server_scope_sz != b->server_scope_sz)
8428 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8432 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8434 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8435 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8436 struct nfs_client *clp = args->client;
8438 switch (task->tk_status) {
8439 case -NFS4ERR_BADSESSION:
8440 case -NFS4ERR_DEADSESSION:
8441 nfs4_schedule_session_recovery(clp->cl_session,
8445 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8446 res->dir != NFS4_CDFS4_BOTH) {
8447 rpc_task_close_connection(task);
8448 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8449 rpc_restart_call(task);
8453 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8454 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8458 * nfs4_proc_bind_one_conn_to_session()
8460 * The 4.1 client currently uses the same TCP connection for the
8461 * fore and backchannel.
8464 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8465 struct rpc_xprt *xprt,
8466 struct nfs_client *clp,
8467 const struct cred *cred)
8470 struct nfs41_bind_conn_to_session_args args = {
8472 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8475 struct nfs41_bind_conn_to_session_res res;
8476 struct rpc_message msg = {
8478 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8483 struct rpc_task_setup task_setup_data = {
8486 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8487 .rpc_message = &msg,
8488 .flags = RPC_TASK_TIMEOUT,
8490 struct rpc_task *task;
8492 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8493 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8494 args.dir = NFS4_CDFC4_FORE;
8496 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8497 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8498 args.dir = NFS4_CDFC4_FORE;
8500 task = rpc_run_task(&task_setup_data);
8501 if (!IS_ERR(task)) {
8502 status = task->tk_status;
8505 status = PTR_ERR(task);
8506 trace_nfs4_bind_conn_to_session(clp, status);
8508 if (memcmp(res.sessionid.data,
8509 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8510 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8513 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8514 dprintk("NFS: %s: Unexpected direction from server\n",
8518 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8519 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8528 struct rpc_bind_conn_calldata {
8529 struct nfs_client *clp;
8530 const struct cred *cred;
8534 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8535 struct rpc_xprt *xprt,
8538 struct rpc_bind_conn_calldata *p = calldata;
8540 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8543 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8545 struct rpc_bind_conn_calldata data = {
8549 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8550 nfs4_proc_bind_conn_to_session_callback, &data);
8554 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8555 * and operations we'd like to see to enable certain features in the allow map
8557 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8558 .how = SP4_MACH_CRED,
8559 .enforce.u.words = {
8560 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8561 1 << (OP_EXCHANGE_ID - 32) |
8562 1 << (OP_CREATE_SESSION - 32) |
8563 1 << (OP_DESTROY_SESSION - 32) |
8564 1 << (OP_DESTROY_CLIENTID - 32)
8567 [0] = 1 << (OP_CLOSE) |
8568 1 << (OP_OPEN_DOWNGRADE) |
8570 1 << (OP_DELEGRETURN) |
8572 [1] = 1 << (OP_SECINFO - 32) |
8573 1 << (OP_SECINFO_NO_NAME - 32) |
8574 1 << (OP_LAYOUTRETURN - 32) |
8575 1 << (OP_TEST_STATEID - 32) |
8576 1 << (OP_FREE_STATEID - 32) |
8577 1 << (OP_WRITE - 32)
8582 * Select the state protection mode for client `clp' given the server results
8583 * from exchange_id in `sp'.
8585 * Returns 0 on success, negative errno otherwise.
8587 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8588 struct nfs41_state_protection *sp)
8590 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8591 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8592 1 << (OP_EXCHANGE_ID - 32) |
8593 1 << (OP_CREATE_SESSION - 32) |
8594 1 << (OP_DESTROY_SESSION - 32) |
8595 1 << (OP_DESTROY_CLIENTID - 32)
8597 unsigned long flags = 0;
8601 if (sp->how == SP4_MACH_CRED) {
8602 /* Print state protect result */
8603 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8604 for (i = 0; i <= LAST_NFS4_OP; i++) {
8605 if (test_bit(i, sp->enforce.u.longs))
8606 dfprintk(MOUNT, " enforce op %d\n", i);
8607 if (test_bit(i, sp->allow.u.longs))
8608 dfprintk(MOUNT, " allow op %d\n", i);
8611 /* make sure nothing is on enforce list that isn't supported */
8612 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8613 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8614 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8621 * Minimal mode - state operations are allowed to use machine
8622 * credential. Note this already happens by default, so the
8623 * client doesn't have to do anything more than the negotiation.
8625 * NOTE: we don't care if EXCHANGE_ID is in the list -
8626 * we're already using the machine cred for exchange_id
8627 * and will never use a different cred.
8629 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8630 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8631 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8632 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8633 dfprintk(MOUNT, "sp4_mach_cred:\n");
8634 dfprintk(MOUNT, " minimal mode enabled\n");
8635 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8637 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8642 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8643 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8644 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8645 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8646 dfprintk(MOUNT, " cleanup mode enabled\n");
8647 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8650 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8651 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8652 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8655 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8656 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8657 dfprintk(MOUNT, " secinfo mode enabled\n");
8658 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8661 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8662 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8663 dfprintk(MOUNT, " stateid mode enabled\n");
8664 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8667 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8668 dfprintk(MOUNT, " write mode enabled\n");
8669 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8672 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8673 dfprintk(MOUNT, " commit mode enabled\n");
8674 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8678 clp->cl_sp4_flags = flags;
8682 struct nfs41_exchange_id_data {
8683 struct nfs41_exchange_id_res res;
8684 struct nfs41_exchange_id_args args;
8687 static void nfs4_exchange_id_release(void *data)
8689 struct nfs41_exchange_id_data *cdata =
8690 (struct nfs41_exchange_id_data *)data;
8692 nfs_put_client(cdata->args.client);
8693 kfree(cdata->res.impl_id);
8694 kfree(cdata->res.server_scope);
8695 kfree(cdata->res.server_owner);
8699 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8700 .rpc_release = nfs4_exchange_id_release,
8704 * _nfs4_proc_exchange_id()
8706 * Wrapper for EXCHANGE_ID operation.
8708 static struct rpc_task *
8709 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8710 u32 sp4_how, struct rpc_xprt *xprt)
8712 struct rpc_message msg = {
8713 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8716 struct rpc_task_setup task_setup_data = {
8717 .rpc_client = clp->cl_rpcclient,
8718 .callback_ops = &nfs4_exchange_id_call_ops,
8719 .rpc_message = &msg,
8720 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8722 struct nfs41_exchange_id_data *calldata;
8725 if (!refcount_inc_not_zero(&clp->cl_count))
8726 return ERR_PTR(-EIO);
8729 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8733 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8735 status = nfs4_init_uniform_client_string(clp);
8739 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8742 if (unlikely(calldata->res.server_owner == NULL))
8745 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8747 if (unlikely(calldata->res.server_scope == NULL))
8748 goto out_server_owner;
8750 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8751 if (unlikely(calldata->res.impl_id == NULL))
8752 goto out_server_scope;
8756 calldata->args.state_protect.how = SP4_NONE;
8760 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8770 task_setup_data.rpc_xprt = xprt;
8771 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8772 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8773 sizeof(calldata->args.verifier.data));
8775 calldata->args.client = clp;
8776 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8777 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8778 #ifdef CONFIG_NFS_V4_1_MIGRATION
8779 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8781 msg.rpc_argp = &calldata->args;
8782 msg.rpc_resp = &calldata->res;
8783 task_setup_data.callback_data = calldata;
8785 return rpc_run_task(&task_setup_data);
8788 kfree(calldata->res.impl_id);
8790 kfree(calldata->res.server_scope);
8792 kfree(calldata->res.server_owner);
8796 nfs_put_client(clp);
8797 return ERR_PTR(status);
8801 * _nfs4_proc_exchange_id()
8803 * Wrapper for EXCHANGE_ID operation.
8805 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8808 struct rpc_task *task;
8809 struct nfs41_exchange_id_args *argp;
8810 struct nfs41_exchange_id_res *resp;
8811 unsigned long now = jiffies;
8814 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8816 return PTR_ERR(task);
8818 argp = task->tk_msg.rpc_argp;
8819 resp = task->tk_msg.rpc_resp;
8820 status = task->tk_status;
8824 status = nfs4_check_cl_exchange_flags(resp->flags,
8825 clp->cl_mvops->minor_version);
8829 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8833 do_renew_lease(clp, now);
8835 clp->cl_clientid = resp->clientid;
8836 clp->cl_exchange_flags = resp->flags;
8837 clp->cl_seqid = resp->seqid;
8838 /* Client ID is not confirmed */
8839 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8840 clear_bit(NFS4_SESSION_ESTABLISHED,
8841 &clp->cl_session->session_state);
8843 if (clp->cl_serverscope != NULL &&
8844 !nfs41_same_server_scope(clp->cl_serverscope,
8845 resp->server_scope)) {
8846 dprintk("%s: server_scope mismatch detected\n",
8848 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8851 swap(clp->cl_serverowner, resp->server_owner);
8852 swap(clp->cl_serverscope, resp->server_scope);
8853 swap(clp->cl_implid, resp->impl_id);
8855 /* Save the EXCHANGE_ID verifier session trunk tests */
8856 memcpy(clp->cl_confirm.data, argp->verifier.data,
8857 sizeof(clp->cl_confirm.data));
8859 trace_nfs4_exchange_id(clp, status);
8865 * nfs4_proc_exchange_id()
8867 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8869 * Since the clientid has expired, all compounds using sessions
8870 * associated with the stale clientid will be returning
8871 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8872 * be in some phase of session reset.
8874 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8876 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8878 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8881 /* try SP4_MACH_CRED if krb5i/p */
8882 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8883 authflavor == RPC_AUTH_GSS_KRB5P) {
8884 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8890 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8894 * nfs4_test_session_trunk
8896 * This is an add_xprt_test() test function called from
8897 * rpc_clnt_setup_test_and_add_xprt.
8899 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8900 * and is dereferrenced in nfs4_exchange_id_release
8902 * Upon success, add the new transport to the rpc_clnt
8904 * @clnt: struct rpc_clnt to get new transport
8905 * @xprt: the rpc_xprt to test
8906 * @data: call data for _nfs4_proc_exchange_id.
8908 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8911 struct nfs4_add_xprt_data *adata = data;
8912 struct rpc_task *task;
8917 dprintk("--> %s try %s\n", __func__,
8918 xprt->address_strings[RPC_DISPLAY_ADDR]);
8920 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8922 /* Test connection for session trunking. Async exchange_id call */
8923 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8927 status = task->tk_status;
8929 status = nfs4_detect_session_trunking(adata->clp,
8930 task->tk_msg.rpc_resp, xprt);
8933 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8934 else if (rpc_clnt_xprt_switch_has_addr(clnt,
8935 (struct sockaddr *)&xprt->addr))
8936 rpc_clnt_xprt_switch_remove_xprt(clnt, xprt);
8940 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8942 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8943 const struct cred *cred)
8945 struct rpc_message msg = {
8946 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8952 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8953 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8954 trace_nfs4_destroy_clientid(clp, status);
8956 dprintk("NFS: Got error %d from the server %s on "
8957 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8961 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8962 const struct cred *cred)
8967 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8968 ret = _nfs4_proc_destroy_clientid(clp, cred);
8970 case -NFS4ERR_DELAY:
8971 case -NFS4ERR_CLIENTID_BUSY:
8981 int nfs4_destroy_clientid(struct nfs_client *clp)
8983 const struct cred *cred;
8986 if (clp->cl_mvops->minor_version < 1)
8988 if (clp->cl_exchange_flags == 0)
8990 if (clp->cl_preserve_clid)
8992 cred = nfs4_get_clid_cred(clp);
8993 ret = nfs4_proc_destroy_clientid(clp, cred);
8997 case -NFS4ERR_STALE_CLIENTID:
8998 clp->cl_exchange_flags = 0;
9004 #endif /* CONFIG_NFS_V4_1 */
9006 struct nfs4_get_lease_time_data {
9007 struct nfs4_get_lease_time_args *args;
9008 struct nfs4_get_lease_time_res *res;
9009 struct nfs_client *clp;
9012 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
9015 struct nfs4_get_lease_time_data *data =
9016 (struct nfs4_get_lease_time_data *)calldata;
9018 /* just setup sequence, do not trigger session recovery
9019 since we're invoked within one */
9020 nfs4_setup_sequence(data->clp,
9021 &data->args->la_seq_args,
9022 &data->res->lr_seq_res,
9027 * Called from nfs4_state_manager thread for session setup, so don't recover
9028 * from sequence operation or clientid errors.
9030 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
9032 struct nfs4_get_lease_time_data *data =
9033 (struct nfs4_get_lease_time_data *)calldata;
9035 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
9037 switch (task->tk_status) {
9038 case -NFS4ERR_DELAY:
9039 case -NFS4ERR_GRACE:
9040 rpc_delay(task, NFS4_POLL_RETRY_MIN);
9041 task->tk_status = 0;
9043 case -NFS4ERR_RETRY_UNCACHED_REP:
9044 rpc_restart_call_prepare(task);
9049 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
9050 .rpc_call_prepare = nfs4_get_lease_time_prepare,
9051 .rpc_call_done = nfs4_get_lease_time_done,
9054 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
9056 struct nfs4_get_lease_time_args args;
9057 struct nfs4_get_lease_time_res res = {
9058 .lr_fsinfo = fsinfo,
9060 struct nfs4_get_lease_time_data data = {
9065 struct rpc_message msg = {
9066 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
9070 struct rpc_task_setup task_setup = {
9071 .rpc_client = clp->cl_rpcclient,
9072 .rpc_message = &msg,
9073 .callback_ops = &nfs4_get_lease_time_ops,
9074 .callback_data = &data,
9075 .flags = RPC_TASK_TIMEOUT,
9078 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
9079 return nfs4_call_sync_custom(&task_setup);
9082 #ifdef CONFIG_NFS_V4_1
9085 * Initialize the values to be used by the client in CREATE_SESSION
9086 * If nfs4_init_session set the fore channel request and response sizes,
9089 * Set the back channel max_resp_sz_cached to zero to force the client to
9090 * always set csa_cachethis to FALSE because the current implementation
9091 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
9093 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
9094 struct rpc_clnt *clnt)
9096 unsigned int max_rqst_sz, max_resp_sz;
9097 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
9098 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
9100 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
9101 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
9103 /* Fore channel attributes */
9104 args->fc_attrs.max_rqst_sz = max_rqst_sz;
9105 args->fc_attrs.max_resp_sz = max_resp_sz;
9106 args->fc_attrs.max_ops = NFS4_MAX_OPS;
9107 args->fc_attrs.max_reqs = max_session_slots;
9109 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
9110 "max_ops=%u max_reqs=%u\n",
9112 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
9113 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
9115 /* Back channel attributes */
9116 args->bc_attrs.max_rqst_sz = max_bc_payload;
9117 args->bc_attrs.max_resp_sz = max_bc_payload;
9118 args->bc_attrs.max_resp_sz_cached = 0;
9119 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
9120 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
9121 if (args->bc_attrs.max_reqs > max_bc_slots)
9122 args->bc_attrs.max_reqs = max_bc_slots;
9124 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
9125 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
9127 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
9128 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
9129 args->bc_attrs.max_reqs);
9132 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
9133 struct nfs41_create_session_res *res)
9135 struct nfs4_channel_attrs *sent = &args->fc_attrs;
9136 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
9138 if (rcvd->max_resp_sz > sent->max_resp_sz)
9141 * Our requested max_ops is the minimum we need; we're not
9142 * prepared to break up compounds into smaller pieces than that.
9143 * So, no point even trying to continue if the server won't
9146 if (rcvd->max_ops < sent->max_ops)
9148 if (rcvd->max_reqs == 0)
9150 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
9151 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
9155 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
9156 struct nfs41_create_session_res *res)
9158 struct nfs4_channel_attrs *sent = &args->bc_attrs;
9159 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
9161 if (!(res->flags & SESSION4_BACK_CHAN))
9163 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
9165 if (rcvd->max_resp_sz < sent->max_resp_sz)
9167 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
9169 if (rcvd->max_ops > sent->max_ops)
9171 if (rcvd->max_reqs > sent->max_reqs)
9177 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
9178 struct nfs41_create_session_res *res)
9182 ret = nfs4_verify_fore_channel_attrs(args, res);
9185 return nfs4_verify_back_channel_attrs(args, res);
9188 static void nfs4_update_session(struct nfs4_session *session,
9189 struct nfs41_create_session_res *res)
9191 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
9192 /* Mark client id and session as being confirmed */
9193 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
9194 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
9195 session->flags = res->flags;
9196 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
9197 if (res->flags & SESSION4_BACK_CHAN)
9198 memcpy(&session->bc_attrs, &res->bc_attrs,
9199 sizeof(session->bc_attrs));
9202 static int _nfs4_proc_create_session(struct nfs_client *clp,
9203 const struct cred *cred)
9205 struct nfs4_session *session = clp->cl_session;
9206 struct nfs41_create_session_args args = {
9208 .clientid = clp->cl_clientid,
9209 .seqid = clp->cl_seqid,
9210 .cb_program = NFS4_CALLBACK,
9212 struct nfs41_create_session_res res;
9214 struct rpc_message msg = {
9215 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9222 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
9223 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
9225 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9226 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9227 trace_nfs4_create_session(clp, status);
9230 case -NFS4ERR_STALE_CLIENTID:
9231 case -NFS4ERR_DELAY:
9240 /* Verify the session's negotiated channel_attrs values */
9241 status = nfs4_verify_channel_attrs(&args, &res);
9242 /* Increment the clientid slot sequence id */
9245 nfs4_update_session(session, &res);
9252 * Issues a CREATE_SESSION operation to the server.
9253 * It is the responsibility of the caller to verify the session is
9254 * expired before calling this routine.
9256 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9260 struct nfs4_session *session = clp->cl_session;
9261 struct nfs4_add_xprt_data xprtdata = {
9264 struct rpc_add_xprt_test rpcdata = {
9265 .add_xprt_test = clp->cl_mvops->session_trunk,
9269 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9271 status = _nfs4_proc_create_session(clp, cred);
9275 /* Init or reset the session slot tables */
9276 status = nfs4_setup_session_slot_tables(session);
9277 dprintk("slot table setup returned %d\n", status);
9281 ptr = (unsigned *)&session->sess_id.data[0];
9282 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9283 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9284 rpc_clnt_probe_trunked_xprts(clp->cl_rpcclient, &rpcdata);
9290 * Issue the over-the-wire RPC DESTROY_SESSION.
9291 * The caller must serialize access to this routine.
9293 int nfs4_proc_destroy_session(struct nfs4_session *session,
9294 const struct cred *cred)
9296 struct rpc_message msg = {
9297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9298 .rpc_argp = session,
9303 /* session is still being setup */
9304 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9307 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9308 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9309 trace_nfs4_destroy_session(session->clp, status);
9312 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9313 "Session has been destroyed regardless...\n", status);
9314 rpc_clnt_manage_trunked_xprts(session->clp->cl_rpcclient);
9319 * Renew the cl_session lease.
9321 struct nfs4_sequence_data {
9322 struct nfs_client *clp;
9323 struct nfs4_sequence_args args;
9324 struct nfs4_sequence_res res;
9327 static void nfs41_sequence_release(void *data)
9329 struct nfs4_sequence_data *calldata = data;
9330 struct nfs_client *clp = calldata->clp;
9332 if (refcount_read(&clp->cl_count) > 1)
9333 nfs4_schedule_state_renewal(clp);
9334 nfs_put_client(clp);
9338 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9340 switch(task->tk_status) {
9341 case -NFS4ERR_DELAY:
9342 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9345 nfs4_schedule_lease_recovery(clp);
9350 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9352 struct nfs4_sequence_data *calldata = data;
9353 struct nfs_client *clp = calldata->clp;
9355 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9358 trace_nfs4_sequence(clp, task->tk_status);
9359 if (task->tk_status < 0) {
9360 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9361 if (refcount_read(&clp->cl_count) == 1)
9364 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9365 rpc_restart_call_prepare(task);
9369 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9372 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9374 struct nfs4_sequence_data *calldata = data;
9375 struct nfs_client *clp = calldata->clp;
9376 struct nfs4_sequence_args *args;
9377 struct nfs4_sequence_res *res;
9379 args = task->tk_msg.rpc_argp;
9380 res = task->tk_msg.rpc_resp;
9382 nfs4_setup_sequence(clp, args, res, task);
9385 static const struct rpc_call_ops nfs41_sequence_ops = {
9386 .rpc_call_done = nfs41_sequence_call_done,
9387 .rpc_call_prepare = nfs41_sequence_prepare,
9388 .rpc_release = nfs41_sequence_release,
9391 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9392 const struct cred *cred,
9393 struct nfs4_slot *slot,
9396 struct nfs4_sequence_data *calldata;
9397 struct rpc_message msg = {
9398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9401 struct rpc_task_setup task_setup_data = {
9402 .rpc_client = clp->cl_rpcclient,
9403 .rpc_message = &msg,
9404 .callback_ops = &nfs41_sequence_ops,
9405 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
9407 struct rpc_task *ret;
9409 ret = ERR_PTR(-EIO);
9410 if (!refcount_inc_not_zero(&clp->cl_count))
9413 ret = ERR_PTR(-ENOMEM);
9414 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
9415 if (calldata == NULL)
9417 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9418 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9419 msg.rpc_argp = &calldata->args;
9420 msg.rpc_resp = &calldata->res;
9421 calldata->clp = clp;
9422 task_setup_data.callback_data = calldata;
9424 ret = rpc_run_task(&task_setup_data);
9429 nfs_put_client(clp);
9431 nfs41_release_slot(slot);
9435 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9437 struct rpc_task *task;
9440 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9442 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9444 ret = PTR_ERR(task);
9446 rpc_put_task_async(task);
9447 dprintk("<-- %s status=%d\n", __func__, ret);
9451 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9453 struct rpc_task *task;
9456 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9458 ret = PTR_ERR(task);
9461 ret = rpc_wait_for_completion_task(task);
9463 ret = task->tk_status;
9466 dprintk("<-- %s status=%d\n", __func__, ret);
9470 struct nfs4_reclaim_complete_data {
9471 struct nfs_client *clp;
9472 struct nfs41_reclaim_complete_args arg;
9473 struct nfs41_reclaim_complete_res res;
9476 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9478 struct nfs4_reclaim_complete_data *calldata = data;
9480 nfs4_setup_sequence(calldata->clp,
9481 &calldata->arg.seq_args,
9482 &calldata->res.seq_res,
9486 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9488 switch(task->tk_status) {
9490 wake_up_all(&clp->cl_lock_waitq);
9492 case -NFS4ERR_COMPLETE_ALREADY:
9493 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9495 case -NFS4ERR_DELAY:
9496 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9498 case -NFS4ERR_RETRY_UNCACHED_REP:
9500 dprintk("%s: failed to reclaim complete error %d for server %s, retrying\n",
9501 __func__, task->tk_status, clp->cl_hostname);
9503 case -NFS4ERR_BADSESSION:
9504 case -NFS4ERR_DEADSESSION:
9505 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9508 nfs4_schedule_lease_recovery(clp);
9513 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9515 struct nfs4_reclaim_complete_data *calldata = data;
9516 struct nfs_client *clp = calldata->clp;
9517 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9519 if (!nfs41_sequence_done(task, res))
9522 trace_nfs4_reclaim_complete(clp, task->tk_status);
9523 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9524 rpc_restart_call_prepare(task);
9529 static void nfs4_free_reclaim_complete_data(void *data)
9531 struct nfs4_reclaim_complete_data *calldata = data;
9536 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9537 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9538 .rpc_call_done = nfs4_reclaim_complete_done,
9539 .rpc_release = nfs4_free_reclaim_complete_data,
9543 * Issue a global reclaim complete.
9545 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9546 const struct cred *cred)
9548 struct nfs4_reclaim_complete_data *calldata;
9549 struct rpc_message msg = {
9550 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9553 struct rpc_task_setup task_setup_data = {
9554 .rpc_client = clp->cl_rpcclient,
9555 .rpc_message = &msg,
9556 .callback_ops = &nfs4_reclaim_complete_call_ops,
9557 .flags = RPC_TASK_NO_ROUND_ROBIN,
9559 int status = -ENOMEM;
9561 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9562 if (calldata == NULL)
9564 calldata->clp = clp;
9565 calldata->arg.one_fs = 0;
9567 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9568 msg.rpc_argp = &calldata->arg;
9569 msg.rpc_resp = &calldata->res;
9570 task_setup_data.callback_data = calldata;
9571 status = nfs4_call_sync_custom(&task_setup_data);
9573 dprintk("<-- %s status=%d\n", __func__, status);
9578 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9580 struct nfs4_layoutget *lgp = calldata;
9581 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9583 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9584 &lgp->res.seq_res, task);
9587 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9589 struct nfs4_layoutget *lgp = calldata;
9591 nfs41_sequence_process(task, &lgp->res.seq_res);
9595 nfs4_layoutget_handle_exception(struct rpc_task *task,
9596 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9598 struct inode *inode = lgp->args.inode;
9599 struct nfs_server *server = NFS_SERVER(inode);
9600 struct pnfs_layout_hdr *lo = lgp->lo;
9601 int nfs4err = task->tk_status;
9602 int err, status = 0;
9605 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9607 nfs4_sequence_free_slot(&lgp->res.seq_res);
9614 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9615 * on the file. set tk_status to -ENODATA to tell upper layer to
9618 case -NFS4ERR_LAYOUTUNAVAILABLE:
9622 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9623 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9625 case -NFS4ERR_BADLAYOUT:
9626 status = -EOVERFLOW;
9629 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9630 * (or clients) writing to the same RAID stripe except when
9631 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9633 * Treat it like we would RECALLCONFLICT -- we retry for a little
9634 * while, and then eventually give up.
9636 case -NFS4ERR_LAYOUTTRYLATER:
9637 if (lgp->args.minlength == 0) {
9638 status = -EOVERFLOW;
9643 case -NFS4ERR_RECALLCONFLICT:
9644 status = -ERECALLCONFLICT;
9646 case -NFS4ERR_DELEG_REVOKED:
9647 case -NFS4ERR_ADMIN_REVOKED:
9648 case -NFS4ERR_EXPIRED:
9649 case -NFS4ERR_BAD_STATEID:
9650 exception->timeout = 0;
9651 spin_lock(&inode->i_lock);
9652 /* If the open stateid was bad, then recover it. */
9653 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9654 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9655 spin_unlock(&inode->i_lock);
9656 exception->state = lgp->args.ctx->state;
9657 exception->stateid = &lgp->args.stateid;
9662 * Mark the bad layout state as invalid, then retry
9664 pnfs_mark_layout_stateid_invalid(lo, &head);
9665 spin_unlock(&inode->i_lock);
9666 nfs_commit_inode(inode, 0);
9667 pnfs_free_lseg_list(&head);
9672 err = nfs4_handle_exception(server, nfs4err, exception);
9674 if (exception->retry)
9683 size_t max_response_pages(struct nfs_server *server)
9685 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9686 return nfs_page_array_len(0, max_resp_sz);
9689 static void nfs4_layoutget_release(void *calldata)
9691 struct nfs4_layoutget *lgp = calldata;
9693 nfs4_sequence_free_slot(&lgp->res.seq_res);
9694 pnfs_layoutget_free(lgp);
9697 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9698 .rpc_call_prepare = nfs4_layoutget_prepare,
9699 .rpc_call_done = nfs4_layoutget_done,
9700 .rpc_release = nfs4_layoutget_release,
9703 struct pnfs_layout_segment *
9704 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9706 struct inode *inode = lgp->args.inode;
9707 struct nfs_server *server = NFS_SERVER(inode);
9708 struct rpc_task *task;
9709 struct rpc_message msg = {
9710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9711 .rpc_argp = &lgp->args,
9712 .rpc_resp = &lgp->res,
9713 .rpc_cred = lgp->cred,
9715 struct rpc_task_setup task_setup_data = {
9716 .rpc_client = server->client,
9717 .rpc_message = &msg,
9718 .callback_ops = &nfs4_layoutget_call_ops,
9719 .callback_data = lgp,
9720 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
9723 struct pnfs_layout_segment *lseg = NULL;
9724 struct nfs4_exception exception = {
9726 .timeout = *timeout,
9730 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9732 task = rpc_run_task(&task_setup_data);
9734 return ERR_CAST(task);
9736 status = rpc_wait_for_completion_task(task);
9740 if (task->tk_status < 0) {
9741 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9742 *timeout = exception.timeout;
9743 } else if (lgp->res.layoutp->len == 0) {
9745 *timeout = nfs4_update_delay(&exception.timeout);
9747 lseg = pnfs_layout_process(lgp);
9749 trace_nfs4_layoutget(lgp->args.ctx,
9756 dprintk("<-- %s status=%d\n", __func__, status);
9758 return ERR_PTR(status);
9763 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9765 struct nfs4_layoutreturn *lrp = calldata;
9767 nfs4_setup_sequence(lrp->clp,
9768 &lrp->args.seq_args,
9771 if (!pnfs_layout_is_valid(lrp->args.layout))
9775 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9777 struct nfs4_layoutreturn *lrp = calldata;
9778 struct nfs_server *server;
9780 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9784 * Was there an RPC level error? Assume the call succeeded,
9785 * and that we need to release the layout
9787 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9788 lrp->res.lrs_present = 0;
9792 server = NFS_SERVER(lrp->args.inode);
9793 switch (task->tk_status) {
9794 case -NFS4ERR_OLD_STATEID:
9795 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9801 task->tk_status = 0;
9805 case -NFS4ERR_DELAY:
9806 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9812 task->tk_status = 0;
9813 nfs4_sequence_free_slot(&lrp->res.seq_res);
9814 rpc_restart_call_prepare(task);
9817 static void nfs4_layoutreturn_release(void *calldata)
9819 struct nfs4_layoutreturn *lrp = calldata;
9820 struct pnfs_layout_hdr *lo = lrp->args.layout;
9822 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9823 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9824 nfs4_sequence_free_slot(&lrp->res.seq_res);
9825 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9826 lrp->ld_private.ops->free(&lrp->ld_private);
9827 pnfs_put_layout_hdr(lrp->args.layout);
9828 nfs_iput_and_deactive(lrp->inode);
9829 put_cred(lrp->cred);
9833 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9834 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9835 .rpc_call_done = nfs4_layoutreturn_done,
9836 .rpc_release = nfs4_layoutreturn_release,
9839 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9841 struct rpc_task *task;
9842 struct rpc_message msg = {
9843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9844 .rpc_argp = &lrp->args,
9845 .rpc_resp = &lrp->res,
9846 .rpc_cred = lrp->cred,
9848 struct rpc_task_setup task_setup_data = {
9849 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9850 .rpc_message = &msg,
9851 .callback_ops = &nfs4_layoutreturn_call_ops,
9852 .callback_data = lrp,
9853 .flags = RPC_TASK_MOVEABLE,
9857 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9858 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9859 &task_setup_data.rpc_client, &msg);
9861 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9864 nfs4_layoutreturn_release(lrp);
9867 task_setup_data.flags |= RPC_TASK_ASYNC;
9870 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9873 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9875 task = rpc_run_task(&task_setup_data);
9877 return PTR_ERR(task);
9879 status = task->tk_status;
9880 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9881 dprintk("<-- %s status=%d\n", __func__, status);
9887 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9888 struct pnfs_device *pdev,
9889 const struct cred *cred)
9891 struct nfs4_getdeviceinfo_args args = {
9893 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9894 NOTIFY_DEVICEID4_DELETE,
9896 struct nfs4_getdeviceinfo_res res = {
9899 struct rpc_message msg = {
9900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9907 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9908 if (res.notification & ~args.notify_types)
9909 dprintk("%s: unsupported notification\n", __func__);
9910 if (res.notification != args.notify_types)
9913 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9915 dprintk("<-- %s status=%d\n", __func__, status);
9920 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9921 struct pnfs_device *pdev,
9922 const struct cred *cred)
9924 struct nfs4_exception exception = { };
9928 err = nfs4_handle_exception(server,
9929 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9931 } while (exception.retry);
9934 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9936 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9938 struct nfs4_layoutcommit_data *data = calldata;
9939 struct nfs_server *server = NFS_SERVER(data->args.inode);
9941 nfs4_setup_sequence(server->nfs_client,
9942 &data->args.seq_args,
9948 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9950 struct nfs4_layoutcommit_data *data = calldata;
9951 struct nfs_server *server = NFS_SERVER(data->args.inode);
9953 if (!nfs41_sequence_done(task, &data->res.seq_res))
9956 switch (task->tk_status) { /* Just ignore these failures */
9957 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9958 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9959 case -NFS4ERR_BADLAYOUT: /* no layout */
9960 case -NFS4ERR_GRACE: /* loca_recalim always false */
9961 task->tk_status = 0;
9966 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9967 rpc_restart_call_prepare(task);
9973 static void nfs4_layoutcommit_release(void *calldata)
9975 struct nfs4_layoutcommit_data *data = calldata;
9977 pnfs_cleanup_layoutcommit(data);
9978 nfs_post_op_update_inode_force_wcc(data->args.inode,
9980 put_cred(data->cred);
9981 nfs_iput_and_deactive(data->inode);
9985 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9986 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9987 .rpc_call_done = nfs4_layoutcommit_done,
9988 .rpc_release = nfs4_layoutcommit_release,
9992 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9994 struct rpc_message msg = {
9995 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9996 .rpc_argp = &data->args,
9997 .rpc_resp = &data->res,
9998 .rpc_cred = data->cred,
10000 struct rpc_task_setup task_setup_data = {
10001 .task = &data->task,
10002 .rpc_client = NFS_CLIENT(data->args.inode),
10003 .rpc_message = &msg,
10004 .callback_ops = &nfs4_layoutcommit_ops,
10005 .callback_data = data,
10006 .flags = RPC_TASK_MOVEABLE,
10008 struct rpc_task *task;
10011 dprintk("NFS: initiating layoutcommit call. sync %d "
10012 "lbw: %llu inode %lu\n", sync,
10013 data->args.lastbytewritten,
10014 data->args.inode->i_ino);
10017 data->inode = nfs_igrab_and_active(data->args.inode);
10018 if (data->inode == NULL) {
10019 nfs4_layoutcommit_release(data);
10022 task_setup_data.flags = RPC_TASK_ASYNC;
10024 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
10025 task = rpc_run_task(&task_setup_data);
10027 return PTR_ERR(task);
10029 status = task->tk_status;
10030 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
10031 dprintk("%s: status %d\n", __func__, status);
10032 rpc_put_task(task);
10037 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
10038 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
10041 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10042 struct nfs_fsinfo *info,
10043 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
10045 struct nfs41_secinfo_no_name_args args = {
10046 .style = SECINFO_STYLE_CURRENT_FH,
10048 struct nfs4_secinfo_res res = {
10049 .flavors = flavors,
10051 struct rpc_message msg = {
10052 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
10056 struct nfs4_call_sync_data data = {
10057 .seq_server = server,
10058 .seq_args = &args.seq_args,
10059 .seq_res = &res.seq_res,
10061 struct rpc_task_setup task_setup = {
10062 .rpc_client = server->client,
10063 .rpc_message = &msg,
10064 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
10065 .callback_data = &data,
10066 .flags = RPC_TASK_NO_ROUND_ROBIN,
10068 const struct cred *cred = NULL;
10071 if (use_integrity) {
10072 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
10074 cred = nfs4_get_clid_cred(server->nfs_client);
10075 msg.rpc_cred = cred;
10078 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
10079 status = nfs4_call_sync_custom(&task_setup);
10080 dprintk("<-- %s status=%d\n", __func__, status);
10088 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10089 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
10091 struct nfs4_exception exception = {
10092 .interruptible = true,
10096 /* first try using integrity protection */
10097 err = -NFS4ERR_WRONGSEC;
10099 /* try to use integrity protection with machine cred */
10100 if (_nfs4_is_integrity_protected(server->nfs_client))
10101 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10105 * if unable to use integrity protection, or SECINFO with
10106 * integrity protection returns NFS4ERR_WRONGSEC (which is
10107 * disallowed by spec, but exists in deployed servers) use
10108 * the current filesystem's rpc_client and the user cred.
10110 if (err == -NFS4ERR_WRONGSEC)
10111 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10116 case -NFS4ERR_WRONGSEC:
10120 err = nfs4_handle_exception(server, err, &exception);
10122 } while (exception.retry);
10128 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
10129 struct nfs_fsinfo *info)
10133 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
10134 struct nfs4_secinfo_flavors *flavors;
10135 struct nfs4_secinfo4 *secinfo;
10138 page = alloc_page(GFP_KERNEL);
10144 flavors = page_address(page);
10145 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
10148 * Fall back on "guess and check" method if
10149 * the server doesn't support SECINFO_NO_NAME
10151 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
10152 err = nfs4_find_root_sec(server, fhandle, info);
10158 for (i = 0; i < flavors->num_flavors; i++) {
10159 secinfo = &flavors->flavors[i];
10161 switch (secinfo->flavor) {
10162 case RPC_AUTH_NULL:
10163 case RPC_AUTH_UNIX:
10165 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
10166 &secinfo->flavor_info);
10169 flavor = RPC_AUTH_MAXFLAVOR;
10173 if (!nfs_auth_info_match(&server->auth_info, flavor))
10174 flavor = RPC_AUTH_MAXFLAVOR;
10176 if (flavor != RPC_AUTH_MAXFLAVOR) {
10177 err = nfs4_lookup_root_sec(server, fhandle,
10184 if (flavor == RPC_AUTH_MAXFLAVOR)
10189 if (err == -EACCES)
10195 static int _nfs41_test_stateid(struct nfs_server *server,
10196 nfs4_stateid *stateid,
10197 const struct cred *cred)
10200 struct nfs41_test_stateid_args args = {
10201 .stateid = stateid,
10203 struct nfs41_test_stateid_res res;
10204 struct rpc_message msg = {
10205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10210 struct rpc_clnt *rpc_client = server->client;
10212 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10213 &rpc_client, &msg);
10215 dprintk("NFS call test_stateid %p\n", stateid);
10216 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
10217 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
10218 &args.seq_args, &res.seq_res);
10219 if (status != NFS_OK) {
10220 dprintk("NFS reply test_stateid: failed, %d\n", status);
10223 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10224 return -res.status;
10227 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10228 int err, struct nfs4_exception *exception)
10230 exception->retry = 0;
10232 case -NFS4ERR_DELAY:
10233 case -NFS4ERR_RETRY_UNCACHED_REP:
10234 nfs4_handle_exception(server, err, exception);
10236 case -NFS4ERR_BADSESSION:
10237 case -NFS4ERR_BADSLOT:
10238 case -NFS4ERR_BAD_HIGH_SLOT:
10239 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10240 case -NFS4ERR_DEADSESSION:
10241 nfs4_do_handle_exception(server, err, exception);
10246 * nfs41_test_stateid - perform a TEST_STATEID operation
10248 * @server: server / transport on which to perform the operation
10249 * @stateid: state ID to test
10250 * @cred: credential
10252 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10253 * Otherwise a negative NFS4ERR value is returned if the operation
10254 * failed or the state ID is not currently valid.
10256 static int nfs41_test_stateid(struct nfs_server *server,
10257 nfs4_stateid *stateid,
10258 const struct cred *cred)
10260 struct nfs4_exception exception = {
10261 .interruptible = true,
10265 err = _nfs41_test_stateid(server, stateid, cred);
10266 nfs4_handle_delay_or_session_error(server, err, &exception);
10267 } while (exception.retry);
10271 struct nfs_free_stateid_data {
10272 struct nfs_server *server;
10273 struct nfs41_free_stateid_args args;
10274 struct nfs41_free_stateid_res res;
10277 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10279 struct nfs_free_stateid_data *data = calldata;
10280 nfs4_setup_sequence(data->server->nfs_client,
10281 &data->args.seq_args,
10282 &data->res.seq_res,
10286 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10288 struct nfs_free_stateid_data *data = calldata;
10290 nfs41_sequence_done(task, &data->res.seq_res);
10292 switch (task->tk_status) {
10293 case -NFS4ERR_DELAY:
10294 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10295 rpc_restart_call_prepare(task);
10299 static void nfs41_free_stateid_release(void *calldata)
10301 struct nfs_free_stateid_data *data = calldata;
10302 struct nfs_client *clp = data->server->nfs_client;
10304 nfs_put_client(clp);
10308 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10309 .rpc_call_prepare = nfs41_free_stateid_prepare,
10310 .rpc_call_done = nfs41_free_stateid_done,
10311 .rpc_release = nfs41_free_stateid_release,
10315 * nfs41_free_stateid - perform a FREE_STATEID operation
10317 * @server: server / transport on which to perform the operation
10318 * @stateid: state ID to release
10319 * @cred: credential
10320 * @privileged: set to true if this call needs to be privileged
10322 * Note: this function is always asynchronous.
10324 static int nfs41_free_stateid(struct nfs_server *server,
10325 const nfs4_stateid *stateid,
10326 const struct cred *cred,
10329 struct rpc_message msg = {
10330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10333 struct rpc_task_setup task_setup = {
10334 .rpc_client = server->client,
10335 .rpc_message = &msg,
10336 .callback_ops = &nfs41_free_stateid_ops,
10337 .flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
10339 struct nfs_free_stateid_data *data;
10340 struct rpc_task *task;
10341 struct nfs_client *clp = server->nfs_client;
10343 if (!refcount_inc_not_zero(&clp->cl_count))
10346 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10347 &task_setup.rpc_client, &msg);
10349 dprintk("NFS call free_stateid %p\n", stateid);
10350 data = kmalloc(sizeof(*data), GFP_KERNEL);
10353 data->server = server;
10354 nfs4_stateid_copy(&data->args.stateid, stateid);
10356 task_setup.callback_data = data;
10358 msg.rpc_argp = &data->args;
10359 msg.rpc_resp = &data->res;
10360 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10361 task = rpc_run_task(&task_setup);
10363 return PTR_ERR(task);
10364 rpc_put_task(task);
10369 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10371 const struct cred *cred = lsp->ls_state->owner->so_cred;
10373 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10374 nfs4_free_lock_state(server, lsp);
10377 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10378 const nfs4_stateid *s2)
10380 if (s1->type != s2->type)
10383 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10386 if (s1->seqid == s2->seqid)
10389 return s1->seqid == 0 || s2->seqid == 0;
10392 #endif /* CONFIG_NFS_V4_1 */
10394 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10395 const nfs4_stateid *s2)
10397 return nfs4_stateid_match(s1, s2);
10401 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10402 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10403 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10404 .recover_open = nfs4_open_reclaim,
10405 .recover_lock = nfs4_lock_reclaim,
10406 .establish_clid = nfs4_init_clientid,
10407 .detect_trunking = nfs40_discover_server_trunking,
10410 #if defined(CONFIG_NFS_V4_1)
10411 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10412 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10413 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10414 .recover_open = nfs4_open_reclaim,
10415 .recover_lock = nfs4_lock_reclaim,
10416 .establish_clid = nfs41_init_clientid,
10417 .reclaim_complete = nfs41_proc_reclaim_complete,
10418 .detect_trunking = nfs41_discover_server_trunking,
10420 #endif /* CONFIG_NFS_V4_1 */
10422 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10423 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10424 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10425 .recover_open = nfs40_open_expired,
10426 .recover_lock = nfs4_lock_expired,
10427 .establish_clid = nfs4_init_clientid,
10430 #if defined(CONFIG_NFS_V4_1)
10431 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10432 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10433 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10434 .recover_open = nfs41_open_expired,
10435 .recover_lock = nfs41_lock_expired,
10436 .establish_clid = nfs41_init_clientid,
10438 #endif /* CONFIG_NFS_V4_1 */
10440 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10441 .sched_state_renewal = nfs4_proc_async_renew,
10442 .get_state_renewal_cred = nfs4_get_renew_cred,
10443 .renew_lease = nfs4_proc_renew,
10446 #if defined(CONFIG_NFS_V4_1)
10447 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10448 .sched_state_renewal = nfs41_proc_async_sequence,
10449 .get_state_renewal_cred = nfs4_get_machine_cred,
10450 .renew_lease = nfs4_proc_sequence,
10454 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10455 .get_locations = _nfs40_proc_get_locations,
10456 .fsid_present = _nfs40_proc_fsid_present,
10459 #if defined(CONFIG_NFS_V4_1)
10460 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10461 .get_locations = _nfs41_proc_get_locations,
10462 .fsid_present = _nfs41_proc_fsid_present,
10464 #endif /* CONFIG_NFS_V4_1 */
10466 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10467 .minor_version = 0,
10468 .init_caps = NFS_CAP_READDIRPLUS
10469 | NFS_CAP_ATOMIC_OPEN
10470 | NFS_CAP_POSIX_LOCK,
10471 .init_client = nfs40_init_client,
10472 .shutdown_client = nfs40_shutdown_client,
10473 .match_stateid = nfs4_match_stateid,
10474 .find_root_sec = nfs4_find_root_sec,
10475 .free_lock_state = nfs4_release_lockowner,
10476 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10477 .alloc_seqid = nfs_alloc_seqid,
10478 .call_sync_ops = &nfs40_call_sync_ops,
10479 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10480 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10481 .state_renewal_ops = &nfs40_state_renewal_ops,
10482 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10485 #if defined(CONFIG_NFS_V4_1)
10486 static struct nfs_seqid *
10487 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10492 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10493 .minor_version = 1,
10494 .init_caps = NFS_CAP_READDIRPLUS
10495 | NFS_CAP_ATOMIC_OPEN
10496 | NFS_CAP_POSIX_LOCK
10497 | NFS_CAP_STATEID_NFSV41
10498 | NFS_CAP_ATOMIC_OPEN_V1
10500 | NFS_CAP_MOVEABLE,
10501 .init_client = nfs41_init_client,
10502 .shutdown_client = nfs41_shutdown_client,
10503 .match_stateid = nfs41_match_stateid,
10504 .find_root_sec = nfs41_find_root_sec,
10505 .free_lock_state = nfs41_free_lock_state,
10506 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10507 .alloc_seqid = nfs_alloc_no_seqid,
10508 .session_trunk = nfs4_test_session_trunk,
10509 .call_sync_ops = &nfs41_call_sync_ops,
10510 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10511 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10512 .state_renewal_ops = &nfs41_state_renewal_ops,
10513 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10517 #if defined(CONFIG_NFS_V4_2)
10518 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10519 .minor_version = 2,
10520 .init_caps = NFS_CAP_READDIRPLUS
10521 | NFS_CAP_ATOMIC_OPEN
10522 | NFS_CAP_POSIX_LOCK
10523 | NFS_CAP_STATEID_NFSV41
10524 | NFS_CAP_ATOMIC_OPEN_V1
10528 | NFS_CAP_OFFLOAD_CANCEL
10529 | NFS_CAP_COPY_NOTIFY
10530 | NFS_CAP_DEALLOCATE
10532 | NFS_CAP_LAYOUTSTATS
10534 | NFS_CAP_LAYOUTERROR
10535 | NFS_CAP_READ_PLUS
10536 | NFS_CAP_MOVEABLE,
10537 .init_client = nfs41_init_client,
10538 .shutdown_client = nfs41_shutdown_client,
10539 .match_stateid = nfs41_match_stateid,
10540 .find_root_sec = nfs41_find_root_sec,
10541 .free_lock_state = nfs41_free_lock_state,
10542 .call_sync_ops = &nfs41_call_sync_ops,
10543 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10544 .alloc_seqid = nfs_alloc_no_seqid,
10545 .session_trunk = nfs4_test_session_trunk,
10546 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10547 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10548 .state_renewal_ops = &nfs41_state_renewal_ops,
10549 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10553 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10554 [0] = &nfs_v4_0_minor_ops,
10555 #if defined(CONFIG_NFS_V4_1)
10556 [1] = &nfs_v4_1_minor_ops,
10558 #if defined(CONFIG_NFS_V4_2)
10559 [2] = &nfs_v4_2_minor_ops,
10563 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10565 ssize_t error, error2, error3;
10567 error = generic_listxattr(dentry, list, size);
10575 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10584 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10588 return error + error2 + error3;
10591 static void nfs4_enable_swap(struct inode *inode)
10593 /* The state manager thread must always be running.
10594 * It will notice the client is a swapper, and stay put.
10596 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10598 nfs4_schedule_state_manager(clp);
10601 static void nfs4_disable_swap(struct inode *inode)
10603 /* The state manager thread will now exit once it is
10606 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10608 nfs4_schedule_state_manager(clp);
10611 static const struct inode_operations nfs4_dir_inode_operations = {
10612 .create = nfs_create,
10613 .lookup = nfs_lookup,
10614 .atomic_open = nfs_atomic_open,
10616 .unlink = nfs_unlink,
10617 .symlink = nfs_symlink,
10618 .mkdir = nfs_mkdir,
10619 .rmdir = nfs_rmdir,
10620 .mknod = nfs_mknod,
10621 .rename = nfs_rename,
10622 .permission = nfs_permission,
10623 .getattr = nfs_getattr,
10624 .setattr = nfs_setattr,
10625 .listxattr = nfs4_listxattr,
10628 static const struct inode_operations nfs4_file_inode_operations = {
10629 .permission = nfs_permission,
10630 .getattr = nfs_getattr,
10631 .setattr = nfs_setattr,
10632 .listxattr = nfs4_listxattr,
10635 const struct nfs_rpc_ops nfs_v4_clientops = {
10636 .version = 4, /* protocol version */
10637 .dentry_ops = &nfs4_dentry_operations,
10638 .dir_inode_ops = &nfs4_dir_inode_operations,
10639 .file_inode_ops = &nfs4_file_inode_operations,
10640 .file_ops = &nfs4_file_operations,
10641 .getroot = nfs4_proc_get_root,
10642 .submount = nfs4_submount,
10643 .try_get_tree = nfs4_try_get_tree,
10644 .getattr = nfs4_proc_getattr,
10645 .setattr = nfs4_proc_setattr,
10646 .lookup = nfs4_proc_lookup,
10647 .lookupp = nfs4_proc_lookupp,
10648 .access = nfs4_proc_access,
10649 .readlink = nfs4_proc_readlink,
10650 .create = nfs4_proc_create,
10651 .remove = nfs4_proc_remove,
10652 .unlink_setup = nfs4_proc_unlink_setup,
10653 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10654 .unlink_done = nfs4_proc_unlink_done,
10655 .rename_setup = nfs4_proc_rename_setup,
10656 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10657 .rename_done = nfs4_proc_rename_done,
10658 .link = nfs4_proc_link,
10659 .symlink = nfs4_proc_symlink,
10660 .mkdir = nfs4_proc_mkdir,
10661 .rmdir = nfs4_proc_rmdir,
10662 .readdir = nfs4_proc_readdir,
10663 .mknod = nfs4_proc_mknod,
10664 .statfs = nfs4_proc_statfs,
10665 .fsinfo = nfs4_proc_fsinfo,
10666 .pathconf = nfs4_proc_pathconf,
10667 .set_capabilities = nfs4_server_capabilities,
10668 .decode_dirent = nfs4_decode_dirent,
10669 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10670 .read_setup = nfs4_proc_read_setup,
10671 .read_done = nfs4_read_done,
10672 .write_setup = nfs4_proc_write_setup,
10673 .write_done = nfs4_write_done,
10674 .commit_setup = nfs4_proc_commit_setup,
10675 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10676 .commit_done = nfs4_commit_done,
10677 .lock = nfs4_proc_lock,
10678 .clear_acl_cache = nfs4_zap_acl_attr,
10679 .close_context = nfs4_close_context,
10680 .open_context = nfs4_atomic_open,
10681 .have_delegation = nfs4_have_delegation,
10682 .alloc_client = nfs4_alloc_client,
10683 .init_client = nfs4_init_client,
10684 .free_client = nfs4_free_client,
10685 .create_server = nfs4_create_server,
10686 .clone_server = nfs_clone_server,
10687 .discover_trunking = nfs4_discover_trunking,
10688 .enable_swap = nfs4_enable_swap,
10689 .disable_swap = nfs4_disable_swap,
10692 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10693 .name = XATTR_NAME_NFSV4_ACL,
10694 .list = nfs4_xattr_list_nfs4_acl,
10695 .get = nfs4_xattr_get_nfs4_acl,
10696 .set = nfs4_xattr_set_nfs4_acl,
10699 #if defined(CONFIG_NFS_V4_1)
10700 static const struct xattr_handler nfs4_xattr_nfs4_dacl_handler = {
10701 .name = XATTR_NAME_NFSV4_DACL,
10702 .list = nfs4_xattr_list_nfs4_dacl,
10703 .get = nfs4_xattr_get_nfs4_dacl,
10704 .set = nfs4_xattr_set_nfs4_dacl,
10707 static const struct xattr_handler nfs4_xattr_nfs4_sacl_handler = {
10708 .name = XATTR_NAME_NFSV4_SACL,
10709 .list = nfs4_xattr_list_nfs4_sacl,
10710 .get = nfs4_xattr_get_nfs4_sacl,
10711 .set = nfs4_xattr_set_nfs4_sacl,
10715 #ifdef CONFIG_NFS_V4_2
10716 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10717 .prefix = XATTR_USER_PREFIX,
10718 .get = nfs4_xattr_get_nfs4_user,
10719 .set = nfs4_xattr_set_nfs4_user,
10723 const struct xattr_handler *nfs4_xattr_handlers[] = {
10724 &nfs4_xattr_nfs4_acl_handler,
10725 #if defined(CONFIG_NFS_V4_1)
10726 &nfs4_xattr_nfs4_dacl_handler,
10727 &nfs4_xattr_nfs4_sacl_handler,
10729 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10730 &nfs4_xattr_nfs4_label_handler,
10732 #ifdef CONFIG_NFS_V4_2
10733 &nfs4_xattr_nfs4_user_handler,