4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
57 #include <linux/iversion.h>
60 #include "delegation.h"
67 #include "nfs4idmap.h"
68 #include "nfs4session.h"
72 #include "nfs4trace.h"
74 #define NFSDBG_FACILITY NFSDBG_PROC
76 #define NFS4_BITMASK_SZ 3
78 #define NFS4_POLL_RETRY_MIN (HZ/10)
79 #define NFS4_POLL_RETRY_MAX (15*HZ)
81 /* file attributes which can be mapped to nfs attributes */
82 #define NFS4_VALID_ATTRS (ATTR_MODE \
93 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
94 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
95 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
96 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
97 struct nfs_fattr *fattr, struct inode *inode);
98 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
99 struct nfs_fattr *fattr, struct iattr *sattr,
100 struct nfs_open_context *ctx, struct nfs4_label *ilabel);
101 #ifdef CONFIG_NFS_V4_1
102 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
103 const struct cred *cred,
104 struct nfs4_slot *slot,
106 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
107 const struct cred *);
108 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
109 const struct cred *, bool);
112 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
113 static inline struct nfs4_label *
114 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
115 struct iattr *sattr, struct nfs4_label *label)
122 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
130 err = security_dentry_init_security(dentry, sattr->ia_mode,
131 &dentry->d_name, NULL,
132 (void **)&label->label, &label->len);
139 nfs4_label_release_security(struct nfs4_label *label)
142 security_release_secctx(label->label, label->len);
144 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
147 return server->attr_bitmask;
149 return server->attr_bitmask_nl;
152 static inline struct nfs4_label *
153 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
154 struct iattr *sattr, struct nfs4_label *l)
157 nfs4_label_release_security(struct nfs4_label *label)
160 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
161 { return server->attr_bitmask; }
164 /* Prevent leaks of NFSv4 errors into userland */
165 static int nfs4_map_errors(int err)
170 case -NFS4ERR_RESOURCE:
171 case -NFS4ERR_LAYOUTTRYLATER:
172 case -NFS4ERR_RECALLCONFLICT:
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 set_bit(NFS4_SLOT_TBL_DRAINING, &session->fc_slot_table.slot_tbl_state);
925 nfs4_schedule_session_recovery(session, status);
926 dprintk("%s ERROR: %d Reset session\n", __func__, status);
927 nfs41_sequence_free_slot(res);
932 if (rpc_restart_call_prepare(task)) {
933 nfs41_sequence_free_slot(res);
939 if (!rpc_restart_call(task))
941 rpc_delay(task, NFS4_POLL_RETRY_MAX);
945 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
947 if (!nfs41_sequence_process(task, res))
949 if (res->sr_slot != NULL)
950 nfs41_sequence_free_slot(res);
954 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
956 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
958 if (res->sr_slot == NULL)
960 if (res->sr_slot->table->session != NULL)
961 return nfs41_sequence_process(task, res);
962 return nfs40_sequence_done(task, res);
965 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
967 if (res->sr_slot != NULL) {
968 if (res->sr_slot->table->session != NULL)
969 nfs41_sequence_free_slot(res);
971 nfs40_sequence_free_slot(res);
975 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
977 if (res->sr_slot == NULL)
979 if (!res->sr_slot->table->session)
980 return nfs40_sequence_done(task, res);
981 return nfs41_sequence_done(task, res);
983 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
985 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
987 struct nfs4_call_sync_data *data = calldata;
989 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
991 nfs4_setup_sequence(data->seq_server->nfs_client,
992 data->seq_args, data->seq_res, task);
995 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
997 struct nfs4_call_sync_data *data = calldata;
999 nfs41_sequence_done(task, data->seq_res);
1002 static const struct rpc_call_ops nfs41_call_sync_ops = {
1003 .rpc_call_prepare = nfs41_call_sync_prepare,
1004 .rpc_call_done = nfs41_call_sync_done,
1007 #else /* !CONFIG_NFS_V4_1 */
1009 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1011 return nfs40_sequence_done(task, res);
1014 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1016 if (res->sr_slot != NULL)
1017 nfs40_sequence_free_slot(res);
1020 int nfs4_sequence_done(struct rpc_task *task,
1021 struct nfs4_sequence_res *res)
1023 return nfs40_sequence_done(task, res);
1025 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1027 #endif /* !CONFIG_NFS_V4_1 */
1029 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1031 res->sr_timestamp = jiffies;
1032 res->sr_status_flags = 0;
1037 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1038 struct nfs4_sequence_res *res,
1039 struct nfs4_slot *slot)
1043 slot->privileged = args->sa_privileged ? 1 : 0;
1044 args->sa_slot = slot;
1046 res->sr_slot = slot;
1049 int nfs4_setup_sequence(struct nfs_client *client,
1050 struct nfs4_sequence_args *args,
1051 struct nfs4_sequence_res *res,
1052 struct rpc_task *task)
1054 struct nfs4_session *session = nfs4_get_session(client);
1055 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1056 struct nfs4_slot *slot;
1058 /* slot already allocated? */
1059 if (res->sr_slot != NULL)
1063 tbl = &session->fc_slot_table;
1065 spin_lock(&tbl->slot_tbl_lock);
1066 /* The state manager will wait until the slot table is empty */
1067 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1070 slot = nfs4_alloc_slot(tbl);
1072 if (slot == ERR_PTR(-ENOMEM))
1073 goto out_sleep_timeout;
1076 spin_unlock(&tbl->slot_tbl_lock);
1078 nfs4_sequence_attach_slot(args, res, slot);
1080 trace_nfs4_setup_sequence(session, args);
1082 nfs41_sequence_res_init(res);
1083 rpc_call_start(task);
1086 /* Try again in 1/4 second */
1087 if (args->sa_privileged)
1088 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1089 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1091 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1092 NULL, jiffies + (HZ >> 2));
1093 spin_unlock(&tbl->slot_tbl_lock);
1096 if (args->sa_privileged)
1097 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1098 RPC_PRIORITY_PRIVILEGED);
1100 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1101 spin_unlock(&tbl->slot_tbl_lock);
1104 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1106 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1108 struct nfs4_call_sync_data *data = calldata;
1109 nfs4_setup_sequence(data->seq_server->nfs_client,
1110 data->seq_args, data->seq_res, task);
1113 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1115 struct nfs4_call_sync_data *data = calldata;
1116 nfs4_sequence_done(task, data->seq_res);
1119 static const struct rpc_call_ops nfs40_call_sync_ops = {
1120 .rpc_call_prepare = nfs40_call_sync_prepare,
1121 .rpc_call_done = nfs40_call_sync_done,
1124 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1127 struct rpc_task *task;
1129 task = rpc_run_task(task_setup);
1131 return PTR_ERR(task);
1133 ret = task->tk_status;
1138 static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1139 struct nfs_server *server,
1140 struct rpc_message *msg,
1141 struct nfs4_sequence_args *args,
1142 struct nfs4_sequence_res *res,
1143 unsigned short task_flags)
1145 struct nfs_client *clp = server->nfs_client;
1146 struct nfs4_call_sync_data data = {
1147 .seq_server = server,
1151 struct rpc_task_setup task_setup = {
1154 .callback_ops = clp->cl_mvops->call_sync_ops,
1155 .callback_data = &data,
1156 .flags = task_flags,
1159 return nfs4_call_sync_custom(&task_setup);
1162 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1163 struct nfs_server *server,
1164 struct rpc_message *msg,
1165 struct nfs4_sequence_args *args,
1166 struct nfs4_sequence_res *res)
1168 unsigned short task_flags = 0;
1170 if (server->caps & NFS_CAP_MOVEABLE)
1171 task_flags = RPC_TASK_MOVEABLE;
1172 return nfs4_do_call_sync(clnt, server, msg, args, res, task_flags);
1176 int nfs4_call_sync(struct rpc_clnt *clnt,
1177 struct nfs_server *server,
1178 struct rpc_message *msg,
1179 struct nfs4_sequence_args *args,
1180 struct nfs4_sequence_res *res,
1183 nfs4_init_sequence(args, res, cache_reply, 0);
1184 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1188 nfs4_inc_nlink_locked(struct inode *inode)
1190 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1191 NFS_INO_INVALID_CTIME |
1192 NFS_INO_INVALID_NLINK);
1197 nfs4_inc_nlink(struct inode *inode)
1199 spin_lock(&inode->i_lock);
1200 nfs4_inc_nlink_locked(inode);
1201 spin_unlock(&inode->i_lock);
1205 nfs4_dec_nlink_locked(struct inode *inode)
1207 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1208 NFS_INO_INVALID_CTIME |
1209 NFS_INO_INVALID_NLINK);
1214 nfs4_update_changeattr_locked(struct inode *inode,
1215 struct nfs4_change_info *cinfo,
1216 unsigned long timestamp, unsigned long cache_validity)
1218 struct nfs_inode *nfsi = NFS_I(inode);
1219 u64 change_attr = inode_peek_iversion_raw(inode);
1221 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
1222 if (S_ISDIR(inode->i_mode))
1223 cache_validity |= NFS_INO_INVALID_DATA;
1225 switch (NFS_SERVER(inode)->change_attr_type) {
1226 case NFS4_CHANGE_TYPE_IS_UNDEFINED:
1227 if (cinfo->after == change_attr)
1231 if ((s64)(change_attr - cinfo->after) >= 0)
1235 inode_set_iversion_raw(inode, cinfo->after);
1236 if (!cinfo->atomic || cinfo->before != change_attr) {
1237 if (S_ISDIR(inode->i_mode))
1238 nfs_force_lookup_revalidate(inode);
1240 if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1242 NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
1243 NFS_INO_INVALID_SIZE | NFS_INO_INVALID_OTHER |
1244 NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
1245 NFS_INO_INVALID_MODE | NFS_INO_INVALID_XATTR;
1246 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1248 nfsi->attrtimeo_timestamp = jiffies;
1249 nfsi->read_cache_jiffies = timestamp;
1250 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1251 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1253 nfs_set_cache_invalid(inode, cache_validity);
1257 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1258 unsigned long timestamp, unsigned long cache_validity)
1260 spin_lock(&dir->i_lock);
1261 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1262 spin_unlock(&dir->i_lock);
1265 struct nfs4_open_createattrs {
1266 struct nfs4_label *label;
1267 struct iattr *sattr;
1268 const __u32 verf[2];
1271 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1272 int err, struct nfs4_exception *exception)
1276 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1278 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1279 exception->retry = 1;
1283 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1285 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1288 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1290 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1292 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1296 nfs4_map_atomic_open_share(struct nfs_server *server,
1297 fmode_t fmode, int openflags)
1301 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1303 res = NFS4_SHARE_ACCESS_READ;
1306 res = NFS4_SHARE_ACCESS_WRITE;
1308 case FMODE_READ|FMODE_WRITE:
1309 res = NFS4_SHARE_ACCESS_BOTH;
1311 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1313 /* Want no delegation if we're using O_DIRECT */
1314 if (openflags & O_DIRECT)
1315 res |= NFS4_SHARE_WANT_NO_DELEG;
1320 static enum open_claim_type4
1321 nfs4_map_atomic_open_claim(struct nfs_server *server,
1322 enum open_claim_type4 claim)
1324 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1329 case NFS4_OPEN_CLAIM_FH:
1330 return NFS4_OPEN_CLAIM_NULL;
1331 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1332 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1333 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1334 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1338 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1340 p->o_res.f_attr = &p->f_attr;
1341 p->o_res.seqid = p->o_arg.seqid;
1342 p->c_res.seqid = p->c_arg.seqid;
1343 p->o_res.server = p->o_arg.server;
1344 p->o_res.access_request = p->o_arg.access;
1345 nfs_fattr_init(&p->f_attr);
1346 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1349 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1350 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1351 const struct nfs4_open_createattrs *c,
1352 enum open_claim_type4 claim,
1355 struct dentry *parent = dget_parent(dentry);
1356 struct inode *dir = d_inode(parent);
1357 struct nfs_server *server = NFS_SERVER(dir);
1358 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1359 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1360 struct nfs4_opendata *p;
1362 p = kzalloc(sizeof(*p), gfp_mask);
1366 p->f_attr.label = nfs4_label_alloc(server, gfp_mask);
1367 if (IS_ERR(p->f_attr.label))
1370 p->a_label = nfs4_label_alloc(server, gfp_mask);
1371 if (IS_ERR(p->a_label))
1374 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1375 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1376 if (IS_ERR(p->o_arg.seqid))
1377 goto err_free_label;
1378 nfs_sb_active(dentry->d_sb);
1379 p->dentry = dget(dentry);
1382 atomic_inc(&sp->so_count);
1383 p->o_arg.open_flags = flags;
1384 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1385 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1386 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1388 if (flags & O_CREAT) {
1389 p->o_arg.umask = current_umask();
1390 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1391 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1392 p->o_arg.u.attrs = &p->attrs;
1393 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1395 memcpy(p->o_arg.u.verifier.data, c->verf,
1396 sizeof(p->o_arg.u.verifier.data));
1399 /* ask server to check for all possible rights as results
1401 switch (p->o_arg.claim) {
1404 case NFS4_OPEN_CLAIM_NULL:
1405 case NFS4_OPEN_CLAIM_FH:
1406 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1407 NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE |
1408 NFS4_ACCESS_EXECUTE |
1409 nfs_access_xattr_mask(server);
1411 p->o_arg.clientid = server->nfs_client->cl_clientid;
1412 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1413 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1414 p->o_arg.name = &dentry->d_name;
1415 p->o_arg.server = server;
1416 p->o_arg.bitmask = nfs4_bitmask(server, label);
1417 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1418 switch (p->o_arg.claim) {
1419 case NFS4_OPEN_CLAIM_NULL:
1420 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1421 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1422 p->o_arg.fh = NFS_FH(dir);
1424 case NFS4_OPEN_CLAIM_PREVIOUS:
1425 case NFS4_OPEN_CLAIM_FH:
1426 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1427 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1428 p->o_arg.fh = NFS_FH(d_inode(dentry));
1430 p->c_arg.fh = &p->o_res.fh;
1431 p->c_arg.stateid = &p->o_res.stateid;
1432 p->c_arg.seqid = p->o_arg.seqid;
1433 nfs4_init_opendata_res(p);
1434 kref_init(&p->kref);
1438 nfs4_label_free(p->a_label);
1440 nfs4_label_free(p->f_attr.label);
1448 static void nfs4_opendata_free(struct kref *kref)
1450 struct nfs4_opendata *p = container_of(kref,
1451 struct nfs4_opendata, kref);
1452 struct super_block *sb = p->dentry->d_sb;
1454 nfs4_lgopen_release(p->lgp);
1455 nfs_free_seqid(p->o_arg.seqid);
1456 nfs4_sequence_free_slot(&p->o_res.seq_res);
1457 if (p->state != NULL)
1458 nfs4_put_open_state(p->state);
1459 nfs4_put_state_owner(p->owner);
1461 nfs4_label_free(p->a_label);
1462 nfs4_label_free(p->f_attr.label);
1466 nfs_sb_deactive(sb);
1467 nfs_fattr_free_names(&p->f_attr);
1468 kfree(p->f_attr.mdsthreshold);
1472 static void nfs4_opendata_put(struct nfs4_opendata *p)
1475 kref_put(&p->kref, nfs4_opendata_free);
1478 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1481 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1482 case FMODE_READ|FMODE_WRITE:
1483 return state->n_rdwr != 0;
1485 return state->n_wronly != 0;
1487 return state->n_rdonly != 0;
1493 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1494 int open_mode, enum open_claim_type4 claim)
1498 if (open_mode & (O_EXCL|O_TRUNC))
1501 case NFS4_OPEN_CLAIM_NULL:
1502 case NFS4_OPEN_CLAIM_FH:
1507 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1509 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1510 && state->n_rdonly != 0;
1513 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1514 && state->n_wronly != 0;
1516 case FMODE_READ|FMODE_WRITE:
1517 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1518 && state->n_rdwr != 0;
1524 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1525 enum open_claim_type4 claim)
1527 if (delegation == NULL)
1529 if ((delegation->type & fmode) != fmode)
1532 case NFS4_OPEN_CLAIM_NULL:
1533 case NFS4_OPEN_CLAIM_FH:
1535 case NFS4_OPEN_CLAIM_PREVIOUS:
1536 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1542 nfs_mark_delegation_referenced(delegation);
1546 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1555 case FMODE_READ|FMODE_WRITE:
1558 nfs4_state_set_mode_locked(state, state->state | fmode);
1561 #ifdef CONFIG_NFS_V4_1
1562 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1564 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1566 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1568 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1572 #endif /* CONFIG_NFS_V4_1 */
1574 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1576 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1577 wake_up_all(&state->waitq);
1580 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1582 struct nfs_client *clp = state->owner->so_server->nfs_client;
1583 bool need_recover = false;
1585 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1586 need_recover = true;
1587 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1588 need_recover = true;
1589 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1590 need_recover = true;
1592 nfs4_state_mark_reclaim_nograce(clp, state);
1596 * Check for whether or not the caller may update the open stateid
1597 * to the value passed in by stateid.
1599 * Note: This function relies heavily on the server implementing
1600 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1602 * i.e. The stateid seqids have to be initialised to 1, and
1603 * are then incremented on every state transition.
1605 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1606 const nfs4_stateid *stateid)
1608 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1609 /* The common case - we're updating to a new sequence number */
1610 if (nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1611 if (nfs4_stateid_is_next(&state->open_stateid, stateid))
1615 /* The server returned a new stateid */
1617 /* This is the first OPEN in this generation */
1618 if (stateid->seqid == cpu_to_be32(1))
1623 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1625 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1627 if (state->n_wronly)
1628 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1629 if (state->n_rdonly)
1630 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1632 set_bit(NFS_O_RDWR_STATE, &state->flags);
1633 set_bit(NFS_OPEN_STATE, &state->flags);
1636 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1637 nfs4_stateid *stateid, fmode_t fmode)
1639 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1640 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1642 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1645 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1648 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1649 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1650 clear_bit(NFS_OPEN_STATE, &state->flags);
1652 if (stateid == NULL)
1654 /* Handle OPEN+OPEN_DOWNGRADE races */
1655 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1656 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1657 nfs_resync_open_stateid_locked(state);
1660 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1661 nfs4_stateid_copy(&state->stateid, stateid);
1662 nfs4_stateid_copy(&state->open_stateid, stateid);
1663 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1665 nfs_state_log_update_open_stateid(state);
1668 static void nfs_clear_open_stateid(struct nfs4_state *state,
1669 nfs4_stateid *arg_stateid,
1670 nfs4_stateid *stateid, fmode_t fmode)
1672 write_seqlock(&state->seqlock);
1673 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1674 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1675 nfs_clear_open_stateid_locked(state, stateid, fmode);
1676 write_sequnlock(&state->seqlock);
1677 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1678 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1681 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1682 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1683 __must_hold(&state->owner->so_lock)
1684 __must_hold(&state->seqlock)
1692 if (nfs_stateid_is_sequential(state, stateid))
1697 /* Rely on seqids for serialisation with NFSv4.0 */
1698 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1701 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1702 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1704 * Ensure we process the state changes in the same order
1705 * in which the server processed them by delaying the
1706 * update of the stateid until we are in sequence.
1708 write_sequnlock(&state->seqlock);
1709 spin_unlock(&state->owner->so_lock);
1711 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1713 if (!fatal_signal_pending(current)) {
1714 if (schedule_timeout(5*HZ) == 0)
1720 finish_wait(&state->waitq, &wait);
1722 spin_lock(&state->owner->so_lock);
1723 write_seqlock(&state->seqlock);
1726 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1727 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1728 nfs4_stateid_copy(freeme, &state->open_stateid);
1729 nfs_test_and_clear_all_open_stateid(state);
1732 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1733 nfs4_stateid_copy(&state->stateid, stateid);
1734 nfs4_stateid_copy(&state->open_stateid, stateid);
1735 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1736 nfs_state_log_update_open_stateid(state);
1739 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1740 const nfs4_stateid *open_stateid,
1742 nfs4_stateid *freeme)
1745 * Protect the call to nfs4_state_set_mode_locked and
1746 * serialise the stateid update
1748 write_seqlock(&state->seqlock);
1749 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1752 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1755 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1757 case FMODE_READ|FMODE_WRITE:
1758 set_bit(NFS_O_RDWR_STATE, &state->flags);
1760 set_bit(NFS_OPEN_STATE, &state->flags);
1761 write_sequnlock(&state->seqlock);
1764 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1766 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1767 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1768 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1769 clear_bit(NFS_OPEN_STATE, &state->flags);
1772 static void nfs_state_set_delegation(struct nfs4_state *state,
1773 const nfs4_stateid *deleg_stateid,
1777 * Protect the call to nfs4_state_set_mode_locked and
1778 * serialise the stateid update
1780 write_seqlock(&state->seqlock);
1781 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1782 set_bit(NFS_DELEGATED_STATE, &state->flags);
1783 write_sequnlock(&state->seqlock);
1786 static void nfs_state_clear_delegation(struct nfs4_state *state)
1788 write_seqlock(&state->seqlock);
1789 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1790 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1791 write_sequnlock(&state->seqlock);
1794 int update_open_stateid(struct nfs4_state *state,
1795 const nfs4_stateid *open_stateid,
1796 const nfs4_stateid *delegation,
1799 struct nfs_server *server = NFS_SERVER(state->inode);
1800 struct nfs_client *clp = server->nfs_client;
1801 struct nfs_inode *nfsi = NFS_I(state->inode);
1802 struct nfs_delegation *deleg_cur;
1803 nfs4_stateid freeme = { };
1806 fmode &= (FMODE_READ|FMODE_WRITE);
1809 spin_lock(&state->owner->so_lock);
1810 if (open_stateid != NULL) {
1811 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1815 deleg_cur = nfs4_get_valid_delegation(state->inode);
1816 if (deleg_cur == NULL)
1819 spin_lock(&deleg_cur->lock);
1820 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1821 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1822 (deleg_cur->type & fmode) != fmode)
1823 goto no_delegation_unlock;
1825 if (delegation == NULL)
1826 delegation = &deleg_cur->stateid;
1827 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1828 goto no_delegation_unlock;
1830 nfs_mark_delegation_referenced(deleg_cur);
1831 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1833 no_delegation_unlock:
1834 spin_unlock(&deleg_cur->lock);
1837 update_open_stateflags(state, fmode);
1838 spin_unlock(&state->owner->so_lock);
1841 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1842 nfs4_schedule_state_manager(clp);
1843 if (freeme.type != 0)
1844 nfs4_test_and_free_stateid(server, &freeme,
1845 state->owner->so_cred);
1850 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1851 const nfs4_stateid *stateid)
1853 struct nfs4_state *state = lsp->ls_state;
1856 spin_lock(&state->state_lock);
1857 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1859 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1861 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1864 spin_unlock(&state->state_lock);
1868 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1870 struct nfs_delegation *delegation;
1872 fmode &= FMODE_READ|FMODE_WRITE;
1874 delegation = nfs4_get_valid_delegation(inode);
1875 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1880 nfs4_inode_return_delegation(inode);
1883 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1885 struct nfs4_state *state = opendata->state;
1886 struct nfs_delegation *delegation;
1887 int open_mode = opendata->o_arg.open_flags;
1888 fmode_t fmode = opendata->o_arg.fmode;
1889 enum open_claim_type4 claim = opendata->o_arg.claim;
1890 nfs4_stateid stateid;
1894 spin_lock(&state->owner->so_lock);
1895 if (can_open_cached(state, fmode, open_mode, claim)) {
1896 update_open_stateflags(state, fmode);
1897 spin_unlock(&state->owner->so_lock);
1898 goto out_return_state;
1900 spin_unlock(&state->owner->so_lock);
1902 delegation = nfs4_get_valid_delegation(state->inode);
1903 if (!can_open_delegated(delegation, fmode, claim)) {
1907 /* Save the delegation */
1908 nfs4_stateid_copy(&stateid, &delegation->stateid);
1910 nfs_release_seqid(opendata->o_arg.seqid);
1911 if (!opendata->is_recover) {
1912 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1918 /* Try to update the stateid using the delegation */
1919 if (update_open_stateid(state, NULL, &stateid, fmode))
1920 goto out_return_state;
1923 return ERR_PTR(ret);
1925 refcount_inc(&state->count);
1930 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1932 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1933 struct nfs_delegation *delegation;
1934 int delegation_flags = 0;
1937 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1939 delegation_flags = delegation->flags;
1941 switch (data->o_arg.claim) {
1944 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1945 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1946 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1947 "returning a delegation for "
1948 "OPEN(CLAIM_DELEGATE_CUR)\n",
1952 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1953 nfs_inode_set_delegation(state->inode,
1954 data->owner->so_cred,
1955 data->o_res.delegation_type,
1956 &data->o_res.delegation,
1957 data->o_res.pagemod_limit);
1959 nfs_inode_reclaim_delegation(state->inode,
1960 data->owner->so_cred,
1961 data->o_res.delegation_type,
1962 &data->o_res.delegation,
1963 data->o_res.pagemod_limit);
1965 if (data->o_res.do_recall)
1966 nfs_async_inode_return_delegation(state->inode,
1967 &data->o_res.delegation);
1971 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1972 * and update the nfs4_state.
1974 static struct nfs4_state *
1975 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1977 struct inode *inode = data->state->inode;
1978 struct nfs4_state *state = data->state;
1981 if (!data->rpc_done) {
1982 if (data->rpc_status)
1983 return ERR_PTR(data->rpc_status);
1984 return nfs4_try_open_cached(data);
1987 ret = nfs_refresh_inode(inode, &data->f_attr);
1989 return ERR_PTR(ret);
1991 if (data->o_res.delegation_type != 0)
1992 nfs4_opendata_check_deleg(data, state);
1994 if (!update_open_stateid(state, &data->o_res.stateid,
1995 NULL, data->o_arg.fmode))
1996 return ERR_PTR(-EAGAIN);
1997 refcount_inc(&state->count);
2002 static struct inode *
2003 nfs4_opendata_get_inode(struct nfs4_opendata *data)
2005 struct inode *inode;
2007 switch (data->o_arg.claim) {
2008 case NFS4_OPEN_CLAIM_NULL:
2009 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2010 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2011 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2012 return ERR_PTR(-EAGAIN);
2013 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2017 inode = d_inode(data->dentry);
2019 nfs_refresh_inode(inode, &data->f_attr);
2024 static struct nfs4_state *
2025 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2027 struct nfs4_state *state;
2028 struct inode *inode;
2030 inode = nfs4_opendata_get_inode(data);
2032 return ERR_CAST(inode);
2033 if (data->state != NULL && data->state->inode == inode) {
2034 state = data->state;
2035 refcount_inc(&state->count);
2037 state = nfs4_get_open_state(inode, data->owner);
2040 state = ERR_PTR(-ENOMEM);
2044 static struct nfs4_state *
2045 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2047 struct nfs4_state *state;
2049 if (!data->rpc_done) {
2050 state = nfs4_try_open_cached(data);
2051 trace_nfs4_cached_open(data->state);
2055 state = nfs4_opendata_find_nfs4_state(data);
2059 if (data->o_res.delegation_type != 0)
2060 nfs4_opendata_check_deleg(data, state);
2061 if (!update_open_stateid(state, &data->o_res.stateid,
2062 NULL, data->o_arg.fmode)) {
2063 nfs4_put_open_state(state);
2064 state = ERR_PTR(-EAGAIN);
2067 nfs_release_seqid(data->o_arg.seqid);
2071 static struct nfs4_state *
2072 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2074 struct nfs4_state *ret;
2076 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2077 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2079 ret = _nfs4_opendata_to_nfs4_state(data);
2080 nfs4_sequence_free_slot(&data->o_res.seq_res);
2084 static struct nfs_open_context *
2085 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2087 struct nfs_inode *nfsi = NFS_I(state->inode);
2088 struct nfs_open_context *ctx;
2091 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2092 if (ctx->state != state)
2094 if ((ctx->mode & mode) != mode)
2096 if (!get_nfs_open_context(ctx))
2102 return ERR_PTR(-ENOENT);
2105 static struct nfs_open_context *
2106 nfs4_state_find_open_context(struct nfs4_state *state)
2108 struct nfs_open_context *ctx;
2110 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2113 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2116 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2119 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2120 struct nfs4_state *state, enum open_claim_type4 claim)
2122 struct nfs4_opendata *opendata;
2124 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2125 NULL, claim, GFP_NOFS);
2126 if (opendata == NULL)
2127 return ERR_PTR(-ENOMEM);
2128 opendata->state = state;
2129 refcount_inc(&state->count);
2133 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2136 struct nfs4_state *newstate;
2137 struct nfs_server *server = NFS_SB(opendata->dentry->d_sb);
2138 int openflags = opendata->o_arg.open_flags;
2141 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2143 opendata->o_arg.fmode = fmode;
2144 opendata->o_arg.share_access =
2145 nfs4_map_atomic_open_share(server, fmode, openflags);
2146 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2147 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2148 nfs4_init_opendata_res(opendata);
2149 ret = _nfs4_recover_proc_open(opendata);
2152 newstate = nfs4_opendata_to_nfs4_state(opendata);
2153 if (IS_ERR(newstate))
2154 return PTR_ERR(newstate);
2155 if (newstate != opendata->state)
2157 nfs4_close_state(newstate, fmode);
2161 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2165 /* memory barrier prior to reading state->n_* */
2167 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2170 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2173 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2177 * We may have performed cached opens for all three recoveries.
2178 * Check if we need to update the current stateid.
2180 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2181 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2182 write_seqlock(&state->seqlock);
2183 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2184 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2185 write_sequnlock(&state->seqlock);
2192 * reclaim state on the server after a reboot.
2194 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2196 struct nfs_delegation *delegation;
2197 struct nfs4_opendata *opendata;
2198 fmode_t delegation_type = 0;
2201 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2202 NFS4_OPEN_CLAIM_PREVIOUS);
2203 if (IS_ERR(opendata))
2204 return PTR_ERR(opendata);
2206 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2207 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2208 delegation_type = delegation->type;
2210 opendata->o_arg.u.delegation_type = delegation_type;
2211 status = nfs4_open_recover(opendata, state);
2212 nfs4_opendata_put(opendata);
2216 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2218 struct nfs_server *server = NFS_SERVER(state->inode);
2219 struct nfs4_exception exception = { };
2222 err = _nfs4_do_open_reclaim(ctx, state);
2223 trace_nfs4_open_reclaim(ctx, 0, err);
2224 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2226 if (err != -NFS4ERR_DELAY)
2228 nfs4_handle_exception(server, err, &exception);
2229 } while (exception.retry);
2233 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2235 struct nfs_open_context *ctx;
2238 ctx = nfs4_state_find_open_context(state);
2241 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2242 nfs_state_clear_open_state_flags(state);
2243 ret = nfs4_do_open_reclaim(ctx, state);
2244 put_nfs_open_context(ctx);
2248 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)
2252 printk(KERN_ERR "NFS: %s: unhandled error "
2253 "%d.\n", __func__, err);
2261 case -NFS4ERR_BADSESSION:
2262 case -NFS4ERR_BADSLOT:
2263 case -NFS4ERR_BAD_HIGH_SLOT:
2264 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2265 case -NFS4ERR_DEADSESSION:
2267 case -NFS4ERR_STALE_CLIENTID:
2268 case -NFS4ERR_STALE_STATEID:
2269 /* Don't recall a delegation if it was lost */
2270 nfs4_schedule_lease_recovery(server->nfs_client);
2272 case -NFS4ERR_MOVED:
2273 nfs4_schedule_migration_recovery(server);
2275 case -NFS4ERR_LEASE_MOVED:
2276 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2278 case -NFS4ERR_DELEG_REVOKED:
2279 case -NFS4ERR_ADMIN_REVOKED:
2280 case -NFS4ERR_EXPIRED:
2281 case -NFS4ERR_BAD_STATEID:
2282 case -NFS4ERR_OPENMODE:
2283 nfs_inode_find_state_and_recover(state->inode,
2285 nfs4_schedule_stateid_recovery(server, state);
2287 case -NFS4ERR_DELAY:
2288 case -NFS4ERR_GRACE:
2292 case -NFS4ERR_DENIED:
2294 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2296 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2303 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2304 struct nfs4_state *state, const nfs4_stateid *stateid)
2306 struct nfs_server *server = NFS_SERVER(state->inode);
2307 struct nfs4_opendata *opendata;
2310 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2311 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2312 if (IS_ERR(opendata))
2313 return PTR_ERR(opendata);
2314 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2315 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2316 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2320 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2321 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2325 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2326 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2330 nfs_state_clear_delegation(state);
2332 nfs4_opendata_put(opendata);
2333 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2336 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2338 struct nfs4_opendata *data = calldata;
2340 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2341 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2344 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2346 struct nfs4_opendata *data = calldata;
2348 nfs40_sequence_done(task, &data->c_res.seq_res);
2350 data->rpc_status = task->tk_status;
2351 if (data->rpc_status == 0) {
2352 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2353 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2354 renew_lease(data->o_res.server, data->timestamp);
2355 data->rpc_done = true;
2359 static void nfs4_open_confirm_release(void *calldata)
2361 struct nfs4_opendata *data = calldata;
2362 struct nfs4_state *state = NULL;
2364 /* If this request hasn't been cancelled, do nothing */
2365 if (!data->cancelled)
2367 /* In case of error, no cleanup! */
2368 if (!data->rpc_done)
2370 state = nfs4_opendata_to_nfs4_state(data);
2372 nfs4_close_state(state, data->o_arg.fmode);
2374 nfs4_opendata_put(data);
2377 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2378 .rpc_call_prepare = nfs4_open_confirm_prepare,
2379 .rpc_call_done = nfs4_open_confirm_done,
2380 .rpc_release = nfs4_open_confirm_release,
2384 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2386 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2388 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2389 struct rpc_task *task;
2390 struct rpc_message msg = {
2391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2392 .rpc_argp = &data->c_arg,
2393 .rpc_resp = &data->c_res,
2394 .rpc_cred = data->owner->so_cred,
2396 struct rpc_task_setup task_setup_data = {
2397 .rpc_client = server->client,
2398 .rpc_message = &msg,
2399 .callback_ops = &nfs4_open_confirm_ops,
2400 .callback_data = data,
2401 .workqueue = nfsiod_workqueue,
2402 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2406 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2408 kref_get(&data->kref);
2409 data->rpc_done = false;
2410 data->rpc_status = 0;
2411 data->timestamp = jiffies;
2412 task = rpc_run_task(&task_setup_data);
2414 return PTR_ERR(task);
2415 status = rpc_wait_for_completion_task(task);
2417 data->cancelled = true;
2420 status = data->rpc_status;
2425 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2427 struct nfs4_opendata *data = calldata;
2428 struct nfs4_state_owner *sp = data->owner;
2429 struct nfs_client *clp = sp->so_server->nfs_client;
2430 enum open_claim_type4 claim = data->o_arg.claim;
2432 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2435 * Check if we still need to send an OPEN call, or if we can use
2436 * a delegation instead.
2438 if (data->state != NULL) {
2439 struct nfs_delegation *delegation;
2441 if (can_open_cached(data->state, data->o_arg.fmode,
2442 data->o_arg.open_flags, claim))
2445 delegation = nfs4_get_valid_delegation(data->state->inode);
2446 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2447 goto unlock_no_action;
2450 /* Update client id. */
2451 data->o_arg.clientid = clp->cl_clientid;
2455 case NFS4_OPEN_CLAIM_PREVIOUS:
2456 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2457 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2458 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2460 case NFS4_OPEN_CLAIM_FH:
2461 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2463 data->timestamp = jiffies;
2464 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2465 &data->o_arg.seq_args,
2466 &data->o_res.seq_res,
2468 nfs_release_seqid(data->o_arg.seqid);
2470 /* Set the create mode (note dependency on the session type) */
2471 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2472 if (data->o_arg.open_flags & O_EXCL) {
2473 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2474 if (clp->cl_mvops->minor_version == 0) {
2475 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2476 /* don't put an ACCESS op in OPEN compound if O_EXCL,
2477 * because ACCESS will return permission denied for
2478 * all bits until close */
2479 data->o_res.access_request = data->o_arg.access = 0;
2480 } else if (nfs4_has_persistent_session(clp))
2481 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2485 trace_nfs4_cached_open(data->state);
2488 task->tk_action = NULL;
2490 nfs4_sequence_done(task, &data->o_res.seq_res);
2493 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2495 struct nfs4_opendata *data = calldata;
2497 data->rpc_status = task->tk_status;
2499 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2502 if (task->tk_status == 0) {
2503 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2504 switch (data->o_res.f_attr->mode & S_IFMT) {
2508 data->rpc_status = -ELOOP;
2511 data->rpc_status = -EISDIR;
2514 data->rpc_status = -ENOTDIR;
2517 renew_lease(data->o_res.server, data->timestamp);
2518 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2519 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2521 data->rpc_done = true;
2524 static void nfs4_open_release(void *calldata)
2526 struct nfs4_opendata *data = calldata;
2527 struct nfs4_state *state = NULL;
2529 /* If this request hasn't been cancelled, do nothing */
2530 if (!data->cancelled)
2532 /* In case of error, no cleanup! */
2533 if (data->rpc_status != 0 || !data->rpc_done)
2535 /* In case we need an open_confirm, no cleanup! */
2536 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2538 state = nfs4_opendata_to_nfs4_state(data);
2540 nfs4_close_state(state, data->o_arg.fmode);
2542 nfs4_opendata_put(data);
2545 static const struct rpc_call_ops nfs4_open_ops = {
2546 .rpc_call_prepare = nfs4_open_prepare,
2547 .rpc_call_done = nfs4_open_done,
2548 .rpc_release = nfs4_open_release,
2551 static int nfs4_run_open_task(struct nfs4_opendata *data,
2552 struct nfs_open_context *ctx)
2554 struct inode *dir = d_inode(data->dir);
2555 struct nfs_server *server = NFS_SERVER(dir);
2556 struct nfs_openargs *o_arg = &data->o_arg;
2557 struct nfs_openres *o_res = &data->o_res;
2558 struct rpc_task *task;
2559 struct rpc_message msg = {
2560 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2563 .rpc_cred = data->owner->so_cred,
2565 struct rpc_task_setup task_setup_data = {
2566 .rpc_client = server->client,
2567 .rpc_message = &msg,
2568 .callback_ops = &nfs4_open_ops,
2569 .callback_data = data,
2570 .workqueue = nfsiod_workqueue,
2571 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2575 if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
2576 task_setup_data.flags |= RPC_TASK_MOVEABLE;
2578 kref_get(&data->kref);
2579 data->rpc_done = false;
2580 data->rpc_status = 0;
2581 data->cancelled = false;
2582 data->is_recover = false;
2584 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2585 data->is_recover = true;
2586 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2588 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2589 pnfs_lgopen_prepare(data, ctx);
2591 task = rpc_run_task(&task_setup_data);
2593 return PTR_ERR(task);
2594 status = rpc_wait_for_completion_task(task);
2596 data->cancelled = true;
2599 status = data->rpc_status;
2605 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2607 struct inode *dir = d_inode(data->dir);
2608 struct nfs_openres *o_res = &data->o_res;
2611 status = nfs4_run_open_task(data, NULL);
2612 if (status != 0 || !data->rpc_done)
2615 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2617 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2618 status = _nfs4_proc_open_confirm(data);
2624 * Additional permission checks in order to distinguish between an
2625 * open for read, and an open for execute. This works around the
2626 * fact that NFSv4 OPEN treats read and execute permissions as being
2628 * Note that in the non-execute case, we want to turn off permission
2629 * checking if we just created a new file (POSIX open() semantics).
2631 static int nfs4_opendata_access(const struct cred *cred,
2632 struct nfs4_opendata *opendata,
2633 struct nfs4_state *state, fmode_t fmode)
2635 struct nfs_access_entry cache;
2638 /* access call failed or for some reason the server doesn't
2639 * support any access modes -- defer access call until later */
2640 if (opendata->o_res.access_supported == 0)
2644 if (fmode & FMODE_EXEC) {
2645 /* ONLY check for exec rights */
2646 if (S_ISDIR(state->inode->i_mode))
2647 mask = NFS4_ACCESS_LOOKUP;
2649 mask = NFS4_ACCESS_EXECUTE;
2650 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2651 mask = NFS4_ACCESS_READ;
2653 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2654 nfs_access_add_cache(state->inode, &cache, cred);
2656 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2657 if ((mask & ~cache.mask & flags) == 0)
2664 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2666 static int _nfs4_proc_open(struct nfs4_opendata *data,
2667 struct nfs_open_context *ctx)
2669 struct inode *dir = d_inode(data->dir);
2670 struct nfs_server *server = NFS_SERVER(dir);
2671 struct nfs_openargs *o_arg = &data->o_arg;
2672 struct nfs_openres *o_res = &data->o_res;
2675 status = nfs4_run_open_task(data, ctx);
2676 if (!data->rpc_done)
2679 if (status == -NFS4ERR_BADNAME &&
2680 !(o_arg->open_flags & O_CREAT))
2685 nfs_fattr_map_and_free_names(server, &data->f_attr);
2687 if (o_arg->open_flags & O_CREAT) {
2688 if (o_arg->open_flags & O_EXCL)
2689 data->file_created = true;
2690 else if (o_res->cinfo.before != o_res->cinfo.after)
2691 data->file_created = true;
2692 if (data->file_created ||
2693 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2694 nfs4_update_changeattr(dir, &o_res->cinfo,
2695 o_res->f_attr->time_start,
2696 NFS_INO_INVALID_DATA);
2698 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2699 server->caps &= ~NFS_CAP_POSIX_LOCK;
2700 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2701 status = _nfs4_proc_open_confirm(data);
2705 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2706 struct nfs_fh *fh = &o_res->fh;
2708 nfs4_sequence_free_slot(&o_res->seq_res);
2709 if (o_arg->claim == NFS4_OPEN_CLAIM_FH)
2710 fh = NFS_FH(d_inode(data->dentry));
2711 nfs4_proc_getattr(server, fh, o_res->f_attr, NULL);
2718 * reclaim state on the server after a network partition.
2719 * Assumes caller holds the appropriate lock
2721 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2723 struct nfs4_opendata *opendata;
2726 opendata = nfs4_open_recoverdata_alloc(ctx, state, NFS4_OPEN_CLAIM_FH);
2727 if (IS_ERR(opendata))
2728 return PTR_ERR(opendata);
2730 * We're not recovering a delegation, so ask for no delegation.
2731 * Otherwise the recovery thread could deadlock with an outstanding
2732 * delegation return.
2734 opendata->o_arg.open_flags = O_DIRECT;
2735 ret = nfs4_open_recover(opendata, state);
2737 d_drop(ctx->dentry);
2738 nfs4_opendata_put(opendata);
2742 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2744 struct nfs_server *server = NFS_SERVER(state->inode);
2745 struct nfs4_exception exception = { };
2749 err = _nfs4_open_expired(ctx, state);
2750 trace_nfs4_open_expired(ctx, 0, err);
2751 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2756 case -NFS4ERR_GRACE:
2757 case -NFS4ERR_DELAY:
2758 nfs4_handle_exception(server, err, &exception);
2761 } while (exception.retry);
2766 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2768 struct nfs_open_context *ctx;
2771 ctx = nfs4_state_find_open_context(state);
2774 ret = nfs4_do_open_expired(ctx, state);
2775 put_nfs_open_context(ctx);
2779 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2780 const nfs4_stateid *stateid)
2782 nfs_remove_bad_delegation(state->inode, stateid);
2783 nfs_state_clear_delegation(state);
2786 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2788 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2789 nfs_finish_clear_delegation_stateid(state, NULL);
2792 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2794 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2795 nfs40_clear_delegation_stateid(state);
2796 nfs_state_clear_open_state_flags(state);
2797 return nfs4_open_expired(sp, state);
2800 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2801 nfs4_stateid *stateid,
2802 const struct cred *cred)
2804 return -NFS4ERR_BAD_STATEID;
2807 #if defined(CONFIG_NFS_V4_1)
2808 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2809 nfs4_stateid *stateid,
2810 const struct cred *cred)
2814 switch (stateid->type) {
2817 case NFS4_INVALID_STATEID_TYPE:
2818 case NFS4_SPECIAL_STATEID_TYPE:
2819 return -NFS4ERR_BAD_STATEID;
2820 case NFS4_REVOKED_STATEID_TYPE:
2824 status = nfs41_test_stateid(server, stateid, cred);
2826 case -NFS4ERR_EXPIRED:
2827 case -NFS4ERR_ADMIN_REVOKED:
2828 case -NFS4ERR_DELEG_REVOKED:
2834 /* Ack the revoked state to the server */
2835 nfs41_free_stateid(server, stateid, cred, true);
2836 return -NFS4ERR_EXPIRED;
2839 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2841 struct nfs_server *server = NFS_SERVER(state->inode);
2842 nfs4_stateid stateid;
2843 struct nfs_delegation *delegation;
2844 const struct cred *cred = NULL;
2845 int status, ret = NFS_OK;
2847 /* Get the delegation credential for use by test/free_stateid */
2849 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2850 if (delegation == NULL) {
2852 nfs_state_clear_delegation(state);
2856 spin_lock(&delegation->lock);
2857 nfs4_stateid_copy(&stateid, &delegation->stateid);
2859 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2860 &delegation->flags)) {
2861 spin_unlock(&delegation->lock);
2866 if (delegation->cred)
2867 cred = get_cred(delegation->cred);
2868 spin_unlock(&delegation->lock);
2870 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2871 trace_nfs4_test_delegation_stateid(state, NULL, status);
2872 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2873 nfs_finish_clear_delegation_stateid(state, &stateid);
2881 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2885 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2886 nfs4_copy_delegation_stateid(state->inode, state->state,
2888 nfs4_stateid_match_other(&state->stateid, &tmp))
2889 nfs_state_set_delegation(state, &tmp, state->state);
2891 nfs_state_clear_delegation(state);
2895 * nfs41_check_expired_locks - possibly free a lock stateid
2897 * @state: NFSv4 state for an inode
2899 * Returns NFS_OK if recovery for this stateid is now finished.
2900 * Otherwise a negative NFS4ERR value is returned.
2902 static int nfs41_check_expired_locks(struct nfs4_state *state)
2904 int status, ret = NFS_OK;
2905 struct nfs4_lock_state *lsp, *prev = NULL;
2906 struct nfs_server *server = NFS_SERVER(state->inode);
2908 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2911 spin_lock(&state->state_lock);
2912 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2913 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2914 const struct cred *cred = lsp->ls_state->owner->so_cred;
2916 refcount_inc(&lsp->ls_count);
2917 spin_unlock(&state->state_lock);
2919 nfs4_put_lock_state(prev);
2922 status = nfs41_test_and_free_expired_stateid(server,
2925 trace_nfs4_test_lock_stateid(state, lsp, status);
2926 if (status == -NFS4ERR_EXPIRED ||
2927 status == -NFS4ERR_BAD_STATEID) {
2928 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2929 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2930 if (!recover_lost_locks)
2931 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2932 } else if (status != NFS_OK) {
2934 nfs4_put_lock_state(prev);
2937 spin_lock(&state->state_lock);
2940 spin_unlock(&state->state_lock);
2941 nfs4_put_lock_state(prev);
2947 * nfs41_check_open_stateid - possibly free an open stateid
2949 * @state: NFSv4 state for an inode
2951 * Returns NFS_OK if recovery for this stateid is now finished.
2952 * Otherwise a negative NFS4ERR value is returned.
2954 static int nfs41_check_open_stateid(struct nfs4_state *state)
2956 struct nfs_server *server = NFS_SERVER(state->inode);
2957 nfs4_stateid *stateid = &state->open_stateid;
2958 const struct cred *cred = state->owner->so_cred;
2961 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2962 return -NFS4ERR_BAD_STATEID;
2963 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2964 trace_nfs4_test_open_stateid(state, NULL, status);
2965 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2966 nfs_state_clear_open_state_flags(state);
2967 stateid->type = NFS4_INVALID_STATEID_TYPE;
2970 if (nfs_open_stateid_recover_openmode(state))
2971 return -NFS4ERR_OPENMODE;
2975 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2979 status = nfs41_check_delegation_stateid(state);
2980 if (status != NFS_OK)
2982 nfs41_delegation_recover_stateid(state);
2984 status = nfs41_check_expired_locks(state);
2985 if (status != NFS_OK)
2987 status = nfs41_check_open_stateid(state);
2988 if (status != NFS_OK)
2989 status = nfs4_open_expired(sp, state);
2995 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2996 * fields corresponding to attributes that were used to store the verifier.
2997 * Make sure we clobber those fields in the later setattr call
2999 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
3000 struct iattr *sattr, struct nfs4_label **label)
3002 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
3007 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
3008 attrset[i] = opendata->o_res.attrset[i];
3009 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3010 attrset[i] &= ~bitmask[i];
3013 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3014 sattr->ia_valid : 0;
3016 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3017 if (sattr->ia_valid & ATTR_ATIME_SET)
3018 ret |= ATTR_ATIME_SET;
3023 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3024 if (sattr->ia_valid & ATTR_MTIME_SET)
3025 ret |= ATTR_MTIME_SET;
3030 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3035 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3036 struct nfs_open_context *ctx)
3038 struct nfs4_state_owner *sp = opendata->owner;
3039 struct nfs_server *server = sp->so_server;
3040 struct dentry *dentry;
3041 struct nfs4_state *state;
3042 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3043 struct inode *dir = d_inode(opendata->dir);
3044 unsigned long dir_verifier;
3048 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3049 dir_verifier = nfs_save_change_attribute(dir);
3051 ret = _nfs4_proc_open(opendata, ctx);
3055 state = _nfs4_opendata_to_nfs4_state(opendata);
3056 ret = PTR_ERR(state);
3060 if (server->caps & NFS_CAP_POSIX_LOCK)
3061 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3062 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3063 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3064 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_PRESERVE_UNLINKED)
3065 set_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(state->inode)->flags);
3067 dentry = opendata->dentry;
3068 if (d_really_is_negative(dentry)) {
3069 struct dentry *alias;
3071 alias = d_exact_alias(dentry, state->inode);
3073 alias = d_splice_alias(igrab(state->inode), dentry);
3074 /* d_splice_alias() can't fail here - it's a non-directory */
3077 ctx->dentry = dentry = alias;
3081 switch(opendata->o_arg.claim) {
3084 case NFS4_OPEN_CLAIM_NULL:
3085 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3086 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3087 if (!opendata->rpc_done)
3089 if (opendata->o_res.delegation_type != 0)
3090 dir_verifier = nfs_save_change_attribute(dir);
3091 nfs_set_verifier(dentry, dir_verifier);
3094 /* Parse layoutget results before we check for access */
3095 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3097 ret = nfs4_opendata_access(sp->so_cred, opendata, state, acc_mode);
3101 if (d_inode(dentry) == state->inode) {
3102 nfs_inode_attach_open_context(ctx);
3103 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3104 nfs4_schedule_stateid_recovery(server, state);
3108 if (!opendata->cancelled) {
3109 if (opendata->lgp) {
3110 nfs4_lgopen_release(opendata->lgp);
3111 opendata->lgp = NULL;
3113 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3119 * Returns a referenced nfs4_state
3121 static int _nfs4_do_open(struct inode *dir,
3122 struct nfs_open_context *ctx,
3124 const struct nfs4_open_createattrs *c,
3127 struct nfs4_state_owner *sp;
3128 struct nfs4_state *state = NULL;
3129 struct nfs_server *server = NFS_SERVER(dir);
3130 struct nfs4_opendata *opendata;
3131 struct dentry *dentry = ctx->dentry;
3132 const struct cred *cred = ctx->cred;
3133 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3134 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3135 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3136 struct iattr *sattr = c->sattr;
3137 struct nfs4_label *label = c->label;
3140 /* Protect against reboot recovery conflicts */
3142 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3144 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3147 status = nfs4_client_recover_expired_lease(server->nfs_client);
3149 goto err_put_state_owner;
3150 if (d_really_is_positive(dentry))
3151 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3153 if (d_really_is_positive(dentry))
3154 claim = NFS4_OPEN_CLAIM_FH;
3155 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3156 c, claim, GFP_KERNEL);
3157 if (opendata == NULL)
3158 goto err_put_state_owner;
3160 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3161 if (!opendata->f_attr.mdsthreshold) {
3162 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3163 if (!opendata->f_attr.mdsthreshold)
3164 goto err_opendata_put;
3166 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3168 if (d_really_is_positive(dentry))
3169 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3171 status = _nfs4_open_and_get_state(opendata, ctx);
3173 goto err_opendata_put;
3176 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3177 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3178 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3180 * send create attributes which was not set by open
3181 * with an extra setattr.
3183 if (attrs || label) {
3184 unsigned ia_old = sattr->ia_valid;
3186 sattr->ia_valid = attrs;
3187 nfs_fattr_init(opendata->o_res.f_attr);
3188 status = nfs4_do_setattr(state->inode, cred,
3189 opendata->o_res.f_attr, sattr,
3192 nfs_setattr_update_inode(state->inode, sattr,
3193 opendata->o_res.f_attr);
3194 nfs_setsecurity(state->inode, opendata->o_res.f_attr);
3196 sattr->ia_valid = ia_old;
3199 if (opened && opendata->file_created)
3202 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3203 *ctx_th = opendata->f_attr.mdsthreshold;
3204 opendata->f_attr.mdsthreshold = NULL;
3207 nfs4_opendata_put(opendata);
3208 nfs4_put_state_owner(sp);
3211 nfs4_opendata_put(opendata);
3212 err_put_state_owner:
3213 nfs4_put_state_owner(sp);
3219 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3220 struct nfs_open_context *ctx,
3222 struct iattr *sattr,
3223 struct nfs4_label *label,
3226 struct nfs_server *server = NFS_SERVER(dir);
3227 struct nfs4_exception exception = {
3228 .interruptible = true,
3230 struct nfs4_state *res;
3231 struct nfs4_open_createattrs c = {
3235 [0] = (__u32)jiffies,
3236 [1] = (__u32)current->pid,
3242 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3244 trace_nfs4_open_file(ctx, flags, status);
3247 /* NOTE: BAD_SEQID means the server and client disagree about the
3248 * book-keeping w.r.t. state-changing operations
3249 * (OPEN/CLOSE/LOCK/LOCKU...)
3250 * It is actually a sign of a bug on the client or on the server.
3252 * If we receive a BAD_SEQID error in the particular case of
3253 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3254 * have unhashed the old state_owner for us, and that we can
3255 * therefore safely retry using a new one. We should still warn
3256 * the user though...
3258 if (status == -NFS4ERR_BAD_SEQID) {
3259 pr_warn_ratelimited("NFS: v4 server %s "
3260 " returned a bad sequence-id error!\n",
3261 NFS_SERVER(dir)->nfs_client->cl_hostname);
3262 exception.retry = 1;
3266 * BAD_STATEID on OPEN means that the server cancelled our
3267 * state before it received the OPEN_CONFIRM.
3268 * Recover by retrying the request as per the discussion
3269 * on Page 181 of RFC3530.
3271 if (status == -NFS4ERR_BAD_STATEID) {
3272 exception.retry = 1;
3275 if (status == -NFS4ERR_EXPIRED) {
3276 nfs4_schedule_lease_recovery(server->nfs_client);
3277 exception.retry = 1;
3280 if (status == -EAGAIN) {
3281 /* We must have found a delegation */
3282 exception.retry = 1;
3285 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3287 res = ERR_PTR(nfs4_handle_exception(server,
3288 status, &exception));
3289 } while (exception.retry);
3293 static int _nfs4_do_setattr(struct inode *inode,
3294 struct nfs_setattrargs *arg,
3295 struct nfs_setattrres *res,
3296 const struct cred *cred,
3297 struct nfs_open_context *ctx)
3299 struct nfs_server *server = NFS_SERVER(inode);
3300 struct rpc_message msg = {
3301 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3306 const struct cred *delegation_cred = NULL;
3307 unsigned long timestamp = jiffies;
3311 nfs_fattr_init(res->fattr);
3313 /* Servers should only apply open mode checks for file size changes */
3314 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3316 nfs4_inode_make_writeable(inode);
3320 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3321 /* Use that stateid */
3322 } else if (ctx != NULL && ctx->state) {
3323 struct nfs_lock_context *l_ctx;
3324 if (!nfs4_valid_open_stateid(ctx->state))
3326 l_ctx = nfs_get_lock_context(ctx);
3328 return PTR_ERR(l_ctx);
3329 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3330 &arg->stateid, &delegation_cred);
3331 nfs_put_lock_context(l_ctx);
3334 else if (status == -EAGAIN)
3338 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3340 if (delegation_cred)
3341 msg.rpc_cred = delegation_cred;
3343 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3345 put_cred(delegation_cred);
3346 if (status == 0 && ctx != NULL)
3347 renew_lease(server, timestamp);
3348 trace_nfs4_setattr(inode, &arg->stateid, status);
3352 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3353 struct nfs_fattr *fattr, struct iattr *sattr,
3354 struct nfs_open_context *ctx, struct nfs4_label *ilabel)
3356 struct nfs_server *server = NFS_SERVER(inode);
3357 __u32 bitmask[NFS4_BITMASK_SZ];
3358 struct nfs4_state *state = ctx ? ctx->state : NULL;
3359 struct nfs_setattrargs arg = {
3360 .fh = NFS_FH(inode),
3366 struct nfs_setattrres res = {
3370 struct nfs4_exception exception = {
3373 .stateid = &arg.stateid,
3375 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
3378 if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3379 adjust_flags |= NFS_INO_INVALID_MODE;
3380 if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
3381 adjust_flags |= NFS_INO_INVALID_OTHER;
3384 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label),
3385 inode, adjust_flags);
3387 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3389 case -NFS4ERR_OPENMODE:
3390 if (!(sattr->ia_valid & ATTR_SIZE)) {
3391 pr_warn_once("NFSv4: server %s is incorrectly "
3392 "applying open mode checks to "
3393 "a SETATTR that is not "
3394 "changing file size.\n",
3395 server->nfs_client->cl_hostname);
3397 if (state && !(state->state & FMODE_WRITE)) {
3399 if (sattr->ia_valid & ATTR_OPEN)
3404 err = nfs4_handle_exception(server, err, &exception);
3405 } while (exception.retry);
3411 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3413 if (inode == NULL || !nfs_have_layout(inode))
3416 return pnfs_wait_on_layoutreturn(inode, task);
3420 * Update the seqid of an open stateid
3422 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3423 struct nfs4_state *state)
3430 if (!nfs4_valid_open_stateid(state))
3432 seq = read_seqbegin(&state->seqlock);
3433 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3434 nfs4_stateid_copy(dst, &state->open_stateid);
3435 if (read_seqretry(&state->seqlock, seq))
3439 seqid_open = state->open_stateid.seqid;
3440 if (read_seqretry(&state->seqlock, seq))
3443 dst_seqid = be32_to_cpu(dst->seqid);
3444 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3445 dst->seqid = seqid_open;
3451 * Update the seqid of an open stateid after receiving
3452 * NFS4ERR_OLD_STATEID
3454 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3455 struct nfs4_state *state)
3460 int seq, status = -EAGAIN;
3465 if (!nfs4_valid_open_stateid(state))
3467 seq = read_seqbegin(&state->seqlock);
3468 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3469 if (read_seqretry(&state->seqlock, seq))
3474 write_seqlock(&state->seqlock);
3475 seqid_open = state->open_stateid.seqid;
3477 dst_seqid = be32_to_cpu(dst->seqid);
3479 /* Did another OPEN bump the state's seqid? try again: */
3480 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3481 dst->seqid = seqid_open;
3482 write_sequnlock(&state->seqlock);
3487 /* server says we're behind but we haven't seen the update yet */
3488 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3489 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3490 write_sequnlock(&state->seqlock);
3491 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3493 if (fatal_signal_pending(current))
3496 if (schedule_timeout(5*HZ) != 0)
3499 finish_wait(&state->waitq, &wait);
3503 if (status == -EINTR)
3506 /* we slept the whole 5 seconds, we must have lost a seqid */
3507 dst->seqid = cpu_to_be32(dst_seqid + 1);
3515 struct nfs4_closedata {
3516 struct inode *inode;
3517 struct nfs4_state *state;
3518 struct nfs_closeargs arg;
3519 struct nfs_closeres res;
3521 struct nfs4_layoutreturn_args arg;
3522 struct nfs4_layoutreturn_res res;
3523 struct nfs4_xdr_opaque_data ld_private;
3527 struct nfs_fattr fattr;
3528 unsigned long timestamp;
3531 static void nfs4_free_closedata(void *data)
3533 struct nfs4_closedata *calldata = data;
3534 struct nfs4_state_owner *sp = calldata->state->owner;
3535 struct super_block *sb = calldata->state->inode->i_sb;
3537 if (calldata->lr.roc)
3538 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3539 calldata->res.lr_ret);
3540 nfs4_put_open_state(calldata->state);
3541 nfs_free_seqid(calldata->arg.seqid);
3542 nfs4_put_state_owner(sp);
3543 nfs_sb_deactive(sb);
3547 static void nfs4_close_done(struct rpc_task *task, void *data)
3549 struct nfs4_closedata *calldata = data;
3550 struct nfs4_state *state = calldata->state;
3551 struct nfs_server *server = NFS_SERVER(calldata->inode);
3552 nfs4_stateid *res_stateid = NULL;
3553 struct nfs4_exception exception = {
3555 .inode = calldata->inode,
3556 .stateid = &calldata->arg.stateid,
3559 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3561 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3563 /* Handle Layoutreturn errors */
3564 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3565 &calldata->res.lr_ret) == -EAGAIN)
3568 /* hmm. we are done with the inode, and in the process of freeing
3569 * the state_owner. we keep this around to process errors
3571 switch (task->tk_status) {
3573 res_stateid = &calldata->res.stateid;
3574 renew_lease(server, calldata->timestamp);
3576 case -NFS4ERR_ACCESS:
3577 if (calldata->arg.bitmask != NULL) {
3578 calldata->arg.bitmask = NULL;
3579 calldata->res.fattr = NULL;
3584 case -NFS4ERR_OLD_STATEID:
3585 /* Did we race with OPEN? */
3586 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3590 case -NFS4ERR_ADMIN_REVOKED:
3591 case -NFS4ERR_STALE_STATEID:
3592 case -NFS4ERR_EXPIRED:
3593 nfs4_free_revoked_stateid(server,
3594 &calldata->arg.stateid,
3595 task->tk_msg.rpc_cred);
3597 case -NFS4ERR_BAD_STATEID:
3598 if (calldata->arg.fmode == 0)
3602 task->tk_status = nfs4_async_handle_exception(task,
3603 server, task->tk_status, &exception);
3604 if (exception.retry)
3607 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3608 res_stateid, calldata->arg.fmode);
3610 task->tk_status = 0;
3611 nfs_release_seqid(calldata->arg.seqid);
3612 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3613 dprintk("%s: ret = %d\n", __func__, task->tk_status);
3616 task->tk_status = 0;
3617 rpc_restart_call_prepare(task);
3621 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3623 struct nfs4_closedata *calldata = data;
3624 struct nfs4_state *state = calldata->state;
3625 struct inode *inode = calldata->inode;
3626 struct nfs_server *server = NFS_SERVER(inode);
3627 struct pnfs_layout_hdr *lo;
3628 bool is_rdonly, is_wronly, is_rdwr;
3631 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3634 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3635 spin_lock(&state->owner->so_lock);
3636 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3637 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3638 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3639 /* Calculate the change in open mode */
3640 calldata->arg.fmode = 0;
3641 if (state->n_rdwr == 0) {
3642 if (state->n_rdonly == 0)
3643 call_close |= is_rdonly;
3645 calldata->arg.fmode |= FMODE_READ;
3646 if (state->n_wronly == 0)
3647 call_close |= is_wronly;
3649 calldata->arg.fmode |= FMODE_WRITE;
3650 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3651 call_close |= is_rdwr;
3653 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3655 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3656 if (!nfs4_valid_open_stateid(state))
3658 spin_unlock(&state->owner->so_lock);
3661 /* Note: exit _without_ calling nfs4_close_done */
3665 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3666 nfs_release_seqid(calldata->arg.seqid);
3670 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3671 if (lo && !pnfs_layout_is_valid(lo)) {
3672 calldata->arg.lr_args = NULL;
3673 calldata->res.lr_res = NULL;
3676 if (calldata->arg.fmode == 0)
3677 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3679 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3680 /* Close-to-open cache consistency revalidation */
3681 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3682 nfs4_bitmask_set(calldata->arg.bitmask_store,
3683 server->cache_consistency_bitmask,
3685 calldata->arg.bitmask = calldata->arg.bitmask_store;
3687 calldata->arg.bitmask = NULL;
3690 calldata->arg.share_access =
3691 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3692 calldata->arg.fmode, 0);
3694 if (calldata->res.fattr == NULL)
3695 calldata->arg.bitmask = NULL;
3696 else if (calldata->arg.bitmask == NULL)
3697 calldata->res.fattr = NULL;
3698 calldata->timestamp = jiffies;
3699 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3700 &calldata->arg.seq_args,
3701 &calldata->res.seq_res,
3703 nfs_release_seqid(calldata->arg.seqid);
3706 task->tk_action = NULL;
3708 nfs4_sequence_done(task, &calldata->res.seq_res);
3711 static const struct rpc_call_ops nfs4_close_ops = {
3712 .rpc_call_prepare = nfs4_close_prepare,
3713 .rpc_call_done = nfs4_close_done,
3714 .rpc_release = nfs4_free_closedata,
3718 * It is possible for data to be read/written from a mem-mapped file
3719 * after the sys_close call (which hits the vfs layer as a flush).
3720 * This means that we can't safely call nfsv4 close on a file until
3721 * the inode is cleared. This in turn means that we are not good
3722 * NFSv4 citizens - we do not indicate to the server to update the file's
3723 * share state even when we are done with one of the three share
3724 * stateid's in the inode.
3726 * NOTE: Caller must be holding the sp->so_owner semaphore!
3728 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3730 struct nfs_server *server = NFS_SERVER(state->inode);
3731 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3732 struct nfs4_closedata *calldata;
3733 struct nfs4_state_owner *sp = state->owner;
3734 struct rpc_task *task;
3735 struct rpc_message msg = {
3736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3737 .rpc_cred = state->owner->so_cred,
3739 struct rpc_task_setup task_setup_data = {
3740 .rpc_client = server->client,
3741 .rpc_message = &msg,
3742 .callback_ops = &nfs4_close_ops,
3743 .workqueue = nfsiod_workqueue,
3744 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3746 int status = -ENOMEM;
3748 if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
3749 task_setup_data.flags |= RPC_TASK_MOVEABLE;
3751 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3752 &task_setup_data.rpc_client, &msg);
3754 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3755 if (calldata == NULL)
3757 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3758 calldata->inode = state->inode;
3759 calldata->state = state;
3760 calldata->arg.fh = NFS_FH(state->inode);
3761 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3762 goto out_free_calldata;
3763 /* Serialization for the sequence id */
3764 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3765 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3766 if (IS_ERR(calldata->arg.seqid))
3767 goto out_free_calldata;
3768 nfs_fattr_init(&calldata->fattr);
3769 calldata->arg.fmode = 0;
3770 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3771 calldata->res.fattr = &calldata->fattr;
3772 calldata->res.seqid = calldata->arg.seqid;
3773 calldata->res.server = server;
3774 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3775 calldata->lr.roc = pnfs_roc(state->inode,
3776 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3777 if (calldata->lr.roc) {
3778 calldata->arg.lr_args = &calldata->lr.arg;
3779 calldata->res.lr_res = &calldata->lr.res;
3781 nfs_sb_active(calldata->inode->i_sb);
3783 msg.rpc_argp = &calldata->arg;
3784 msg.rpc_resp = &calldata->res;
3785 task_setup_data.callback_data = calldata;
3786 task = rpc_run_task(&task_setup_data);
3788 return PTR_ERR(task);
3791 status = rpc_wait_for_completion_task(task);
3797 nfs4_put_open_state(state);
3798 nfs4_put_state_owner(sp);
3802 static struct inode *
3803 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3804 int open_flags, struct iattr *attr, int *opened)
3806 struct nfs4_state *state;
3807 struct nfs4_label l, *label;
3809 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3811 /* Protect against concurrent sillydeletes */
3812 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3814 nfs4_label_release_security(label);
3817 return ERR_CAST(state);
3818 return state->inode;
3821 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3823 if (ctx->state == NULL)
3826 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3828 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3831 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3832 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3833 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3835 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3837 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3838 struct nfs4_server_caps_arg args = {
3842 struct nfs4_server_caps_res res = {};
3843 struct rpc_message msg = {
3844 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3851 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3852 FATTR4_WORD0_FH_EXPIRE_TYPE |
3853 FATTR4_WORD0_LINK_SUPPORT |
3854 FATTR4_WORD0_SYMLINK_SUPPORT |
3855 FATTR4_WORD0_ACLSUPPORT |
3856 FATTR4_WORD0_CASE_INSENSITIVE |
3857 FATTR4_WORD0_CASE_PRESERVING;
3859 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3861 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3863 /* Sanity check the server answers */
3864 switch (minorversion) {
3866 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3867 res.attr_bitmask[2] = 0;
3870 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3873 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3875 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3876 server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3877 NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3878 server->fattr_valid = NFS_ATTR_FATTR_V4;
3879 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3880 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3881 server->caps |= NFS_CAP_ACLS;
3882 if (res.has_links != 0)
3883 server->caps |= NFS_CAP_HARDLINKS;
3884 if (res.has_symlinks != 0)
3885 server->caps |= NFS_CAP_SYMLINKS;
3886 if (res.case_insensitive)
3887 server->caps |= NFS_CAP_CASE_INSENSITIVE;
3888 if (res.case_preserving)
3889 server->caps |= NFS_CAP_CASE_PRESERVING;
3890 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3891 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3892 server->caps |= NFS_CAP_SECURITY_LABEL;
3894 if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS)
3895 server->caps |= NFS_CAP_FS_LOCATIONS;
3896 if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3897 server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3898 if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3899 server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3900 if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3901 server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3902 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3903 server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3904 NFS_ATTR_FATTR_OWNER_NAME);
3905 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3906 server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3907 NFS_ATTR_FATTR_GROUP_NAME);
3908 if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3909 server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3910 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3911 server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3912 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3913 server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3914 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3915 server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
3916 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3917 sizeof(server->attr_bitmask));
3918 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3920 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3921 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3922 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3923 server->cache_consistency_bitmask[2] = 0;
3925 /* Avoid a regression due to buggy server */
3926 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3927 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3928 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3929 sizeof(server->exclcreat_bitmask));
3931 server->acl_bitmask = res.acl_bitmask;
3932 server->fh_expire_type = res.fh_expire_type;
3938 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3940 struct nfs4_exception exception = {
3941 .interruptible = true,
3945 nfs4_server_set_init_caps(server);
3947 err = nfs4_handle_exception(server,
3948 _nfs4_server_capabilities(server, fhandle),
3950 } while (exception.retry);
3954 static void test_fs_location_for_trunking(struct nfs4_fs_location *location,
3955 struct nfs_client *clp,
3956 struct nfs_server *server)
3960 for (i = 0; i < location->nservers; i++) {
3961 struct nfs4_string *srv_loc = &location->servers[i];
3962 struct sockaddr_storage addr;
3964 struct xprt_create xprt_args = {
3968 struct nfs4_add_xprt_data xprtdata = {
3971 struct rpc_add_xprt_test rpcdata = {
3972 .add_xprt_test = clp->cl_mvops->session_trunk,
3975 char *servername = NULL;
3980 addrlen = nfs_parse_server_name(srv_loc->data, srv_loc->len,
3981 &addr, sizeof(addr),
3982 clp->cl_net, server->port);
3985 xprt_args.dstaddr = (struct sockaddr *)&addr;
3986 xprt_args.addrlen = addrlen;
3987 servername = kmalloc(srv_loc->len + 1, GFP_KERNEL);
3990 memcpy(servername, srv_loc->data, srv_loc->len);
3991 servername[srv_loc->len] = '\0';
3992 xprt_args.servername = servername;
3994 xprtdata.cred = nfs4_get_clid_cred(clp);
3995 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
3996 rpc_clnt_setup_test_and_add_xprt,
3999 put_cred(xprtdata.cred);
4004 static int _nfs4_discover_trunking(struct nfs_server *server,
4005 struct nfs_fh *fhandle)
4007 struct nfs4_fs_locations *locations = NULL;
4009 const struct cred *cred;
4010 struct nfs_client *clp = server->nfs_client;
4011 const struct nfs4_state_maintenance_ops *ops =
4012 clp->cl_mvops->state_renewal_ops;
4013 int status = -ENOMEM, i;
4015 cred = ops->get_state_renewal_cred(clp);
4017 cred = nfs4_get_clid_cred(clp);
4022 page = alloc_page(GFP_KERNEL);
4025 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4028 locations->fattr = nfs_alloc_fattr();
4029 if (!locations->fattr)
4032 status = nfs4_proc_get_locations(server, fhandle, locations, page,
4037 for (i = 0; i < locations->nlocations; i++)
4038 test_fs_location_for_trunking(&locations->locations[i], clp,
4041 kfree(locations->fattr);
4051 static int nfs4_discover_trunking(struct nfs_server *server,
4052 struct nfs_fh *fhandle)
4054 struct nfs4_exception exception = {
4055 .interruptible = true,
4057 struct nfs_client *clp = server->nfs_client;
4060 if (!nfs4_has_session(clp))
4063 err = nfs4_handle_exception(server,
4064 _nfs4_discover_trunking(server, fhandle),
4066 } while (exception.retry);
4071 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4072 struct nfs_fsinfo *info)
4075 struct nfs4_lookup_root_arg args = {
4078 struct nfs4_lookup_res res = {
4080 .fattr = info->fattr,
4083 struct rpc_message msg = {
4084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
4089 bitmask[0] = nfs4_fattr_bitmap[0];
4090 bitmask[1] = nfs4_fattr_bitmap[1];
4092 * Process the label in the upcoming getfattr
4094 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
4096 nfs_fattr_init(info->fattr);
4097 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4100 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4101 struct nfs_fsinfo *info)
4103 struct nfs4_exception exception = {
4104 .interruptible = true,
4108 err = _nfs4_lookup_root(server, fhandle, info);
4109 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
4112 case -NFS4ERR_WRONGSEC:
4115 err = nfs4_handle_exception(server, err, &exception);
4117 } while (exception.retry);
4122 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4123 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4125 struct rpc_auth_create_args auth_args = {
4126 .pseudoflavor = flavor,
4128 struct rpc_auth *auth;
4130 auth = rpcauth_create(&auth_args, server->client);
4133 return nfs4_lookup_root(server, fhandle, info);
4137 * Retry pseudoroot lookup with various security flavors. We do this when:
4139 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4140 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4142 * Returns zero on success, or a negative NFS4ERR value, or a
4143 * negative errno value.
4145 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4146 struct nfs_fsinfo *info)
4148 /* Per 3530bis 15.33.5 */
4149 static const rpc_authflavor_t flav_array[] = {
4153 RPC_AUTH_UNIX, /* courtesy */
4156 int status = -EPERM;
4159 if (server->auth_info.flavor_len > 0) {
4160 /* try each flavor specified by user */
4161 for (i = 0; i < server->auth_info.flavor_len; i++) {
4162 status = nfs4_lookup_root_sec(server, fhandle, info,
4163 server->auth_info.flavors[i]);
4164 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4169 /* no flavors specified by user, try default list */
4170 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4171 status = nfs4_lookup_root_sec(server, fhandle, info,
4173 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4180 * -EACCES could mean that the user doesn't have correct permissions
4181 * to access the mount. It could also mean that we tried to mount
4182 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4183 * existing mount programs don't handle -EACCES very well so it should
4184 * be mapped to -EPERM instead.
4186 if (status == -EACCES)
4192 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4193 * @server: initialized nfs_server handle
4194 * @fhandle: we fill in the pseudo-fs root file handle
4195 * @info: we fill in an FSINFO struct
4196 * @auth_probe: probe the auth flavours
4198 * Returns zero on success, or a negative errno.
4200 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4201 struct nfs_fsinfo *info,
4207 status = nfs4_lookup_root(server, fhandle, info);
4209 if (auth_probe || status == NFS4ERR_WRONGSEC)
4210 status = server->nfs_client->cl_mvops->find_root_sec(server,
4214 status = nfs4_server_capabilities(server, fhandle);
4216 status = nfs4_do_fsinfo(server, fhandle, info);
4218 return nfs4_map_errors(status);
4221 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4222 struct nfs_fsinfo *info)
4225 struct nfs_fattr *fattr = info->fattr;
4227 error = nfs4_server_capabilities(server, mntfh);
4229 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4233 error = nfs4_proc_getattr(server, mntfh, fattr, NULL);
4235 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4239 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4240 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4241 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4248 * Get locations and (maybe) other attributes of a referral.
4249 * Note that we'll actually follow the referral later when
4250 * we detect fsid mismatch in inode revalidation
4252 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4253 const struct qstr *name, struct nfs_fattr *fattr,
4254 struct nfs_fh *fhandle)
4256 int status = -ENOMEM;
4257 struct page *page = NULL;
4258 struct nfs4_fs_locations *locations = NULL;
4260 page = alloc_page(GFP_KERNEL);
4263 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4264 if (locations == NULL)
4267 locations->fattr = fattr;
4269 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4274 * If the fsid didn't change, this is a migration event, not a
4275 * referral. Cause us to drop into the exception handler, which
4276 * will kick off migration recovery.
4278 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &fattr->fsid)) {
4279 dprintk("%s: server did not return a different fsid for"
4280 " a referral at %s\n", __func__, name->name);
4281 status = -NFS4ERR_MOVED;
4284 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4285 nfs_fixup_referral_attributes(fattr);
4286 memset(fhandle, 0, sizeof(struct nfs_fh));
4294 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4295 struct nfs_fattr *fattr, struct inode *inode)
4297 __u32 bitmask[NFS4_BITMASK_SZ];
4298 struct nfs4_getattr_arg args = {
4302 struct nfs4_getattr_res res = {
4306 struct rpc_message msg = {
4307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4311 unsigned short task_flags = 0;
4313 if (nfs4_has_session(server->nfs_client))
4314 task_flags = RPC_TASK_MOVEABLE;
4316 /* Is this is an attribute revalidation, subject to softreval? */
4317 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4318 task_flags |= RPC_TASK_TIMEOUT;
4320 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0);
4321 nfs_fattr_init(fattr);
4322 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4323 return nfs4_do_call_sync(server->client, server, &msg,
4324 &args.seq_args, &res.seq_res, task_flags);
4327 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4328 struct nfs_fattr *fattr, struct inode *inode)
4330 struct nfs4_exception exception = {
4331 .interruptible = true,
4335 err = _nfs4_proc_getattr(server, fhandle, fattr, inode);
4336 trace_nfs4_getattr(server, fhandle, fattr, err);
4337 err = nfs4_handle_exception(server, err,
4339 } while (exception.retry);
4344 * The file is not closed if it is opened due to the a request to change
4345 * the size of the file. The open call will not be needed once the
4346 * VFS layer lookup-intents are implemented.
4348 * Close is called when the inode is destroyed.
4349 * If we haven't opened the file for O_WRONLY, we
4350 * need to in the size_change case to obtain a stateid.
4353 * Because OPEN is always done by name in nfsv4, it is
4354 * possible that we opened a different file by the same
4355 * name. We can recognize this race condition, but we
4356 * can't do anything about it besides returning an error.
4358 * This will be fixed with VFS changes (lookup-intent).
4361 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4362 struct iattr *sattr)
4364 struct inode *inode = d_inode(dentry);
4365 const struct cred *cred = NULL;
4366 struct nfs_open_context *ctx = NULL;
4369 if (pnfs_ld_layoutret_on_setattr(inode) &&
4370 sattr->ia_valid & ATTR_SIZE &&
4371 sattr->ia_size < i_size_read(inode))
4372 pnfs_commit_and_return_layout(inode);
4374 nfs_fattr_init(fattr);
4376 /* Deal with open(O_TRUNC) */
4377 if (sattr->ia_valid & ATTR_OPEN)
4378 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4380 /* Optimization: if the end result is no change, don't RPC */
4381 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4384 /* Search for an existing open(O_WRITE) file */
4385 if (sattr->ia_valid & ATTR_FILE) {
4387 ctx = nfs_file_open_context(sattr->ia_file);
4392 /* Return any delegations if we're going to change ACLs */
4393 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4394 nfs4_inode_make_writeable(inode);
4396 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL);
4398 nfs_setattr_update_inode(inode, sattr, fattr);
4399 nfs_setsecurity(inode, fattr);
4404 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4405 struct dentry *dentry, struct nfs_fh *fhandle,
4406 struct nfs_fattr *fattr)
4408 struct nfs_server *server = NFS_SERVER(dir);
4410 struct nfs4_lookup_arg args = {
4411 .bitmask = server->attr_bitmask,
4412 .dir_fh = NFS_FH(dir),
4413 .name = &dentry->d_name,
4415 struct nfs4_lookup_res res = {
4420 struct rpc_message msg = {
4421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4425 unsigned short task_flags = 0;
4427 if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
4428 task_flags = RPC_TASK_MOVEABLE;
4430 /* Is this is an attribute revalidation, subject to softreval? */
4431 if (nfs_lookup_is_soft_revalidate(dentry))
4432 task_flags |= RPC_TASK_TIMEOUT;
4434 args.bitmask = nfs4_bitmask(server, fattr->label);
4436 nfs_fattr_init(fattr);
4438 dprintk("NFS call lookup %pd2\n", dentry);
4439 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4440 status = nfs4_do_call_sync(clnt, server, &msg,
4441 &args.seq_args, &res.seq_res, task_flags);
4442 dprintk("NFS reply lookup: %d\n", status);
4446 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4448 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4449 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4450 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4454 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4455 struct dentry *dentry, struct nfs_fh *fhandle,
4456 struct nfs_fattr *fattr)
4458 struct nfs4_exception exception = {
4459 .interruptible = true,
4461 struct rpc_clnt *client = *clnt;
4462 const struct qstr *name = &dentry->d_name;
4465 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr);
4466 trace_nfs4_lookup(dir, name, err);
4468 case -NFS4ERR_BADNAME:
4471 case -NFS4ERR_MOVED:
4472 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4473 if (err == -NFS4ERR_MOVED)
4474 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4476 case -NFS4ERR_WRONGSEC:
4478 if (client != *clnt)
4480 client = nfs4_negotiate_security(client, dir, name);
4482 return PTR_ERR(client);
4484 exception.retry = 1;
4487 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4489 } while (exception.retry);
4494 else if (client != *clnt)
4495 rpc_shutdown_client(client);
4500 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4501 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4504 struct rpc_clnt *client = NFS_CLIENT(dir);
4506 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4507 if (client != NFS_CLIENT(dir)) {
4508 rpc_shutdown_client(client);
4509 nfs_fixup_secinfo_attributes(fattr);
4515 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4516 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4518 struct rpc_clnt *client = NFS_CLIENT(dir);
4521 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4523 return ERR_PTR(status);
4524 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4527 static int _nfs4_proc_lookupp(struct inode *inode,
4528 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4530 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4531 struct nfs_server *server = NFS_SERVER(inode);
4533 struct nfs4_lookupp_arg args = {
4534 .bitmask = server->attr_bitmask,
4535 .fh = NFS_FH(inode),
4537 struct nfs4_lookupp_res res = {
4542 struct rpc_message msg = {
4543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4547 unsigned short task_flags = 0;
4549 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4550 task_flags |= RPC_TASK_TIMEOUT;
4552 args.bitmask = nfs4_bitmask(server, fattr->label);
4554 nfs_fattr_init(fattr);
4556 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4557 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4558 &res.seq_res, task_flags);
4559 dprintk("NFS reply lookupp: %d\n", status);
4563 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4564 struct nfs_fattr *fattr)
4566 struct nfs4_exception exception = {
4567 .interruptible = true,
4571 err = _nfs4_proc_lookupp(inode, fhandle, fattr);
4572 trace_nfs4_lookupp(inode, err);
4573 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4575 } while (exception.retry);
4579 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4580 const struct cred *cred)
4582 struct nfs_server *server = NFS_SERVER(inode);
4583 struct nfs4_accessargs args = {
4584 .fh = NFS_FH(inode),
4585 .access = entry->mask,
4587 struct nfs4_accessres res = {
4590 struct rpc_message msg = {
4591 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4598 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4599 res.fattr = nfs_alloc_fattr();
4600 if (res.fattr == NULL)
4602 args.bitmask = server->cache_consistency_bitmask;
4604 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4606 nfs_access_set_mask(entry, res.access);
4608 nfs_refresh_inode(inode, res.fattr);
4610 nfs_free_fattr(res.fattr);
4614 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4615 const struct cred *cred)
4617 struct nfs4_exception exception = {
4618 .interruptible = true,
4622 err = _nfs4_proc_access(inode, entry, cred);
4623 trace_nfs4_access(inode, err);
4624 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4626 } while (exception.retry);
4631 * TODO: For the time being, we don't try to get any attributes
4632 * along with any of the zero-copy operations READ, READDIR,
4635 * In the case of the first three, we want to put the GETATTR
4636 * after the read-type operation -- this is because it is hard
4637 * to predict the length of a GETATTR response in v4, and thus
4638 * align the READ data correctly. This means that the GETATTR
4639 * may end up partially falling into the page cache, and we should
4640 * shift it into the 'tail' of the xdr_buf before processing.
4641 * To do this efficiently, we need to know the total length
4642 * of data received, which doesn't seem to be available outside
4645 * In the case of WRITE, we also want to put the GETATTR after
4646 * the operation -- in this case because we want to make sure
4647 * we get the post-operation mtime and size.
4649 * Both of these changes to the XDR layer would in fact be quite
4650 * minor, but I decided to leave them for a subsequent patch.
4652 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4653 unsigned int pgbase, unsigned int pglen)
4655 struct nfs4_readlink args = {
4656 .fh = NFS_FH(inode),
4661 struct nfs4_readlink_res res;
4662 struct rpc_message msg = {
4663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4668 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4671 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4672 unsigned int pgbase, unsigned int pglen)
4674 struct nfs4_exception exception = {
4675 .interruptible = true,
4679 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4680 trace_nfs4_readlink(inode, err);
4681 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4683 } while (exception.retry);
4688 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4691 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4694 struct nfs_server *server = NFS_SERVER(dir);
4695 struct nfs4_label l, *ilabel;
4696 struct nfs_open_context *ctx;
4697 struct nfs4_state *state;
4700 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4702 return PTR_ERR(ctx);
4704 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4706 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4707 sattr->ia_mode &= ~current_umask();
4708 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4709 if (IS_ERR(state)) {
4710 status = PTR_ERR(state);
4714 nfs4_label_release_security(ilabel);
4715 put_nfs_open_context(ctx);
4720 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4722 struct nfs_server *server = NFS_SERVER(dir);
4723 struct nfs_removeargs args = {
4727 struct nfs_removeres res = {
4730 struct rpc_message msg = {
4731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4735 unsigned long timestamp = jiffies;
4738 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4740 spin_lock(&dir->i_lock);
4741 /* Removing a directory decrements nlink in the parent */
4742 if (ftype == NF4DIR && dir->i_nlink > 2)
4743 nfs4_dec_nlink_locked(dir);
4744 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4745 NFS_INO_INVALID_DATA);
4746 spin_unlock(&dir->i_lock);
4751 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4753 struct nfs4_exception exception = {
4754 .interruptible = true,
4756 struct inode *inode = d_inode(dentry);
4760 if (inode->i_nlink == 1)
4761 nfs4_inode_return_delegation(inode);
4763 nfs4_inode_make_writeable(inode);
4766 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4767 trace_nfs4_remove(dir, &dentry->d_name, err);
4768 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4770 } while (exception.retry);
4774 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4776 struct nfs4_exception exception = {
4777 .interruptible = true,
4782 err = _nfs4_proc_remove(dir, name, NF4DIR);
4783 trace_nfs4_remove(dir, name, err);
4784 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4786 } while (exception.retry);
4790 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4791 struct dentry *dentry,
4792 struct inode *inode)
4794 struct nfs_removeargs *args = msg->rpc_argp;
4795 struct nfs_removeres *res = msg->rpc_resp;
4797 res->server = NFS_SB(dentry->d_sb);
4798 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4799 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4801 nfs_fattr_init(res->dir_attr);
4804 nfs4_inode_return_delegation(inode);
4805 nfs_d_prune_case_insensitive_aliases(inode);
4809 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4811 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4812 &data->args.seq_args,
4817 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4819 struct nfs_unlinkdata *data = task->tk_calldata;
4820 struct nfs_removeres *res = &data->res;
4822 if (!nfs4_sequence_done(task, &res->seq_res))
4824 if (nfs4_async_handle_error(task, res->server, NULL,
4825 &data->timeout) == -EAGAIN)
4827 if (task->tk_status == 0)
4828 nfs4_update_changeattr(dir, &res->cinfo,
4829 res->dir_attr->time_start,
4830 NFS_INO_INVALID_DATA);
4834 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4835 struct dentry *old_dentry,
4836 struct dentry *new_dentry)
4838 struct nfs_renameargs *arg = msg->rpc_argp;
4839 struct nfs_renameres *res = msg->rpc_resp;
4840 struct inode *old_inode = d_inode(old_dentry);
4841 struct inode *new_inode = d_inode(new_dentry);
4844 nfs4_inode_make_writeable(old_inode);
4846 nfs4_inode_return_delegation(new_inode);
4847 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4848 res->server = NFS_SB(old_dentry->d_sb);
4849 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4852 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4854 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4855 &data->args.seq_args,
4860 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4861 struct inode *new_dir)
4863 struct nfs_renamedata *data = task->tk_calldata;
4864 struct nfs_renameres *res = &data->res;
4866 if (!nfs4_sequence_done(task, &res->seq_res))
4868 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4871 if (task->tk_status == 0) {
4872 nfs_d_prune_case_insensitive_aliases(d_inode(data->old_dentry));
4873 if (new_dir != old_dir) {
4874 /* Note: If we moved a directory, nlink will change */
4875 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4876 res->old_fattr->time_start,
4877 NFS_INO_INVALID_NLINK |
4878 NFS_INO_INVALID_DATA);
4879 nfs4_update_changeattr(new_dir, &res->new_cinfo,
4880 res->new_fattr->time_start,
4881 NFS_INO_INVALID_NLINK |
4882 NFS_INO_INVALID_DATA);
4884 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4885 res->old_fattr->time_start,
4886 NFS_INO_INVALID_DATA);
4891 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4893 struct nfs_server *server = NFS_SERVER(inode);
4894 __u32 bitmask[NFS4_BITMASK_SZ];
4895 struct nfs4_link_arg arg = {
4896 .fh = NFS_FH(inode),
4897 .dir_fh = NFS_FH(dir),
4901 struct nfs4_link_res res = {
4904 struct rpc_message msg = {
4905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4909 int status = -ENOMEM;
4911 res.fattr = nfs_alloc_fattr_with_label(server);
4912 if (res.fattr == NULL)
4915 nfs4_inode_make_writeable(inode);
4916 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label), inode,
4917 NFS_INO_INVALID_CHANGE);
4918 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4920 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4921 NFS_INO_INVALID_DATA);
4922 nfs4_inc_nlink(inode);
4923 status = nfs_post_op_update_inode(inode, res.fattr);
4925 nfs_setsecurity(inode, res.fattr);
4929 nfs_free_fattr(res.fattr);
4933 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4935 struct nfs4_exception exception = {
4936 .interruptible = true,
4940 err = nfs4_handle_exception(NFS_SERVER(inode),
4941 _nfs4_proc_link(inode, dir, name),
4943 } while (exception.retry);
4947 struct nfs4_createdata {
4948 struct rpc_message msg;
4949 struct nfs4_create_arg arg;
4950 struct nfs4_create_res res;
4952 struct nfs_fattr fattr;
4955 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4956 const struct qstr *name, struct iattr *sattr, u32 ftype)
4958 struct nfs4_createdata *data;
4960 data = kzalloc(sizeof(*data), GFP_KERNEL);
4962 struct nfs_server *server = NFS_SERVER(dir);
4964 data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL);
4965 if (IS_ERR(data->fattr.label))
4968 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4969 data->msg.rpc_argp = &data->arg;
4970 data->msg.rpc_resp = &data->res;
4971 data->arg.dir_fh = NFS_FH(dir);
4972 data->arg.server = server;
4973 data->arg.name = name;
4974 data->arg.attrs = sattr;
4975 data->arg.ftype = ftype;
4976 data->arg.bitmask = nfs4_bitmask(server, data->fattr.label);
4977 data->arg.umask = current_umask();
4978 data->res.server = server;
4979 data->res.fh = &data->fh;
4980 data->res.fattr = &data->fattr;
4981 nfs_fattr_init(data->res.fattr);
4989 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4991 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4992 &data->arg.seq_args, &data->res.seq_res, 1);
4994 spin_lock(&dir->i_lock);
4995 /* Creating a directory bumps nlink in the parent */
4996 if (data->arg.ftype == NF4DIR)
4997 nfs4_inc_nlink_locked(dir);
4998 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4999 data->res.fattr->time_start,
5000 NFS_INO_INVALID_DATA);
5001 spin_unlock(&dir->i_lock);
5002 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
5007 static void nfs4_free_createdata(struct nfs4_createdata *data)
5009 nfs4_label_free(data->fattr.label);
5013 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5014 struct page *page, unsigned int len, struct iattr *sattr,
5015 struct nfs4_label *label)
5017 struct nfs4_createdata *data;
5018 int status = -ENAMETOOLONG;
5020 if (len > NFS4_MAXPATHLEN)
5024 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
5028 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
5029 data->arg.u.symlink.pages = &page;
5030 data->arg.u.symlink.len = len;
5031 data->arg.label = label;
5033 status = nfs4_do_create(dir, dentry, data);
5035 nfs4_free_createdata(data);
5040 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5041 struct page *page, unsigned int len, struct iattr *sattr)
5043 struct nfs4_exception exception = {
5044 .interruptible = true,
5046 struct nfs4_label l, *label;
5049 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5052 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
5053 trace_nfs4_symlink(dir, &dentry->d_name, err);
5054 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5056 } while (exception.retry);
5058 nfs4_label_release_security(label);
5062 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5063 struct iattr *sattr, struct nfs4_label *label)
5065 struct nfs4_createdata *data;
5066 int status = -ENOMEM;
5068 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
5072 data->arg.label = label;
5073 status = nfs4_do_create(dir, dentry, data);
5075 nfs4_free_createdata(data);
5080 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5081 struct iattr *sattr)
5083 struct nfs_server *server = NFS_SERVER(dir);
5084 struct nfs4_exception exception = {
5085 .interruptible = true,
5087 struct nfs4_label l, *label;
5090 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5092 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5093 sattr->ia_mode &= ~current_umask();
5095 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
5096 trace_nfs4_mkdir(dir, &dentry->d_name, err);
5097 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5099 } while (exception.retry);
5100 nfs4_label_release_security(label);
5105 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5106 struct nfs_readdir_res *nr_res)
5108 struct inode *dir = d_inode(nr_arg->dentry);
5109 struct nfs_server *server = NFS_SERVER(dir);
5110 struct nfs4_readdir_arg args = {
5112 .pages = nr_arg->pages,
5114 .count = nr_arg->page_len,
5115 .plus = nr_arg->plus,
5117 struct nfs4_readdir_res res;
5118 struct rpc_message msg = {
5119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5122 .rpc_cred = nr_arg->cred,
5126 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5127 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5128 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5129 args.bitmask = server->attr_bitmask_nl;
5131 args.bitmask = server->attr_bitmask;
5133 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5134 res.pgbase = args.pgbase;
5135 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5138 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5139 status += args.pgbase;
5142 nfs_invalidate_atime(dir);
5144 dprintk("%s: returns %d\n", __func__, status);
5148 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5149 struct nfs_readdir_res *res)
5151 struct nfs4_exception exception = {
5152 .interruptible = true,
5156 err = _nfs4_proc_readdir(arg, res);
5157 trace_nfs4_readdir(d_inode(arg->dentry), err);
5158 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5160 } while (exception.retry);
5164 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5165 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5167 struct nfs4_createdata *data;
5168 int mode = sattr->ia_mode;
5169 int status = -ENOMEM;
5171 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5176 data->arg.ftype = NF4FIFO;
5177 else if (S_ISBLK(mode)) {
5178 data->arg.ftype = NF4BLK;
5179 data->arg.u.device.specdata1 = MAJOR(rdev);
5180 data->arg.u.device.specdata2 = MINOR(rdev);
5182 else if (S_ISCHR(mode)) {
5183 data->arg.ftype = NF4CHR;
5184 data->arg.u.device.specdata1 = MAJOR(rdev);
5185 data->arg.u.device.specdata2 = MINOR(rdev);
5186 } else if (!S_ISSOCK(mode)) {
5191 data->arg.label = label;
5192 status = nfs4_do_create(dir, dentry, data);
5194 nfs4_free_createdata(data);
5199 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5200 struct iattr *sattr, dev_t rdev)
5202 struct nfs_server *server = NFS_SERVER(dir);
5203 struct nfs4_exception exception = {
5204 .interruptible = true,
5206 struct nfs4_label l, *label;
5209 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5211 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5212 sattr->ia_mode &= ~current_umask();
5214 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5215 trace_nfs4_mknod(dir, &dentry->d_name, err);
5216 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5218 } while (exception.retry);
5220 nfs4_label_release_security(label);
5225 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5226 struct nfs_fsstat *fsstat)
5228 struct nfs4_statfs_arg args = {
5230 .bitmask = server->attr_bitmask,
5232 struct nfs4_statfs_res res = {
5235 struct rpc_message msg = {
5236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5241 nfs_fattr_init(fsstat->fattr);
5242 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5245 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5247 struct nfs4_exception exception = {
5248 .interruptible = true,
5252 err = nfs4_handle_exception(server,
5253 _nfs4_proc_statfs(server, fhandle, fsstat),
5255 } while (exception.retry);
5259 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5260 struct nfs_fsinfo *fsinfo)
5262 struct nfs4_fsinfo_arg args = {
5264 .bitmask = server->attr_bitmask,
5266 struct nfs4_fsinfo_res res = {
5269 struct rpc_message msg = {
5270 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5275 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5278 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5280 struct nfs4_exception exception = {
5281 .interruptible = true,
5286 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5287 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5289 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5292 err = nfs4_handle_exception(server, err, &exception);
5293 } while (exception.retry);
5297 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5301 nfs_fattr_init(fsinfo->fattr);
5302 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5304 /* block layout checks this! */
5305 server->pnfs_blksize = fsinfo->blksize;
5306 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5312 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5313 struct nfs_pathconf *pathconf)
5315 struct nfs4_pathconf_arg args = {
5317 .bitmask = server->attr_bitmask,
5319 struct nfs4_pathconf_res res = {
5320 .pathconf = pathconf,
5322 struct rpc_message msg = {
5323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5328 /* None of the pathconf attributes are mandatory to implement */
5329 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5330 memset(pathconf, 0, sizeof(*pathconf));
5334 nfs_fattr_init(pathconf->fattr);
5335 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5338 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5339 struct nfs_pathconf *pathconf)
5341 struct nfs4_exception exception = {
5342 .interruptible = true,
5347 err = nfs4_handle_exception(server,
5348 _nfs4_proc_pathconf(server, fhandle, pathconf),
5350 } while (exception.retry);
5354 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5355 const struct nfs_open_context *ctx,
5356 const struct nfs_lock_context *l_ctx,
5359 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5361 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5363 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5364 const struct nfs_open_context *ctx,
5365 const struct nfs_lock_context *l_ctx,
5368 nfs4_stateid _current_stateid;
5370 /* If the current stateid represents a lost lock, then exit */
5371 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5373 return nfs4_stateid_match(stateid, &_current_stateid);
5376 static bool nfs4_error_stateid_expired(int err)
5379 case -NFS4ERR_DELEG_REVOKED:
5380 case -NFS4ERR_ADMIN_REVOKED:
5381 case -NFS4ERR_BAD_STATEID:
5382 case -NFS4ERR_STALE_STATEID:
5383 case -NFS4ERR_OLD_STATEID:
5384 case -NFS4ERR_OPENMODE:
5385 case -NFS4ERR_EXPIRED:
5391 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5393 struct nfs_server *server = NFS_SERVER(hdr->inode);
5395 trace_nfs4_read(hdr, task->tk_status);
5396 if (task->tk_status < 0) {
5397 struct nfs4_exception exception = {
5398 .inode = hdr->inode,
5399 .state = hdr->args.context->state,
5400 .stateid = &hdr->args.stateid,
5402 task->tk_status = nfs4_async_handle_exception(task,
5403 server, task->tk_status, &exception);
5404 if (exception.retry) {
5405 rpc_restart_call_prepare(task);
5410 if (task->tk_status > 0)
5411 renew_lease(server, hdr->timestamp);
5415 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5416 struct nfs_pgio_args *args)
5419 if (!nfs4_error_stateid_expired(task->tk_status) ||
5420 nfs4_stateid_is_current(&args->stateid,
5425 rpc_restart_call_prepare(task);
5429 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5430 struct nfs_pgio_header *hdr)
5432 struct nfs_server *server = NFS_SERVER(hdr->inode);
5433 struct rpc_message *msg = &task->tk_msg;
5435 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5436 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5437 server->caps &= ~NFS_CAP_READ_PLUS;
5438 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5439 rpc_restart_call_prepare(task);
5445 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5447 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5449 if (nfs4_read_stateid_changed(task, &hdr->args))
5451 if (nfs4_read_plus_not_supported(task, hdr))
5453 if (task->tk_status > 0)
5454 nfs_invalidate_atime(hdr->inode);
5455 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5456 nfs4_read_done_cb(task, hdr);
5459 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5460 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5461 struct rpc_message *msg)
5463 /* Note: We don't use READ_PLUS with pNFS yet */
5464 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp) {
5465 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5466 return nfs_read_alloc_scratch(hdr, READ_PLUS_SCRATCH_SIZE);
5471 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5472 struct rpc_message *msg)
5476 #endif /* CONFIG_NFS_V4_2 */
5478 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5479 struct rpc_message *msg)
5481 hdr->timestamp = jiffies;
5482 if (!hdr->pgio_done_cb)
5483 hdr->pgio_done_cb = nfs4_read_done_cb;
5484 if (!nfs42_read_plus_support(hdr, msg))
5485 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5486 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5489 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5490 struct nfs_pgio_header *hdr)
5492 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5493 &hdr->args.seq_args,
5497 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5498 hdr->args.lock_context,
5499 hdr->rw_mode) == -EIO)
5501 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5506 static int nfs4_write_done_cb(struct rpc_task *task,
5507 struct nfs_pgio_header *hdr)
5509 struct inode *inode = hdr->inode;
5511 trace_nfs4_write(hdr, task->tk_status);
5512 if (task->tk_status < 0) {
5513 struct nfs4_exception exception = {
5514 .inode = hdr->inode,
5515 .state = hdr->args.context->state,
5516 .stateid = &hdr->args.stateid,
5518 task->tk_status = nfs4_async_handle_exception(task,
5519 NFS_SERVER(inode), task->tk_status,
5521 if (exception.retry) {
5522 rpc_restart_call_prepare(task);
5526 if (task->tk_status >= 0) {
5527 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5528 nfs_writeback_update_inode(hdr);
5533 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5534 struct nfs_pgio_args *args)
5537 if (!nfs4_error_stateid_expired(task->tk_status) ||
5538 nfs4_stateid_is_current(&args->stateid,
5543 rpc_restart_call_prepare(task);
5547 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5549 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5551 if (nfs4_write_stateid_changed(task, &hdr->args))
5553 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5554 nfs4_write_done_cb(task, hdr);
5558 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5560 /* Don't request attributes for pNFS or O_DIRECT writes */
5561 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5563 /* Otherwise, request attributes if and only if we don't hold
5566 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5569 void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[],
5570 struct inode *inode, unsigned long cache_validity)
5572 struct nfs_server *server = NFS_SERVER(inode);
5575 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5576 cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity);
5578 if (cache_validity & NFS_INO_INVALID_CHANGE)
5579 bitmask[0] |= FATTR4_WORD0_CHANGE;
5580 if (cache_validity & NFS_INO_INVALID_ATIME)
5581 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5582 if (cache_validity & NFS_INO_INVALID_MODE)
5583 bitmask[1] |= FATTR4_WORD1_MODE;
5584 if (cache_validity & NFS_INO_INVALID_OTHER)
5585 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5586 if (cache_validity & NFS_INO_INVALID_NLINK)
5587 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5588 if (cache_validity & NFS_INO_INVALID_CTIME)
5589 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5590 if (cache_validity & NFS_INO_INVALID_MTIME)
5591 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5592 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5593 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5595 if (cache_validity & NFS_INO_INVALID_SIZE)
5596 bitmask[0] |= FATTR4_WORD0_SIZE;
5598 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5599 bitmask[i] &= server->attr_bitmask[i];
5602 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5603 struct rpc_message *msg,
5604 struct rpc_clnt **clnt)
5606 struct nfs_server *server = NFS_SERVER(hdr->inode);
5608 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5609 hdr->args.bitmask = NULL;
5610 hdr->res.fattr = NULL;
5612 nfs4_bitmask_set(hdr->args.bitmask_store,
5613 server->cache_consistency_bitmask,
5614 hdr->inode, NFS_INO_INVALID_BLOCKS);
5615 hdr->args.bitmask = hdr->args.bitmask_store;
5618 if (!hdr->pgio_done_cb)
5619 hdr->pgio_done_cb = nfs4_write_done_cb;
5620 hdr->res.server = server;
5621 hdr->timestamp = jiffies;
5623 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5624 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5625 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5628 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5630 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5631 &data->args.seq_args,
5636 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5638 struct inode *inode = data->inode;
5640 trace_nfs4_commit(data, task->tk_status);
5641 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5642 NULL, NULL) == -EAGAIN) {
5643 rpc_restart_call_prepare(task);
5649 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5651 if (!nfs4_sequence_done(task, &data->res.seq_res))
5653 return data->commit_done_cb(task, data);
5656 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5657 struct rpc_clnt **clnt)
5659 struct nfs_server *server = NFS_SERVER(data->inode);
5661 if (data->commit_done_cb == NULL)
5662 data->commit_done_cb = nfs4_commit_done_cb;
5663 data->res.server = server;
5664 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5665 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5666 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5669 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5670 struct nfs_commitres *res)
5672 struct inode *dst_inode = file_inode(dst);
5673 struct nfs_server *server = NFS_SERVER(dst_inode);
5674 struct rpc_message msg = {
5675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5680 args->fh = NFS_FH(dst_inode);
5681 return nfs4_call_sync(server->client, server, &msg,
5682 &args->seq_args, &res->seq_res, 1);
5685 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5687 struct nfs_commitargs args = {
5691 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5692 struct nfs4_exception exception = { };
5696 status = _nfs4_proc_commit(dst, &args, res);
5697 status = nfs4_handle_exception(dst_server, status, &exception);
5698 } while (exception.retry);
5703 struct nfs4_renewdata {
5704 struct nfs_client *client;
5705 unsigned long timestamp;
5709 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5710 * standalone procedure for queueing an asynchronous RENEW.
5712 static void nfs4_renew_release(void *calldata)
5714 struct nfs4_renewdata *data = calldata;
5715 struct nfs_client *clp = data->client;
5717 if (refcount_read(&clp->cl_count) > 1)
5718 nfs4_schedule_state_renewal(clp);
5719 nfs_put_client(clp);
5723 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5725 struct nfs4_renewdata *data = calldata;
5726 struct nfs_client *clp = data->client;
5727 unsigned long timestamp = data->timestamp;
5729 trace_nfs4_renew_async(clp, task->tk_status);
5730 switch (task->tk_status) {
5733 case -NFS4ERR_LEASE_MOVED:
5734 nfs4_schedule_lease_moved_recovery(clp);
5737 /* Unless we're shutting down, schedule state recovery! */
5738 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5740 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5741 nfs4_schedule_lease_recovery(clp);
5744 nfs4_schedule_path_down_recovery(clp);
5746 do_renew_lease(clp, timestamp);
5749 static const struct rpc_call_ops nfs4_renew_ops = {
5750 .rpc_call_done = nfs4_renew_done,
5751 .rpc_release = nfs4_renew_release,
5754 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5756 struct rpc_message msg = {
5757 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5761 struct nfs4_renewdata *data;
5763 if (renew_flags == 0)
5765 if (!refcount_inc_not_zero(&clp->cl_count))
5767 data = kmalloc(sizeof(*data), GFP_NOFS);
5769 nfs_put_client(clp);
5773 data->timestamp = jiffies;
5774 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5775 &nfs4_renew_ops, data);
5778 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5780 struct rpc_message msg = {
5781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5785 unsigned long now = jiffies;
5788 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5791 do_renew_lease(clp, now);
5795 static bool nfs4_server_supports_acls(const struct nfs_server *server,
5796 enum nfs4_acl_type type)
5800 return server->attr_bitmask[0] & FATTR4_WORD0_ACL;
5802 return server->attr_bitmask[1] & FATTR4_WORD1_DACL;
5804 return server->attr_bitmask[1] & FATTR4_WORD1_SACL;
5808 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5809 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5812 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5814 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5815 struct page **pages)
5817 struct page *newpage, **spages;
5823 len = min_t(size_t, PAGE_SIZE, buflen);
5824 newpage = alloc_page(GFP_KERNEL);
5826 if (newpage == NULL)
5828 memcpy(page_address(newpage), buf, len);
5833 } while (buflen != 0);
5839 __free_page(spages[rc-1]);
5843 struct nfs4_cached_acl {
5844 enum nfs4_acl_type type;
5850 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5852 struct nfs_inode *nfsi = NFS_I(inode);
5854 spin_lock(&inode->i_lock);
5855 kfree(nfsi->nfs4_acl);
5856 nfsi->nfs4_acl = acl;
5857 spin_unlock(&inode->i_lock);
5860 static void nfs4_zap_acl_attr(struct inode *inode)
5862 nfs4_set_cached_acl(inode, NULL);
5865 static ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf,
5866 size_t buflen, enum nfs4_acl_type type)
5868 struct nfs_inode *nfsi = NFS_I(inode);
5869 struct nfs4_cached_acl *acl;
5872 spin_lock(&inode->i_lock);
5873 acl = nfsi->nfs4_acl;
5876 if (acl->type != type)
5878 if (buf == NULL) /* user is just asking for length */
5880 if (acl->cached == 0)
5882 ret = -ERANGE; /* see getxattr(2) man page */
5883 if (acl->len > buflen)
5885 memcpy(buf, acl->data, acl->len);
5889 spin_unlock(&inode->i_lock);
5893 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages,
5894 size_t pgbase, size_t acl_len,
5895 enum nfs4_acl_type type)
5897 struct nfs4_cached_acl *acl;
5898 size_t buflen = sizeof(*acl) + acl_len;
5900 if (buflen <= PAGE_SIZE) {
5901 acl = kmalloc(buflen, GFP_KERNEL);
5905 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5907 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5915 nfs4_set_cached_acl(inode, acl);
5919 * The getxattr API returns the required buffer length when called with a
5920 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5921 * the required buf. On a NULL buf, we send a page of data to the server
5922 * guessing that the ACL request can be serviced by a page. If so, we cache
5923 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5924 * the cache. If not so, we throw away the page, and cache the required
5925 * length. The next getxattr call will then produce another round trip to
5926 * the server, this time with the input buf of the required size.
5928 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf,
5929 size_t buflen, enum nfs4_acl_type type)
5931 struct page **pages;
5932 struct nfs_getaclargs args = {
5933 .fh = NFS_FH(inode),
5937 struct nfs_getaclres res = {
5941 struct rpc_message msg = {
5942 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5946 unsigned int npages;
5947 int ret = -ENOMEM, i;
5948 struct nfs_server *server = NFS_SERVER(inode);
5951 buflen = server->rsize;
5953 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5954 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
5958 args.acl_pages = pages;
5960 for (i = 0; i < npages; i++) {
5961 pages[i] = alloc_page(GFP_KERNEL);
5966 /* for decoding across pages */
5967 res.acl_scratch = alloc_page(GFP_KERNEL);
5968 if (!res.acl_scratch)
5971 args.acl_len = npages * PAGE_SIZE;
5973 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5974 __func__, buf, buflen, npages, args.acl_len);
5975 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5976 &msg, &args.seq_args, &res.seq_res, 0);
5980 /* Handle the case where the passed-in buffer is too short */
5981 if (res.acl_flags & NFS4_ACL_TRUNC) {
5982 /* Did the user only issue a request for the acl length? */
5988 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len,
5991 if (res.acl_len > buflen) {
5995 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
6001 __free_page(pages[i]);
6002 if (res.acl_scratch)
6003 __free_page(res.acl_scratch);
6008 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf,
6009 size_t buflen, enum nfs4_acl_type type)
6011 struct nfs4_exception exception = {
6012 .interruptible = true,
6016 ret = __nfs4_get_acl_uncached(inode, buf, buflen, type);
6017 trace_nfs4_get_acl(inode, ret);
6020 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
6021 } while (exception.retry);
6025 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen,
6026 enum nfs4_acl_type type)
6028 struct nfs_server *server = NFS_SERVER(inode);
6031 if (!nfs4_server_supports_acls(server, type))
6033 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
6036 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
6037 nfs_zap_acl_cache(inode);
6038 ret = nfs4_read_cached_acl(inode, buf, buflen, type);
6040 /* -ENOENT is returned if there is no ACL or if there is an ACL
6041 * but no cached acl data, just the acl length */
6043 return nfs4_get_acl_uncached(inode, buf, buflen, type);
6046 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf,
6047 size_t buflen, enum nfs4_acl_type type)
6049 struct nfs_server *server = NFS_SERVER(inode);
6050 struct page *pages[NFS4ACL_MAXPAGES];
6051 struct nfs_setaclargs arg = {
6052 .fh = NFS_FH(inode),
6057 struct nfs_setaclres res;
6058 struct rpc_message msg = {
6059 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
6063 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
6066 /* You can't remove system.nfs4_acl: */
6069 if (!nfs4_server_supports_acls(server, type))
6071 if (npages > ARRAY_SIZE(pages))
6073 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
6076 nfs4_inode_make_writeable(inode);
6077 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6080 * Free each page after tx, so the only ref left is
6081 * held by the network stack
6084 put_page(pages[i-1]);
6087 * Acl update can result in inode attribute update.
6088 * so mark the attribute cache invalid.
6090 spin_lock(&inode->i_lock);
6091 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
6092 NFS_INO_INVALID_CTIME |
6093 NFS_INO_REVAL_FORCED);
6094 spin_unlock(&inode->i_lock);
6095 nfs_access_zap_cache(inode);
6096 nfs_zap_acl_cache(inode);
6100 static int nfs4_proc_set_acl(struct inode *inode, const void *buf,
6101 size_t buflen, enum nfs4_acl_type type)
6103 struct nfs4_exception exception = { };
6106 err = __nfs4_proc_set_acl(inode, buf, buflen, type);
6107 trace_nfs4_set_acl(inode, err);
6108 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
6110 * no need to retry since the kernel
6111 * isn't involved in encoding the ACEs.
6116 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6118 } while (exception.retry);
6122 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6123 static int _nfs4_get_security_label(struct inode *inode, void *buf,
6126 struct nfs_server *server = NFS_SERVER(inode);
6127 struct nfs4_label label = {0, 0, buflen, buf};
6129 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6130 struct nfs_fattr fattr = {
6133 struct nfs4_getattr_arg arg = {
6134 .fh = NFS_FH(inode),
6137 struct nfs4_getattr_res res = {
6141 struct rpc_message msg = {
6142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6148 nfs_fattr_init(&fattr);
6150 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6153 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6158 static int nfs4_get_security_label(struct inode *inode, void *buf,
6161 struct nfs4_exception exception = {
6162 .interruptible = true,
6166 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6170 err = _nfs4_get_security_label(inode, buf, buflen);
6171 trace_nfs4_get_security_label(inode, err);
6172 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6174 } while (exception.retry);
6178 static int _nfs4_do_set_security_label(struct inode *inode,
6179 struct nfs4_label *ilabel,
6180 struct nfs_fattr *fattr)
6183 struct iattr sattr = {0};
6184 struct nfs_server *server = NFS_SERVER(inode);
6185 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6186 struct nfs_setattrargs arg = {
6187 .fh = NFS_FH(inode),
6193 struct nfs_setattrres res = {
6197 struct rpc_message msg = {
6198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6204 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6206 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6208 dprintk("%s failed: %d\n", __func__, status);
6213 static int nfs4_do_set_security_label(struct inode *inode,
6214 struct nfs4_label *ilabel,
6215 struct nfs_fattr *fattr)
6217 struct nfs4_exception exception = { };
6221 err = _nfs4_do_set_security_label(inode, ilabel, fattr);
6222 trace_nfs4_set_security_label(inode, err);
6223 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6225 } while (exception.retry);
6230 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6232 struct nfs4_label ilabel = {0, 0, buflen, (char *)buf };
6233 struct nfs_fattr *fattr;
6236 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6239 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
6243 status = nfs4_do_set_security_label(inode, &ilabel, fattr);
6245 nfs_setsecurity(inode, fattr);
6249 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6252 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6253 nfs4_verifier *bootverf)
6257 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6258 /* An impossible timestamp guarantees this value
6259 * will never match a generated boot time. */
6260 verf[0] = cpu_to_be32(U32_MAX);
6261 verf[1] = cpu_to_be32(U32_MAX);
6263 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6264 u64 ns = ktime_to_ns(nn->boot_time);
6266 verf[0] = cpu_to_be32(ns >> 32);
6267 verf[1] = cpu_to_be32(ns);
6269 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6273 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6275 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6276 struct nfs_netns_client *nn_clp = nn->nfs_client;
6283 id = rcu_dereference(nn_clp->identifier);
6285 strscpy(buf, id, buflen);
6289 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6290 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6296 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6298 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6303 if (clp->cl_owner_id != NULL)
6308 strlen(clp->cl_rpcclient->cl_nodename) +
6310 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6314 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6318 if (len > NFS4_OPAQUE_LIMIT + 1)
6322 * Since this string is allocated at mount time, and held until the
6323 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6324 * about a memory-reclaim deadlock.
6326 str = kmalloc(len, GFP_KERNEL);
6332 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6333 clp->cl_rpcclient->cl_nodename, buf,
6334 rpc_peeraddr2str(clp->cl_rpcclient,
6337 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6338 clp->cl_rpcclient->cl_nodename,
6339 rpc_peeraddr2str(clp->cl_rpcclient,
6343 clp->cl_owner_id = str;
6348 nfs4_init_uniform_client_string(struct nfs_client *clp)
6350 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6355 if (clp->cl_owner_id != NULL)
6358 len = 10 + 10 + 1 + 10 + 1 +
6359 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6361 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6365 if (len > NFS4_OPAQUE_LIMIT + 1)
6369 * Since this string is allocated at mount time, and held until the
6370 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6371 * about a memory-reclaim deadlock.
6373 str = kmalloc(len, GFP_KERNEL);
6378 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6379 clp->rpc_ops->version, clp->cl_minorversion,
6380 buf, clp->cl_rpcclient->cl_nodename);
6382 scnprintf(str, len, "Linux NFSv%u.%u %s",
6383 clp->rpc_ops->version, clp->cl_minorversion,
6384 clp->cl_rpcclient->cl_nodename);
6385 clp->cl_owner_id = str;
6390 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6391 * services. Advertise one based on the address family of the
6395 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6397 if (strchr(clp->cl_ipaddr, ':') != NULL)
6398 return scnprintf(buf, len, "tcp6");
6400 return scnprintf(buf, len, "tcp");
6403 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6405 struct nfs4_setclientid *sc = calldata;
6407 if (task->tk_status == 0)
6408 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6411 static const struct rpc_call_ops nfs4_setclientid_ops = {
6412 .rpc_call_done = nfs4_setclientid_done,
6416 * nfs4_proc_setclientid - Negotiate client ID
6417 * @clp: state data structure
6418 * @program: RPC program for NFSv4 callback service
6419 * @port: IP port number for NFS4 callback service
6420 * @cred: credential to use for this call
6421 * @res: where to place the result
6423 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6425 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6426 unsigned short port, const struct cred *cred,
6427 struct nfs4_setclientid_res *res)
6429 nfs4_verifier sc_verifier;
6430 struct nfs4_setclientid setclientid = {
6431 .sc_verifier = &sc_verifier,
6435 struct rpc_message msg = {
6436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6437 .rpc_argp = &setclientid,
6441 struct rpc_task_setup task_setup_data = {
6442 .rpc_client = clp->cl_rpcclient,
6443 .rpc_message = &msg,
6444 .callback_ops = &nfs4_setclientid_ops,
6445 .callback_data = &setclientid,
6446 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6448 unsigned long now = jiffies;
6451 /* nfs_client_id4 */
6452 nfs4_init_boot_verifier(clp, &sc_verifier);
6454 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6455 status = nfs4_init_uniform_client_string(clp);
6457 status = nfs4_init_nonuniform_client_string(clp);
6463 setclientid.sc_netid_len =
6464 nfs4_init_callback_netid(clp,
6465 setclientid.sc_netid,
6466 sizeof(setclientid.sc_netid));
6467 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6468 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6469 clp->cl_ipaddr, port >> 8, port & 255);
6471 dprintk("NFS call setclientid auth=%s, '%s'\n",
6472 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6475 status = nfs4_call_sync_custom(&task_setup_data);
6476 if (setclientid.sc_cred) {
6477 kfree(clp->cl_acceptor);
6478 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6479 put_rpccred(setclientid.sc_cred);
6483 do_renew_lease(clp, now);
6485 trace_nfs4_setclientid(clp, status);
6486 dprintk("NFS reply setclientid: %d\n", status);
6491 * nfs4_proc_setclientid_confirm - Confirm client ID
6492 * @clp: state data structure
6493 * @arg: result of a previous SETCLIENTID
6494 * @cred: credential to use for this call
6496 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6498 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6499 struct nfs4_setclientid_res *arg,
6500 const struct cred *cred)
6502 struct rpc_message msg = {
6503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6509 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6510 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6512 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6513 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6514 trace_nfs4_setclientid_confirm(clp, status);
6515 dprintk("NFS reply setclientid_confirm: %d\n", status);
6519 struct nfs4_delegreturndata {
6520 struct nfs4_delegreturnargs args;
6521 struct nfs4_delegreturnres res;
6523 nfs4_stateid stateid;
6524 unsigned long timestamp;
6526 struct nfs4_layoutreturn_args arg;
6527 struct nfs4_layoutreturn_res res;
6528 struct nfs4_xdr_opaque_data ld_private;
6532 struct nfs_fattr fattr;
6534 struct inode *inode;
6537 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6539 struct nfs4_delegreturndata *data = calldata;
6540 struct nfs4_exception exception = {
6541 .inode = data->inode,
6542 .stateid = &data->stateid,
6543 .task_is_privileged = data->args.seq_args.sa_privileged,
6546 if (!nfs4_sequence_done(task, &data->res.seq_res))
6549 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6551 /* Handle Layoutreturn errors */
6552 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6553 &data->res.lr_ret) == -EAGAIN)
6556 switch (task->tk_status) {
6558 renew_lease(data->res.server, data->timestamp);
6560 case -NFS4ERR_ADMIN_REVOKED:
6561 case -NFS4ERR_DELEG_REVOKED:
6562 case -NFS4ERR_EXPIRED:
6563 nfs4_free_revoked_stateid(data->res.server,
6565 task->tk_msg.rpc_cred);
6567 case -NFS4ERR_BAD_STATEID:
6568 case -NFS4ERR_STALE_STATEID:
6570 task->tk_status = 0;
6572 case -NFS4ERR_OLD_STATEID:
6573 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6574 nfs4_stateid_seqid_inc(&data->stateid);
6575 if (data->args.bitmask) {
6576 data->args.bitmask = NULL;
6577 data->res.fattr = NULL;
6580 case -NFS4ERR_ACCESS:
6581 if (data->args.bitmask) {
6582 data->args.bitmask = NULL;
6583 data->res.fattr = NULL;
6588 task->tk_status = nfs4_async_handle_exception(task,
6589 data->res.server, task->tk_status,
6591 if (exception.retry)
6594 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6595 data->rpc_status = task->tk_status;
6598 task->tk_status = 0;
6599 rpc_restart_call_prepare(task);
6602 static void nfs4_delegreturn_release(void *calldata)
6604 struct nfs4_delegreturndata *data = calldata;
6605 struct inode *inode = data->inode;
6608 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6611 nfs4_fattr_set_prechange(&data->fattr,
6612 inode_peek_iversion_raw(inode));
6613 nfs_refresh_inode(inode, &data->fattr);
6614 nfs_iput_and_deactive(inode);
6619 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6621 struct nfs4_delegreturndata *d_data;
6622 struct pnfs_layout_hdr *lo;
6626 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6627 nfs4_sequence_done(task, &d_data->res.seq_res);
6631 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6632 if (lo && !pnfs_layout_is_valid(lo)) {
6633 d_data->args.lr_args = NULL;
6634 d_data->res.lr_res = NULL;
6637 nfs4_setup_sequence(d_data->res.server->nfs_client,
6638 &d_data->args.seq_args,
6639 &d_data->res.seq_res,
6643 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6644 .rpc_call_prepare = nfs4_delegreturn_prepare,
6645 .rpc_call_done = nfs4_delegreturn_done,
6646 .rpc_release = nfs4_delegreturn_release,
6649 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6651 struct nfs4_delegreturndata *data;
6652 struct nfs_server *server = NFS_SERVER(inode);
6653 struct rpc_task *task;
6654 struct rpc_message msg = {
6655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6658 struct rpc_task_setup task_setup_data = {
6659 .rpc_client = server->client,
6660 .rpc_message = &msg,
6661 .callback_ops = &nfs4_delegreturn_ops,
6662 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6666 if (nfs_server_capable(inode, NFS_CAP_MOVEABLE))
6667 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6669 data = kzalloc(sizeof(*data), GFP_KERNEL);
6673 nfs4_state_protect(server->nfs_client,
6674 NFS_SP4_MACH_CRED_CLEANUP,
6675 &task_setup_data.rpc_client, &msg);
6677 data->args.fhandle = &data->fh;
6678 data->args.stateid = &data->stateid;
6679 nfs4_bitmask_set(data->args.bitmask_store,
6680 server->cache_consistency_bitmask, inode, 0);
6681 data->args.bitmask = data->args.bitmask_store;
6682 nfs_copy_fh(&data->fh, NFS_FH(inode));
6683 nfs4_stateid_copy(&data->stateid, stateid);
6684 data->res.fattr = &data->fattr;
6685 data->res.server = server;
6686 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6687 data->lr.arg.ld_private = &data->lr.ld_private;
6688 nfs_fattr_init(data->res.fattr);
6689 data->timestamp = jiffies;
6690 data->rpc_status = 0;
6691 data->inode = nfs_igrab_and_active(inode);
6692 if (data->inode || issync) {
6693 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6696 data->args.lr_args = &data->lr.arg;
6697 data->res.lr_res = &data->lr.res;
6702 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6705 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6707 task_setup_data.callback_data = data;
6708 msg.rpc_argp = &data->args;
6709 msg.rpc_resp = &data->res;
6710 task = rpc_run_task(&task_setup_data);
6712 return PTR_ERR(task);
6715 status = rpc_wait_for_completion_task(task);
6718 status = data->rpc_status;
6724 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6726 struct nfs_server *server = NFS_SERVER(inode);
6727 struct nfs4_exception exception = { };
6730 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6731 trace_nfs4_delegreturn(inode, stateid, err);
6733 case -NFS4ERR_STALE_STATEID:
6734 case -NFS4ERR_EXPIRED:
6738 err = nfs4_handle_exception(server, err, &exception);
6739 } while (exception.retry);
6743 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6745 struct inode *inode = state->inode;
6746 struct nfs_server *server = NFS_SERVER(inode);
6747 struct nfs_client *clp = server->nfs_client;
6748 struct nfs_lockt_args arg = {
6749 .fh = NFS_FH(inode),
6752 struct nfs_lockt_res res = {
6755 struct rpc_message msg = {
6756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6759 .rpc_cred = state->owner->so_cred,
6761 struct nfs4_lock_state *lsp;
6764 arg.lock_owner.clientid = clp->cl_clientid;
6765 status = nfs4_set_lock_state(state, request);
6768 lsp = request->fl_u.nfs4_fl.owner;
6769 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6770 arg.lock_owner.s_dev = server->s_dev;
6771 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6774 request->fl_type = F_UNLCK;
6776 case -NFS4ERR_DENIED:
6779 request->fl_ops->fl_release_private(request);
6780 request->fl_ops = NULL;
6785 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6787 struct nfs4_exception exception = {
6788 .interruptible = true,
6793 err = _nfs4_proc_getlk(state, cmd, request);
6794 trace_nfs4_get_lock(request, state, cmd, err);
6795 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6797 } while (exception.retry);
6802 * Update the seqid of a lock stateid after receiving
6803 * NFS4ERR_OLD_STATEID
6805 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6806 struct nfs4_lock_state *lsp)
6808 struct nfs4_state *state = lsp->ls_state;
6811 spin_lock(&state->state_lock);
6812 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6814 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6815 nfs4_stateid_seqid_inc(dst);
6817 dst->seqid = lsp->ls_stateid.seqid;
6820 spin_unlock(&state->state_lock);
6824 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6825 struct nfs4_lock_state *lsp)
6827 struct nfs4_state *state = lsp->ls_state;
6830 spin_lock(&state->state_lock);
6831 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6832 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6833 spin_unlock(&state->state_lock);
6837 struct nfs4_unlockdata {
6838 struct nfs_locku_args arg;
6839 struct nfs_locku_res res;
6840 struct nfs4_lock_state *lsp;
6841 struct nfs_open_context *ctx;
6842 struct nfs_lock_context *l_ctx;
6843 struct file_lock fl;
6844 struct nfs_server *server;
6845 unsigned long timestamp;
6848 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6849 struct nfs_open_context *ctx,
6850 struct nfs4_lock_state *lsp,
6851 struct nfs_seqid *seqid)
6853 struct nfs4_unlockdata *p;
6854 struct nfs4_state *state = lsp->ls_state;
6855 struct inode *inode = state->inode;
6857 p = kzalloc(sizeof(*p), GFP_KERNEL);
6860 p->arg.fh = NFS_FH(inode);
6862 p->arg.seqid = seqid;
6863 p->res.seqid = seqid;
6865 /* Ensure we don't close file until we're done freeing locks! */
6866 p->ctx = get_nfs_open_context(ctx);
6867 p->l_ctx = nfs_get_lock_context(ctx);
6868 locks_init_lock(&p->fl);
6869 locks_copy_lock(&p->fl, fl);
6870 p->server = NFS_SERVER(inode);
6871 spin_lock(&state->state_lock);
6872 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6873 spin_unlock(&state->state_lock);
6877 static void nfs4_locku_release_calldata(void *data)
6879 struct nfs4_unlockdata *calldata = data;
6880 nfs_free_seqid(calldata->arg.seqid);
6881 nfs4_put_lock_state(calldata->lsp);
6882 nfs_put_lock_context(calldata->l_ctx);
6883 put_nfs_open_context(calldata->ctx);
6887 static void nfs4_locku_done(struct rpc_task *task, void *data)
6889 struct nfs4_unlockdata *calldata = data;
6890 struct nfs4_exception exception = {
6891 .inode = calldata->lsp->ls_state->inode,
6892 .stateid = &calldata->arg.stateid,
6895 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6897 switch (task->tk_status) {
6899 renew_lease(calldata->server, calldata->timestamp);
6900 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6901 if (nfs4_update_lock_stateid(calldata->lsp,
6902 &calldata->res.stateid))
6905 case -NFS4ERR_ADMIN_REVOKED:
6906 case -NFS4ERR_EXPIRED:
6907 nfs4_free_revoked_stateid(calldata->server,
6908 &calldata->arg.stateid,
6909 task->tk_msg.rpc_cred);
6911 case -NFS4ERR_BAD_STATEID:
6912 case -NFS4ERR_STALE_STATEID:
6913 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6915 rpc_restart_call_prepare(task);
6917 case -NFS4ERR_OLD_STATEID:
6918 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6920 rpc_restart_call_prepare(task);
6923 task->tk_status = nfs4_async_handle_exception(task,
6924 calldata->server, task->tk_status,
6926 if (exception.retry)
6927 rpc_restart_call_prepare(task);
6929 nfs_release_seqid(calldata->arg.seqid);
6932 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6934 struct nfs4_unlockdata *calldata = data;
6936 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6937 nfs_async_iocounter_wait(task, calldata->l_ctx))
6940 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6942 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6943 /* Note: exit _without_ running nfs4_locku_done */
6946 calldata->timestamp = jiffies;
6947 if (nfs4_setup_sequence(calldata->server->nfs_client,
6948 &calldata->arg.seq_args,
6949 &calldata->res.seq_res,
6951 nfs_release_seqid(calldata->arg.seqid);
6954 task->tk_action = NULL;
6956 nfs4_sequence_done(task, &calldata->res.seq_res);
6959 static const struct rpc_call_ops nfs4_locku_ops = {
6960 .rpc_call_prepare = nfs4_locku_prepare,
6961 .rpc_call_done = nfs4_locku_done,
6962 .rpc_release = nfs4_locku_release_calldata,
6965 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6966 struct nfs_open_context *ctx,
6967 struct nfs4_lock_state *lsp,
6968 struct nfs_seqid *seqid)
6970 struct nfs4_unlockdata *data;
6971 struct rpc_message msg = {
6972 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6973 .rpc_cred = ctx->cred,
6975 struct rpc_task_setup task_setup_data = {
6976 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6977 .rpc_message = &msg,
6978 .callback_ops = &nfs4_locku_ops,
6979 .workqueue = nfsiod_workqueue,
6980 .flags = RPC_TASK_ASYNC,
6983 if (nfs_server_capable(lsp->ls_state->inode, NFS_CAP_MOVEABLE))
6984 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6986 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6987 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6989 /* Ensure this is an unlock - when canceling a lock, the
6990 * canceled lock is passed in, and it won't be an unlock.
6992 fl->fl_type = F_UNLCK;
6993 if (fl->fl_flags & FL_CLOSE)
6994 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6996 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6998 nfs_free_seqid(seqid);
6999 return ERR_PTR(-ENOMEM);
7002 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
7003 msg.rpc_argp = &data->arg;
7004 msg.rpc_resp = &data->res;
7005 task_setup_data.callback_data = data;
7006 return rpc_run_task(&task_setup_data);
7009 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
7011 struct inode *inode = state->inode;
7012 struct nfs4_state_owner *sp = state->owner;
7013 struct nfs_inode *nfsi = NFS_I(inode);
7014 struct nfs_seqid *seqid;
7015 struct nfs4_lock_state *lsp;
7016 struct rpc_task *task;
7017 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7019 unsigned char fl_flags = request->fl_flags;
7021 status = nfs4_set_lock_state(state, request);
7022 /* Unlock _before_ we do the RPC call */
7023 request->fl_flags |= FL_EXISTS;
7024 /* Exclude nfs_delegation_claim_locks() */
7025 mutex_lock(&sp->so_delegreturn_mutex);
7026 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
7027 down_read(&nfsi->rwsem);
7028 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
7029 up_read(&nfsi->rwsem);
7030 mutex_unlock(&sp->so_delegreturn_mutex);
7033 lsp = request->fl_u.nfs4_fl.owner;
7034 set_bit(NFS_LOCK_UNLOCKING, &lsp->ls_flags);
7035 up_read(&nfsi->rwsem);
7036 mutex_unlock(&sp->so_delegreturn_mutex);
7039 /* Is this a delegated lock? */
7040 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
7042 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
7043 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
7047 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
7048 status = PTR_ERR(task);
7051 status = rpc_wait_for_completion_task(task);
7054 request->fl_flags = fl_flags;
7055 trace_nfs4_unlock(request, state, F_SETLK, status);
7059 struct nfs4_lockdata {
7060 struct nfs_lock_args arg;
7061 struct nfs_lock_res res;
7062 struct nfs4_lock_state *lsp;
7063 struct nfs_open_context *ctx;
7064 struct file_lock fl;
7065 unsigned long timestamp;
7068 struct nfs_server *server;
7071 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
7072 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
7075 struct nfs4_lockdata *p;
7076 struct inode *inode = lsp->ls_state->inode;
7077 struct nfs_server *server = NFS_SERVER(inode);
7078 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7080 p = kzalloc(sizeof(*p), gfp_mask);
7084 p->arg.fh = NFS_FH(inode);
7086 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
7087 if (IS_ERR(p->arg.open_seqid))
7089 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
7090 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
7091 if (IS_ERR(p->arg.lock_seqid))
7092 goto out_free_seqid;
7093 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
7094 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
7095 p->arg.lock_owner.s_dev = server->s_dev;
7096 p->res.lock_seqid = p->arg.lock_seqid;
7099 p->ctx = get_nfs_open_context(ctx);
7100 locks_init_lock(&p->fl);
7101 locks_copy_lock(&p->fl, fl);
7104 nfs_free_seqid(p->arg.open_seqid);
7110 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7112 struct nfs4_lockdata *data = calldata;
7113 struct nfs4_state *state = data->lsp->ls_state;
7115 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
7117 /* Do we need to do an open_to_lock_owner? */
7118 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
7119 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
7120 goto out_release_lock_seqid;
7122 nfs4_stateid_copy(&data->arg.open_stateid,
7123 &state->open_stateid);
7124 data->arg.new_lock_owner = 1;
7125 data->res.open_seqid = data->arg.open_seqid;
7127 data->arg.new_lock_owner = 0;
7128 nfs4_stateid_copy(&data->arg.lock_stateid,
7129 &data->lsp->ls_stateid);
7131 if (!nfs4_valid_open_stateid(state)) {
7132 data->rpc_status = -EBADF;
7133 task->tk_action = NULL;
7134 goto out_release_open_seqid;
7136 data->timestamp = jiffies;
7137 if (nfs4_setup_sequence(data->server->nfs_client,
7138 &data->arg.seq_args,
7142 out_release_open_seqid:
7143 nfs_release_seqid(data->arg.open_seqid);
7144 out_release_lock_seqid:
7145 nfs_release_seqid(data->arg.lock_seqid);
7147 nfs4_sequence_done(task, &data->res.seq_res);
7148 dprintk("%s: ret = %d\n", __func__, data->rpc_status);
7151 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7153 struct nfs4_lockdata *data = calldata;
7154 struct nfs4_lock_state *lsp = data->lsp;
7156 if (!nfs4_sequence_done(task, &data->res.seq_res))
7159 data->rpc_status = task->tk_status;
7160 switch (task->tk_status) {
7162 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
7164 if (data->arg.new_lock && !data->cancelled) {
7165 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
7166 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7169 if (data->arg.new_lock_owner != 0) {
7170 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7171 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7172 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7173 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7176 case -NFS4ERR_OLD_STATEID:
7177 if (data->arg.new_lock_owner != 0 &&
7178 nfs4_refresh_open_old_stateid(&data->arg.open_stateid,
7181 if (nfs4_refresh_lock_old_stateid(&data->arg.lock_stateid, lsp))
7184 case -NFS4ERR_BAD_STATEID:
7185 case -NFS4ERR_STALE_STATEID:
7186 case -NFS4ERR_EXPIRED:
7187 if (data->arg.new_lock_owner != 0) {
7188 if (!nfs4_stateid_match(&data->arg.open_stateid,
7189 &lsp->ls_state->open_stateid))
7191 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7196 dprintk("%s: ret = %d!\n", __func__, data->rpc_status);
7199 if (!data->cancelled)
7200 rpc_restart_call_prepare(task);
7204 static void nfs4_lock_release(void *calldata)
7206 struct nfs4_lockdata *data = calldata;
7208 nfs_free_seqid(data->arg.open_seqid);
7209 if (data->cancelled && data->rpc_status == 0) {
7210 struct rpc_task *task;
7211 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7212 data->arg.lock_seqid);
7214 rpc_put_task_async(task);
7215 dprintk("%s: cancelling lock!\n", __func__);
7217 nfs_free_seqid(data->arg.lock_seqid);
7218 nfs4_put_lock_state(data->lsp);
7219 put_nfs_open_context(data->ctx);
7223 static const struct rpc_call_ops nfs4_lock_ops = {
7224 .rpc_call_prepare = nfs4_lock_prepare,
7225 .rpc_call_done = nfs4_lock_done,
7226 .rpc_release = nfs4_lock_release,
7229 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7232 case -NFS4ERR_ADMIN_REVOKED:
7233 case -NFS4ERR_EXPIRED:
7234 case -NFS4ERR_BAD_STATEID:
7235 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7236 if (new_lock_owner != 0 ||
7237 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7238 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7240 case -NFS4ERR_STALE_STATEID:
7241 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7242 nfs4_schedule_lease_recovery(server->nfs_client);
7246 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7248 struct nfs4_lockdata *data;
7249 struct rpc_task *task;
7250 struct rpc_message msg = {
7251 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7252 .rpc_cred = state->owner->so_cred,
7254 struct rpc_task_setup task_setup_data = {
7255 .rpc_client = NFS_CLIENT(state->inode),
7256 .rpc_message = &msg,
7257 .callback_ops = &nfs4_lock_ops,
7258 .workqueue = nfsiod_workqueue,
7259 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7263 if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
7264 task_setup_data.flags |= RPC_TASK_MOVEABLE;
7266 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7267 fl->fl_u.nfs4_fl.owner, GFP_KERNEL);
7271 data->arg.block = 1;
7272 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7273 recovery_type > NFS_LOCK_NEW);
7274 msg.rpc_argp = &data->arg;
7275 msg.rpc_resp = &data->res;
7276 task_setup_data.callback_data = data;
7277 if (recovery_type > NFS_LOCK_NEW) {
7278 if (recovery_type == NFS_LOCK_RECLAIM)
7279 data->arg.reclaim = NFS_LOCK_RECLAIM;
7281 data->arg.new_lock = 1;
7282 task = rpc_run_task(&task_setup_data);
7284 return PTR_ERR(task);
7285 ret = rpc_wait_for_completion_task(task);
7287 ret = data->rpc_status;
7289 nfs4_handle_setlk_error(data->server, data->lsp,
7290 data->arg.new_lock_owner, ret);
7292 data->cancelled = true;
7293 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7295 dprintk("%s: ret = %d\n", __func__, ret);
7299 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7301 struct nfs_server *server = NFS_SERVER(state->inode);
7302 struct nfs4_exception exception = {
7303 .inode = state->inode,
7308 /* Cache the lock if possible... */
7309 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7311 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7312 if (err != -NFS4ERR_DELAY)
7314 nfs4_handle_exception(server, err, &exception);
7315 } while (exception.retry);
7319 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7321 struct nfs_server *server = NFS_SERVER(state->inode);
7322 struct nfs4_exception exception = {
7323 .inode = state->inode,
7327 err = nfs4_set_lock_state(state, request);
7330 if (!recover_lost_locks) {
7331 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7335 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7337 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7341 case -NFS4ERR_GRACE:
7342 case -NFS4ERR_DELAY:
7343 nfs4_handle_exception(server, err, &exception);
7346 } while (exception.retry);
7351 #if defined(CONFIG_NFS_V4_1)
7352 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7354 struct nfs4_lock_state *lsp;
7357 status = nfs4_set_lock_state(state, request);
7360 lsp = request->fl_u.nfs4_fl.owner;
7361 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7362 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7364 return nfs4_lock_expired(state, request);
7368 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7370 struct nfs_inode *nfsi = NFS_I(state->inode);
7371 struct nfs4_state_owner *sp = state->owner;
7372 unsigned char fl_flags = request->fl_flags;
7375 request->fl_flags |= FL_ACCESS;
7376 status = locks_lock_inode_wait(state->inode, request);
7379 mutex_lock(&sp->so_delegreturn_mutex);
7380 down_read(&nfsi->rwsem);
7381 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7382 /* Yes: cache locks! */
7383 /* ...but avoid races with delegation recall... */
7384 request->fl_flags = fl_flags & ~FL_SLEEP;
7385 status = locks_lock_inode_wait(state->inode, request);
7386 up_read(&nfsi->rwsem);
7387 mutex_unlock(&sp->so_delegreturn_mutex);
7390 up_read(&nfsi->rwsem);
7391 mutex_unlock(&sp->so_delegreturn_mutex);
7392 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7394 request->fl_flags = fl_flags;
7398 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7400 struct nfs4_exception exception = {
7402 .inode = state->inode,
7403 .interruptible = true,
7408 err = _nfs4_proc_setlk(state, cmd, request);
7409 if (err == -NFS4ERR_DENIED)
7411 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7413 } while (exception.retry);
7417 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7418 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7421 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7422 struct file_lock *request)
7424 int status = -ERESTARTSYS;
7425 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7427 while(!signalled()) {
7428 status = nfs4_proc_setlk(state, cmd, request);
7429 if ((status != -EAGAIN) || IS_SETLK(cmd))
7431 __set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
7432 schedule_timeout(timeout);
7434 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7435 status = -ERESTARTSYS;
7440 #ifdef CONFIG_NFS_V4_1
7441 struct nfs4_lock_waiter {
7442 struct inode *inode;
7443 struct nfs_lowner owner;
7444 wait_queue_entry_t wait;
7448 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7450 struct nfs4_lock_waiter *waiter =
7451 container_of(wait, struct nfs4_lock_waiter, wait);
7453 /* NULL key means to wake up everyone */
7455 struct cb_notify_lock_args *cbnl = key;
7456 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7457 *wowner = &waiter->owner;
7459 /* Only wake if the callback was for the same owner. */
7460 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7463 /* Make sure it's for the right inode */
7464 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7468 return woken_wake_function(wait, mode, flags, key);
7472 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7474 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7475 struct nfs_server *server = NFS_SERVER(state->inode);
7476 struct nfs_client *clp = server->nfs_client;
7477 wait_queue_head_t *q = &clp->cl_lock_waitq;
7478 struct nfs4_lock_waiter waiter = {
7479 .inode = state->inode,
7480 .owner = { .clientid = clp->cl_clientid,
7481 .id = lsp->ls_seqid.owner_id,
7482 .s_dev = server->s_dev },
7486 /* Don't bother with waitqueue if we don't expect a callback */
7487 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7488 return nfs4_retry_setlk_simple(state, cmd, request);
7490 init_wait(&waiter.wait);
7491 waiter.wait.func = nfs4_wake_lock_waiter;
7492 add_wait_queue(q, &waiter.wait);
7495 status = nfs4_proc_setlk(state, cmd, request);
7496 if (status != -EAGAIN || IS_SETLK(cmd))
7499 status = -ERESTARTSYS;
7500 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE|TASK_FREEZABLE,
7501 NFS4_LOCK_MAXTIMEOUT);
7502 } while (!signalled());
7504 remove_wait_queue(q, &waiter.wait);
7508 #else /* !CONFIG_NFS_V4_1 */
7510 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7512 return nfs4_retry_setlk_simple(state, cmd, request);
7517 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7519 struct nfs_open_context *ctx;
7520 struct nfs4_state *state;
7523 /* verify open state */
7524 ctx = nfs_file_open_context(filp);
7527 if (IS_GETLK(cmd)) {
7529 return nfs4_proc_getlk(state, F_GETLK, request);
7533 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7536 if (request->fl_type == F_UNLCK) {
7538 return nfs4_proc_unlck(state, cmd, request);
7545 if ((request->fl_flags & FL_POSIX) &&
7546 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7550 * Don't rely on the VFS having checked the file open mode,
7551 * since it won't do this for flock() locks.
7553 switch (request->fl_type) {
7555 if (!(filp->f_mode & FMODE_READ))
7559 if (!(filp->f_mode & FMODE_WRITE))
7563 status = nfs4_set_lock_state(state, request);
7567 return nfs4_retry_setlk(state, cmd, request);
7570 static int nfs4_delete_lease(struct file *file, void **priv)
7572 return generic_setlease(file, F_UNLCK, NULL, priv);
7575 static int nfs4_add_lease(struct file *file, int arg, struct file_lock **lease,
7578 struct inode *inode = file_inode(file);
7579 fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7582 /* No delegation, no lease */
7583 if (!nfs4_have_delegation(inode, type))
7585 ret = generic_setlease(file, arg, lease, priv);
7586 if (ret || nfs4_have_delegation(inode, type))
7588 /* We raced with a delegation return */
7589 nfs4_delete_lease(file, priv);
7593 int nfs4_proc_setlease(struct file *file, int arg, struct file_lock **lease,
7599 return nfs4_add_lease(file, arg, lease, priv);
7601 return nfs4_delete_lease(file, priv);
7607 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7609 struct nfs_server *server = NFS_SERVER(state->inode);
7612 err = nfs4_set_lock_state(state, fl);
7616 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7617 if (err != -NFS4ERR_DELAY)
7620 } while (err == -NFS4ERR_DELAY);
7621 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7624 struct nfs_release_lockowner_data {
7625 struct nfs4_lock_state *lsp;
7626 struct nfs_server *server;
7627 struct nfs_release_lockowner_args args;
7628 struct nfs_release_lockowner_res res;
7629 unsigned long timestamp;
7632 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7634 struct nfs_release_lockowner_data *data = calldata;
7635 struct nfs_server *server = data->server;
7636 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7637 &data->res.seq_res, task);
7638 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7639 data->timestamp = jiffies;
7642 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7644 struct nfs_release_lockowner_data *data = calldata;
7645 struct nfs_server *server = data->server;
7647 nfs40_sequence_done(task, &data->res.seq_res);
7649 switch (task->tk_status) {
7651 renew_lease(server, data->timestamp);
7653 case -NFS4ERR_STALE_CLIENTID:
7654 case -NFS4ERR_EXPIRED:
7655 nfs4_schedule_lease_recovery(server->nfs_client);
7657 case -NFS4ERR_LEASE_MOVED:
7658 case -NFS4ERR_DELAY:
7659 if (nfs4_async_handle_error(task, server,
7660 NULL, NULL) == -EAGAIN)
7661 rpc_restart_call_prepare(task);
7665 static void nfs4_release_lockowner_release(void *calldata)
7667 struct nfs_release_lockowner_data *data = calldata;
7668 nfs4_free_lock_state(data->server, data->lsp);
7672 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7673 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7674 .rpc_call_done = nfs4_release_lockowner_done,
7675 .rpc_release = nfs4_release_lockowner_release,
7679 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7681 struct nfs_release_lockowner_data *data;
7682 struct rpc_message msg = {
7683 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7686 if (server->nfs_client->cl_mvops->minor_version != 0)
7689 data = kmalloc(sizeof(*data), GFP_KERNEL);
7693 data->server = server;
7694 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7695 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7696 data->args.lock_owner.s_dev = server->s_dev;
7698 msg.rpc_argp = &data->args;
7699 msg.rpc_resp = &data->res;
7700 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7701 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7704 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7706 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7707 struct mnt_idmap *idmap,
7708 struct dentry *unused, struct inode *inode,
7709 const char *key, const void *buf,
7710 size_t buflen, int flags)
7712 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_ACL);
7715 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7716 struct dentry *unused, struct inode *inode,
7717 const char *key, void *buf, size_t buflen)
7719 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_ACL);
7722 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7724 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_ACL);
7727 #if defined(CONFIG_NFS_V4_1)
7728 #define XATTR_NAME_NFSV4_DACL "system.nfs4_dacl"
7730 static int nfs4_xattr_set_nfs4_dacl(const struct xattr_handler *handler,
7731 struct mnt_idmap *idmap,
7732 struct dentry *unused, struct inode *inode,
7733 const char *key, const void *buf,
7734 size_t buflen, int flags)
7736 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_DACL);
7739 static int nfs4_xattr_get_nfs4_dacl(const struct xattr_handler *handler,
7740 struct dentry *unused, struct inode *inode,
7741 const char *key, void *buf, size_t buflen)
7743 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_DACL);
7746 static bool nfs4_xattr_list_nfs4_dacl(struct dentry *dentry)
7748 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_DACL);
7751 #define XATTR_NAME_NFSV4_SACL "system.nfs4_sacl"
7753 static int nfs4_xattr_set_nfs4_sacl(const struct xattr_handler *handler,
7754 struct mnt_idmap *idmap,
7755 struct dentry *unused, struct inode *inode,
7756 const char *key, const void *buf,
7757 size_t buflen, int flags)
7759 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_SACL);
7762 static int nfs4_xattr_get_nfs4_sacl(const struct xattr_handler *handler,
7763 struct dentry *unused, struct inode *inode,
7764 const char *key, void *buf, size_t buflen)
7766 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_SACL);
7769 static bool nfs4_xattr_list_nfs4_sacl(struct dentry *dentry)
7771 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_SACL);
7776 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7778 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7779 struct mnt_idmap *idmap,
7780 struct dentry *unused, struct inode *inode,
7781 const char *key, const void *buf,
7782 size_t buflen, int flags)
7784 if (security_ismaclabel(key))
7785 return nfs4_set_security_label(inode, buf, buflen);
7790 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7791 struct dentry *unused, struct inode *inode,
7792 const char *key, void *buf, size_t buflen)
7794 if (security_ismaclabel(key))
7795 return nfs4_get_security_label(inode, buf, buflen);
7800 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7804 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7805 len = security_inode_listsecurity(inode, list, list_len);
7806 if (len >= 0 && list_len && len > list_len)
7812 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7813 .prefix = XATTR_SECURITY_PREFIX,
7814 .get = nfs4_xattr_get_nfs4_label,
7815 .set = nfs4_xattr_set_nfs4_label,
7821 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7828 #ifdef CONFIG_NFS_V4_2
7829 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7830 struct mnt_idmap *idmap,
7831 struct dentry *unused, struct inode *inode,
7832 const char *key, const void *buf,
7833 size_t buflen, int flags)
7838 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7842 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7843 * flags right now. Handling of xattr operations use the normal
7844 * file read/write permissions.
7846 * Just in case the server has other ideas (which RFC 8276 allows),
7847 * do a cached access check for the XA* flags to possibly avoid
7848 * doing an RPC and getting EACCES back.
7850 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7851 if (!(mask & NFS_ACCESS_XAWRITE))
7856 ret = nfs42_proc_removexattr(inode, key);
7858 nfs4_xattr_cache_remove(inode, key);
7860 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7862 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7868 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7869 struct dentry *unused, struct inode *inode,
7870 const char *key, void *buf, size_t buflen)
7875 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7878 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7879 if (!(mask & NFS_ACCESS_XAREAD))
7883 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7887 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7888 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7891 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7897 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7906 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7909 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7910 if (!(mask & NFS_ACCESS_XALIST))
7914 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7918 ret = nfs4_xattr_cache_list(inode, list, list_len);
7919 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7924 buflen = list_len ? list_len : XATTR_LIST_MAX;
7925 buf = list_len ? list : NULL;
7929 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7942 nfs4_xattr_cache_set_list(inode, list, size);
7950 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7954 #endif /* CONFIG_NFS_V4_2 */
7957 * nfs_fhget will use either the mounted_on_fileid or the fileid
7959 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7961 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7962 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7963 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7964 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7967 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7968 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7969 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7973 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7974 const struct qstr *name,
7975 struct nfs4_fs_locations *fs_locations,
7978 struct nfs_server *server = NFS_SERVER(dir);
7980 struct nfs4_fs_locations_arg args = {
7981 .dir_fh = NFS_FH(dir),
7986 struct nfs4_fs_locations_res res = {
7987 .fs_locations = fs_locations,
7989 struct rpc_message msg = {
7990 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7996 dprintk("%s: start\n", __func__);
7998 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7999 bitmask[1] = nfs4_fattr_bitmap[1];
8001 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
8002 * is not supported */
8003 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
8004 bitmask[0] &= ~FATTR4_WORD0_FILEID;
8006 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
8008 nfs_fattr_init(fs_locations->fattr);
8009 fs_locations->server = server;
8010 fs_locations->nlocations = 0;
8011 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
8012 dprintk("%s: returned status = %d\n", __func__, status);
8016 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
8017 const struct qstr *name,
8018 struct nfs4_fs_locations *fs_locations,
8021 struct nfs4_exception exception = {
8022 .interruptible = true,
8026 err = _nfs4_proc_fs_locations(client, dir, name,
8027 fs_locations, page);
8028 trace_nfs4_get_fs_locations(dir, name, err);
8029 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8031 } while (exception.retry);
8036 * This operation also signals the server that this client is
8037 * performing migration recovery. The server can stop returning
8038 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
8039 * appended to this compound to identify the client ID which is
8040 * performing recovery.
8042 static int _nfs40_proc_get_locations(struct nfs_server *server,
8043 struct nfs_fh *fhandle,
8044 struct nfs4_fs_locations *locations,
8045 struct page *page, const struct cred *cred)
8047 struct rpc_clnt *clnt = server->client;
8049 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8051 struct nfs4_fs_locations_arg args = {
8052 .clientid = server->nfs_client->cl_clientid,
8056 .migration = 1, /* skip LOOKUP */
8057 .renew = 1, /* append RENEW */
8059 struct nfs4_fs_locations_res res = {
8060 .fs_locations = locations,
8064 struct rpc_message msg = {
8065 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8070 unsigned long now = jiffies;
8073 nfs_fattr_init(locations->fattr);
8074 locations->server = server;
8075 locations->nlocations = 0;
8077 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8078 status = nfs4_call_sync_sequence(clnt, server, &msg,
8079 &args.seq_args, &res.seq_res);
8083 renew_lease(server, now);
8087 #ifdef CONFIG_NFS_V4_1
8090 * This operation also signals the server that this client is
8091 * performing migration recovery. The server can stop asserting
8092 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
8093 * performing this operation is identified in the SEQUENCE
8094 * operation in this compound.
8096 * When the client supports GETATTR(fs_locations_info), it can
8097 * be plumbed in here.
8099 static int _nfs41_proc_get_locations(struct nfs_server *server,
8100 struct nfs_fh *fhandle,
8101 struct nfs4_fs_locations *locations,
8102 struct page *page, const struct cred *cred)
8104 struct rpc_clnt *clnt = server->client;
8106 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8108 struct nfs4_fs_locations_arg args = {
8112 .migration = 1, /* skip LOOKUP */
8114 struct nfs4_fs_locations_res res = {
8115 .fs_locations = locations,
8118 struct rpc_message msg = {
8119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8124 struct nfs4_call_sync_data data = {
8125 .seq_server = server,
8126 .seq_args = &args.seq_args,
8127 .seq_res = &res.seq_res,
8129 struct rpc_task_setup task_setup_data = {
8131 .rpc_message = &msg,
8132 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
8133 .callback_data = &data,
8134 .flags = RPC_TASK_NO_ROUND_ROBIN,
8138 nfs_fattr_init(locations->fattr);
8139 locations->server = server;
8140 locations->nlocations = 0;
8142 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8143 status = nfs4_call_sync_custom(&task_setup_data);
8144 if (status == NFS4_OK &&
8145 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8146 status = -NFS4ERR_LEASE_MOVED;
8150 #endif /* CONFIG_NFS_V4_1 */
8153 * nfs4_proc_get_locations - discover locations for a migrated FSID
8154 * @server: pointer to nfs_server to process
8155 * @fhandle: pointer to the kernel NFS client file handle
8156 * @locations: result of query
8158 * @cred: credential to use for this operation
8160 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
8161 * operation failed, or a negative errno if a local error occurred.
8163 * On success, "locations" is filled in, but if the server has
8164 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
8167 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
8168 * from this client that require migration recovery.
8170 int nfs4_proc_get_locations(struct nfs_server *server,
8171 struct nfs_fh *fhandle,
8172 struct nfs4_fs_locations *locations,
8173 struct page *page, const struct cred *cred)
8175 struct nfs_client *clp = server->nfs_client;
8176 const struct nfs4_mig_recovery_ops *ops =
8177 clp->cl_mvops->mig_recovery_ops;
8178 struct nfs4_exception exception = {
8179 .interruptible = true,
8183 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8184 (unsigned long long)server->fsid.major,
8185 (unsigned long long)server->fsid.minor,
8187 nfs_display_fhandle(fhandle, __func__);
8190 status = ops->get_locations(server, fhandle, locations, page,
8192 if (status != -NFS4ERR_DELAY)
8194 nfs4_handle_exception(server, status, &exception);
8195 } while (exception.retry);
8200 * This operation also signals the server that this client is
8201 * performing "lease moved" recovery. The server can stop
8202 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
8203 * is appended to this compound to identify the client ID which is
8204 * performing recovery.
8206 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
8208 struct nfs_server *server = NFS_SERVER(inode);
8209 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8210 struct rpc_clnt *clnt = server->client;
8211 struct nfs4_fsid_present_arg args = {
8212 .fh = NFS_FH(inode),
8213 .clientid = clp->cl_clientid,
8214 .renew = 1, /* append RENEW */
8216 struct nfs4_fsid_present_res res = {
8219 struct rpc_message msg = {
8220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8225 unsigned long now = jiffies;
8228 res.fh = nfs_alloc_fhandle();
8232 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8233 status = nfs4_call_sync_sequence(clnt, server, &msg,
8234 &args.seq_args, &res.seq_res);
8235 nfs_free_fhandle(res.fh);
8239 do_renew_lease(clp, now);
8243 #ifdef CONFIG_NFS_V4_1
8246 * This operation also signals the server that this client is
8247 * performing "lease moved" recovery. The server can stop asserting
8248 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8249 * this operation is identified in the SEQUENCE operation in this
8252 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8254 struct nfs_server *server = NFS_SERVER(inode);
8255 struct rpc_clnt *clnt = server->client;
8256 struct nfs4_fsid_present_arg args = {
8257 .fh = NFS_FH(inode),
8259 struct nfs4_fsid_present_res res = {
8261 struct rpc_message msg = {
8262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8269 res.fh = nfs_alloc_fhandle();
8273 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8274 status = nfs4_call_sync_sequence(clnt, server, &msg,
8275 &args.seq_args, &res.seq_res);
8276 nfs_free_fhandle(res.fh);
8277 if (status == NFS4_OK &&
8278 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8279 status = -NFS4ERR_LEASE_MOVED;
8283 #endif /* CONFIG_NFS_V4_1 */
8286 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8287 * @inode: inode on FSID to check
8288 * @cred: credential to use for this operation
8290 * Server indicates whether the FSID is present, moved, or not
8291 * recognized. This operation is necessary to clear a LEASE_MOVED
8292 * condition for this client ID.
8294 * Returns NFS4_OK if the FSID is present on this server,
8295 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8296 * NFS4ERR code if some error occurred on the server, or a
8297 * negative errno if a local failure occurred.
8299 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8301 struct nfs_server *server = NFS_SERVER(inode);
8302 struct nfs_client *clp = server->nfs_client;
8303 const struct nfs4_mig_recovery_ops *ops =
8304 clp->cl_mvops->mig_recovery_ops;
8305 struct nfs4_exception exception = {
8306 .interruptible = true,
8310 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8311 (unsigned long long)server->fsid.major,
8312 (unsigned long long)server->fsid.minor,
8314 nfs_display_fhandle(NFS_FH(inode), __func__);
8317 status = ops->fsid_present(inode, cred);
8318 if (status != -NFS4ERR_DELAY)
8320 nfs4_handle_exception(server, status, &exception);
8321 } while (exception.retry);
8326 * If 'use_integrity' is true and the state managment nfs_client
8327 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8328 * and the machine credential as per RFC3530bis and RFC5661 Security
8329 * Considerations sections. Otherwise, just use the user cred with the
8330 * filesystem's rpc_client.
8332 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8335 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8336 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8337 struct nfs4_secinfo_arg args = {
8338 .dir_fh = NFS_FH(dir),
8341 struct nfs4_secinfo_res res = {
8344 struct rpc_message msg = {
8345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8349 struct nfs4_call_sync_data data = {
8350 .seq_server = NFS_SERVER(dir),
8351 .seq_args = &args.seq_args,
8352 .seq_res = &res.seq_res,
8354 struct rpc_task_setup task_setup = {
8356 .rpc_message = &msg,
8357 .callback_ops = clp->cl_mvops->call_sync_ops,
8358 .callback_data = &data,
8359 .flags = RPC_TASK_NO_ROUND_ROBIN,
8361 const struct cred *cred = NULL;
8363 if (use_integrity) {
8364 clnt = clp->cl_rpcclient;
8365 task_setup.rpc_client = clnt;
8367 cred = nfs4_get_clid_cred(clp);
8368 msg.rpc_cred = cred;
8371 dprintk("NFS call secinfo %s\n", name->name);
8373 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8374 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8375 status = nfs4_call_sync_custom(&task_setup);
8377 dprintk("NFS reply secinfo: %d\n", status);
8383 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8384 struct nfs4_secinfo_flavors *flavors)
8386 struct nfs4_exception exception = {
8387 .interruptible = true,
8391 err = -NFS4ERR_WRONGSEC;
8393 /* try to use integrity protection with machine cred */
8394 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8395 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8398 * if unable to use integrity protection, or SECINFO with
8399 * integrity protection returns NFS4ERR_WRONGSEC (which is
8400 * disallowed by spec, but exists in deployed servers) use
8401 * the current filesystem's rpc_client and the user cred.
8403 if (err == -NFS4ERR_WRONGSEC)
8404 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8406 trace_nfs4_secinfo(dir, name, err);
8407 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8409 } while (exception.retry);
8413 #ifdef CONFIG_NFS_V4_1
8415 * Check the exchange flags returned by the server for invalid flags, having
8416 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8419 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8421 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8423 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8425 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8426 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8428 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8432 return -NFS4ERR_INVAL;
8436 nfs41_same_server_scope(struct nfs41_server_scope *a,
8437 struct nfs41_server_scope *b)
8439 if (a->server_scope_sz != b->server_scope_sz)
8441 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8445 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8447 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8448 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8449 struct nfs_client *clp = args->client;
8451 switch (task->tk_status) {
8452 case -NFS4ERR_BADSESSION:
8453 case -NFS4ERR_DEADSESSION:
8454 nfs4_schedule_session_recovery(clp->cl_session,
8458 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8459 res->dir != NFS4_CDFS4_BOTH) {
8460 rpc_task_close_connection(task);
8461 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8462 rpc_restart_call(task);
8466 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8467 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8471 * nfs4_proc_bind_one_conn_to_session()
8473 * The 4.1 client currently uses the same TCP connection for the
8474 * fore and backchannel.
8477 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8478 struct rpc_xprt *xprt,
8479 struct nfs_client *clp,
8480 const struct cred *cred)
8483 struct nfs41_bind_conn_to_session_args args = {
8485 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8488 struct nfs41_bind_conn_to_session_res res;
8489 struct rpc_message msg = {
8491 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8496 struct rpc_task_setup task_setup_data = {
8499 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8500 .rpc_message = &msg,
8501 .flags = RPC_TASK_TIMEOUT,
8503 struct rpc_task *task;
8505 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8506 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8507 args.dir = NFS4_CDFC4_FORE;
8509 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8510 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8511 args.dir = NFS4_CDFC4_FORE;
8513 task = rpc_run_task(&task_setup_data);
8514 if (!IS_ERR(task)) {
8515 status = task->tk_status;
8518 status = PTR_ERR(task);
8519 trace_nfs4_bind_conn_to_session(clp, status);
8521 if (memcmp(res.sessionid.data,
8522 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8523 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8526 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8527 dprintk("NFS: %s: Unexpected direction from server\n",
8531 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8532 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8541 struct rpc_bind_conn_calldata {
8542 struct nfs_client *clp;
8543 const struct cred *cred;
8547 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8548 struct rpc_xprt *xprt,
8551 struct rpc_bind_conn_calldata *p = calldata;
8553 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8556 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8558 struct rpc_bind_conn_calldata data = {
8562 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8563 nfs4_proc_bind_conn_to_session_callback, &data);
8567 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8568 * and operations we'd like to see to enable certain features in the allow map
8570 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8571 .how = SP4_MACH_CRED,
8572 .enforce.u.words = {
8573 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8574 1 << (OP_EXCHANGE_ID - 32) |
8575 1 << (OP_CREATE_SESSION - 32) |
8576 1 << (OP_DESTROY_SESSION - 32) |
8577 1 << (OP_DESTROY_CLIENTID - 32)
8580 [0] = 1 << (OP_CLOSE) |
8581 1 << (OP_OPEN_DOWNGRADE) |
8583 1 << (OP_DELEGRETURN) |
8585 [1] = 1 << (OP_SECINFO - 32) |
8586 1 << (OP_SECINFO_NO_NAME - 32) |
8587 1 << (OP_LAYOUTRETURN - 32) |
8588 1 << (OP_TEST_STATEID - 32) |
8589 1 << (OP_FREE_STATEID - 32) |
8590 1 << (OP_WRITE - 32)
8595 * Select the state protection mode for client `clp' given the server results
8596 * from exchange_id in `sp'.
8598 * Returns 0 on success, negative errno otherwise.
8600 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8601 struct nfs41_state_protection *sp)
8603 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8604 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8605 1 << (OP_EXCHANGE_ID - 32) |
8606 1 << (OP_CREATE_SESSION - 32) |
8607 1 << (OP_DESTROY_SESSION - 32) |
8608 1 << (OP_DESTROY_CLIENTID - 32)
8610 unsigned long flags = 0;
8614 if (sp->how == SP4_MACH_CRED) {
8615 /* Print state protect result */
8616 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8617 for (i = 0; i <= LAST_NFS4_OP; i++) {
8618 if (test_bit(i, sp->enforce.u.longs))
8619 dfprintk(MOUNT, " enforce op %d\n", i);
8620 if (test_bit(i, sp->allow.u.longs))
8621 dfprintk(MOUNT, " allow op %d\n", i);
8624 /* make sure nothing is on enforce list that isn't supported */
8625 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8626 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8627 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8634 * Minimal mode - state operations are allowed to use machine
8635 * credential. Note this already happens by default, so the
8636 * client doesn't have to do anything more than the negotiation.
8638 * NOTE: we don't care if EXCHANGE_ID is in the list -
8639 * we're already using the machine cred for exchange_id
8640 * and will never use a different cred.
8642 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8643 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8644 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8645 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8646 dfprintk(MOUNT, "sp4_mach_cred:\n");
8647 dfprintk(MOUNT, " minimal mode enabled\n");
8648 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8650 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8655 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8656 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8657 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8658 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8659 dfprintk(MOUNT, " cleanup mode enabled\n");
8660 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8663 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8664 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8665 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8668 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8669 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8670 dfprintk(MOUNT, " secinfo mode enabled\n");
8671 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8674 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8675 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8676 dfprintk(MOUNT, " stateid mode enabled\n");
8677 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8680 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8681 dfprintk(MOUNT, " write mode enabled\n");
8682 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8685 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8686 dfprintk(MOUNT, " commit mode enabled\n");
8687 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8691 clp->cl_sp4_flags = flags;
8695 struct nfs41_exchange_id_data {
8696 struct nfs41_exchange_id_res res;
8697 struct nfs41_exchange_id_args args;
8700 static void nfs4_exchange_id_release(void *data)
8702 struct nfs41_exchange_id_data *cdata =
8703 (struct nfs41_exchange_id_data *)data;
8705 nfs_put_client(cdata->args.client);
8706 kfree(cdata->res.impl_id);
8707 kfree(cdata->res.server_scope);
8708 kfree(cdata->res.server_owner);
8712 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8713 .rpc_release = nfs4_exchange_id_release,
8717 * _nfs4_proc_exchange_id()
8719 * Wrapper for EXCHANGE_ID operation.
8721 static struct rpc_task *
8722 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8723 u32 sp4_how, struct rpc_xprt *xprt)
8725 struct rpc_message msg = {
8726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8729 struct rpc_task_setup task_setup_data = {
8730 .rpc_client = clp->cl_rpcclient,
8731 .callback_ops = &nfs4_exchange_id_call_ops,
8732 .rpc_message = &msg,
8733 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8735 struct nfs41_exchange_id_data *calldata;
8738 if (!refcount_inc_not_zero(&clp->cl_count))
8739 return ERR_PTR(-EIO);
8742 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8746 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8748 status = nfs4_init_uniform_client_string(clp);
8752 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8755 if (unlikely(calldata->res.server_owner == NULL))
8758 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8760 if (unlikely(calldata->res.server_scope == NULL))
8761 goto out_server_owner;
8763 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8764 if (unlikely(calldata->res.impl_id == NULL))
8765 goto out_server_scope;
8769 calldata->args.state_protect.how = SP4_NONE;
8773 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8783 task_setup_data.rpc_xprt = xprt;
8784 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8785 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8786 sizeof(calldata->args.verifier.data));
8788 calldata->args.client = clp;
8789 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8790 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8791 #ifdef CONFIG_NFS_V4_1_MIGRATION
8792 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8794 if (test_bit(NFS_CS_DS, &clp->cl_flags))
8795 calldata->args.flags |= EXCHGID4_FLAG_USE_PNFS_DS;
8796 msg.rpc_argp = &calldata->args;
8797 msg.rpc_resp = &calldata->res;
8798 task_setup_data.callback_data = calldata;
8800 return rpc_run_task(&task_setup_data);
8803 kfree(calldata->res.impl_id);
8805 kfree(calldata->res.server_scope);
8807 kfree(calldata->res.server_owner);
8811 nfs_put_client(clp);
8812 return ERR_PTR(status);
8816 * _nfs4_proc_exchange_id()
8818 * Wrapper for EXCHANGE_ID operation.
8820 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8823 struct rpc_task *task;
8824 struct nfs41_exchange_id_args *argp;
8825 struct nfs41_exchange_id_res *resp;
8826 unsigned long now = jiffies;
8829 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8831 return PTR_ERR(task);
8833 argp = task->tk_msg.rpc_argp;
8834 resp = task->tk_msg.rpc_resp;
8835 status = task->tk_status;
8839 status = nfs4_check_cl_exchange_flags(resp->flags,
8840 clp->cl_mvops->minor_version);
8844 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8848 do_renew_lease(clp, now);
8850 clp->cl_clientid = resp->clientid;
8851 clp->cl_exchange_flags = resp->flags;
8852 clp->cl_seqid = resp->seqid;
8853 /* Client ID is not confirmed */
8854 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8855 clear_bit(NFS4_SESSION_ESTABLISHED,
8856 &clp->cl_session->session_state);
8858 if (clp->cl_serverscope != NULL &&
8859 !nfs41_same_server_scope(clp->cl_serverscope,
8860 resp->server_scope)) {
8861 dprintk("%s: server_scope mismatch detected\n",
8863 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8866 swap(clp->cl_serverowner, resp->server_owner);
8867 swap(clp->cl_serverscope, resp->server_scope);
8868 swap(clp->cl_implid, resp->impl_id);
8870 /* Save the EXCHANGE_ID verifier session trunk tests */
8871 memcpy(clp->cl_confirm.data, argp->verifier.data,
8872 sizeof(clp->cl_confirm.data));
8873 if (resp->flags & EXCHGID4_FLAG_USE_PNFS_DS)
8874 set_bit(NFS_CS_DS, &clp->cl_flags);
8876 trace_nfs4_exchange_id(clp, status);
8882 * nfs4_proc_exchange_id()
8884 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8886 * Since the clientid has expired, all compounds using sessions
8887 * associated with the stale clientid will be returning
8888 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8889 * be in some phase of session reset.
8891 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8893 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8895 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8898 /* try SP4_MACH_CRED if krb5i/p */
8899 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8900 authflavor == RPC_AUTH_GSS_KRB5P) {
8901 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8907 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8911 * nfs4_test_session_trunk
8913 * This is an add_xprt_test() test function called from
8914 * rpc_clnt_setup_test_and_add_xprt.
8916 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8917 * and is dereferrenced in nfs4_exchange_id_release
8919 * Upon success, add the new transport to the rpc_clnt
8921 * @clnt: struct rpc_clnt to get new transport
8922 * @xprt: the rpc_xprt to test
8923 * @data: call data for _nfs4_proc_exchange_id.
8925 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8928 struct nfs4_add_xprt_data *adata = data;
8929 struct rpc_task *task;
8934 dprintk("--> %s try %s\n", __func__,
8935 xprt->address_strings[RPC_DISPLAY_ADDR]);
8937 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8939 /* Test connection for session trunking. Async exchange_id call */
8940 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8944 status = task->tk_status;
8946 status = nfs4_detect_session_trunking(adata->clp,
8947 task->tk_msg.rpc_resp, xprt);
8950 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8951 else if (rpc_clnt_xprt_switch_has_addr(clnt,
8952 (struct sockaddr *)&xprt->addr))
8953 rpc_clnt_xprt_switch_remove_xprt(clnt, xprt);
8957 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8959 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8960 const struct cred *cred)
8962 struct rpc_message msg = {
8963 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8969 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8970 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8971 trace_nfs4_destroy_clientid(clp, status);
8973 dprintk("NFS: Got error %d from the server %s on "
8974 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8978 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8979 const struct cred *cred)
8984 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8985 ret = _nfs4_proc_destroy_clientid(clp, cred);
8987 case -NFS4ERR_DELAY:
8988 case -NFS4ERR_CLIENTID_BUSY:
8998 int nfs4_destroy_clientid(struct nfs_client *clp)
9000 const struct cred *cred;
9003 if (clp->cl_mvops->minor_version < 1)
9005 if (clp->cl_exchange_flags == 0)
9007 if (clp->cl_preserve_clid)
9009 cred = nfs4_get_clid_cred(clp);
9010 ret = nfs4_proc_destroy_clientid(clp, cred);
9014 case -NFS4ERR_STALE_CLIENTID:
9015 clp->cl_exchange_flags = 0;
9021 #endif /* CONFIG_NFS_V4_1 */
9023 struct nfs4_get_lease_time_data {
9024 struct nfs4_get_lease_time_args *args;
9025 struct nfs4_get_lease_time_res *res;
9026 struct nfs_client *clp;
9029 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
9032 struct nfs4_get_lease_time_data *data =
9033 (struct nfs4_get_lease_time_data *)calldata;
9035 /* just setup sequence, do not trigger session recovery
9036 since we're invoked within one */
9037 nfs4_setup_sequence(data->clp,
9038 &data->args->la_seq_args,
9039 &data->res->lr_seq_res,
9044 * Called from nfs4_state_manager thread for session setup, so don't recover
9045 * from sequence operation or clientid errors.
9047 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
9049 struct nfs4_get_lease_time_data *data =
9050 (struct nfs4_get_lease_time_data *)calldata;
9052 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
9054 switch (task->tk_status) {
9055 case -NFS4ERR_DELAY:
9056 case -NFS4ERR_GRACE:
9057 rpc_delay(task, NFS4_POLL_RETRY_MIN);
9058 task->tk_status = 0;
9060 case -NFS4ERR_RETRY_UNCACHED_REP:
9061 rpc_restart_call_prepare(task);
9066 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
9067 .rpc_call_prepare = nfs4_get_lease_time_prepare,
9068 .rpc_call_done = nfs4_get_lease_time_done,
9071 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
9073 struct nfs4_get_lease_time_args args;
9074 struct nfs4_get_lease_time_res res = {
9075 .lr_fsinfo = fsinfo,
9077 struct nfs4_get_lease_time_data data = {
9082 struct rpc_message msg = {
9083 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
9087 struct rpc_task_setup task_setup = {
9088 .rpc_client = clp->cl_rpcclient,
9089 .rpc_message = &msg,
9090 .callback_ops = &nfs4_get_lease_time_ops,
9091 .callback_data = &data,
9092 .flags = RPC_TASK_TIMEOUT,
9095 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
9096 return nfs4_call_sync_custom(&task_setup);
9099 #ifdef CONFIG_NFS_V4_1
9102 * Initialize the values to be used by the client in CREATE_SESSION
9103 * If nfs4_init_session set the fore channel request and response sizes,
9106 * Set the back channel max_resp_sz_cached to zero to force the client to
9107 * always set csa_cachethis to FALSE because the current implementation
9108 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
9110 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
9111 struct rpc_clnt *clnt)
9113 unsigned int max_rqst_sz, max_resp_sz;
9114 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
9115 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
9117 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
9118 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
9120 /* Fore channel attributes */
9121 args->fc_attrs.max_rqst_sz = max_rqst_sz;
9122 args->fc_attrs.max_resp_sz = max_resp_sz;
9123 args->fc_attrs.max_ops = NFS4_MAX_OPS;
9124 args->fc_attrs.max_reqs = max_session_slots;
9126 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
9127 "max_ops=%u max_reqs=%u\n",
9129 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
9130 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
9132 /* Back channel attributes */
9133 args->bc_attrs.max_rqst_sz = max_bc_payload;
9134 args->bc_attrs.max_resp_sz = max_bc_payload;
9135 args->bc_attrs.max_resp_sz_cached = 0;
9136 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
9137 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
9138 if (args->bc_attrs.max_reqs > max_bc_slots)
9139 args->bc_attrs.max_reqs = max_bc_slots;
9141 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
9142 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
9144 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
9145 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
9146 args->bc_attrs.max_reqs);
9149 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
9150 struct nfs41_create_session_res *res)
9152 struct nfs4_channel_attrs *sent = &args->fc_attrs;
9153 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
9155 if (rcvd->max_resp_sz > sent->max_resp_sz)
9158 * Our requested max_ops is the minimum we need; we're not
9159 * prepared to break up compounds into smaller pieces than that.
9160 * So, no point even trying to continue if the server won't
9163 if (rcvd->max_ops < sent->max_ops)
9165 if (rcvd->max_reqs == 0)
9167 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
9168 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
9172 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
9173 struct nfs41_create_session_res *res)
9175 struct nfs4_channel_attrs *sent = &args->bc_attrs;
9176 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
9178 if (!(res->flags & SESSION4_BACK_CHAN))
9180 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
9182 if (rcvd->max_resp_sz < sent->max_resp_sz)
9184 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
9186 if (rcvd->max_ops > sent->max_ops)
9188 if (rcvd->max_reqs > sent->max_reqs)
9194 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
9195 struct nfs41_create_session_res *res)
9199 ret = nfs4_verify_fore_channel_attrs(args, res);
9202 return nfs4_verify_back_channel_attrs(args, res);
9205 static void nfs4_update_session(struct nfs4_session *session,
9206 struct nfs41_create_session_res *res)
9208 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
9209 /* Mark client id and session as being confirmed */
9210 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
9211 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
9212 session->flags = res->flags;
9213 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
9214 if (res->flags & SESSION4_BACK_CHAN)
9215 memcpy(&session->bc_attrs, &res->bc_attrs,
9216 sizeof(session->bc_attrs));
9219 static int _nfs4_proc_create_session(struct nfs_client *clp,
9220 const struct cred *cred)
9222 struct nfs4_session *session = clp->cl_session;
9223 struct nfs41_create_session_args args = {
9225 .clientid = clp->cl_clientid,
9226 .seqid = clp->cl_seqid,
9227 .cb_program = NFS4_CALLBACK,
9229 struct nfs41_create_session_res res;
9231 struct rpc_message msg = {
9232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9239 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
9240 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
9242 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9243 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9244 trace_nfs4_create_session(clp, status);
9247 case -NFS4ERR_STALE_CLIENTID:
9248 case -NFS4ERR_DELAY:
9257 /* Verify the session's negotiated channel_attrs values */
9258 status = nfs4_verify_channel_attrs(&args, &res);
9259 /* Increment the clientid slot sequence id */
9262 nfs4_update_session(session, &res);
9269 * Issues a CREATE_SESSION operation to the server.
9270 * It is the responsibility of the caller to verify the session is
9271 * expired before calling this routine.
9273 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9277 struct nfs4_session *session = clp->cl_session;
9278 struct nfs4_add_xprt_data xprtdata = {
9281 struct rpc_add_xprt_test rpcdata = {
9282 .add_xprt_test = clp->cl_mvops->session_trunk,
9286 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9288 status = _nfs4_proc_create_session(clp, cred);
9292 /* Init or reset the session slot tables */
9293 status = nfs4_setup_session_slot_tables(session);
9294 dprintk("slot table setup returned %d\n", status);
9298 ptr = (unsigned *)&session->sess_id.data[0];
9299 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9300 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9301 rpc_clnt_probe_trunked_xprts(clp->cl_rpcclient, &rpcdata);
9307 * Issue the over-the-wire RPC DESTROY_SESSION.
9308 * The caller must serialize access to this routine.
9310 int nfs4_proc_destroy_session(struct nfs4_session *session,
9311 const struct cred *cred)
9313 struct rpc_message msg = {
9314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9315 .rpc_argp = session,
9320 /* session is still being setup */
9321 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9324 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9325 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9326 trace_nfs4_destroy_session(session->clp, status);
9329 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9330 "Session has been destroyed regardless...\n", status);
9331 rpc_clnt_manage_trunked_xprts(session->clp->cl_rpcclient);
9336 * Renew the cl_session lease.
9338 struct nfs4_sequence_data {
9339 struct nfs_client *clp;
9340 struct nfs4_sequence_args args;
9341 struct nfs4_sequence_res res;
9344 static void nfs41_sequence_release(void *data)
9346 struct nfs4_sequence_data *calldata = data;
9347 struct nfs_client *clp = calldata->clp;
9349 if (refcount_read(&clp->cl_count) > 1)
9350 nfs4_schedule_state_renewal(clp);
9351 nfs_put_client(clp);
9355 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9357 switch(task->tk_status) {
9358 case -NFS4ERR_DELAY:
9359 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9362 nfs4_schedule_lease_recovery(clp);
9367 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9369 struct nfs4_sequence_data *calldata = data;
9370 struct nfs_client *clp = calldata->clp;
9372 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9375 trace_nfs4_sequence(clp, task->tk_status);
9376 if (task->tk_status < 0 && !task->tk_client->cl_shutdown) {
9377 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9378 if (refcount_read(&clp->cl_count) == 1)
9381 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9382 rpc_restart_call_prepare(task);
9386 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9389 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9391 struct nfs4_sequence_data *calldata = data;
9392 struct nfs_client *clp = calldata->clp;
9393 struct nfs4_sequence_args *args;
9394 struct nfs4_sequence_res *res;
9396 args = task->tk_msg.rpc_argp;
9397 res = task->tk_msg.rpc_resp;
9399 nfs4_setup_sequence(clp, args, res, task);
9402 static const struct rpc_call_ops nfs41_sequence_ops = {
9403 .rpc_call_done = nfs41_sequence_call_done,
9404 .rpc_call_prepare = nfs41_sequence_prepare,
9405 .rpc_release = nfs41_sequence_release,
9408 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9409 const struct cred *cred,
9410 struct nfs4_slot *slot,
9413 struct nfs4_sequence_data *calldata;
9414 struct rpc_message msg = {
9415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9418 struct rpc_task_setup task_setup_data = {
9419 .rpc_client = clp->cl_rpcclient,
9420 .rpc_message = &msg,
9421 .callback_ops = &nfs41_sequence_ops,
9422 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
9424 struct rpc_task *ret;
9426 ret = ERR_PTR(-EIO);
9427 if (!refcount_inc_not_zero(&clp->cl_count))
9430 ret = ERR_PTR(-ENOMEM);
9431 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
9432 if (calldata == NULL)
9434 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9435 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9436 msg.rpc_argp = &calldata->args;
9437 msg.rpc_resp = &calldata->res;
9438 calldata->clp = clp;
9439 task_setup_data.callback_data = calldata;
9441 ret = rpc_run_task(&task_setup_data);
9446 nfs_put_client(clp);
9448 nfs41_release_slot(slot);
9452 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9454 struct rpc_task *task;
9457 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9459 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9461 ret = PTR_ERR(task);
9463 rpc_put_task_async(task);
9464 dprintk("<-- %s status=%d\n", __func__, ret);
9468 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9470 struct rpc_task *task;
9473 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9475 ret = PTR_ERR(task);
9478 ret = rpc_wait_for_completion_task(task);
9480 ret = task->tk_status;
9483 dprintk("<-- %s status=%d\n", __func__, ret);
9487 struct nfs4_reclaim_complete_data {
9488 struct nfs_client *clp;
9489 struct nfs41_reclaim_complete_args arg;
9490 struct nfs41_reclaim_complete_res res;
9493 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9495 struct nfs4_reclaim_complete_data *calldata = data;
9497 nfs4_setup_sequence(calldata->clp,
9498 &calldata->arg.seq_args,
9499 &calldata->res.seq_res,
9503 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9505 switch(task->tk_status) {
9507 wake_up_all(&clp->cl_lock_waitq);
9509 case -NFS4ERR_COMPLETE_ALREADY:
9510 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9512 case -NFS4ERR_DELAY:
9513 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9515 case -NFS4ERR_RETRY_UNCACHED_REP:
9517 dprintk("%s: failed to reclaim complete error %d for server %s, retrying\n",
9518 __func__, task->tk_status, clp->cl_hostname);
9520 case -NFS4ERR_BADSESSION:
9521 case -NFS4ERR_DEADSESSION:
9522 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9525 nfs4_schedule_lease_recovery(clp);
9530 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9532 struct nfs4_reclaim_complete_data *calldata = data;
9533 struct nfs_client *clp = calldata->clp;
9534 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9536 if (!nfs41_sequence_done(task, res))
9539 trace_nfs4_reclaim_complete(clp, task->tk_status);
9540 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9541 rpc_restart_call_prepare(task);
9546 static void nfs4_free_reclaim_complete_data(void *data)
9548 struct nfs4_reclaim_complete_data *calldata = data;
9553 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9554 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9555 .rpc_call_done = nfs4_reclaim_complete_done,
9556 .rpc_release = nfs4_free_reclaim_complete_data,
9560 * Issue a global reclaim complete.
9562 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9563 const struct cred *cred)
9565 struct nfs4_reclaim_complete_data *calldata;
9566 struct rpc_message msg = {
9567 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9570 struct rpc_task_setup task_setup_data = {
9571 .rpc_client = clp->cl_rpcclient,
9572 .rpc_message = &msg,
9573 .callback_ops = &nfs4_reclaim_complete_call_ops,
9574 .flags = RPC_TASK_NO_ROUND_ROBIN,
9576 int status = -ENOMEM;
9578 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9579 if (calldata == NULL)
9581 calldata->clp = clp;
9582 calldata->arg.one_fs = 0;
9584 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9585 msg.rpc_argp = &calldata->arg;
9586 msg.rpc_resp = &calldata->res;
9587 task_setup_data.callback_data = calldata;
9588 status = nfs4_call_sync_custom(&task_setup_data);
9590 dprintk("<-- %s status=%d\n", __func__, status);
9595 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9597 struct nfs4_layoutget *lgp = calldata;
9598 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9600 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9601 &lgp->res.seq_res, task);
9604 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9606 struct nfs4_layoutget *lgp = calldata;
9608 nfs41_sequence_process(task, &lgp->res.seq_res);
9612 nfs4_layoutget_handle_exception(struct rpc_task *task,
9613 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9615 struct inode *inode = lgp->args.inode;
9616 struct nfs_server *server = NFS_SERVER(inode);
9617 struct pnfs_layout_hdr *lo = lgp->lo;
9618 int nfs4err = task->tk_status;
9619 int err, status = 0;
9622 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9624 nfs4_sequence_free_slot(&lgp->res.seq_res);
9631 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9632 * on the file. set tk_status to -ENODATA to tell upper layer to
9635 case -NFS4ERR_LAYOUTUNAVAILABLE:
9639 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9640 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9642 case -NFS4ERR_BADLAYOUT:
9643 status = -EOVERFLOW;
9646 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9647 * (or clients) writing to the same RAID stripe except when
9648 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9650 * Treat it like we would RECALLCONFLICT -- we retry for a little
9651 * while, and then eventually give up.
9653 case -NFS4ERR_LAYOUTTRYLATER:
9654 if (lgp->args.minlength == 0) {
9655 status = -EOVERFLOW;
9660 case -NFS4ERR_RECALLCONFLICT:
9661 status = -ERECALLCONFLICT;
9663 case -NFS4ERR_DELEG_REVOKED:
9664 case -NFS4ERR_ADMIN_REVOKED:
9665 case -NFS4ERR_EXPIRED:
9666 case -NFS4ERR_BAD_STATEID:
9667 exception->timeout = 0;
9668 spin_lock(&inode->i_lock);
9669 /* If the open stateid was bad, then recover it. */
9670 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9671 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9672 spin_unlock(&inode->i_lock);
9673 exception->state = lgp->args.ctx->state;
9674 exception->stateid = &lgp->args.stateid;
9679 * Mark the bad layout state as invalid, then retry
9681 pnfs_mark_layout_stateid_invalid(lo, &head);
9682 spin_unlock(&inode->i_lock);
9683 nfs_commit_inode(inode, 0);
9684 pnfs_free_lseg_list(&head);
9689 err = nfs4_handle_exception(server, nfs4err, exception);
9691 if (exception->retry)
9700 size_t max_response_pages(struct nfs_server *server)
9702 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9703 return nfs_page_array_len(0, max_resp_sz);
9706 static void nfs4_layoutget_release(void *calldata)
9708 struct nfs4_layoutget *lgp = calldata;
9710 nfs4_sequence_free_slot(&lgp->res.seq_res);
9711 pnfs_layoutget_free(lgp);
9714 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9715 .rpc_call_prepare = nfs4_layoutget_prepare,
9716 .rpc_call_done = nfs4_layoutget_done,
9717 .rpc_release = nfs4_layoutget_release,
9720 struct pnfs_layout_segment *
9721 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9723 struct inode *inode = lgp->args.inode;
9724 struct nfs_server *server = NFS_SERVER(inode);
9725 struct rpc_task *task;
9726 struct rpc_message msg = {
9727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9728 .rpc_argp = &lgp->args,
9729 .rpc_resp = &lgp->res,
9730 .rpc_cred = lgp->cred,
9732 struct rpc_task_setup task_setup_data = {
9733 .rpc_client = server->client,
9734 .rpc_message = &msg,
9735 .callback_ops = &nfs4_layoutget_call_ops,
9736 .callback_data = lgp,
9737 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
9740 struct pnfs_layout_segment *lseg = NULL;
9741 struct nfs4_exception exception = {
9743 .timeout = *timeout,
9747 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9749 task = rpc_run_task(&task_setup_data);
9751 return ERR_CAST(task);
9753 status = rpc_wait_for_completion_task(task);
9757 if (task->tk_status < 0) {
9758 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9759 *timeout = exception.timeout;
9760 } else if (lgp->res.layoutp->len == 0) {
9762 *timeout = nfs4_update_delay(&exception.timeout);
9764 lseg = pnfs_layout_process(lgp);
9766 trace_nfs4_layoutget(lgp->args.ctx,
9773 dprintk("<-- %s status=%d\n", __func__, status);
9775 return ERR_PTR(status);
9780 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9782 struct nfs4_layoutreturn *lrp = calldata;
9784 nfs4_setup_sequence(lrp->clp,
9785 &lrp->args.seq_args,
9788 if (!pnfs_layout_is_valid(lrp->args.layout))
9792 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9794 struct nfs4_layoutreturn *lrp = calldata;
9795 struct nfs_server *server;
9797 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9801 * Was there an RPC level error? Assume the call succeeded,
9802 * and that we need to release the layout
9804 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9805 lrp->res.lrs_present = 0;
9809 server = NFS_SERVER(lrp->args.inode);
9810 switch (task->tk_status) {
9811 case -NFS4ERR_OLD_STATEID:
9812 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9818 task->tk_status = 0;
9822 case -NFS4ERR_DELAY:
9823 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9829 task->tk_status = 0;
9830 nfs4_sequence_free_slot(&lrp->res.seq_res);
9831 rpc_restart_call_prepare(task);
9834 static void nfs4_layoutreturn_release(void *calldata)
9836 struct nfs4_layoutreturn *lrp = calldata;
9837 struct pnfs_layout_hdr *lo = lrp->args.layout;
9839 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9840 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9841 nfs4_sequence_free_slot(&lrp->res.seq_res);
9842 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9843 lrp->ld_private.ops->free(&lrp->ld_private);
9844 pnfs_put_layout_hdr(lrp->args.layout);
9845 nfs_iput_and_deactive(lrp->inode);
9846 put_cred(lrp->cred);
9850 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9851 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9852 .rpc_call_done = nfs4_layoutreturn_done,
9853 .rpc_release = nfs4_layoutreturn_release,
9856 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9858 struct rpc_task *task;
9859 struct rpc_message msg = {
9860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9861 .rpc_argp = &lrp->args,
9862 .rpc_resp = &lrp->res,
9863 .rpc_cred = lrp->cred,
9865 struct rpc_task_setup task_setup_data = {
9866 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9867 .rpc_message = &msg,
9868 .callback_ops = &nfs4_layoutreturn_call_ops,
9869 .callback_data = lrp,
9870 .flags = RPC_TASK_MOVEABLE,
9874 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9875 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9876 &task_setup_data.rpc_client, &msg);
9878 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9881 nfs4_layoutreturn_release(lrp);
9884 task_setup_data.flags |= RPC_TASK_ASYNC;
9887 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9890 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9892 task = rpc_run_task(&task_setup_data);
9894 return PTR_ERR(task);
9896 status = task->tk_status;
9897 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9898 dprintk("<-- %s status=%d\n", __func__, status);
9904 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9905 struct pnfs_device *pdev,
9906 const struct cred *cred)
9908 struct nfs4_getdeviceinfo_args args = {
9910 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9911 NOTIFY_DEVICEID4_DELETE,
9913 struct nfs4_getdeviceinfo_res res = {
9916 struct rpc_message msg = {
9917 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9924 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9925 if (res.notification & ~args.notify_types)
9926 dprintk("%s: unsupported notification\n", __func__);
9927 if (res.notification != args.notify_types)
9930 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9932 dprintk("<-- %s status=%d\n", __func__, status);
9937 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9938 struct pnfs_device *pdev,
9939 const struct cred *cred)
9941 struct nfs4_exception exception = { };
9945 err = nfs4_handle_exception(server,
9946 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9948 } while (exception.retry);
9951 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9953 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9955 struct nfs4_layoutcommit_data *data = calldata;
9956 struct nfs_server *server = NFS_SERVER(data->args.inode);
9958 nfs4_setup_sequence(server->nfs_client,
9959 &data->args.seq_args,
9965 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9967 struct nfs4_layoutcommit_data *data = calldata;
9968 struct nfs_server *server = NFS_SERVER(data->args.inode);
9970 if (!nfs41_sequence_done(task, &data->res.seq_res))
9973 switch (task->tk_status) { /* Just ignore these failures */
9974 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9975 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9976 case -NFS4ERR_BADLAYOUT: /* no layout */
9977 case -NFS4ERR_GRACE: /* loca_recalim always false */
9978 task->tk_status = 0;
9983 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9984 rpc_restart_call_prepare(task);
9990 static void nfs4_layoutcommit_release(void *calldata)
9992 struct nfs4_layoutcommit_data *data = calldata;
9994 pnfs_cleanup_layoutcommit(data);
9995 nfs_post_op_update_inode_force_wcc(data->args.inode,
9997 put_cred(data->cred);
9998 nfs_iput_and_deactive(data->inode);
10002 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
10003 .rpc_call_prepare = nfs4_layoutcommit_prepare,
10004 .rpc_call_done = nfs4_layoutcommit_done,
10005 .rpc_release = nfs4_layoutcommit_release,
10009 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
10011 struct rpc_message msg = {
10012 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
10013 .rpc_argp = &data->args,
10014 .rpc_resp = &data->res,
10015 .rpc_cred = data->cred,
10017 struct rpc_task_setup task_setup_data = {
10018 .task = &data->task,
10019 .rpc_client = NFS_CLIENT(data->args.inode),
10020 .rpc_message = &msg,
10021 .callback_ops = &nfs4_layoutcommit_ops,
10022 .callback_data = data,
10023 .flags = RPC_TASK_MOVEABLE,
10025 struct rpc_task *task;
10028 dprintk("NFS: initiating layoutcommit call. sync %d "
10029 "lbw: %llu inode %lu\n", sync,
10030 data->args.lastbytewritten,
10031 data->args.inode->i_ino);
10034 data->inode = nfs_igrab_and_active(data->args.inode);
10035 if (data->inode == NULL) {
10036 nfs4_layoutcommit_release(data);
10039 task_setup_data.flags = RPC_TASK_ASYNC;
10041 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
10042 task = rpc_run_task(&task_setup_data);
10044 return PTR_ERR(task);
10046 status = task->tk_status;
10047 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
10048 dprintk("%s: status %d\n", __func__, status);
10049 rpc_put_task(task);
10054 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
10055 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
10058 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10059 struct nfs_fsinfo *info,
10060 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
10062 struct nfs41_secinfo_no_name_args args = {
10063 .style = SECINFO_STYLE_CURRENT_FH,
10065 struct nfs4_secinfo_res res = {
10066 .flavors = flavors,
10068 struct rpc_message msg = {
10069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
10073 struct nfs4_call_sync_data data = {
10074 .seq_server = server,
10075 .seq_args = &args.seq_args,
10076 .seq_res = &res.seq_res,
10078 struct rpc_task_setup task_setup = {
10079 .rpc_client = server->client,
10080 .rpc_message = &msg,
10081 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
10082 .callback_data = &data,
10083 .flags = RPC_TASK_NO_ROUND_ROBIN,
10085 const struct cred *cred = NULL;
10088 if (use_integrity) {
10089 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
10091 cred = nfs4_get_clid_cred(server->nfs_client);
10092 msg.rpc_cred = cred;
10095 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
10096 status = nfs4_call_sync_custom(&task_setup);
10097 dprintk("<-- %s status=%d\n", __func__, status);
10105 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10106 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
10108 struct nfs4_exception exception = {
10109 .interruptible = true,
10113 /* first try using integrity protection */
10114 err = -NFS4ERR_WRONGSEC;
10116 /* try to use integrity protection with machine cred */
10117 if (_nfs4_is_integrity_protected(server->nfs_client))
10118 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10122 * if unable to use integrity protection, or SECINFO with
10123 * integrity protection returns NFS4ERR_WRONGSEC (which is
10124 * disallowed by spec, but exists in deployed servers) use
10125 * the current filesystem's rpc_client and the user cred.
10127 if (err == -NFS4ERR_WRONGSEC)
10128 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10133 case -NFS4ERR_WRONGSEC:
10137 err = nfs4_handle_exception(server, err, &exception);
10139 } while (exception.retry);
10145 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
10146 struct nfs_fsinfo *info)
10150 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
10151 struct nfs4_secinfo_flavors *flavors;
10152 struct nfs4_secinfo4 *secinfo;
10155 page = alloc_page(GFP_KERNEL);
10161 flavors = page_address(page);
10162 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
10165 * Fall back on "guess and check" method if
10166 * the server doesn't support SECINFO_NO_NAME
10168 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
10169 err = nfs4_find_root_sec(server, fhandle, info);
10175 for (i = 0; i < flavors->num_flavors; i++) {
10176 secinfo = &flavors->flavors[i];
10178 switch (secinfo->flavor) {
10179 case RPC_AUTH_NULL:
10180 case RPC_AUTH_UNIX:
10182 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
10183 &secinfo->flavor_info);
10186 flavor = RPC_AUTH_MAXFLAVOR;
10190 if (!nfs_auth_info_match(&server->auth_info, flavor))
10191 flavor = RPC_AUTH_MAXFLAVOR;
10193 if (flavor != RPC_AUTH_MAXFLAVOR) {
10194 err = nfs4_lookup_root_sec(server, fhandle,
10201 if (flavor == RPC_AUTH_MAXFLAVOR)
10206 if (err == -EACCES)
10212 static int _nfs41_test_stateid(struct nfs_server *server,
10213 nfs4_stateid *stateid,
10214 const struct cred *cred)
10217 struct nfs41_test_stateid_args args = {
10218 .stateid = stateid,
10220 struct nfs41_test_stateid_res res;
10221 struct rpc_message msg = {
10222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10227 struct rpc_clnt *rpc_client = server->client;
10229 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10230 &rpc_client, &msg);
10232 dprintk("NFS call test_stateid %p\n", stateid);
10233 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
10234 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
10235 &args.seq_args, &res.seq_res);
10236 if (status != NFS_OK) {
10237 dprintk("NFS reply test_stateid: failed, %d\n", status);
10240 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10241 return -res.status;
10244 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10245 int err, struct nfs4_exception *exception)
10247 exception->retry = 0;
10249 case -NFS4ERR_DELAY:
10250 case -NFS4ERR_RETRY_UNCACHED_REP:
10251 nfs4_handle_exception(server, err, exception);
10253 case -NFS4ERR_BADSESSION:
10254 case -NFS4ERR_BADSLOT:
10255 case -NFS4ERR_BAD_HIGH_SLOT:
10256 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10257 case -NFS4ERR_DEADSESSION:
10258 nfs4_do_handle_exception(server, err, exception);
10263 * nfs41_test_stateid - perform a TEST_STATEID operation
10265 * @server: server / transport on which to perform the operation
10266 * @stateid: state ID to test
10267 * @cred: credential
10269 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10270 * Otherwise a negative NFS4ERR value is returned if the operation
10271 * failed or the state ID is not currently valid.
10273 static int nfs41_test_stateid(struct nfs_server *server,
10274 nfs4_stateid *stateid,
10275 const struct cred *cred)
10277 struct nfs4_exception exception = {
10278 .interruptible = true,
10282 err = _nfs41_test_stateid(server, stateid, cred);
10283 nfs4_handle_delay_or_session_error(server, err, &exception);
10284 } while (exception.retry);
10288 struct nfs_free_stateid_data {
10289 struct nfs_server *server;
10290 struct nfs41_free_stateid_args args;
10291 struct nfs41_free_stateid_res res;
10294 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10296 struct nfs_free_stateid_data *data = calldata;
10297 nfs4_setup_sequence(data->server->nfs_client,
10298 &data->args.seq_args,
10299 &data->res.seq_res,
10303 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10305 struct nfs_free_stateid_data *data = calldata;
10307 nfs41_sequence_done(task, &data->res.seq_res);
10309 switch (task->tk_status) {
10310 case -NFS4ERR_DELAY:
10311 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10312 rpc_restart_call_prepare(task);
10316 static void nfs41_free_stateid_release(void *calldata)
10318 struct nfs_free_stateid_data *data = calldata;
10319 struct nfs_client *clp = data->server->nfs_client;
10321 nfs_put_client(clp);
10325 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10326 .rpc_call_prepare = nfs41_free_stateid_prepare,
10327 .rpc_call_done = nfs41_free_stateid_done,
10328 .rpc_release = nfs41_free_stateid_release,
10332 * nfs41_free_stateid - perform a FREE_STATEID operation
10334 * @server: server / transport on which to perform the operation
10335 * @stateid: state ID to release
10336 * @cred: credential
10337 * @privileged: set to true if this call needs to be privileged
10339 * Note: this function is always asynchronous.
10341 static int nfs41_free_stateid(struct nfs_server *server,
10342 const nfs4_stateid *stateid,
10343 const struct cred *cred,
10346 struct rpc_message msg = {
10347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10350 struct rpc_task_setup task_setup = {
10351 .rpc_client = server->client,
10352 .rpc_message = &msg,
10353 .callback_ops = &nfs41_free_stateid_ops,
10354 .flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
10356 struct nfs_free_stateid_data *data;
10357 struct rpc_task *task;
10358 struct nfs_client *clp = server->nfs_client;
10360 if (!refcount_inc_not_zero(&clp->cl_count))
10363 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10364 &task_setup.rpc_client, &msg);
10366 dprintk("NFS call free_stateid %p\n", stateid);
10367 data = kmalloc(sizeof(*data), GFP_KERNEL);
10370 data->server = server;
10371 nfs4_stateid_copy(&data->args.stateid, stateid);
10373 task_setup.callback_data = data;
10375 msg.rpc_argp = &data->args;
10376 msg.rpc_resp = &data->res;
10377 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10378 task = rpc_run_task(&task_setup);
10380 return PTR_ERR(task);
10381 rpc_put_task(task);
10386 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10388 const struct cred *cred = lsp->ls_state->owner->so_cred;
10390 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10391 nfs4_free_lock_state(server, lsp);
10394 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10395 const nfs4_stateid *s2)
10397 if (s1->type != s2->type)
10400 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10403 if (s1->seqid == s2->seqid)
10406 return s1->seqid == 0 || s2->seqid == 0;
10409 #endif /* CONFIG_NFS_V4_1 */
10411 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10412 const nfs4_stateid *s2)
10414 return nfs4_stateid_match(s1, s2);
10418 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10419 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10420 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10421 .recover_open = nfs4_open_reclaim,
10422 .recover_lock = nfs4_lock_reclaim,
10423 .establish_clid = nfs4_init_clientid,
10424 .detect_trunking = nfs40_discover_server_trunking,
10427 #if defined(CONFIG_NFS_V4_1)
10428 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10429 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10430 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10431 .recover_open = nfs4_open_reclaim,
10432 .recover_lock = nfs4_lock_reclaim,
10433 .establish_clid = nfs41_init_clientid,
10434 .reclaim_complete = nfs41_proc_reclaim_complete,
10435 .detect_trunking = nfs41_discover_server_trunking,
10437 #endif /* CONFIG_NFS_V4_1 */
10439 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10440 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10441 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10442 .recover_open = nfs40_open_expired,
10443 .recover_lock = nfs4_lock_expired,
10444 .establish_clid = nfs4_init_clientid,
10447 #if defined(CONFIG_NFS_V4_1)
10448 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10449 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10450 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10451 .recover_open = nfs41_open_expired,
10452 .recover_lock = nfs41_lock_expired,
10453 .establish_clid = nfs41_init_clientid,
10455 #endif /* CONFIG_NFS_V4_1 */
10457 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10458 .sched_state_renewal = nfs4_proc_async_renew,
10459 .get_state_renewal_cred = nfs4_get_renew_cred,
10460 .renew_lease = nfs4_proc_renew,
10463 #if defined(CONFIG_NFS_V4_1)
10464 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10465 .sched_state_renewal = nfs41_proc_async_sequence,
10466 .get_state_renewal_cred = nfs4_get_machine_cred,
10467 .renew_lease = nfs4_proc_sequence,
10471 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10472 .get_locations = _nfs40_proc_get_locations,
10473 .fsid_present = _nfs40_proc_fsid_present,
10476 #if defined(CONFIG_NFS_V4_1)
10477 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10478 .get_locations = _nfs41_proc_get_locations,
10479 .fsid_present = _nfs41_proc_fsid_present,
10481 #endif /* CONFIG_NFS_V4_1 */
10483 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10484 .minor_version = 0,
10485 .init_caps = NFS_CAP_READDIRPLUS
10486 | NFS_CAP_ATOMIC_OPEN
10487 | NFS_CAP_POSIX_LOCK,
10488 .init_client = nfs40_init_client,
10489 .shutdown_client = nfs40_shutdown_client,
10490 .match_stateid = nfs4_match_stateid,
10491 .find_root_sec = nfs4_find_root_sec,
10492 .free_lock_state = nfs4_release_lockowner,
10493 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10494 .alloc_seqid = nfs_alloc_seqid,
10495 .call_sync_ops = &nfs40_call_sync_ops,
10496 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10497 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10498 .state_renewal_ops = &nfs40_state_renewal_ops,
10499 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10502 #if defined(CONFIG_NFS_V4_1)
10503 static struct nfs_seqid *
10504 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10509 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10510 .minor_version = 1,
10511 .init_caps = NFS_CAP_READDIRPLUS
10512 | NFS_CAP_ATOMIC_OPEN
10513 | NFS_CAP_POSIX_LOCK
10514 | NFS_CAP_STATEID_NFSV41
10515 | NFS_CAP_ATOMIC_OPEN_V1
10517 | NFS_CAP_MOVEABLE,
10518 .init_client = nfs41_init_client,
10519 .shutdown_client = nfs41_shutdown_client,
10520 .match_stateid = nfs41_match_stateid,
10521 .find_root_sec = nfs41_find_root_sec,
10522 .free_lock_state = nfs41_free_lock_state,
10523 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10524 .alloc_seqid = nfs_alloc_no_seqid,
10525 .session_trunk = nfs4_test_session_trunk,
10526 .call_sync_ops = &nfs41_call_sync_ops,
10527 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10528 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10529 .state_renewal_ops = &nfs41_state_renewal_ops,
10530 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10534 #if defined(CONFIG_NFS_V4_2)
10535 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10536 .minor_version = 2,
10537 .init_caps = NFS_CAP_READDIRPLUS
10538 | NFS_CAP_ATOMIC_OPEN
10539 | NFS_CAP_POSIX_LOCK
10540 | NFS_CAP_STATEID_NFSV41
10541 | NFS_CAP_ATOMIC_OPEN_V1
10545 | NFS_CAP_OFFLOAD_CANCEL
10546 | NFS_CAP_COPY_NOTIFY
10547 | NFS_CAP_DEALLOCATE
10549 | NFS_CAP_LAYOUTSTATS
10551 | NFS_CAP_LAYOUTERROR
10552 | NFS_CAP_READ_PLUS
10553 | NFS_CAP_MOVEABLE,
10554 .init_client = nfs41_init_client,
10555 .shutdown_client = nfs41_shutdown_client,
10556 .match_stateid = nfs41_match_stateid,
10557 .find_root_sec = nfs41_find_root_sec,
10558 .free_lock_state = nfs41_free_lock_state,
10559 .call_sync_ops = &nfs41_call_sync_ops,
10560 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10561 .alloc_seqid = nfs_alloc_no_seqid,
10562 .session_trunk = nfs4_test_session_trunk,
10563 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10564 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10565 .state_renewal_ops = &nfs41_state_renewal_ops,
10566 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10570 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10571 [0] = &nfs_v4_0_minor_ops,
10572 #if defined(CONFIG_NFS_V4_1)
10573 [1] = &nfs_v4_1_minor_ops,
10575 #if defined(CONFIG_NFS_V4_2)
10576 [2] = &nfs_v4_2_minor_ops,
10580 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10582 ssize_t error, error2, error3;
10584 error = generic_listxattr(dentry, list, size);
10592 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10601 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10605 return error + error2 + error3;
10608 static void nfs4_enable_swap(struct inode *inode)
10610 /* The state manager thread must always be running.
10611 * It will notice the client is a swapper, and stay put.
10613 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10615 nfs4_schedule_state_manager(clp);
10618 static void nfs4_disable_swap(struct inode *inode)
10620 /* The state manager thread will now exit once it is
10623 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10625 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
10626 clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
10627 wake_up_var(&clp->cl_state);
10630 static const struct inode_operations nfs4_dir_inode_operations = {
10631 .create = nfs_create,
10632 .lookup = nfs_lookup,
10633 .atomic_open = nfs_atomic_open,
10635 .unlink = nfs_unlink,
10636 .symlink = nfs_symlink,
10637 .mkdir = nfs_mkdir,
10638 .rmdir = nfs_rmdir,
10639 .mknod = nfs_mknod,
10640 .rename = nfs_rename,
10641 .permission = nfs_permission,
10642 .getattr = nfs_getattr,
10643 .setattr = nfs_setattr,
10644 .listxattr = nfs4_listxattr,
10647 static const struct inode_operations nfs4_file_inode_operations = {
10648 .permission = nfs_permission,
10649 .getattr = nfs_getattr,
10650 .setattr = nfs_setattr,
10651 .listxattr = nfs4_listxattr,
10654 const struct nfs_rpc_ops nfs_v4_clientops = {
10655 .version = 4, /* protocol version */
10656 .dentry_ops = &nfs4_dentry_operations,
10657 .dir_inode_ops = &nfs4_dir_inode_operations,
10658 .file_inode_ops = &nfs4_file_inode_operations,
10659 .file_ops = &nfs4_file_operations,
10660 .getroot = nfs4_proc_get_root,
10661 .submount = nfs4_submount,
10662 .try_get_tree = nfs4_try_get_tree,
10663 .getattr = nfs4_proc_getattr,
10664 .setattr = nfs4_proc_setattr,
10665 .lookup = nfs4_proc_lookup,
10666 .lookupp = nfs4_proc_lookupp,
10667 .access = nfs4_proc_access,
10668 .readlink = nfs4_proc_readlink,
10669 .create = nfs4_proc_create,
10670 .remove = nfs4_proc_remove,
10671 .unlink_setup = nfs4_proc_unlink_setup,
10672 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10673 .unlink_done = nfs4_proc_unlink_done,
10674 .rename_setup = nfs4_proc_rename_setup,
10675 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10676 .rename_done = nfs4_proc_rename_done,
10677 .link = nfs4_proc_link,
10678 .symlink = nfs4_proc_symlink,
10679 .mkdir = nfs4_proc_mkdir,
10680 .rmdir = nfs4_proc_rmdir,
10681 .readdir = nfs4_proc_readdir,
10682 .mknod = nfs4_proc_mknod,
10683 .statfs = nfs4_proc_statfs,
10684 .fsinfo = nfs4_proc_fsinfo,
10685 .pathconf = nfs4_proc_pathconf,
10686 .set_capabilities = nfs4_server_capabilities,
10687 .decode_dirent = nfs4_decode_dirent,
10688 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10689 .read_setup = nfs4_proc_read_setup,
10690 .read_done = nfs4_read_done,
10691 .write_setup = nfs4_proc_write_setup,
10692 .write_done = nfs4_write_done,
10693 .commit_setup = nfs4_proc_commit_setup,
10694 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10695 .commit_done = nfs4_commit_done,
10696 .lock = nfs4_proc_lock,
10697 .clear_acl_cache = nfs4_zap_acl_attr,
10698 .close_context = nfs4_close_context,
10699 .open_context = nfs4_atomic_open,
10700 .have_delegation = nfs4_have_delegation,
10701 .alloc_client = nfs4_alloc_client,
10702 .init_client = nfs4_init_client,
10703 .free_client = nfs4_free_client,
10704 .create_server = nfs4_create_server,
10705 .clone_server = nfs_clone_server,
10706 .discover_trunking = nfs4_discover_trunking,
10707 .enable_swap = nfs4_enable_swap,
10708 .disable_swap = nfs4_disable_swap,
10711 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10712 .name = XATTR_NAME_NFSV4_ACL,
10713 .list = nfs4_xattr_list_nfs4_acl,
10714 .get = nfs4_xattr_get_nfs4_acl,
10715 .set = nfs4_xattr_set_nfs4_acl,
10718 #if defined(CONFIG_NFS_V4_1)
10719 static const struct xattr_handler nfs4_xattr_nfs4_dacl_handler = {
10720 .name = XATTR_NAME_NFSV4_DACL,
10721 .list = nfs4_xattr_list_nfs4_dacl,
10722 .get = nfs4_xattr_get_nfs4_dacl,
10723 .set = nfs4_xattr_set_nfs4_dacl,
10726 static const struct xattr_handler nfs4_xattr_nfs4_sacl_handler = {
10727 .name = XATTR_NAME_NFSV4_SACL,
10728 .list = nfs4_xattr_list_nfs4_sacl,
10729 .get = nfs4_xattr_get_nfs4_sacl,
10730 .set = nfs4_xattr_set_nfs4_sacl,
10734 #ifdef CONFIG_NFS_V4_2
10735 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10736 .prefix = XATTR_USER_PREFIX,
10737 .get = nfs4_xattr_get_nfs4_user,
10738 .set = nfs4_xattr_set_nfs4_user,
10742 const struct xattr_handler *nfs4_xattr_handlers[] = {
10743 &nfs4_xattr_nfs4_acl_handler,
10744 #if defined(CONFIG_NFS_V4_1)
10745 &nfs4_xattr_nfs4_dacl_handler,
10746 &nfs4_xattr_nfs4_sacl_handler,
10748 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10749 &nfs4_xattr_nfs4_label_handler,
10751 #ifdef CONFIG_NFS_V4_2
10752 &nfs4_xattr_nfs4_user_handler,